/search.css" rel="stylesheet" type="text/css"/> /search.js">
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

In This Package:

Public Member Functions | Private Attributes
HistMan Class Reference

manage histograms. More...

#include <HistMan.h>

List of all members.

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)
 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)
 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

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.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 72 of file HistMan.cc.

{
    fFolder = &this->BaseFolder();
    fFolder = &mkdir_p(*fFolder,base_directory);
    fOwn = false;
    fFolder->SetOwner(true);
}
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.

{
    TDirectory* dir = dynamic_cast<TDirectory*>(file.Get(baseName));
    if (!dir) {
      //cerr << "Failed to find HistMan folder in " << file.GetName() << endl;
        fFolder = &this->BaseFolder();
        fFolder = &mkdir_p(*fFolder,"");
        fOwn = false;
        fFolder->SetOwner(true);
        return;
    }
    fFolder = directory_to_folder(*dir);
    if (attach) {
        fOwn = false;
        gROOT->Add(fFolder);
    }
}
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.

    : fFolder(folder)
    , fOwn(own)
{
    if (!fFolder) {
        fFolder = new TFolder(Form("HistFolder%d",histcount),
                              Form("Histogram Folder #%d",histcount));
    }
    fFolder->SetOwner(true);
}
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.

{
    fFolder = &this->BaseFolder();
    fFolder = &mkdir_p(*fFolder,"");
    fOwn = false;
    fFolder->SetOwner(true);
    //
    for (int ifile=0; file_list[ifile]; ++ifile){
        // check first if there the file exists, if not, print out a
        // warning message and continue with the other files        
        if (gSystem->AccessPathName(file_list[ifile])) {
            cout << "HistMan: Warning: file " <<  file_list[ifile]
                 << " does not exist!" << endl;
            continue;
        }
        TFile file(file_list[ifile]);  
        if (!file.IsOpen()) {
            cout << "HistMan: Warning: problems opening file "
                 <<  file_list[ifile] << endl;
            continue;
        }
        HistMan hm(file,false);
        for (int ihist=0; hist_list[ihist]; ++ihist){           
            // Get a pointer to the histogram in the old file
            TH1* hold = hm.Get<TH1>(hist_list[ihist]);
            if (!hold) {
                cout << "HistMan: Warning: histogram named "
                     << hist_list[ihist] << " does not exist in file "
                     <<  file_list[ifile] << endl;
                continue;
            }
            
            // try to get a pointer to the hitogram in the new
            // HistMan. If it exists, add the old histogram to it. I
            // it doesn't, adopt the old histo 
            TH1* hnew = this->Get<TH1>(hist_list[ihist]);
            if (hnew)
                hnew->Add(hold);            
            else{ 
                string hname_fullp(hist_list[ihist]);
                string::size_type pos = hname_fullp.rfind("/"); 
                string hname_path;
                if (pos < hname_fullp.length())
                    hname_path = hname_fullp.substr(0,pos);
                else
                    hname_path = "";

                this->Adopt(hname_path.c_str(),dynamic_cast<TH1*>(hold->Clone()));
            }
        }
        file.Close();
    }
}
HistMan::~HistMan ( )

Destructor deletes folder if owned.

Definition at line 184 of file HistMan.cc.

{
    if (fOwn) {
        //cerr<< "HistMan::~HistMan: Deleting file\n";
        delete fFolder;
    }
    fFolder = 0;
}

Member Function Documentation

TFolder & HistMan::BaseFolder ( )

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

Definition at line 193 of file HistMan.cc.

{
    TFolder* folder = dynamic_cast<TFolder*>(gROOT->FindObjectAny(baseName));
    if (folder) return *folder;

    folder = new TFolder(baseName,"Base Histogram Manager Folder");
    gROOT->Add(folder);
    return *folder;
}
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.

{
    if (!fOwn) return;
    fOwn = false;

    TFolder& base = this->BaseFolder();
    base.Add(fFolder);
}
void HistMan::WriteOut ( TFile &  opened_file)

Write hierachy of histograms to given TFile.

Definition at line 248 of file HistMan.cc.

{
    if (!fFolder) return;
    folder_to_directory(fFolder,file);
}
void HistMan::WriteOut ( const char *  filename)

Write out hierachy to recreated file of given name.

Definition at line 253 of file HistMan.cc.

{
    TFile f(filename,"recreate");
    this->WriteOut(f);
    f.Close();
}
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.

{
    TH1* hist = dynamic_cast<TH1*>(obj);
    if (hist)
        hist->SetDirectory(0);
    TTree* tree = dynamic_cast<TTree*>(obj);
    if (tree)
        tree->SetDirectory(0);
    TFolder* folder = &mkdir_p(*fFolder,dirpath);
    if (folder->FindObject(obj->GetName())) { // FindObject(Object*) not implemented!
        cerr << "Object: " << dirpath << "/" 
             << obj->GetName() << " already exists\n";
        delete obj;
        return 0;
    }
    folder->Add(obj);

    return obj;
}
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.h.

                                      {
        return dynamic_cast<THType*>(this->GetObject(pathname));
    }
