Adding statistics in plot from ascii file without fitting

I have a .txt file with two columns. I tried to put one column in x axis and and another in y axis and plot x vs y. Now I want to show the statistics in the plot without fitting it. How can I do so. Here is what I have done.

void nofit()
{
TGraph* gr = new TGraph(“m1.txt”);
gROOT->ForceStyle();
gr->SetMarkerStyle(0);
gr->SetFillColor(kGreen);
gr->Draw(“AP”);
gr->GetYaxis()->SetTitle(“Events”);
gr->GetXaxis()->SetTitle(“Time”);
//gr->SetStats(stats=kTRUE)
//gStyle->SetOptStat(“ne”);
gStyle->SetOptStat(1111);
//gr->SetStats(1111);
}

I tried all the options available in the forum for statistics in TGraph, but it looks like nothing worked for me.

Try

        TGraph* gr = new TGraph("m1.txt");
        gStyle->SetOptStat(1111);
        gr->GetHistogram()->SetStats(kTRUE);
        gr->SetMarkerStyle(0);
        gr->SetFillColor(kGreen);
        gr->Draw("AP");
        gr->GetYaxis()->SetTitle("Events");
        gr->GetXaxis()->SetTitle("Time");

By changing it, I’m getting the legend, but it’s giving 0 value for all the statistical parameters. Also, SetMarkerStyle() doesn’t seem to be working after the changes.

can you provide m1.txt ?

Note that statistics are for histograms not for graphs. Graphs are just (x,y) points there are no statistics. The histogram you get with GetHistogram is an empty one used to draw the axis only. As it is empty there are no statistics either. Also, note that marker style 0 does not exist,

m1.txt (72.6 KB)

Here is the file. So is it mean I should plot a histogram only to get the statistics? Also, marker style is not working for different styles also if I’m putting GetHistogram

void nofit()
{
   TGraph* gr = new TGraph("m1.txt");
   gr->SetLineColor(kGreen);
   gr->Draw("AL");
   gr->GetYaxis()->SetTitle("Events");
   gr->GetXaxis()->SetTitle("Time");
}

But the is a TGraph. As i said TGraph are just set off point, you cannot have stats from a TGraph. You need to fill a TH1D or TH1F to get stats.

I tried the following code and it worked!

#include
#include
#include “TH1F.h”
#include “TCanvas.h”

using namespace std;

int solved() {
ifstream inputFile(“m1.txt”);
TH1F *hist = new TH1F(“hist”, “M1”, 5000, -0.00000005, -0.00000003);
hist->GetXaxis()->SetTitle(“Time(sec)”);
hist->GetYaxis()->SetTitle(“Events”);

double x, y;
while (inputFile >> x >> y) {
cout << "x = " << x << ", y = " << y << endl;

hist->Fill(x, y);

}

TCanvas *canvas = new TCanvas(“canvas”, “My Canvas”);
hist->Draw();
double yMax = hist->GetBinContent(hist->GetMaximumBin());
hist->GetYaxis()->SetRangeUser(0, yMax * 2.1);

canvas->SaveAs(“histogram.jpg”);

return 0;
}

But I just have a doubt that value on Y axis got changed. I tried with maximum Y value as Y* 1.1 but it could not generate whole plot, So I have to change that to Y*2.1. Can you tell where the error is?

If the coulmns in the file are x and y, you need a 2D histogram, not 1D, i.e. use TH2F; then fill the 2D histo the way you did: h->Fill(x,y). If you fill a 1D histo like this, you are actually filling (incrementing) x with a weight y.

You can convert a graph into a histogram:

{
  TGraph *g = new TGraph("m1.txt");
  // g->Sort(); // just a precaution
  Int_t n = g->GetN();
  // note: if the graph's points "x coordinates" are not equidistant,
  //       they will usually not coincide with the histogram's "bin centers"
  Double_t *x = new Double_t[(n + 1)];
  x[0] = (3.0 * g->GetX()[0] - g->GetX()[1]) / 2.0;
  x[n] = (3.0 * g->GetX()[(n - 1)] - g->GetX()[(n - 2)]) / 2.0;
  for (Int_t i = 1; i < n; i++)
    { x[i] = (g->GetX()[(i - 1)] + g->GetX()[i]) / 2.0; }
  TH1D *h = new TH1D("h", "time histogram;Time;Events", n, x);
  for (Int_t i = 0; i < n; i++) {
    h->SetBinContent(i + 1, g->GetY()[i]);
  }
  h->ResetStats(); // reset the statistics including the number of entries
  delete [] x; // no longer needed
  delete g; // no longer needed
  gStyle->SetOptStat("neMRuoISK");
  h->Draw();
  // g->SetMarkerStyle(20); g->Draw("P");
}

