Iterating over histograms defined in current ROOT session?

Hi,

I’d like to do something like in hadd.C, but for the histograms from the current ROOT session. Is there a way of accessing all currently defined TH1 objects - there should be, since ROOT provides tab completion for variable names?
Then, I could do something like:
TIter nextkey( gROOT->GetListOfKeys() );

TKey *key, oldkey=0;
UInt_t numKey = 0;
cout << “Before loop.” << endl;
while ( (key = (TKey
)nextkey())) {
TObject *obj = key->ReadObj();
cout << “Key #” << numKey << ", object pointer: " << obj << endl;

if ( obj->IsA()->InheritsFrom( "TH1" ) ) {

  TH1 *h1 = (TH1*)obj;

  cout << h1->GetName() << endl;
  // ...
  }
  ++numKey;

}

Thanks,
Holger

Hi,

if you created them all in the same place then you can access all histograms from gDirectory (an object of type TDirectory), the way you described.

Cheers, Axel.

Hi,

a colleague from ATLAS (Tayfun Ince incet@uvic.ca) has found a working solution:
TObject* obj = 0;
TIter next(gPad->GetListOfPrimitives());
while ( obj = (TObject*)next() ) {
if ( obj->InheritsFrom(TH1::Class()) ) {
TH1* h1d = (TH1*)obj;
}
}

Thanks,
Holger

Hi,

yes, that works well for all histograms drawn on a given pad; gDirectory->GetList() will iterate over all objects created in all canvases in the current directory.

For completeness: if you want to iterate only over the drawn histograms, you can loop over all pads using gROOT->GetListOfCanvases().

Cheers, Axel.