TObject * HistMan::GetObject ( const char *  pathname)

Get the object at the given path.

Definition at line 319 of file HistMan.cc.

                                               {
  TObject* obj = GetObjectOrFolder(pathname);
  if(!obj) return 0;
  // Don't return folders
  TFolder* f = dynamic_cast<TFolder*>(obj);
  if ( f ) return 0;
  return obj;
}
TFolder * HistMan::GetFolder ( const char *  pathname)

Get the folder at the given path.

Definition at line 328 of file HistMan.cc.

                                               {
  TObject* obj = GetObjectOrFolder(pathname);
  if(!obj) return 0;
  // Only return folders
  TFolder* f = dynamic_cast<TFolder*>(obj);
  if ( !f ) return 0;
  return f;
}
TObject * HistMan::GetObjectOrFolder ( const char *  pathname)

Get the folder at the given path.

Definition at line 337 of file HistMan.cc.

{
    if (!fFolder) return 0;
    vector<string> path = parse_path(pathname);
    TFolder* folder = fFolder;
    for (size_t ind=0; ind < path.size(); ++ind) {
        //cerr << "Checking " << path[ind] << endl;
        TObject* obj = folder->FindObject(path[ind].c_str());
        if( !obj ) return 0;
        if( ind == (path.size()-1) ) return obj;
        TFolder* f = dynamic_cast<TFolder*>(obj);
        if ( !f ) return 0;
        folder = f;
    }
    return folder;
}
vector< TObject * > HistMan::GetObjects ( const char *  pathname)

Get the objects (excluding sub-folders) in the folder.

Definition at line 354 of file HistMan.cc.

                                                        {
  // Return the list of Objects (non-folders) in this folder
  vector<TObject*> objects;
  TFolder* currentFolder = this->GetFolder(pathname);
  if(!currentFolder) return objects;
  TCollection* contents = currentFolder->GetListOfFolders();
  TIter next(contents);
  TObject* obj = 0;
  while( (obj = next()) ){
    TFolder* folder = dynamic_cast<TFolder*>(obj);
    if(folder) continue; // Ignore sub-folders
    objects.push_back(obj);
  }
  return objects;
}
vector< TFolder * > HistMan::GetSubFolders ( const char *  pathname)

Get the folder at the given path.

Definition at line 370 of file HistMan.cc.

                                                           {
  // Return the list of sub-folders in this folder
  vector<TFolder*> folders;
  TFolder* currentFolder = this->GetFolder(pathname);
  if(!currentFolder) return folders;
  TCollection* contents = currentFolder->GetListOfFolders();
  TIter next(contents);
  TObject* obj = 0;
  while( (obj = next()) ){
    TFolder* folder = dynamic_cast<TFolder*>(obj);
    if(!folder) continue; // Ignore non-folders
    folders.push_back(folder);
  }
  return folders;
}
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]

A simplified histogram API is exported with the methods below.

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.

                                                            {
        THType* h = new THType(name,title,nbinsx,xmin,xmax);
        if ( sumw2 ) {
            h->Sumw2();
        }
        TObject* o = Adopt(path, h);
        return dynamic_cast<THType*>(o);
    }
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 140 of file HistMan.h.

                                                            {
        THType* h = new THType(name,title,nbinsx,xmin,xmax, nbinsy,ymin,ymax);
        if ( sumw2 ) {
            h->Sumw2();
        }
        TObject* o = Adopt(path, h);
        return dynamic_cast<THType*>(o);
    }
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.

{
    TObject* o = fFolder->FindObject(pathname);
    if (!o) o = fFolder->FindObjectAny(pathname);
    TH1* h = dynamic_cast<TH1*>(o);
    if (!h) {
        cerr << "Fill1d(\""<<pathname<<"\") failed lookup\n";
        return false;
    }
    h->Fill(x,w);
    return true;
}
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.

{
    TObject* o = fFolder->FindObject(pathname);
    if (!o) o = fFolder->FindObjectAny(pathname);
    TH2* h = dynamic_cast<TH2*>(o);
    if (!h) {
        cerr << "Fill2d(\""<<pathname<<"\") failed lookup\n";
        return false;
    }
    h->Fill(x,y,w);
    return true;
}
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.

{
    TObject* o = fFolder->FindObject(pathname);
    if (!o) o = fFolder->FindObjectAny(pathname);
    TProfile* h = dynamic_cast<TProfile*>(o);
    if (!h) {
        cerr << "FillProfile(\""<<pathname<<"\") failed lookup\n";
        return false;
    }
    h->Fill(x,y,w);
    return true;
}

Member Data Documentation

TFolder* HistMan::fFolder [private]

Definition at line 39 of file HistMan.h.

bool HistMan::fOwn [private]

Definition at line 40 of file HistMan.h.


The documentation for this class was generated from the following files:
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

Generated on Fri May 16 2014 09:51:37 for HistMan by doxygen 1.7.4