Is there any way to plot many files on same canvas and give X axis a desired range?
I did the following, but getting a plot like attached one. Also, can I plot a smooth line in the plot? changing the Draw() options is not helping.

void combined(Double_t xMin=0.00000001, Double_t xMax=0.00000007)
{
TGraph *g1 = new TGraph(“F1–180v–00012.txt”);
// g->Sort(); // just a precaution
Int_t n1 = g1->GetN();
TGraph *g2 = new TGraph(“F1–180v–00013.txt”);
Int_t n2 = g2->GetN();
TGraph *g3 = new TGraph(“F1–180v–00014.txt”);
Int_t n3 = g3->GetN();
TGraph *g4 = new TGraph(“F1–180v–00015.txt”);
Int_t n4 = g4->GetN();
TGraph *g5 = new TGraph(“F1–180v–00016.txt”);
Int_t n5 = g5->GetN();
// note: if the graph’s points “x coordinates” are not equidistant,
// they will usually not coincide with the histogram’s “bin centers”
Double_t *x1 = new Double_t[(n1 + 1)];
x1[0] = (3.0 * g1->GetX()[0] - g1->GetX()[1]) / 2.0;
x1[n1] = (3.0 * g1->GetX()[(n1 - 1)] - g1->GetX()[(n1 - 2)]) / 2.0;
for (Int_t i = 1; i < n1; i++)
{ x1[i] = (g1->GetX()[(i - 1)] + g1->GetX()[i]) / 2.0; }

Double_t *x2 = new Double_t[(n2 + 1)];

x2[0] = (3.0 * g2->GetX()[0] - g2->GetX()[1]) / 2.0;
x2[n2] = (3.0 * g2->GetX()[(n2 - 1)] - g2->GetX()[(n2 - 2)]) / 2.0;
for (Int_t i = 1; i < n2; i++)
{ x2[i] = (g2->GetX()[(i - 1)] + g2->GetX()[i]) / 2.0; }

Double_t *x3 = new Double_t[(n3 + 1)];

x3[0] = (3.0 * g3->GetX()[0] - g3->GetX()[1]) / 2.0;
x3[n3] = (3.0 * g3->GetX()[(n3 - 1)] - g3->GetX()[(n3 - 2)]) / 2.0;
for (Int_t i = 1; i < n3; i++)
{ x3[i] = (g3->GetX()[(i - 1)] + g3->GetX()[i]) / 2.0; }

Double_t *x4 = new Double_t[(n4 + 1)];

x4[0] = (3.0 * g4->GetX()[0] - g4->GetX()[1]) / 2.0;
x4[n4] = (3.0 * g4->GetX()[(n4 - 1)] - g4->GetX()[(n4 - 2)]) / 2.0;
for (Int_t i = 1; i < n4; i++)
{ x4[i] = (g4->GetX()[(i - 1)] + g4->GetX()[i]) / 2.0; }

Double_t *x5 = new Double_t[(n5 + 1)];

x5[0] = (3.0 * g5->GetX()[0] - g5->GetX()[1]) / 2.0;
x5[n5] = (3.0 * g5->GetX()[(n5 - 1)] - g5->GetX()[(n5 - 2)]) / 2.0;
for (Int_t i = 1; i < n5; i++)
{ x5[i] = (g5->GetX()[(i - 1)] + g5->GetX()[i]) / 2.0; }

TH1D *h1 = new TH1D(“h1”, " ;Time;Events", n1, x1);
for (Int_t i = 0; i < n1; i++) {
h1->SetBinContent(i + 1, g1->GetY()[i]);
}
h1->ResetStats();
h1->GetXaxis()->SetRangeUser(xMin, xMax); // reset the statistics including the number of entries

TH1D *h2 = new TH1D(“h2”, " ;Time;Events", n2, x2);
for (Int_t i = 0; i < n2; i++) {
h2->SetBinContent(i + 1, g2->GetY()[i]);
}
h2->ResetStats();

TH1D *h3 = new TH1D(“h3”, “10Hz;Time;Events”, n3, x3);
for (Int_t i = 0; i < n3; i++) {
h3->SetBinContent(i + 1, g3->GetY()[i]);
}
h3->ResetStats();

TH1D *h4 = new TH1D(“h4”, “20Hz;Time;Events”, n4, x4);
for (Int_t i = 0; i < n4; i++) {
h4->SetBinContent(i + 1, g4->GetY()[i]);
}
h4->ResetStats();

TH1D *h5 = new TH1D(“h5”, “40Hz;Time;Events”, n5, x5);
for (Int_t i = 0; i < n5; i++) {
h5->SetBinContent(i + 1, g5->GetY()[i]);
}
h5->ResetStats();
delete [] x1; // no longer needed
delete g1; // no longer needed
delete [] x2; // no longer needed
delete g2;
delete [] x3; // no longer needed
delete g3;
delete [] x4; // no longer needed
delete g4;
delete [] x5; // no longer needed
delete g5;
gStyle->SetOptStat(“0000”);
h1->Draw();
h1->SetLineColor(kBlue);
h1->SetLineWidth(2);
h1->GetXaxis()->SetRange(0.000000041,0.000000043);
h1->Scale(1.0/h1->GetMaximum());
//h1->Scale(1/h1->Integral(0,-1));
//h1->GetRms();
h2->Draw(“same”);
h2->SetLineColor(kRed);
h2->SetLineWidth(2);
//h2->GetXaxis()->SetRange(0.000000041,0.000000043);
h2->Scale(1.0/h2->GetMaximum());
//h2->Scale(1/h2->Integral(0,-1));
//h2->GetRms();
h3->Draw(“same”);
h3->SetLineColor(kGreen);
h3->SetLineWidth(2);
//h3->GetXaxis()->SetRange(0.000000041,0.000000043);
h3->Scale(1.0/h3->GetMaximum());
//h3->Scale(1/h3->Integral(0,-1));
//h3->GetRms();
h4->Draw(“same”);
h4->SetLineColor(kYellow);
h4->SetLineWidth(2);
//h4->GetXaxis()->SetRange(0.000000041,0.000000043);
h4->Scale(1.0/h4->GetMaximum());
//h4->Scale(1/h4->Integral(0,-1));
//h4->GetRms();
h5->Draw(“same”);
h5->SetLineColor(kCyan);
h5->SetLineWidth(2);
//h5->GetXaxis()->SetRange(0.000000041,0.000000043);
h5->Scale(1.0/h5->GetMaximum());
//h5->Scale(1/h5->Integral(0,-1));
//h5->GetRms();

//auto legend = new TLegend(0.1,0.7,0.48,0.9);
//legend->SetHeader(“The Legend Title”,“C”); // option “C” allows to center the header
//legend->AddEntry(“h1”,“2.5Hz,rms=83.12ps”,“l”);
//legend->AddEntry(“h2”,“5Hz, rms=83.08ps”,“l”);
//legend->AddEntry(“h3”,“10Hz, rms=99.69ps”,“l”);
//legend->AddEntry(“h4”,“20Hz, rms=97.73ps”,“l”);
//legend->AddEntry(“h5”,“40Hz, rms=98.63ps”,“l”);
//legend->Draw();
//gStyle->SetOptStat(“eMR”);
// g->SetMarkerStyle(20); g->Draw(“P”);

}

