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.

766 lines
32 KiB

  1. //Functions used to obtain various efficiencies in the K+pi0 channel
  2. //Renata Kopecna
  3. #include "GlobalFunctions.hh"
  4. #include "EfficiencyClass.cpp"
  5. #include "Design.hpp"
  6. #include "Paths.hpp"
  7. #include "Utils.hpp"
  8. using namespace std;
  9. using namespace RooFit;
  10. using namespace RooStats;
  11. double defaultBDTstep = 0.005;
  12. // Small helper function
  13. //----------------------------------------------------------------------------------------------------------
  14. Double_t getLowBDTcut(bool fineScan){
  15. if (fineScan) return 0.9;
  16. else return (TMVAmethod == "MLP") ? 0.0 : -1.0;
  17. }
  18. TH1D *convertTGraph(TGraphErrors *effGraph){
  19. double *x, *y;
  20. double *ex, *ey;
  21. x = effGraph->GetX();
  22. y = effGraph->GetY();
  23. ex = effGraph->GetEX();
  24. ey = effGraph->GetEY();
  25. int N = effGraph->GetN();
  26. double binEdges[N+1];
  27. for (int i = 0; i<N; i++){
  28. binEdges[i] = x[i]-ex[i];
  29. coutDebug(to_string(binEdges[i]));
  30. }
  31. binEdges[N] = x[N-1]+ex[N-1];
  32. coutDebug(to_string(binEdges[N]));
  33. TH1D *hist = new TH1D("hist","hist",N,binEdges);
  34. for (int b = 0; b < N; b++){
  35. hist->SetBinContent(b+1,y[b]);
  36. hist->SetBinError(b,ey[b]);
  37. }
  38. hist->Draw();
  39. return hist;
  40. }
  41. //Get L0Muon efficiency
  42. //----------------------------------------------------------------------------------------------------------
  43. TH1D * getL0MuonEfficiency(string year, string magnet, bool MC, bool ReferenceChannel, bool PHSP, bool useDimuon){
  44. gStyle -> SetOptStat(0);
  45. gROOT->SetBatch(kTRUE);
  46. TH1::SetDefaultSumw2(kTRUE);
  47. if (PHSP) MC = true;
  48. if (ReferenceChannel) MC = true;
  49. //Load all files
  50. TChain* tree = new TChain("DecayTree");
  51. if (magnet == "both"){
  52. string input_path = GetInputFile(year,"down",true,MC,ReferenceChannel,PHSP,false);
  53. replace(input_path,".root","_L0Muon.root");
  54. tree->Add(input_path.c_str());
  55. replace(input_path,"down","up");
  56. tree->Add(input_path.c_str());
  57. }
  58. else{
  59. string input_path = GetInputFile(year,magnet,true,MC,ReferenceChannel,PHSP,false);
  60. replace(input_path,".root","_L0Muon.root");
  61. tree->Add(input_path.c_str());
  62. }
  63. //activate all branches
  64. tree->SetBranchStatus("*",1);
  65. //Draw the histograms
  66. Double_t xEdges[16] = {0.0,1950.0,2254.25,2522.0,
  67. 2787.75,3061.75,3346.0,3658.75,
  68. 4003.75,4401.75,4868.5,5444.0,
  69. 6190.25,7275.25,9257.25,20000.0};
  70. TH1D *mu_max_all = new TH1D ("mu_max_all","mu_max_all",15,xEdges);
  71. TH1D *mu_max_pass = new TH1D ("mu_max_pass","mu_max_pass",15,xEdges);
  72. //In RunI nSPDHits < 600
  73. string cut = "max(mu_plus_PT,mu_minus_PT)< 20000 && (mu_plus_ProbNNmu > 0.25 && mu_minus_ProbNNmu > 0.25) && nSPDHits < 450";
  74. if (year == "2011" || year == "2012") replace(cut,"450","600");
  75. tree->Draw("max(mu_plus_PT,mu_minus_PT)>>mu_max_all",cut.c_str());
  76. if (useDimuon) cut = cut + " && (B_plus_L0MuonDecision_TOS==1 || B_plus_L0DiMuonDecision_TOS==1 )";
  77. else cut = cut + "&& B_plus_L0MuonDecision_TOS==1";
  78. tree->Draw("max(mu_plus_PT,mu_minus_PT)>>mu_max_pass",cut.c_str());
  79. //Add true muon pt possibly for MC
  80. mu_max_all = (TH1D*)gDirectory->Get("mu_max_all");
  81. mu_max_pass = (TH1D*)gDirectory->Get("mu_max_pass");
  82. string sMC = ( ReferenceChannel ? "RefMC" : (PHSP ? "PHSP" : "MC") );
  83. string L0MuonEffName = "L0MuonEff" + (MC ? sMC : "");
  84. TH1D *L0MuonEff = (TH1D*)mu_max_all->Clone(L0MuonEffName.c_str());
  85. L0MuonEff->Divide(mu_max_pass,mu_max_all);
  86. delete mu_max_all;
  87. delete mu_max_pass;
  88. return L0MuonEff;
  89. }
  90. int getAllL0MuonEfficiencies(string year, string magnet, bool useDimuon){
  91. TH1D *L0MuonEffData = getL0MuonEfficiency(year,magnet,false, false, false,useDimuon);
  92. TH1D *L0MuonEffMC = getL0MuonEfficiency(year,magnet,true, false, false,useDimuon);
  93. TH1D *L0MuonEffRefMC = getL0MuonEfficiency(year,magnet,false, true, false,useDimuon);
  94. TH1D *L0MuonEffPHSP = getL0MuonEfficiency(year,magnet,false, false, true,useDimuon);
  95. TH1D *L0MuonEffRatioMC = (TH1D*)L0MuonEffData->Clone("L0MuonEffRatioMC");
  96. TH1D *L0MuonEffRatioRefMC = (TH1D*)L0MuonEffData->Clone("L0MuonEffRatioRefMC");
  97. TH1D *L0MuonEffRatioPHSP = (TH1D*)L0MuonEffData->Clone("L0MuonEffRatioPHSP");
  98. L0MuonEffRatioMC->Divide(L0MuonEffMC);
  99. L0MuonEffRatioRefMC->Divide(L0MuonEffRefMC);
  100. L0MuonEffRatioPHSP->Divide(L0MuonEffPHSP);
  101. string output_path = GetEfficiencyFile("L0Muon",year,magnet,false,false,false,false,false,false,false,false,useDimuon ? "DiMuon" : string(""));
  102. TFile *output = new TFile(output_path.c_str(),"RECREATE");
  103. output->cd();
  104. TCanvas *c_plot = designL0MuonEfficiencies(year, magnet, L0MuonEffData,L0MuonEffPHSP,L0MuonEffRatioPHSP,useDimuon ? "DiMuon_PHSP" :"PHSP");
  105. TCanvas *c_plotMC = designL0MuonEfficiencies(year, magnet, L0MuonEffData,L0MuonEffMC,L0MuonEffRatioMC,useDimuon ? "DiMuon_MC" :"MC");
  106. TCanvas *c_plotRefMC = designL0MuonEfficiencies(year, magnet, L0MuonEffData,L0MuonEffRefMC,L0MuonEffRatioRefMC,useDimuon ? "DiMuon_RefMC" :"RefMC");
  107. L0MuonEffData->Write();
  108. L0MuonEffMC->Write();
  109. L0MuonEffRefMC->Write();
  110. L0MuonEffPHSP->Write();
  111. L0MuonEffRatioMC->Write();
  112. L0MuonEffRatioRefMC->Write();
  113. L0MuonEffRatioPHSP->Write();
  114. c_plot->Write();
  115. c_plotMC->Write();
  116. c_plotRefMC->Write();
  117. delete L0MuonEffData;
  118. delete L0MuonEffMC;
  119. delete L0MuonEffRefMC;
  120. delete L0MuonEffPHSP;
  121. delete L0MuonEffRatioMC;
  122. delete L0MuonEffRatioRefMC;
  123. delete L0MuonEffRatioPHSP;
  124. delete c_plot;
  125. delete c_plotMC;
  126. delete c_plotRefMC;
  127. output->Close();
  128. return 1;
  129. }
  130. int getAllL0MuonEfficienciesAllYears(int Run, string magnet, bool useDimuon){
  131. for (auto& year : yearsMC(false,true,Run)){
  132. if (getAllL0MuonEfficiencies(year,magnet,useDimuon)==0) return 0;
  133. }
  134. return 1;
  135. }
  136. //----------------------------------------------------------------------------------------------------------
  137. //----------------------------------------------------------------------------------------------------------
  138. //BDT scans
  139. //----------------------------------------------------------------------------------------------------------
  140. int ScanBDTEfficiency(string year, string magnet, Double_t BDTstep,
  141. int Run, bool UseOnlyMuMuEvents, bool PHSP,
  142. bool KshortDecaysInVelo, bool IncludeMultipleEff,
  143. bool weighted){
  144. //Get low TMVA response lower boundary
  145. Double_t lowBDTcut = getLowBDTcut(false);
  146. TGraphErrors *effGraph = new TGraphErrors();
  147. gROOT->SetBatch(); //ROOT stops plotting canvases
  148. //loop over BDT cuts
  149. for(double fBDTcut = lowBDTcut+BDTstep; fBDTcut < 1.0; fBDTcut += BDTstep){
  150. EffAndError efficiency = EffAndError(); //put zeroes everywhere
  151. efficiency = getBDTEfficiencySimple(year,fBDTcut,Run,UseOnlyMuMuEvents,PHSP,KshortDecaysInVelo,IncludeMultipleEff,weighted,"",-1);
  152. //Save the efficiency
  153. effGraph->SetPoint(effGraph->GetN(), fBDTcut, efficiency.value);
  154. effGraph->SetPointError(effGraph->GetN()-1,0,efficiency.highError);
  155. coutDebug("---"+string(TMVAmethod)+" cut "+to_string(fBDTcut)+" efficiency: "+to_string(efficiency.value)+"+-"+to_string(efficiency.highError));
  156. }
  157. //Write to file and close
  158. string path = GetEfficiencyFile("BDT",year,magnet,Run,!UseOnlyMuMuEvents&&!PHSP, UseOnlyMuMuEvents,PHSP, KshortDecaysInVelo, IncludeMultipleEff,weighted, false, false,"");
  159. TFile * effFile = new TFile(path.c_str(), "RECREATE");
  160. coutInfo("Writting into a file " + path);
  161. effFile->cd();
  162. string effName ="";
  163. if (IncludeMultipleEff) effName = "{"+string(TMVAmethod)+"}^{Removed multiple}";
  164. else effName = "{"+string(TMVAmethod)+"}";
  165. string title ="";
  166. if (Run ==0) title = string(TMVAmethod)+"effScan_"+year;
  167. else title = string(TMVAmethod)+"effScan_Run" + to_string(Run);
  168. designBasicEfficiency(effGraph,title,string(TMVAmethod)+" response",effName,0.5,1.025);
  169. effGraph->Write("",TObject::kWriteDelete);
  170. TCanvas * c1 = new TCanvas("c1", "c1");
  171. c1->cd();
  172. effGraph->Draw("ap");
  173. addYearTag(0.25,0.55,year, Run, 1, 1.3);
  174. addAnyTag(0.25,0.45,getDataTypeTag(true,!UseOnlyMuMuEvents&&!PHSP,PHSP) ,1,1.3);
  175. replace(path,".root",".eps");
  176. c1->SaveAs(path.c_str(),"eps");
  177. effFile->Close();
  178. return 1;
  179. }
  180. int ScanBDTEfficiencyAllYears(bool UseOnlyMuMuEvents, bool PHSP, bool weighted,bool IncludeMultipleEff){
  181. for (auto yr: yearsMC(!UseOnlyMuMuEvents&&!PHSP,PHSP,12)){
  182. if (ScanBDTEfficiency(yr,"both",defaultBDTstep,0,UseOnlyMuMuEvents,PHSP,false,IncludeMultipleEff,weighted)==0) return 0;
  183. if (KshortChannel)
  184. if (ScanBDTEfficiency(yr,"both",defaultBDTstep,0,UseOnlyMuMuEvents,PHSP,true,IncludeMultipleEff,weighted)==0) return 0;
  185. }
  186. return 1;
  187. }
  188. int ScanMultipleCandidatesEfficiencyAllYearsAllSamples(bool weighted,bool IncludeMultipleEff){
  189. if (ScanBDTEfficiencyAllYears(true, false, weighted, IncludeMultipleEff)==0) return 0;
  190. if (ScanBDTEfficiencyAllYears(false, false, weighted, IncludeMultipleEff)==0) return 0;
  191. if (ScanBDTEfficiencyAllYears(false, true, weighted, IncludeMultipleEff)==0) return 0;
  192. return 1;
  193. }
  194. int ScanBDTEfficiencyAllSig (int Run, Double_t BDTstep, bool SplitByYears, bool RemoveMultiple, bool weighted){
  195. bool KshortDecaysInVelo = false;
  196. if(Run != 1 && Run != 2 && Run != 12){
  197. std::cout << "[ERROR]\t\tInvalid Run ID given: " << Run << ". Exit programm!" << std::endl;
  198. return 0;
  199. }
  200. if (SplitByYears){
  201. for (auto& year : yearsMC(false,false,Run))
  202. if (ScanBDTEfficiency(year,"both",BDTstep,0,true,false, KshortDecaysInVelo, RemoveMultiple, weighted)==0) return 0;
  203. }
  204. else{
  205. if (Run == 1 || Run == 12) if (ScanBDTEfficiency("2011","both",BDTstep,1,true, false, KshortDecaysInVelo, RemoveMultiple, weighted)==0) return 0;
  206. if (Run == 2 || Run == 12) if (ScanBDTEfficiency("2016","both",BDTstep,2,true, false, KshortDecaysInVelo, RemoveMultiple, weighted)==0) return 0;
  207. }
  208. return 1;
  209. }
  210. int ScanBDTEfficiencyAllPHSP(int Run, Double_t BDTstep, bool SplitByYears, bool RemoveMultiple, bool weighted){
  211. bool KshortDecaysInVelo = false;
  212. if(Run != 1 && Run != 2 && Run != 12){
  213. std::cout << "[ERROR]\t\tInvalid Run ID given: " << Run << ". Exit programm!" << std::endl;
  214. return 0;
  215. }
  216. if (SplitByYears){
  217. for (auto& year : yearsMC(false,true,Run))
  218. if (ScanBDTEfficiency(year,"both",BDTstep,0,true,true, KshortDecaysInVelo, RemoveMultiple, weighted)==0) return 0; //TODO: check if PHSP efficiency should be for mumu or overall!
  219. }
  220. else{
  221. if (ScanBDTEfficiency("2011","both",BDTstep,1,true, true, KshortDecaysInVelo, RemoveMultiple, weighted)==0) return 0;
  222. if (ScanBDTEfficiency("2016","both",BDTstep,2,true, true, KshortDecaysInVelo, RemoveMultiple, weighted)==0) return 0;
  223. }
  224. return 1;
  225. }
  226. int ScanBDTEfficiencyAllRef (int Run, Double_t BDTstep, bool SplitByYears, bool RemoveMultiple, bool weighted){
  227. bool KshortDecaysInVelo = false;
  228. if (SplitByYears){
  229. for (auto& year : yearsMC(true,false,Run))
  230. if (ScanBDTEfficiency(year,"both",BDTstep,0,false, false, KshortDecaysInVelo, RemoveMultiple, weighted)==0) return 0;
  231. }
  232. else{
  233. if (ScanBDTEfficiency("2011","both",BDTstep,1,false, false, KshortDecaysInVelo, RemoveMultiple, weighted)==0) return 0;
  234. if (ScanBDTEfficiency("2016","both",BDTstep,2,false, false, KshortDecaysInVelo, RemoveMultiple, weighted)==0) return 0;
  235. }
  236. return 1;
  237. }
  238. int ScanBDTEfficiencyAll(){
  239. ScanBDTEfficiencyAllSig(1,0.001,true,true,true);
  240. ScanBDTEfficiencyAllSig(2,0.001,true,true,true);
  241. ScanBDTEfficiencyAllSig(1,0.001,false,true,true);
  242. ScanBDTEfficiencyAllSig(2,0.001,false,true,true);
  243. ScanBDTEfficiencyAllRef(1,0.001,true,true,true);
  244. ScanBDTEfficiencyAllRef(2,0.001,true,true,true);
  245. ScanBDTEfficiencyAllRef(1,0.001,false,true,true);
  246. ScanBDTEfficiencyAllRef(2,0.001,false,true,true);
  247. ScanBDTEfficiencyAllPHSP(1,0.001,true,true,true);
  248. ScanBDTEfficiencyAllPHSP(2,0.001,true,true,true);
  249. ScanBDTEfficiencyAllPHSP(1,0.001,false,true,true);
  250. ScanBDTEfficiencyAllPHSP(2,0.001,false,true,true);
  251. return 1;
  252. }
  253. //Plot BDT efficiency at certain TMVA cut in a variable
  254. //----------------------------------------------------
  255. int plotBDTEfficiency(string year, int Run, Double_t TMVAcut, bool UseOnlyMuMuEvents, bool PHSP, bool KshortDecaysInVelo, bool RemoveMultiple, bool weighted,string sExtraVar){
  256. gROOT->SetBatch(); //ROOT stops plotting canvases
  257. string magnet = "both";
  258. bool useRefChannel = !PHSP && !UseOnlyMuMuEvents;
  259. string MCtype = useRefChannel ? "Reference channel" : (PHSP ? "PHSP" : "signal MC");
  260. coutInfo("Get efficiencies from " + MCtype + ".");
  261. TGraphErrors *effGraph = new TGraphErrors();
  262. coutInfo("Get efficiencies from signal channel in bins of " + sExtraVar + ".");
  263. int nBins = 0;
  264. TMefficiencyClass extraVar = TMefficiencyClass(sExtraVar);
  265. if (sExtraVar != 0){
  266. nBins = extraVar.Bins;
  267. }
  268. double binCenter = 0;
  269. double binError = 0;
  270. if (nBins ==0){
  271. coutERROR("Wrong variable used!");
  272. return 0; //I'm feeling adventerous
  273. }
  274. vector <double> binBoundaries = extraVar.isEquidistant ? extraVar.binEdgesEquidistant : extraVar.binEdges;
  275. for (int bin = 0; bin < nBins; bin++){
  276. EffAndError efficiency = EffAndError(); //put zeroes everywhere
  277. efficiency = getBDTEfficiencySimple(year,TMVAcut,Run,UseOnlyMuMuEvents,PHSP,KshortDecaysInVelo,RemoveMultiple,weighted,sExtraVar,bin);
  278. binCenter = (binBoundaries.at(bin+1)+binBoundaries.at(bin))/2.0;
  279. binError = (binBoundaries.at(bin+1)-binBoundaries.at(bin))/2.0;
  280. if (sExtraVar == "q2_binned"){ //Gev
  281. binCenter = binCenter/1e6;
  282. binError = binError/1e6;
  283. }
  284. effGraph->SetPoint(effGraph->GetN(), binCenter, efficiency.value);
  285. effGraph->SetPointError(effGraph->GetN()-1,binError,efficiency.highError); //Add error if necessary
  286. coutDebug("---" + year + " efficiency in bin " + to_string(bin) + ": " + to_string(efficiency.value) + "+-" + to_string(efficiency.highError));
  287. }
  288. string name = "BDTeffScan_";
  289. if (Run ==0) name.append(year);
  290. else name.append("Run"+to_string(Run));
  291. //Write to file and close
  292. string path = GetEfficiencyFile("BDT", year, magnet, Run, useRefChannel, UseOnlyMuMuEvents, PHSP, KshortDecaysInVelo, RemoveMultiple, weighted, false, false, sExtraVar);
  293. coutDebug("Writting graph into "+ path);
  294. TFile * effFile = new TFile(path.c_str(), "RECREATE");
  295. effFile->cd();
  296. effGraph->Write("",TObject::kWriteDelete);
  297. TCanvas * c1 = new TCanvas("c1", "c1");
  298. c1->cd();
  299. //Make the plot pretty and plot it
  300. designBasicEfficiency(effGraph,name,extraVar.sVariable,"{"+string(TMVAmethod)+"}",0.2,0.6); //Add error if necessary
  301. effGraph->Draw("ap");
  302. addYearTag(0.65,0.3,year, Run, 1, 1.42);
  303. replace(path,".root",".eps");
  304. c1->SaveAs(path.c_str(),"eps");
  305. effFile->Close();
  306. coutInfo("All binned BDT efficiency at " + to_string(TMVAcut) +" in " + sExtraVar + " for " + MCtype + " done.");
  307. return 1;
  308. }
  309. int plotBDTEffciencyInAngles(string year, int Run){
  310. double finalTMVAcut = (Run == 0 ? getTMVAcut(getRunID(year)) : getTMVAcut(Run));
  311. plotBDTEfficiency(year, Run, finalTMVAcut, false, true, false, true, true, "q2_binned");
  312. plotBDTEfficiency(year, Run, finalTMVAcut, false, true, false, true, true, "thetak_equal");
  313. plotBDTEfficiency(year, Run, finalTMVAcut, false, true, false, true, true, "thetal_equal");
  314. plotBDTEfficiency(year, Run, finalTMVAcut, false, true, false, true, true, "phi");
  315. return 1;
  316. }
  317. //----------------------------------------------------------------------------------------------------------
  318. // Selection efficiency
  319. //----------------------------------------------------
  320. TGraphErrors *getSelectionEffTGraph(bool full,bool UseOnlyMuMuEvents, bool PHSP, bool KshortDecaysInVelo,
  321. bool RemoveMultiple, bool weighted,
  322. string customTMbrach, bool gammaTM){
  323. bool useRefChannel = !PHSP && !UseOnlyMuMuEvents;
  324. string MCtype = useRefChannel ? "ReferenceChannel" : (PHSP ? "PHSP" : "SignalMC");
  325. coutInfo("Get efficiencies from signal channel from " + MCtype+ ".");
  326. TGraphErrors *effGraph = new TGraphErrors();
  327. for (auto& yr : yearsMC(useRefChannel, PHSP,12)){
  328. EffAndError efficiency = EffAndError(); //put zeroes everywhere
  329. efficiency = getSelectionEfficiencySimple(full, yr,0, UseOnlyMuMuEvents, useRefChannel, PHSP, KshortDecaysInVelo, RemoveMultiple, weighted, "",-1, customTMbrach, gammaTM);
  330. effGraph->SetPoint(effGraph->GetN(), stoi(yr), efficiency.value);
  331. effGraph->SetPointError(effGraph->GetN()-1,0.5,efficiency.highError); //Possibly add error
  332. coutDebug("---"+yr+" efficiency: "+to_string(efficiency.value)+"+-"+to_string(efficiency.highError));
  333. }
  334. Color_t color = kBlack;
  335. if (PHSP) color = kBlue +1;
  336. if (useRefChannel) color = kRed;
  337. designBasicEfficiency(effGraph,"SelEffScan"+MCtype,"Year","{Selection}",0.0,full ? 0.00075 : 0.002,color);
  338. if (!full) effGraph->GetYaxis()->SetTitleOffset(1.75);
  339. return effGraph;
  340. }
  341. TGraphErrors *getSelectionEffTGraph(bool full, string year, int Run, string customTMbranch, bool gammaTM, bool UseOnlyMuMuEvents, bool Reference, bool PHSP, bool RemoveMultiple, bool weighted, string sExtraVar){
  342. string MCtype = Reference ? "ReferenceChannel" : (PHSP ? "PHSP" : "SignalMC");
  343. bool KshortDecaysInVelo = false;
  344. coutInfo("Get efficiencies from " + MCtype + ".");
  345. TGraphErrors *effGraph = new TGraphErrors();
  346. coutInfo("Get efficiencies from signal channel in bins of " + sExtraVar + ".");
  347. int nBins = 0;
  348. TMefficiencyClass extraVar = TMefficiencyClass(sExtraVar);
  349. if (sExtraVar != 0){
  350. nBins = extraVar.Bins;
  351. }
  352. double binCenter = 0;
  353. double binError = 0;
  354. if (nBins ==0){
  355. coutERROR("Wrong variable used!");
  356. return 0; //I'm feeling adventerous
  357. }
  358. vector <double> binBoundaries = extraVar.isEquidistant ? extraVar.binEdgesEquidistant : extraVar.binEdges;
  359. for (int bin = 0; bin < nBins; bin++){
  360. binCenter = (binBoundaries.at(bin+1)+binBoundaries.at(bin));
  361. binError = (binBoundaries.at(bin+1)-binBoundaries.at(bin));
  362. if (sExtraVar == "q2_binned"){
  363. binCenter = binCenter/2.0e6;
  364. binError = binError/2.0e6;
  365. }
  366. EffAndError efficiency = EffAndError(); //put zeroes everywhere
  367. efficiency = getSelectionEfficiencySimple(full, year, Run, UseOnlyMuMuEvents, Reference, PHSP, KshortDecaysInVelo, RemoveMultiple, weighted, sExtraVar, bin, customTMbranch, gammaTM);
  368. effGraph->SetPoint(effGraph->GetN(), binCenter, efficiency.value);
  369. effGraph->SetPointError(effGraph->GetN()-1,binError,efficiency.highError); //Add error if necessary
  370. coutDebug("---" + year + " efficiency in bin " + to_string(bin) + ": " + to_string(efficiency.value) + "+-" + to_string(efficiency.highError));
  371. }
  372. Color_t color = kBlack;
  373. if (PHSP) color = kBlue +1;
  374. if (Reference) color = kRed;
  375. string name = "TMeffScan_";
  376. if (Run ==0) name.append(year);
  377. else name.append("Run"+to_string(Run));
  378. designBasicEfficiency(effGraph,name,extraVar.sVariable,"{Selection}",0.0,PHSP ? 0.1 : 0.0075);
  379. return effGraph;
  380. }
  381. int plotSelectionEfficiency(bool full, string year, int Run, bool UseOnlyMuMuEvents, bool Reference, bool PHSP, bool weighted, string sExtraVar, string customTMbranch, bool gammaTM){
  382. gROOT->SetBatch(); //ROOT stops plotting canvases
  383. bool KshortDecaysInVelo = false;
  384. string MCtype = Reference ? "Reference channel" : (PHSP ? "PHSP" : "signal MC");
  385. bool RemoveMultiple = false;
  386. coutInfo("Get efficiencies from " + MCtype + ".");
  387. TGraphErrors *effGraph;
  388. if (sExtraVar == "") effGraph = getSelectionEffTGraph(full,UseOnlyMuMuEvents,PHSP,KshortDecaysInVelo,RemoveMultiple,weighted,customTMbranch,gammaTM);
  389. else effGraph = getSelectionEffTGraph(full, year, Run, customTMbranch, gammaTM, UseOnlyMuMuEvents, Reference, PHSP, RemoveMultiple, false, sExtraVar);
  390. //Write to file and close
  391. string path = GetEfficiencyFileTM("Selection", year, "both", Run, Reference, UseOnlyMuMuEvents, PHSP, KshortDecaysInVelo, RemoveMultiple, weighted, false, "", false, false, sExtraVar, false, customTMbranch, gammaTM);
  392. if (sExtraVar == "") path = GetEfficiencyFile("Selection","2011",12,Reference,UseOnlyMuMuEvents,PHSP,KshortDecaysInVelo,RemoveMultiple,false,false,false,"");
  393. coutInfo("Writting graph into " + path);
  394. if (full) replace(path,".root","_full.root");
  395. TFile * effFile = new TFile(path.c_str(), "RECREATE");
  396. effFile->cd();
  397. effGraph->Write("",TObject::kWriteDelete);
  398. TCanvas * c1 = new TCanvas("c1", "c1");
  399. c1->SetTopMargin(0.1);
  400. c1->SetLeftMargin(0.16);
  401. c1->cd();
  402. double yrange = full ? 0.0025 : 0.015;
  403. if (PHSP) yrange = full ? 0.0025 : 0.02;
  404. effGraph->GetYaxis()->SetRangeUser(0.0,yrange);
  405. effGraph->GetYaxis()->SetNdivisions(505);
  406. effGraph->GetYaxis()->SetTitleOffset(1.2);
  407. effGraph->Draw("ap");
  408. effGraph->GetYaxis()->SetRangeUser(0.0,yrange);
  409. addYearTag(0.65,0.3,year, Run, 1, 1.42);
  410. replace(path,".root",".eps");
  411. c1->SaveAs(path.c_str(),"eps");
  412. effFile->Close();
  413. coutInfo("All binned Selection efficiency in "+sExtraVar+" for "+MCtype+" done.");
  414. return 1;
  415. }
  416. int plotSelectionEfficiencyAllYearsAll(bool full, bool RemoveMultiple, bool weighted, string customTMbrach, bool gammaTM ){
  417. gROOT->SetBatch(); //ROOT stops plotting canvases
  418. bool KshortDecaysInVelo = false;
  419. coutInfo("Get efficiencies from signal MC, reference channel and PHSP.");
  420. //Create TGraphErrors
  421. TGraphErrors *effGraphMuMu = getSelectionEffTGraph(full, true, false, KshortDecaysInVelo, RemoveMultiple, weighted, customTMbrach, gammaTM);
  422. TGraphErrors *effGraphJpsi = getSelectionEffTGraph(full, false, false, KshortDecaysInVelo, RemoveMultiple, weighted, customTMbrach, gammaTM);
  423. // TGraphErrors *effGraphPHSP = getSelectionEffTGraph(full, false, true, KshortDecaysInVelo, RemoveMultiple, weighted, customTMbrach, gammaTM);
  424. //Write to file and close
  425. string path = GetEfficiencyFile("Selection", "", "both", 12, false, false, false, KshortDecaysInVelo, RemoveMultiple,weighted, false, false, "",customTMbrach,gammaTM);
  426. //replace(path,"Run","All_Run");
  427. if (full) replace(path,".root","_full.root");
  428. coutInfo("Writting graph into "+ path);
  429. TFile * effFileTM = new TFile(path.c_str(), "RECREATE");
  430. effFileTM->cd();
  431. effGraphMuMu->Write("",TObject::kWriteDelete);
  432. effGraphJpsi->Write("",TObject::kWriteDelete);
  433. //effGraphPHSP->Write("",TObject::kWriteDelete);
  434. TCanvas * c1 = new TCanvas("c1", "c1");
  435. c1->cd();
  436. c1->SetLeftMargin(0.16);
  437. if (full) c1->SetTopMargin(0.1);
  438. TMultiGraph *mg = new TMultiGraph();
  439. mg->Add(effGraphMuMu);
  440. mg->Add(effGraphJpsi);
  441. //mg->Add(effGraphPHSP);
  442. mg->Draw("ap");
  443. mg->GetXaxis()->SetNdivisions(110);
  444. double yrange = full ? 0.003 : 0.025;
  445. designMultiGraphEfficiency(mg,"Selection","Year","{Selection}", 0.0, yrange);
  446. mg->GetYaxis()->SetTitleOffset(1.75);
  447. mg->Draw("ap");
  448. TLegend *leg = new TLegend(0.83,full ? 0.87 : 0.9 ,0.6, full ? 0.65 : 0.73);
  449. leg->AddEntry(effGraphMuMu, "Signal","l");
  450. leg->AddEntry(effGraphJpsi, "Reference","l");
  451. leg->Draw("SAME");
  452. effFileTM->cd();
  453. c1->Write();
  454. leg->Write();
  455. replace(path,".root",".eps");
  456. c1->SaveAs(path.c_str(),"eps");
  457. effFileTM->Close();
  458. return 1;
  459. }
  460. int plotSelectionEfficiencyAll(bool full, bool UseOnlyMuMuEvents, bool Reference, bool PHSP, bool weighted, string sExtraVar){
  461. for (auto yr: yearsMC(Reference,PHSP,12)){
  462. if (plotSelectionEfficiency(full, yr, 0, UseOnlyMuMuEvents, Reference, PHSP, weighted, sExtraVar, "TMed", gammaTMdefault) ==0) return 0;
  463. }
  464. if (plotSelectionEfficiency(full, "2011", 1, UseOnlyMuMuEvents, Reference, PHSP, weighted, sExtraVar, "TMed", gammaTMdefault)==0) return 0;
  465. if (plotSelectionEfficiency(full, "2015", 2, UseOnlyMuMuEvents, Reference, PHSP, weighted, sExtraVar, "TMed", gammaTMdefault)==0) return 0;
  466. return 1;
  467. }
  468. int plotSelectionEfficiencyAll(bool full){
  469. if (plotSelectionEfficiencyAll(full, false, false, false, true, "q2_binned") ==0) return 0;
  470. if (plotSelectionEfficiencyAll(full, false, true, false, true, "q2_binned") ==0) return 0;
  471. if (plotSelectionEfficiencyAll(full, false, false, true, true,"q2_binned") ==0) return 0;
  472. return 1;
  473. }
  474. //----------------------------------------------------------------------------------------------------------
  475. //Get TM efficiency
  476. //----------------------------------------------------------------------------------------------------------
  477. //Get overall TM efficiency from B+ mass
  478. //----------------------------------------------------
  479. TGraphErrors *getTMeffTGraph(bool UseOnlyMuMuEvents, bool PHSP, bool KshortDecaysInVelo,
  480. bool RemoveMultiple, bool weighted,
  481. string customTMbrach, bool gammaTM){
  482. bool useRefChannel = !PHSP && !UseOnlyMuMuEvents;
  483. string MCtype = useRefChannel ? "ReferenceChannel" : (PHSP ? "PHSP" : "SignalMC");
  484. coutInfo("Get efficiencies from signal channel from " + MCtype+ ".");
  485. TGraphErrors *effGraph = new TGraphErrors();
  486. for (auto& yr : yearsMC(false, false,12)){
  487. EffAndError efficiency = EffAndError(); //put zeroes everywhere
  488. efficiency = getTMEfficiencySimple(yr,0, UseOnlyMuMuEvents, PHSP, KshortDecaysInVelo, RemoveMultiple, weighted, "",-1, customTMbrach, gammaTM);
  489. effGraph->SetPoint(effGraph->GetN(), stoi(yr), efficiency.value);
  490. effGraph->SetPointError(effGraph->GetN()-1,0.5,efficiency.highError); //Possibly add error
  491. coutDebug("---"+yr+" efficiency: "+to_string(efficiency.value)+"+-"+to_string(efficiency.highError));
  492. }
  493. Color_t color = kBlack;
  494. if (PHSP) color = kBlue +1;
  495. if (useRefChannel) color = kRed;
  496. designBasicEfficiency(effGraph,"TMeffScan"+MCtype,"Year","{TM}",0.25,1.05,color);
  497. return effGraph;
  498. }
  499. int plotTruthMatchingEfficiencyAllYearsAll(bool RemoveMultiple, bool weighted, string customTMbrach, bool gammaTM ){
  500. gROOT->SetBatch(); //ROOT stops plotting canvases
  501. bool KshortDecaysInVelo = false;
  502. bool UseLowQ2Range = false;
  503. coutInfo("Get efficiencies from signal MC, reference channel and PHSP.");
  504. //Create TGraphErrors
  505. TGraphErrors *effGraphMuMu = getTMeffTGraph(true, false,KshortDecaysInVelo,RemoveMultiple,weighted,customTMbrach, gammaTM);
  506. TGraphErrors *effGraphJpsi = getTMeffTGraph(false,false,KshortDecaysInVelo,RemoveMultiple,weighted,customTMbrach, gammaTM);
  507. TGraphErrors *effGraphPHSP = getTMeffTGraph(false,true, KshortDecaysInVelo,RemoveMultiple,weighted,customTMbrach, gammaTM);
  508. //Write to file and close
  509. string path = GetEfficiencyFile("TM", "", "both", 12, false, false, false, KshortDecaysInVelo, RemoveMultiple,weighted, UseLowQ2Range, false, "",customTMbrach,gammaTM);
  510. replace(path,"Run","All_Run");
  511. coutInfo("Writting graph into "+ path);
  512. TFile * effFileTM = new TFile(path.c_str(), "RECREATE");
  513. effFileTM->cd();
  514. effGraphMuMu->Write("",TObject::kWriteDelete);
  515. effGraphJpsi->Write("",TObject::kWriteDelete);
  516. effGraphPHSP->Write("",TObject::kWriteDelete);
  517. TCanvas * c1 = new TCanvas("c1", "c1");
  518. c1->cd();
  519. TMultiGraph *mg = new TMultiGraph();
  520. mg->Add(effGraphMuMu);
  521. mg->Add(effGraphJpsi);
  522. mg->Add(effGraphPHSP);
  523. mg->Draw("ap");
  524. mg->GetXaxis()->SetNdivisions(110);
  525. designMultiGraphEfficiency(mg,"TMeffScan","Year","{TM}", 0.5, 1.05);
  526. mg->Draw("ap");
  527. replace(path,".root",".eps");
  528. c1->SaveAs(path.c_str(),"eps");
  529. effFileTM->Close();
  530. return 1;
  531. }
  532. int plotTruthMatchingEfficiencyAllYearsAll(bool gammaTM, string customTMbranch){
  533. if (plotTruthMatchingEfficiencyAllYearsAll(false,false, customTMbranch, gammaTM)==0)return 0;
  534. if (plotTruthMatchingEfficiencyAllYearsAll(false,true, customTMbranch, gammaTM)==0)return 0;
  535. if (plotTruthMatchingEfficiencyAllYearsAll(true, false, customTMbranch, gammaTM)==0)return 0;
  536. if (plotTruthMatchingEfficiencyAllYearsAll(true, true, customTMbranch, gammaTM)==0)return 0;
  537. return 1;
  538. }
  539. //Get TM efficiency from B+ mass in bins of sExtraVar
  540. //----------------------------------------------------
  541. TGraphErrors *getTMeffTGraph(string year, int Run, string customTMbranch, bool gammaTM, bool UseOnlyMuMuEvents, bool PHSP, bool RemoveMultiple, bool weighted, string sExtraVar){
  542. bool useRefChannel = !PHSP && !UseOnlyMuMuEvents;
  543. string MCtype = useRefChannel ? "ReferenceChannel" : (PHSP ? "PHSP" : "SignalMC");
  544. bool KshortDecaysInVelo = false;
  545. coutInfo("Get efficiencies from " + MCtype + ".");
  546. TGraphErrors *effGraph = new TGraphErrors();
  547. coutInfo("Get efficiencies from signal channel in bins of " + sExtraVar + ".");
  548. int nBins = 0;
  549. TMefficiencyClass extraVar = TMefficiencyClass(sExtraVar);
  550. if (sExtraVar != 0){
  551. nBins = extraVar.Bins;
  552. }
  553. double binCenter = 0;
  554. if (nBins ==0){
  555. coutERROR("Wrong variable used!");
  556. return 0; //I'm feeling adventerous
  557. }
  558. vector <double> binBoundaries = extraVar.isEquidistant ? extraVar.binEdgesEquidistant : extraVar.binEdges;
  559. for (int bin = 0; bin < nBins; bin++){
  560. EffAndError efficiency = EffAndError(); //put zeroes everywhere
  561. efficiency = getTMEfficiencySimple(year,Run,UseOnlyMuMuEvents,PHSP,KshortDecaysInVelo,RemoveMultiple,weighted,sExtraVar,bin,customTMbranch, gammaTM);
  562. binCenter = (binBoundaries.at(bin+1)+binBoundaries.at(bin))/2.0;
  563. effGraph->SetPoint(effGraph->GetN(), binCenter, efficiency.value);
  564. effGraph->SetPointError(effGraph->GetN()-1,(binBoundaries.at(bin+1)-binBoundaries.at(bin))/2.0,efficiency.highError); //Add error if necessary
  565. coutDebug("---" + year + " efficiency in bin " + to_string(bin) + ": " + to_string(efficiency.value) + "+-" + to_string(efficiency.highError));
  566. }
  567. Color_t color = kBlack;
  568. if (PHSP) color = kBlue +1;
  569. if (useRefChannel) color = kRed;
  570. string name = "TMeffScan_";
  571. if (Run ==0) name.append(year);
  572. else name.append("Run"+to_string(Run));
  573. designBasicEfficiency(effGraph,name,extraVar.sVariable,"{TM}",0.25,1.05);
  574. return effGraph;
  575. }
  576. int plotTruthMatchingEfficiency(string year, int Run, bool UseOnlyMuMuEvents, bool PHSP, string sExtraVar, string customTMbranch, bool gammaTM){ //TODO sigEff
  577. gROOT->SetBatch(); //ROOT stops plotting canvases
  578. string magnet = "both";
  579. bool KshortDecaysInVelo = false;
  580. bool useRefChannel = !PHSP && !UseOnlyMuMuEvents;
  581. string MCtype = useRefChannel ? "Reference channel" : (PHSP ? "PHSP" : "signal MC");
  582. coutInfo("Get efficiencies from " + MCtype + ".");
  583. TGraphErrors *effGraph = getTMeffTGraph(year,Run, customTMbranch , gammaTM, UseOnlyMuMuEvents,PHSP,false,false, sExtraVar); //TODO customTMbranch
  584. //Write to file and close
  585. string path = GetEfficiencyFileTM("TM", year, magnet, Run, useRefChannel, UseOnlyMuMuEvents, PHSP, KshortDecaysInVelo, false, false,false, "", false, false, sExtraVar, false, customTMbranch, gammaTM);
  586. coutInfo("Writting graph into " + path);
  587. TFile * effFile = new TFile(path.c_str(), "RECREATE");
  588. effFile->cd();
  589. effGraph->Write("",TObject::kWriteDelete);
  590. TCanvas * c1 = new TCanvas("c1", "c1");
  591. c1->cd();
  592. effGraph->Draw("ap");
  593. replace(path,".root",".eps");
  594. c1->SaveAs(path.c_str(),"eps");
  595. effFile->Close();
  596. coutInfo("All binned TM efficiency in "+sExtraVar+" for "+MCtype+" done.");
  597. return 1;
  598. }
  599. //----------------------------------------------------------------------------------------------------------
  600. // Run all the stuff for nice plots
  601. //----------------------------------------------------------------------------------------------------------
  602. int runAllEff(){
  603. //Selection efficiency
  604. plotSelectionEfficiencyAllYearsAll(false,false,true,"",gammaTMdefault);
  605. plotSelectionEfficiencyAll(false,true,false,false,true,"q2_binned");
  606. plotSelectionEfficiencyAll(false,false,true,false,true,"q2_binned");
  607. //BDT
  608. ScanBDTEfficiencyAll();
  609. return 1;
  610. }