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.

285 lines
12 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. #include <filesystem>
  33. const int nBins = 70;
  34. const Double_t MASS_HIST_MIN = 5100.;
  35. const Double_t MASS_HIST_MAX = 6000.;
  36. const Double_t K_MASS = 493.677;
  37. const Double_t PSI2S_MASS = 3686.093;
  38. const Double_t JPSI_MASS = 3096.9;
  39. const Double_t KSTAR_MASS = 891.76;
  40. const int PID_KAON = 321;
  41. const int PID_PION = 211;
  42. const int PID_MUON = 13;
  43. const char *NAME = "B0ToHpHmMuMu_Fullstream_wobkgcat";
  44. const char *X_AXIS = "m(#pi^{+}_{#rightarrow K^{+}}#pi^{-}#mu^{+}#mu^{-})";
  45. const bool USE_HYP_REPLACE = false;
  46. const char *B_NAME = "B0";
  47. TString quickformat_name(const char *format)
  48. {
  49. return TString::Format(format, NAME);
  50. };
  51. void CreateRooFitAndDraw(TH1D *hist, double total_entries, double selected_entries);
  52. int simulation_fullstream_B02KpPimMuMu()
  53. {
  54. TChain *data_chain = new TChain("B0ToHpHmMuMu_noPID/DecayTree");
  55. data_chain->Add("/auto/data/pfeiffer/inclusive_detached_dilepton/MC/rd_btoxll_simulation_fullstream_v0r0p6671378_B0ToKpPimMuMu_11144002_magdown.root");
  56. /*
  57. rd_btoxll_simulation_fullstream_v0r0p6671378_B0ToKpPimMuMu_11144002_magdown.root
  58. rd_btoxll_simulation_fullstream_v0r0p6671378_BuToKpMuMu_12143001_magdown.root
  59. rd_btoxll_simulation_turbo_v0r0p6657752_B0ToKpPimMuMu_11144002_magdown.root
  60. rd_btoxll_simulation_turbo_v0r0p6657752_BuToKpMuMu_12143001_magdown.root
  61. */
  62. Int_t B_BKGCAT;
  63. data_chain->SetBranchAddress(TString::Format("%s_BKGCAT", B_NAME).Data(), &B_BKGCAT);
  64. Float_t L1_PX, L1_PY, L1_PZ, L1_ENERGY,
  65. L2_PX, L2_PY, L2_PZ, L2_ENERGY,
  66. Hp_PX, Hp_PY, Hp_PZ, Hp_ENERGY,
  67. Hm_PX, Hm_PY, Hm_PZ, Hm_ENERGY;
  68. Double_t B_M;
  69. Int_t L1_TRUEID, L2_TRUEID, Hp_TRUEID, Hm_TRUEID;
  70. data_chain->SetBranchAddress("L1_TRUEID", &L1_TRUEID);
  71. data_chain->SetBranchAddress("L2_TRUEID", &L2_TRUEID);
  72. data_chain->SetBranchAddress("L1_PX", &L1_PX);
  73. data_chain->SetBranchAddress("L1_PY", &L1_PY);
  74. data_chain->SetBranchAddress("L1_PZ", &L1_PZ);
  75. data_chain->SetBranchAddress("L1_ENERGY", &L1_ENERGY);
  76. data_chain->SetBranchAddress("L2_PX", &L2_PX);
  77. data_chain->SetBranchAddress("L2_PY", &L2_PY);
  78. data_chain->SetBranchAddress("L2_PZ", &L2_PZ);
  79. data_chain->SetBranchAddress("L2_ENERGY", &L2_ENERGY);
  80. data_chain->SetBranchAddress("Hp_PX", &Hp_PX);
  81. data_chain->SetBranchAddress("Hp_PY", &Hp_PY);
  82. data_chain->SetBranchAddress("Hp_PZ", &Hp_PZ);
  83. data_chain->SetBranchAddress("Hp_ENERGY", &Hp_ENERGY);
  84. data_chain->SetBranchAddress("Hm_PX", &Hm_PX);
  85. data_chain->SetBranchAddress("Hm_PY", &Hm_PY);
  86. data_chain->SetBranchAddress("Hm_PZ", &Hm_PZ);
  87. data_chain->SetBranchAddress("Hm_ENERGY", &Hm_ENERGY);
  88. data_chain->SetBranchAddress("Hp_TRUEID", &Hp_TRUEID);
  89. data_chain->SetBranchAddress("Hm_TRUEID", &Hm_TRUEID);
  90. TH1D *h1_B_M = new TH1D("h1_B_M", "B Mass", nBins, MASS_HIST_MIN, MASS_HIST_MAX);
  91. TH1D *h1_Dimuon_M = new TH1D("h1_Dimuon_M", "#mu #mu Mass", nBins, JPSI_MASS - 200., JPSI_MASS + 200.);
  92. TH1D *h1_B_M_trueid = new TH1D("h1_B_M_trueid", "B Mass, TrueID matched", nBins, MASS_HIST_MIN, MASS_HIST_MAX);
  93. TH1I *h1_B_BKGCAT = new TH1I("h1_B_BKGCAT", "B Background Category", nBins, 0, 120);
  94. TH2D *h2_B_M_vs_B_BKGCAT = new TH2D("h2_B_M_vs_B_BKGCAT", "B Mass vs Background Category", nBins, MASS_HIST_MIN, MASS_HIST_MAX, nBins, 0, 120);
  95. h1_B_M->GetXaxis()->SetTitle(X_AXIS);
  96. h1_B_M_trueid->GetXaxis()->SetTitle(X_AXIS);
  97. unsigned int entries = data_chain->GetEntries();
  98. unsigned int selected_entries = 0;
  99. for (size_t i = 0; i < entries; i++)
  100. {
  101. data_chain->GetEntry(i);
  102. // std::cout << B_BKGCAT << std::endl;
  103. Double_t reconstructed_B_Mass = 0;
  104. TLorentzVector l1_4v(L1_PX, L1_PY, L1_PZ, L1_ENERGY);
  105. TLorentzVector l2_4v(L2_PX, L2_PY, L2_PZ, L2_ENERGY);
  106. if (B_BKGCAT == 30 && TMath::Abs(L1_TRUEID) == PID_MUON && L2_TRUEID == -L1_TRUEID)
  107. {
  108. if (TMath::Abs(Hp_TRUEID) == PID_KAON && TMath::Abs(Hm_TRUEID) == PID_PION)
  109. {
  110. TVector3 K_momentum(Hp_PX, Hp_PY, Hp_PZ);
  111. double K_energy = TMath::Sqrt(TMath::Sq(K_MASS) + K_momentum.Mag2());
  112. TLorentzVector K_4v(K_momentum, K_energy);
  113. TLorentzVector Pi_4v(Hm_PX, Hm_PY, Hm_PZ, Hm_ENERGY);
  114. reconstructed_B_Mass = (K_4v + l1_4v + l2_4v + Pi_4v).M();
  115. selected_entries++;
  116. }
  117. else if (TMath::Abs(Hp_TRUEID) == PID_PION && TMath::Abs(Hm_TRUEID) == PID_KAON)
  118. {
  119. TVector3 K_momentum(Hm_PX, Hm_PY, Hm_PZ);
  120. double K_energy = TMath::Sqrt(TMath::Sq(K_MASS) + K_momentum.Mag2());
  121. TLorentzVector K_4v(K_momentum, K_energy);
  122. TLorentzVector Pi_4v(Hp_PX, Hp_PY, Hp_PZ, Hp_ENERGY);
  123. reconstructed_B_Mass = (K_4v + l1_4v + l2_4v + Pi_4v).M();
  124. selected_entries++;
  125. }
  126. }
  127. TLorentzVector dimuon = l1_4v + l2_4v;
  128. if (reconstructed_B_Mass != 0)
  129. {
  130. h1_Dimuon_M->Fill(dimuon.M());
  131. h1_B_M_trueid->Fill(reconstructed_B_Mass);
  132. h1_B_M->Fill(reconstructed_B_Mass);
  133. h1_B_BKGCAT->Fill(B_BKGCAT);
  134. h2_B_M_vs_B_BKGCAT->Fill(reconstructed_B_Mass, B_BKGCAT);
  135. }
  136. if ((i + 1) % 10000 == 0 || i + 1 == entries)
  137. {
  138. std::cout << "["
  139. << NAME
  140. << "] Processed event: " << i + 1 << " (" << TString::Format("%.2f", ((double)(i + 1) / (double)entries) * 100.) << "%), "
  141. << "Current Efficiency: " << TString::Format("%.2f%%, with %d", ((double)selected_entries / (double)entries) * 100., selected_entries)
  142. << std::endl;
  143. }
  144. }
  145. h1_B_M->SetLineColor(kBlue);
  146. h1_B_M_trueid->SetLineColor(kMagenta + 2);
  147. h1_B_M_trueid->SetFillColor(kMagenta + 2);
  148. h1_B_M_trueid->SetFillStyle(3244);
  149. h1_B_BKGCAT->SetLineColor(kBlue);
  150. h1_B_BKGCAT->SetFillColor(kBlue);
  151. h1_B_BKGCAT->SetFillStyle(3351);
  152. std::filesystem::create_directory(TString::Format("images/sim/%s", NAME).Data());
  153. TCanvas *c1 = new TCanvas("c1", "c1", 0, 0, 800, 600);
  154. h1_B_M->SetStats(0);
  155. h1_B_M_trueid->SetStats(0);
  156. h1_B_M->Draw();
  157. h1_B_M_trueid->Draw("SAME");
  158. c1->BuildLegend(0.58, 0.65, 0.85, 0.87);
  159. c1->Draw();
  160. c1->SaveAs(quickformat_name("images/sim/%s/h1_B_M.pdf"));
  161. TCanvas *c2 = new TCanvas("c2", "c2", 0, 0, 800, 600);
  162. h1_B_BKGCAT->SetStats(0);
  163. h1_B_BKGCAT->Draw();
  164. c2->BuildLegend(0.58, 0.77, 0.85, 0.87);
  165. c2->Draw();
  166. c2->SaveAs(quickformat_name("images/sim/%s/h1_B_BKGCAT.pdf"));
  167. TCanvas *c3 = new TCanvas("c3", "c3", 0, 0, 800, 600);
  168. h2_B_M_vs_B_BKGCAT->SetStats(0);
  169. h2_B_M_vs_B_BKGCAT->Draw("COLZ");
  170. c3->Draw();
  171. c3->SaveAs(quickformat_name("images/sim/%s/h2_B_M_vs_B_BKGCAT.pdf"));
  172. TCanvas *c4 = new TCanvas("c4", "c4", 0, 0, 800, 600);
  173. h1_Dimuon_M->SetStats(0);
  174. h1_Dimuon_M->Draw();
  175. c4->BuildLegend(0.58, 0.77, 0.85, 0.87);
  176. c4->Draw();
  177. CreateRooFitAndDraw(h1_B_M_trueid, entries, selected_entries);
  178. return 0;
  179. }
  180. void CreateRooFitAndDraw(TH1D *hist, double total_entries, double selected_entries)
  181. {
  182. // std::cout << "### Entries: " << hist->GetEntries() << std::endl;
  183. RooRealVar roo_var_mass("var_mass", "B Mass Variable", MASS_HIST_MIN, MASS_HIST_MAX);
  184. RooDataHist roohist_B_M("roohist_B_M", "B Mass Histogram", roo_var_mass, RooFit::Import(*hist));
  185. RooPlot *roo_frame_mass = roo_var_mass.frame(RooFit::Title(quickformat_name("Simulation of %s")));
  186. roohist_B_M.plotOn(roo_frame_mass, RooFit::Binning(nBins), RooFit::Name("B Mass Distribution"));
  187. roo_frame_mass->GetXaxis()->SetTitle(X_AXIS);
  188. // Crystal Ball for Signal
  189. RooRealVar var_mass_x0("var_mass_x0", "#mu", 5278., 5170., 5500.);
  190. RooRealVar var_mass_sigmaLR("var_mass_sigmaLR", "#sigma_{LR}", 16., 5., 25.);
  191. RooRealVar var_mass_alphaL("var_mass_alphaL", "#alpha_{L}", 2., 0., 4.);
  192. RooRealVar var_mass_nL("var_mass_nL", "n_{L}", 5., 0., 15.);
  193. RooRealVar var_mass_alphaR("var_mass_alphaR", "#alpha_{R}", 2., 0., 4.);
  194. RooRealVar var_mass_nR("var_mass_nR", "n_{R}", 5., 0., 15.);
  195. 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);
  196. // Exponential for Background
  197. // RooRealVar var_mass_bkg_c("var_mass_bkg_c", "Background C", -0.00231049, -0.003, -0.002);
  198. // RooExponential bkg_exp("bkg_exp", "Exp Background", var_mass, var_mass_bkg_c);
  199. // RooRealVar var_mass_nsig(TString::Format("var_%s_nsig", var_id.c_str()), "Mass N Signal", 0., hist->GetEntries());
  200. // RooRealVar var_mass_nbkg(TString::Format("var_%s_nbkg", var_id.c_str()), "Mass N Background", 0., hist->GetEntries());
  201. // TString pdf_name = TString::Format("%s_sigplusbkg", var_id.c_str());
  202. // RooAddPdf sigplusbkg(pdf_name, "Sig and Bkg PDF", RooArgList(sig_cb, bkg_exp), RooArgList(var_mass_nsig, var_mass_nbkg));
  203. RooFitResult *fitres = sig_cb.fitTo(roohist_B_M, RooFit::Save(), RooFit::PrintLevel(1), RooFit::Range("fitting_range"));
  204. sig_cb.plotOn(roo_frame_mass, RooFit::VisualizeError(*fitres, 1), RooFit::FillColor(kOrange + 1), RooFit::FillStyle(3144));
  205. sig_cb.plotOn(roo_frame_mass, RooFit::LineColor(kRed), RooFit::LineStyle(kSolid), RooFit::Range("fitting_range"), RooFit::Name("sig_cb"));
  206. // sigplusbkg.plotOn(roo_frame_mass, RooFit::Components(RooArgSet(bkg_exp)), RooFit::LineColor(kBlue), RooFit::LineStyle(kDashed), RooFit::Range(5170., 5500.));
  207. TCanvas *c = new TCanvas("roofit_c", "roofit_c", 0, 0, 800, 600);
  208. roo_frame_mass->Draw();
  209. TLegend *leg1 = new TLegend(0.58, 0.50, 0.87, 0.87);
  210. // leg1->SetFillColor(kWhite);
  211. leg1->SetLineColor(kWhite);
  212. // leg1->AddEntry(roo_frame_mass->findObject(pdf_name.c_str()), "Signal + Background", "LP");
  213. leg1->AddEntry(roo_frame_mass->findObject("sig_cb"), "Signal", "LP");
  214. // leg1->AddEntry(roo_frame_mass->findObject(name_fit_func_bkg.c_str()), "Background", "LP");
  215. leg1->AddEntry((TObject *)0, TString::Format("%s = %.3f #pm %.3f", var_mass_x0.getTitle().Data(), var_mass_x0.getVal(), var_mass_x0.getError()).Data(), "");
  216. leg1->AddEntry((TObject *)0, TString::Format("%s = %.3f #pm %.3f", var_mass_sigmaLR.getTitle().Data(), var_mass_sigmaLR.getVal(), var_mass_sigmaLR.getError()).Data(), "");
  217. // leg1->AddEntry((TObject *)0, TString::Format("%s = %.8f #pm %.8f", roo_sig_tail.getTitle().Data(), roo_sig_tail.getVal(), roo_sig_tail.getError()).Data(), "");
  218. leg1->AddEntry((TObject *)0, TString::Format("%s = %.3f #pm %.3f", var_mass_alphaL.getTitle().Data(), var_mass_alphaL.getVal(), var_mass_alphaL.getError()).Data(), "");
  219. leg1->AddEntry((TObject *)0, TString::Format("%s = %.3f #pm %.3f", var_mass_nL.getTitle().Data(), var_mass_nL.getVal(), var_mass_nL.getError()).Data(), "");
  220. leg1->AddEntry((TObject *)0, TString::Format("%s = %.3f #pm %.3f", var_mass_alphaR.getTitle().Data(), var_mass_alphaR.getVal(), var_mass_alphaR.getError()).Data(), "");
  221. leg1->AddEntry((TObject *)0, TString::Format("%s = %.3f #pm %.3f", var_mass_nR.getTitle().Data(), var_mass_nR.getVal(), var_mass_nR.getError()).Data(), "");
  222. leg1->AddEntry((TObject *)0, TString::Format("#epsilon = %.2f%%", (selected_entries / total_entries) * 100).Data(), "");
  223. leg1->Draw();
  224. c->Draw();
  225. c->SaveAs(quickformat_name("images/sim/%s/roof_B_M.pdf"));
  226. }