Fitting Multiple Graphs

Hello,

I have a query.

I have multiple data sets, stored in different text files. I would like to open all the text files,
plot all the multiple data sets.

I have defined a fitting routine to be able to fit these graphs. So additionally I want to fit all these plots.

Then, finally I need to plot them all together in a single canvas.

The way I do it for one file is as follows:

//*********************************************************************************************//
cout<<"What Clover #crystal# do you want to analyze"<<endl;
  cin>>counter;
  cout<<"Crystal : "<<counter<<" Will be Analyzed"<<endl;
  
  Int_t npeaks = 12;
  stringstream ss;
  ss.str();
  string s1 = "Clover56Co_";
  string s2 = "_Results.txt";
  
  ss<<s1<<counter<<s2;

  ifstream file;
  file.open(ss.str().c_str());
  // file.ignore(256,'  ');
  for (Int_t i=0;i<npeaks;i++)
  {file>>Energy[i]>>Mean[i]>>Mean_Error[i]>>Area[i]>>Area_Error[i]>>Efficiency[i]>>Efficiency_Error[i];  }
  
  file.close();

 TCanvas *c1 = new TCanvas("Efficiency Exp","Efficiency Exp",640,480);
 
 TMultiGraph *mg = new TMultiGraph();
 mg->SetTitle("Efficiency plots on log-log scale");
 mg->SetName("Eff_Cal");
 mg->GetXaxis()->SetTitle(" Energy (keV)");
 mg->GetYaxis()->SetTitle("Efficiency (%)");

 
 // c1->SetLogx();
 // c1->SetLogy();

 TGraph *gr1 = new TGraphErrors(npeaks, Energy, Efficiency, Energy_Unc, Efficiency_Error);
 gr1->SetName("Eff_Cal");
 gr1->SetTitle("Efficiency  vs Energy");
 gr1->GetXaxis()->SetTitle("Energy (keV)");
 gr1->GetYaxis()->SetTitle("Efficiency (%)");
 gr1->SetMarkerStyle(20);
 gr1->SetMarkerColor(5);
 TGraph *gr2 = new TGraphErrors(npeaks, Energy, Efficiency, Energy_Unc, Efficiency_Error);
 gr2->SetName("Eff_Cal");
 gr2->SetTitle("Efficiency vs Energy");
 gr2->GetXaxis()->SetTitle("Energy (keV)");
 gr2->GetYaxis()->SetTitle("Efficiency (%)");
 gr2->SetMarkerStyle(20);
 gr2->SetMarkerColor(5);
 

 c1->cd();
 TF1 *eff_fit = new TF1("eff_fit",Expo,40,4000,7);
 eff_fit->SetParameter(0,10);
 eff_fit->SetParameter(1,0.01);
 eff_fit->SetParameter(2,0.5);
 eff_fit->SetParameter(3,1);
 eff_fit->SetParameter(4,.2);
 eff_fit->SetParameter(5,-0.001);
 eff_fit->SetParameter(6,1);
     
 eff_fit->SetLineColor(kGreen);
 eff_fit->SetLineWidth(2);
 eff_fit->SetLineStyle(3);
 gr1->Fit("eff_fit","RMQ");
 gr1->Fit("eff_fit","RMQ");
 gr1->Fit("eff_fit","SERM");


 
 //gr1->Draw("AP");

 c1->cd();
 TF1 *eff_fit1 = new TF1("eff_fit1",effit,40,4000,7);
 eff_fit->SetParameter(0,-300);
 eff_fit->SetParameter(1,50);
 eff_fit->SetParameter(2,-20);
 eff_fit->SetParameter(3,3);
 eff_fit->SetParameter(4,3);
 eff_fit->SetParameter(5,-0.01);
 eff_fit->SetParameter(6,10);
 eff_fit1->SetLineColor(kRed);
 eff_fit1->SetLineStyle(3);
 gr2->Fit("eff_fit1","RQM");
 gr2->Fit("eff_fit1","RMQ");
 gr2->Fit("eff_fit1","SERM");

 

 mg->Add(gr1);
 mg->Add(gr2);


 
 TLegend *legend = new TLegend(0.6,0.65,.9,0.85);
 legend->SetHeader("    Fitting functions "); 				// option "C" allows to center the header
 legend->AddEntry(eff_fit,"Exponential Fn","l");
 legend->AddEntry(eff_fit1,"Polynomial Fn","l");
  
 mg->Draw("AP");
 legend->Draw();

//**********************************************************************************************//

Mansi

TMultigraph also has a Fit method. May be that’s what you are looking for ?
https://root.cern/doc/master/classTMultiGraph.html#a1881365d2185e14bbcbdb81defdcc56a

But, if I want to open all files in a loop. How do I store the contents of the file individually and call them using the fitting option in TMultigraph ?

I guess you can:

  • Create a TMultiGraph mg
  • Loop over all the files, take the TGraph you need in each of them
  • Add each TGraph in mg
  • Fit mg

Ok,
Does TGraph, also accept Vectors instead of double/float.
What will be the way to store the contents of the files using one variables looped over all files?
For example each file has - energy and area for different peaks.

Yes see the TGraph documentation.

You can create a TGraph from an ascii file. see also the doc.

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