// ---------------------------------------------------------------------- // // HepRootHist1D.cc - implementation of Root two-dimensional histogram // // HepRootHist1D is the implementation of the Root one-dimensional // histogram. It is derived from HepHist1D (the generic interface) and // HepRootHist. It is mainly a C++ wrapper around the Histo // C histogramming libraries. // // No HepRootHist1D objects are ever directly // instantiated by the programmer. When creating new HepHist* objects, // the manager is in charge of creating the appropriate Root object. // // To use generate Root histograms, HepRootFileManager must be used: // // int lunit = 10; // HepFileManager* m = new HepRootFileManager("filename.rz", lunit); // : // : // HepHist1D* h(m->hist1D("Title", 100, 0.0, 23.0, 100, 0.0, 23.0)); // float x, y, weight; // : // [gather data] // : // h->accumulate(x, y, weight); // // // // 12-Dec-1998 Philippe Canal Creation // 25-Feb-1999 J. Marraffino Added increment operators for histograms // // ---------------------------------------------------------------------- #ifndef HEPTRACE_H #include "HepTuple/HepTrace.h" #endif #ifndef HEPROOTFILEMANAGER_H #include "HepTuple/HepRootFileManager.h" #endif #ifndef HEPROOTHIST1D_H #include "HepTuple/HepRootHist1D.h" #endif #ifndef HEPRAWHIST_H #include "HepTuple/HepRawHist.h" #endif #ifndef HEPTH1F_h #include "HepTuple/HepTH1F.h" #endif #include #include #include USING( std::string ) ZM_BEGIN_NAMESPACE( zmht ) /* namespace zmht { */ // Create a 1D histogram from an existing TH1 HepRootHist1D::HepRootHist1D ( HepRootFileManager * manager , TH1* histo ) : HepHist1D( manager, histo->GetName(), histo->GetNbinsX(), histo->GetXaxis()->GetXmin(), histo->GetXaxis()->GetXmax(), histo->GetUniqueID() ), HepRootObj(histo) { HEP_DEBUG( "HepRootHist1D::constructor( TH1* " << histo << " )" ); } // create a 1D histogram with the given title and binning HepRootHist1D::HepRootHist1D( HepRootFileManager * manager , const string& title , int nBins, float low, float high , int idReq ) : HepHist1D( manager, title.c_str(), nBins, low, high, idReq ), HepRootObj() { HEP_DEBUG( "HepRootHist1D::constructor( \"" << title << "\", " << id_ << " )" ); string name = mkname(title, *id_); data( new TH1F ( name.c_str(), title.c_str(), nBins, low, high ) ); if ( data() == 0 ) { ZMthrow(ZMxHepCantMakeHist(string("Histo failed to create the hist 1d: ") + title ) ); isValid(false); } else { data()->SetUniqueID( idReq ); } } // HepHistoHist1D::HepHistoHist1D() // clone an existing 1D histogram HepRootHist1D::HepRootHist1D( HepRootHist1D * original , const string& title , const int idReq ) : HepHist1D( original->manager(), title.c_str(), original->nBins(), original->min(), original->max(), idReq ), HepRootObj(NULL) { data( (TNamed*) original->data()->Clone() ); if ( data() == 0 ) { ZMthrow(ZMxHepCantMakeHist(string("Histo failed to create the hist 1d: ") + title ) ); isValid(false); } else { data()->SetTitle( title.c_str() ); data()->SetUniqueID( idReq ); } } // HepRootHist1D::HepRootHist1D() // Used for dummy construction ... // the object is then not valid ... HepRootHist1D::HepRootHist1D() : HepHist1D() { HEP_DEBUG( "HepRootHist1D::constructor( )" ); } HepRootHist1D::~HepRootHist1D() { HEP_DEBUG( "HepRootHist1D::destructor( \"" << *title_ << "\", " << id_ << " )" ); } void HepRootHist1D::accumulate ( const float x , const float weight ) { if ( mayUse() ) { _accumulate( x, weight ); ((TH1*)data())->Fill( x, weight); } else { ZMthrow(ZMxHepUnmanagedItem( string("attempt to accumulate() to unmanaged histogram ") +title()+string("."))); } } float HepRootHist1D::bin( // retrieve value of given bin const int binNum ) const { if ( mayUse() ) { return (float)(((TH1*)data())->GetBinContent(binNum)); } else { ZMthrow(ZMxHepUnmanagedItem( string("attempt to bin() to unmanaged histogram ") +title()+string("."))); return 0.0; } } float HepRootHist1D::binError( // retrieve error of given bin const int binNum ) const { if ( mayUse() ) { return (float)(((TH1*)data())->GetBinError(binNum)); } else { ZMthrow(ZMxHepUnmanagedItem( string("attempt to binError() to unmanaged histogram ") +title()+string("."))); return 0.0; } } void HepRootHist1D::getContents( // get values for all in-range bins float * values ) const { if ( mayUse() ) { int nBins = this->nBins(); for( int i=0; inBins(); for( int i=0; inBins(); for( int i=0; iSetBinContent(i+1, values[i])); } (((TH1*)data())->SetEntries((Stat_t)nBins)); } else { ZMthrow(ZMxHepUnmanagedItem( string("attempt to access an unmanaged histogram ") +title()+string("."))); } } void HepRootHist1D::putErrors( // set errors for all in-range bins float* errors ) const { if ( mayUse() ) { int nBins = this->nBins(); for( int i=0; iSetBinError(i+1, errors[i])); } } else { ZMthrow(ZMxHepUnmanagedItem( string("attempt to binError() to unmanaged histogram ") +title()+string("."))); } } int HepRootHist1D::entries() const { // number of entries if (! mayUse() ) { ZMthrow(ZMxHepUnmanagedItem( string("attempt to entries() an unmanaged histogram ") +title()+string("."))); return 0; } // call histo function return (int)(((TH1*)data())->GetEntries()); } float HepRootHist1D::weight() const { // sum of all weight if (! mayUse() ) { ZMthrow(ZMxHepUnmanagedItem( string("attempt to weight() an unmanaged histogram ") +title()+string("."))); return 0.0; } return (float)(((TH1*)data())->GetSumOfWeights()); } float HepRootHist1D::weight2() const { // retrieve sum is weight^2 if (! mayUse() ) { ZMthrow(ZMxHepUnmanagedItem( string("attempt to weight2() an unmanaged histogram ") +title()+string("."))); return 0.0; } TH1 * hist = (TH1*) data(); float sum = 0; int nBins = this->nBins(); for (int i=1;iGetBinContent(i) * hist->GetBinContent(i); } return sum; } float HepRootHist1D::sum() const { // retrieve weighted sum of X if (! mayUse() ) { ZMthrow(ZMxHepUnmanagedItem( string("attempt to sum() an unmanaged histogram ") +title()+string("."))); return 0.0; } TH1 * hist = (TH1*) data(); float sum = 0; int nBins = this->nBins(); for (int i=1;iGetBinCenter(i) * hist->GetBinContent(i); } return sum; } float HepRootHist1D::sum2() const { // retrieve weighted sum of square (X^2) if (! mayUse() ) { ZMthrow(ZMxHepUnmanagedItem( string("attempt to sum() an unmanaged histogram ") +title()+string("."))); return 0.0; } TH1 * hist = (TH1*) data(); float sum = 0; int nBins = this->nBins(); for (int i=1;iGetBinCenter(i); sum += val * val * (float)(hist->GetBinContent(i)); } return sum; } void HepRootHist1D::getStatistics( // get histogram-wide statistics int & numEntries , float & sumW , float & sumW2 , double & sumWX , double & sumWX2 ) const { if (! mayUse() ) { ZMthrow(ZMxHepUnmanagedItem( string("attempt to getOverflows() from an unmanaged histogram ") +title()+string("."))); } else { numEntries = entries(); sumW = weight(); sumW2 = weight2(); sumWX = sum(); sumWX2 = sum2(); } } void HepRootHist1D::getOverflows( // get over/under-flow count float * numOver , float * numUnder ) const { if (! mayUse() ) { ZMthrow(ZMxHepUnmanagedItem( string("attempt to getOverflows() from an unmanaged histogram ") +title()+string("."))); } else { *numUnder = this->bin( 0 ); *numOver = this->bin( nBins()+1 ); } } void HepRootHist1D::setXTitle ( const string& label ) { if ( mayUse() ) { ((TH1*)data())->SetXTitle( label.c_str() ); ((TH1*)data())->GetXaxis()->CenterTitle(); } else { ZMthrow(ZMxHepUnmanagedItem( string("attempt to modify unmanaged histogram ") +title()+string("."))); } } void HepRootHist1D::setYTitle ( const string& label ) { if ( mayUse() ) { ((TH1*)data())->SetYTitle( label.c_str() ); ((TH1*)data())->GetYaxis()->CenterTitle(); } else { ZMthrow(ZMxHepUnmanagedItem( string("attempt to modify unmanaged histogram ") +title()+string("."))); } } void HepRootHist1D::setZTitle ( const string& label ) { if ( mayUse() ) { ((TH1*)data())->SetZTitle( label.c_str() ); ((TH1*)data())->GetZaxis()->CenterTitle(); } else { ZMthrow(ZMxHepUnmanagedItem( string("attempt to modify unmanaged histogram ") +title()+string("."))); } } void HepRootHist1D::setTitle ( const string& label ) { if ( mayUse() ) { ((TH1*)data())->SetTitle( label.c_str() ); } else { ZMthrow(ZMxHepUnmanagedItem( string("attempt to modify unmanaged histogram ") +title()+string("."))); } } HepRawHist HepRootHist1D::importHist1D( const HepHist1D & h ) { // Manufacture a temporary Root histogram with all the characteristics of h. // Make it a HepTH1F object so we can get to the protected data. HepTH1F * tmpHist = new HepTH1F( h ); // Instantiate a HepRawHist object to pass back to say where the // Raw Histogram lives and what it's called HepRawHist temp( tmpHist ); return temp; } void HepRootHist1D::addHist1D( const HepRawHist & temp ) { // If we got here, the temporary histogram object referred to by temp is // known to be valid. On the other hand, it's worth being careful. TH1 * tmpHist = temp.HepTH1Fptr; if( tmpHist != 0 ) ((TH1*)data())->Add( tmpHist, 1.0 ); } void HepRootHist1D::subtractHist1D( const HepRawHist & temp ) { // If we got here, the temporary histogram object referred to by temp is // known to be valid. On the other hand, it's worth being careful. TH1 * tmpHist = temp.HepTH1Fptr; if( tmpHist != 0 ) ((TH1*)data())->Add( tmpHist, -1.0 ); } void HepRootHist1D::multiplyHist1D( const HepRawHist & temp ) { // If we got here, the temporary histogram object referred to by temp is // known to be valid. On the other hand, it's worth being careful. TH1 * tmpHist = temp.HepTH1Fptr; if( tmpHist != 0 ) ((TH1*)data())->Multiply( tmpHist ); } void HepRootHist1D::divideHist1D( const HepRawHist & temp ) { // If we got here, the temporary histogram object referred to by temp is // known to be valid. On the other hand, it's worth being careful. TH1 * tmpHist = temp.HepTH1Fptr; if( tmpHist != 0 ) ((TH1*)data())->Divide( tmpHist ); } void HepRootHist1D::removeImportedHist1D( const HepRawHist & temp ) { // If we got here, the temporary histogram object referred to by temp is // known to be valid. On the other hand, it's worth being careful. HepTH1F * tmpHist = temp.HepTH1Fptr; if( tmpHist != 0 ) delete tmpHist; } void HepRootHist1D::reset() { if ( mayUse() ) { ((TH1*) data())->Reset(); } else { ZMthrow(ZMxHepUnmanagedItem( string("attempt to reset() to unmanaged histogram ") +title()+string("."))); } } ZM_END_NAMESPACE( zmht ) /* } // namespace zmht */