# How to include errors in Graph

**URL:** <https://root-forum.cern.ch/t/how-to-include-errors-in-graph/38592>\
**Category:** Newbie\
**Tags:** hist, root\
**Created:** [March 27, 2020, 8:10am UTC](https://root-forum.cern.ch/t/how-to-include-errors-in-graph/38592 "2020-03-27T08:10:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![robertfroast](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/robertfroast/32/21121_2.png) [@robertfroast](https://root-forum.cern.ch/u/robertfroast)\
**Post date:** [March 27, 2020, 8:10am UTC](https://root-forum.cern.ch/t/how-to-include-errors-in-graph/38592/1 "2020-03-27T08:10:28Z")

</div>

@couet @Wile_E_Coyote  
Hi !  
I have drawn a histogram (Efficiency vs Energy).  
I got the value of efficiency by using formula Efficiency = (A/B) for different points in the graph.  
Now, I want to include the errors by using the following:  
If error in A = delta\_A and error in B is delta\_B, then error\_Efficiency = efficiency x Sqrt [(delta\_A /A)^2 + (delta\_B /B)^2]

where delta\_A = Sqrt (A) , delta\_B = Sqrt (B).  
Till now i have plotted the curve.

 ![Eff](https://root-forum.cern.ch/uploads/default/original/3X/9/e/9e80a2dba5fbbd0f0edb86c63ff94f2001046c7e.png)  
How should i proceed?

[effvsE.txt](https://root-forum.cern.ch/uploads/short-url/xKAspnCPLbmriKYo3om6JaTlFBv.txt) (144 Bytes)  
This file include two columns. First columns is denoting A and second column is denoting B.

---

<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:** [March 27, 2020, 10:01am UTC](https://root-forum.cern.ch/t/how-to-include-errors-in-graph/38592/2 "2020-03-27T10:01:12Z")

</div>

In which object have put your data ? A TGraph I guess ? If you need to have errors you should use a TGraphErrors.

---

<div class="post-metadata">

**Author:** ![robertfroast](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/robertfroast/32/21121_2.png) [@robertfroast](https://root-forum.cern.ch/u/robertfroast)\
**Post date:** [March 27, 2020, 10:56am UTC](https://root-forum.cern.ch/t/how-to-include-errors-in-graph/38592/3 "2020-03-27T10:56:29Z")

</div>

ok @couet.  
Let me start with Tgraphs first.

Below is the text file  
[effvsE.txt](https://root-forum.cern.ch/uploads/short-url/ctWKn7MEnNoRcFmQa5PFT5XAbMJ.txt) (218 Bytes)  
which includes x,y,z where z denotes error.  
The code that i was using is the following:

```auto
#include "TFile.h"
#include "TGraphErrors.h"
#include "TF1.h"
void efficiency() {
   ifstream mydat("effvsE.txt");
   TCanvas* c1 = new TCanvas("c1","Graph");
   float x,y,z;

   auto g = new TGraphErrors();
   int i = 0;

    while(!mydat.eof()) {
      if (mydat >> x >> y >> z) {
         g->SetPoint(i,x,y);
         i++;
      }
    }

   g->SetMarkerStyle(3);
   g->Draw("ALP");
   g->SetTitle("Efficiency Vs Energy");
   g->GetXaxis()->SetTitle("Energy");
   g->GetYaxis()->SetTitle("Efficiency");
   mydat.close();
}

```

This is just showing the graph that i was getting earlier when z is not written and Tgrapherrors are not making any difference.  
Tell me where i am doing some mistake.

---

<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:** [March 27, 2020, 11:09am UTC](https://root-forum.cern.ch/t/how-to-include-errors-in-graph/38592/4 "2020-03-27T11:09:05Z")

</div>

```auto
void efficiency() {
   ifstream mydat("effvsE.txt");
   TCanvas* c1 = new TCanvas("c1","Graph");
   float x,y,z;

   auto g = new TGraphErrors();
   int i = 0;

    while(!mydat.eof()) {
      if (mydat >> x >> y >> z) {
         g->SetPoint(i,x,y);
         g->SetPointError( i, 0, z);
         i++;
      }
    }

   g->SetMarkerStyle(3);
   g->Draw("ALP");
   g->SetTitle("Efficiency Vs Energy");
   g->GetXaxis()->SetTitle("Energy");
   g->GetYaxis()->SetTitle("Efficiency");
   mydat.close();
}

```

 ![Screenshot 2020-03-27 at 12.07.49](https://root-forum.cern.ch/uploads/default/original/3X/3/7/37ef216e775555d9df5795e319e169f6061f1662.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:** [March 27, 2020, 11:12am UTC](https://root-forum.cern.ch/t/how-to-include-errors-in-graph/38592/5 "2020-03-27T11:12:30Z")

</div>

```cpp
{
  TGraphErrors *g = new TGraphErrors("effvsE.txt", "%lg %lg %lg");
  g->SetTitle("Efficiency Vs Energy;Energy;Efficiency");
  g->SetMarkerStyle(3);
  g->Draw("ALP");
}

```

---

<div class="post-metadata">

**Author:** ![robertfroast](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/robertfroast/32/21121_2.png) [@robertfroast](https://root-forum.cern.ch/u/robertfroast)\
**Post date:** [March 27, 2020, 11:46am UTC](https://root-forum.cern.ch/t/how-to-include-errors-in-graph/38592/6 "2020-03-27T11:46:53Z")

</div>

Thanks @couet @Wile_E_Coyote.  
Now, I want to fit this graph by using below function:

```auto
Double_t Fit(Double_t *x, Double_t *par)
{
        Double_t arg1 = TMath::Log(0.01*x[0]); // in keV
        Double_t arg2 = TMath::Log(0.001*x[0]); // in keV

            Double_t fit_val = TMath::Exp(TMath::Power((TMath::Power((par[0]+par[1]*arg1+par[2]*arg1*arg1),(-1.0*par[3]))+TMath::Power((par[4]+par[5]*arg2+par[6]*arg2*arg2),(-1.0*par[3]))),(-1.0*1/par[3])))/1000.;
        return fit_val;
} 

```

How should i proceed?

---

<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:** [March 27, 2020, 1:22pm UTC](https://root-forum.cern.ch/t/how-to-include-errors-in-graph/38592/7 "2020-03-27T13:22:12Z")

</div>

[ROOT User’s Guide -\> Graphs -\> Fitting a Graph](https://root.cern.ch/root/htmldoc/guides/users-guide/ROOTUsersGuide.html#fitting-a-graph)
