// TupleManager.cpp #include "TupleManager.h" #ifndef NO_HEPTUPLE #include "HepTuple/HepHBookFileManager.h" #endif #include #include #include using std::string; using std::ifstream; //********************************************************************** // Static data. //********************************************************************** Ptr TupleManager::_pthis(0); //********************************************************************** // Local methods. //********************************************************************** namespace { bool exist_file(const string& name) { ifstream file(name.c_str()); bool result = file.good() && file.is_open(); return result; } } //********************************************************************** // Static methods //********************************************************************** // Return an ntuple. // Create if neccessary. Tuple& TupleManager::tuple(const char* name) { // if manager does not exist, create it. if ( ! _pthis ) _pthis = new TupleManager; // If tuple has already been defined, return it. TupleMap::const_iterator itup = _pthis->_tuples.find(name); if ( itup != _pthis->_tuples.end() ) return *(*itup).second; // Create the ntuple. #ifndef NO_HEPTUPLE TuplePtr ptup( &_pthis->_pmgr->ntuple(name) ); #else TuplePtr ptup = new Tuple( *_pthis->_pmgr->ntuple(name) ); #endif // Store in map and return reference. _pthis->_tuples[name] = ptup; return *ptup; } //********************************************************************** // Return an ntuple with string argument. Tuple& TupleManager::tuple(string name) { return tuple( name.c_str() ); } //********************************************************************** // Return true if an ntuple has been defined. bool TupleManager::is_tuple(string name) { if ( ! _pthis ) return false; return ( _pthis->_tuples.find(name) != _pthis->_tuples.end() ); } //********************************************************************** #ifndef NO_HEPTUPLE // Create a 1D histogram Hist1D& TupleManager::hist1d(std::string title, int nbins, float low, float high) { // if manager does not exist, create it. if ( ! _pthis ) _pthis = new TupleManager; // It is an error if the specified histogram already exists. int n = _pthis->_hist1ds.count(title); assert(n == 0); if(n != 0) abort(); // Create the histogram and add it to the map.. Hist1DPtr phist( &_pthis->_pmgr->hist1D(title, nbins, low, high) ); // Store in map and return reference. _pthis->_hist1ds[title] = phist; return *phist; } //********************************************************************** // Fetch an already existing 1D histogram. Hist1D& TupleManager::hist1d(std::string title) { // It is an error if the specified histogram does not already exist. assert( _pthis != 0); if(_pthis == 0) abort(); int n = _pthis->_hist1ds.count(title); assert(n == 1); if(n != 1) abort(); // Fetch the histogram. Hist1DPtr phist = (*_pthis->_hist1ds.find(title)).second; return *phist; } //********************************************************************** // Check if the specified 1D histogram has been defined. bool TupleManager::is_hist1d(std::string title) { if(_pthis == 0) return false; return _pthis->_hist1ds.count(title) > 0; } #endif //********************************************************************** // methods //********************************************************************** // constructor TupleManager::TupleManager() : _pmgr(0) { string tuple_file("trf.hbk"); string bak_file("trf.hbk.bak"); if(exist_file(bak_file)) remove(bak_file.c_str()); if(exist_file(tuple_file)) rename(tuple_file.c_str(), bak_file.c_str()); #ifdef NO_HEPTUPLE _pmgr = new HBookFile(tuple_file,21); #else _pmgr = new HepHBookFileManager(tuple_file); #endif } //********************************************************************** // destructor TupleManager::~TupleManager() { // check the pointer is valid assert( _pmgr != 0 ); // write out the file #ifdef NO_HEPTUPLE _pmgr->write(); #endif } //**********************************************************************