// ---------------------------------------------------------------------- // // HepRootObj.cc -- implementation for the Root helper class // #include "ZMutility/ZMenvironment.h" #include "HepTuple/HepRootObj.h" #include #include #include #include #include "HepTuple/HepID.h" #include "HepTuple/Exceptions.h" #include USING( std::string ) // ********************************************************************** // Constructors HepRootObj::HepRootObj( TNamed* what ) : data_(what) {} HepRootObj::HepRootObj( const HepRootObj& base ) : data_(base.data_) {} // ********************************************************************** // Destructors HepRootObj::~HepRootObj() {} #include "ZMutility/iostream" // ********************************************************************** // Get/Set the pointer field. TNamed* HepRootObj::data () const { return data_; } TNamed* HepRootObj::data (TNamed* set) { if ( set != 0 ) { if ( data_ != 0 ) { ZMthrow(ZMxHepImproperUse("Can not change the pointer to TNamed")); } else { data_ = set; } } return data_; } // ********************************************************************** // Write void HepRootObj::Write( char* name, Int4 option, Int4 bufsize ) { if ( data_ != NULL ) { if( option == TObject::kOverwrite ) { TKey * key = (TKey*)gDirectory->GetListOfKeys()->FindObject(data_->GetName()); Int_t ok = data_->Write(name,0,bufsize); if( ok && key ) { key->Delete(); delete key; } } else { data_->Write(name,option,bufsize); } gDirectory->SaveSelf(kTRUE); } } // ********************************************************************** // mkname // Construct names (keys) for histos and ntuples. When hhid contains 0, // a key using the prefix and a running counter is created: #include "ZMutility/itos.h" const string& HepRootObj::mkname(const string& title, const HepID& id) { static string name; static string prefix; name = title; int index = 0; if( id.isIDstringSet() ) { name = id.IDstring(); } if ( gDirectory->GetKey(name.c_str(), 9999) == NULL ) { return name; } prefix = name; if( id.isIDnumberSet() ) { if( id.IDnumber() != 0 ) { index = id.IDnumber(); name += itos( index ); } } while ( gDirectory->GetKey(name.c_str(), 9999) != NULL ) { index ++; name = prefix + itos( index ); } return name; }