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.

697 lines
24 KiB

  1. //Tools to add kinematic and TRUE kinematic variables to tuples
  2. //Renata Kopecna
  3. #include "../GlobalFunctions.hh"
  4. #include "../Paths.hpp"
  5. #include "../Utils.hpp"
  6. bool bkgSample = false;
  7. std::string tmpAdress(string year, string magnet){
  8. //return "/home/lhcb/kopecna/B2KstarMuMu/data/data/MC/BackgroundSamples/B0toKstJpsi/"+year+magnet+"/"+year+magnet+"_pi0Resolved.root"; //uncomment for B0->KstJpsi sample
  9. //return "/home/lhcb/kopecna/B2KstarMuMu/data/data/MC/BackgroundSamples/B0toKstMuMu/"+year+magnet+"/"+year+magnet+"_pi0Resolved.root"; //uncomment for B0->KstMuMu sample
  10. //return "/home/lhcb/kopecna/B2KstarMuMu/data/data/MC/BackgroundSamples/BtoK1Jpsi/"+year+magnet+"/"+year+magnet+"_pi0Resolved.root"; //uncomment for B->K1Jpsi sample
  11. return "/home/lhcb/kopecna/B2KstarMuMu/data/data/MC/BackgroundSamples/BtoXJpsi/"+year+magnet+"/"+year+magnet+"_pi0Resolved.root";
  12. //return "/home/lhcb/kopecna/B2KstarMuMu/data/data/MC/BackgroundSamples/BtoK1MuMu/"+year+magnet+"/"+year+magnet+"_pi0Resolved.root"; //uncomment for B->K1Jpsi sample
  13. }
  14. int addPi0TRUEVariables(string year, int Run, string magnet, bool BDTed, bool ReferenceChannel, bool PHSP, bool KshortDecayInVelo){
  15. //sanity checks
  16. if (magnet == "Down") magnet = "down";
  17. if (magnet == "Up") magnet = "up";
  18. if (magnet == "Both" || magnet == "") magnet = "both";
  19. if (magnet != "both" && magnet != "up" && magnet != "down"){
  20. coutWarning("Wrong magnet polarity used! Setting magnet to both!");
  21. magnet = "both";
  22. }
  23. if (magnet != "both" && BDTed){
  24. coutWarning("BDT is applied only per yer, not per polarity! Setting magnet to both!");
  25. magnet = "both";
  26. }
  27. if (!Kst2Kspiplus && KshortDecayInVelo){
  28. coutERROR("No LL/DD tracks in KplusPizero channel! Setting KshortDecaysInVelo to false!");
  29. KshortDecayInVelo = false;
  30. }
  31. gROOT->SetBatch(kTRUE);
  32. LHCbStyle();
  33. string treeAddress = returnFileAddress(year,Run,magnet,true,BDTed,true,ReferenceChannel,PHSP,KshortDecayInVelo);
  34. if (bkgSample) treeAddress = tmpAdress(year,magnet);
  35. if (BDTed) replace(treeAddress, "BDToutputSelection", "BDToutput");
  36. TFile *file = TFile::Open(treeAddress.c_str());
  37. if(file == 0){
  38. coutERROR("No file called " + treeAddress+ ". Exit program!");
  39. return 0;
  40. }
  41. coutInfo("Reading data from "+treeAddress);
  42. TTree *tree = (TTree*)file->Get("DecayTreeTruthMatched");
  43. int nOldEvts = tree->GetEntries();
  44. if(nOldEvts == 0){
  45. coutERROR("No entries found in TTree. Exit program!");
  46. return 0;
  47. }
  48. coutDebug("Old Tree entries:\t" + to_string(nOldEvts));
  49. //Save //NOW in a new file
  50. replace(treeAddress,".root","_test.root");
  51. coutDebug("Creating new file " + treeAddress);
  52. TFile *output = new TFile(treeAddress.c_str(),"RECREATE");
  53. TTree * newTree = tree->CloneTree();
  54. //Create new branches
  55. string particle = "pi_zero_resolved";
  56. TBranch *trueMomentum;
  57. double trueP = 0.0;
  58. string branchName = particle + "_TRUEP_NEW";
  59. string branchNameType =branchName+ "/D";
  60. trueMomentum = newTree->Branch(branchName.c_str(), &trueP, branchNameType.c_str() );
  61. TBranch *trueMomentumX;
  62. double truePX = 0.0;
  63. branchName = particle + "_TRUEP_X_NEW";
  64. branchNameType =branchName+ "/D";
  65. trueMomentumX = newTree->Branch(branchName.c_str(), &truePX, branchNameType.c_str() );
  66. TBranch *trueMomentumY;
  67. double truePY = 0.0;
  68. branchName = particle + "_TRUEP_Y_NEW";
  69. branchNameType =branchName+ "/D";
  70. trueMomentumY = newTree->Branch(branchName.c_str(), &truePY, branchNameType.c_str() );
  71. TBranch *trueMomentumZ;
  72. double truePZ = 0.0;
  73. branchName = particle + "_TRUEP_Z_NEW";
  74. branchNameType =branchName+ "/D";
  75. trueMomentumZ = newTree->Branch(branchName.c_str(), &truePZ, branchNameType.c_str() );
  76. TBranch *trueMomentumT;
  77. double truePT = 0.0;
  78. branchName = particle + "_TRUEPT_NEW";
  79. branchNameType =branchName+ "/D";
  80. trueMomentumT = newTree->Branch(branchName.c_str(), &truePT, branchNameType.c_str() );
  81. TBranch *trueMomentumE;
  82. double truePE = 0.0;
  83. branchName = particle + "_TRUEP_E_NEW";
  84. branchNameType =branchName+ "/D";
  85. trueMomentumE = newTree->Branch(branchName.c_str(), &truePE, branchNameType.c_str() );
  86. TBranch *trueEta;
  87. double trueETA = 0.0;
  88. branchName = particle + "_TRUEETA_NEW";
  89. branchNameType =branchName+ "/D";
  90. trueEta= newTree->Branch(branchName.c_str(), &trueETA, branchNameType.c_str() );
  91. TBranch *truePhi ;
  92. double truePHI = 0.0;
  93. branchName = particle + "_TRUEPHI_NEW";
  94. branchNameType =branchName+ "/D";
  95. truePhi = newTree->Branch(branchName.c_str(), &truePHI, branchNameType.c_str() );
  96. TBranch *trueMass;
  97. double trueM = 0.0;
  98. branchName = particle + "_TRUEM_NEW";
  99. branchNameType =branchName+ "/D";
  100. trueMass = newTree->Branch(branchName.c_str(), &trueM, branchNameType.c_str() );
  101. //Create a lorentz vector and load some branches form the old tree
  102. TLorentzVector LorVec[5];
  103. TLorentzVector LorVecTrue;
  104. TLorentzVector LorTmp,LorVecDiMuon;
  105. Double_t PX[5];
  106. Double_t PY[5];
  107. Double_t PZ[5];
  108. Double_t PT[5];
  109. Double_t PE[5];
  110. string particles[5] = {"B_plus","K_star_plus","K_plus","mu_plus","mu_minus"};
  111. for (int i = 0; i < 5; i++){
  112. newTree->SetBranchAddress( Form("%s_TRUEP_X",particles[i].c_str()), &PX[i]);
  113. newTree->SetBranchAddress( Form("%s_TRUEP_Y",particles[i].c_str()), &PY[i]);
  114. newTree->SetBranchAddress( Form("%s_TRUEP_Z",particles[i].c_str()), &PZ[i]);
  115. newTree->SetBranchAddress( Form("%s_TRUEPT",particles[i].c_str()), &PT[i]);
  116. newTree->SetBranchAddress( Form("%s_TRUEP_E",particles[i].c_str()), &PE[i]);
  117. }
  118. //Loop over the old tree
  119. coutInfo("Starting the loop!");
  120. for (int i = 0; i < nOldEvts; i++){
  121. newTree->GetEntry(i);
  122. if (0ul == (i % 10000ul) || nOldEvts == i + 1) coutDebug("Read event " + to_string(i) + "/" + to_string(nOldEvts));
  123. //Get LorVec
  124. for (int i = 0; i < 5; i++){
  125. LorVec[i].SetPxPyPzE(PX[i],PY[i],PZ[i],PE[i]);
  126. }
  127. LorVecDiMuon = LorVec[3]+LorVec[4];
  128. LorTmp = LorVec[0] - LorVecDiMuon;
  129. LorVecTrue = LorTmp - LorVec[2];
  130. //Calculate the momenta and eta and phi
  131. trueP = LorVecTrue.P();
  132. truePX = LorVecTrue.Px();
  133. truePY = LorVecTrue.Py();
  134. truePZ = LorVecTrue.Pz();
  135. truePT = LorVecTrue.Pt();
  136. truePE = LorVecTrue.E();
  137. trueM = LorVecTrue.M();
  138. truePHI = LorVecTrue.Phi();
  139. trueETA = LorVecTrue.Eta();
  140. //Fill
  141. trueMomentum->Fill();
  142. trueMomentumX->Fill();
  143. trueMomentumY->Fill();
  144. trueMomentumZ->Fill();
  145. trueMomentumT->Fill();
  146. trueMomentumE->Fill();
  147. trueEta->Fill();
  148. truePhi->Fill();
  149. trueMass->Fill();
  150. }
  151. if (nOldEvts != newTree->GetEntries()){
  152. coutERROR("Something went wrong with filling the tree! Exit without saving the new tree.");
  153. return 0;
  154. }
  155. if(!output->IsOpen()){
  156. coutERROR("Could not create output file. Abort!");
  157. return 0;
  158. }
  159. output->cd();
  160. coutDebug("Writting into the new file " + treeAddress );
  161. newTree->Write("",TObject::kWriteDelete);
  162. //tree->Clear();
  163. output->Close();
  164. file->Close();
  165. string command = "mv " + treeAddress + " ";
  166. replace(treeAddress,"_test.root",".root");
  167. command = "rm " + treeAddress + "; " + command + treeAddress;
  168. coutDebug("Running: " + command);
  169. system(command.c_str());
  170. return 1;
  171. }
  172. int addPi0TRUEVariablesAllYearMC(string year, bool ReferenceChannel, bool PHSP){
  173. if (addPi0TRUEVariables(year,getRunID(year),"down",false,ReferenceChannel,PHSP,false)==0) return 0;
  174. if (addPi0TRUEVariables(year,getRunID(year),"up", false,ReferenceChannel,PHSP,false)==0) return 0;
  175. // if (addPi0TRUEVariables(year,getRunID(year),"both", false,ReferenceChannel,PHSP,false)==0) return 0;
  176. // if (addPi0TRUEVariables(year,getRunID(year),"both", true, ReferenceChannel,PHSP,false)==0) return 0;
  177. return 1;
  178. }
  179. int addPi0TRUEVariablesAllMC(int Run, bool ReferenceChannel, bool PHSP){
  180. for (auto &year: yearsData(Run)){
  181. addPi0TRUEVariablesAllYearMC(year,ReferenceChannel,PHSP);
  182. }
  183. return 1;
  184. }
  185. int addPi0TRUEVariablesAllMC(int Run){
  186. if (addPi0TRUEVariablesAllMC(Run,false,false)==0) return 0;
  187. if (addPi0TRUEVariablesAllMC(Run,true, false)==0) return 0;
  188. if (addPi0TRUEVariablesAllMC(Run,false,true )==0) return 0;
  189. return 1;
  190. }
  191. int addVariable(string year, int Run, string magnet, bool Preselected, bool BDTed, bool TM, bool ReferenceChannel, bool PHSP, bool KshortDecayInVelo,
  192. string particle){
  193. //sanity checks
  194. if (magnet == "Down") magnet = "down";
  195. if (magnet == "Up") magnet = "up";
  196. if (magnet == "Both" || magnet == "") magnet = "both";
  197. if (magnet != "both" && magnet != "up" && magnet != "down"){
  198. coutWarning("Wrong magnet polarity used! Setting magnet to both!");
  199. magnet = "both";
  200. }
  201. if (magnet != "both" && BDTed){
  202. coutWarning("BDT is applied only per yer, not per polarity! Setting magnet to both!");
  203. magnet = "both";
  204. }
  205. if (TM && !Preselected){
  206. coutWarning("Stripped MC is not TruthMatched! Setting TM to false!");
  207. TM = false;
  208. }
  209. if (!Kst2Kspiplus && KshortDecayInVelo){
  210. coutERROR("No LL/DD tracks in KplusPizero channel! Setting KshortDecaysInVelo to false!");
  211. KshortDecayInVelo = false;
  212. }
  213. if (!BDTed && !Preselected){ //if TMVAcut == -1, no TMVA cut is applied
  214. coutWarning("TMVA cut can be only aplied on preselected data! Setting Preselected to true!");
  215. Preselected = true; //Cannot do BDT cut on stripped data
  216. }
  217. string treeAddress = returnFileAddress(year,Run,magnet,Preselected,BDTed,true,ReferenceChannel,PHSP,KshortDecayInVelo);
  218. if (bkgSample) treeAddress = tmpAdress(year,magnet);
  219. if (BDTed) replace(treeAddress, "BDToutputSelection", "BDToutput");
  220. TFile *file = TFile::Open(treeAddress.c_str());
  221. if(file == 0){
  222. coutERROR("No file called " + treeAddress+ ". Exit program!");
  223. return 0;
  224. }
  225. coutInfo("Reading data from TTree... "+treeAddress);
  226. TTree *tree;
  227. if (Preselected) tree = (TTree*)file->Get("DecayTreeTruthMatched");
  228. else tree = (TTree*)file->Get("DecayTree");
  229. int nOldEvts = tree->GetEntries();
  230. if(nOldEvts == 0){
  231. coutERROR("No entries found in TTree. Exit program!");
  232. return 0;
  233. }
  234. coutDebug("Old Tree entries:\t" + to_string(nOldEvts));
  235. //Save //NOW in a new file
  236. replace(treeAddress,".root","_test.root");
  237. coutDebug("Creating new file " + treeAddress);
  238. TFile *output = new TFile(treeAddress.c_str(),"RECREATE");
  239. TTree * newTree = tree->CloneTree();
  240. //Create new branches
  241. string branchName = "";
  242. string branchNameType = "";
  243. //Create new branches for MC
  244. TBranch *trueMass;
  245. TBranch *trueMomentum;
  246. TBranch *trueEta;
  247. TBranch *truePhi ;
  248. double trueM = 0.0;
  249. double trueP = 0.0;
  250. double trueETA = 0.0;
  251. double truePHI = 0.0;
  252. branchName = particle + "_TRUEM";
  253. branchNameType =branchName+ "/D";
  254. trueMass = newTree->Branch(branchName.c_str(), &trueM, branchNameType.c_str() );
  255. branchName = particle + "_TRUEP";
  256. branchNameType =branchName+ "/D";
  257. trueMomentum = newTree->Branch(branchName.c_str(), &trueP, branchNameType.c_str() );
  258. branchName = particle + "_TRUEETA";
  259. branchNameType =branchName+ "/D";
  260. trueEta= newTree->Branch(branchName.c_str(), &trueETA, branchNameType.c_str() );
  261. branchName = particle + "_TRUEPHI";
  262. branchNameType =branchName+ "/D";
  263. truePhi = newTree->Branch(branchName.c_str(), &truePHI, branchNameType.c_str() );
  264. //Create a lorentz vector and load some branches form the old tree
  265. TLorentzVector LorVecTrue;
  266. Double_t TRUEP_X;
  267. Double_t TRUEP_Y;
  268. Double_t TRUEP_Z;
  269. Double_t TRUEP_E;
  270. newTree->SetBranchAddress( Form("%s_TRUEP_X",particle.c_str()), &TRUEP_X );
  271. newTree->SetBranchAddress( Form("%s_TRUEP_Y",particle.c_str()), &TRUEP_Y );
  272. newTree->SetBranchAddress( Form("%s_TRUEP_Z",particle.c_str()), &TRUEP_Z );
  273. newTree->SetBranchAddress( Form("%s_TRUEP_E",particle.c_str()), &TRUEP_E );
  274. //Loop over the old tree
  275. coutInfo("Starting the loop!");
  276. for (int i = 0; i < nOldEvts; i++){
  277. newTree->GetEntry(i);
  278. if (0ul == (i % 10000ul) || nOldEvts == i + 1) coutDebug("Read event " + to_string(i) + "/" + to_string(nOldEvts));
  279. //Get TRUE LorVec
  280. LorVecTrue.SetPxPyPzE(TRUEP_X,TRUEP_Y,TRUEP_Z,TRUEP_E);
  281. //Calculate the momenta and eta and phi
  282. trueM = LorVecTrue.M();
  283. trueP = LorVecTrue.P();
  284. trueETA = LorVecTrue.Eta();
  285. truePHI = LorVecTrue.Phi();
  286. //Fill
  287. trueMass->Fill();
  288. trueMomentum->Fill();
  289. trueEta->Fill();
  290. truePhi->Fill();
  291. }
  292. if (nOldEvts != newTree->GetEntries()){
  293. coutERROR("Something went wrong with filling the tree! Exit without saving the new tree.");
  294. return 0;
  295. }
  296. if(!output->IsOpen()){
  297. coutERROR("Could not create output file. Abort!");
  298. return 0;
  299. }
  300. output->cd();
  301. coutDebug("Writting into the new file " + treeAddress );
  302. newTree->Write("",TObject::kWriteDelete);
  303. output->Close();
  304. file->Close();
  305. string command = "mv " + treeAddress + " ";
  306. replace(treeAddress,"_test.root",".root");
  307. command = "rm " + treeAddress + "; " + command + treeAddress;
  308. coutDebug("Running: " + command);
  309. system(command.c_str());
  310. return 1;
  311. }
  312. int addAllVariables(string year, int Run, string magnet, bool Preselected, bool BDTed, bool TM, bool ReferenceChannel, bool PHSP, bool KshortDecayInVelo){
  313. vector <string> particles = {"B_plus", "K_star_plus","pi_zero_resolved","K_plus", "gamma1","gamma2","mu_minus","mu_plus"} ;//TODO
  314. for (auto& particle: particles){
  315. coutInfo("Running particle " + particle);
  316. if (addVariable(year,Run,magnet,Preselected,BDTed,TM,ReferenceChannel,PHSP,KshortDecayInVelo,particle)==0) return 0;
  317. }
  318. return 1;
  319. }
  320. int addAllVariablesAllYearMC(string year, bool ReferenceChannel, bool PHSP){
  321. //if (addAllVariables(year,getRunID(year),"down",true,false,false,ReferenceChannel,PHSP,false)==0) return 0;
  322. //if (addAllVariables(year,getRunID(year),"up", true,false,false,ReferenceChannel,PHSP,false)==0) return 0;
  323. //if (addAllVariables(year,getRunID(year),"both",true,false,true,true,ReferenceChannel,PHSP,false)==0) return 0;
  324. if (addAllVariables(year,getRunID(year),"both",true,true,true,ReferenceChannel,PHSP,false)==0) return 0;
  325. if (Kst2Kspiplus){
  326. if (addAllVariables(year,getRunID(year),"down",true,false,false,ReferenceChannel,PHSP,true)==0) return 0;
  327. if (addAllVariables(year,getRunID(year),"up", true,false,false,ReferenceChannel,PHSP,true)==0) return 0;
  328. // if (addAllVariables(year,getRunID(year),"both",true,false,true,true,ReferenceChannel,PHSP,true)==0) return 0;
  329. if (addAllVariables(year,getRunID(year),"both",true,true,true,ReferenceChannel,PHSP,true)==0) return 0;
  330. }
  331. return 1;
  332. }
  333. int addAllVariablesAllMC(int Run,bool ReferenceChannel, bool PHSP){
  334. for (auto &year: yearsMC(ReferenceChannel,PHSP,Run)){
  335. addAllVariablesAllYearMC(year,ReferenceChannel,PHSP);
  336. }
  337. return 1;
  338. }
  339. int addAllVariablesAllMCSamples(int Run){
  340. if (addAllVariablesAllMC(Run,false,false)==0) return 0;
  341. if (addAllVariablesAllMC(Run,true, false)==0) return 0;
  342. if (addAllVariablesAllMC(Run,false,true )==0) return 0;
  343. return 1;
  344. }
  345. int testFunction(){
  346. setVerboseLevel(0);
  347. return 1;
  348. }
  349. int addXMuMuMass(bool Kplus, bool DTF, string year, string magnet, int Run, bool MC, bool ReferenceChannel, bool PHSP, bool KshortDecayInVelo){
  350. //sanity checks
  351. if (!Kst2Kspiplus && KshortDecayInVelo){
  352. coutERROR("No LL/DD tracks in KplusPizero channel! Setting KshortDecaysInVelo to false!");
  353. KshortDecayInVelo = false;
  354. }
  355. gROOT->SetBatch(kTRUE);
  356. LHCbStyle();
  357. string treeAddress = returnFileAddress(year,Run,magnet,true,false, MC,ReferenceChannel,PHSP,KshortDecayInVelo);
  358. if (bkgSample) treeAddress = tmpAdress(year,magnet);
  359. TFile *file = TFile::Open(treeAddress.c_str());
  360. if(file == 0){
  361. coutERROR("No file called " + treeAddress+ ". Exit program!");
  362. return 0;
  363. }
  364. coutInfo("Reading data from "+treeAddress);
  365. TTree *tree = (TTree*)file->Get(treeName(MC,true).c_str());
  366. int nOldEvts = tree->GetEntries();
  367. if(nOldEvts == 0){
  368. coutERROR("No entries found in TTree. Exit program!");
  369. return 0;
  370. }
  371. coutDebug("Old Tree entries:\t" + to_string(nOldEvts));
  372. //Save //NOW in a new file
  373. replace(treeAddress,".root","_test.root");
  374. coutDebug("Creating new file " + treeAddress);
  375. TFile *output = new TFile(treeAddress.c_str(),"RECREATE");
  376. TTree * newTree = tree->CloneTree();
  377. //Create new branches
  378. TBranch *M_XMuMu;
  379. double new_M = 0.0;
  380. string branchName = Kplus ? ("M_KplusMuMu") : (DTF ? "M_PizMuMu" : "M_PizMuMu_noDTF");
  381. string branchNameType =branchName+ "/D";
  382. M_XMuMu = newTree->Branch(branchName.c_str(), &new_M, branchNameType.c_str() );
  383. //Create a lorentz vector and load some branches form the old tree
  384. const int nParticles = 3;
  385. TLorentzVector LorVec[nParticles];
  386. TLorentzVector LorTmp,LorVecDiMuon;
  387. Double_t PX[nParticles];
  388. Double_t PY[nParticles];
  389. Double_t PZ[nParticles];
  390. Double_t PE[nParticles];
  391. string particles[nParticles] = {Kplus ? "K_plus" : "pi_zero_resolved","mu_plus","mu_minus"};
  392. string s_DTF = DTF ? "_DTF" : "";
  393. for (int i = 0; i < nParticles; i++){
  394. newTree->SetBranchAddress( Form("%s_PX%s",particles[i].c_str(),s_DTF.c_str()), &PX[i]);
  395. newTree->SetBranchAddress( Form("%s_PY%s",particles[i].c_str(),s_DTF.c_str()), &PY[i]);
  396. newTree->SetBranchAddress( Form("%s_PZ%s",particles[i].c_str(),s_DTF.c_str()), &PZ[i]);
  397. newTree->SetBranchAddress( Form("%s_PE%s",particles[i].c_str(),s_DTF.c_str()), &PE[i]);
  398. }
  399. //Loop over the old tree
  400. coutInfo("Starting the loop!");
  401. for (int i = 0; i < nOldEvts; i++){
  402. newTree->GetEntry(i);
  403. if (0ul == (i % 10000ul) || nOldEvts == i + 1) coutDebug("Read event " + to_string(i) + "/" + to_string(nOldEvts));
  404. //Get LorVec
  405. for (int i = 0; i < nParticles; i++){
  406. LorVec[i].SetPxPyPzE(PX[i],PY[i],PZ[i],PE[i]);
  407. }
  408. LorVecDiMuon = LorVec[1]+LorVec[2];
  409. LorTmp = LorVec[0] + LorVecDiMuon;
  410. new_M = LorTmp.M();
  411. //Fill
  412. M_XMuMu->Fill();
  413. }
  414. if (nOldEvts != newTree->GetEntries()){
  415. coutERROR("Something went wrong with filling the tree! Exit without saving the new tree.");
  416. return 0;
  417. }
  418. if(!output->IsOpen()){
  419. coutERROR("Could not create output file. Abort!");
  420. return 0;
  421. }
  422. output->cd();
  423. coutDebug("Writting into the new file " + treeAddress );
  424. newTree->Write("",TObject::kWriteDelete);
  425. //tree->Clear();
  426. output->Close();
  427. file->Close();
  428. string command = "mv " + treeAddress + " ";
  429. replace(treeAddress,"_test.root",".root");
  430. command = "rm " + treeAddress + "; " + command + treeAddress;
  431. coutDebug("Running: " + command);
  432. system(command.c_str());
  433. return 1;
  434. }
  435. int addAllXMuMuMass(bool Kplus, bool DTF, int Run, bool MC, bool ReferenceChannel, bool PHSP){
  436. for (auto &year: yearsMC(ReferenceChannel,PHSP,Run)){
  437. addXMuMuMass(Kplus, DTF,year, "up",getRunID(year), MC, ReferenceChannel,PHSP,false);
  438. addXMuMuMass(Kplus, DTF,year, "down",getRunID(year), MC, ReferenceChannel,PHSP,false);
  439. }
  440. return 1;
  441. }
  442. int addAllXMuMuMass(bool Kplus, bool DTF,int Run){
  443. addAllXMuMuMass(Kplus, DTF, Run, false, false, false);
  444. addAllXMuMuMass(Kplus, DTF, Run, true, false, false);
  445. addAllXMuMuMass(Kplus, DTF, Run, true, true, false);
  446. addAllXMuMuMass(Kplus, DTF, Run, true, false, true);
  447. return 1;
  448. }
  449. int applyVetoKplusMuMuMass(string year, string magnet, int Run, bool MC, bool ReferenceChannel, bool PHSP, bool KshortDecayInVelo){
  450. //sanity checks
  451. if (!Kst2Kspiplus && KshortDecayInVelo){
  452. coutERROR("No LL/DD tracks in KplusPizero channel! Setting KshortDecaysInVelo to false!");
  453. KshortDecayInVelo = false;
  454. }
  455. gROOT->SetBatch(kTRUE);
  456. LHCbStyle();
  457. string treeAddress = returnFileAddress(year,Run,magnet,true,false, MC,ReferenceChannel,PHSP,KshortDecayInVelo);
  458. if (bkgSample) treeAddress = tmpAdress(year,magnet);
  459. TFile *file = TFile::Open(treeAddress.c_str());
  460. if(file == 0){
  461. coutERROR("No file called " + treeAddress+ ". Exit program!");
  462. return 0;
  463. }
  464. coutInfo("Reading data from "+treeAddress);
  465. TTree *tree = (TTree*)file->Get(treeName(MC,true).c_str());
  466. //Save //NOW in a new file
  467. replace(treeAddress,".root","_test.root");
  468. coutDebug("Creating new file " + treeAddress);
  469. TFile *output = new TFile(treeAddress.c_str(),"RECREATE");
  470. TTree * newTree = tree->CopyTree("M_KplusMuMu<5179 || M_KplusMuMu>5379","", tree->GetEntries());
  471. output->cd();
  472. coutDebug("Writting into the new file " + treeAddress );
  473. newTree->Write("",TObject::kWriteDelete);
  474. //tree->Clear();
  475. output->Close();
  476. file->Close();
  477. string command = "mv " + treeAddress + " ";
  478. replace(treeAddress,"_test.root",".root");
  479. command = "rm " + treeAddress + "; " + command + treeAddress;
  480. coutDebug("Running: " + command);
  481. system(command.c_str());
  482. return 1;
  483. }
  484. int applyAllVetoKplusMuMuMass(int Run, bool MC, bool ReferenceChannel, bool PHSP){
  485. for (auto &year: yearsMC(ReferenceChannel,PHSP,Run)){
  486. applyVetoKplusMuMuMass(year, "up",getRunID(year), MC, ReferenceChannel,PHSP,false);
  487. applyVetoKplusMuMuMass(year, "down",getRunID(year), MC, ReferenceChannel,PHSP,false);
  488. }
  489. return 1;
  490. }
  491. int applyAllVetoKplusMuMuMass(int Run){
  492. applyAllVetoKplusMuMuMass(Run, false, false, false);
  493. applyAllVetoKplusMuMuMass(Run, true, false, false);
  494. applyAllVetoKplusMuMuMass(Run, true, true, false);
  495. applyAllVetoKplusMuMuMass(Run, true, false, true);
  496. return 1;
  497. }
  498. int applyVetoPizMuMuMass(string year, string magnet, int Run, bool MC){
  499. //sanity checks
  500. gROOT->SetBatch(kTRUE);
  501. LHCbStyle();
  502. //MC only do Jpsi region
  503. string treeAddress = returnFileAddress(year,Run,magnet,true,false,MC,MC,false,false);
  504. TFile *file = TFile::Open(treeAddress.c_str());
  505. if(file == 0){
  506. coutERROR("No file called " + treeAddress+ ". Exit program!");
  507. return 0;
  508. }
  509. coutInfo("Reading data from "+treeAddress);
  510. TTree *tree = (TTree*)file->Get(treeName(MC,true).c_str());
  511. //Save //NOW in a new file
  512. replace(treeAddress,".root","_test.root");
  513. coutDebug("Creating new file " + treeAddress);
  514. TFile *output = new TFile(treeAddress.c_str(),"RECREATE");
  515. coutDebug("Writting into the new file " + treeAddress );
  516. if (MC){
  517. TTree * newTree = tree->CopyTree("M_PizMuMu>3700","", tree->GetEntries());
  518. newTree->Write("",TObject::kWriteDelete);
  519. }
  520. else{
  521. string cut = "!(" + getJpsicut() + ") || ( M_PizMuMu>3700)";
  522. TTree *newTree = tree->CopyTree(cut.c_str(),"", tree->GetEntries());
  523. newTree->Write("",TObject::kWriteDelete);
  524. }
  525. //tree->Clear();
  526. output->Close();
  527. file->Close();
  528. string command = "mv " + treeAddress + " ";
  529. replace(treeAddress,"_test.root",".root");
  530. command = "rm " + treeAddress + "; " + command + treeAddress;
  531. coutDebug("Running: " + command);
  532. system(command.c_str());
  533. return 1;
  534. }
  535. int applyVetoPizMuMuMass(int Run){
  536. for (auto &year: yearsData(Run)){
  537. applyVetoPizMuMuMass(year, "up",getRunID(year), true);
  538. applyVetoPizMuMuMass(year, "up",getRunID(year), false);
  539. applyVetoPizMuMuMass(year, "down",getRunID(year), true);
  540. applyVetoPizMuMuMass(year, "down",getRunID(year), false);
  541. }
  542. return 1;
  543. }