Problem in fitting (in function!)

Dear @Wile_E_Coyote and @Couet,

Should the peaks coming outside the fit range, be due to the (1) ill-defined fit parameters or (2) ill-defined functional form?

If the above two are correct, then is there a way to get rid of those unwanted peaks outside the fit-range?

I have tried changing the fit-range too, but, it it very difficult to remove those?

Is there any other trick to remove those?

Most probably your function diverges.

Ok, is there any way to get the integral under TGraph2DErrors?

{like what we do for histograms…myhist->Integral(); }

Maybe you should take the integral of your fitted function?

When the function is not uniquely defined…then…!!!

You anyhow go through an “intermediate” histogram so maybe use its integral.

Otherwisse, you would need to create a TF2 from your 2D graph and integrate this new function.
An example of a TF1 created from a 1D graph can be found in: “Integral of TGraph

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.