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

H2DCnv.cpp

Go to the documentation of this file.
00001 
00002 #define ROOTHISTCNV_H2DCNV_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/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 // Implementation of class :  RootHistCnv::H2DCnv
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::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         // No histogram ID, no histogram name
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();     // Number of bins in the axis X
00062       float  xLow     = h->xAxis()->lowerEdge();  // X-lower edge
00063       float  xHigh    = h->xAxis()->upperEdge();  // X-upper edge
00064       float  x_uflow  = xLow  - 1.0;      // Underflow coordinate X
00065       float  x_oflow  = xHigh + 1.0;      // Overflow coordinate X
00066 
00067       int    nBinY    = h->yAxis()->bins();     // Number of bins in the axis Y
00068       float  yLow     = h->yAxis()->lowerEdge();  // Y-lower edge
00069       float  yHigh    = h->yAxis()->upperEdge();  // Y-upper edge
00070       float  y_uflow  = yLow  - 1.0;      // Underflow coordinate Y
00071       float  y_oflow  = yHigh + 1.0;      // Overflow coordinate Y
00072 
00073       int    entries  = 0;                // Number of entries in a bin
00074       float  height   = 0.;               // Height if a bin
00075       float  centreX  = 0.;               // Centre of a bin on the axis X
00076       float  centreY  = 0.;               // Centre of a bin on the axis Y
00077       float  weight   = 0.;               // height / entries
00078       int    ix       = 0;                // Bin index on the axis X
00079       int    iy       = 0;                // Bin index on the axis Y
00080       int    j        = 0;                // Fill index
00081 
00082       Stat_t* errs    = new Stat_t[nBinX*nBinY];  // Array of bin errors
00083       int    ind      = 0;                        // Index of bin errors
00084       
00085       float x_mean  = xLow + (xHigh-xLow)/2.0;
00086       float y_mean  = yLow + (yHigh-yLow)/2.0;
00087 
00088       // Book the ROOT 2D histogram with fixed binning
00089       std::string full = pObject->fullpath();
00090       //      cdToPath(full);
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       // Fill it
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       // Underflow and overflow bins
00123       // W
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       // NW
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       // N
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       // NE
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       // E
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       // SE
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       // S
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       // SW
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       // HBOOK 2D histograms do not support mean and rms
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 /*histoID*/, IHistogram2D* h, IOpaqueAddress*& refpAddress)   {
00192     DataObject* pObject = dynamic_cast<DataObject*>(h);
00193     //    IDataDirectory* par = pObject->directory()->parent();
00194     std::string full(pObject->location());
00195     //    int icycle = 0;
00196     full.insert(0,"/");
00197 
00198     MsgStream log(msgSvc(), "RootHistCnv::H2DCnv");
00199 //      log << MSG::DEBUG;
00200 //      if( log.isActive() ) {
00201 //        HPRINT(histoID);
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 }    // namespace RootHistCnv

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