# Plot best fit with errors

**URL:** <https://root-forum.cern.ch/t/plot-best-fit-with-errors/33802>\
**Category:** Newbie\
**Created:** [April 29, 2019, 7:10am UTC](https://root-forum.cern.ch/t/plot-best-fit-with-errors/33802 "2019-04-29T07:10:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thabo\_Lepota](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/58956e/32.png) [@Thabo\_Lepota](https://root-forum.cern.ch/u/Thabo_Lepota)\
**Post date:** [April 29, 2019, 7:10am UTC](https://root-forum.cern.ch/t/plot-best-fit-with-errors/33802/1 "2019-04-29T07:10:41Z")

</div>

Hello,  
How can i plot and fit the data with first degree polynomial, reading in the data from an ascii file (see attached) [Run\_13-19.txt](https://root-forum.cern.ch/uploads/default/original/3X/5/2/52e73b995039f4c6f047df77cf440ee6c3c25cc6.txt) (1.7 KB)  
i’m tryin to use this script [basic.C](https://root-forum.cern.ch/uploads/default/original/3X/4/0/40696b9679ff831ec611f58f848b3e7c878f45a1.C) (2.7 KB)  
instead of plotting histogram : i’m not if its wise to use TGraph \*h1 = new TGraph(nlines,time,Vi\_brick1) ; gStyle-\>SetOptFit(0001);

i’m more interested in displaying the best fit line, with its parameters and excluding the points

Thank you in advance

---

<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:** [April 29, 2019, 8:06am UTC](https://root-forum.cern.ch/t/plot-best-fit-with-errors/33802/2 "2019-04-29T08:06:26Z")

</div>

```auto
{
  gStyle->SetStatStyle(0); // hollow
  gStyle->SetOptFit(10011);
  TTree *t = new TTree("ntuple", "data from txt file");
  t->ReadFile("Run_13-19.txt",
              "Time/I:Vi_Brick1/F:Io_Brick1:Vo_Brick1:Vi_Brick2:Io_Brick2:Vo_Brick2:Vi_Brick3:Io_Brick3:Vo_Brick3:Vi_Brick4:Io_Brick4:Vo_Brick4");
  t->SetEstimate(-1);
  // t->Print();
  t->SetMarkerStyle(kFullStar); t->SetMarkerColor(kMagenta);
  t->Draw("Vi_Brick1:Time");
  if (gPad) {
    TGraph *g = (TGraph*)gPad->GetPrimitive("Graph");
    if (g) {
      g->Fit("pol1", "Q");
#if 0 /* 0 or 1 */
      TF1 *f = (TF1*)g->GetListOfFunctions()->FindObject("pol1");
      if (f) {
        f = new TF1(*f); // make a copy
        f->Draw();
        TLatex *l = new TLatex(); l->SetTextFont(42); l->SetTextSize(0.03);
        l->DrawLatexNDC(0.35, 0.85, Form("p0 = %g #pm %g", f->GetParameter(0), f->GetParError(0)));
        l->DrawLatexNDC(0.35, 0.80, Form("p1 = %g #pm %g", f->GetParameter(1), f->GetParError(1)));
        // f->Print();
      }
#endif /* 0 or 1 */
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Thabo\_Lepota](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/58956e/32.png) [@Thabo\_Lepota](https://root-forum.cern.ch/u/Thabo_Lepota)\
**Post date:** [April 29, 2019, 8:48am UTC](https://root-forum.cern.ch/t/plot-best-fit-with-errors/33802/3 "2019-04-29T08:48:11Z")

</div>

I should incorporate this macro into the basic.C script?

---

<div class="post-metadata">

**Author:** ![Thabo\_Lepota](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/58956e/32.png) [@Thabo\_Lepota](https://root-forum.cern.ch/u/Thabo_Lepota)\
**Post date:** [April 29, 2019, 9:56am UTC](https://root-forum.cern.ch/t/plot-best-fit-with-errors/33802/4 "2019-04-29T09:56:16Z")

</div>

If you want to display just the best fit line and exclude points on the graph, how would you go about doing that?

---

<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:** [April 29, 2019, 10:37am UTC](https://root-forum.cern.ch/t/plot-best-fit-with-errors/33802/5 "2019-04-29T10:37:08Z")

</div>

```auto
#if 1 /* 0 or 1 */
```

---

<div class="post-metadata">

**Author:** ![Thabo\_Lepota](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/58956e/32.png) [@Thabo\_Lepota](https://root-forum.cern.ch/u/Thabo_Lepota)\
**Post date:** [April 29, 2019, 10:44am UTC](https://root-forum.cern.ch/t/plot-best-fit-with-errors/33802/6 "2019-04-29T10:44:09Z")

</div>

doing so it doesnt display the parameters of the best fit on the plot

---

<div class="post-metadata">

**Author:** ![Thabo\_Lepota](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/58956e/32.png) [@Thabo\_Lepota](https://root-forum.cern.ch/u/Thabo_Lepota)\
**Post date:** [April 29, 2019, 12:58pm UTC](https://root-forum.cern.ch/t/plot-best-fit-with-errors/33802/7 "2019-04-29T12:58:12Z")

</div>

In a case where by the each parameter (Vi\_brick1,Io\_brick1,Vo\_brick1,…) have errors that accompanies them , how can i include them in the plots?
