Obtaining Chi2 value from TGraph


I have a root file containing three fitted histograms. I am using a TGraph to go through each histogram, bin by bin, and calculate the product of the fits for the three histograms for each bin. I then plot the resultant line. I want to calculate the chi2 value of the product of the three fits.

   TH1D *h1 = (TH1D*)File->Get("R_29099_N0A2byN0A0;1");
   TF1 *h1f1 = h1->GetFunction("f1");
   TH1D *h2 = (TH1D*)File->Get("R_29099_N2A4byN2A0");
   TF1 *h2f1 = h2->GetFunction("f1");
   TH1D *h3 = (TH1D*)File->Get("R_29099_TruthbyN4A0");
   TF1 *h3f1 = h3->GetFunction("f1");
   TH1D *h4 = (TH1D*)File->Get("R_29099_RecobyTruth"); 
   TF1 *h4f1 = h4->GetFunction("f1");


TGraph *gr3 = new TGraph (50); //number of points
gr3->SetTitle("M1234"); //set x and y here too

  for (int i=0; i<50; i++){ //Loop over bins

  double x = h1->GetBinCenter(i+1);

  double y1 = h1f1->Eval(x);
  double y2 = h2f1->Eval(x);
  double y3 = h3f1->Eval(x);
  double y4 = h4f1->Eval(x);

  double Graph3 = y1 * y2 * y3 * y4;

   gr3->SetPoint(i, x, Graph3);


  } //closing loop over histogram bins (i)

 TCanvas *c3 = new TCanvas ("c3", "c3", 700, 500);     

I am not sure how to pull the Chi2 value, as I am not wanting to perform a fit on top.

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

What is the purpose of this line ? what is “Function” ?

It was an error, I have since corrected the code, i.e removed the Function(“f1”); part

I think you have use the definition of chi square.
Inside the for cycle you have to evaluate also the histograms content and the histograms error, fo rthe chi2 evaluation

double y_h1 = h1->GetBinContent(i+1);
double err_h1 = h1->GetBinError(i+1);
double y_h4 = h4->GetBinContent(i+1);
double err_h4 = h4->GetBinError(i+1);

compute the useful variables

double y_prod = y_h1* y_h2* y_h3* y_h4;
double err_sum_sq = err_h1* err_h1 + err_h2* err_h2 + err_h3* err_h3 + err_h4* err_h4;


chi2+= (y_prod-Graph3)*(y_prod-Graph3)/err_sum_sq;

This method should be the right one.


1 Like

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