Problem in plotting from text file using TGraph Err

Hi,

I want to plot a TGRaph with errors by reading the x, y and the corresponding error of data points from a text file of 100 entries. Unfortunatly I am not able to plot it correctly and I dont undertant what is the problem. I attach my macro and text file here with :

#include <TRandom.h>
#include <TMath.h>
#include <TH1.h>
#include <TH2.h>
#include <TFile.h>

#include <TTree.h>
#include <TF1.h>

#include
#include
#include
#include
#include
#include
#include <stdlib.h>
#include <stdio.h>
#include <math.h>

using namespace std;

TString currentfile;
TRandom rnd;

void Der_Err()
{

Double_t data[8];
char line[150];
Int_t len=150;

Double_t x[100], y[100], Err_y[100], Err_x[100];

Int_t i, j;
strstream *InOut;

ifstream simu;
simu.open(“Der_Err_1.txt”);

if(!simu)
{
cout << “Could not open the simulation file !” << endl;
exit(-1);
}
else
{
cout.setf(ios::showpoint);
cout << “Reading GEF input file” << endl;
}

for(j=0;j<100;j++) {

for(i=0;i<5;i++) 
{
data[i] = 0.0;
}
simu.getline(line,len);

  InOut = new strstream();
  *InOut << line; 
for(i=0;i<5;i++) 

{
*InOut >> data[i];

}

delete InOut;

x[i]= data[0];
y[i]= data[1];
Err_x[i]= data[2];
Err_y[i]= data[3];

cout<<x[i]<<“\t”<<y[i]<<“\t”<<Err_x[i]<<“\t”<< Err_y[i]<<endl;
}

TGraphErrors *ggamma = new TGraphErrors(100,x,y,Err_x,Err_y);
ggamma->SetMarkerColor(kGreen+3);
ggamma->SetMarkerStyle(20);
ggamma->SetMarkerSize(1.6);
ggamma->Draw(“ALP”);

}

and here is the text file

Der_Err_1.txt (3.3 KB)

When I run the above macro, I am able to assign the values to the array but when I plot I get :

Capture


Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


{
  TGraphErrors *g = new TGraphErrors("Der_Err_1.txt");
  if (g->GetN() > 0) g->Draw("AP");
}
1 Like

Amazing ! That works, thanks a lot!

I tried the same for a different text file and unfortunately it doest work, cant understand the reason why …

Attaching the file here
new1.txt (4.1 KB)

It is because you have very large (wrong ?) errors bars along X.
Try:

TGraph *g = new TGraph("new1.txt");
g->Draw("AL");

With TGraph it works but not with TGraph errors.

Also I use the following to fit the plot, but unfortunately the fit is not plotted on the histogram, how can I plot the fit function:

{
  TGraph *g = new TGraph("new1.txt");
  g->SetTitle("Yield versus Mass;Mass [amu];Yield [%]");
  g->SetMarkerStyle(kFullDotMedium);
  // g->Print();
  TH1F *h1 = new TH1F("h1", "Yield versus Mass;Mass [amu];Yield [%]", g->GetN(),
                     g->GetX()[0] - 0.5, g->GetX()[(g->GetN() - 1)] + 0.5);
  for (int i = 0; i < g->GetN(); i++) h1->Fill(g->GetX()[i], g->GetY()[i]);
  // h->Print("all");

 // gStyle->SetOptStat("iRMe");
  TCanvas *c = new TCanvas("c", "c");

gStyle->SetOptStat(2211);
gStyle->SetOptFit(1111);
  c->Divide(1, 2);
  c->cd(1);
  g->Draw("ALP");
  c->cd(2);
 // h->Draw("HIST");
 // c->cd(0);

// Mass Limits
Double_t LowerLimit_Mass = 38;//52;//55;
Double_t UpperLimit_Mass = 142;//127;//124;



TF1 *f12 = new TF1("f12","[0]*exp(-0.5*((x-[1])/[2])**2) + [3]*exp(-0.5*((x-([1]-[4]))/[5])**2) + [3]*exp(-0.5*((x-([1]+[4]))/[5])**2)");


   f12->SetFillColor(19);
   f12->SetFillStyle(0);
   
   f12->SetLineWidth(2);
  

   f12->SetParameter(0,100);
   f12->SetParLimits(0,50,120000);

   f12->FixParameter(1, 90);
   //f12->SetParameter(1, 88);
   //f12->SetParLimits(1,0,150);

   //f12->FixParameter(2, 9.59);
   f12->SetParameter(2, 9);
   f12->SetParLimits(2,0,40);

   f12->SetParameter(3, 2000);
   f12->SetParLimits(3,0,5000);

   f12->SetParameter(4, 13.83);
   f12->SetParLimits(4,5,20);

   f12->SetParameter(5, 9.055);
   f12->SetParLimits(5,0,15);

   //h12->Fit("f12", "MEL", "", LowerLimit_Mass, UpperLimit_Mass);
   h1->Draw("HIST");
   h1->Fit(f12, "", "", LowerLimit_Mass, UpperLimit_Mass);
   f12->SetLineColor(2);
   f12->Draw("SAME");

}