Suppose you draw the first histogram without the option SAME and the others with the option SAME. the range along X and Y axis will be imposed by the first histogram drawn. You can also group the histograms into a THStack and the best range including all the histograms will be computed for you.

I made the code like
void combined4(){
auto hs = new THStack(“hs”,“test stacked histograms”);

ifstream in;
in.open(Form(“F1–180v–00012.txt”));
Float_t x,y;
TH1F *he1 = new TH1F(“he1”," with Histogram ",50000,0.00000004,0.000000045);

while (1) {
in >> x >> y;
if (!in.good()) break;
he1->Fill(x,y);
}

he1->SetFillColor(kYellow);
he1->GetXaxis()->SetTitle(“Time(sec)”);
he1->GetYaxis()->SetTitle(“Events”);
he1->GetXaxis()->SetRange(0.00000004,0.000000045);
hs->Add(he1,“hist1”);

ifstream inp;
inp.open(Form(“F1–180v–00013.txt”));
Float_t e,r;

TH1F *he2 = new TH1F(“he2”," with Histogram ",50000,0.00000004,0.000000045);
while (1) {
inp >> e >> r;
if (!inp.good()) break;
he2->Fill(e,r);
}
he2->SetFillColor(kRed);
hs->Add(he2,“hist2”);

hs->Draw(“AL*”);

}

And the error that I’m getting is
/home/ganapati/xray/1.timing/combined4.C:18:41: warning: implicit conversion from ‘double’ to ‘Int_t’ (aka ‘int’) changes value from 4.5E-8 to 0 [-Wliteral-conversion]
he1->GetXaxis()->SetRange(0.00000004,0.000000045);
~~~~~~~~ ^~~~~~~~~~~
/home/ganapati/xray/1.timing/combined4.C:18:30: warning: implicit conversion from ‘double’ to ‘Int_t’ (aka ‘int’) changes value from 4.0E-8 to 0 [-Wliteral-conversion]
he1->GetXaxis()->SetRange(0.00000004,0.000000045);

The plot is also attached where i’m not getting desired color and the x and Y axis names as well.

Try SetRangeUser() instead of SetRange()