Fit multiple histograms at once

Hi,

I do have 64 histograms of the same type. They are all 2D histograms, I can fit them individually using fit2d.C. I tried to make it at once by following code

{
#include "Riostream.h"

  Double_t par[3];
  TFile file("Scatter.root"); 
  Fit = new TF1("Fit", "pol2", -0.4, 0);

  // Create file to write output information.
  ofstream OutFile;
  OutFile.open("ScatterFitOut.txt",ios::out);
 
  for (Int_t i=0; i<64; i++)
    {
      TCanvas *Canvas1 = new TCanvas("Canvas1","Scatter Plot");
      TCanvas *Canvas2 = new TCanvas("Canvas2","Fit to Plot");
      Canvas1->Divide(1,1);
      Canvas1->cd(1);
      h20[i]->Draw();
      Canvas2->Divide(1,1);
      Canvas2->cd(1);
      
      //use a TProfile to convert the 2-d to 1-d problem
      TProfile *Profile = h20[i]->ProfileX();
      Profile->Fit(Fit,"R");
      Fit->GetParameters(&par[0]);
      OutFile  << i << " " << par[0] << " " << -par[1] << " " << +par[2] << endl;
    }    
}

It gives me the following error.

Error: Symbol h20[i] is not defined in current scope  FitAll.C:21:
Error: Failed to evaluate h20[i]->Draw()

How can I resolve this?

Thanks.

Where do you declare and fill your array h20?
The error message seems to be very clear!

Rene

Hi,

It is defined in Scatter.root file. So it should be fine. I can read individually not in a loop.

Thanks

These objects are may be in your ROOT file, but I do not see any code creating the array h20 in memory. When you have a ROOT file containg histograms named “h1”,“h2”, etc , “h20” in the ROOT file, you need code like

TFile *f = new TFile("myfile.root"); TH1 *h20[20]; for (int i=0;i<20;i++) { h20[i] = (TH1*)f->Get(Form("h%d",i+1)); }
Rene

I see. I modified part of your code and worked. Thanks a lot!