HistMan Class Reference

manage histograms. More...

#include <HistMan.h>

List of all members.

Public Member Functions

 HistMan (const char *base_directory="")
 HistMan (TFile &file, bool attach=true)
 HistMan (TFolder *folder, bool own=true)
 HistMan (const char **file_list, const char **hist_list)
 ~HistMan ()
 Destructor deletes folder if owned.
TFolder & BaseFolder ()
 Get the base HistMan. It will will create if not yet existing.
void RegisterWithRoot ()
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)
template<class THType >
THType * Get (const char *pathname)
TObject * GetObject (const char *pathname)
 Get the object 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)
 A simplified histogram API is exported with the methods below.
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)
bool Fill1d (const char *pathname, Axis_t x, Stat_t w=1.0)
bool Fill2d (const char *pathname, Axis_t x, Axis_t y, Stat_t w=1.0)
bool FillProfile (const char *pathname, Axis_t x, Axis_t y, Stat_t w=1.0)

Private Attributes

TFolder * fFolder
bool fOwn

Detailed Description

manage histograms.

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/HEAD/HistMan.h.


Constructor & Destructor Documentation

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 71 of file HistMan/HEAD/HistMan.cxx.

References BaseFolder(), fFolder, fOwn, and mkdir_p().

00072 {
00073     fFolder = &this->BaseFolder();
00074     fFolder = &mkdir_p(*fFolder,base_directory);
00075     fOwn = false;
00076     fFolder->SetOwner(true);
00077 }

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 111 of file HistMan/HEAD/HistMan.cxx.

References BaseFolder(), directory_to_folder(), fFolder, fOwn, and mkdir_p().

00112 {
00113     TDirectory* dir = dynamic_cast<TDirectory*>(file.Get("HistMan"));
00114     if (!dir) {
00115         cerr << "Failed to find HistMan folder in " << file.GetName() << endl;
00116         fFolder = &this->BaseFolder();
00117         fFolder = &mkdir_p(*fFolder,"");
00118         fOwn = false;
00119         fFolder->SetOwner(true);
00120         return;
00121     }
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 60 of file HistMan/HEAD/HistMan.cxx.

References fFolder, Form(), and histcount.

00061     : fFolder(folder)
00062     , fOwn(own)
00063 {
00064     if (!fFolder) {
00065         fFolder = new TFolder(Form("HistFolder%d",histcount),
00066                               Form("Histogram Folder #%d",histcount));
00067     }
00068     fFolder->SetOwner(true);
00069 }

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/HEAD/HistMan.cxx.

References Adopt(), BaseFolder(), fFolder, fOwn, Get(), gSystem(), and mkdir_p().

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/HEAD/HistMan.cxx.

References fFolder, and fOwn.

00185 {
00186     if (fOwn) {
00187         //cerr<< "HistMan::~HistMan: Deleting file\n";
00188         delete fFolder;
00189     }
00190     fFolder = 0;
00191 }


Member Function Documentation

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/HEAD/HistMan.cxx.

References fFolder, and mkdir_p().

Referenced by NueSensitivity::Analysis(), LossModule::BeginJob(), HadMuMonModule::BeginJob(), NpotModule::BeginJob(), TempModule::BeginJob(), TargetModule::BeginJob(), HornModule::BeginJob(), Book(), Npot::Book(), AtNuShieldPerformance::Finish(), ShowerEnergyPerf::Finish(), TrackRecoPerf::Finish(), ShowerRecoPerf::Finish(), HistMan(), CamEvd::MakeCanvases(), TrackRecoPerf::MakeHistos(), ShowerRecoPerf::MakeHistos(), ShowerEnergyPerf::MakeHistos(), ShowerDirectionPerf::MakeHistos(), and THMtest::Test1().

00262 {
00263     TH1* hist = dynamic_cast<TH1*>(obj);
00264     if (hist)
00265         hist->SetDirectory(0);
00266     TFolder* folder = &mkdir_p(*fFolder,dirpath);
00267     if (folder->FindObject(obj->GetName())) { // FindObject(Object*) not implemented!
00268         cerr << "Object: " << dirpath << "/" 
00269              << obj->GetName() << " already exists\n";
00270         delete obj;
00271         return 0;
00272     }
00273     folder->Add(obj);
00274     return obj;
00275 }

TFolder & HistMan::BaseFolder (  ) 

Get the base HistMan. It will will create if not yet existing.

Definition at line 193 of file HistMan/HEAD/HistMan.cxx.

Referenced by HistMan(), RegisterWithRoot(), CDFMonitoringModuleImp::Update(), and PlotMan::UpdateCanvases().

00194 {
00195     TFolder* folder = dynamic_cast<TFolder*>(gROOT->FindObjectAny("HistMan"));
00196     if (folder) return *folder;
00197 
00198     folder = new TFolder("HistMan","Base Histogram Manager Folder");
00199     gROOT->Add(folder);
00200     return *folder;
00201 }

template<class THType >
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 132 of file HistMan/HEAD/HistMan.h.

References Adopt().

00135                                                             {
00136         THType* h = new THType(name,title,nbinsx,xmin,xmax, nbinsy,ymin,ymax);
00137         if ( sumw2 ) {
00138             h->Sumw2();
00139         }
00140         TObject* o = Adopt(path, h);
00141         return dynamic_cast<THType*>(o);
00142     }

template<class THType >
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]
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 276 of file HistMan/HEAD/HistMan.cxx.

