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.

126 lines
4.7 KiB

  1. import root_pandas as rp
  2. import numpy as np
  3. from uproot3_methods import TLorentzVectorArray as lorentzarr
  4. from uproot3_methods import TLorentzVector as lorentz
  5. from tqdm import tqdm
  6. import sys
  7. from shutil import copyfile
  8. import os
  9. from ROOT import TFile, TTree, TList
  10. #fname = sys.argv[1]
  11. #tname = sys.argv[2]
  12. def makeNewKstar(fname,tname,MC):
  13. d = rp.read_root(fname+'.root', key=tname)
  14. # load DTF momenta in arrays of 4-vectors
  15. fourmom = {}
  16. for pi in ['mu_plus', 'mu_minus', 'pi_zero_resolved', 'K_plus']:
  17. fourmom[pi] = lorentzarr.from_cartesian(d[pi+'_PX_DTF'], d[pi+'_PY_DTF'], d[pi+'_PZ_DTF'], d[pi+'_PE_DTF'])
  18. # calculate the rescaling (k)
  19. Bplus_M_PDG = 5279.34
  20. kvalues = []
  21. for i in tqdm(range(len(d)), desc='calculating pi0 rescaling'):
  22. ks = np.linspace(0.2, 2, 1000)
  23. ppi0 = fourmom['pi_zero_resolved'][i]
  24. pi0_rescaled = lorentzarr.from_xyzm(ppi0.x*ks, ppi0.y*ks, ppi0.z*ks, fourmom['pi_zero_resolved'][i].mass)
  25. Bmass_rescaled = (pi0_rescaled + fourmom['K_plus'][i] + fourmom['mu_plus'][i] + fourmom['mu_minus'][i]).mass
  26. bestk = ks[np.argmin(abs(Bmass_rescaled-Bplus_M_PDG))]
  27. kvalues.append(bestk)
  28. # now apply the rescaling
  29. pi0_p3 = fourmom['pi_zero_resolved'].p3 * kvalues
  30. fourmom['pi0_rescaled'] = lorentzarr.from_xyzm(pi0_p3.x, pi0_p3.y, pi0_p3.z, fourmom['pi_zero_resolved'].mass)
  31. d['pi_zero_resolved_PX_scaled'] = fourmom['pi0_rescaled'].x
  32. d['pi_zero_resolved_PY_scaled'] = fourmom['pi0_rescaled'].y
  33. d['pi_zero_resolved_PZ_scaled'] = fourmom['pi0_rescaled'].z
  34. d['pi_zero_resolved_PT_scaled'] = fourmom['pi0_rescaled'].pt
  35. d['pi_zero_resolved_PE_scaled'] = fourmom['pi0_rescaled'].E
  36. d['K_star_plus_M_scaled'] = (fourmom['pi0_rescaled'] + fourmom['K_plus']).mass
  37. if(MC):
  38. truefourmom = {}
  39. for pi in ['K_star_plus']:
  40. truefourmom[pi] = lorentzarr.from_cartesian(d[pi+'_TRUEP_X'], d[pi+'_TRUEP_Y'], d[pi+'_TRUEP_Z'], d[pi+'_TRUEP_E'])
  41. d['K_star_plus_M_TRUE'] = truefourmom['K_star_plus'].mass
  42. d.to_root(fname+'_pi0rescaled.root', key=tname, store_index=False)
  43. return
  44. def cutFile(fname, tname):
  45. d = rp.read_root(fname+'_pi0rescaled.root', key=tname,where='K_star_plus_M_scaled>791.66 & K_star_plus_M_scaled<991.66') #It takes & not &&!!!!!!!!
  46. d.to_root(fname+'_pi0rescaledCut.root', key=tname, store_index=False)
  47. copyfile(fname+'_pi0rescaledCut.root',fname+'.root')
  48. os.remove(fname+'_pi0rescaledCut.root')
  49. def mergeFiles(fname1, fname2, outname, tname):
  50. tfile1 = TFile(fname1, 'read')
  51. tfile2 = TFile(fname2, 'read')
  52. ttree1 = tfile1.Get(tname)
  53. ttree2 = tfile2.Get(tname)
  54. treeList = TList()
  55. treeList.append(ttree1)
  56. treeList.append(ttree2)
  57. outFile = TFile(outname,'recreate')
  58. outFile.cd()
  59. outTree = TTree.MergeTrees(treeList)
  60. outFile.Write()
  61. outFile.Close()
  62. return
  63. years = [2011,2012,2015,2016,2017,2018]
  64. treeName = "DecayTree"
  65. mainPath = "/home/lhcb/kopecna/B2KstarMuMu_clean/Data/Tuples"
  66. #MC
  67. for pol in ["down","up"]:
  68. for yr in years:
  69. if (yr==2015): continue
  70. fileName = mainPath+"/MC/"+str(yr)+pol+"/"+str(yr)+pol+"_pi0Resolved"
  71. makeNewKstar(fileName,treeName,True)
  72. cutFile(fileName,treeName)
  73. #Ref
  74. for pol in ["down","up"]:
  75. for yr in years:
  76. if (yr>2016): continue
  77. fileName = mainPath+"/RefMC/"+str(yr)+pol+"/"+str(yr)+pol+"_pi0Resolved"
  78. makeNewKstar(fileName,treeName,True)
  79. cutFile(fileName,treeName)
  80. #PHSP
  81. for pol in ["down","up"]:
  82. for yr in years:
  83. fileName = mainPath+"/PHSP/"+str(yr)+pol+"/"+str(yr)+pol+"_pi0Resolved"
  84. makeNewKstar(fileName,treeName,True)
  85. cutFile(fileName,treeName)
  86. #Data
  87. for pol in ["down","up"]:
  88. for yr in years:
  89. fileName = mainPath+"/Data/"+str(yr)+pol+"/"+str(yr)+pol+"_pi0Resolved"
  90. makeNewKstar(fileName,treeName,False)
  91. cutFile(fileName,treeName)
  92. '''
  93. #Inclusive
  94. for pol in ["down","up"]:
  95. for yr in years:
  96. if (yr==2015): continue
  97. if (yr>2016): continue
  98. fileName = "/home/lhcb/kopecna/B2KstarMuMu/data/data/MC/BackgroundSamples/BtoXJpsi/"+str(yr)+pol+"/"+str(yr)+pol+"_pi0Resolved"
  99. makeNewKstar(fileName,treeName,True)
  100. cutFile(fileName,treeName)
  101. for yr in [2011,2012,2016]:
  102. fileName1 = "/home/lhcb/kopecna/B2KstarMuMu/data/data/MC/BackgroundSamples/BtoXJpsi/"+str(yr)+"down/"+str(yr)+"down_pi0Resolved.root"
  103. fileName1 = "/home/lhcb/kopecna/B2KstarMuMu/data/data/MC/BackgroundSamples/BtoXJpsi/"+str(yr)+"up/"+str(yr)+"up_pi0Resolved.root"
  104. outname = "/home/lhcb/kopecna/B2KstarMuMu/data/data/MC/"+str(yr)+"_Inclusive_BDTinput.root"
  105. mergeFiles(fileName1, fileName1, outname, "DecayTreeTruthMatched")
  106. '''