data analysis scripts
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

282 lines
11 KiB

  1. //+ Combined (simultaneous) fit of two histogram with separate functions
  2. // and some common parameters
  3. //
  4. // See http://root.cern.ch/phpBB3//viewtopic.php?f=3&t=11740#p50908
  5. // for a modified version working with Fumili or GSLMultiFit
  6. //
  7. // N.B. this macro must be compiled with ACliC
  8. //
  9. //Author: L. Moneta - Dec 2010
  10. #include "Fit/Fitter.h"
  11. #include "Fit/BinData.h"
  12. #include "Fit/Chi2FCN.h"
  13. #include "TH1.h"
  14. #include "TList.h"
  15. #include "Math/WrappedMultiTF1.h"
  16. #include "HFitInterface.h"
  17. #include "TCanvas.h"
  18. #include "TStyle.h"
  19. #include "TGraphErrors.h"
  20. #include "TLegend.h"
  21. #include "TMultiGraph.h"
  22. // definition of shared parameter
  23. // background function
  24. int iparB[3] = { 0, // exp amplitude in B histo
  25. 1, // T_0
  26. 2 // common parameter
  27. };
  28. // signal + background function
  29. int iparSB2[3] = { 0, // amplitude in S+B histo
  30. 3, // T_0
  31. 2, // common parameter
  32. };
  33. int iparSB3[3] = { 0, // amplitude
  34. 4, // T_0
  35. 2, // common parameter
  36. };
  37. int iparSB4[3] = { 0, // amplitude
  38. 5, // T_0
  39. 2, // common parameter
  40. };
  41. struct GlobalChi2 {
  42. GlobalChi2( ROOT::Math::IMultiGenFunction & f1,
  43. ROOT::Math::IMultiGenFunction & f2,
  44. ROOT::Math::IMultiGenFunction & f3,
  45. ROOT::Math::IMultiGenFunction & f4) :
  46. fChi2_1(&f1), fChi2_2(&f2), fChi2_3(&f3), fChi2_4(&f4) {}
  47. // parameter vector is first background (in common 1 and 2)
  48. // and then is signal (only in 2)
  49. double operator() (const double *par) const {
  50. double p1[3];
  51. for (int i = 0; i < 3; ++i) p1[i] = par[iparB[i] ];
  52. double p2[3];
  53. for (int i = 0; i < 3; ++i) p2[i] = par[iparSB2[i] ];
  54. double p3[3];
  55. for (int i = 0; i < 3; ++i) p3[i] = par[iparSB3[i] ];
  56. double p4[3];
  57. for (int i = 0; i < 3; ++i) p4[i] = par[iparSB4[i] ];
  58. return (*fChi2_1)(p1) + (*fChi2_2)(p2) + (*fChi2_3)(p3) + (*fChi2_4)(p4);
  59. }
  60. const ROOT::Math::IMultiGenFunction * fChi2_1;
  61. const ROOT::Math::IMultiGenFunction * fChi2_2;
  62. const ROOT::Math::IMultiGenFunction * fChi2_3;
  63. const ROOT::Math::IMultiGenFunction * fChi2_4;
  64. };
  65. void combinedFit_hit2() {
  66. #if defined(__CINT__) && !defined(__MAKECINT__)
  67. cout << "ERROR: This tutorial can run only using ACliC, you must run it by doing: " << endl;
  68. cout << "\t .x $ROOTSYS/tutorials/fit/combinedFit_hit.C+" << endl;
  69. return;
  70. #endif
  71. TF1 * fB = new TF1("fB","[0]*( (1- 0.5*(TMath::Log(2*0.511E6*x*x/(1-x*x)/[1]) -x*x )/(TMath::Log(2*0.511E6*x*x/(1-x*x)/68.7) -x*x )) / (1+[2]* (1- 0.5*(TMath::Log(2*0.511E6*x*x/(1-x*x)/[1]) -x*x )/(TMath::Log(2*0.511E6*x*x/(1-x*x)/68.7) -x*x ))*1.0*1.65901*TMath::Power(x,-1.7218)) + 0.5*(TMath::Log(2*0.511E6*x*x/(1-x*x)/[1]) -x*x )/(TMath::Log(2*0.511E6*x*x/(1-x*x)/68.7) -x*x ) ) ",0,1);//energy units in keV
  72. //TF1 * fB = new TF1("fB","[0]*( (1-[1]) / (1+[2]* (1- [1])*1.0*1.65901*TMath::Power(x,-1.7218)) +[1] ) ",0,1);
  73. TF1 * fSB2 = new TF1("fSB2","[0]*( (1- 0.5*(TMath::Log(2*0.511E6*x*x/(1-x*x)/[1]) -x*x )/(TMath::Log(2*0.511E6*x*x/(1-x*x)/68.7) -x*x )) / (1+[2]* (1- 0.5*(TMath::Log(2*0.511E6*x*x/(1-x*x)/[1]) -x*x )/(TMath::Log(2*0.511E6*x*x/(1-x*x)/68.7) -x*x ))*4.0*1.65901*TMath::Power(x,-1.7218)) + 0.5*(TMath::Log(2*0.511E6*x*x/(1-x*x)/[1]) -x*x )/(TMath::Log(2*0.511E6*x*x/(1-x*x)/68.7) -x*x ) ) ",0,1);//energy units in keV
  74. //TF1 * fSB2 = new TF1("fSB2","[0]*( (1-[1]) / (1+[2]* (1- [1])*4.0*1.65901*TMath::Power(x,-1.7218)) +[1] ) ",0,1);
  75. TF1 * fSB3 = new TF1("fSB3","[0]*( (1- 0.5*(TMath::Log(2*0.511E6*x*x/(1-x*x)/[1]) -x*x )/(TMath::Log(2*0.511E6*x*x/(1-x*x)/68.7) -x*x )) / (1+[2]* (1- 0.5*(TMath::Log(2*0.511E6*x*x/(1-x*x)/[1]) -x*x )/(TMath::Log(2*0.511E6*x*x/(1-x*x)/68.7) -x*x ))*36.0*1.65901*TMath::Power(x,-1.7218)) + 0.5*(TMath::Log(2*0.511E6*x*x/(1-x*x)/[1]) -x*x )/(TMath::Log(2*0.511E6*x*x/(1-x*x)/68.7) -x*x ) ) ",0,1);//energy units in keV
  76. //TF1 * fSB3 = new TF1("fSB3","[0]*( (1-[1]) / (1+[2]* (1- [1])*36.0*1.65901*TMath::Power(x,-1.7218)) +[1] ) ",0,1);
  77. TF1 * fSB4 = new TF1("fSB4","[0]*( (1- 0.5*(TMath::Log(2*0.511E6*x*x/(1-x*x)/[1]) -x*x )/(TMath::Log(2*0.511E6*x*x/(1-x*x)/68.7) -x*x )) / (1+[2]* (1- 0.5*(TMath::Log(2*0.511E6*x*x/(1-x*x)/[1]) -x*x )/(TMath::Log(2*0.511E6*x*x/(1-x*x)/68.7) -x*x ))*64.0*1.65901*TMath::Power(x,-1.7218)) + 0.5*(TMath::Log(2*0.511E6*x*x/(1-x*x)/[1]) -x*x )/(TMath::Log(2*0.511E6*x*x/(1-x*x)/68.7) -x*x ) ) ",0,1); //energy units in keV
  78. // TF1 * fSB4 = new TF1("fSB4","[0]*( (1-[1]) / (1+[2]* (1- [1])*64.0*1.65901*TMath::Power(x,-1.7218)) +[1] ) ",0,1);
  79. TGraphErrors * hB = new TGraphErrors("energylist_p_bpmbeta1.txt","%lg %lg %lg");
  80. TGraphErrors * hSB2 = new TGraphErrors("energylist_he_bpmbeta1.txt","%lg %lg %lg");
  81. TGraphErrors * hSB3 = new TGraphErrors("energylist_c_bpmbeta1.txt","%lg %lg %lg");
  82. TGraphErrors * hSB4 = new TGraphErrors("energylist_o_bpmbeta1.txt","%lg %lg %lg");
  83. TGraphErrors * hB_2 = new TGraphErrors("energylist_p_bpmbeta1.txt","%lg %lg %lg");
  84. TGraphErrors * hSB2_2 = new TGraphErrors("energylist_he_bpmbeta1.txt","%lg %lg %lg");
  85. TGraphErrors * hSB3_2 = new TGraphErrors("energylist_c_bpmbeta1.txt","%lg %lg %lg");
  86. TGraphErrors * hSB4_2 = new TGraphErrors("energylist_o_bpmbeta1.txt","%lg %lg %lg");
  87. // perform now global fit
  88. // TF1 * fSB2 = new TF1("fSB2","expo + gaus(2)",0,100);
  89. ROOT::Math::WrappedMultiTF1 wfB(*fB,1);
  90. ROOT::Math::WrappedMultiTF1 wfSB2(*fSB2,1);
  91. ROOT::Math::WrappedMultiTF1 wfSB3(*fSB3,1);
  92. ROOT::Math::WrappedMultiTF1 wfSB4(*fSB4,1);
  93. ROOT::Fit::DataOptions opt;
  94. ROOT::Fit::DataRange rangeB;
  95. // set the data range
  96. rangeB.SetRange(0.3,0.59);
  97. ROOT::Fit::BinData dataB(opt,rangeB);
  98. ROOT::Fit::FillData(dataB, hB);
  99. ROOT::Fit::DataRange rangeSB2;
  100. rangeSB2.SetRange(0.34,0.59);
  101. ROOT::Fit::BinData dataSB2(opt,rangeSB2);
  102. ROOT::Fit::FillData(dataSB2, hSB2);
  103. ROOT::Fit::DataRange rangeSB3;
  104. rangeSB3.SetRange(0.4,0.73);
  105. ROOT::Fit::BinData dataSB3(opt,rangeSB3);
  106. ROOT::Fit::FillData(dataSB3, hSB3);
  107. ROOT::Fit::DataRange rangeSB4;
  108. rangeSB4.SetRange(0.43,0.73);
  109. ROOT::Fit::BinData dataSB4(opt,rangeSB4);
  110. ROOT::Fit::FillData(dataSB4, hSB4);
  111. ROOT::Fit::Chi2Function chi2_B(dataB, wfB);
  112. ROOT::Fit::Chi2Function chi2_SB2(dataSB2, wfSB2);
  113. ROOT::Fit::Chi2Function chi2_SB3(dataSB3, wfSB3);
  114. ROOT::Fit::Chi2Function chi2_SB4(dataSB4, wfSB4);
  115. GlobalChi2 globalChi2(chi2_B, chi2_SB2, chi2_SB3, chi2_SB4);
  116. ROOT::Fit::Fitter fitter;
  117. const int Npar = 6;
  118. double par0[Npar] = {1.4,1E5, 0.005, 1e4,1e4,1e4};
  119. // double par0[Npar] = {1.4,0.000007,0.15,1.4, 0.00250,2, 0.005,2, 0.007};
  120. // create before the parameter settings in order to fix or set range on them
  121. fitter.Config().SetParamsSettings(Npar,par0);
  122. // fix 5-th parameter
  123. // fitter.Config().ParSettings(1).Fix();
  124. // set limits on the third and 4-th parameter
  125. fitter.Config().ParSettings(0).SetLimits(0,5);
  126. fitter.Config().ParSettings(2).SetLimits(0,1.0);
  127. fitter.Config().ParSettings(1).SetLimits(0,2E9);
  128. fitter.Config().ParSettings(3).SetLimits(0,2E6);//1
  129. fitter.Config().ParSettings(4).SetLimits(0,2E6);//10
  130. fitter.Config().ParSettings(5).SetLimits(0,2E6);//10
  131. // fitter.Config().ParSettings(4).SetStepSize(2);
  132. // fitter.Config().ParSettings(1).SetStepSize(0.1);
  133. // fitter.Config().ParSettings(6).SetStepSize(10);
  134. // fitter.Config().ParSettings(8).SetStepSize(10);
  135. fitter.Config().MinimizerOptions().SetPrintLevel(0);
  136. fitter.Config().SetMinimizer("Minuit2","Migrad"); //Minuit2
  137. //fitter.Config().SetNormErrors(true);
  138. // fit FCN function directly
  139. // (specify optionally data size and flag to indicate that is a chi2 fit)
  140. fitter.FitFCN(Npar,globalChi2,0,dataB.Size()+dataSB2.Size()+dataSB3.Size()+dataSB4.Size(),true);
  141. ROOT::Fit::FitResult result = fitter.Result();
  142. result.Print(std::cout, true);
  143. //result.PrintCovMatrix(std::cout);
  144. cout << "FitResult.Status() = " << result.Status() << endl;
  145. TCanvas * c1 = new TCanvas("Simfit","Simultaneous fit");
  146. // c1->Divide(2,2);
  147. // c1->cd(1);
  148. // gStyle->SetOptFit(1111);
  149. fB->SetFitResult( result, iparB);
  150. fB->SetRange(rangeB().first, rangeB().second);
  151. fB->SetLineColor(kRed);
  152. hB->GetListOfFunctions()->Add(fB);
  153. //hB->Draw("AP");
  154. // c1->cd(2);
  155. fSB2->SetFitResult( result, iparSB2);
  156. fSB2->SetRange(rangeSB2().first, rangeSB2().second);
  157. fSB2->SetLineColor(kRed);
  158. hSB2->GetListOfFunctions()->Add(fSB2);
  159. // hSB2->Draw("AP");
  160. // c1->cd(3);
  161. fSB3->SetFitResult( result, iparSB3);
  162. fSB3->SetRange(rangeSB3().first, rangeSB3().second);
  163. fSB3->SetLineColor(kRed);
  164. hSB3->GetListOfFunctions()->Add(fSB3);
  165. // hSB3->Draw("AP");
  166. // c1->cd(4);
  167. fSB4->SetFitResult( result, iparSB4);
  168. fSB4->SetRange(rangeSB4().first, rangeSB4().second);
  169. fSB4->SetLineColor(kRed);
  170. hSB4->GetListOfFunctions()->Add(fSB4);
  171. // hSB4->Draw("AP");
  172. TMultiGraph * mg_e9 = new TMultiGraph();
  173. mg_e9->Add(hB,"p"); hB->SetMarkerStyle(20);
  174. mg_e9->Add(hSB2,"p"); hSB2->SetMarkerStyle(21);
  175. mg_e9->Add(hSB3,"p"); hSB3->SetMarkerStyle(22);
  176. mg_e9->Add(hSB4,"p"); hSB4->SetMarkerStyle(23);
  177. mg_e9->Draw("a"); //must draw first before labeling axes
  178. mg_e9->SetTitle(" ");
  179. mg_e9->GetXaxis()->SetTitle("Lorentz #beta");
  180. mg_e9->GetYaxis()->SetTitle("dA/dE / (10^{-6} a.u./(Mev#upointion#upoints^{-1})");
  181. mg_e9->SetMinimum(0.5);
  182. mg_e9->SetMaximum(1.4);
  183. TF1 * tf1_birk1 = new TF1("tf1_birk1","[0]*(1/(1+[1]*1.65901*TMath::Power(x,-1.7218)))",0.3,0.59);
  184. TF1 * tf1_birk2 = new TF1("tf1_birk2","[0]*(1/(1+[1]*4*1.65901*TMath::Power(x,-1.7218)))",0.34,0.59);
  185. TF1 * tf1_birk3 = new TF1("tf1_birk3","[0]*(1/(1+[1]*36*1.65901*TMath::Power(x,-1.7218)))",0.4,0.73);
  186. TF1 * tf1_birk4 = new TF1("tf1_birk4","[0]*(1/(1+[1]*64*1.65901*TMath::Power(x,-1.7218)))",0.43,0.73);
  187. tf1_birk1->SetParameters(1.46123,0.0177651);tf1_birk1->SetLineStyle(2);tf1_birk1->SetLineColor(kBlack);
  188. tf1_birk2->SetParameters(1.46123,0.00682331);tf1_birk2->SetLineStyle(2);tf1_birk2->SetLineColor(kBlack);
  189. tf1_birk3->SetParameters(1.46123,0.00389243);tf1_birk3->SetLineStyle(2);tf1_birk3->SetLineColor(kBlack);
  190. tf1_birk4->SetParameters(1.46123,0.00310792);tf1_birk4->SetLineStyle(2);tf1_birk4->SetLineColor(kBlack);
  191. // hB_2->Fit(tf1_birk1);
  192. // hSB2_2->Fit(tf1_birk2);
  193. //hSB3_2->Fit(tf1_birk3);
  194. //hSB4_2->Fit(tf1_birk4);
  195. tf1_birk1->Draw("same");
  196. tf1_birk2->Draw("same");
  197. tf1_birk3->Draw("same");
  198. tf1_birk4->Draw("same");
  199. TLegend * mylegende9 = new TLegend(0.65,0.65,0.9,0.9);
  200. mylegende9->SetFillColor(0); // white background
  201. mylegende9->SetTextFont(22);
  202. mylegende9->SetBorderSize(0); // get rid of the box
  203. mylegende9->SetTextSize(0.035); // set text size
  204. mylegende9->AddEntry(hB,"Protons","p"); // options: p,l,f
  205. mylegende9->AddEntry(hSB2,"Helium","p"); // options: p,l,f
  206. mylegende9->AddEntry(hSB3,"Carbon","p"); // options: p,l,f
  207. mylegende9->AddEntry(hSB4,"Oxygen","p"); // options: p,l,f
  208. mylegende9->AddEntry(fB,"BVT Fit","l"); // options: p,l,f
  209. mylegende9->AddEntry(tf1_birk1,"Birks Fit","l"); // options: p,l,f
  210. // mylegende9->AddEntry(tf1_pow1,"[1]#upointpow(#beta,[2])}","l"); // options: p,l,f
  211. mylegende9->Draw();
  212. gPad->Modified();
  213. c1->SaveAs("figs/bvt_beta_combinedfit.pdf");
  214. c1->SaveAs("figs/bvt_beta_combinedfit.png");
  215. c1->SaveAs("figs/bvt_beta_combinedfit.C");
  216. }