// ---------------------------------------------------------------------- // // HepHBookHist2D.cc - implementation of HBook two-dimensional histogram // // HepHBookHist2D is the implementation of the HBook two-dimensional // histogram. It is derived from HepHist2D (the generic interface) and // HepHBookHist. It is mainly a C++ wrapper around the CERNlib HBook // Fortran histogramming libraries. // // No HepHBookHist2D objects are ever directly // instantiated by the programmer. When creating new HepHist* objects, // the manager is in charge of creating the appropriate HepHBook object. // // To use generate HBook histograms, HepHBookFileManager must be used: // // int lunit = 10; // HepFileManager* m = new HepHBookFileManager("filename.rz", lunit); // : // : // HepHist2D* h(m->hist2D("Title", 100, 0.0, 23.0, 100, 0.0, 23.0)); // float x, y, weight; // : // [gather data] // : // h->accumulate(x, y, weight); // // // Note that the constructor can take an ID number which is used by // the HBook implementation of HepHist2D. If omitted, the ID number is // generated automatically (which is recommended). The ID number is // available through the id() method. // // Bob Jacobsen // 30-May-1997 Walter Brown Added cloning functions // 29-May-1997 Jason Luther Added comments // 04-Jun-1997 Jason Luther Rewrote comments // 04-Aug-1998 Philippe Canal Added weight, entries, sum, etc.. // 25-Feb-1999 J. Marraffino Added increment operators for histograms // // ---------------------------------------------------------------------- #ifndef HEPTRACE_H #include "HepTuple/HepTrace.h" #endif #ifndef HEPHBOOKHIST2D_H #include "HepTuple/HepHBookHist2D.h" #endif #ifndef HEPRAWHIST_H #include "HepTuple/HepRawHist.h" #endif #ifndef HEPHBOOKWRAPPERS_H #include "HepTuple/HepHBookWrappers.h" #include "ZMtools/pretendToUse.h" #endif #include "ZMutility/iostream" #include USING( std::string ) ZM_BEGIN_NAMESPACE( zmht ) /* namespace zmht { */ // create a 2D histogram with the given title and binning HepHBookHist2D::HepHBookHist2D( HepHBookFileManager * manager , const string& title , const int nBinsX, const float lowX, const float highX , const int nBinsY, const float lowY, const float highY , const int idReq ) : HepHist2D( manager, title, nBinsX, lowX, highX, nBinsY, lowY, highY, idReq ) { HEP_DEBUG( "HepHBookHist2D::constructor( \"" << title << "\", " << *id_ << " )" ); } // HepHBookHist2D::HepHBookHist2D() // Used for dummy construction ... // the object is then not valid ... HepHBookHist2D::HepHBookHist2D() : HepHist2D() { HEP_DEBUG( "HepHBookHist2D::constructor( )" ); } void HepHBookHist2D::silenceCompiler() { pretendToUse(&(__cf__HEXIST)); pretendToUse(&(__cf__HSUM)); pretendToUse(&(__cf__HI)); pretendToUse(&(__cf__HIE)); pretendToUse(&(__cf__HIF)); pretendToUse(&(__cf__HIJ)); pretendToUse(&(__cf__HIJE)); pretendToUse(&(__cf__HMIN)); pretendToUse(&(__cf__HMAX)); pretendToUse(&(__cf__HRNDM1)); pretendToUse(&(__cf__HSPFUN)); pretendToUse(&(__cf__HLNXTF)); pretendToUse(&(__cf__HSTATI)); pretendToUse(&(__cf__HX)); pretendToUse(&(__cf__HXE)); pretendToUse(&(__cf__HXY)); pretendToUse(&(__cf__ISCWN)); pretendToUse(&c2fstrv); pretendToUse(&f2cstrv); pretendToUse(&vkill_trailing); pretendToUse(&num_elem); } HepHBookHist2D::~HepHBookHist2D() { HEP_DEBUG( "HepHBookHist2D::destructor( \"" << *title_ << "\", " << *id_ << " )" ); } void HepHBookHist2D::accumulate ( const float x , const float y , const float weight ) { if ( mayUse() ) { _accumulate( x, y, weight ); HBookPushd( dir() ); HFILL( *id_, x, y, weight ); HBookPopd(); } else { ZMthrow(ZMxHepUnmanagedItem("attempt to accumulate() to unmanaged histogram.")); return; } } float HepHBookHist2D::bin( const int binNumX , const int binNumY ) const { if ( mayUse() ) { HBookPushd( dir() ); float result = HIJ ( *id_, binNumX, binNumY ); HBookPopd(); return result; } else { ZMthrow(ZMxHepUnmanagedItem("attempt to bin() from unmanaged histogram.")); return 0.0; } } #define BADCALL(name) \ if (! mayUse() ) { \ ZMthrow(ZMxHepUnmanagedItem( \ string("attempt to " #name "() an unmanaged histogram ") \ +title()+string("."))); \ return 0; \ } int HepHBookHist2D::entries() const { // Number of entries in bins BADCALL(entries); int result = 0; HBookPushd( dir() ); HNOENT(*id_, result); HBookPopd(); return result; } float HepHBookHist2D::weight() const { // Sum of all weights BADCALL(weight); HBookPushd( dir() ); float result = HSUM(*id_); HBookPopd(); return result; } float HepHBookHist2D::weight2() const { // retrieve sum of weight^2 if (! mayUse() ) { ZMthrow(ZMxHepUnmanagedItem( string("attempt to weight2() an unmanaged histogram ") +title()+string("."))); return 0.0; } float sum = 0; float weight; int numBinsX = this->nBinsX(); int numBinsY = this->nBinsY(); HBookPushd( dir() ); for (int i=1;inBinsX(); int numBinsY = this->nBinsY(); float deltaX = (maxX()-lowX)/numBinsX; HBookPushd( dir() ); // loop over X for (int i=1;inBinsX(); int numBinsY = this->nBinsY(); float deltaY = (maxY()-lowY)/numBinsY; HBookPushd( dir() ); // loop over X for (int i=1;inBinsX(); int numBinsY = this->nBinsY(); float deltaX = (maxX()-lowX)/numBinsX; HBookPushd( dir() ); // loop over X for (int i=1;inBinsX(); int numBinsY = this->nBinsY(); float deltaX = (maxX()-lowX)/numBinsX; float deltaY = (maxY()-lowY)/numBinsY; HBookPushd( dir() ); float* theY = new float[numBinsY+2]; for (int k=0;knBinsX(); int numBinsY = this->nBinsY(); float deltaY = (maxY()-lowY)/numBinsY; HBookPushd( dir() ); // loop over Y for (int i=1;i 0 ) { tempx = maxX+1.0; tempy = maxY+1.0; HFILL( id, tempx, tempy, numXOverYOver ); } if( numXOverYUnder > 0 ) { tempx = maxX+1.0; tempy = minY-1.0; HFILL( id, tempx, tempy, numXOverYUnder ); } if( numXUnderYUnder > 0 ) { tempx = minX-1.0; tempy = minY-1.0; HFILL( id, tempx, tempy, numXUnderYUnder ); } if( numXUnderYOver > 0 ) { tempx = minX-1.0; tempy = maxY+1.0; HFILL( id, tempx, tempy, numXUnderYOver ); } float binWidthX = ( maxX - minX )/(float)numBinsX; float binWidthY = ( maxY - minY )/(float)numBinsY; for(int i = 0; i < numBinsX; ++i ) { tempx = minX + ((float)i+0.5)*binWidthX; tempy = maxY+1.0; HFILL( id, tempx, tempy, numTop[i] ); tempy=minY-1.0; HFILL( id, tempx, tempy, numBottom[i] ); } for(int i = 0; i < numBinsY; ++i ) { tempx = maxX+1.0; tempy = minY + ((float)i+0.5)*binWidthY; HFILL( id, tempx, tempy, numRight[i] ); tempx = minX-1.0; HFILL( id, tempx, tempy, numLeft[i] ); } delete [] numTop; delete [] numBottom; delete [] numRight; delete [] numLeft; // Now set the overall statistics // HBook only wants numEntries for 2D plots. Set sumW2 negative to signal that. sumW2 = -1000.0; PUTSTAT( temp.hid, numEntries, sumW, sumW2, sumWX, sumWX2 ); // Since we're in control here, let's postpone the Pop and save some time //HBookPopd(); return temp; } void HepHBookHist2D::addHist2D( 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. // Since we postponed the Pop in importHist2D, we shouldn't Push here //HBookPushd( dir() ); char* operation = "+"; if( temp.hid != 0 ) HOPERA( *id_, operation, temp.hid, *id_, 1.0, 1.0 ); // Postpone the Pop again //HBookPopd (); } void HepHBookHist2D::subtractHist2D( 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. // Since we postponed the Pop in importHist2D, we shouldn't Push here //HBookPushd( dir() ); char* operation = "-"; if( temp.hid != 0 ) HOPERA( *id_, operation, temp.hid, *id_, 1.0, 1.0 ); // Postpone the Pop again //HBookPopd (); } void HepHBookHist2D::multiplyHist2D( 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. // Since we postponed the Pop in importHist2D, we shouldn't Push here //HBookPushd( dir() ); char* operation = "*"; if( temp.hid != 0 ) HOPERA( *id_, operation, temp.hid, *id_, 1.0, 1.0 ); // Postpone the Pop again //HBookPopd (); } void HepHBookHist2D::divideHist2D( 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. // Since we postponed the Pop in importHist2D, we shouldn't Push here //HBookPushd( dir() ); char* operation = "/"; if( temp.hid != 0 ) HOPERA( *id_, operation, temp.hid, *id_, 1.0, 1.0 ); // Postpone the Pop again //HBookPopd (); } void HepHBookHist2D::removeImportedHist2D( 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. If we // pass a zero to HDELET, it will happily (and silently) eat everything! // Since we postponed the Pop in importHist2D, we shouldn't Push here //HBookPushd( dir() ); if( temp.hid != 0 ) HDELET( temp.hid ); // Now, we're really done. Finally, do the Pop we've been postponing. HBookPopd (); } ZM_END_NAMESPACE( zmht ) /* } // namespace zmht */