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.

344 lines
14 KiB

11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
  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 <string>
  27. #include <iostream>
  28. #include <cmath>
  29. const int nBins = 70;
  30. const Double_t MASS_HIST_MIN = 5100.;
  31. const Double_t MASS_HIST_MAX = 6000.;
  32. const Double_t J_PSI_MASS = 3096.916;
  33. std::vector<RooPlot*> CreateRooFit(TH1D *hist);
  34. bool inRange(double value, double center, double low_intvl, double up_intvl) {
  35. return center - low_intvl < value && value < center + up_intvl;
  36. }
  37. bool inRange(double value, double center, double intvl) {
  38. return inRange(value, center, intvl, intvl);
  39. }
  40. int pre_selection_cuts()
  41. {
  42. // just some plotting options
  43. gROOT->SetStyle("Plain");
  44. TPad foo;
  45. const int NRGBs = 5;
  46. const int NCont = 250;
  47. double stops[NRGBs] = {0.00, 0.34, 0.61, 0.84, 1.00};
  48. double red[NRGBs] = {0.00, 0.00, 0.87, 1.00, 0.51};
  49. double green[NRGBs] = {0.00, 0.81, 1.00, 0.20, 0.00};
  50. double blue[NRGBs] = {0.51, 1.00, 0.12, 0.00, 0.00};
  51. TColor::CreateGradientColorTable(NRGBs, stops, red, green, blue, NCont);
  52. gStyle->SetNumberContours(NCont);
  53. gStyle->SetLabelFont(132, "xyz");
  54. gStyle->SetTitleFont(132, "xyz");
  55. gStyle->SetLegendFont(132);
  56. gStyle->SetStatFont(132);
  57. gStyle->SetEndErrorSize(10.0);
  58. gStyle->SetOptStat(0);
  59. gStyle->SetOptFit(0);
  60. // files to load
  61. std::vector<std::string> data_filenames =
  62. {
  63. "/auto/data/pfeiffer/inclusive_detached_dilepton/data_samples/BuToKpMuMu_Collision23_Beam6800GeV-VeloClosed-MagDown-Excl-UT_RealData_Sprucing23r1_90000000_RD.root"};
  64. TChain *data_chain = new TChain("SpruceRD_BuToKpMuMu/DecayTree");
  65. for (unsigned int i = 0; i < data_filenames.size(); i++)
  66. {
  67. data_chain->Add(data_filenames.at(i).c_str());
  68. }
  69. // files to load
  70. std::vector<std::string> mc_filenames =
  71. {
  72. "/auto/data/pfeiffer/inclusive_detached_dilepton/MC/BuToKpMuMu_rd_btoxll_simulation_12143001_MagDown_v0r0p6316365_FULLSTREAM.root"};
  73. TChain *mc_chain = new TChain("BuToKpMuMu_noPID/DecayTree");
  74. for (unsigned int i = 0; i < mc_filenames.size(); i++)
  75. {
  76. mc_chain->Add(mc_filenames.at(i).c_str());
  77. }
  78. Double_t Bplus_M, // 4400 -> 8200
  79. Jpsi_M, // 200 -> 6600
  80. Kplus_M; // 493.6769 -> 493.6779
  81. Float_t Bplus_PT, // 0 -> 30 * 10^3
  82. Bplus_BPVFDCHI2, // 0 -> 7000
  83. Bplus_BPVIPCHI2,
  84. Jpsi_BPVIPCHI2, // 0 -> 3
  85. Jpsi_PT, // 0 -> 26 * 10^3
  86. Kplus_BPVIPCHI2, // 0 -> 7
  87. Kplus_PT; // 0 -> 11 * 10^3
  88. Double_t Bplus_M_mc, // 4400 -> 8200
  89. Jpsi_M_mc, // 200 -> 6600
  90. Kplus_M_mc, // 493.6769 -> 493.6779
  91. Kplus_PID_K; // -20 -> 20
  92. Float_t Bplus_PT_mc, // 0 -> 30 * 10^3
  93. Bplus_BPVFDCHI2_mc, // 0 -> 7000
  94. Jpsi_BPVIPCHI2_mc, // 0 -> 3
  95. Jpsi_PT_mc, // 0 -> 26 * 10^3
  96. Kplus_BPVIPCHI2_mc, // 0 -> 7
  97. Kplus_PT_mc, // 0 -> 11 * 10^3
  98. Kplus_PID_K_mc;
  99. Float_t muplus_PX, // 0 -> 30 * 10^3
  100. muplus_PY, // 0 -> 7000
  101. muplus_PZ, // 0 -> 3
  102. muplus_ENERGY, // 0 -> 26 * 10^3
  103. muminus_PX, // 0 -> 7
  104. muminus_PY, // 0 -> 11 * 10^3
  105. muminus_PZ, // 0 -> 7
  106. muminus_ENERGY; // 0 -> 11 * 10^3
  107. data_chain->SetBranchAddress("Bplus_M", &Bplus_M);
  108. data_chain->SetBranchAddress("Bplus_PT", &Bplus_PT);
  109. data_chain->SetBranchAddress("Bplus_BPVFDCHI2", &Bplus_BPVFDCHI2);
  110. data_chain->SetBranchAddress("Bplus_BPVIPCHI2", &Bplus_BPVIPCHI2);
  111. data_chain->SetBranchAddress("Jpsi_M", &Jpsi_M);
  112. data_chain->SetBranchAddress("Jpsi_BPVIPCHI2", &Jpsi_BPVIPCHI2);
  113. data_chain->SetBranchAddress("Jpsi_PT", &Jpsi_PT);
  114. data_chain->SetBranchAddress("Kplus_M", &Kplus_M);
  115. data_chain->SetBranchAddress("Kplus_BPVIPCHI2", &Kplus_BPVIPCHI2);
  116. data_chain->SetBranchAddress("Kplus_PT", &Kplus_PT);
  117. data_chain->SetBranchAddress("Kplus_PID_K", &Kplus_PID_K);
  118. data_chain->SetBranchAddress("muplus_PX", &muplus_PX);
  119. data_chain->SetBranchAddress("muplus_PY", &muplus_PY);
  120. data_chain->SetBranchAddress("muplus_PZ", &muplus_PZ);
  121. data_chain->SetBranchAddress("muplus_ENERGY", &muplus_ENERGY);
  122. data_chain->SetBranchAddress("muminus_PX", &muminus_PX);
  123. data_chain->SetBranchAddress("muminus_PY", &muminus_PY);
  124. data_chain->SetBranchAddress("muminus_PZ", &muminus_PZ);
  125. data_chain->SetBranchAddress("muminus_ENERGY", &muminus_ENERGY);
  126. mc_chain->SetBranchAddress("B_M", &Bplus_M_mc);
  127. mc_chain->SetBranchAddress("B_PT", &Bplus_PT_mc);
  128. mc_chain->SetBranchAddress("B_BPVFDCHI2", &Bplus_BPVFDCHI2_mc);
  129. mc_chain->SetBranchAddress("Jpsi_M", &Jpsi_M_mc);
  130. mc_chain->SetBranchAddress("Jpsi_BPVIPCHI2", &Jpsi_BPVIPCHI2_mc);
  131. mc_chain->SetBranchAddress("Jpsi_PT", &Jpsi_PT_mc);
  132. mc_chain->SetBranchAddress("K_M", &Kplus_M_mc);
  133. mc_chain->SetBranchAddress("K_BPVIPCHI2", &Kplus_BPVIPCHI2_mc);
  134. mc_chain->SetBranchAddress("K_PT", &Kplus_PT_mc);
  135. TH1D *h1_Bplus_M = new TH1D("h1_Bplus_M", "B+ Mass", nBins, MASS_HIST_MIN, MASS_HIST_MAX);
  136. TH1D *h1_Bplus_PT = new TH1D("h1_Bplus_PT", "B+ P_{T}", nBins, 0, 15000);
  137. TH1D *h1_Bplus_BPVFDCHI2 = new TH1D("h1_Bplus_BPVFDCHI2", "B+ FD #chi^{2}", nBins, 0, 1500);
  138. TH1D *h1_Jpsi_M = new TH1D("h1_Jpsi_M", "J/#psi Mass", nBins, 200, 6600);
  139. TH1D *h1_Jpsi_BPVIPCHI2 = new TH1D("h1_Jpsi_BPVIPCHI2", "J/#psi IP #chi^{2}", nBins, 0, 100);
  140. TH1D *h1_Jpsi_PT = new TH1D("h1_Jpsi_PT", "J/#psi P_{T}", nBins, 0, 15000);
  141. TH1D *h1_Kplus_M = new TH1D("h1_Kplus_M", "K^{+} Mass", nBins, 493.676999999, 493.677000001);
  142. TH1D *h1_Kplus_BPVIPCHI2 = new TH1D("h1_Kplus_BPVIPCHI2", "K^{+} IP #chi^{2}", nBins, 0, 100);
  143. TH1D *h1_Kplus_PT = new TH1D("h1_Kplus_PT", "K^{+} P_{T}", nBins, 0, 6500);
  144. TH1D *h1_Kplus_PID_K = new TH1D("h1_Kplus_PID_K", "K^{+} PID K", nBins, -5, 5);
  145. TH1D *h1_bkg_Bplus_M = new TH1D("h1_bkg_Bplus_M", "B+ Mass", nBins, MASS_HIST_MIN, MASS_HIST_MAX);
  146. TH1D *h1_bkg_Bplus_PT = new TH1D("h1_bkg_Bplus_PT", "B+ P_{T}", nBins, 0, 15000);
  147. TH1D *h1_bkg_Bplus_BPVFDCHI2 = new TH1D("h1_bkg_Bplus_BPVFDCHI2", "B+ FD #chi^{2}", nBins, 0, 1500);
  148. TH1D *h1_bkg_Jpsi_M = new TH1D("h1_bkg_Jpsi_M", "J/#psi Mass", nBins, 200, 6600);
  149. TH1D *h1_bkg_Jpsi_BPVIPCHI2 = new TH1D("h1_bkg_Jpsi_BPVIPCHI2", "J/#psi IP #chi^{2}", nBins, 0, 100);
  150. TH1D *h1_bkg_Jpsi_PT = new TH1D("h1_bkg_Jpsi_PT", "J/#psi P_{T}", nBins, 0, 15000);
  151. TH1D *h1_bkg_Kplus_M = new TH1D("h1_bkg_Kplus_M", "K^{+} Mass", nBins, 493.676999999, 493.677000001);
  152. TH1D *h1_bkg_Kplus_BPVIPCHI2 = new TH1D("h1_bkg_Kplus_BPVIPCHI2", "K^{+} IP #chi^{2}", nBins, 0, 100);
  153. TH1D *h1_bkg_Kplus_PT = new TH1D("h1_bkg_Kplus_PT", "K^{+} P_{T}", nBins, 0, 6500);
  154. TH1D *h1_bkg_Kplus_PID_K = new TH1D("h1_bkg_Kplus_PID_K", "K^{+} PID K", nBins, -5, 5);
  155. unsigned int entries = data_chain->GetEntries();
  156. unsigned int nan_values = 0;
  157. // loop over all entries in the tree
  158. for (unsigned int i = 0; i < entries; i++)
  159. {
  160. data_chain->GetEntry(i);
  161. const double JSPI_MASS = 3096.;
  162. if (!std::isfinite(Jpsi_M)) {
  163. nan_values++;
  164. continue;
  165. }
  166. TLorentzVector v4_muplus(muplus_PX, muplus_PY, muplus_PZ, muplus_ENERGY);
  167. TLorentzVector v4_muminus(muminus_PX, muminus_PY, muminus_PZ, muminus_ENERGY);
  168. Double_t mumu_inv_mass = (v4_muplus + v4_muminus).M();
  169. if (inRange(mumu_inv_mass, J_PSI_MASS, 120.))
  170. {
  171. h1_Bplus_M->Fill(Bplus_M);
  172. }
  173. h1_bkg_Bplus_PT->Fill(Bplus_PT);
  174. h1_bkg_Bplus_BPVFDCHI2->Fill(Bplus_BPVFDCHI2);
  175. h1_bkg_Jpsi_M->Fill(Jpsi_M);
  176. h1_bkg_Jpsi_BPVIPCHI2->Fill(Jpsi_BPVIPCHI2);
  177. h1_bkg_Jpsi_PT->Fill(Jpsi_PT);
  178. h1_bkg_Kplus_M->Fill(Kplus_M);
  179. h1_bkg_Kplus_BPVIPCHI2->Fill(Kplus_BPVIPCHI2);
  180. h1_bkg_Kplus_PT->Fill(Kplus_PT);
  181. h1_bkg_Kplus_PID_K->Fill(Kplus_PID_K);
  182. }
  183. std::cout << TString::Format("Skipped %d NaN Values.", nan_values) << std::endl;
  184. unsigned int mc_entries = mc_chain->GetEntries();
  185. for (size_t i = 0; i < mc_entries; i++)
  186. {
  187. mc_chain->GetEntry(i);
  188. h1_Bplus_PT->Fill(Bplus_PT_mc);
  189. h1_Bplus_BPVFDCHI2->Fill(Bplus_BPVFDCHI2_mc);
  190. h1_Jpsi_M->Fill(Jpsi_M_mc);
  191. h1_Jpsi_BPVIPCHI2->Fill(Jpsi_BPVIPCHI2_mc);
  192. h1_Jpsi_PT->Fill(Jpsi_PT_mc);
  193. h1_Kplus_M->Fill(Kplus_M);
  194. h1_Kplus_BPVIPCHI2->Fill(Kplus_BPVIPCHI2_mc);
  195. h1_Kplus_PT->Fill(Kplus_PT_mc);
  196. h1_Kplus_PID_K->Fill(Kplus_PID_K_mc);
  197. }
  198. TCanvas *c1 = new TCanvas("c1", "Canvas 1", 0, 0, 800, 600);
  199. //h1_bkg_Bplus_M->SetLineColor(kRed);
  200. //h1_bkg_Bplus_M->Draw();
  201. h1_Bplus_M->Draw();
  202. c1->Draw();
  203. std::vector<TH1 *> small_histos{h1_Bplus_PT, h1_Bplus_BPVFDCHI2, h1_Jpsi_M, h1_Jpsi_BPVIPCHI2, h1_Jpsi_PT, h1_Kplus_M, h1_Kplus_BPVIPCHI2, h1_Kplus_PT, h1_Kplus_PID_K};
  204. std::vector<TH1 *> small_bkg_histos{h1_bkg_Bplus_PT, h1_bkg_Bplus_BPVFDCHI2, h1_bkg_Jpsi_M, h1_bkg_Jpsi_BPVIPCHI2, h1_bkg_Jpsi_PT, h1_bkg_Kplus_M, h1_bkg_Kplus_BPVIPCHI2, h1_bkg_Kplus_PT, h1_bkg_Kplus_PID_K};
  205. TCanvas *c2 = new TCanvas("c2", "Canvas 2", 0, 0, 1200, 600);
  206. c2->Divide(4, 3);
  207. for (size_t i = 0; i < small_histos.size(); i++)
  208. {
  209. c2->cd(i + 1);
  210. // small_histos[i]->SetLineWidth(1);
  211. // small_histos[i]->SetMinimum(0);
  212. // small_histos[i]->SetLineColor(kRed);
  213. // small_histos[i]->SetFillColor(kRed);
  214. // small_histos[i]->SetFillStyle(3004);
  215. // small_histos[i]->Scale(1. / small_histos[i]->Integral(), "width");
  216. small_bkg_histos[i]->SetLineColor(kBlue);
  217. small_bkg_histos[i]->SetLineWidth(2);
  218. small_bkg_histos[i]->SetMinimum(0);
  219. // small_bkg_histos[i]->Scale(1. / small_bkg_histos[i]->Integral(), "width");
  220. // if (small_histos[i]->GetMaximum() > small_bkg_histos[i]->GetMaximum())
  221. // {
  222. // small_histos[i]->Draw("HIST");
  223. // small_bkg_histos[i]->Draw("HIST SAME");
  224. // }
  225. // else
  226. // {
  227. // small_bkg_histos[i]->Draw("HIST");
  228. // small_histos[i]->Draw("HIST SAME");
  229. // }
  230. small_bkg_histos[i]->Draw("HIST");
  231. }
  232. c2->Draw();
  233. TCanvas *c3 = new TCanvas("c3", "Canvas 3", 0, 0, 1000, 700);
  234. auto fittedPlots = CreateRooFit(h1_Bplus_M);
  235. auto *p2 = new TPad("p2", "Pull", 0., 0., 1., 0.3);
  236. p2->Draw();
  237. p2->SetTopMargin(0.001);
  238. p2->SetBottomMargin(0.3);
  239. p2->SetGrid();
  240. auto *p1 = new TPad("p1", "Fit", 0., 0.32, 1., 1.);
  241. p1->Draw();
  242. p1->SetBottomMargin(0.001);
  243. p1->cd();
  244. fittedPlots[0]->Draw();
  245. p2->cd();
  246. fittedPlots[1]->Draw();
  247. c3->Draw();
  248. return 0;
  249. }
  250. std::vector<RooPlot*> CreateRooFit(TH1D *hist)
  251. {
  252. RooRealVar roo_var_mass("roo_var_mass", "B+ Mass Variable", MASS_HIST_MIN, MASS_HIST_MAX);
  253. roo_var_mass.setRange("fitting_range", 5100., 6000.);
  254. TString hist_name = "roohist_bplus_M";
  255. RooDataHist roohist_bplus_M(hist_name, "B Plus Mass Histogram", roo_var_mass, RooFit::Import(*hist));
  256. RooRealVar roo_sig_bw_mean("roo_sig_bw_mean", "Mass BW Mean", 5250., 5100., 5400.);
  257. RooRealVar roo_sig_bw_with("roo_sig_bw_with", "Mass BW Width", 20., 0., 50.);
  258. RooBreitWigner roo_sig_bw("roo_sig_bw", "B+ Signal Breit Wigner", roo_var_mass, roo_sig_bw_mean, roo_sig_bw_with);
  259. RooRealVar roo_bkg_exp_c("roo_bkg_exp_c", "Background C", -0.001145, -0.00199, -0.00100);
  260. RooExponential roo_bkg_exp("roo_bkg_exp", "B+ Mass Background Exp", roo_var_mass, roo_bkg_exp_c);
  261. RooRealVar roo_var_mass_nsig("roo_var_mass_nsig", "B+ Mass N Signal", 0., hist->GetEntries());
  262. RooRealVar roo_var_mass_nbkg("roo_var_mass_nbkg", "B+ Mass N Background", 0., hist->GetEntries());
  263. TString pdf_name = "roo_pdf_sig_plus_bkg";
  264. RooAddPdf roo_pdf_sig_plus_bkg(pdf_name, "Sig + Bkg PDF",
  265. RooArgList(roo_sig_bw, roo_bkg_exp),
  266. RooArgList(roo_var_mass_nsig, roo_var_mass_nbkg));
  267. RooPlot *roo_frame_mass = roo_var_mass.frame();
  268. roohist_bplus_M.plotOn(roo_frame_mass, RooFit::Binning(nBins), RooFit::Name(hist_name));
  269. RooFitResult *fitres = roo_pdf_sig_plus_bkg.fitTo(roohist_bplus_M, RooFit::Save(), RooFit::PrintLevel(1), RooFit::Range("fitting_range"));
  270. roo_pdf_sig_plus_bkg.plotOn(roo_frame_mass, RooFit::VisualizeError(*fitres, 1), RooFit::FillColor(kOrange + 1), RooFit::FillStyle(3144));
  271. roo_pdf_sig_plus_bkg.plotOn(roo_frame_mass, RooFit::LineColor(kRed), RooFit::LineStyle(kSolid), RooFit::Range("fitting_range"), RooFit::Name(pdf_name));
  272. roo_pdf_sig_plus_bkg.plotOn(roo_frame_mass, RooFit::Components(RooArgSet(roo_bkg_exp)), RooFit::LineColor(kBlue), RooFit::LineStyle(kDashed), RooFit::Range("fitting_range"));
  273. roo_pdf_sig_plus_bkg.paramOn(roo_frame_mass, RooFit::Layout(0.50, 0.99, 0.90));
  274. roo_frame_mass->getAttText()->SetTextSize(0.025);
  275. RooPlot *roo_frame_pull = roo_var_mass.frame(RooFit::Title("Pull Distribution"));
  276. roo_frame_pull->addPlotable(roo_frame_mass->pullHist(hist_name, pdf_name), "P");
  277. return std::vector<RooPlot*> { roo_frame_mass, roo_frame_pull };
  278. }