Angular analysis of B+->K*+(K+pi0)mumu
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.

708 lines
35 KiB

  1. //Truth matching of MC data for the decay B+->Kst+mumu
  2. //david gerick
  3. //Renata Kopecna
  4. #include "GlobalFunctions.hh"
  5. #include "Design.hpp"
  6. #include "MassFit.hpp"
  7. #include "Paths.hpp"
  8. #include "TMsource.cpp"
  9. bool FixParameters = false;
  10. using namespace std;
  11. using namespace RooFit ;
  12. //////////////////////////////////////////////////////
  13. /// MCtruth()
  14. /// compares the particleID of all reconstructed particles in the chain with the trueID
  15. /// ID-check of the (grand)mother particles will be performed!
  16. ///
  17. /// TruthMatchAll()
  18. /// run the MCtruth() for all four configurations of 2011/2012 and down/up
  19. ///
  20. /// PlotTruthMatchingEfficienciesToPDF()
  21. /// Take the four TGraphErrors from the TFile, plot them nicely into canvases and print it to pdf files in a dedicated folder!
  22. ///
  23. /// PlotAllTruthMatchingEfficienciesToPDF()
  24. /// The function name explains it all ;)
  25. ///
  26. Double_t YieldBeforeTruthMatching = 0., YieldAfterTruthMatching = 0.;
  27. Double_t YieldBeforeTruthMatchingErr = 0., YieldAfterTruthMatchingErr = 0.;
  28. //1: passed gammas (both), 2: one passed, one converted, 3: both converted, 4: one passed, one random, 5: one converted, one random, 6: both failed
  29. int passGammas(gamma_IDs gamma1, gamma_IDs gamma2, counters *counter,
  30. bool B_plus_TM, bool K_star_plus_TM, bool pi_zero_TM, bool gamma_TM, bool gamma_TM_full){
  31. bool pass1 = gamma1.isGammaTrue(counter,gamma_TM,gamma_TM_full,pi_zero_TM,K_star_plus_TM,B_plus_TM);
  32. bool pass2 = gamma2.isGammaTrue(counter,gamma_TM,gamma_TM_full,pi_zero_TM,K_star_plus_TM,B_plus_TM);
  33. bool conv1 = gamma1.isGammaConversion(counter,gamma_TM,pi_zero_TM,K_star_plus_TM);
  34. bool conv2 = gamma2.isGammaConversion(counter,gamma_TM,pi_zero_TM,K_star_plus_TM);
  35. if (pass1 && pass2){
  36. coutDebug("Both gammas passed.");
  37. return 1;
  38. }
  39. else{
  40. if (pass1 || pass2){
  41. if (conv1 || conv2){
  42. coutDebug("One gamma passed, one is converted.");
  43. return 2;
  44. }
  45. else{
  46. coutDebug("One gamma passed, one is random.");
  47. return 4;
  48. }
  49. }
  50. else{
  51. if (conv1 && conv2){
  52. coutDebug("Both gammas are converted.");
  53. return 3;
  54. }
  55. else{
  56. if (conv1 || conv2){
  57. coutDebug("One gamma is converted, one is random.");
  58. return 5;
  59. }
  60. else{
  61. coutDebug("Both gammas failed.");
  62. return 6;
  63. }
  64. }
  65. }
  66. }
  67. }
  68. bool passAllTM(B_plus_IDs B_plus,K_star_IDs K_star,J_psi_IDs J_psi,mu_IDs mu_plus, mu_IDs mu_minus,
  69. K_plus_IDs K_plus,pi_zero_IDs pi_zero,
  70. K_short_IDs K_short,pi_plus_IDs pi_plus,Ks_pi_IDs Ks_pi_plus, Ks_pi_IDs Ks_pi_minus,
  71. counters *counter,double pi_zero_resolved_M, double B_plus_M,
  72. TH1D *h_pi0_mismatched_mass,TH1D *h_Bplus_M_pi0mismatched, TH1D *h_Bplus_M_noPi0Constr,TH1D *h_pi0_resolved_mass,TH1D *h_Bplus_M_pi0random,
  73. bool B_plus_TM, bool K_star_plus_TM, bool K_plus_TM, bool pi_zero_TM, bool mu_TM, bool J_psi_TM, bool pi_zero_TM_full ){
  74. if(!B_plus.isBplus(counter,B_plus_TM)) return false;
  75. coutDebug("B+:\t passed");
  76. if (!K_star.isKstar(counter,K_star_plus_TM, B_plus_TM))return false;
  77. coutDebug("K*:\t passed");
  78. if (!J_psi.isJpsi(counter,J_psi_TM, B_plus_TM))return false;
  79. coutDebug("J/psi:\t passed");
  80. if (!mu_plus.isMu(counter, mu_TM, J_psi_TM, B_plus_TM))return false;
  81. coutDebug("Mu+:\t passed");
  82. if (!mu_minus.isMu(counter, mu_TM, J_psi_TM, B_plus_TM))return false;
  83. coutDebug("Mu-:\t passed");
  84. if (Kst2Kspiplus){
  85. if (!K_short.isKshort(counter)) return false;
  86. if (!pi_plus.isPiPlus(counter)) return false;
  87. if (!Ks_pi_plus.isKsPi(counter)) return false;
  88. if (!Ks_pi_minus.isKsPi(counter)) return false;
  89. }
  90. else if (Kst2Kpluspi0Resolved){
  91. if (!K_plus.isKplus(counter, K_plus_TM, K_star_plus_TM, B_plus_TM))return false;
  92. coutDebug("K+:\t passed");
  93. h_Bplus_M_noPi0Constr->Fill(B_plus_M);
  94. if (pi_zero_TM_full){
  95. if (!pi_zero.isPi0(counter, pi_zero_TM, K_star_plus_TM, B_plus_TM, pi_zero_resolved_M, B_plus_M, h_pi0_mismatched_mass, h_Bplus_M_pi0mismatched, h_pi0_resolved_mass)){
  96. h_Bplus_M_pi0random->Fill(B_plus_M);
  97. return false;
  98. }
  99. else coutDebug("pi0:\t passed");
  100. }
  101. }
  102. return true;
  103. }
  104. Int_t MCtruth(string year = "2011", string magnet = "down", bool ReferenceChannel = false, bool PHSP = false, bool B0 = false, bool K1 = false, bool Inc = false) {
  105. gSystem->Load("./Design_cpp.so");
  106. if(ReferenceChannel && PHSP){
  107. coutWarning("Cannot set boolean of reference channel and phase-space MC at the same time! Process Reference Channel!");
  108. PHSP = false;
  109. }
  110. //Load MC data from preselected TFile
  111. TChain * treeMC = new TChain("DecayTree");
  112. string string_input = "";
  113. if (B0 || K1 || Inc) string_input = GetInputFileBkg(year, magnet, true, ReferenceChannel, B0, K1, Inc);
  114. else string_input = GetInputFile(year,magnet,true,true,ReferenceChannel,PHSP,false);
  115. treeMC->Add(string_input.c_str());
  116. //Save TMed data
  117. string string_output = "";
  118. if (B0 || K1 || Inc) string_output = GetInputFileBkg(year, magnet, true, ReferenceChannel, B0, K1, Inc);
  119. else string_output = GetInputFile(year,magnet,true,true,ReferenceChannel,PHSP,false);
  120. replace(string_output,".root","_TM.root");
  121. TFile * outputFile = outputFile = new TFile(string_output.c_str(), "RECREATE");
  122. Int_t nEvents = treeMC->GetEntries();
  123. if (nEvents==0){
  124. coutERROR("No events in the tree! Exit.");
  125. return 0;
  126. }
  127. //BKGCAT branch
  128. Int_t BKGCAT = -10;
  129. B_plus_IDs B_plus_ID;
  130. K_star_IDs K_star_ID;
  131. J_psi_IDs J_psi_ID(ReferenceChannel);
  132. mu_IDs mu_plus_ID(ReferenceChannel);
  133. mu_IDs mu_minus_ID(ReferenceChannel);
  134. K_plus_IDs K_plus_ID;
  135. pi_zero_IDs pi_zero_ID;
  136. gamma_IDs gamma1_ID;
  137. gamma_IDs gamma2_ID;
  138. K_short_IDs K_short_ID;
  139. pi_plus_IDs pi_plus_ID;
  140. Ks_pi_IDs Ks_pi_plus_ID;
  141. Ks_pi_IDs Ks_pi_minus_ID;
  142. //link variables to branches
  143. if(UseBKGCAT){
  144. treeMC -> SetBranchAddress( "B_plus_BKGCAT" , &BKGCAT );
  145. }
  146. if (Kst2Kpluspi0Resolved){//I need control plots to check pi0 TM and so on
  147. treeMC -> SetBranchAddress( "B_plus_TRUEID" , &B_plus_ID.B_plus_TRUEID );
  148. treeMC -> SetBranchAddress( "K_star_plus_TRUEID" , &K_star_ID.K_star_plus_TRUEID );
  149. treeMC -> SetBranchAddress( "mu_plus_TRUEID" , &mu_plus_ID.mu_TRUEID );
  150. treeMC -> SetBranchAddress( "mu_minus_TRUEID" , &mu_minus_ID.mu_TRUEID );
  151. treeMC -> SetBranchAddress( "K_star_plus_MC_MOTHER_ID" , &K_star_ID.K_star_plus_MOTHER_ID );
  152. treeMC -> SetBranchAddress( "mu_plus_MC_MOTHER_ID" , &mu_plus_ID.mu_MOTHER_ID );
  153. treeMC -> SetBranchAddress( "mu_minus_MC_MOTHER_ID" , &mu_minus_ID.mu_MOTHER_ID );
  154. treeMC -> SetBranchAddress( "mu_plus_MC_GD_MOTHER_ID" , &mu_plus_ID.mu_GD_MOTHER_ID );
  155. treeMC -> SetBranchAddress( "mu_minus_MC_GD_MOTHER_ID" , &mu_minus_ID.mu_GD_MOTHER_ID );
  156. if(Kst2Kpluspi0Resolved){
  157. treeMC -> SetBranchAddress( "B_plus_BKGCAT" , &BKGCAT );
  158. treeMC -> SetBranchAddress( "K_plus_TRUEID" , &K_plus_ID.K_plus_TRUEID );
  159. treeMC -> SetBranchAddress( "pi_zero_resolved_TRUEID" , &pi_zero_ID.pi_zero_TRUEID );
  160. treeMC -> SetBranchAddress( "gamma1_TRUEID" , &gamma1_ID.gamma_TRUEID );
  161. treeMC -> SetBranchAddress( "gamma2_TRUEID" , &gamma2_ID.gamma_TRUEID );
  162. treeMC -> SetBranchAddress( "K_plus_MC_MOTHER_ID" , &K_plus_ID.K_plus_MOTHER_ID );
  163. treeMC -> SetBranchAddress( "pi_zero_resolved_MC_MOTHER_ID", &pi_zero_ID.pi_zero_MOTHER_ID );
  164. treeMC -> SetBranchAddress( "gamma1_MC_MOTHER_ID" , &gamma1_ID.gamma_MOTHER_ID );
  165. treeMC -> SetBranchAddress( "gamma2_MC_MOTHER_ID" , &gamma2_ID.gamma_MOTHER_ID );
  166. treeMC -> SetBranchAddress( "K_plus_MC_GD_MOTHER_ID" , &K_plus_ID.K_plus_GD_MOTHER_ID );
  167. treeMC -> SetBranchAddress( "pi_zero_resolved_MC_GD_MOTHER_ID", &pi_zero_ID.pi_zero_GD_MOTHER_ID );
  168. treeMC -> SetBranchAddress( "gamma1_MC_GD_MOTHER_ID" , &gamma1_ID.gamma_GD_MOTHER_ID );
  169. treeMC -> SetBranchAddress( "gamma2_MC_GD_MOTHER_ID" , &gamma2_ID.gamma_GD_MOTHER_ID );
  170. treeMC -> SetBranchAddress( "gamma1_MC_GD_GD_MOTHER_ID" , &gamma1_ID.gamma_GD_GD_MOTHER_ID );
  171. treeMC -> SetBranchAddress( "gamma2_MC_GD_GD_MOTHER_ID" , &gamma2_ID.gamma_GD_GD_MOTHER_ID );
  172. }
  173. if(Kst2Kspiplus){
  174. treeMC -> SetBranchAddress( "K_short_TRUEID" , &K_short_ID.K_short_TRUEID );
  175. treeMC -> SetBranchAddress( "pi_plus_TRUEID" , &pi_plus_ID.pi_plus_TRUEID );
  176. treeMC -> SetBranchAddress( "Ks_pi_plus_TRUEID" , &Ks_pi_plus_ID.Ks_pi_TRUEID );
  177. treeMC -> SetBranchAddress( "Ks_pi_minus_TRUEID" , &Ks_pi_minus_ID.Ks_pi_TRUEID );
  178. treeMC -> SetBranchAddress( "K_short_MC_MOTHER_ID" , &K_short_ID.K_short_MOTHER_ID );
  179. treeMC -> SetBranchAddress( "pi_plus_MC_MOTHER_ID" , &pi_plus_ID.pi_plus_MOTHER_ID );
  180. treeMC -> SetBranchAddress( "Ks_pi_plus_MC_MOTHER_ID" , &Ks_pi_plus_ID.Ks_pi_MOTHER_ID );
  181. treeMC -> SetBranchAddress( "Ks_pi_minus_MC_MOTHER_ID" ,&Ks_pi_minus_ID.Ks_pi_MOTHER_ID );
  182. treeMC -> SetBranchAddress( "K_short_MC_GD_MOTHER_ID" , &K_short_ID.K_short_GD_MOTHER_ID );
  183. treeMC -> SetBranchAddress( "pi_plus_MC_GD_MOTHER_ID" , &pi_plus_ID.pi_plus_GD_MOTHER_ID );
  184. treeMC -> SetBranchAddress( "Ks_pi_plus_MC_GD_MOTHER_ID" , &Ks_pi_plus_ID.Ks_pi_GD_MOTHER_ID );
  185. treeMC -> SetBranchAddress( "Ks_pi_minus_MC_GD_MOTHER_ID" , &Ks_pi_minus_ID.Ks_pi_GD_MOTHER_ID );
  186. treeMC -> SetBranchAddress( "Ks_pi_plus_MC_GD_GD_MOTHER_ID" , &Ks_pi_plus_ID.Ks_pi_GD_GD_MOTHER_ID );
  187. treeMC -> SetBranchAddress( "Ks_pi_minus_MC_GD_GD_MOTHER_ID" , &Ks_pi_minus_ID.Ks_pi_GD_GD_MOTHER_ID );
  188. }
  189. treeMC -> SetBranchAddress( "J_psi_TRUEID" , &J_psi_ID.J_psi_TRUEID );
  190. treeMC -> SetBranchAddress( "J_psi_MC_MOTHER_ID" , &J_psi_ID.J_psi_MOTHER_ID );
  191. }
  192. Int_t TRUE_events = 0;
  193. //counter for BKGCAT:
  194. Int_t BKGCAT_VETO[20] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  195. Int_t BKGCAT_TRUE[20] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  196. //control plots for pi0
  197. Double_t pi_zero_resolved_M = 0.0;
  198. if (Kst2Kpluspi0Resolved) treeMC -> SetBranchAddress( "pi_zero_resolved_M" , &pi_zero_resolved_M );
  199. //Use either B mass from tuple or from DTF
  200. Double_t B_plus_M;
  201. UseDTF ? treeMC->SetBranchStatus( "B_plus_M_DTF", 1) : treeMC->SetBranchStatus( "B_plus_M", 1);
  202. UseDTF ? treeMC->SetBranchAddress("B_plus_M_DTF", &B_plus_M): treeMC->SetBranchAddress("B_plus_M", &B_plus_M);
  203. //Control pi0 histograms
  204. TH1D * h_pi0_resolved_mass = generalHistogram("h_pi0_resolved_mass" , "m_{#pi^{0}} resolved", 70, 100, 170, "m_{pi^{0}} resolved [MeV]", "Counts / 1MeV", kBlue);
  205. TH1D * h_pi0_mismatched_mass= generalHistogram("h_pi0_mismatched_mass" , "m_{#pi^{0}} IDed as gamma", 70, 100, 170, "m_{pi^{0}}[MeV] MisIDed", "Counts / 1MeV", kRed);
  206. //Control B+ mass for pi0 correctly/wrong/no matched
  207. TH1D * h_Bplus_M_pi0mismatched= BmassHistogram("", "m_{B^{+}}, #pi^{0} not #pi^{0}", "m_{B^{+}}[MeV] mismatched #pi^{0}",kRed);
  208. TH1D * h_Bplus_M_pi0random = BmassHistogram("h_Bplus_M_pi0random", "m_{B^{+}}, #pi^{0} random", "m_{B^{+}}[MeV] random #pi^{0}",kBlue);
  209. TH1D * h_Bplus_M_noPi0Constr = BmassHistogram("h_Bplus_M_noPi0Constr", "m_{B^{+}}, no #pi^{0} TM", "m_{B^{+}}[MeV], no #pi^{0} TM",kBlack);
  210. TH1D * h_Bplus_M_TM_BKGCAT = BmassHistogram("h_Bplus_M_TM_BKGCAT", "m_{B^{+}} TMed", "m_{B^{+}}[MeV] TMed(BKG CAT)",kGreen+2);
  211. TH1D * h_Bplus_M_TMed = BmassHistogram("h_Bplus_M_TMed", "m_{B^{+}} TMed", "m_{B^{+}}[MeV] TMed",kGreen+2);
  212. TH1D * h_Bplus_M_TMed_noPi0 = BmassHistogram("h_Bplus_M_TMed_noPi0", "m_{B^{+}} TMed, no #pi^{0}", "m_{B^{+}}[MeV] TMed, no #pi^{0}",kGreen+2);
  213. TH1D * h_Bplus_M_TMed_noPi0_noBplus = BmassHistogram("h_Bplus_M_TMed_noPi0_noBplus", "m_{B^{+}} TMed, no #pi^{0}, B^{+}", "m_{B^{+}}[MeV] TMed, no #pi^{0}, B^{+}",kGreen+2);
  214. TH1D * h_Bplus_M_TMed_noPi0_noKst_noBplus= BmassHistogram("h_Bplus_M_TMed_noPi0_noKst_noBplus", "m_{B^{+}} TMed, no #pi^{0}, K^{*}, B^{+}", "m_{B^{+}}[MeV] TMed, no #pi^{0}, K^{*}, B^{+}",kGreen+2);
  215. TH1D * h_Bplus_M_notTM_BKGCAT = BmassHistogram("h_Bplus_M_notTM_BKGCAT", "m_{B^{+}} not TMed", "m_{B^{+}}[MeV] not TMed(BKG CAT)",kRed);
  216. TH1D * h_Bplus_M_notTMed = BmassHistogram("h_Bplus_M_notTMed", "m_{B^{+}} not TMed", "m_{B^{+}}[MeV] not TMed",kRed);
  217. TH1D * h_Bplus_M_notTMed_noPi0 = BmassHistogram("h_Bplus_M_notTMed_noPi0", "m_{B^{+}} not TMed, no #pi^{0}", "m_{B^{+}}[MeV] not TMed, no #pi^{0}",kRed);
  218. TH1D * h_Bplus_M_notTMed_noPi0_noBplus = BmassHistogram("h_Bplus_M_notTMed_noPi0_noBplus", "m_{B^{+}} not TMed, no #pi^{0}, B^{+},", "m_{B^{+}}[MeV] not TMed, no #pi^{0}, B^{+}",kRed);
  219. TH1D * h_Bplus_M_notTMed_noPi0_noKst_noBplus= BmassHistogram("h_Bplus_M_notTMed_noPi0_noKst_noBplus", "m_{B^{+}} not TMed, no #pi^{0}, K^{*}, B^{+}", "m_{B^{+}}[MeV] not TMed, no #pi^{0}, K^{*}, B^{+}",kRed);
  220. TH1D * h_Bplus_M_all = BmassHistogram("h_Bplus_M_all", "m_{B^{+}} ", "m_{B^{+}}[MeV] preSel", kBlack);
  221. //TMP empty histogram (filled when running over not pi0 TM)
  222. TH1D * h_Bplus_M_tmp = BmassHistogram("h_Bplus_M_tmp", "m_{B^{+}}", "m_{B^{+}}[MeV]",kBlack);
  223. //control PID plots
  224. TH1D * h_B_plus_TRUEID = new TH1D("h_B_plus_TRUEID", "B^{+} TRUEID", 100000, -1, 99999);
  225. TH1D * h_K_star_plus_TRUEID = new TH1D("h_K_star_plus_TRUEID", "K^{*+} TRUEID", 100000, -1, 99999);
  226. TH1D * h_K_plus_TRUEID = new TH1D("h_K_plus_TRUEID", "K^{+} TRUEID", 100000, -1, 99999);
  227. TH1D * h_pi_zero_TRUEID = new TH1D("h_pi_zero_TRUEID", "#pi^{0} TRUEID", 100000, -1, 99999);
  228. TH1D * h_gamma_TRUEID = new TH1D("h_gamma_TRUEID", "#gamma TRUEID", 100000, -1, 99999);
  229. TTree * treeTruthMatched = treeMC->CloneTree(0);
  230. treeTruthMatched->SetName("DecayTreeTruthMatched");
  231. //If needed to save the eents into a new file (when adding branches such as TMed), uncomment
  232. //TMed branch //0: didn't pass TM,1: passed TM
  233. Int_t iTMed = -1;
  234. treeTruthMatched->Branch("TMed", &iTMed, "iTMed/I");
  235. Int_t iTMedBKGCAT = -1;
  236. treeTruthMatched->Branch("TMedBKGCAT", &iTMedBKGCAT, "iTMedBKGCAT/I");
  237. Int_t iTMed_noPi0 = -1;
  238. if (!KshortChannel) treeTruthMatched->Branch("TMed_noPi0", &iTMed_noPi0, "iTMed_noPi0/I");
  239. Int_t iTMed_noPi0_noBplus = -1;
  240. if (!KshortChannel) treeTruthMatched->Branch("TMed_noPi0_noBplus", &iTMed_noPi0_noBplus, "iTMed_noPi0_noBplus/I");
  241. Int_t iTMed_noPi0_noKst_noBplus = -1;
  242. if (!KshortChannel) treeTruthMatched->Branch("TMed_noPi0_noKst_noBplus", &iTMed_noPi0_noKst_noBplus, "iTMed_noPi0_noKst_noBplus/I");
  243. //1: passed gammas (both), 2: one passed, one converted, 3: both converted, 4: one passed, one random, 5: one converted, one random, 6: both failed
  244. Int_t TM_gammas = -1;
  245. if (!KshortChannel) treeTruthMatched->Branch("TM_gammas", &TM_gammas, "TM_gammas/I");
  246. Int_t TM_gammas_noPi0 = -1;
  247. if (!KshortChannel) treeTruthMatched->Branch("TM_gammas_noPi0", &TM_gammas_noPi0, "TM_gammas_noPi0/I");
  248. Int_t TM_gammas_noPi0_noBplus = -1;
  249. if (!KshortChannel) treeTruthMatched->Branch("TM_gammas_noPi0_noBplus", &TM_gammas_noPi0_noBplus, "TM_gammas_noPi0_noBplus/I");
  250. Int_t TM_gammas_noPi0_noKst_noBplus= -1;
  251. if (!KshortChannel) treeTruthMatched->Branch("TM_gammas_noPi0_noKst_noBplus", &TM_gammas_noPi0_noKst_noBplus, "TM_gammas_noPi0_noKst_noBplus/I");
  252. //initialize VETO counters
  253. counters veto_counters;
  254. counters veto_counters_noPi0;
  255. counters veto_counters_noPi0_noBplus;
  256. counters veto_counters_noPi0_noKst_noBplus;
  257. //MC loop
  258. coutInfo("Truth matching of " + to_string(nEvents) + " MC events for " + year + magnet + ".");
  259. for(int i = 0; i < nEvents; i++){
  260. if(i%2000==0 && i != 0)coutDebug("Loading MC event "+to_string(i) +" / " +to_string(nEvents));
  261. treeMC->GetEntry(i);
  262. h_Bplus_M_all->Fill(B_plus_M);
  263. //Check the BKGCAT category and fill control histograms
  264. if (isBKGCAT(BKGCAT,ReferenceChannel)){
  265. BKGCAT_TRUE[BKGCAT/10]++;
  266. iTMedBKGCAT = 1;
  267. TRUE_events++;
  268. h_Bplus_M_TM_BKGCAT->Fill(B_plus_M);
  269. }
  270. else{
  271. BKGCAT_VETO[BKGCAT/10]++;
  272. iTMedBKGCAT = 0;
  273. h_Bplus_M_notTM_BKGCAT->Fill(B_plus_M);
  274. }
  275. //======================================
  276. //check all particles for correct TRUEID
  277. //======================================
  278. if (B0 || K1){
  279. //TODO
  280. }
  281. else{
  282. if (Inc){
  283. h_B_plus_TRUEID ->Fill(TMath::Abs(B_plus_ID.B_plus_TRUEID));
  284. h_K_star_plus_TRUEID->Fill(TMath::Abs(K_star_ID.K_star_plus_TRUEID));
  285. h_K_plus_TRUEID ->Fill(TMath::Abs(K_plus_ID.K_plus_TRUEID));
  286. h_pi_zero_TRUEID ->Fill(TMath::Abs(pi_zero_ID.pi_zero_TRUEID));
  287. }
  288. iTMed = passAllTM(B_plus_ID,K_star_ID,J_psi_ID,mu_plus_ID,mu_minus_ID,
  289. K_plus_ID,pi_zero_ID,
  290. K_short_ID,pi_plus_ID,Ks_pi_plus_ID,Ks_pi_minus_ID,
  291. &veto_counters, pi_zero_resolved_M, B_plus_M,
  292. h_pi0_mismatched_mass,h_Bplus_M_pi0mismatched, h_Bplus_M_noPi0Constr, h_pi0_resolved_mass, h_Bplus_M_pi0random,
  293. true, true, true, true, true, true, true);
  294. iTMed_noPi0 = passAllTM(B_plus_ID,K_star_ID,J_psi_ID,mu_plus_ID,mu_minus_ID,
  295. K_plus_ID,pi_zero_ID,
  296. K_short_ID,pi_plus_ID,Ks_pi_plus_ID,Ks_pi_minus_ID,
  297. &veto_counters_noPi0, pi_zero_resolved_M, B_plus_M,
  298. h_pi0_mismatched_mass,h_Bplus_M_pi0mismatched, h_Bplus_M_tmp, h_pi0_resolved_mass, h_Bplus_M_tmp,
  299. true, true, true, false, true, true, false);
  300. iTMed_noPi0_noBplus = passAllTM(B_plus_ID,K_star_ID,J_psi_ID,mu_plus_ID,mu_minus_ID,
  301. K_plus_ID,pi_zero_ID,
  302. K_short_ID,pi_plus_ID,Ks_pi_plus_ID,Ks_pi_minus_ID,
  303. &veto_counters_noPi0_noBplus, pi_zero_resolved_M, B_plus_M,
  304. h_pi0_mismatched_mass,h_Bplus_M_pi0mismatched, h_Bplus_M_tmp, h_pi0_resolved_mass, h_Bplus_M_tmp,
  305. false, true, true, false, true, true, true);
  306. iTMed_noPi0_noKst_noBplus= passAllTM(B_plus_ID,K_star_ID,J_psi_ID,mu_plus_ID,mu_minus_ID,
  307. K_plus_ID,pi_zero_ID,
  308. K_short_ID,pi_plus_ID,Ks_pi_plus_ID,Ks_pi_minus_ID,
  309. &veto_counters_noPi0_noKst_noBplus, pi_zero_resolved_M, B_plus_M,
  310. h_pi0_mismatched_mass,h_Bplus_M_pi0mismatched, h_Bplus_M_tmp, h_pi0_resolved_mass, h_Bplus_M_tmp,
  311. false, false, true, false, true, true, true);
  312. }
  313. if (Kst2Kpluspi0Resolved){
  314. TM_gammas = passGammas(gamma1_ID,gamma2_ID,&veto_counters, true,true,true,true,true);
  315. TM_gammas_noPi0 = passGammas(gamma1_ID,gamma2_ID,&veto_counters, true,true,false,true,true);
  316. TM_gammas_noPi0_noBplus = passGammas(gamma1_ID,gamma2_ID,&veto_counters, false,true,false,true,true);
  317. TM_gammas_noPi0_noKst_noBplus =passGammas(gamma1_ID,gamma2_ID,&veto_counters, false,false,false,true,true);
  318. }
  319. if (iTMed) h_Bplus_M_TMed->Fill(B_plus_M);
  320. else h_Bplus_M_notTMed->Fill(B_plus_M);
  321. if (iTMed_noPi0) h_Bplus_M_TMed_noPi0->Fill(B_plus_M);
  322. else h_Bplus_M_notTMed_noPi0->Fill(B_plus_M);
  323. if (iTMed_noPi0_noBplus) h_Bplus_M_TMed_noPi0_noBplus->Fill(B_plus_M);
  324. else h_Bplus_M_notTMed_noPi0_noBplus->Fill(B_plus_M);
  325. if (iTMed_noPi0_noKst_noBplus) h_Bplus_M_TMed_noPi0_noKst_noBplus->Fill(B_plus_M);
  326. else h_Bplus_M_notTMed_noPi0_noKst_noBplus->Fill(B_plus_M);
  327. treeTruthMatched->Fill();
  328. }//loop over events end
  329. //output truthmatching results
  330. coutInfo("Resulting TruthMatching data: " + to_string(TRUE_events) + "/" + to_string(nEvents) + " are found to be correctly reconstructed!" );
  331. coutInfo("For the sample: " + year + " " + magnet + ":\t" + (ReferenceChannel ? "JpsiChannel" : (PHSP ? "PHSP" : "Signal")) + " MC events" );
  332. coutInfo("The following BKGCAT have been found in the sample:" );
  333. coutInfo("BKGCAT\tAccepted\tRejected\tTotal");
  334. for(int i = 0; i < 20; i++){
  335. if(BKGCAT_VETO[i] != 0 || BKGCAT_TRUE[i] != 0){
  336. coutInfo(to_string(i*10) + "\t" + to_string(BKGCAT_TRUE[i]) + "\t\t" + to_string(BKGCAT_VETO[i]) + "\t\t" + to_string(BKGCAT_TRUE[i]+BKGCAT_VETO[i]) );
  337. }
  338. }
  339. veto_counters.printCounters(B0,K1);
  340. veto_counters_noPi0.printCounters(false,false);
  341. veto_counters_noPi0_noBplus.printCounters(false,false);
  342. veto_counters_noPi0_noKst_noBplus.printCounters(false,false);
  343. if(!outputFile->IsOpen()){
  344. coutERROR("ile was not opened succesfully!");
  345. return 0;
  346. }
  347. outputFile->cd();
  348. treeTruthMatched->Write("",TObject::kWriteDelete);
  349. if (Kst2Kpluspi0Resolved){
  350. //Save hists into a file
  351. string path = GetInputFile(year,magnet,true,true,ReferenceChannel,PHSP, false);
  352. replace(path,".root", "_TMcontrolPlots.root");
  353. TFile *controlHists = new TFile(path.c_str(),"RECREATE");
  354. gROOT->SetBatch(kTRUE);
  355. controlHists->cd();
  356. h_Bplus_M_pi0mismatched->Write("",TObject::kWriteDelete);
  357. h_Bplus_M_pi0random->Write("",TObject::kWriteDelete);
  358. h_Bplus_M_noPi0Constr->Write("",TObject::kWriteDelete);
  359. h_Bplus_M_TM_BKGCAT->Write("",TObject::kWriteDelete);
  360. h_Bplus_M_notTM_BKGCAT->Write("",TObject::kWriteDelete);
  361. h_Bplus_M_TMed->Write("",TObject::kWriteDelete);
  362. h_Bplus_M_notTMed->Write("",TObject::kWriteDelete);
  363. h_Bplus_M_TMed_noPi0->Write("",TObject::kWriteDelete);
  364. h_Bplus_M_notTMed_noPi0->Write("",TObject::kWriteDelete);
  365. h_Bplus_M_TMed_noPi0_noBplus->Write("",TObject::kWriteDelete);
  366. h_Bplus_M_notTMed_noPi0_noBplus->Write("",TObject::kWriteDelete);
  367. h_Bplus_M_TMed_noPi0_noKst_noBplus->Write("",TObject::kWriteDelete);
  368. h_Bplus_M_notTMed_noPi0_noKst_noBplus->Write("",TObject::kWriteDelete);
  369. h_Bplus_M_all->Write("",TObject::kWriteDelete);
  370. if (Inc){
  371. h_B_plus_TRUEID->Write("",TObject::kWriteDelete);
  372. h_K_star_plus_TRUEID->Write("",TObject::kWriteDelete);
  373. h_K_plus_TRUEID->Write("",TObject::kWriteDelete);
  374. h_pi_zero_TRUEID->Write("",TObject::kWriteDelete);
  375. h_gamma_TRUEID->Write("",TObject::kWriteDelete);
  376. }
  377. h_pi0_mismatched_mass->Write("",TObject::kWriteDelete);
  378. h_pi0_resolved_mass->Write("",TObject::kWriteDelete);
  379. //Draw and save
  380. TM_canvas("Check_pi0", h_pi0_resolved_mass,h_pi0_mismatched_mass, h_pi0_mismatched_mass, year,ReferenceChannel,PHSP, B0, K1, Inc);
  381. TM_canvas("BKGCAT", h_Bplus_M_all,h_Bplus_M_TM_BKGCAT, h_Bplus_M_notTM_BKGCAT, year,ReferenceChannel,PHSP, B0, K1, Inc);
  382. TM_canvas("TMed", h_Bplus_M_all,h_Bplus_M_TMed, h_Bplus_M_notTMed, year,ReferenceChannel,PHSP, B0, K1, Inc);
  383. TM_canvas("TMed_noPi0", h_Bplus_M_all,h_Bplus_M_TMed_noPi0, h_Bplus_M_notTMed_noPi0, year,ReferenceChannel,PHSP, B0, K1, Inc);
  384. TM_canvas("TMed_noPi0_noBplus", h_Bplus_M_all,h_Bplus_M_TMed_noPi0_noBplus, h_Bplus_M_notTMed_noPi0_noBplus, year,ReferenceChannel,PHSP, B0, K1, Inc);
  385. TM_canvas("TMed_noPi0_noKst_noBplus", h_Bplus_M_all,h_Bplus_M_TMed_noPi0_noKst_noBplus,h_Bplus_M_notTMed_noPi0_noKst_noBplus, year,ReferenceChannel,PHSP, B0, K1, Inc);
  386. controlHists->Close();
  387. }
  388. delete h_Bplus_M_pi0mismatched;
  389. delete h_Bplus_M_pi0random;
  390. delete h_Bplus_M_noPi0Constr;
  391. delete h_Bplus_M_all;
  392. delete h_Bplus_M_TM_BKGCAT;
  393. delete h_Bplus_M_notTM_BKGCAT;
  394. delete h_Bplus_M_TMed;
  395. delete h_Bplus_M_notTMed;
  396. delete h_Bplus_M_TMed_noPi0;
  397. delete h_Bplus_M_notTMed_noPi0;
  398. delete h_Bplus_M_TMed_noPi0_noBplus;
  399. delete h_Bplus_M_notTMed_noPi0_noBplus;
  400. delete h_Bplus_M_TMed_noPi0_noKst_noBplus;
  401. delete h_Bplus_M_notTMed_noPi0_noKst_noBplus;
  402. delete h_B_plus_TRUEID;
  403. delete h_K_star_plus_TRUEID;
  404. delete h_K_plus_TRUEID;
  405. delete h_pi_zero_TRUEID;
  406. delete h_gamma_TRUEID;
  407. delete h_pi0_mismatched_mass;
  408. delete h_pi0_resolved_mass;
  409. delete treeMC;
  410. delete treeTruthMatched;
  411. outputFile->Close();
  412. string command = "mv " + string_input + " ";
  413. replace(string_input,".root","_TMbackup.root");
  414. command += string_input;
  415. system(command.c_str());
  416. replace(string_input,"_TMbackup.root",".root");
  417. command = "mv " + string_output + " " + string_input;
  418. system(command.c_str());
  419. coutInfo("Finished Truth-Matching for sample: " + year + " " + magnet + " : " + TheDecay + (ReferenceChannel ? " RefMC" : (PHSP ? " PHSP MC" : " ") ) );
  420. return 1;
  421. }
  422. bool restore_from_backup(string year = "2011", string magnet = "down", bool ReferenceChannel = false, bool PHSP = false, bool B0 = false, bool K1 = false, bool Inc = false){
  423. //In case one needs to rerun the TruthMatching
  424. string string_input = "";
  425. if (B0 || K1 || Inc) string_input = GetInputFileBkg(year, magnet, true, ReferenceChannel, B0, K1, Inc);
  426. else string_input = GetInputFile(year,magnet,true,true,ReferenceChannel,PHSP,false);
  427. replace(string_input,".root","_TMbackup.root");
  428. string command = "mv " + string_input + " ";
  429. replace(string_input,"_TMbackup.root",".root");
  430. command += string_input;
  431. coutDebug(command);
  432. system(command.c_str());
  433. coutInfo("Succesfully restored all MC for " + TheDecay + (ReferenceChannel ? " (Reference channel)" : "") + " in year " + year);
  434. return 1;
  435. }
  436. bool restore_allyears_from_backup(int Run = 1, bool Reference = false, bool PHSP = false, bool B0 = false, bool K1 = false, bool Inc = false){
  437. std::vector<string> years = (B0 || K1 || Inc) ? yearsBkgMC(Reference, B0, K1, Inc, Run) : yearsMC(Reference,PHSP,Run);
  438. if (years.empty()){
  439. coutERROR("Invalid Run number given: " + to_string(Run) + ". Exit program!");
  440. return 0;
  441. }
  442. for(unsigned int y = 0; y < years.size(); y++){
  443. if(restore_from_backup(years.at(y),"down", Reference, PHSP, B0, K1, Inc) == 0){
  444. coutERROR("Failed to restore " + years.at(y) + " magnet DOWN for " + TheDecay + getDataTypeTag(true, Reference, PHSP, B0, K1, Inc) + " from backup");
  445. return 0;
  446. }
  447. if(restore_from_backup(years.at(y),"up", Reference, PHSP, B0, K1, Inc) == 0){
  448. coutERROR("Failed to restore " + years.at(y) + " magnet UP for " + TheDecay + getDataTypeTag(true, Reference, PHSP, B0, K1, Inc) +" from backup");
  449. return 0;
  450. }
  451. coutInfo("Sucesfully restored " + years.at(y) + " magnet UP for " + TheDecay + getDataTypeTag(true, Reference, PHSP, B0, K1, Inc) +" from backup");
  452. }
  453. return 1;
  454. }
  455. bool restore_all_from_backup(){
  456. if (restore_allyears_from_backup(12, false, false, false, false, false) ==0){
  457. coutERROR("Failed to restore signal MC from backup!");
  458. return 0;
  459. }
  460. if (restore_allyears_from_backup(12, true, false, false, false, false) ==0){
  461. coutERROR("Failed to restore reference MC from backup!");
  462. return 0;
  463. }
  464. if (restore_allyears_from_backup(12, false, true, false, false, false) ==0){
  465. coutERROR("Failed to restore PHSP MC from backup!");
  466. return 0;
  467. }
  468. coutInfo("Succesfully restored all MC for alls samples!");
  469. return 1;
  470. }
  471. Int_t TruthMatchAllBkg(bool Reference = false, Int_t Run = 1, bool B0 = false, bool K1 = false, bool Inc = false){
  472. std::vector<string> years = yearsBkgMC(Reference,B0,K1,Inc,Run);
  473. if (years.empty()){
  474. coutERROR("Invalid Run number given: " + to_string(Run) + ". Exit program!");
  475. return 0;
  476. }
  477. for(unsigned int y = 0; y < years.size(); y++){
  478. if(MCtruth(years.at(y),"down", Reference, false, B0, K1, Inc) == 0){
  479. coutERROR("Failed to process " + years.at(y) + " magnet DOWN for " + TheDecay+ getDataTypeTag(true, Reference, false, B0, K1, Inc) + " from backup");
  480. return 0;
  481. }
  482. if(MCtruth(years.at(y),"up", Reference, false, B0, K1, Inc) == 0){
  483. coutERROR("Failed to process " + years.at(y) + " magnet UP for " + TheDecay + getDataTypeTag(true, Reference, false, B0, K1, Inc) + " from backup");
  484. return 0;
  485. }
  486. }
  487. coutInfo("Succesfully finished all truth-matching for " + TheDecay + getDataTypeTag(true, Reference, false, B0, K1, Inc) + " in Run" + to_string(Run));
  488. return 1;
  489. }
  490. Int_t TruthMatchAll(bool Reference = false, bool PHSP = false, Int_t Run = 1){
  491. std::vector<string> years = yearsMC(Reference,PHSP,Run);
  492. if (years.empty()){
  493. coutERROR("Invalid Run number given: " + to_string(Run) + ". Exit program!");
  494. return 0;
  495. }
  496. for(unsigned int y = 0; y < years.size(); y++){
  497. if(MCtruth(years.at(y),"down", Reference, PHSP, false, false, false) == 0){
  498. coutERROR("Failed to process " + years.at(y) + " magnet DOWN for " + TheDecay + getDataTypeTag(true, Reference, PHSP, false, false, false) + " from backup");
  499. return 0;
  500. }
  501. if(MCtruth(years.at(y),"up", Reference, PHSP, false, false, false) == 0){
  502. coutERROR("Failed to process " + years.at(y) + " magnet UP for " + TheDecay + getDataTypeTag(true, Reference, PHSP, false, false, false) + " from backup");
  503. return 0;
  504. }
  505. }
  506. coutInfo("Succesfully finished all truth-matching for " + TheDecay + getDataTypeTag(true, Reference, PHSP, false, false, false) + " in Run" + to_string(Run));
  507. return 1;
  508. }
  509. Int_t TruthMatchAllAll(Int_t Run){
  510. if (TruthMatchAll(false, false, Run) ==0) return 0;
  511. if (TruthMatchAll(true, false, Run) ==0) return 0;
  512. if (TruthMatchAll(false, true, Run) ==0) return 0;
  513. return 1;
  514. }
  515. //This is a reliq from the TM issue we had back in the day in DaVinci, not really used now
  516. Int_t CombinePolarityPi0MisID(string year ="2011", bool ReferenceChannel = false, bool PHSP = false){
  517. gROOT->SetBatch(kTRUE);
  518. string filePath = "";
  519. //Define combined TH1 and make it pretty
  520. TH1D * h_Bplus_M_pi0mismatched= new TH1D("h_Bplus_M_pi0mismatched" , "m_{B^{+}}, #pi^{0} not TMed", 100, cut_B_plus_M_low, cut_B_plus_M_high);
  521. TCanvas *canvas_B = new TCanvas("canvas", "#pi^0 TM check", 10,10, 1200,600);
  522. design_canvas_Bmass(canvas_B,"#pi^0 TM check");
  523. int counts = (cut_B_plus_M_high-cut_B_plus_M_low)/100;
  524. design_lines(h_Bplus_M_pi0mismatched,"m_{B^{+}}, #pi^{0} not TMed","Counts / " + to_string(counts)+"MeV",kRed+1);
  525. h_Bplus_M_pi0mismatched->GetYaxis()->SetTitleOffset(0.7);
  526. string Magnets[] = {"down", "up"};
  527. //Loop over polarities and load the histograms
  528. for(string & magnet : Magnets){ //loop over both magnet polaritys
  529. string path = GetInputFile(year,magnet,true,true,ReferenceChannel,PHSP, false);
  530. replace(path,".root", "_TMcontrolPlots.root");
  531. TFile *file = TFile::Open(path.c_str());
  532. TH1D *tmp = (TH1D*)file->Get("h_Bplus_M_pi0mismatched");
  533. h_Bplus_M_pi0mismatched->Add(tmp);
  534. file->Close();
  535. }
  536. //Save the combined TH1 in a canvas
  537. canvas_B->cd();
  538. h_Bplus_M_pi0mismatched->Draw();
  539. filePath = "./../../data/figures/pi0TM/" + year + (ReferenceChannel ? "_Ref":"") + (PHSP ? "_PHSP":"") + "_Bmass";
  540. string adr = filePath + ".eps";
  541. canvas_B->Print(adr.c_str(), "eps");
  542. adr = filePath + ".root";
  543. canvas_B->Print(adr.c_str(), "root");
  544. return 1;
  545. }
  546. //Also obsolete (see one function up)
  547. Int_t CombineYearsPi0MisID(Int_t Run =1, bool ReferenceChannel = false, bool PHSP = false){
  548. gROOT->SetBatch(kTRUE);
  549. string filePath = "";
  550. //Define combined TH1 and make it pretty
  551. TH1D * h_Bplus_M_pi0mismatched= new TH1D("h_Bplus_M_pi0mismatched" , "m_{B^{+}}, #pi^{0} not TMed", 100, cut_B_plus_M_low, cut_B_plus_M_high);
  552. TCanvas *canvas_B = new TCanvas("canvas", "#pi^0 TM check", 10,10, 1200,600);
  553. design_canvas_Bmass(canvas_B,"#pi^0 TM check");
  554. int counts = (cut_B_plus_M_high-cut_B_plus_M_low)/100;
  555. design_lines(h_Bplus_M_pi0mismatched,"m_{B^{+}}, #pi^{0} not TMed","Counts / " + to_string(counts)+"MeV",kRed+1);
  556. h_Bplus_M_pi0mismatched->GetYaxis()->SetTitleOffset(0.7);
  557. //Loop over years and polarities, load the histograms
  558. std::vector<string> years;
  559. if(Run == 1){
  560. years.push_back("2011");
  561. years.push_back("2012");
  562. }
  563. if(Run == 2){
  564. years.push_back("2015");
  565. years.push_back("2016");
  566. if(Use2017 && !ReferenceChannel)years.push_back("2017");
  567. if(Use2018 && !ReferenceChannel)years.push_back("2018");
  568. }
  569. string Magnets[] = {"down", "up"};
  570. for(unsigned int y = 0; y < years.size(); y++){ //loop over all years
  571. for(string & magnet : Magnets){ //loop over both magnet polaritys
  572. string path = GetInputFile(years.at(y),magnet,true,true,ReferenceChannel,PHSP, false);
  573. replace(path,".root", "_TMcontrolPlots.root");
  574. TFile *file = TFile::Open(path.c_str());
  575. TH1D *tmp = (TH1D*)file->Get("h_Bplus_M_pi0mismatched");
  576. h_Bplus_M_pi0mismatched->Add(tmp);
  577. file->Close();
  578. }
  579. }
  580. //Save the combined TH1 in a canvas
  581. canvas_B->cd();
  582. h_Bplus_M_pi0mismatched->Draw();
  583. filePath = "./../../data/figures/pi0TM/Run" + to_string(Run) + (ReferenceChannel ? "_Ref":"") + (PHSP ? "_PHSP":"") + "_Bmass";
  584. string adr = filePath + ".eps";
  585. canvas_B->Print(adr.c_str(), "eps");
  586. adr = filePath + ".root";
  587. canvas_B->Print(adr.c_str(), "root");
  588. return 1;
  589. }