Read histograms from multiple ROOT files

Please read tips for efficient and successful posting and posting code

_ROOT Version: 6.26/11
_Platform: Scientific Linux 6.7
Compiler: Not Provided

Hi everyone,
I have some ROOT files, named HistoOutMcDY_1.root, HistoOutMcDY_2.root and so on. Inside these files there are many TH1 and TH2 histograms, some of them with the same name, and I want to create a macro that opens these files, and plot one or more of them (in a stacked way). The code I wrote so far is the following


void graph(){
  string histogram = "MuonEtaHistoGL"; //name of the histogram inside the file
  TFile *f = new TFile("HistoOutMcDY_1.root", "read");

  TH1F *Histo= (TH1F*)f->Get(histogram.c_str());

This works fine, but now I want to open all the files at the same time, and retrieve the desired histogram (or histograms) but I don’t know how to do that. I thought to use TChain, but (correct me if I’m wrong), it works only if there are Trees inside the file, which is not my case. I thought also to use the hadd method to merge the files, but as far as I know it works only from terminal, whereas i’d like to call it inside the macro. I thought then to use a TList of files, as I saw in this example, but the section to access the single file and objects inside it’s not clear to me. Could someone explain me a way to solve this problem? Or if there’s a better solution to do what I need?

Hi there. I am not an expert but maybe a simple for loop can help. lets say you have n files

TFile *f[n];
for (Int_t  i =0; i < n ; i++) {
f[n] = new TFile (Form("HistoOutMcDY_%d.root",i), "READ");

this can help. right??

Thanks. I thought also of this, but when retrieving an histogram I have to pass to the code the number of the file, for example
TH1F *Histo= (TH1F*)f[i]->Get(histogram.c_str());
I hoped to do something similar to a TChain, like merging all file or maybe all histograms together in a list (maybe a TList?) and then just passing the name of the histogram(s) I want, with the code searching automatically inside the list for that histogram. Is this feasible?