How to include errors in Graph

@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.


How should i proceed?

effvsE.txt (144 Bytes)
This file include two columns. First columns is denoting A and second column is denoting B.

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

ok @couet.
Let me start with Tgraphs first.

Below is the text file
effvsE.txt (218 Bytes)
which includes x,y,z where z denotes error.
The code that i was using is the following:

#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.

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

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

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

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?

ROOT User’s Guide -> Graphs -> Fitting a Graph