Gaudi Framework, version v20r2

Generated: 18 Jul 2008

H1DCnv.cpp

Go to the documentation of this file.
00001 // $Header: /local/reps/Gaudi/HbookCnv/src/H1DCnv.cpp,v 1.13 2006/01/10 20:11:13 hmd Exp $
00002 #define HBOOKCNV_H1DCNV_CPP
00003 
00004 
00005 // Include files
00006 #include "GaudiKernel/CnvFactory.h"
00007 #include "GaudiKernel/IOpaqueAddress.h"
00008 #include "GaudiKernel/IDataProviderSvc.h"
00009 #include "GaudiKernel/DataObject.h"
00010 #include "GaudiKernel/MsgStream.h"
00011 #include "GaudiKernel/IRegistry.h"
00012 #include "AIDA/IHistogramFactory.h"
00013 #include "AIDA/IHistogram1D.h"
00014 #include "AIDA/IAxis.h"
00015 #include "HbookDef.h"
00016 #include "H1DCnv.h"
00017 #include <iostream>
00018 
00019 namespace HbookCnv {
00020   using AIDA::IHistogramFactory;
00021   using AIDA::IHistogram1D;
00022   using AIDA::IAxis;
00023 }
00024 
00025 //------------------------------------------------------------------------------
00026 //
00027 // Implementation of class :  HbookCnv::H1DCnv
00028 //
00029 // Author :                   Pavel Binko
00030 // Author :                   Grigori Rybkine
00031 //
00032 //------------------------------------------------------------------------------
00033 
00034 
00035 // Instantiation of a static factory class used by clients to create
00036 // instances of this service
00037 DECLARE_NAMESPACE_CONVERTER_FACTORY(HbookCnv,H1DCnv)
00038 
00039 
00040 StatusCode HbookCnv::H1DCnv::book(IOpaqueAddress* pAdd, DataObject* pObj){
00041   IHistogram1D* h = dynamic_cast<IHistogram1D*>(pObj);
00042   if ( 0 != h ) {
00043     int idh = pAdd->ipar()[0];
00044     const IAxis& x = h->axis();
00045     int nBinX = x.bins();      // Number of bins in the axis X
00046 
00047     if ( x.isFixedBinning() ) {
00048       ::HBOOK1(idh, h->title(), nBinX, float(x.lowerEdge()), float(x.upperEdge())); 
00049       ::HIDOPT(idh, "STAT");
00050     }
00051     else {
00052       float *edges = new float[nBinX+1];
00053       for ( int i = 0; i < nBinX; i++ )
00054         edges[i] = float(x.binLowerEdge(i));
00055 
00056       edges[nBinX] = float(x.upperEdge());
00057       ::HBOOKB(idh, h->title(), nBinX, edges);
00058       delete [] edges;
00059     }
00060     
00061     return StatusCode::SUCCESS;
00062   }
00063   return StatusCode::FAILURE;
00064 }
00065 
00067 StatusCode HbookCnv::H1DCnv::updateRep(IOpaqueAddress* pAddr, 
00068                                        DataObject* pObj)  {
00069   IHistogram1D* h = dynamic_cast<IHistogram1D*>(pObj);
00070   if ( 0 != h ) {
00071     const  IAxis& x = h->axis();
00072     int    histoID  = pAddr->ipar()[0]; // Histogram ID
00073     int    nBinX    = x.bins();         // Number of bins
00074     float  xLow     = float(x.lowerEdge());    // Histogram lower edge
00075     float  xHigh    = float(x.upperEdge());    // Histogram upper edge
00076     float  x_uflow  = xLow  - 1.0F;      // Underflow coordinate
00077     float  x_oflow  = xHigh + 1.0F;      // Overflow coordinate
00078 
00079     int    entries  = 0;                // Number of entries in a bin
00080     float  height   = 0.;               // Height if a bin
00081     float  centre   = 0.;               // Mean of a bin
00082     float  weight   = 0.;               // height / entries
00083     float *errs     = new float[nBinX]; // Array of bin errors
00084     int    i, j     = 0;
00085 
00086     // Fill the HBOOK 1D histogram (with fixed or variable binning)
00087     for ( i = 0; i < nBinX; i++ )  {
00088       errs[i] = float(h->binError(i));
00089       height  = float(h->binHeight(i));
00090       if( 0 != height ) {
00091         centre  = float(h->binMean(i));
00092         entries = h->binEntries(i);
00093         weight  = height / entries;
00094         for( j = 0; j < entries; j++ ) {
00095           ::HFILL(histoID, centre, 0.0, weight);
00096         }
00097       }
00098     }
00099     ::HPAKE(histoID,errs);
00100     delete [] errs;
00101 
00102 
00103     // Underflow bins
00104     entries          = h->binEntries( AIDA::IAxis::UNDERFLOW_BIN );
00105     height           = float(h->binHeight( AIDA::IAxis::UNDERFLOW_BIN ));
00106     weight  = height / entries;
00107     for( j = 0; j < entries; j++ ) {
00108       ::HFILL(histoID, x_uflow, 0.0, weight);
00109     }
00110     // Overflow bins
00111     entries          = h->binEntries( AIDA::IAxis::OVERFLOW_BIN );
00112     height           = float(h->binHeight( AIDA::IAxis::OVERFLOW_BIN ));
00113     weight  = height / entries;
00114     for( j = 0; j < entries; j++ ) {
00115       ::HFILL(histoID, x_oflow, 0.0, weight);
00116     }
00117 
00118     // Set the correct MEAN and RMS vales in the HBOOK histogram
00119     float mean          = (float) h->mean();
00120     float rms           = (float) h->rms();
00121     float sumOfHeights  = (float) h->sumBinHeights();
00122     float equivEntries  = (float) h->equivalentBinEntries();
00123     ::SET_MEAN_RMS( histoID, mean, rms, sumOfHeights, equivEntries );
00124     return StatusCode::SUCCESS;
00125   }
00126   return StatusCode::FAILURE;
00127 }
00128 
00130 StatusCode HbookCnv::H1DCnv::createObj(IOpaqueAddress* pAddr, 
00131                                        DataObject*& refpObj)
00132 {
00133   //MsgStream log(msgSvc(), "HHistoCnv");
00134 
00135   IHistogramFactory* pFac = dynamic_cast<IHistogramFactory*>(dataProvider());
00136   if ( 0 != pAddr && pFac ) {
00137     std::string loc = pAddr->registry()->identifier();
00138     long id = pAddr->ipar()[0];
00139     if ( readObject(loc, id).isSuccess() )  {
00140       std::string tit;
00141       int nx, ny, nwt, lpaw;
00142       float xmi, xma, ymi, yma;
00143       ::HGIVE(id, tit, nx, xmi, xma, ny, ymi, yma, nwt, lpaw);
00144       long kind[32];
00145       ::HKIND(id, kind, "A", 1);
00146 
00147       if ( 0 == kind[5] ) {
00148         pAddr->addRef();
00149         refpObj = dynamic_cast<DataObject*>(pFac->createHistogram1D(loc,tit,
00150                                                                     nx,xmi,xma));
00151       }
00152       else {
00153         std::vector<double> binEdges;
00154         float val;
00155         for ( int i = 1; i <= nx; ++i ) {
00156           ::HIX(id, i, val);
00157           binEdges.push_back(val);
00158         }
00159         binEdges.push_back(xma);
00160         pAddr->addRef();
00161         refpObj = dynamic_cast<DataObject*>(pFac->createHistogram1D(loc,tit,
00162                                                                     binEdges));
00163       }
00164       
00165       refpObj->registry()->setAddress(pAddr);
00166       StatusCode sc = updateObj(pAddr, refpObj);
00167       pAddr->release();
00168       return sc;
00169     }
00170   }
00171   return StatusCode::FAILURE;
00172 }
00173 
00175 StatusCode HbookCnv::H1DCnv::updateObj(IOpaqueAddress* pAddr, 
00176                                        DataObject* pObj)  {
00177   IHistogram1D* h = dynamic_cast<IHistogram1D*>(pObj);
00178   if ( 0 != h && 0 != pAddr ) {
00179     int    id  = pAddr->ipar()[0]; // Histogram ID
00180     std::string loc = pAddr->registry()->identifier();
00181     if ( !::HEXIST( id ) )  {
00182       readObject(loc, id);
00183     }
00184     if ( ::HEXIST( id ) )  {
00185       const  IAxis& x = h->axis();
00186       int    nBinX    = x.bins();         // Number of bins
00187       double xLow     = x.lowerEdge();    // Histogram lower edge
00188       double xHigh    = x.upperEdge();    // Histogram upper edge
00189       double x_uflow  = xLow  - 1.0;      // Underflow coordinate
00190       double x_oflow  = xHigh + 1.0;      // Overflow coordinate
00191       double height = 0, err = 0, mean = 0, rms = 0, 
00192             centre = 0, sumOfHeights = 0, equivEntries = 0;
00193       h->reset();
00194       for ( int i = 0; i < nBinX; i++ )  {
00195         centre = h->binMean(i);
00196         height = ::HI (id, i+1);
00197         err    = ::HIE(id, i+1);
00198         sumOfHeights += height;
00199         // Unfortunately IHistogram1D does not support to set bins
00200         // and bin errors....errors will be SQRT(height)
00201         h->fill(centre, height);
00202       }
00203       // Underflow bins: also here no error filling possible
00204     //centre = h->binMean(AIDA::IAxis::UNDERFLOW_BIN);
00205       height = ::HI(id, 0);
00206       sumOfHeights += height;
00207       h->fill(x_uflow, height);
00208       // Overflow bins: also here no error filling possible
00209     //centre = h->binMean(AIDA::IAxis::OVERFLOW_BIN);
00210       height = ::HI(id, nBinX+1);
00211       sumOfHeights += height;
00212       h->fill(x_oflow, height);
00213       // Set the correct MEAN and RMS vales in the HBOOK histogram
00214       mean = ::HSTATI(id, 1, " ", 0);
00215       rms  = ::HSTATI(id, 2, " ", 0);
00216       equivEntries = ::HSTATI(id, 3, " ", 0);
00217       //h->SET_MEAN_RMS( id, mean, rms, sumOfHeights, equivEntries );
00218       ::HDELET(id);
00219       return StatusCode::SUCCESS;
00220     }
00221   }
00222   return StatusCode::FAILURE;
00223 }

Generated at Fri Jul 18 11:59:26 2008 for Gaudi Framework, version v20r2 by Doxygen version 1.5.1 written by Dimitri van Heesch, © 1997-2004