Accessing all histograms inside a rootfile

Ah, I see! I assumed

was for the output file.

This is how it is for the input file I’m trying to read:

root [0] 
Attaching file merged_160919_104734.root as _file0...
root [1] .ls
TFile**		merged_160919_104734.root	
 TFile*		merged_160919_104734.root	
  KEY: TH1D	hEtaX;1	Eta distribution for X planes
  KEY: TH1D	hEtaY;1	Eta distribution for Y planes
  KEY: TH1D	hVAEnergyX_48_0_0;1	Energy for ladder 48 X VA 0 #eta region 0
  KEY: TH1D	hVAEnergyY_0_0_0;1	Energy for ladder 0 Y VA 0 #eta region 0
  .
  .
  .

and it continues for ~2k histograms.

So, are there any “TH2D” at all?

Hi Ruina,

this is my bad/typo in my original code. Where I wrote TH2D I meant (as your did TH1D):

std::string inFileName = argv[1];
TFile *f = new TFile(inFileName.c_str());
TList *list = f->GetListOfKeys();
TFile *outFile = new TFile("myfile.root", "recreate");
TKey *key;
TIter iter(list); //or TIter iter(list->MakeIterator());
static TString classname("TH1D");
while((key = (TKey*)iter())) {
    if (key->GetClassName() == classname) {
       TH1D *hist = (TH1D*)key->ReadObj();
       if (hist) {
          output->WriteTObject(hist);
          delete hist;
       }
    }
}
outFile->cd();
outFile->Write();
outFile->Close();

Oh gosh! Even I totally overlooked it! :open_mouth: My apologies!
It works perfectly now! Many thanks to all of you, esp. @pcanal and @Wile_E_Coyote!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.