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.

186 lines
6.8 KiB

  1. #Core code for removal of multiple cnadidates from the sample
  2. #Renata Kopecna
  3. from ROOT import TTree, TChain, TFile, TVector3, TObject
  4. import numpy as np
  5. import itertools
  6. #import matplotlib.pyplot as plt
  7. from array import array
  8. from collections import Counter
  9. import time
  10. import sys
  11. sys.path.insert(0,'/home/lhcb/kopecna/B2KstarMuMu_clean/Code/Selection/ComparisonTool')
  12. #import Utils
  13. from Utils3 import getTreePath, getOptionsDictionary,treeName, stopWatch, getTreeWithPairingBranch
  14. test = False
  15. #Read Inclusive sample (horribly hacked in)
  16. IsInc = False
  17. def isMultipleCandidate(year, Run, Data, MC, TM, TMpid, gammaTM, ReferenceChannel, PHSP, KshortDecaysInVelo,verbose):
  18. #read file
  19. sWeighted = False
  20. bWeighted = False
  21. b2Dweighted = False
  22. if ((TM + TMpid) > 1): raise Exception("Select only one of TM or TMpid!")
  23. optionsDictionary = getOptionsDictionary(int(year),Run,"both",MC,TM,ReferenceChannel,PHSP,
  24. True, True, sWeighted,bWeighted,b2Dweighted,"",KshortDecaysInVelo,False)
  25. #Add tree to TChain (only one, since it has to be BDTed file)
  26. treePath, treePath2 = getTreePath(optionsDictionary,verbose) #treePath2 is not used
  27. if (IsInc): treePath = treePath.replace("KplusPi0Resolved","Inclusive",-1)
  28. if (verbose): print("Opening " +treePath)
  29. treeFileTmp = TFile.Open(treePath,"UPDATE")
  30. treeTmp = treeFileTmp.Get(treeName(MC,TM,True))
  31. if (not MC and TM):
  32. print ("[WARN]\t\tTM cannot be set to True for data! setting TM to False")
  33. TM = False
  34. if (test) : nEvts = 23 #testing
  35. else: nEvts = treeTmp.GetEntries()
  36. #Check if the pairing function branch is in the tree
  37. #If yes, continue, if not, create the branch:
  38. if not("pairingNumber" in treeTmp.GetListOfBranches()):
  39. treeFile, tree = getTreeWithPairingBranch(treeFileTmp,treeTmp,Run,test)
  40. else: treeFile, tree = treeFileTmp,treeTmp
  41. #Load needed branches
  42. pairingNumber = array('L',[0])
  43. MLPresponse = array('d',[0])
  44. TMed = array('i',[0])
  45. TM_gammas = array('i',[0])
  46. #Activate and read branches
  47. tree.SetBranchStatus('*',0)
  48. tree.SetBranchStatus('pairingNumber',1)
  49. tree.SetBranchStatus('MLPresponse',1)
  50. if (TM): tree.SetBranchStatus('TMedBKGCAT',1)
  51. elif (TMpid):
  52. tree.SetBranchStatus('TMed',1)
  53. tree.SetBranchStatus('TM_gammas',1)
  54. #elif (pi0TMoff): tree.SetBranchStatus('TMed_noPi0',1)
  55. tree.SetBranchAddress('pairingNumber',pairingNumber)
  56. tree.SetBranchAddress('MLPresponse',MLPresponse)
  57. if (TM): tree.SetBranchAddress('TMedBKGCAT',TMed)
  58. elif (TMpid):
  59. tree.SetBranchAddress('TMed',TMed)
  60. tree.SetBranchAddress('TM_gammas',TM_gammas)
  61. #elif (pi0TMoff): tree.SetBranchAddress('TMed_noPi0',TMed)
  62. #Load events into a 3D array: [row,column] (1 row = 1 event)
  63. evtNumberArray = np.zeros((nEvts,3), dtype=float)
  64. for evt in range (nEvts):
  65. tree.GetEntry(evt)
  66. evtNumberArray[evt,0] = pairingNumber[0]
  67. evtNumberArray[evt,1] = MLPresponse[0]
  68. if (TM): evtNumberArray[evt,2] = TMed[0]
  69. if (TMpid):
  70. if (gammaTM): evtNumberArray[evt,2] = TMed[0] and TM_gammas[0]<4
  71. else: evtNumberArray[evt,2] = TMed[0] and TM_gammas[0]<6
  72. if (test): print (evtNumberArray)
  73. print ("Loaded MLP+event number array.")
  74. #Get all MLP values
  75. MLPvalues = np.sort(evtNumberArray[:,1])
  76. if (test): print (MLPvalues)
  77. #Select only TMed events
  78. if (TM or TMpid):
  79. evtNumberArray = evtNumberArray[np.where(evtNumberArray[:,2] == 1)]
  80. if(test): print (evtNumberArray)
  81. #Run the first round to remove lonely events first (huge speed gain)
  82. dictCountMLP = {}
  83. unique, counts = np.unique(evtNumberArray[:,0],return_counts = True)
  84. dictEvtCount = dict(zip(unique, counts))
  85. for key in dictEvtCount.keys():
  86. key_int = int(key)
  87. if (dictEvtCount[key_int] == 1):
  88. dictCountMLP[key_int]=0.0
  89. evtNumberArray = evtNumberArray[np.where(evtNumberArray[:,0] != key_int)]
  90. if(test): print (evtNumberArray)
  91. #Loop over all possible MLP values
  92. count = 0
  93. start = time.time()
  94. for MLPcut in MLPvalues:
  95. if (verbose and count%1000 == 0): print ("Reading event", count)
  96. #Select only events with MLP values than the previous smallest value
  97. tmpArr = evtNumberArray[(evtNumberArray[:,1]>=MLPcut)]
  98. if (len(tmpArr)==0): break
  99. unique, counts = np.unique(tmpArr[:,0],return_counts = True)
  100. dictEvtCount = dict(zip(unique, counts))
  101. if (test): print ("dictEvtCount\n",dictEvtCount)
  102. for key in dictEvtCount.keys():
  103. key_int = int(key)
  104. if key_int not in dictCountMLP:
  105. if dictEvtCount[key_int] == 1:
  106. dictCountMLP[key_int]=MLPcut
  107. count += 1
  108. end = time.time()
  109. print ("Needed ", stopWatch(end-start), "to finish.")
  110. if (test): print (dictCountMLP)
  111. print ("Multiple candidates dictionary created")
  112. tree.SetBranchStatus('*',1)
  113. #Add new branch
  114. IsAloneAt = array('d',[0])
  115. if (MC):
  116. if (TM): b_IsAloneAt = tree.Branch("IsAloneAt", IsAloneAt, 'IsAloneAt/D')
  117. elif (TMpid):
  118. if (gammaTM): b_IsAloneAt = tree.Branch("IsAloneAt_TMed", IsAloneAt, 'IsAloneAt_TMed/D')
  119. else: b_IsAloneAt = tree.Branch("IsAloneAt_TMed_rndGamma", IsAloneAt, 'IsAloneAt_TMed_rndGamma/D')
  120. else: b_IsAloneAt = tree.Branch("IsAloneAtNotTM", IsAloneAt, 'IsAloneAtNotTM/D')
  121. else:
  122. b_IsAloneAt = tree.Branch("IsAloneAt", IsAloneAt, 'IsAloneAt/D')
  123. if (TM or TMpid):
  124. for evt in range (nEvts):
  125. tree.GetEntry(evt)
  126. if (test): print (pairingNumber[0],dictCountMLP.get(pairingNumber[0]))
  127. if (not TMed[0]): IsAloneAt[0] = 2.0
  128. elif (TMpid and (gammaTM and TM_gammas[0]>3)): IsAloneAt[0] = 2.0
  129. elif (TMpid and (not gammaTM and TM_gammas[0]==6)): IsAloneAt[0] = 2.0
  130. else:
  131. if (pairingNumber[0] in dictCountMLP.keys()): #check makes it slower, but oh well
  132. IsAloneAt[0] = dictCountMLP.get(pairingNumber[0])
  133. else:
  134. IsAloneAt[0] = -1.0
  135. b_IsAloneAt.Fill()
  136. else:
  137. for evt in range (nEvts):
  138. tree.GetEntry(evt)
  139. if (test): print (pairingNumber[0],dictCountMLP.get(pairingNumber[0]))
  140. if (pairingNumber[0] in dictCountMLP.keys()): #check makes it slower, but oh well
  141. IsAloneAt[0] = dictCountMLP.get(pairingNumber[0])
  142. else:
  143. IsAloneAt[0] = -1.0
  144. b_IsAloneAt.Fill()
  145. print ("Writing into file...")
  146. treeFile.Write("",TFile.kOverwrite)
  147. treeFile.Close()
  148. print ("All done, exiting!")
  149. return
  150. if __name__ == '__main__':
  151. removeMultiple()