Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

H1DCnv.cpp

Go to the documentation of this file.
00001 
00002 #define ROOTHISTCNV_H1DCNV_CPP
00003 
00004 
00005 // Include files
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 // Implementation of class :  RootHistCnv::H1DCnv
00024 //
00025 // Author :                   Charles Leggett
00026 //
00027 //------------------------------------------------------------------------------
00028 
00029 
00030 // Instantiation of a static factory class used by clients to create
00031 // instances of this service
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       //      log << MSG::DEBUG << "temp: " << temp << endreq;
00050 
00051       int sep = temp.find('|');
00052       if ( 0 >= sep ) {
00053         // No histogram ID, no histogram name
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();        // Number of bins
00063       float  xLow     = h->xAxis()->lowerEdge();   // Histogram lower edge
00064       float  xHigh    = h->xAxis()->upperEdge();   // Histogram upper edge
00065       float  x_uflow  = xLow  - 1.0;      // Underflow coordinate
00066       float  x_oflow  = xHigh + 1.0;      // Overflow coordinate
00067 
00068       int    entries  = 0;                // Number of entries in a bin
00069       float  height   = 0.;               // Height if a bin
00070       float  centre   = 0.;               // Centre of a bin
00071       float  weight   = 0.;               // height / entries
00072       int    i        = 0;                // Bin index
00073       int    j        = 0;                // Fill index
00074 //        float *errs     = new float[nBinX]; // Array of bin errors
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       //      cdToPath(full);
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 //        log << MSG::DEBUG << "filled hist" << endreq;
00103 
00104       rhist->SetError( errs );
00105 
00106 //        log << MSG::DEBUG << "set errors" << endreq;
00107 
00108       delete [] errs;
00109 
00110 
00111       // Underflow bins
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 //        log << MSG::DEBUG << "filled underflow" << endreq;
00119       // Overflow bins
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 //        log << MSG::DEBUG << "filled overflow" << endreq;
00127 
00128       // Set the correct MEAN and RMS vales in the HBOOK histogram
00129 //        float mean          = (float) h->mean();
00130 //        float rms           = (float) h->rms();
00131 //        float sumOfHeights  = (float) h->sumBinHeights();
00132 //        float equivEntries  = (float) h->equivalentBinEntries();
00133       //      SET_MEAN_RMS( histoID, mean, rms, sumOfHeights, equivEntries );
00134 
00135       write(histoID, h, refpAddress);
00136 //        log << MSG::DEBUG << "called write()" << endreq;
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 /*histoID*/, IHistogram1D* h, IOpaqueAddress*& refpAddress)   {
00147 
00148     MsgStream log(msgSvc(), "RootHistCnv::H1DCnv");
00149 
00150     DataObject* pObject = dynamic_cast<DataObject*>(h);
00151 //      IDataDirectory* par = pObject->directory()->parent();
00152     std::string full(pObject->location());
00153 //      std::string lpath(pObject->localPath());
00154 //      std::string fpath(pObject->fullpath()); 
00155 
00156 //      DataObject* pObjPar = pObject->parent();
00157 //      std::string pPath = pObjPar->fullpath();
00158 
00159 //      std::list<std::string> dirlst;
00160 //      std::list<std::string>::const_iterator diritr;
00161 
00162 //      while (pObjPar != NULL) {
00163 //        std::string dir = pObjPar->localPath();
00164 //        std::string pth = pObjPar->location();
00165 
00166 //        dirlst.push_front(dir);
00167 
00168 //        pObjPar = pObjPar->parent();
00169 //      }
00170     
00171 //      for (diritr=dirlst.begin(); diritr!=dirlst.end(); ++diritr) {
00172 //        log << MSG::DEBUG << "pth: " << *diritr << endreq;
00173 //      }
00174     
00175 
00176 //      int icycle = 0;
00177     full.insert(0,"/");
00178 
00179 //      // I know this is a big hack...but there is no other way to find out
00180 //      // If the RZ directory exists or not!
00181 //      if ( par != 0 && par->address() == 0 )    {
00182 //        HMDIR(full,"S");
00183 //      }
00184 //      else  {
00185 //        HCDIR(full);
00186 //      }
00187 
00188 //      log << MSG::INFO << "In R:H1DCnv::write" << endreq;
00189 //      log << MSG::DEBUG << "location(): " << full << endreq;
00190 //      log << MSG::DEBUG << "localPath(): " << lpath << endreq;
00191 //      log << MSG::DEBUG << "fullpath(): " << fpath << endreq;
00192 //      log << MSG::DEBUG << "parentPath: " << pPath << endreq;
00193 
00194 //      log << MSG::DEBUG;
00195 //      if( log.isActive() ) {
00196 //        //      HPRINT(histoID);
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 }    // namespace RootHistCnv

Generated at Wed Nov 21 12:20:49 2001 by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000