and I get the following where the fit is not plotted :

image

It works with TGrapErrors also. And the plot you get is want you have in your file:

...
  64.000  0.2004E+04  0.2049E+04  0.1959E+04
  66.000  0.2383E+04  0.2432E+04  0.2334E+04
  68.000  0.3012E+04  0.3067E+04  0.2957E+04
  70.000  0.3510E+04  0.3569E+04  0.3451E+04
  72.000  0.4191E+04  0.4256E+04  0.4126E+04
  74.000  0.4601E+04  0.4669E+04  0.4533E+04
  76.000  0.5205E+04  0.5277E+04  0.5133E+04
  78.000  0.5538E+04  0.5612E+04  0.5464E+04
  80.000  0.5724E+04  0.5800E+04  0.5648E+04
...

Look at the errors, 3rd and 4th columns, they are as big as the Y values, 2nd column.

{
   TGraph *g = new TGraph("new1.txt");
   g->SetTitle("Yield versus Mass;Mass [amu];Yield [%]");
   g->SetMarkerStyle(kFullDotMedium);

   TH1F *h1 = new TH1F("h1", "Yield versus Mass;Mass [amu];Yield [%]", g->GetN(),
                     g->GetX()[0] - 0.5, g->GetX()[(g->GetN() - 1)] + 0.5);
   for (int i = 0; i < g->GetN(); i++) h1->Fill(g->GetX()[i], g->GetY()[i]);

   TCanvas *c = new TCanvas("c", "c");

   gStyle->SetOptStat(2211);
   gStyle->SetOptFit(1111);
   c->Divide(1, 2);
   c->cd(1);
   g->Draw("ALP");
   c->cd(2);

// Mass Limits
   Double_t LowerLimit_Mass = 38;//52;//55;
   Double_t UpperLimit_Mass = 142;//127;//124;

   TF1 *f12 = new TF1("f12","[0]*exp(-0.5*((x-[1])/[2])**2) + [3]*exp(-0.5*((x-([1]-[4]))/[5])**2) + [3]*exp(-0.5*((x-([1]+[4]))/[5])**2)");


   f12->SetFillColor(19);
   f12->SetFillStyle(0);

   f12->SetLineWidth(2);


   f12->SetParameter(0,100);
   f12->SetParLimits(0,50,120000);

   f12->FixParameter(1, 90);

   f12->SetParameter(2, 9);
   f12->SetParLimits(2,0,40);

   f12->SetParameter(3, 2000);
   f12->SetParLimits(3,0,5000);

   f12->SetParameter(4, 13.83);
   f12->SetParLimits(4,5,20);

   f12->SetParameter(5, 9.055);
   f12->SetParLimits(5,0,15);

   h1->Fit(f12, "", "", LowerLimit_Mass, UpperLimit_Mass);
}

It seems to me that your new file does not follow the TGraphErrors standard (each line should be “x y ex ey”).

My guess is that you have “x y (y + eyh) (y - eyl)”.

{
  TGraphErrors *g = new TGraphErrors("new1.txt"); // x y (y + eyh) (y - eyl)
  for (Int_t i = 0; i < g->GetN(); i++) // assuming that always eyh = eyl
    { g->GetEX()[i] = 0.; g->GetEY()[i] = g->GetY()[i] - g->GetEY()[i]; }
  if (g->GetN() > 0) g->Draw("AP");
}
2 Likes

yes I confimed, thats how it is in the txt file. and the errors are not uniform with respect to the data points…

Next time, ensure you know precisely IN ADVANCE what your data files contain.

Sure I will.
Sorry for the inconvenience…