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

H1DVarCnv.cpp

Go to the documentation of this file.
00001 #define ROOTHISTCNV_H1DVARCNV_CPP
00002 
00003 
00004 // Include files
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 // Implementation of class :  RootHistCnv::H1DVarCnv
00021 //
00022 // Author :                   Charles Leggett
00023 //
00024 //------------------------------------------------------------------------------
00025 
00026 
00027 // Instantiation of a static factory class used by clients to create
00028 // instances of this service
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         // No histogram ID, no histogram name
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()); // Histogram ID
00057 
00058       int    nBinX    = h->xAxis()->bins();        // Number of bins
00059       float  xLow     = h->xAxis()->lowerEdge();   // Histogram lower edge
00060       float  xHigh    = h->xAxis()->upperEdge();   // Histogram upper edge
00061       float  x_uflow  = xLow  - 1.0;      // Underflow coordinate
00062       float  x_oflow  = xHigh + 1.0;      // Overflow coordinate
00063 
00064       int    entries  = 0;                // Number of entries in a bin
00065       float  height   = 0.;               // Height if a bin
00066       float  centre   = 0.;               // Centre of a bin
00067       float  weight   = 0.;               // height / entries
00068       int    i        = 0;                // Bin index
00069       int    j        = 0;                // Fill index
00070       Stat_t *errs     = new Stat_t[nBinX]; // Array of bin errors
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       // Book the HBOOK 1D histogram with variable binning
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       // Underflow bins
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       // Overflow bins
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 //        // Set the correct MEAN and RMS vales in the HBOOK histogram
00124 //        float mean          = (float) h->mean();
00125 //        float rms           = (float) h->rms();
00126 //        float sumOfHeights  = (float) h->sumBinHeights();
00127 //        float equivEntries  = (float) h->equivalentBinEntries();
00128 //        SET_MEAN_RMS( histoID, mean, rms, sumOfHeights, equivEntries );
00129 
00130       write(histoID, h, refpAddress);
00131       return StatusCode::SUCCESS;
00132     }
00133     return StatusCode::FAILURE;
00134   }
00135 
00137   StatusCode H1DVarCnv::write(int /*histoID*/, IHistogram1D* h, 
00138                               IOpaqueAddress*& refpAddress)   {
00139     MsgStream log(msgSvc(), "RootHistCnv::H1DVarCnv");
00140 
00141     DataObject* pObject = dynamic_cast<DataObject*>(h);
00142     //    IDataDirectory* par = pObject->directory()->parent();
00143     std::string full(pObject->location());
00144     //    int icycle = 0;
00145     full.insert(0,"/");
00146 
00147     // I know this is a big hack...but there is no other way to find out
00148     // If the RZ directory exists or not!
00149 //      if ( par != 0 && par->address() == 0 )    {
00150 //        HMDIR(full,"S");
00151 //      }
00152 //      else  {
00153 //        HCDIR(full);
00154 //      }
00155 
00156     log << MSG::DEBUG;
00157     if( log.isActive() ) {
00158 //        HPRINT(histoID);
00159     }
00160 //      HROUT(histoID, &icycle, "", 0);
00161 //      HDELET(histoID);
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 }    // namespace RootHistCnv

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