00001
00002 #define ROOTHISTCNV_H2DCNV_CPP
00003
00004
00005
00006 #include <iostream>
00007 #include "GaudiKernel/MsgStream.h"
00008 #include "GaudiKernel/CnvFactory.h"
00009 #include "GaudiKernel/GenericAddress.h"
00010 #include "GaudiKernel/DataObject.h"
00011 #include "GaudiKernel/IHistogram2D.h"
00012 #include "GaudiKernel/IAxis.h"
00013
00014 #include "H2DCnv.h"
00015 #include "RootDirFcn.h"
00016
00017 #include "TH2.h"
00018 #include "TFile.h"
00019 #include <list>
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 static CnvFactory<RootHistCnv::H2DCnv> s_factory;
00033 const ICnvFactory& RootHistH2DCnvFactory = s_factory;
00034
00035
00036 namespace RootHistCnv {
00037
00039 StatusCode H2DCnv::createRep(DataObject* pObject, IOpaqueAddress*& refpAddress) {
00040
00041 IHistogram2D* h = dynamic_cast<IHistogram2D*>(pObject);
00042 refpAddress = 0;
00043
00044 MsgStream log(msgSvc(), "RootHistCnv::H2DCnv");
00045
00046 if ( 0 != h ) {
00047
00048 std::string temp = h->title();
00049
00050 int sep = temp.find('|');
00051 if ( 0 >= sep ) {
00052
00053 log << MSG::ERROR << "Cannot separate histogram title and identifier" << endreq;
00054 log << MSG::ERROR << "while converting the histogram: " << temp << endreq;
00055 return StatusCode::FAILURE;
00056 }
00057
00058 std::string histoID( temp, 0, sep );
00059 std::string title( temp, sep+1, temp.length() );
00060
00061 int nBinX = h->xAxis()->bins();
00062 float xLow = h->xAxis()->lowerEdge();
00063 float xHigh = h->xAxis()->upperEdge();
00064 float x_uflow = xLow - 1.0;
00065 float x_oflow = xHigh + 1.0;
00066
00067 int nBinY = h->yAxis()->bins();
00068 float yLow = h->yAxis()->lowerEdge();
00069 float yHigh = h->yAxis()->upperEdge();
00070 float y_uflow = yLow - 1.0;
00071 float y_oflow = yHigh + 1.0;
00072
00073 int entries = 0;
00074 float height = 0.;
00075 float centreX = 0.;
00076 float centreY = 0.;
00077 float weight = 0.;
00078 int ix = 0;
00079 int iy = 0;
00080 int j = 0;
00081
00082 Stat_t* errs = new Stat_t[nBinX*nBinY];
00083 int ind = 0;
00084
00085 float x_mean = xLow + (xHigh-xLow)/2.0;
00086 float y_mean = yLow + (yHigh-yLow)/2.0;
00087
00088
00089 std::string full = pObject->fullpath();
00090
00091 std::string dir = pObject->location();
00092 RootTrimLeadingDir(dir,"/stat");
00093 RootMkdir(dir);
00094 RootCd(dir);
00095
00096 log << MSG::INFO << "creating hist id: " << histoID
00097 << " title: " << title << " in " << dir << endreq;
00098
00099 TH2F *rhist = new TH2F(histoID.c_str(), title.c_str(),
00100 nBinX, xLow, xHigh, nBinY, yLow, yHigh);
00101
00102
00103
00104 for ( ix = 0; ix < nBinX; ix++ ) {
00105 for ( iy = 0; iy < nBinY; iy++, ind++ ) {
00106 errs[ind] = h->binError(ix,iy);
00107 height = h->binHeight(ix,iy);
00108 if( 0 != height ) {
00109 centreX = h->xAxis()->binCentre(ix);
00110 centreY = h->yAxis()->binCentre(iy);
00111 entries = h->binEntries(ix,iy);
00112 weight = height / entries;
00113 for( j = 0; j < entries; j++ ) {
00114 rhist->Fill(centreX, centreY, weight);
00115 }
00116 }
00117 }
00118 }
00119
00120 rhist->SetError( errs );
00121
00122
00123
00124 entries = h->binEntriesX( IHistogram::UNDERFLOW_BIN );
00125 height = h->binHeightX ( IHistogram::UNDERFLOW_BIN );
00126 weight = height / entries;
00127 for( j = 0; j < entries; j++ ) {
00128 rhist->Fill(x_uflow,y_mean,weight);
00129 }
00130
00131 entries = h->binEntries ( IHistogram::UNDERFLOW_BIN, IHistogram::OVERFLOW_BIN );
00132 height = h->binHeight ( IHistogram::UNDERFLOW_BIN, IHistogram::OVERFLOW_BIN );
00133 weight = height / entries;
00134 for( j = 0; j < entries; j++ ) {
00135 rhist->Fill(x_uflow, y_oflow, weight);
00136 }
00137
00138 entries = h->binEntriesY( IHistogram::OVERFLOW_BIN );
00139 height = h->binHeightY ( IHistogram::OVERFLOW_BIN );
00140 weight = height / entries;
00141 for( j = 0; j < entries; j++ ) {
00142 rhist->Fill(x_mean, y_oflow, weight);
00143 }
00144
00145 entries = h->binEntries ( IHistogram::OVERFLOW_BIN, IHistogram::OVERFLOW_BIN );
00146 height = h->binHeight ( IHistogram::OVERFLOW_BIN, IHistogram::OVERFLOW_BIN );
00147 weight = height / entries;
00148 for( j = 0; j < entries; j++ ) {
00149 rhist->Fill(x_oflow, y_oflow, weight);
00150 }
00151
00152 entries = h->binEntriesX( IHistogram::OVERFLOW_BIN );
00153 height = h->binHeightX ( IHistogram::OVERFLOW_BIN );
00154 weight = height / entries;
00155 for( j = 0; j < entries; j++ ) {
00156 rhist->Fill(x_oflow, y_mean, weight);
00157 }
00158
00159 entries = h->binEntries ( IHistogram::OVERFLOW_BIN, IHistogram::UNDERFLOW_BIN );
00160 height = h->binHeight ( IHistogram::OVERFLOW_BIN, IHistogram::UNDERFLOW_BIN );
00161 weight = height / entries;
00162 for( j = 0; j < entries; j++ ) {
00163 rhist->Fill(x_oflow, y_uflow, weight);
00164 }
00165
00166 entries = h->binEntriesY( IHistogram::UNDERFLOW_BIN );
00167 height = h->binHeightY ( IHistogram::UNDERFLOW_BIN );
00168 weight = height / entries;
00169 for( j = 0; j < entries; j++ ) {
00170 rhist->Fill(x_mean, y_uflow, weight);
00171 }
00172
00173 entries = h->binEntries ( IHistogram::UNDERFLOW_BIN, IHistogram::UNDERFLOW_BIN );
00174 height = h->binHeight ( IHistogram::UNDERFLOW_BIN, IHistogram::UNDERFLOW_BIN );
00175 weight = height / entries;
00176 for( j = 0; j < entries; j++ ) {
00177 rhist->Fill(x_uflow, y_uflow, weight);
00178 }
00179
00180
00181
00182 write(histoID, h, refpAddress);
00183 return StatusCode::SUCCESS;
00184 }
00185 return StatusCode::FAILURE;
00186 }
00187
00188
00189
00191 StatusCode H2DCnv::write(std::string , IHistogram2D* h, IOpaqueAddress*& refpAddress) {
00192 DataObject* pObject = dynamic_cast<DataObject*>(h);
00193
00194 std::string full(pObject->location());
00195
00196 full.insert(0,"/");
00197
00198 MsgStream log(msgSvc(), "RootHistCnv::H2DCnv");
00199
00200
00201
00202
00203
00204 GenericAddress* addr = new GenericAddress(repSvcType() , objType());
00205 addr->setContainerName(full);
00206 addr->setObjectName(h->title());
00207 refpAddress = addr;
00208 return StatusCode::SUCCESS;
00209 }
00210
00211
00212
00213 }