#include <HistMan.h>
Public Member Functions | |
HistMan (const char *base_directory="") | |
Create a HistMan with a TFolder created in the given base directory. | |
HistMan (TFile &file, bool attach=true) | |
Create a Histman and try to attach the "HistMan" directory in the given file (such as would exist if the file was created by HistMan::WriteOut()). | |
HistMan (TFolder *folder, bool own=true) | |
Create a HistMan. | |
HistMan (const char **file_list, const char **hist_list) | |
Create a HistMan and add a list of histograms from a list of files that have a "HistMan" directory. | |
~HistMan () | |
Destructor deletes folder if owned. | |
TFolder & | BaseFolder () |
Get the base HistMan. It will will create if not yet existing. | |
void | RegisterWithRoot () |
Register the base folder with gROOT, gives up ownership and folder becomes visible in //root/ROOT Memory/HistMan in a TBrowser. | |
void | WriteOut (TFile &opened_file) |
Write hierachy of histograms to given TFile. | |
void | WriteOut (const char *filename) |
Write out hierachy to recreated file of given name. | |
TObject * | Adopt (const char *path, TObject *hist) |
Adopt an already created histogram (or really any TObject), return NULL (and delete hist) if a histogram with same name as "hist" at path already exists, otherwise return the object. | |
template<class THType> | |
THType * | Get (const char *pathname) |
Try to find histogram of with pathname="path/name" in this instances branch of the hierarchy. | |
TObject * | GetObject (const char *pathname) |
Get the object at the given path. | |
TFolder * | GetFolder (const char *pathname) |
Get the folder at the given path. | |
TObject * | GetObjectOrFolder (const char *pathname) |
Get the folder at the given path. | |
std::vector< TObject * > | GetObjects (const char *pathname) |
Get the objects (excluding sub-folders) in the folder. | |
std::vector< TFolder * > | GetSubFolders (const char *pathname) |
Get the folder at the given path. | |
template<class THType> | |
THType * | Book (const char *name, const char *title, int nbinsx, Axis_t xmin, Axis_t xmax, const char *path=".", Bool_t sumw2=kFALSE) |
Book 1D histograms of type THType. | |
template<class THType> | |
THType * | Book (const char *name, const char *title, int nbinsx, Axis_t xmin, Axis_t xmax, int nbinsy, Axis_t ymin, Axis_t ymax, const char *path=".", Bool_t sumw2=kFALSE) |
Book 2D histograms of type THType. | |
bool | Fill1d (const char *pathname, Axis_t x, Stat_t w=1.0) |
Lookup a 1D histogram by pathname="path/name" and Fill() it. | |
bool | Fill2d (const char *pathname, Axis_t x, Axis_t y, Stat_t w=1.0) |
Lookup a 2D histogram by pathname="path/name" and Fill() it. | |
bool | FillProfile (const char *pathname, Axis_t x, Axis_t y, Stat_t w=1.0) |
Lookup a profile histogram by pathname="path/name" and Fill() it. | |
Private Attributes | |
TFolder * | fFolder |
bool | fOwn |
HistMan is a class that simplifies working with histograms. It provides a simple HBOOK like interface to booking and filling histograms. It also handles organizing them in a hierarchy based on named paths very much like the Unix filesystem. Finally it handles file I/O in a way that preserves this organization.
While in memory, the hierarchy is maintained with TFolders and on disk in TDirectories. The hierarchy can either be owned by the HistMan instance, and thus have the same persistency as the instance itself or it can be owned by ROOT. This latter case is the most useful one as it lets one create and destroy HistMan instance at will while the hierarchy is built up.
Regardless of the memory management, a HistMan instance is logically rooted at some point of the hierarchy. That is, any instance can only know about histograms and sub hierarchies that are on the branch on which the instance sits.
Definition at line 38 of file HistMan.h.
HistMan::HistMan | ( | const char * | base_directory = "" |
) |
Create a HistMan with a TFolder created in the given base directory.
This implies that ownership is given to gROOT's memory. Histograms will be visible in "//root/ROOT memory/HistMan/base_directory/" This is the most typical way to use HistMan.
Definition at line 72 of file HistMan.cc.
00073 { 00074 fFolder = &this->BaseFolder(); 00075 fFolder = &mkdir_p(*fFolder,base_directory); 00076 fOwn = false; 00077 fFolder->SetOwner(true); 00078 }
HistMan::HistMan | ( | TFile & | file, | |
bool | attach = true | |||
) |
Create a Histman and try to attach the "HistMan" directory in the given file (such as would exist if the file was created by HistMan::WriteOut()).
If fail, will act like Histman("") above. If successfull, the file is still used to store the histograms so must be kept open while they are to be used. If attach is false, the hierarchy is not attached to ROOT Memory.
Definition at line 112 of file HistMan.cc.
00113 { 00114 TDirectory* dir = dynamic_cast<TDirectory*>(file.Get(baseName)); 00115 if (!dir) { 00116 //cerr << "Failed to find HistMan folder in " << file.GetName() << endl; 00117 fFolder = &this->BaseFolder(); 00118 fFolder = &mkdir_p(*fFolder,""); 00119 fOwn = false; 00120 fFolder->SetOwner(true); 00121 return; 00122 } 00123 fFolder = directory_to_folder(*dir); 00124 if (attach) { 00125 fOwn = false; 00126 gROOT->Add(fFolder); 00127 } 00128 }
HistMan::HistMan | ( | TFolder * | folder, | |
bool | own = true | |||
) |
Create a HistMan.
If "own" is true the folder is deleted along with this object. This will root the hierarchy in a uniquely named root folder (like HistFolderXXX).
Definition at line 61 of file HistMan.cc.
00062 : fFolder(folder) 00063 , fOwn(own) 00064 { 00065 if (!fFolder) { 00066 fFolder = new TFolder(Form("HistFolder%d",histcount), 00067 Form("Histogram Folder #%d",histcount)); 00068 } 00069 fFolder->SetOwner(true); 00070 }
HistMan::HistMan | ( | const char ** | file_list, | |
const char ** | hist_list | |||
) |
Create a HistMan and add a list of histograms from a list of files that have a "HistMan" directory.
If the file does not exist, it will print out a warning and ignore this file. If the histogram does not exist in the file, it will do the same. Both the list of files and histograms should end with a 0, e.g. const char* file_list[] = {"file1","file2",0};
Definition at line 130 of file HistMan.cc.
00131 { 00132 fFolder = &this->BaseFolder(); 00133 fFolder = &mkdir_p(*fFolder,""); 00134 fOwn = false; 00135 fFolder->SetOwner(true); 00136 // 00137 for (int ifile=0; file_list[ifile]; ++ifile){ 00138 // check first if there the file exists, if not, print out a 00139 // warning message and continue with the other files 00140 if (gSystem->AccessPathName(file_list[ifile])) { 00141 cout << "HistMan: Warning: file " << file_list[ifile] 00142 << " does not exist!" << endl; 00143 continue; 00144 } 00145 TFile file(file_list[ifile]); 00146 if (!file.IsOpen()) { 00147 cout << "HistMan: Warning: problems opening file " 00148 << file_list[ifile] << endl; 00149 continue; 00150 } 00151 HistMan hm(file,false); 00152 for (int ihist=0; hist_list[ihist]; ++ihist){ 00153 // Get a pointer to the histogram in the old file 00154 TH1* hold = hm.Get<TH1>(hist_list[ihist]); 00155 if (!hold) { 00156 cout << "HistMan: Warning: histogram named " 00157 << hist_list[ihist] << " does not exist in file " 00158 << file_list[ifile] << endl; 00159 continue; 00160 } 00161 00162 // try to get a pointer to the hitogram in the new 00163 // HistMan. If it exists, add the old histogram to it. I 00164 // it doesn't, adopt the old histo 00165 TH1* hnew = this->Get<TH1>(hist_list[ihist]); 00166 if (hnew) 00167 hnew->Add(hold); 00168 else{ 00169 string hname_fullp(hist_list[ihist]); 00170 string::size_type pos = hname_fullp.rfind("/"); 00171 string hname_path; 00172 if (pos < hname_fullp.length()) 00173 hname_path = hname_fullp.substr(0,pos); 00174 else 00175 hname_path = ""; 00176 00177 this->Adopt(hname_path.c_str(),dynamic_cast<TH1*>(hold->Clone())); 00178 } 00179 } 00180 file.Close(); 00181 } 00182 }
HistMan::~HistMan | ( | ) |
Destructor deletes folder if owned.
Definition at line 184 of file HistMan.cc.
00185 { 00186 if (fOwn) { 00187 //cerr<< "HistMan::~HistMan: Deleting file\n"; 00188 delete fFolder; 00189 } 00190 fFolder = 0; 00191 }
TFolder & HistMan::BaseFolder | ( | ) |
Get the base HistMan. It will will create if not yet existing.
Definition at line 193 of file HistMan.cc.
00194 { 00195 TFolder* folder = dynamic_cast<TFolder*>(gROOT->FindObjectAny(baseName)); 00196 if (folder) return *folder; 00197 00198 folder = new TFolder(baseName,"Base Histogram Manager Folder"); 00199 gROOT->Add(folder); 00200 return *folder; 00201 }
void HistMan::RegisterWithRoot | ( | ) |
Register the base folder with gROOT, gives up ownership and folder becomes visible in //root/ROOT Memory/HistMan in a TBrowser.
This is only needed if the instance is created with HistMan(TFolder*,bool).
Definition at line 202 of file HistMan.cc.
00203 { 00204 if (!fOwn) return; 00205 fOwn = false; 00206 00207 TFolder& base = this->BaseFolder(); 00208 base.Add(fFolder); 00209 }
void HistMan::WriteOut | ( | TFile & | opened_file | ) |
Write hierachy of histograms to given TFile.
Definition at line 248 of file HistMan.cc.
00249 { 00250 if (!fFolder) return; 00251 folder_to_directory(fFolder,file); 00252 }
void HistMan::WriteOut | ( | const char * | filename | ) |
Write out hierachy to recreated file of given name.
Definition at line 253 of file HistMan.cc.
00254 { 00255 TFile f(filename,"recreate"); 00256 this->WriteOut(f); 00257 f.Close(); 00258 }
TObject * HistMan::Adopt | ( | const char * | path, | |
TObject * | hist | |||
) |
Adopt an already created histogram (or really any TObject), return NULL (and delete hist) if a histogram with same name as "hist" at path already exists, otherwise return the object.
An empty or NULL string can be passed for the path to place the object in HistMan instance's top level folder.
Definition at line 261 of file HistMan.cc.
00262 { 00263 TH1* hist = dynamic_cast<TH1*>(obj); 00264 if (hist) 00265 hist->SetDirectory(0); 00266 TTree* tree = dynamic_cast<TTree*>(obj); 00267 if (tree) 00268 tree->SetDirectory(0); 00269 TFolder* folder = &mkdir_p(*fFolder,dirpath); 00270 if (folder->FindObject(obj->GetName())) { // FindObject(Object*) not implemented! 00271 cerr << "Object: " << dirpath << "/" 00272 << obj->GetName() << " already exists\n"; 00273 delete obj; 00274 return 0; 00275 } 00276 folder->Add(obj); 00277 00278 return obj; 00279 }
THType* HistMan::Get | ( | const char * | pathname | ) | [inline] |
Try to find histogram of with pathname="path/name" in this instances branch of the hierarchy.
Path is relative to this objects TFolder. Returns NULL if histogram does not exist or if there is a type mismatch.
Definition at line 103 of file HistMan.h.
00103 { 00104 return dynamic_cast<THType*>(this->GetObject(pathname)); 00105 }
TObject * HistMan::GetObject | ( | const char * | pathname | ) |
Get the object at the given path.
Definition at line 319 of file HistMan.cc.
00319 { 00320 TObject* obj = GetObjectOrFolder(pathname); 00321 if(!obj) return 0; 00322 // Don't return folders 00323 TFolder* f = dynamic_cast<TFolder*>(obj); 00324 if ( f ) return 0; 00325 return obj; 00326 }
TFolder * HistMan::GetFolder | ( | const char * | pathname | ) |
Get the folder at the given path.
Definition at line 328 of file HistMan.cc.
00328 { 00329 TObject* obj = GetObjectOrFolder(pathname); 00330 if(!obj) return 0; 00331 // Only return folders 00332 TFolder* f = dynamic_cast<TFolder*>(obj); 00333 if ( !f ) return 0; 00334 return f; 00335 }
TObject * HistMan::GetObjectOrFolder | ( | const char * | pathname | ) |
Get the folder at the given path.
Definition at line 337 of file HistMan.cc.
00338 { 00339 if (!fFolder) return 0; 00340 vector<string> path = parse_path(pathname); 00341 TFolder* folder = fFolder; 00342 for (size_t ind=0; ind < path.size(); ++ind) { 00343 //cerr << "Checking " << path[ind] << endl; 00344 TObject* obj = folder->FindObject(path[ind].c_str()); 00345 if( !obj ) return 0; 00346 if( ind == (path.size()-1) ) return obj; 00347 TFolder* f = dynamic_cast<TFolder*>(obj); 00348 if ( !f ) return 0; 00349 folder = f; 00350 } 00351 return folder; 00352 }
vector< TObject * > HistMan::GetObjects | ( | const char * | pathname | ) |
Get the objects (excluding sub-folders) in the folder.
Definition at line 354 of file HistMan.cc.
00354 { 00355 // Return the list of Objects (non-folders) in this folder 00356 vector<TObject*> objects; 00357 TFolder* currentFolder = this->GetFolder(pathname); 00358 if(!currentFolder) return objects; 00359 TCollection* contents = currentFolder->GetListOfFolders(); 00360 TIter next(contents); 00361 TObject* obj = 0; 00362 while( (obj = next()) ){ 00363 TFolder* folder = dynamic_cast<TFolder*>(obj); 00364 if(folder) continue; // Ignore sub-folders 00365 objects.push_back(obj); 00366 } 00367 return objects; 00368 }
vector< TFolder * > HistMan::GetSubFolders | ( | const char * | pathname | ) |
Get the folder at the given path.
Definition at line 370 of file HistMan.cc.
00370 { 00371 // Return the list of sub-folders in this folder 00372 vector<TFolder*> folders; 00373 TFolder* currentFolder = this->GetFolder(pathname); 00374 if(!currentFolder) return folders; 00375 TCollection* contents = currentFolder->GetListOfFolders(); 00376 TIter next(contents); 00377 TObject* obj = 0; 00378 while( (obj = next()) ){ 00379 TFolder* folder = dynamic_cast<TFolder*>(obj); 00380 if(!folder) continue; // Ignore non-folders 00381 folders.push_back(folder); 00382 } 00383 return folders; 00384 }
THType* HistMan::Book | ( | const char * | name, | |
const char * | title, | |||
int | nbinsx, | |||
Axis_t | xmin, | |||
Axis_t | xmax, | |||
const char * | path = "." , |
|||
Bool_t | sumw2 = kFALSE | |||
) | [inline] |
Book 1D histograms of type THType.
Return pointer to created histogram (HistMan retains ownership) or 0 if "path/name" conflicts with previously existing histogram.
Definition at line 125 of file HistMan.h.
00127 { 00128 THType* h = new THType(name,title,nbinsx,xmin,xmax); 00129 if ( sumw2 ) { 00130 h->Sumw2(); 00131 } 00132 TObject* o = Adopt(path, h); 00133 return dynamic_cast<THType*>(o); 00134 }
THType* HistMan::Book | ( | const char * | name, | |
const char * | title, | |||
int | nbinsx, | |||
Axis_t | xmin, | |||
Axis_t | xmax, | |||
int | nbinsy, | |||
Axis_t | ymin, | |||
Axis_t | ymax, | |||
const char * | path = "." , |
|||
Bool_t | sumw2 = kFALSE | |||
) | [inline] |
Book 2D histograms of type THType.
Return pointer to created histogram (HistMan retains ownership) or 0 if "path/name" conflicts with previously existing histogram.
Definition at line 140 of file HistMan.h.
00143 { 00144 THType* h = new THType(name,title,nbinsx,xmin,xmax, nbinsy,ymin,ymax); 00145 if ( sumw2 ) { 00146 h->Sumw2(); 00147 } 00148 TObject* o = Adopt(path, h); 00149 return dynamic_cast<THType*>(o); 00150 }
bool HistMan::Fill1d | ( | const char * | pathname, | |
Axis_t | x, | |||
Stat_t | w = 1.0 | |||
) |
Lookup a 1D histogram by pathname="path/name" and Fill() it.
If lookup or any casting fails, false is returned and an error message is printed at Msg level Warning.
Definition at line 280 of file HistMan.cc.
00281 { 00282 TObject* o = fFolder->FindObject(pathname); 00283 if (!o) o = fFolder->FindObjectAny(pathname); 00284 TH1* h = dynamic_cast<TH1*>(o); 00285 if (!h) { 00286 cerr << "Fill1d(\""<<pathname<<"\") failed lookup\n"; 00287 return false; 00288 } 00289 h->Fill(x,w); 00290 return true; 00291 }
bool HistMan::Fill2d | ( | const char * | pathname, | |
Axis_t | x, | |||
Axis_t | y, | |||
Stat_t | w = 1.0 | |||
) |
Lookup a 2D histogram by pathname="path/name" and Fill() it.
If lookup or any casting fails, false is returned and an error message is printed at Msg level Warning.
Definition at line 292 of file HistMan.cc.
00293 { 00294 TObject* o = fFolder->FindObject(pathname); 00295 if (!o) o = fFolder->FindObjectAny(pathname); 00296 TH2* h = dynamic_cast<TH2*>(o); 00297 if (!h) { 00298 cerr << "Fill2d(\""<<pathname<<"\") failed lookup\n"; 00299 return false; 00300 } 00301 h->Fill(x,y,w); 00302 return true; 00303 }
bool HistMan::FillProfile | ( | const char * | pathname, | |
Axis_t | x, | |||
Axis_t | y, | |||
Stat_t | w = 1.0 | |||
) |
Lookup a profile histogram by pathname="path/name" and Fill() it.
If lookup or any casting fails, false is returned and an error message is printed at Msg level Warning.
Definition at line 305 of file HistMan.cc.
00306 { 00307 TObject* o = fFolder->FindObject(pathname); 00308 if (!o) o = fFolder->FindObjectAny(pathname); 00309 TProfile* h = dynamic_cast<TProfile*>(o); 00310 if (!h) { 00311 cerr << "FillProfile(\""<<pathname<<"\") failed lookup\n"; 00312 return false; 00313 } 00314 h->Fill(x,y,w); 00315 return true; 00316 }
TFolder* HistMan::fFolder [private] |
bool HistMan::fOwn [private] |