# Problem with TH1D-\>Fit

**URL:** <https://root-forum.cern.ch/t/problem-with-th1d-fit/57548>\
**Category:** ROOT\
**Tags:** roofit\
**Created:** [December 14, 2023, 1:47pm UTC](https://root-forum.cern.ch/t/problem-with-th1d-fit/57548 "2023-12-14T13:47:50Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![gustave\_Robichon](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/gustave_robichon/32/30366_2.png) [@gustave\_Robichon](https://root-forum.cern.ch/u/gustave_Robichon)\
**Post date:** [December 14, 2023, 1:47pm UTC](https://root-forum.cern.ch/t/problem-with-th1d-fit/57548/1 "2023-12-14T13:47:50Z")

</div>

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();

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [December 14, 2023, 1:51pm UTC](https://root-forum.cern.ch/t/problem-with-th1d-fit/57548/2 "2023-12-14T13:51:59Z")

</div>

Welcome to the ROOT forum.

Which error message to you get ?

I am sure @moneta can help

---

<div class="post-metadata">

**Author:** ![gustave\_Robichon](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/gustave_robichon/32/30366_2.png) [@gustave\_Robichon](https://root-forum.cern.ch/u/gustave_Robichon)\
**Post date:** [December 14, 2023, 1:53pm UTC](https://root-forum.cern.ch/t/problem-with-th1d-fit/57548/3 "2023-12-14T13:53:25Z")

</div>

Hello 🙂  
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 …

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [December 14, 2023, 2:00pm UTC](https://root-forum.cern.ch/t/problem-with-th1d-fit/57548/4 "2023-12-14T14:00:31Z")

</div>

Can you provide a script we can run in ROOT ?  
For instance taille\_min and taille\_max are not defined in your example.

---

<div class="post-metadata">

**Author:** ![gustave\_Robichon](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/gustave_robichon/32/30366_2.png) [@gustave\_Robichon](https://root-forum.cern.ch/u/gustave_Robichon)\
**Post date:** [December 14, 2023, 2:05pm UTC](https://root-forum.cern.ch/t/problem-with-th1d-fit/57548/5 "2023-12-14T14:05:56Z")

</div>

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);

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [December 14, 2023, 2:45pm UTC](https://root-forum.cern.ch/t/problem-with-th1d-fit/57548/6 "2023-12-14T14:45:21Z")

</div>

### file `fitsame.C` :

```auto
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");
}

```

```auto
$ 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] 

```

 ![Screenshot 2023-12-14 at 15.45.26](https://root-forum.cern.ch/uploads/default/original/3X/b/e/be6b25d66c46e60b1c5a19203bac097a3ebe605e.png)

---

<div class="post-metadata">

**Author:** ![gustave\_Robichon](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/gustave_robichon/32/30366_2.png) [@gustave\_Robichon](https://root-forum.cern.ch/u/gustave_Robichon)\
**Post date:** [December 14, 2023, 3:04pm UTC](https://root-forum.cern.ch/t/problem-with-th1d-fit/57548/7 "2023-12-14T15:04:14Z")

</div>

> [@couet](#):
>
> ```auto
> double fit1 (double *x,double *p) {
> return x[0] * x[0] * p[0];
> }
> 
> ```

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

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [December 14, 2023, 3:08pm UTC](https://root-forum.cern.ch/t/problem-with-th1d-fit/57548/8 "2023-12-14T15:08:36Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [December 14, 2023, 3:15pm UTC](https://root-forum.cern.ch/t/problem-with-th1d-fit/57548/9 "2023-12-14T15:15:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gustave\_Robichon](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/gustave_robichon/32/30366_2.png) [@gustave\_Robichon](https://root-forum.cern.ch/u/gustave_Robichon)\
**Post date:** [December 14, 2023, 3:45pm UTC](https://root-forum.cern.ch/t/problem-with-th1d-fit/57548/10 "2023-12-14T15:45:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [December 28, 2023, 3:46pm UTC](https://root-forum.cern.ch/t/problem-with-th1d-fit/57548/11 "2023-12-28T15:46:00Z")

</div>

Closed on December 28, 2023, 3:46pm UTC

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