Creation of a hierarchy of histograms in direrctories


I’ve i have been following the example “Creation of a hierarchy of histograms in direrctories” in the root tutorials. I have been successful at creating the histograms, but have problems trying fill them.

As i understand, I have created a TFile containing sub directories, and each sub directory contains a histogram. The question is what is the easiest way to return to those histograms in order to fill and plot them?


Tim Woolliscroft

Hi Tim,

[code]void FillHist(const char* name, TH1* hist) {
if (hist->InheritsFrom(TH2::Class())
if (strstr(name,“take5”)) hist->Fill(5., 5.);
else hist->Fill(1.,1.);
else hist->Fill(1.);
void LoopOverHists(TDirectory* dir) {
TIter iKey(dir->GetListOfKeys());
TKey* key=0;
while((key=(TKey*)iKey())) {
TH1* hist=key->ReadObjectAny(TH1::Class());
const char* name=Form("%s/%s", dir->GetName(), key->GetName());
FillHist(name, hist);

void LoopOverSubDirs(TFile* file) {
TIter iKey(file->GetListOfKeys());
TKey* key=0;
while((key=(TKey*)iKey())) {
TDirectory* dir=key->ReadObjectAny(TDirectory::Class());
if (dir) LoopOverHists(dir);

void LoopOverFile(const char* filename) {
TFile *file=new TFile(filename);
(I’ve only split the code into methods to make it easier to understand what’s happening where).