References fFolder.

Referenced by BDAnaModule::Ana(), BDataQualityModule::Ana(), NueReadTJPID::Ana(), CompareMST::Ana(), NueReadwPID::Ana(), BDCheckDB::CheckSpill(), FitGC::DoFit(), DeltaT::Fill(), ProtonDist::Fill(), MaxDaeDt::Fill(), fill(), BDataQualityModule::FillFile(), CompareMD::FillFromList(), FitGC::GetSiglin(), horn_off(), THMtest::Test1(), THMtest::Test2(), and tortgt_ok().

00277 {
00278     TObject* o = fFolder->FindObject(pathname);
00279     if (!o) o = fFolder->FindObjectAny(pathname);
00280     TH1* h = dynamic_cast<TH1*>(o);
00281     if (!h) {
00282         cerr << "Fill1d(\""<<pathname<<"\") failed lookup\n";
00283         return false;
00284     }
00285     h->Fill(x,w);
00286     return true;
00287 }

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 288 of file HistMan/HEAD/HistMan.cxx.

References fFolder.

Referenced by NueSensitivity::Ana(), FitGC::DoFit(), DeltaT::Fill(), and ProtonDist::Fill().

00289 {
00290     TObject* o = fFolder->FindObject(pathname);
00291     if (!o) o = fFolder->FindObjectAny(pathname);
00292     TH2* h = dynamic_cast<TH2*>(o);
00293     if (!h) {
00294         cerr << "Fill2d(\""<<pathname<<"\") failed lookup\n";
00295         return false;
00296     }
00297     h->Fill(x,y,w);
00298     return true;
00299 }

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 301 of file HistMan/HEAD/HistMan.cxx.

References fFolder.

00302 {
00303     TObject* o = fFolder->FindObject(pathname);
00304     if (!o) o = fFolder->FindObjectAny(pathname);
00305     TProfile* h = dynamic_cast<TProfile*>(o);
00306     if (!h) {
00307         cerr << "FillProfile(\""<<pathname<<"\") failed lookup\n";
00308         return false;
00309     }
00310     h->Fill(x,y,w);
00311     return true;
00312 }

template<class THType >
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/HEAD/HistMan.h.

References GetObject().

Referenced by EvtAddRawDigitBlockModule::Ana(), BDAnaModule::Ana(), EvtInfoInjectorModule::Ana(), NueSensitivity::Analysis(), BDataQualityModule::BeginFile(), FitGC::DoFit(), BDataQualityModule::EndFile(), Npot::Fill(), BDataQualityModule::FillFile(), ValVtxModule::FillPlots(), Pedestals::GeneratePeds(), FitGC::GetSiglin(), HistMan(), TorCheck::Init(), DevCount::Init(), DbCheck::Init(), Earliest::Init(), PerTime::Init(), make_prints(), FitGC::OpenFile(), pedmaker(), FitGC::PlotPL(), MnvRawDigitSuppression::Reco(), THMtest::Test1(), THMtest::Test1_1(), THMtest::Test2(), THMtest::Test3(), and FitGC::WriteDB().

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 315 of file HistMan/HEAD/HistMan.cxx.

References fFolder, and parse_path().

Referenced by TargetModule::Fill(), LossModule::Fill(), and Get().

00316 {
00317     //cerr << "Getting object at " << pathname << endl;
00318     if (!fFolder) return 0;
00319     vector<string> path = parse_path(pathname);
00320 
00321     TFolder* folder = fFolder;
00322 
00323     for (size_t ind=0; ind < path.size(); ++ind) {
00324         //cerr << "Checking " << path[ind] << endl;
00325         TObject* o = folder->FindObject(path[ind].c_str());
00326         TFolder* f = dynamic_cast<TFolder*>(o);
00327         if (!f) return o;
00328         folder = f;
00329     }
00330     return 0;
00331 }

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/HEAD/HistMan.cxx.

References base, BaseFolder(), fFolder, and fOwn.

00203 {
00204     if (!fOwn) return;
00205     fOwn = false;
00206 
00207     TFolder& base = this->BaseFolder();
00208     base.Add(fFolder);
00209 }

void HistMan::WriteOut ( const char *  filename  ) 

Write out hierachy to recreated file of given name.

Definition at line 253 of file HistMan/HEAD/HistMan.cxx.

References WriteOut().

00254 {
00255     TFile f(filename,"recreate");
00256     this->WriteOut(f);
00257     f.Close();
00258 }

void HistMan::WriteOut ( TFile &  opened_file  ) 

Member Data Documentation

TFolder* HistMan::fFolder [private]
bool HistMan::fOwn [private]

Definition at line 40 of file HistMan/HEAD/HistMan.h.

Referenced by HistMan(), RegisterWithRoot(), and ~HistMan().


The documentation for this class was generated from the following files:

Generated on 22 Nov 2017 for loon by  doxygen 1.6.1