Fit does not work in divided canvas

Dear experts,
I want to fit and hist array then plot then in a canvas.
If I do not fit everything works fine, I have all the 16 hist draw in the 16 pad of the canvas:

But when I make the fit on the hist array, it shows only the last hist in the array on 1 canvas (so canvas->Divide(4, 4) is not working anymore):

Do you see what is wrong? I can find the simple code here (1) and the root file (2) to be added at line 5 of (1).

Regards

(1)
http://calpas.web.cern.ch/calpas/pileup.C

(2)
http://calpas.web.cern.ch/calpas/xAOD.340368.physics_Main.HIST.root

Try this:

void pileup(){

   vector<string> vinputfile {"xAOD.340368.physics_Main.HIST.root"};
   vector<string> vdir {"/AFP/SiT/hit/expert/"};
   vector<string> vstation {"station0", "station1", "station2", "station3"};
   vector<string> vlayer {"layer0", "layer1", "layer2", "layer3"};

   // title location
   gStyle->SetTitleX(0.45); // X
   gStyle->SetTitleY(0.9);  // Y
   gStyle->SetTitleW(0.5);  // width
   gStyle->SetTitleH(0.1);  // height

   TH1D *hist[16];    // 16 layer

   // canvas
   TCanvas *canvas = new TCanvas("hitvspileup", "hit vs pileup", 1500, 950);
   canvas->Divide(4, 4); //column, line

   string name;

   TF1 *f1 = new TF1("f1", "gaus", 55, 60);

   for (auto &inputfile : vinputfile){

      // read input file
      TFile *file = new TFile(inputfile.c_str(), "READ");
      if(!file->IsOpen()) {cout<<"file not found, skipping: "<<inputfile<<endl; return;}
      cout<<"\nfile: "<<inputfile<<endl;

      int nlayer{0};

      for (auto &station : vstation) {
         for (auto &layer : vlayer){
            // get hist name
            name = vdir[0]+"numberOfHitVersusPileup_"+station+"_"+layer;
            hist[nlayer]=(TH1D*)file->Get(name.c_str());
            name = station+" "+layer;
            hist[nlayer]->SetTitle(name.c_str());
            hist[nlayer]->SetStats(0);
            hist[nlayer]->Rebin(4);
            canvas->cd(nlayer+1);
            hist[nlayer]->Fit("f1", "R");
            nlayer++;
         } // layer
      } // station
   } // input
} // void

Dear Couet,

  • thank you, it works fine.
  • now I want to fit the wavy distribution (1). When I fit on separate subrange (in red), the fit works fine, but then I make a global fit (in blue), which is a composition of the red, and you can see that the global fit is bad. I tried to fixe some parameters but it does not help. I wonder if it’s normal that the sub-fits work that’s nice while the global is that’s bad? Is there a way to fix this? You can see the code here (2).
    Regards

(1)
image

(2)
http://calpas.web.cern.ch/calpas/pileup.C

with your new code I get something different from the picture you posted.

Dear Couet,
all is ok now. Thank you for your help.
Regards

Dear experts,
now I’m doing the fit over 2 file and I want to draw the fit the same pad. but with my code (1) it draws only the last one. I have tried different thing without success. Do you see what is wrong?
Regards

(1)
http://calpas.web.cern.ch/calpas/pileup.C

I think the problem is that hist[nlayer]->Fit("fpol", "R"); automatically draws the histogram (and the fitted function), so the previous pad contents is removed.

Ok, but can’t I keep the previous one? If I use “R+” I still can’t…
Regards

The “+” makes no sense in this case. Each histogram is fitted with one function only.

The Draw(“same”) should help right?
Regards

Try a “brutal fix”:

TPad *ptmp = gPad;
TCanvas *ctmp = new TCanvas("ctmp", "ctmp");
hist[nlayer]->Fit("fpol",  "R");
delete ctmp;
ptmp->cd();

or maybe better simply:

hist[nlayer]->Fit("fpol",  "R0");
hist[nlayer]->GetFunction("fpol")->ResetBit(TF1::kNotDraw);

Actually, this may work for you (all fits are automatically drawn):

	if(ninput==0) hist[nlayer]->Fit("fpol", "R");
	else hist[nlayer]->Fit("fpol", "R", "SAME");

Dear Coyote,
the last solution works fine, thank you.
Regards

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