// ---------------------------------------------------------------------- // // HepHist1DArray.cc // // History: // // Created 4/5/99 Philippe Canal // // HepHist1DArray() // ~HepHist1DArray() // makeClone and makeEmpty // operator[](int) // HepHist1DArray::length() // HepHist1DArray::nBins() // HepHist1DArray::min() // HepHist1DArray::max() #ifndef HEPTRACE_H #include "HepTuple/HepTrace.h" #endif #ifndef HEPHBOOKHIST1DARRAY_H #include "HepTuple/HepHist1DArray.h" #endif #ifndef HEPHBOOKHIST1D_H #include "HepTuple/HepHist1D.h" #endif USING( std::string ) ZM_BEGIN_NAMESPACE( zmel ) /* namespace zmel { */ // ---------------------------------------------------------------------- // Constructors // ---------------------------------------------------------------------- HepHist1DArray::HepHist1DArray( // constructor HepFileManager * mgr , const string & title , const dataArray & data ) : HepObj( mgr, title, 0 ), nBins_(0), min_(0), max_(0) { // We assume that if there are any histograms in the dataArray // that the first one defines the meta data of the whole // checking is assume to be done outside of this functions. HEP_DEBUG( "HepHist1DArray::constructor( \"" << title << "\", " << id_ << " )" ); type_ = '1'; data_ = data; if ( length() > 0 ) { nBins_ = data_[0]->nBins(); min_ = data_[0]->min(); max_ = data_[0]->max(); } else { ZMthrow(ZMxHepIllFormedItem("Can not create empty HepHist1DArray")); isValid(false); } } // Used for dummy construction ... // the object is then not valid ... HepHist1DArray::HepHist1DArray() : HepObj() , nBins_( 0 ) , min_ ( 0 ) , max_ ( 0 ) { HEP_DEBUG( "HepHist1DArray::constructor( )" ); type_ = '1'; } // ---------------------------------------------------------------------- // Destructors // ---------------------------------------------------------------------- HepHist1DArray::~HepHist1DArray() { HEP_DEBUG( "HepHist1DArray::destructor( \"" << *title_ << "\", " << id_ << " )" ); } // ---------------------------------------------------------------------- // Clone functionnality! // ---------------------------------------------------------------------- ZM_COVARIANT_TYPE(HepObj,HepHist1DArray) & HepHist1DArray::makeClone( // clone existing histogram, filling bins HepFileManager * manager , const string& title , const int id ) const { if ( ! mayUse() || ! isValid() ) { ZMthrow(ZMxHepUnmanagedItem( string("attempt to makeClone() to unmanaged histogram ") +title+string("."))); return HepFileManager::deadHist1DArray(); } // Static cast for platform without covariant return type // for virtual member function HepHist1DArray & clone = static_cast ( makeEmpty( manager, title, id ) ); int len = length(); for ( int i = 0; i < len ; i++ ) { clone[i] += *(data_[i]); } return clone; } ZM_COVARIANT_TYPE(HepObj,HepHist1DArray) & HepHist1DArray::makeEmpty( // clone existing histogram, but zero bins HepFileManager * mgr , const string& title , const int id ) const { if ( ! mayUse() || ! isValid() ) { ZMthrow(ZMxHepUnmanagedItem( string("attempt to makeClone() to unmanaged histogram ") +title+string("."))); return HepFileManager::deadHist1DArray(); } return mgr->hist1DArray( length(), title, nBins(), min(), max(), id); } ZM_COVARIANT_TYPE(HepObj,HepHist1DArray) & HepHist1DArray::makeClone( // clone existing histogram, filling bins const string& title , const int id ) const { return makeClone( manager(), title, id ); } ZM_COVARIANT_TYPE(HepObj,HepHist1DArray) & HepHist1DArray::makeEmpty( // clone existing histogram, but zero bins const string& title , const int id ) const { return makeEmpty( manager(), title, id ); } // ---------------------------------------------------------------------- // Reset // ---------------------------------------------------------------------- void HepHist1DArray::reset() { if ( mayUse() && isValid() ) { for ( int i = 0; i < length(); ++i ) { data_[i]->reset(); } } else { if ( !mayUse() ) { ZMthrow(ZMxHepUnmanagedItem( string("attempt to access unmanaged histogram array") +title()+string("."))); return ; } else if ( ! isValid() ) { ZMthrow(ZMxHepUnusableEntity( title()+" is invalid")); } } } // ---------------------------------------------------------------------- // Simple Accessors // ---------------------------------------------------------------------- HepHist1D& HepHist1DArray::operator[](int pos) const { if ( mayUse() && isValid() && ( pos < length() ) ) { return *(data_[pos]); } else { if ( !mayUse() ) { ZMthrow(ZMxHepUnmanagedItem( string("attempt to access unmanaged histogram array") +title()+string("."))); if ( manager() == 0 ) { ZMthrow(ZMxHepTuple("No manager to know how to recover from previous error!!",ZMexPROBLEM)); } return *(HepHist1D*)HepFileManager::deadHepObj(); } else if ( ! ( pos < length() ) ) { ZMthrow(ZMxHepOutOfRangeValue( string("In ")+title()+"index out of range.")); } else if ( ! isValid() ) { ZMthrow(ZMxHepUnusableEntity( title()+" is invalid")); } return manager()->deadHist1D(); } } int HepHist1DArray::length() const { return data_.size(); } int HepHist1DArray::nBins() const { return nBins_; } float HepHist1DArray::min() const { return min_; } float HepHist1DArray::max() const { return max_; } // ---------------------------------------------------------------------- ZM_END_NAMESPACE( zmel ) /* } // namespace zmel */