Problem in fitting (in function!)

Actually, what do you mean by “function is not uniquely defined”?

Your fit function is just an ordinary TF2 so you can use its TF2::Integral method.

1 Like

I got your point but I meant that if somebody is not sure about the functional form!

I was trying to do the integral before coming to TGraph2DErrors in your macro but it fails to do so.

Dear @Wile_E_Coyote, @couet

In the following macro, is there any problem? I am getting empty histogram with non-zero entries with TH1D range 1,0,1. But, if I make it 10,0,1, it is plotting, which I do not want. I know that the bin widths are not exactly same. I want to have range 0 to 1 only. And I do not want to use TGraph as there is problem of normalising it. Any idea?

FILE *f1;
double col1[22],col2[22],col3[22];
f1=fopen("data.txt","r"); 
for (int i = 0; i < 22; ++i )
 {
 fscanf(f1,"%lf %lf %lf",&col1[i],&col2[i],&col3[i]);
 }
auto hist0=new TH1D("hist0","hist0",1,0,1);
for (int k=0; k<=22; k++) {
hist0->Fill(col1[k],col2[k]);
int bin = hist0->GetXaxis()->FindBin(col1[k]);
hist0->SetBinError(bin,col3[k]);
}
hist0->Draw("E1");
gPad->Update();
fclose(f1); 

data.txt (890 Bytes)

You created histogram hist0 with 1 bin only…

As I’ve written, I first tried with 10,0,1.

But, The problem with it is, it changes the bin centers. I want the points to be exactly at where the data are. And if I want to keep the bin width to be same as is in original data.
What to do then?

Use TGraphErrors

{
   FILE *f1;
   double col1[22],col2[22],col3[22];
   f1=fopen("data.txt","r");
   for (int i = 0; i < 22; ++i ) {
      fscanf(f1,"%lf %lf %lf",&col1[i],&col2[i],&col3[i]);
   }
   auto g = new TGraphErrors();
   for (int k=0; k<=22; k++) {
      g->SetPoint(k,col1[k],col2[k]);
      g->SetPointError(k,0.,col3[k]);
   }
   g->SetMarkerStyle(20);
   g->Draw("PA");
   fclose(f1);
}

Note: when you have a new question try to make a new post instead of continuously re-use the same.

@couet I don’t really understand what your’re trying to achieve with the long macro.

{
  TGraphErrors *g = new TGraphErrors("data.txt", "%lg %lg %lg");
  g->SetTitle("Some Thing;My X;My Y");
  g->SetMarkerStyle(20); g->Draw("AP");
}
1 Like

I was trying to be closer to the original macro. Of course I know your suggestion. But lets imagine @Sandy wants to manipulate the vectors before filling the graph then the way I did it is better. At least the two solutions are on the table now. Thanks for your input.

1 Like

Thanks. But, can it not be done through TH1D? so, that I can normalise also, which is problematic with TGraph or TGraphErrors…

In the case of histograms the errors bars are drawn at the bin center. So it will never be the exact position. Graph are doing that. If you create the histogram with many bins it should be close though (try 1000).

Also, as you have the vectors, you can “normalise” them as you wish before putting them in the TGraph.

1 Like

The use of 1000, helps. Thanks. I was doing that using TH1D. I normalise my TH1D by 1/hist0->Integral();

But, the same thing if I want to do with your macro, (if I want to manipulate vectors after):

for (int i = 0; i < 22; ++i ) {
fscanf(f1,"%lf %lf %lf",&col1[i],&col2[i],&col3[i]);
}

before filling TGraph…then how to do that?

something like that:

{
   FILE *f1;
   double col1[22],col2[22],col3[22];
   double sum=0;
   f1=fopen("data.txt","r");
   for (int i = 0; i < 22; ++i ) {
      fscanf(f1,"%lf %lf %lf",&col1[i],&col2[i],&col3[i]);
      sum = sum + col2[i];
   }
   auto g = new TGraphErrors();
   for (int k=0; k<=22; k++) {
      g->SetPoint(k,col1[k],col2[k]/sum);
      //g->SetPointError(k,0.,col3[k]/sum);
   }
   g->SetMarkerStyle(20);
   g->Draw("PA");
   fclose(f1);
}

I am not sure how you want to normalise the errors… but that’s the general idea.

1 Like

OK I see. Thanks.

Is there an easy way to make such variable bin width to fixed bin width?

{
  TGraphErrors *g = new TGraphErrors("data.txt", "%lg %lg %lg");
  // g->Sort(); // just a precaution
  Int_t n = g->GetN();
  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", "spectrum;x-value;#counts", n, x);
  for (Int_t i = 0; i < n; i++) {
    h->SetBinContent(i + 1, g->GetY()[i]);
    h->SetBinError(i + 1, g->GetEY()[i]);
  }
  h->ResetStats(); // reset the statistics including the number of entries
  delete [] x; // no longer needed
  delete g; // no longer needed
  h->Draw();
}

Note that the histogram’s “bins’ centers” usually do not coincide with graph’s points “x coordinates”. One can easily see it if one uses:

  // delete g; // no longer needed
  h->Draw();
  g->SetMarkerStyle(20); g->Draw("P");
1 Like

Yeah, that’s what I wanted. Thanks.

Dear @Wile_E_Coyote,
Why h->GetXaxis()->SetRangeUser(0.3,0.7); is not working, in your macro?

The range setting is “bin based”. The value you give are them converted into bins… and your new macro as non equidistant bins. make sure the upper limit of the last bin is 0.7

Is there no other way to customize it?