Two fit boxes on one canvas

I have a TGraphErrors which has two separate distributions in it, which I therefore fit with two pol0s. I would like to be able to draw the fit boxes of these two distributions. Currently what I have doesn’t give me errors, but it seems to put the result from the second fit in the first fit box, as seen in the attached image.
How can I make this work? Thanks in advance

Double_t pol0f(Double_t *x, Double_t *par) {
  return par[0];
}

void MWE(){
  
  gStyle->SetOptStat(1110);
  gStyle->SetOptFit(0001);
  
  TGraphErrors* ge1 = new TGraphErrors();

  TRandom3 *myRNG=new TRandom3();
  myRNG->SetSeed(1);
  
  for(int ii=0;ii<100;ii++)
    {
      double rand = myRNG->Gaus(100,0.01);
      double rand2 = myRNG->Gaus(200,0.01);
      if(ii<50) ge1->SetPoint(ii,ii,rand);
      else ge1->SetPoint(ii,ii,rand2);
    }

  TCanvas* c1 = new TCanvas("c1","c1",900,700);
  ge1->Draw("AP");
  ge1->SetMarkerStyle(kStar);
  
  TF1 *lowfunc = new TF1("lowfit","pol0f",20,47,1);
  lowfunc->SetParName(0,"Low");
  lowfunc->SetLineColor(kRed);
  lowfunc->Draw("same");
  ge1->Fit("lowfit","R");
  lowfunc->SetLineWidth(4);

  c1->Modified();
  c1->Update();

  TPaveStats *sLow = (TPaveStats*)ge1->FindObject("stats");
  sLow->SetName("sLow");

  TF1 *highfunc = new TF1("highfit","pol0f",50,70,1);
  highfunc->SetParName(0,"High");
  highfunc->SetLineColor(kRed);
  highfunc->Draw("same");
  ge1->Fit("highfit","R");   
  highfunc->SetLineWidth(4);

  TPaveStats *sHigh = (TPaveStats*)sLow->Clone();
  sHigh->SetName("sHigh");

}

I’m bumping this because unfortunately I need an answer relatively urgently, I hope there’s enough information here to understand and answer the question, but if not please let me know!

If the stat boxes are overlapping (check interactively). you ill need to move one.

I checked, but there’s only one, and one of the things I don’t understand is that the box called sLow has the label I tried to add to sHigh, as you can see in the picture above

You forgot to

sHigh->Draw()

In your code. Then both will appear, you just have to drag one

I don’t remember by heart the function to call to move the TPaveStats to some location directly within your macro, you can check the documentation.

Cheers,
Vincenzo

Super helpful, thank you for pointing out that stupid error! Not that it matters particularly but the stats boxes have strange names: the one that should be called sHigh is called sLow and the one that should be called sHigh is called sLow


Here it is: ROOT: tutorials/hist/transpad.C File Reference

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