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.

308 lines
13 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 "TLegend.h"
  15. #include "TLatex.h"
  16. #include "RooDataHist.h"
  17. #include "RooRealVar.h"
  18. #include "RooPlot.h"
  19. #include "RooGaussian.h"
  20. #include "RooExponential.h"
  21. #include "RooRealConstant.h"
  22. #include "RooAddPdf.h"
  23. #include "RooFitResult.h"
  24. #include "RooProduct.h"
  25. #include "RooCrystalBall.h"
  26. #include "RooBreitWigner.h"
  27. #include "RooArgSet.h"
  28. #include "RooFFTConvPdf.h"
  29. #include "RooNovosibirsk.h"
  30. #include <string>
  31. #include <iostream>
  32. #include <cmath>
  33. const int nBins = 70;
  34. const Double_t MASS_HIST_MIN = 5100.;
  35. const Double_t MASS_HIST_MAX = 5700.;
  36. const Double_t MASS_HIST_FIT_MIN = 5100.;
  37. const Double_t MASS_HIST_FIT_MAX = 5700.;
  38. const char* TITLE = "SpruceRD_B0ToHpHmMuMu (#pi^{+} #rightarrow K^{+}, w/o excl Hlt2 decision cut)";
  39. const char* FILE_NAME = "SpruceRD_B0ToHpHmMuMu_Pip2Kp_wohlt2cut";
  40. const char* MASS_LITERAL = "m(#pi^{+}_{(#rightarrow K^{+})}#pi^{-}#mu^{+}#mu^{-})";
  41. const Double_t K_MASS = 493.677;
  42. void CreateRooFitAndDraw(TH1D *hist, int fitting_entries);
  43. int analysis_fullstream_b02hphmmumu()
  44. {
  45. TChain *data_chain = new TChain("B0ToHpHmMuMu/DecayTree");
  46. data_chain->Add("/auto/data/pfeiffer/inclusive_detached_dilepton/data_samples/spruce_magdown_2023_v0r1_tuple_90000000_2023_v0r0p6288631.root");
  47. data_chain->Add("/auto/data/pfeiffer/inclusive_detached_dilepton/data_samples/spruce_magdown_2023_v0_tuple_90000000_v0r0p6288631.root");
  48. Float_t B0_BPVFDCHI2,
  49. B0_BPVIPCHI2,
  50. L1_BPVIPCHI2,
  51. L2_BPVIPCHI2,
  52. L1_PT,
  53. L2_PT,
  54. Jpsi_BPVFDCHI2,
  55. Hp_PT,
  56. Hp_BPVIPCHI2,
  57. Hp_P,
  58. Hm_PT,
  59. Hm_BPVIPCHI2,
  60. Hm_P,
  61. Res_PT,
  62. Res_BPVIPCHI2,
  63. Res_P;
  64. Double_t L1_PID_MU,
  65. L2_PID_MU,
  66. B0_CHI2VXNDOF,
  67. Jpsi_MAXDOCACHI2,
  68. Jpsi_CHI2DOF,
  69. Res_CHI2DOF,
  70. Hp_PID_K,
  71. Hm_PID_K,
  72. Jpsi_M,
  73. B0_M,
  74. Res_MAXDOCACHI2,
  75. Res_M;
  76. Bool_t L1_ISMUON,
  77. L2_ISMUON,
  78. Hlt2RD_B0ToKpPimMuMuDecision,
  79. Hlt2_InclDetDiMuon_4BodyDecision,
  80. Hlt2_InclDetDiMuon_3BodyDecision,
  81. Hlt2_InclDetDiMuonDecision,
  82. Hlt1TrackMVADecision,
  83. Hlt1TwoTrackMVADecision;
  84. data_chain->SetBranchAddress("B0_M", &B0_M);
  85. data_chain->SetBranchAddress("B0_BPVFDCHI2", &B0_BPVFDCHI2);
  86. data_chain->SetBranchAddress("B0_BPVIPCHI2", &B0_BPVIPCHI2);
  87. data_chain->SetBranchAddress("L1_BPVIPCHI2", &L1_BPVIPCHI2);
  88. data_chain->SetBranchAddress("L2_BPVIPCHI2", &L2_BPVIPCHI2);
  89. data_chain->SetBranchAddress("L1_PID_MU", &L1_PID_MU);
  90. data_chain->SetBranchAddress("L2_PID_MU", &L2_PID_MU);
  91. data_chain->SetBranchAddress("L1_ISMUON", &L1_ISMUON);
  92. data_chain->SetBranchAddress("L2_ISMUON", &L2_ISMUON);
  93. data_chain->SetBranchAddress("L1_PT", &L1_PT);
  94. data_chain->SetBranchAddress("L2_PT", &L2_PT);
  95. data_chain->SetBranchAddress("B0_CHI2VXNDOF", &B0_CHI2VXNDOF);
  96. data_chain->SetBranchAddress("Jpsi_MAXDOCACHI2", &Jpsi_MAXDOCACHI2);
  97. data_chain->SetBranchAddress("Jpsi_CHI2DOF", &Jpsi_CHI2DOF);
  98. data_chain->SetBranchAddress("Res_CHI2DOF", &Res_CHI2DOF);
  99. data_chain->SetBranchAddress("Hp_PT", &Hp_PT);
  100. data_chain->SetBranchAddress("Hp_BPVIPCHI2", &Hp_BPVIPCHI2);
  101. data_chain->SetBranchAddress("Hp_P", &Hp_P);
  102. data_chain->SetBranchAddress("Hp_PID_K", &Hp_PID_K);
  103. data_chain->SetBranchAddress("Hm_PT", &Hm_PT);
  104. data_chain->SetBranchAddress("Hm_BPVIPCHI2", &Hm_BPVIPCHI2);
  105. data_chain->SetBranchAddress("Hm_P", &Hm_P);
  106. data_chain->SetBranchAddress("Hm_PID_K", &Hm_PID_K);
  107. data_chain->SetBranchAddress("Res_PT", &Res_PT);
  108. data_chain->SetBranchAddress("Res_BPVIPCHI2", &Res_BPVIPCHI2);
  109. data_chain->SetBranchAddress("Res_P", &Res_P);
  110. data_chain->SetBranchAddress("Res_MAXDOCACHI2", &Res_MAXDOCACHI2);
  111. data_chain->SetBranchAddress("Res_M", &Res_M);
  112. data_chain->SetBranchAddress("Jpsi_BPVFDCHI2", &Jpsi_BPVFDCHI2);
  113. data_chain->SetBranchAddress("Jpsi_M", &Jpsi_M);
  114. data_chain->SetBranchAddress("Hlt2RD_B0ToKpPimMuMuDecision", &Hlt2RD_B0ToKpPimMuMuDecision);
  115. data_chain->SetBranchAddress("Hlt2_InclDetDiMuon_4BodyDecision", &Hlt2_InclDetDiMuon_4BodyDecision);
  116. data_chain->SetBranchAddress("Hlt2_InclDetDiMuon_3BodyDecision", &Hlt2_InclDetDiMuon_3BodyDecision);
  117. data_chain->SetBranchAddress("Hlt2_InclDetDiMuonDecision", &Hlt2_InclDetDiMuonDecision);
  118. data_chain->SetBranchAddress("Hlt1TrackMVADecision", &Hlt1TrackMVADecision);
  119. data_chain->SetBranchAddress("Hlt1TwoTrackMVADecision", &Hlt1TwoTrackMVADecision);
  120. Float_t
  121. L1_PX, L1_PY, L1_PZ, L1_ENERGY,
  122. L2_PX, L2_PY, L2_PZ, L2_ENERGY,
  123. Hp_PX, Hp_PY, Hp_PZ, Hp_ENERGY,
  124. Hm_PX, Hm_PY, Hm_PZ, Hm_ENERGY;
  125. data_chain->SetBranchAddress("L1_PX", &L1_PX);
  126. data_chain->SetBranchAddress("L1_PY", &L1_PY);
  127. data_chain->SetBranchAddress("L1_PZ", &L1_PZ);
  128. data_chain->SetBranchAddress("L1_ENERGY", &L1_ENERGY);
  129. data_chain->SetBranchAddress("L2_PX", &L2_PX);
  130. data_chain->SetBranchAddress("L2_PY", &L2_PY);
  131. data_chain->SetBranchAddress("L2_PZ", &L2_PZ);
  132. data_chain->SetBranchAddress("L2_ENERGY", &L2_ENERGY);
  133. data_chain->SetBranchAddress("Hp_PX", &Hp_PX);
  134. data_chain->SetBranchAddress("Hp_PY", &Hp_PY);
  135. data_chain->SetBranchAddress("Hp_PZ", &Hp_PZ);
  136. data_chain->SetBranchAddress("Hp_ENERGY", &Hp_ENERGY);
  137. data_chain->SetBranchAddress("Hm_PX", &Hm_PX);
  138. data_chain->SetBranchAddress("Hm_PY", &Hm_PY);
  139. data_chain->SetBranchAddress("Hm_PZ", &Hm_PZ);
  140. data_chain->SetBranchAddress("Hm_ENERGY", &Hm_ENERGY);
  141. TH1D *h1_B0_M = new TH1D("h1_B0_M", TITLE, nBins, MASS_HIST_MIN, MASS_HIST_MAX);
  142. unsigned int entries = data_chain->GetEntries();
  143. unsigned int fitting_entries = 0;
  144. for (unsigned int i = 0; i < entries; i++)
  145. {
  146. data_chain->GetEntry(i);
  147. TLorentzVector l1_4v(L1_PX, L1_PY, L1_PZ, L1_ENERGY);
  148. TLorentzVector l2_4v(L2_PX, L2_PY, L2_PZ, L2_ENERGY);
  149. // Pi+ -> K+
  150. TVector3 K_momentum(Hp_PX, Hp_PY, Hp_PZ);
  151. double K_energy = TMath::Sqrt(TMath::Sq(K_MASS) + K_momentum.Mag2());
  152. TLorentzVector Pi_4v(Hm_PX, Hm_PY, Hm_PZ, Hm_ENERGY);
  153. // Pi- -> K-
  154. // TVector3 K_momentum(Hm_PX, Hm_PY, Hm_PZ);
  155. // double K_energy = TMath::Sqrt(TMath::Sq(K_MASS) + K_momentum.Mag2());
  156. // TLorentzVector Pi_4v(Hp_PX, Hp_PY, Hp_PZ, Hp_ENERGY);
  157. TLorentzVector K_4v(K_momentum, K_energy);
  158. Double_t reconstructed_Res_Mass = (K_4v + Pi_4v).M();
  159. Double_t reconstructed_B0_Mass = (K_4v + Pi_4v + l1_4v + l2_4v).M();
  160. if ((
  161. (B0_BPVFDCHI2 > 64) & (B0_CHI2VXNDOF < 9) & (B0_BPVIPCHI2 < 25) &
  162. //
  163. (Jpsi_MAXDOCACHI2 < 36) &
  164. //
  165. (L1_BPVIPCHI2 > 9) & (L2_BPVIPCHI2 > 9) & (L1_PID_MU > -3) & (L2_PID_MU > -3) & (L1_ISMUON) & (L2_ISMUON) & (L1_PT > 350) & (L2_PT > 350) &
  166. //
  167. (Jpsi_CHI2DOF < 9) & (Jpsi_BPVFDCHI2 > 16) & (Jpsi_M < 5500) &
  168. //
  169. (Res_PT > 400) & (Res_MAXDOCACHI2 < 36) & (reconstructed_Res_Mass < 2600) & (Res_CHI2DOF < 25) & (Res_BPVIPCHI2 > 4) &
  170. //
  171. (Hm_BPVIPCHI2 > 6) & (Hm_PT > 250) & (Hm_P > 2000) & (Hm_PID_K < 0) &
  172. //
  173. (Hp_BPVIPCHI2 > 6) & (Hp_PT > 250) & (Hp_P > 2000) & (Hp_PID_K > 0)
  174. ) &
  175. //
  176. /*(Hlt2RD_B0ToKpPimMuMuDecision) &*/ ((Hlt2_InclDetDiMuon_4BodyDecision) | (Hlt2_InclDetDiMuon_3BodyDecision) | (Hlt2_InclDetDiMuonDecision)))
  177. {
  178. if ((TMath::Abs(Jpsi_M - 3096.9) < 100) & ((Hlt1TrackMVADecision) | (Hlt1TwoTrackMVADecision)))
  179. {
  180. h1_B0_M->Fill(reconstructed_B0_Mass);
  181. if (MASS_HIST_FIT_MIN <= reconstructed_B0_Mass && reconstructed_B0_Mass <= MASS_HIST_FIT_MAX)
  182. {
  183. fitting_entries++;
  184. }
  185. }
  186. }
  187. if (i % 10000 == 0)
  188. {
  189. std::cout << "["
  190. << FILE_NAME
  191. << "] Processed event: " << i << " (" << TString::Format("%.2f", ((double)i / (double)entries) * 100.) << "%)" << std::endl;
  192. }
  193. }
  194. h1_B0_M->GetXaxis()->SetTitle(MASS_LITERAL);
  195. TCanvas *c1 = new TCanvas("c1", "c1", 0, 0, 800, 600);
  196. h1_B0_M->Draw();
  197. c1->Draw();
  198. c1->SaveAs(TString::Format("images/root_hist_%s_bmass.pdf", FILE_NAME).Data());
  199. CreateRooFitAndDraw(h1_B0_M, fitting_entries);
  200. return 0;
  201. }
  202. void CreateRooFitAndDraw(TH1D *hist, int fitting_entries)
  203. {
  204. RooRealVar roo_var_mass("var_mass", "B0 Mass Variable", MASS_HIST_FIT_MIN, MASS_HIST_FIT_MAX);
  205. roo_var_mass.setRange("fitting_range", MASS_HIST_FIT_MIN, MASS_HIST_FIT_MAX);
  206. RooDataHist roohist_B0_M("roohist_B0_M", "B0 Mass Histogram", roo_var_mass, RooFit::Import(*hist));
  207. RooPlot *roo_frame_mass = roo_var_mass.frame(RooFit::Title(TITLE));
  208. roohist_B0_M.plotOn(roo_frame_mass, RooFit::Binning(nBins), RooFit::Name("B Mass Distribution"));
  209. roo_frame_mass->GetXaxis()->SetTitle(MASS_LITERAL);
  210. // Crystal Ball for Signal
  211. RooRealVar var_mass_x0("var_mass_x0", "#mu", 5278., 5170., 5500.);
  212. RooRealVar var_mass_sigmaLR("var_mass_sigmaLR", "#sigma_{LR}", 16., 5., 40.);
  213. // Same Variables for Left and Right Tail
  214. // RooRealVar var_mass_alphaL("var_mass_alphaL", "#alpha_{L}", 2., 0., 4.);
  215. // RooRealVar var_mass_nL("var_mass_nL", "n_{L}", 5., 0., 15.);
  216. // RooRealVar var_mass_alphaR("var_mass_alphaR", "#alpha_{R}", 2., 0., 4.);
  217. // RooRealVar var_mass_nR("var_mass_nR", "n_{R}", 5., 0., 15.);
  218. auto var_mass_alphaL = RooRealConstant::value(1.915);
  219. auto var_mass_nL = RooRealConstant::value(0.573);
  220. auto var_mass_alphaR = RooRealConstant::value(2.257);
  221. auto var_mass_nR = RooRealConstant::value(0.418);
  222. 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);
  223. // Exponential for Background
  224. RooRealVar var_mass_bkg_c("var_mass_bkg_c", "#lambda", -0.0014, -0.004, -0.000);
  225. RooExponential bkg_exp("bkg_exp", "Exp Background", roo_var_mass, var_mass_bkg_c);
  226. RooRealVar var_mass_nsig("nsig", "Mass N Signal", 0., hist->GetEntries());
  227. RooRealVar var_mass_nbkg("nbkg", "Mass N Background", 0., hist->GetEntries());
  228. // TString pdf_name = TString::Format("%s_sigplusbkg", var_id.c_str());
  229. RooAddPdf sigplusbkg("sigplusbkg", "Sig and Bkg PDF", RooArgList(sig_cb, bkg_exp), RooArgList(var_mass_nsig, var_mass_nbkg));
  230. RooFitResult *fitres = sigplusbkg.fitTo(roohist_B0_M, RooFit::Save(), RooFit::PrintLevel(1), RooFit::Range("fitting_range"));
  231. sigplusbkg.plotOn(roo_frame_mass, RooFit::VisualizeError(*fitres, 1), RooFit::FillColor(kOrange + 1), RooFit::FillStyle(3144));
  232. sigplusbkg.plotOn(roo_frame_mass, RooFit::LineColor(kRed), RooFit::LineStyle(kSolid), RooFit::Range("fitting_range"), RooFit::Name("sigplusbkg"));
  233. 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"));
  234. 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"));
  235. TCanvas *c = new TCanvas("roofit_c", "roofit_c", 0, 0, 800, 600);
  236. roo_frame_mass->Draw();
  237. TLegend *leg1 = new TLegend(0.50, 0.58, 0.87, 0.89);
  238. leg1->SetFillColor(kWhite);
  239. leg1->SetLineColor(kBlack);
  240. leg1->AddEntry(roo_frame_mass->findObject("sigplusbkg"), "Signal + Background", "LP");
  241. leg1->AddEntry(roo_frame_mass->findObject("sig_cb"), "Signal", "LP");
  242. leg1->AddEntry(roo_frame_mass->findObject("bkg_exp"), "Background", "LP");
  243. leg1->AddEntry((TObject *)0, "", "");
  244. leg1->AddEntry((TObject *)0, TString::Format("%s = %.3f #pm %.3f", var_mass_x0.getTitle().Data(), var_mass_x0.getVal(), var_mass_x0.getError()).Data(), "");
  245. leg1->AddEntry((TObject *)0, TString::Format("%s = %.3f #pm %.3f", var_mass_sigmaLR.getTitle().Data(), var_mass_sigmaLR.getVal(), var_mass_sigmaLR.getError()).Data(), "");
  246. 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(), "");
  247. leg1->AddEntry((TObject *)0, TString::Format("%s = %.3f #pm %.3f", "S", var_mass_nsig.getVal(), var_mass_nsig.getError()).Data(), "");
  248. leg1->AddEntry((TObject *)0, TString::Format("%s = %.3f #pm %.3f", "B", var_mass_nbkg.getVal(), var_mass_nbkg.getError()).Data(), "");
  249. leg1->AddEntry((TObject *)0, TString::Format("Entries: %d", fitting_entries), "");
  250. leg1->Draw();
  251. c->Draw();
  252. c->SaveAs(TString::Format("images/data/roofit_hist_%s_fitted_bmass.pdf", FILE_NAME).Data());
  253. }