ROOT Analysis for the Inclusive Detachted Dilepton Trigger Lines
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.

267 lines
11 KiB

  1. #include "TH1D.h"
  2. #include "TH2D.h"
  3. #include "THStack.h"
  4. #include "TGraph.h"
  5. #include "TTree.h"
  6. #include "TChain.h"
  7. #include "TFile.h"
  8. #include "TCanvas.h"
  9. #include "TROOT.h"
  10. #include "TStyle.h"
  11. #include "TColor.h"
  12. #include "TLorentzVector.h"
  13. #include "TRandom3.h"
  14. #include "TLorentzVector.h"
  15. #include "RooDataHist.h"
  16. #include "RooRealVar.h"
  17. #include "RooPlot.h"
  18. #include "RooGaussian.h"
  19. #include "RooExponential.h"
  20. #include "RooRealConstant.h"
  21. #include "RooAddPdf.h"
  22. #include "RooFitResult.h"
  23. #include "RooProduct.h"
  24. #include "RooCrystalBall.h"
  25. #include "RooBreitWigner.h"
  26. #include "RooArgSet.h"
  27. #include "RooFFTConvPdf.h"
  28. #include "RooNovosibirsk.h"
  29. #include <string>
  30. #include <iostream>
  31. #include <cmath>
  32. const int nBins = 70;
  33. const Double_t MASS_HIST_MIN = 5150.;
  34. const Double_t MASS_HIST_MAX = 6000.;
  35. const Double_t PSI2S_MASS = 3686.093;
  36. const Double_t JPSI_MASS = 3096.9;
  37. const char *B0_DECAY = "Hlt2RD_B0ToKpPimMuMu";
  38. const char *Bu_DECAY = "Hlt2RD_BuToKpMuMu";
  39. const char *TITLE = Bu_DECAY;
  40. const char *HIST_TITLE = Bu_DECAY; // "Hlt2RD_BuToKpMuMu";
  41. const char *X_AXIS = "m(K^{+}#mu^{+}#mu^{-})"; // "m(K^{+}#mu^{+}#mu^{-})";
  42. void CreateRooFitAndDraw(TH1D *hist, int fitting_entries);
  43. int analysis_turbo()
  44. {
  45. std::string tree = "";
  46. if (TITLE == B0_DECAY)
  47. {
  48. tree = "Hlt2RD_B0ToKpPimMuMu";
  49. }
  50. else
  51. {
  52. tree = "Hlt2RD_BuToKpMuMu";
  53. }
  54. TChain *data_chain = new TChain(TString::Format("%s/DecayTree", tree.c_str()).Data());
  55. data_chain->Add("/auto/data/pfeiffer/inclusive_detached_dilepton/data_samples/Collision23_Beam6800GeV-VeloClosed-MagDown-Excl-UT_RealData_SprucingPass23r1_94000000_RD.root");
  56. Double_t B_M, Jpsi_M, B0_M, Kst_M;
  57. Bool_t Hlt1TrackMVADecision, Hlt1TwoTrackMVADecision;
  58. UInt_t runNumber;
  59. ULong64_t eventNumber;
  60. data_chain->SetBranchAddress("RUNNUMBER", &runNumber);
  61. data_chain->SetBranchAddress("EVENTNUMBER", &eventNumber);
  62. if (TITLE == B0_DECAY)
  63. {
  64. data_chain->SetBranchAddress("B0_M", &B0_M);
  65. data_chain->SetBranchAddress("Kst0_M", &Kst_M);
  66. }
  67. else
  68. {
  69. data_chain->SetBranchAddress("B_M", &B_M);
  70. }
  71. data_chain->SetBranchAddress("Jpsi_M", &Jpsi_M);
  72. data_chain->SetBranchAddress("Hlt1TrackMVADecision", &Hlt1TrackMVADecision);
  73. data_chain->SetBranchAddress("Hlt1TwoTrackMVADecision", &Hlt1TwoTrackMVADecision);
  74. TH1D *h1_B_M_jpsi = new TH1D("h1_B_M_jpsi", TString::Format("%s (J/#psi)", HIST_TITLE), nBins, MASS_HIST_MIN, MASS_HIST_MAX);
  75. TH1D *h1_B_M_psi2s = new TH1D("h1_B_M_psi2s", TString::Format("%s (#psi(2S))", HIST_TITLE), nBins, MASS_HIST_MIN, MASS_HIST_MAX);
  76. h1_B_M_jpsi->GetXaxis()->SetTitle(X_AXIS);
  77. h1_B_M_psi2s->GetXaxis()->SetTitle(X_AXIS);
  78. std::map<std::pair<unsigned int, unsigned long>, int> run_event_num_dict;
  79. int fitting_entries = 0;
  80. unsigned int entries = data_chain->GetEntries();
  81. unsigned int selected_entries = 0;
  82. for (unsigned int i = 0; i < entries; i++)
  83. {
  84. data_chain->GetEntry(i);
  85. if (TITLE == B0_DECAY)
  86. {
  87. if ((B0_M > 4500) && (B0_M < 6000) && (TMath::Abs(Kst_M - 895.55) < 50) && ((Hlt1TrackMVADecision) || (Hlt1TwoTrackMVADecision)))
  88. {
  89. // check for unique run/event number
  90. selected_entries++;
  91. auto run_event_pair = std::make_pair((unsigned int)runNumber, (unsigned long)eventNumber);
  92. int &run_event_unique_num = run_event_num_dict[run_event_pair];
  93. if (run_event_unique_num)
  94. {
  95. run_event_unique_num = run_event_unique_num + 1;
  96. std::cout << "R: " << runNumber << " / E: " << eventNumber << " / # " << run_event_unique_num
  97. << ", M = " << B0_M << " MeV"
  98. << std::endl;
  99. continue;
  100. }
  101. else
  102. {
  103. run_event_unique_num = 1;
  104. }
  105. if (TMath::Abs(Jpsi_M - JPSI_MASS) < 100)
  106. {
  107. h1_B_M_jpsi->Fill(B0_M);
  108. }
  109. else if (TMath::Abs(Jpsi_M - PSI2S_MASS) < 100)
  110. {
  111. h1_B_M_psi2s->Fill(B0_M);
  112. }
  113. }
  114. }
  115. else
  116. {
  117. if ((B_M > 4500.) && (B_M < 6000.) && ((Hlt1TrackMVADecision) || (Hlt1TwoTrackMVADecision)))
  118. {
  119. // check for unique run/event number
  120. selected_entries++;
  121. auto run_event_pair = std::make_pair((unsigned int)runNumber, (unsigned long)eventNumber);
  122. int &run_event_unique_num = run_event_num_dict[run_event_pair];
  123. if (run_event_unique_num)
  124. {
  125. run_event_unique_num = run_event_unique_num + 1;
  126. std::cout << "R: " << runNumber << " / E: " << eventNumber << " / # " << run_event_unique_num
  127. << ", M = " << B_M << " MeV"
  128. << std::endl;
  129. continue;
  130. }
  131. else
  132. {
  133. run_event_unique_num = 1;
  134. }
  135. if (TMath::Abs(Jpsi_M - JPSI_MASS) < 100)
  136. {
  137. h1_B_M_jpsi->Fill(B_M);
  138. }
  139. else if (TMath::Abs(Jpsi_M - PSI2S_MASS) < 100)
  140. {
  141. h1_B_M_psi2s->Fill(B_M);
  142. }
  143. }
  144. }
  145. if ((i + 1) % 10000 == 0 || i + 1 == entries)
  146. {
  147. std::cout << "["
  148. << TITLE
  149. << "] Processed event: " << i + 1 << " (" << TString::Format("%.2f", ((double)(i + 1) / (double)entries) * 100.) << "%)" << std::endl;
  150. }
  151. }
  152. std::cout << "#### " << run_event_num_dict.size() << " out of " << selected_entries << " unique ("
  153. << TString::Format("%.2f", ((double)(run_event_num_dict.size()) / (double)selected_entries) * 100.) << "%)." << std::endl;
  154. TCanvas *c1 = new TCanvas("c1", "c1", 0, 0, 1400, 600);
  155. c1->Divide(2, 1);
  156. c1->cd(1);
  157. h1_B_M_jpsi->Draw();
  158. c1->cd(2);
  159. h1_B_M_psi2s->Draw();
  160. c1->Draw();
  161. c1->SaveAs(TString::Format("images/root_hist_%s_bmass.pdf", TITLE).Data());
  162. CreateRooFitAndDraw(h1_B_M_jpsi, fitting_entries);
  163. return 0;
  164. }
  165. void CreateRooFitAndDraw(TH1D *hist, int fitting_entries)
  166. {
  167. RooRealVar roo_var_mass("var_mass", "B Mass Variable", MASS_HIST_MIN, MASS_HIST_MAX);
  168. roo_var_mass.setRange("fitting_range", MASS_HIST_MIN, MASS_HIST_MAX);
  169. RooDataHist roohist_B_M("roohist_B_M", "B Mass Histogram", roo_var_mass, RooFit::Import(*hist));
  170. RooPlot *roo_frame_mass = roo_var_mass.frame(RooFit::Title(HIST_TITLE));
  171. roohist_B_M.plotOn(roo_frame_mass, RooFit::Binning(nBins), RooFit::Name("B Mass Distribution"));
  172. roo_frame_mass->GetXaxis()->SetTitle(X_AXIS);
  173. // Crystal Ball for Signal
  174. RooRealVar var_mass_x0("var_mass_x0", "#mu", 5278., 5170., 5500.);
  175. RooRealVar var_mass_sigmaLR("var_mass_sigmaLR", "#sigma_{LR}", 16., 5., 40.);
  176. // Same Variables for Left and Right Tail
  177. // RooRealVar var_mass_alphaL("var_mass_alphaL", "#alpha_{L}", 2., 0., 4.);
  178. // RooRealVar var_mass_nL("var_mass_nL", "n_{L}", 5., 0., 15.);
  179. // RooRealVar var_mass_alphaR("var_mass_alphaR", "#alpha_{R}", 2., 0., 4.);
  180. // RooRealVar var_mass_nR("var_mass_nR", "n_{R}", 5., 0., 15.);
  181. // B+/-
  182. auto var_mass_alphaL = RooRealConstant::value(1.912);
  183. auto var_mass_nL = RooRealConstant::value(1.125);
  184. auto var_mass_alphaR = RooRealConstant::value(2.447);
  185. auto var_mass_nR = RooRealConstant::value(1.491);
  186. // B0
  187. // auto var_mass_alphaL = RooRealConstant::value(1.948);
  188. // auto var_mass_nL = RooRealConstant::value(0.618);
  189. // auto var_mass_alphaR = RooRealConstant::value(2.320);
  190. // auto var_mass_nR = RooRealConstant::value(0.473);
  191. RooCrystalBall sig_cb("sig_cb", "Signal Crystal Ball", roo_var_mass, var_mass_x0, var_mass_sigmaLR, var_mass_alphaL, var_mass_nL, var_mass_alphaR, var_mass_nR);
  192. // Exponential for Background
  193. RooRealVar var_mass_bkg_c("var_mass_bkg_c", "#lambda", -0.0014, -0.004, -0.000);
  194. RooExponential bkg_exp("bkg_exp", "Exp Background", roo_var_mass, var_mass_bkg_c);
  195. RooRealVar var_mass_nsig("nsig", "Mass N Signal", 0., hist->GetEntries());
  196. RooRealVar var_mass_nbkg("nbkg", "Mass N Background", 0., hist->GetEntries());
  197. // TString pdf_name = TString::Format("%s_sigplusbkg", var_id.c_str());
  198. RooAddPdf sigplusbkg("sigplusbkg", "Sig and Bkg PDF", RooArgList(sig_cb, bkg_exp), RooArgList(var_mass_nsig, var_mass_nbkg));
  199. // RooFitResult *fitres = sigplusbkg.fitTo(roohist_B_M, RooFit::Save(), RooFit::PrintLevel(1), RooFit::Range("fitting_range"));
  200. // sigplusbkg.plotOn(roo_frame_mass, RooFit::VisualizeError(*fitres, 1), RooFit::FillColor(kYellow -7), RooFit::FillStyle(3144));
  201. // sigplusbkg.plotOn(roo_frame_mass, RooFit::LineColor(kRed), RooFit::LineStyle(kSolid), RooFit::Range("fitting_range"), RooFit::Name("sigplusbkg"));
  202. // sigplusbkg.plotOn(roo_frame_mass, RooFit::Components(RooArgSet(bkg_exp)), RooFit::LineColor(kBlue - 7), RooFit::LineStyle(kDashed), RooFit::Range("fitting_range"), RooFit::Name("bkg_exp"));
  203. // sigplusbkg.plotOn(roo_frame_mass, RooFit::Components(RooArgSet(sig_cb)), RooFit::LineColor(kRed - 7), RooFit::LineStyle(kDashed), RooFit::Range("fitting_range"), RooFit::Name("sig_cb"));
  204. TCanvas *c = new TCanvas("roofit_c", "roofit_c", 0, 0, 800, 600);
  205. roo_frame_mass->Draw();
  206. TLegend *leg1 = new TLegend(0.50, 0.58, 0.87, 0.89);
  207. leg1->SetFillColor(kWhite);
  208. leg1->SetLineColor(kBlack);
  209. // leg1->AddEntry(roo_frame_mass->findObject("sigplusbkg"), "Signal + Background", "LP");
  210. // leg1->AddEntry(roo_frame_mass->findObject("sig_cb"), "Signal", "LP");
  211. // leg1->AddEntry(roo_frame_mass->findObject("bkg_exp"), "Background", "LP");
  212. // leg1->AddEntry((TObject *)0, "", "");
  213. // leg1->AddEntry((TObject *)0, TString::Format("%s = %.3f #pm %.3f", var_mass_x0.getTitle().Data(), var_mass_x0.getVal(), var_mass_x0.getError()).Data(), "");
  214. // leg1->AddEntry((TObject *)0, TString::Format("%s = %.3f #pm %.3f", var_mass_sigmaLR.getTitle().Data(), var_mass_sigmaLR.getVal(), var_mass_sigmaLR.getError()).Data(), "");
  215. // leg1->AddEntry((TObject *)0, TString::Format("%s = %.6f #pm %.6f", var_mass_bkg_c.getTitle().Data(), var_mass_bkg_c.getVal(), var_mass_bkg_c.getError()).Data(), "");
  216. // leg1->AddEntry((TObject *)0, TString::Format("%s = %.3f #pm %.3f", "S", var_mass_nsig.getVal(), var_mass_nsig.getError()).Data(), "");
  217. // leg1->AddEntry((TObject *)0, TString::Format("%s = %.3f #pm %.3f", "B", var_mass_nbkg.getVal(), var_mass_nbkg.getError()).Data(), "");
  218. // leg1->AddEntry((TObject *)0, TString::Format("Entries: %.2f", var_mass_nsig.getVal() + var_mass_nbkg.getVal()), "");
  219. leg1->AddEntry((TObject *)0, TString::Format("Entries: %.0f", roohist_B_M.sumEntries()), "");
  220. leg1->Draw();
  221. c->Draw();
  222. c->SaveAs(TString::Format("images/data/roofit_hist_%s_fitted_bmass.pdf", TITLE).Data());
  223. }