Add histograms with slightly different bin inside a for loop

I have a root file that consists of histograms of different bins, named histo1, histo2, histo3 and so on.
What I want to do is, add them so as to take the average of them.
The first problem is that to use Add() they should have the same bins. Is there a way to overcome this?

Secondly and more important at the moment, how can I loop over the histograms and access them by their name, since they have the same name, apart from an integer?

I was thinking something like

but I know that I cannot use a string as a histogram pointer. Is there a way around it?

Try: for (int i = min_i; i <= max_i; i++) { TH1 *h; SomeRootFile->GetObject(TString::Format("histo%d", i), h); if (!h) continue; // just a precaution ("histo..." not found) h->SomeHistogramFunction(); }

Thank you soooo much for your post!!!
This is working in general! For instance I could access some data and plot all of them on the same canvas.
However, when I try to add /sum(I want to take the avarage of the historgrams) them using

[code]void access(){

TFile *MyFile = new TFile(“FIMG1.root”,“READ”);
if ( MyFile->IsOpen() ) printf(“Root file opened successfully\n”);

TH1 *h;
TH1 *sum;

for (int i = 1; i <= 12; i++) {
//TH1 *h;
MyFile->GetObject(TString::Format(“FIMG1_EV_%d;1”, i), h);
if (!h) continue; // just a precaution (“histo…” not found)
h->Draw(“same”);//<----- This is working!!!
h->Add(TString::Format(“FIMG1_EV_%d;1”, i),1);//or h>Add(MyFile->GetObject(TString::Format(“FIMG1_EV_%d;1”, i), h),1);
}

}[/code]

I get the error

[quote](in TH1)
/usr/lib64/root/libHist.so -1:-1 0 public: virtual Bool_t TH1::Add(TF1* h1,Double_t c1=1,Option_t* option="");
/usr/lib64/root/libHist.so -1:-1 0 public: virtual Bool_t TH1::Add(const TH1* h1,Double_t c1=1);
/usr/lib64/root/libHist.so -1:-1 0 public: virtual Bool_t TH1::Add(const TH1* h,const TH1* h2,Double_t c1=1,Double_t c2=1); //MENU
*** Interpreter error recovered ***
[/quote]

P.S.: I am trying to find the documentation of GetObject but I cannot find it for TFile, only for TDirectory.

Try: [code]void access(void) {
TFile *MyFile = TFile::Open(“FIMG1.root”, “READ”);
if (!MyFile) return; // just a precaution (“MyFile” not found)

TH1 *sum = ((TH1 *)0);

for (int i = 1; i <= 12; i++) {
TH1 *h;
MyFile->GetObject(TString::Format(“FIMG1_EV_%d;1”, i), h);
if (!h) continue; // just a precaution (“histo…” not found)
if (sum) sum->Add(h); else sum = h;
}

if (sum) sum->Scale((1./12.)); // "average"
if (sum) sum->Draw();
}[/code]
BTW. TFile inherits from TDirectoryFile which inherits from TDirectory which inherits from TNamed which inherits from TObject.

Thank you once again!!!
The problem now is that the histogram don’t have the same number of bins, which was expected…

Error in <TH1F::Add>: Attempt to add histograms with different number of bins Error in <TH1F::Add>: Attempt to add histograms with different number of bins Error in <TH1F::Add>: Attempt to add histograms with different number of bins Error in <TH1F::Add>: Attempt to add histograms with different number of bins Error in <TH1F::Add>: Attempt to add histograms with different number of bins Error in <TH1F::Add>: Attempt to add histograms with different number of bins Error in <TH1F::Add>: Attempt to add histograms with different number of bins Error in <TH1F::Add>: Attempt to add histograms with different number of bins Error in <TH1F::Add>: Attempt to add histograms with different number of bins Error in <TH1F::Add>: Attempt to add histograms with different number of bins Error in <TH1F::Add>: Attempt to add histograms with different number of bins Info in <TCanvas::MakeDefCanvas>: created default TCanvas with name c1

Is there a way to overcome this and sum all the histograms, so as to calculate the mean value?
I think that I must use GetBinContent() and SetBinContent() but I really don’t know how to set this up…:frowning: