Scale fit polynomial with histogram

Hello Rooters,

I am trying to fit a histogram with a higher-order polynomial. The fit goes fine. But when later I try to scale the histogram by a certain factor, the histogram moves by that scaling factor but the fit polynomial stays in the original place as it was before I scaled the histogram. How can one also scale the fit function along with the histogram by the same scaling factor, so that the fit overlaps the histogram?

Here’s the piece of code, I am trying to fit p2 on h2 and later scale both of them by a factor.

TF1* p1 = new TF1("p1","([0]*pow(x,3)+[1]*pow(x,2)+[2]*pow(x,1)+[3])",2180,2280);
TF1* p2 = new TF1("p1","([0]*pow(x,4)+[1]*pow(x,3)+[2]*pow(x,2)+[3]*pow(x,1)+[4])",2180,2280);

 p1->SetLineColor(4);
 h2->Fit(p1,"N"); 
 p2->SetParameter(0,p1->GetParameter(0)); 
 p2->SetParameter(1,p1->GetParameter(1));
 p2->SetParameter(2,p1->GetParameter(2));
 p2->SetParameter(3,p1->GetParameter(3));
 h2->SetLineColor(1);
 h2->Fit(p2,"R");
 h2->Scale(1/0.0774);
 h2->Draw();
 //p2->Scale(1/0.0774); //how to scale this function?? Is there another way to do it?
 p2->Draw("same");

ROOT Version: 5.34 / 38
Platform: Opensuse
Compiler: g++ (suse Linux) 7.4.1

You first need to “scale” the histogram and then “fit” it.

1 Like

Thank you Wile_E_Coyote, I was unnecessarily thinking too much into it!

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