00001 #define ROOTHISTCNV_H1DVARCNV_CPP
00002
00003
00004
00005 #include <iostream>
00006 #include "GaudiKernel/MsgStream.h"
00007 #include "GaudiKernel/CnvFactory.h"
00008 #include "GaudiKernel/GenericAddress.h"
00009 #include "GaudiKernel/DataObject.h"
00010 #include "GaudiKernel/IHistogram1D.h"
00011 #include "GaudiKernel/IAxis.h"
00012
00013 #include "H1DVarCnv.h"
00014 #include "RootDirFcn.h"
00015
00016 #include "TH1.h"
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 static CnvFactory<RootHistCnv::H1DVarCnv> s_factory;
00030 const ICnvFactory& RootHistH1DVarCnvFactory = s_factory;
00031
00032
00033 namespace RootHistCnv {
00034
00036 StatusCode H1DVarCnv::createRep(DataObject* pObject, IOpaqueAddress*& refpAddress) {
00037
00038 IHistogram1D* h = dynamic_cast<IHistogram1D*>(pObject);
00039 refpAddress = 0;
00040
00041 MsgStream log(msgSvc(), "RootHistCnv::H1DVarCnv");
00042
00043 if ( 0 != h ) {
00044
00045 std::string temp = h->title();
00046
00047 int sep = temp.find('|');
00048 if ( 0 >= sep ) {
00049
00050 log << MSG::ERROR << "Cannot separate histogram title and identifier" << endreq;
00051 log << MSG::ERROR << "while converting the histogram: " << temp << endreq;
00052 return StatusCode::FAILURE;
00053 }
00054
00055 std::string numStr( temp, 0, sep );
00056 int histoID = atoi(numStr.c_str());
00057
00058 int nBinX = h->xAxis()->bins();
00059 float xLow = h->xAxis()->lowerEdge();
00060 float xHigh = h->xAxis()->upperEdge();
00061 float x_uflow = xLow - 1.0;
00062 float x_oflow = xHigh + 1.0;
00063
00064 int entries = 0;
00065 float height = 0.;
00066 float centre = 0.;
00067 float weight = 0.;
00068 int i = 0;
00069 int j = 0;
00070 Stat_t *errs = new Stat_t[nBinX];
00071
00072 float *edge = new float[nBinX+1];
00073 for ( i = 0; i < nBinX; i++ ) {
00074 edge[i] = h->xAxis()->binLowerEdge(i);
00075 }
00076 edge[nBinX] = h->xAxis()->upperEdge();
00077
00078
00079 std::string title( temp, sep+1, temp.length() );
00080
00081 std::string dir = pObject->location();
00082 RootTrimLeadingDir(dir,"/stat");
00083 RootMkdir(dir);
00084 RootCd(dir);
00085
00086 log << MSG::INFO << "creating hist id: " << histoID
00087 << " title: " << title << " in " << dir << endreq;
00088
00089 TH1F *rhist = new TH1F(numStr.c_str(), title.c_str(), nBinX, edge);
00090
00091 delete [] edge;
00092
00093 for ( i = 0; i < nBinX; i++ ) {
00094 errs[i] = h->binError(i);
00095 height = h->binHeight(i);
00096 if( 0 != height ) {
00097 centre = h->xAxis()->binCentre(i);
00098 entries = h->binEntries(i);
00099 weight = height / entries;
00100 for( j = 0; j < entries; j++ ) {
00101 rhist->Fill(centre,weight);
00102 }
00103 }
00104 }
00105 rhist->SetError( errs );
00106 delete [] errs;
00107
00108
00109 entries = h->binEntries( IHistogram::UNDERFLOW_BIN );
00110 height = h->binHeight( IHistogram::UNDERFLOW_BIN );
00111 weight = height / entries;
00112 for( j = 0; j < entries; j++ ) {
00113 rhist->Fill(x_uflow,weight);
00114 }
00115
00116 entries = h->binEntries( IHistogram::OVERFLOW_BIN );
00117 height = h->binHeight( IHistogram::OVERFLOW_BIN );
00118 weight = height / entries;
00119 for( j = 0; j < entries; j++ ) {
00120 rhist->Fill(x_oflow,weight);
00121 }
00122
00123
00124
00125
00126
00127
00128
00129
00130 write(histoID, h, refpAddress);
00131 return StatusCode::SUCCESS;
00132 }
00133 return StatusCode::FAILURE;
00134 }
00135
00137 StatusCode H1DVarCnv::write(int , IHistogram1D* h,
00138 IOpaqueAddress*& refpAddress) {
00139 MsgStream log(msgSvc(), "RootHistCnv::H1DVarCnv");
00140
00141 DataObject* pObject = dynamic_cast<DataObject*>(h);
00142
00143 std::string full(pObject->location());
00144
00145 full.insert(0,"/");
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156 log << MSG::DEBUG;
00157 if( log.isActive() ) {
00158
00159 }
00160
00161
00162 GenericAddress* addr = new GenericAddress( repSvcType() , objType() );
00163 addr->setContainerName(full);
00164 addr->setObjectName(h->title());
00165 refpAddress = addr;
00166 return StatusCode::SUCCESS;
00167 }
00168
00169 }