# Problem Fitting .root file histogram

**URL:** <https://root-forum.cern.ch/t/problem-fitting-root-file-histogram/43472>\
**Category:** Newbie\
**Created:** [February 9, 2021, 4:27pm UTC](https://root-forum.cern.ch/t/problem-fitting-root-file-histogram/43472 "2021-02-09T16:27:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Edward\_Finkelstein](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/edward_finkelstein/32/20070_2.png) [@Edward\_Finkelstein](https://root-forum.cern.ch/u/Edward_Finkelstein)\
**Post date:** [February 9, 2021, 4:27pm UTC](https://root-forum.cern.ch/t/problem-fitting-root-file-histogram/43472/1 "2021-02-09T16:27:58Z")

</div>

Hi,  
I am trying to run a script for a new root file with a histogram, but for some reason, calling the fit function for this new file doesn’t even fit the data, whereas with a previous file it did. Below is the script to generate the problem, and attached is the root file. Can someone help figure out how to fix this fit problem?

```auto
double FiveParam2015TEV_plus_Gauss(double *x, double *har){double Ecm=13.;double fCenterOfMassEnergy = Ecm;bool fUseWindowExclusion = false;double y = x[0] / fCenterOfMassEnergy;double ff1=har[0]*TMath::Power((1.0-y),har[1]);double ff2=TMath::Power(y,(har[2]+har[3]*log(y)+har[4]*log(y)*log(y)));double ff=((ff1*ff2));return ff;}
string xdir="";
    string figdir="figs/";
    char nameX[]="m_{jjl} [TeV]";
    char nameY[]="Events / TeV";
    double Ymin=0.9;
    double Ymax=1000000000.0-100000.0;
    double YRMIN=-4.999;
    double YRMAX=4.999;
    double Xmin=400.0;
    double Xmax=10000.0;
    Xmin=Xmin*0.001;
    Xmax=Xmax*0.001;
    double NN=0.0;
    gROOT->SetStyle("Plain");
    TCanvas* c1 = new TCanvas("c","BPRE",10,10,600,600);
    c1->Divide(1,1,0.005,0.005);
    c1->SetTickx();
    c1->SetTicky();
    c1->SetTitle("");
    c1->SetLineWidth(3);
    c1->SetBottomMargin(0.1);
    c1->SetTopMargin(0.05);
    c1->SetRightMargin(0.01);
    c1->SetFillColor(0);
    TPad* pad1 = new TPad("pad1","pad1",0,0.3,1,0.97);
    pad1->SetBottomMargin(0);
    pad1->SetLeftMargin(0.13);
    pad1->SetRightMargin(0.04);
    pad1->SetTopMargin(0.02);
    pad1->Draw();
    pad1->cd();
    pad1->SetLogy(1);
    pad1->SetLogx(1);
    TH1F *h=pad1->DrawFrame(Xmin,Ymin,Xmax,Ymax);
    TAxis *ay = h->GetYaxis();
    ay->SetLabelFont(42);
    ay->SetLabelSize(0.05);
    ay->SetTitleSize(0.06);
    ay->SetNdivisions(505);
    ay->SetTitle(nameY);
    TAxis *ax=h->GetXaxis();
    ax->SetTitle(nameX);
    ax->SetTitleOffset(1.18);
    ay->SetTitleOffset(0.8);
    ax->SetLabelFont(42);
    ay->SetLabelFont(42);
    ax->SetLabelSize(0.12);
    ax->SetTitleSize(0.14);
    ax->Draw("same");
    ay->Draw("same");
    string dir = xdir;
    TFile *ff = new TFile("mjjl_smooth_mc.root");
    ff->ls();
char name[] = "jjlmass";
    TH1* hh = static_cast<TH1*>(ff->Get(name));
    TH1* hhorg = static_cast<TH1*>(hh->Clone());
TFile TFdata("mjjl_smooth_mc.root");
    cout << TFdata.GetName()<< endl;
    TH1* bids= static_cast<TH1*>(TFdata.Get("bins_m"));
    TH1* bins= static_cast<TH1*>(TFdata.Get("bins_m"));
    bids->Scale(1.0/92.0);
    cout << "Bool: "<< hh->Divide(bids) << endl;
    hh->SetTitle("");
    hh->SetStats(0);
    hh->SetLineWidth(2);
    hh->Print("All");
    hh->SetAxisRange(Ymin,Ymax,"y"); //0.00001
    hh->SetAxisRange(Xmin,Xmax,"x");
hh->SetMarkerColor( 3 );
    hh->SetMarkerStyle( 20 );
    hh->SetMarkerSize( 0.8 );
 double MyMinX=Xmin;
    double MyMaxX=Xmax;
    hh->Draw("pe");
  TF1 ball("5p+gauss",FiveParam2015TEV_plus_Gauss,MyMinX,MyMaxX,5);
    ball.SetNpx(200);
    ball.SetLineColor(4);
    ball.SetLineStyle(1);
    ball.SetLineWidth(2);
ball.SetParameter(0,6.05059e+04);
ball.SetParameter(1,1.32594e+01);
ball.SetParameter(2,5.85506e-01);
ball.SetParameter(3,1.78302e+00);
ball.SetParameter(4,2.83882e-01);
auto fitr=hh->Fit(&ball,"SL","");fitr->Print();

```

[mjjl\_smooth\_mc.root](https://root-forum.cern.ch/uploads/short-url/gozHfJ4bU5ocmQbDF3JeuctqMkp.root) (6.7 KB)

* * *

_ROOT Version:_ 6.23/01

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [February 10, 2021, 8:53am UTC](https://root-forum.cern.ch/t/problem-fitting-root-file-histogram/43472/2 "2021-02-10T08:53:04Z")

</div>

Hi,  
is this what you are getting?

```auto
Warning in <Fit>: Abnormal termination of minimization.
 FCN=-nan FROM MIGRAD STATUS=CALL LIMIT 1664 CALLS 1665 TOTAL
                     EDM=-nan STRATEGY= 1 NO ERROR MATRIX       
  EXT PARAMETER CURRENT GUESS STEP FIRST   
  NO. NAME VALUE ERROR SIZE DERIVATIVE 
   1 p0 -nan 1.81518e+04 -nan -nan
   2 p1 -nan 3.97782e+00 -nan -nan
   3 p2 -nan 1.75652e-01 -nan -nan
   4 p3 -nan 5.34906e-01 -nan -nan
   5 p4 -nan 8.51646e-02 -nan -nan
                               ERR DEF= 0.5

****************************************
         Invalid FitResult (status = 4 )
****************************************
Minimizer is Minuit / Migrad
MinFCN = -nan
Chi2 = inf
NDf = 160
NCalls = 1665
p0 = -nan +/- 18151.8     
p1 = -nan +/- 3.97782     
p2 = -nan +/- 0.175652    
p3 = -nan +/- 0.534906    
p4 = -nan +/- 0.0851646   

```

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [February 10, 2021, 8:54am UTC](https://root-forum.cern.ch/t/problem-fitting-root-file-histogram/43472/3 "2021-02-10T08:54:48Z")

</div>

> [@Edward\_Finkelstein](#):
>
> `FiveParam2015TEV_plus_Gauss`

Is it possible that `FiveParam2015TEV_plus_Gauss` evaluates to very large, very small or inf/nan values so the minimizer has a hard time?

EDIT: yes, in fact putting a `std::cout << ff << '\n'` I can see it produces `nan`s, e.g. because of a division by zero.

---

<div class="post-metadata">

**Author:** ![Edward\_Finkelstein](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/edward_finkelstein/32/20070_2.png) [@Edward\_Finkelstein](https://root-forum.cern.ch/u/Edward_Finkelstein)\
**Post date:** [February 10, 2021, 8:41pm UTC](https://root-forum.cern.ch/t/problem-fitting-root-file-histogram/43472/4 "2021-02-10T20:41:11Z")

</div>

Yes, this is exactly the problem I am facing. Do you know how to fix it?

---

<div class="post-metadata">

**Author:** ![Edward\_Finkelstein](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/edward_finkelstein/32/20070_2.png) [@Edward\_Finkelstein](https://root-forum.cern.ch/u/Edward_Finkelstein)\
**Post date:** [February 10, 2021, 9:46pm UTC](https://root-forum.cern.ch/t/problem-fitting-root-file-histogram/43472/5 "2021-02-10T21:46:46Z")

</div>

Ok, so the problem was that this was in GeV, not TeV, so I was fitting out of range, now I fixed it.

```auto
double FiveParam2015TEV_plus_Gauss(double *x, double *har){double Ecm=13000.;double fCenterOfMassEnergy = Ecm;bool fUseWindowExclusion = false;double y = x[0] / fCenterOfMassEnergy;double ff1=har[0]*TMath::Power((1.0-y),har[1]);double ff2=TMath::Power(y,(har[2]+har[3]*log(y)+har[4]*log(y)*log(y)));double ff=((ff1*ff2));return ff;}
string xdir="";
    string figdir="figs/";
    char nameX[]="m_{jjl} [GeV]";
    char nameY[]="Events / GeV";
    double Ymin=0.9;
    double Ymax=1000000000.0-100000.0;
    double YRMIN=-4.999;
    double YRMAX=4.999;
    double Xmin=400.0;
    double Xmax=10000.0;
    Xmin=Xmin;
    Xmax=Xmax;
    double NN=0.0;
    gROOT->SetStyle("Plain");
    TCanvas* c1 = new TCanvas("c","BPRE",10,10,600,600);
    c1->Divide(1,1,0.005,0.005);
    c1->SetTickx();
    c1->SetTicky();
    c1->SetTitle("");
    c1->SetLineWidth(3);
    c1->SetBottomMargin(0.1);
    c1->SetTopMargin(0.05);
    c1->SetRightMargin(0.01);
    c1->SetFillColor(0);
    TPad* pad1 = new TPad("pad1","pad1",0,0.3,1,0.97);
    pad1->SetBottomMargin(0);
    pad1->SetLeftMargin(0.13);
    pad1->SetRightMargin(0.04);
    pad1->SetTopMargin(0.02);
    pad1->Draw();
    pad1->cd();
    pad1->SetLogy(1);
    pad1->SetLogx(1);
    TH1F *h=pad1->DrawFrame(Xmin,Ymin,Xmax,Ymax);
    TAxis *ay = h->GetYaxis();
    ay->SetLabelFont(42);
    ay->SetLabelSize(0.05);
    ay->SetTitleSize(0.06);
    ay->SetNdivisions(505);
    ay->SetTitle(nameY);
    TAxis *ax=h->GetXaxis();
    ax->SetTitle(nameX);
    ax->SetTitleOffset(1.18);
    ay->SetTitleOffset(0.8);
    ax->SetLabelFont(42);
    ay->SetLabelFont(42);
    ax->SetLabelSize(0.12);
    ax->SetTitleSize(0.14);
    ax->Draw("same");
    ay->Draw("same");
    string dir = xdir;
    TFile *ff = new TFile("mjjl_smooth_mc.root");
    ff->ls();
char name[] = "jjlmass";
    TH1* hh = static_cast<TH1*>(ff->Get(name));
    TH1* hhorg = static_cast<TH1*>(hh->Clone());
TFile TFdata("mjjl_smooth_mc.root");
    cout << TFdata.GetName()<< endl;
    TH1* bids= static_cast<TH1*>(TFdata.Get("bins_m"));
    TH1* bins= static_cast<TH1*>(TFdata.Get("bins_m"));
    bids->Scale(1.0/92.0);
    cout << "Bool: "<< hh->Divide(bids) << endl;
    hh->SetTitle("");
    hh->SetStats(0);
    hh->SetLineWidth(2);
    hh->Print("All");
    hh->SetAxisRange(Ymin,Ymax,"y"); //0.00001
    hh->SetAxisRange(Xmin,Xmax,"x");
hh->SetMarkerColor( 3 );
    hh->SetMarkerStyle( 20 );
    hh->SetMarkerSize( 0.8 );
 double MyMinX=Xmin;
    double MyMaxX=Xmax;
    hh->Draw("pe");
  TF1 ball("5p+gauss",FiveParam2015TEV_plus_Gauss,MyMinX,MyMaxX,5);
    ball.SetNpx(200);
    ball.SetLineColor(4);
    ball.SetLineStyle(1);
    ball.SetLineWidth(2);
ball.SetParameter(0,6.05059e+04);
ball.SetParameter(1,1.32594e+01);
ball.SetParameter(2,5.85506e-01);
ball.SetParameter(3,1.78302e+00);
ball.SetParameter(4,2.83882e-01);
auto fitr=hh->Fit(&ball,"SL","");fitr->Print();

```
