# Repositioning Stat Box for a Fit to TMultiGraph

**URL:** <https://root-forum.cern.ch/t/repositioning-stat-box-for-a-fit-to-tmultigraph/51381>\
**Category:** Newbie\
**Tags:** graphics, roofit, root\
**Created:** [August 28, 2022, 1:53am UTC](https://root-forum.cern.ch/t/repositioning-stat-box-for-a-fit-to-tmultigraph/51381 "2022-08-28T01:53:52Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bassam](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/b38774/32.png) [@Bassam](https://root-forum.cern.ch/u/Bassam)\
**Post date:** [August 28, 2022, 1:53am UTC](https://root-forum.cern.ch/t/repositioning-stat-box-for-a-fit-to-tmultigraph/51381/1 "2022-08-28T01:53:52Z")

</div>

Hello All,

I am trying to figure out how to move the stat box for for a fit to a TMultiGraph plot. The following is my attempt:

```auto
        gStyle->SetOptFit(1);
        TMultiGraph* multiGraph5 = new TMultiGraph();
        multiGraph5->SetTitle("p2 of Double Gaussian Fit for n_{sigma,TOF}(e); Avg. Eta Bin; p4 of Double Gaussian Fit");
        multiGraph5->SetMinimum(0.8); // Set the minimum y axis value
        multiGraph5->SetMaximum(1.); // Set the maximum y axis value
        // :
        // Populated a few TGraphError objects with the needed data
        // :
        // Add all TGraphError to multiGraph
        multiGraph5->Add(p4Graph1);
        multiGraph5->Add(p4Graph2);
        multiGraph5->Add(p4Graph3);
        multiGraph5->Add(p4Graph4);
        multiGraph5->Add(p4Graph5);
        multiGraph5->Add(p4Graph6);

        // Do the fitting
        TF1* poly0 = new TF1("poly0", "pol0", -0.88, 1.2);
        multiGraph5->Fit("poly0", "R");
        multiGraph5->Draw("AP");

        c1->Update();
        TPaveText* st = (TPaveText*)multiGraph5->GetListOfFunctions()->FindObject("stats");
        st->SetX1NDC(.15);
        st->SetX2NDC(.5);
        c1->Modified();

```

But that gives me the following error:

```auto
Error: illegal pointer to class object st 0x0 1021 plot_read2EePairTree.C:1043:
***Interpreter error recovered***

```

My final goal is to be able to have two fits to the data on the same plot. But, to do that, I would need to be able to move the stat boxes because they would be overlayed on top of each other. So far, I have not been able to reposition a stat box for a single fit. Any help would be greatly appreciated!

Thank you very much!

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [August 28, 2022, 4:22am UTC](https://root-forum.cern.ch/t/repositioning-stat-box-for-a-fit-to-tmultigraph/51381/2 "2022-08-28T04:22:49Z")

</div>

Try

```auto
        TPaveStats* st = (TPaveStats*)multiGraph5->FindObject("stats");
        if (!st) { cout << "stats not found!\n"; return; }

```

---

<div class="post-metadata">

**Author:** ![Bassam](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/b38774/32.png) [@Bassam](https://root-forum.cern.ch/u/Bassam)\
**Post date:** [August 28, 2022, 12:39pm UTC](https://root-forum.cern.ch/t/repositioning-stat-box-for-a-fit-to-tmultigraph/51381/3 "2022-08-28T12:39:22Z")

</div>

> [@dastudillo](#):
>
> `if (!st) { cout << "stats not found!\n"; return; }`

Thank you very much for the reply @dastudillo! I tried your suggestion, and it returned “stats not found!” So, somehow, it is not finding the stat box object. Any thoughts on why that might be?

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [August 28, 2022, 12:43pm UTC](https://root-forum.cern.ch/t/repositioning-stat-box-for-a-fit-to-tmultigraph/51381/4 "2022-08-28T12:43:36Z")

</div>

Try:

```cpp
gPad->Modified(); gPad->Update();
TPaveStats *st = (TPaveStats*)gPad->GetPrimitive("stats");

```

---

<div class="post-metadata">

**Author:** ![Bassam](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/b38774/32.png) [@Bassam](https://root-forum.cern.ch/u/Bassam)\
**Post date:** [August 28, 2022, 1:55pm UTC](https://root-forum.cern.ch/t/repositioning-stat-box-for-a-fit-to-tmultigraph/51381/5 "2022-08-28T13:55:32Z")

</div>

Thanks for the reply! I tried the your suggestion in the following, but it still returned a null pointer for `st`:

```auto
        gStyle->SetOptFit(1);
        TMultiGraph* multiGraph5 = new TMultiGraph();
        multiGraph5->SetTitle("p2 of Double Gaussian Fit for n_{sigma,TOF}(e); Avg. Eta Bin; p4 of Double Gaussian Fit");
        multiGraph5->SetMinimum(0.8); // Set the minimum y axis value
        multiGraph5->SetMaximum(1.); // Set the maximum y axis value
        // :
        // Populated a few TGraphError objects with the needed data
        // :
        // Add all TGraphError to multiGraph
        multiGraph5->Add(p4Graph1);
        multiGraph5->Add(p4Graph2);
        multiGraph5->Add(p4Graph3);
        multiGraph5->Add(p4Graph4);
        multiGraph5->Add(p4Graph5);
        multiGraph5->Add(p4Graph6);

        // Do the fitting
        TF1* poly0 = new TF1("poly0", "pol0", -0.88, 1.2);
        multiGraph5->Fit("poly0", "R");
        multiGraph5->Draw("AP");
        multiGraph5->GetYaxis()->SetTitleOffset(1.3);
        multiGraph5->GetYaxis()->CenterTitle(true);

        gPad->Modified();
        gPad->Update();

        TPaveStats *st = (TPaveStats*)gPad->GetPrimitive("stats");
        if (!st) { cout << "stats not found!\n"; return; }

```

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [August 28, 2022, 7:08pm UTC](https://root-forum.cern.ch/t/repositioning-stat-box-for-a-fit-to-tmultigraph/51381/6 "2022-08-28T19:08:11Z")

</div>

Try: `gPad->ls();`

---

<div class="post-metadata">

**Author:** ![Bassam](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/b38774/32.png) [@Bassam](https://root-forum.cern.ch/u/Bassam)\
**Post date:** [August 28, 2022, 8:49pm UTC](https://root-forum.cern.ch/t/repositioning-stat-box-for-a-fit-to-tmultigraph/51381/7 "2022-08-28T20:49:37Z")

</div>

Hi @Wile_E_Coyote! I tried your suggestion and here is what I got back. It looks like that there is a TPaveText object.

```auto
Canvas Name=c1 Title= Option=
 TCanvas fXlowNDC=0 fYlowNDC=0 fWNDC=1 fHNDC=1 Name= c1 Title= Option=
  OBJ: TList	TList	Doubly linked list : 0
   TFrame X1= -0.973000 Y1=0.000000 X2=1.293000 Y2=1.000000
   OBJ: TMultiGraph p4 of Double Gaussian Fit for n_{sigma,TOF}(e); Avg. Eta Bin; p4 of Double Gaussian Fit : 0 at: 0x12d9a9a0
   OBJ: TPaveText	title X1= -1.227925 Y1=1.013877 X2=0.574619 Y2=1.118750

```

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [August 28, 2022, 8:58pm UTC](https://root-forum.cern.ch/t/repositioning-stat-box-for-a-fit-to-tmultigraph/51381/8 "2022-08-28T20:58:51Z")

</div>

Try: `multiGraph5->GetListOfFunctions()->ls();`

---

<div class="post-metadata">

**Author:** ![Bassam](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/b38774/32.png) [@Bassam](https://root-forum.cern.ch/u/Bassam)\
**Post date:** [August 28, 2022, 9:07pm UTC](https://root-forum.cern.ch/t/repositioning-stat-box-for-a-fit-to-tmultigraph/51381/9 "2022-08-28T21:07:53Z")

</div>

The result of `multiGraph5->GetListOfFunctions()->ls();` is the following:

```auto
OBJ: TList	TList	Doubly linked list : 0
 OBJ: TF1	poly0	pol0 : 0 at: 0x12610148

```

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [August 28, 2022, 9:08pm UTC](https://root-forum.cern.ch/t/repositioning-stat-box-for-a-fit-to-tmultigraph/51381/10 "2022-08-28T21:08:38Z")

</div>

As you can see, there is no “stats” anywhere.

You could also try:  
`multiGraph5->GetHistogram()->GetListOfFunctions()->ls();`

---

<div class="post-metadata">

**Author:** ![Bassam](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/b38774/32.png) [@Bassam](https://root-forum.cern.ch/u/Bassam)\
**Post date:** [August 28, 2022, 9:21pm UTC](https://root-forum.cern.ch/t/repositioning-stat-box-for-a-fit-to-tmultigraph/51381/11 "2022-08-28T21:21:41Z")

</div>

That’s interesting! Attached is the outcome of my code. Is there a way to access the stat box seen in the plot to change some of its settings? Let’s say, the color of the text.

 ![Screen Shot 2022-08-28 at 4.15.14 PM](https://root-forum.cern.ch/uploads/default/original/3X/9/8/98d8294062c15457f0dd8bb5dd6ebd3d0a4426fd.png)

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [August 28, 2022, 9:30pm UTC](https://root-forum.cern.ch/t/repositioning-stat-box-for-a-fit-to-tmultigraph/51381/12 "2022-08-28T21:30:16Z")

</div>

Just to make sure … did you execute “`gPad->Modified(); gPad->Update();`” before you tried these various “`ls()`” commands?

---

<div class="post-metadata">

**Author:** ![Bassam](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/b38774/32.png) [@Bassam](https://root-forum.cern.ch/u/Bassam)\
**Post date:** [August 28, 2022, 9:46pm UTC](https://root-forum.cern.ch/t/repositioning-stat-box-for-a-fit-to-tmultigraph/51381/13 "2022-08-28T21:46:44Z")

</div>

Yeah! These `ls()` statements were after the following:

```auto
        gPad->Modified();
        gPad->Update();

```

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [August 29, 2022, 4:32am UTC](https://root-forum.cern.ch/t/repositioning-stat-box-for-a-fit-to-tmultigraph/51381/14 "2022-08-29T04:32:00Z")

</div>

Are you doing anything between these

```auto
        // Do the fitting
        TF1* poly0 = new TF1("poly0", "pol0", -0.88, 1.2);
        multiGraph5->Fit("poly0", "R");
        multiGraph5->Draw("AP");
        multiGraph5->GetYaxis()->SetTitleOffset(1.3);
        multiGraph5->GetYaxis()->CenterTitle(true);

```

and these

```auto
        gPad->Modified();
        gPad->Update();

```

(and `gPad->ls()`)?  
Maybe you are doing something else that you are not showing us and is affecting the outcome?  
If you stop your code (add `return;`) right here

```auto
        multiGraph5->Fit("poly0", "R");
        multiGraph5->Draw("AP");
        return;

```

is the fit stats box shown in the canvas?  
By the way, `graph->Fit` already draws the graph with the “AP” options, so you don’t need to do `graph->Draw("AP")` after that --unless, of course, you use other draw options.

---

<div class="post-metadata">

**Author:** ![Bassam](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/b38774/32.png) [@Bassam](https://root-forum.cern.ch/u/Bassam)\
**Post date:** [February 21, 2023, 7:06pm UTC](https://root-forum.cern.ch/t/repositioning-stat-box-for-a-fit-to-tmultigraph/51381/15 "2023-02-21T19:06:10Z")

</div>

@dastudillo Thank you very much for your reply! Yes, the portion of code that I show is only a small fraction of a much larger macro that produces 10’s of histograms. It could be very well that there are other parts of my code that have impact on what I am trying to accomplish in the portion I posted here.

I appreciate the help of everyone here! I decided to focus on other things since moving the stat box was not a top priority. Thank you very much for your help!

---

<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:** [February 22, 2023, 7:50am UTC](https://root-forum.cern.ch/t/repositioning-stat-box-for-a-fit-to-tmultigraph/51381/16 "2023-02-22T07:50:49Z")

</div>

The stats are stored in the last graph:

```auto
{
   TCanvas *c = new TCanvas();
   gStyle->SetOptFit(1111);
   TGraph *g[3];
   Double_t x[10] = {0,1,2,3,4,5,6,7,8,9};
   Double_t y[10] = {1,2,3,4,5,5,4,3,2,1};
   TMultiGraph *mg = new TMultiGraph();
   for (int i=0; i<3; i++) {
      g[i] = new TGraph(10, x, y);
      for (int j=0; j<10; j++) y[j] = y[j]-1;
      mg->Add(g[i]);
   }
   mg->Draw("a*l");
   mg->Fit("gaus");

   gPad->Modified();
   gPad->Update();
   st = (TPaveStats*)g[2]->GetListOfFunctions()->FindObject("stats");
   if (!st) { cout << "stats not found!\n"; return;}
   st->SetX1NDC(0.4);
   st->SetX2NDC(0.6);
   st->SetY1NDC(0.2);
   st->SetY2NDC(0.4);
   gPad->Modified();
   gPad->Update();
}

```

 ![Screenshot 2023-02-22 at 08.50.25](https://root-forum.cern.ch/uploads/default/original/3X/9/3/93762a79d63deb5a27da67b279e31805f9a51872.jpeg)
