Problem with TH1D->Fit

I have two TH1D. When I Fit one with a function, there is no problem and I can show the TH1D together with the Fit function. But when I Fit a second graph, there comes an error and the program is quitted. here is what I write :

TH1D* result1; //get from a function
TH1D* result2; //get from another function

TCanvas* c1 = new TCanvas(“c”, “c”);
c1->Divide(2, 1);

c1->cd(1);
TF1* fonction = new TF1(“x_2”, (double* x, double* p) {return x[0] * x[0] * p[0]; }, (double)taille_min, (double)taille_max + 1.,1);
result1->Fit(fonction, “”, “”, (double)taille_min, (double)taille_max + 1.);
result1->Draw(“HIST”);
fonction->Draw(“SAME”);

c1->cd(2);
TF1* fonction2= new TF1(“x_3”, (double* x, double* p) {return x[0] * x[0] * p[0]; }, (double)taille_min, (double)taille_max + 1.,1);
result2->Fit(fonction2, “”, “”, (double)taille_min, (double)taille_max + 1.);
result2->Draw(“HIST”);
fonction2->Draw(“SAME”);

c1->Show();
app.Run();

Welcome to the ROOT forum.

Which error message to you get ?

I am sure @moneta can help

Hello :slight_smile:
it only leaves the program completely ! Did I have a problem installing it ? I am using visual studio 2023, x86 … Or maybe the option “SAME” does’nt work twice ?

notice that I have written on the Console the result of the second Fit, before it leaves …

Can you provide a script we can run in ROOT ?
For instance taille_min and taille_max are not defined in your example.

Here it is !

int taille_min=5;
int taille_max=100;
TH1D* result1 = new TH1D(“Kemeny”,“Kemeny”, taille_max - taille_min + 1, (double)taille_min, (double)taille_max + 1.);
TH1D* result2 = new TH1D(“inverse_gap”,“inverse_gap”, taille_max - taille_min + 1, (double)taille_min, (double)taille_max + 1.);

if you want you can fill it :

for (int i(taille_min); i <= taille_max; ++i) {
result2->Fill((double)i + 0.5, 1.3 * ((double) (i * i)) );
result1->Fill((double)i + 0.5, 1.7 * ((double) (i * i)) );
}

and you have to launch an application

TApplication app(“lifting square_wave”, &argc, argv);

file fitsame.C :

double fit1 (double *x,double *p) {
   return x[0] * x[0] * p[0];
}


void fitsame(){
   int taille_min=5;
   int taille_max=100;
   TH1D* result1 = new TH1D("Kemeny","Kemeny", taille_max - taille_min + 1, (double)taille_min, (double)taille_max + 1.);
   TH1D* result2 = new TH1D("inverse_gap","inverse_gap", taille_max - taille_min + 1, (double)taille_min, (double)taille_max + 1.);

   for (int i(taille_min); i <= taille_max; ++i) {
      result2->Fill((double)i + 0.5, 1.3 * ((double) (i * i)) );
      result1->Fill((double)i + 0.5, 1.7 * ((double) (i * i)) );
   }

   TCanvas* c1 = new TCanvas("c", "c");
   c1->Divide(2, 1);

   c1->cd(1);
   TF1* fonction = new TF1("x_2", fit1, (double)taille_min, (double)taille_max + 1.,1);
   result1->Fit(fonction, "", "", (double)taille_min, (double)taille_max + 1.);
   result1->Draw("HIST");
   fonction->Draw("SAME");

   c1->cd(2);
   TF1* fonction2= new TF1("x_3", fit1, (double)taille_min, (double)taille_max + 1.,1);
   result2->Fit(fonction2, "", "", (double)taille_min, (double)taille_max + 1.);
   result2->Draw("HIST");
   fonction2->Draw("SAME");
}
$ root
root [0] .x fitsame.C
****************************************
Minimizer is Minuit2 / Migrad
Chi2                      =     0.112309
NDf                       =           95
Edm                       =   1.0364e-16
NCalls                    =           13
p0                        =      1.64395   +/-   0.167883    
****************************************
Minimizer is Minuit2 / Migrad
Chi2                      =     0.112309
NDf                       =           95
Edm                       =  4.38049e-17
NCalls                    =           13
p0                        =      1.25714   +/-   0.128381    
root [1] 

at home it doesn’t work :frowning: actually it worked once, but then I tried again and it did not work (several times) … :frowning:

I tried several time, for me this works. From the 2nd time I get Warnings saying that le histograms already exist, but that’s normal.

Can you copy/paste the messages you get ?

We tried on Windows, it is also working. We only get the warnings.

Hello !
Acutally I was working … i’ve uninstalled the version x86, and installed the x64 ! and now it is working :slightly_smiling_face:
Thanks !!
Bye :slight_smile:

1 Like

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