Problem in histogram filling

Dear experts,

I do not understand why the error lengths plotted are higher than the actual. Any help is appreciated.

    auto g1 = new TGraphErrors("data.txt","%lg %lg %lg");
    g1->Sort();

   int n = g1->GetN();
   double *x = g1->GetX();
   double *y = g1->GetY();

   auto h = new TH1D("h","h",1000,x[0],x[n]);
   for(int i=0; i < n; ++i) {
      g1->GetPoint(i, x[i], y[i]);
      h->Fill(x[i],y[i]);
      h->SetBinError(i, g1->GetEY()[i]);
   }

//   for(int i=0; i < n; ++i) {
//   cout << x[i] <<"\t" << y[i] << "\t" << h->GetBinError(i) << endl;
//  }

   h->Draw("E1");

data.txt (1.3 KB)


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


Why not simply doing:

    auto g1 = new TGraphErrors("data.txt","%lg %lg %lg");
    g1->Sort();
   g1->Draw("AP");

Dear @couet

No, I want a histogram for my later analysis. The problems of histogram created in this manner are-

(1) In that case, error lengths are not ok.
(2) SetRangeUser is not working properly.

Any idea why?

But filling and histogram with these points will give you a complete different view… try:

{
    auto g1 = new TGraphErrors("data.txt","%lg %lg %lg");

   int      n = g1->GetN();
   double  *x = g1->GetX();
   double  *y = g1->GetY();
   double *ey = g1->GetEY();

   double xmin=0, xmax=0;
   for (int j = 0; j<n; j++) {
      if(xmin > x[j]) xmin = x[j];
      if(xmax < x[j]) xmax = x[j];
   }

   auto h = new TH1D("h","h",1000,xmin,xmax);
   printf("%g %g\n",xmin,xmax);
   for(int i=0; i < n; ++i) {
      h->Fill(x[i],y[i]);
   }
   h->Draw("HIST");
}

You have several time the same value on X, they are cumulated in the same bin … you get a flat distribution … I am not sure that’s what you are looking for.

Dear @couet

thanks. Any idea how to force it to be in h->GetXaxis()->SetRangeUser(80.0,180.0); range?

Yes but first see the histogram my last macro produces… is that really want you want ? I think we should clarify that 1st …

I’ve seen that. But, As I said. I want it to be drawn as points. So, I drawn it with h->Draw(“E1”); That is solved.

But, I am now stuck in the range fixing problem only. That’s all.

What is the initial range of your histogram ? you cannot enlarge range

xmin =0, xmax =168

180 > 168 … you cannot enlarge …

Define right away your histogram with a larger range,

1 Like

Now, with that in hand,

Why can’t I fit this data with simple “gaus” and “L” (log likelihood) option? Any idea?

h->Fit(“gaus”,“Lr+”,“E1”,60.0,190.0);

dt.txt (2.7 KB)

dt.txt is a new data set … what is the corresponding macro ? with the Fit command inside ?

{
    auto g1 = new TGraphErrors("dt.txt","%lg %lg %lg");

   int      n = g1->GetN();
   double  *x = g1->GetX();
   double  *y = g1->GetY();
   double *ey = g1->GetEY();

   auto h = new TH1D("h","h",1000,60,190);
   for(int i=0; i < n; ++i) {
      h->Fill(x[i],y[i]);
   }
   h->Fit(“gaus”,“Lr+”,“E1”,60.0,190.0);
}

You have wrong quotes:

{
   auto g1 = new TGraphErrors("dt.txt","%lg %lg %lg");

   int      n = g1->GetN();
   double  *x = g1->GetX();
   double  *y = g1->GetY();
   double *ey = g1->GetEY();

   auto h = new TH1D("h","h",1000,60,190);
   for(int i=0; i < n; ++i) {
      h->Fill(x[i],y[i]);
   }
   h->Fit("gaus","Lr+","E1",60.0,190.0);
}

That was a typo. This “L” option is not fitting the data.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.