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