SND@LHC Software
Loading...
Searching...
No Matches
extractMuonsAndUpdateWeight.py
Go to the documentation of this file.
1from __future__ import print_function
2import os,ROOT
3import rootUtils as ut
4path = '/eos/experiment/ship/data/Mbias/background-prod-2018/'
5
6# functions, should extract events with muons, update weight based on process/decay and PoT
7
8muSources = {'eta':221,'omega':223,'phi':333,'rho0':113,'eta_prime':331}
9charmExtern = [4332,4232,4132,4232,4122,431,411,421]
10
11# for 10GeV Yandex Production 65.041 Billion PoT, weight = 768.75 for 5E13 pot
12weightMbias = 768.75
13
14# for 10GeV charm Production 102.2 Billion PoT equivalent, weight = 489.24
15# added another cycle
16weightCharm = 489.24 * 2./3.
17
18# for 10GeV charm Production 5336 Billion PoT equivalent, weight = 9.37
19weightBeauty = 9.37
20
21def muonUpdateWeight(sTree,diMuboost,xSecboost,noCharm=True):
22 nMu=0
23 for v in sTree.vetoPoint:
24 mu = v.GetTrackID()
25 t = sTree.MCTrack[mu]
26 if abs(t.GetPdgCode())!=13: continue
27 moID = abs(sTree.MCTrack[t.GetMotherId()].GetPdgCode())
28 if moID in charmExtern and noCharm: continue # take heavy flavours from separate production
29 nMu+=1
30 pName = t.GetProcName().Data()
31 t.SetWeight(weight)
32 if not pName.find('Hadronic inelastic')<0:
33 t.MultiplyWeight(1./diMuboost)
34 elif not pName.find('Lepton pair')<0:
35 t.MultiplyWeight(1./xSecboost)
36 elif not pName.find('Positron annihilation')<0:
37 t.MultiplyWeight(1./xSecboost)
38 elif not pName.find('Primary particle')<0 or not pName.find('Decay')<0:
39 if moID in muSources.values():
40 t.MultiplyWeight(1./diMuboost)
41 return nMu
42
43def PoT(f):
44 nTot = 0
45 # POT = 1000000000 with ecut=10.0 diMu100.0 X100.0
46 diMuboost = 0.
47 xSecboost = 0.
48 ncycles = 0
49 for k in f.GetListOfKeys():
50 if k.GetName() == 'FileHeader':
51 txt = k.GetTitle()
52 tmp = txt.split('=')[1]
53 tmp2 = tmp.split('with')[0]
54 if tmp2.find('E')<0: nTot += int(tmp2)
55 else: nTot += float(tmp2)
56 ncycles+=1
57 i = txt.find('diMu')
58 if i < 0:
59 diMuboost+=1.
60 else:
61 diMuboost+=float(txt[i+4:].split(' ')[0])
62 i = txt.find('X')
63 if i < 0:
64 xSecboost+=1.
65 else:
66 xSecboost+=float(txt[i+1:].split(' ')[0])
67 diMuboost=diMuboost/float(ncycles)
68 xSecboost=xSecboost/float(ncycles)
69 print("POT = ",nTot," number of events:",f.cbmsim.GetEntries(),' diMuboost=',diMuboost,' XsecBoost=',xSecboost)
70 return nTot,diMuboost,xSecboost
71
72def TotStat():
73 lfiles = os.listdir(path)
74 ntot = 0
75 for fn in lfiles:
76 f=ROOT.TFile(path+fn)
77 nPot,diMuboost,xSecboost = PoT(f)
78 ntot += nPot
79 print("Total statistics so far",ntot/1.E9," billion")
80
81def processFile(fin,noCharm=True):
82 f = ROOT.TFile.Open(os.environ['EOSSHIP']+path+fin)
83 nPot,diMuboost,xSecboost = PoT(f)
84 sTree = f.cbmsim
85 outFile = fin.replace(".root","_mu.root")
86 fmu = ROOT.TFile(outFile,"recreate")
87 newTree = sTree.CloneTree(0)
88 for n in range(sTree.GetEntries()):
89 sTree.GetEntry(n)
90 nMu = muonUpdateWeight(sTree,diMuboost,xSecboost,noCharm)
91 if nMu>0:
92 rc = newTree.Fill()
93 ff = f.FileHeader.Clone('Extracted Muon Background File')
94 txt = ff.GetTitle()
95 tmp = txt.split('=')[1]
96 newTxt = txt.replace(tmp.split('with')[0].replace(' ',''),str(nPot))
97 ff.SetTitle(newTxt)
98 fmu.cd()
99 ff.Write("FileHeader", ROOT.TObject.kSingleKey)
100 newTree.AutoSave()
101 fmu.Close()
102 return 0
103
104def run():
105 tmp = "pythia8_Geant4_10.0_cXX.root"
106 global weight
107 weight = weightMbias
108 for run in range(0,67000,1000):
109 rc = processFile(tmp.replace('XX',str(run)))
110 if rc == 0:
111 fmu = tmp.replace('XX',str(run)+"_mu")
112 rc = os.system("xrdcp "+fmu+" $EOSSHIP/eos/experiment/ship/data/Mbias/background-prod-2018/"+fmu)
113 if rc != 0:
114 print("copy to EOS failed, stop",fmu)
115 else:
116 rc = os.system("rm "+fmu)
117
118
120 tmp = "pythia8_Geant4_charm_XX-YY_10.0.root"
121 global weight
122 weight = weightCharm
123 for cycle in [0,1,2]:
124 for run in range(0,100,20):
125 crun = run+cycle*1000
126 fname = tmp.replace('XX',str(crun)).replace('YY',str(crun+19))
127 rc = processFile(fname,False)
128 if rc == 0:
129 fmu = fname.replace('.root',"_mu.root")
130 rc = os.system("xrdcp "+fmu+" $EOSSHIP/eos/experiment/ship/data/Mbias/background-prod-2018/"+fmu)
131 if rc != 0:
132 print("copy to EOS failed, stop",fmu)
133 else:
134 rc = os.system("rm "+fmu)
135
137 global weight
138 weight = weightBeauty
139 fname = "pythia8_Geant4_beauty_5336B_10.0.root"
140 rc = processFile(fname,False)
141 if rc == 0:
142 fmu = fname.replace('.root',"_mu.root")
143 rc = os.system("xrdcp "+fmu+" $EOSSHIP/eos/experiment/ship/data/Mbias/background-prod-2018/"+fmu)
144 if rc != 0:
145 print("copy to EOS failed, stop",fmu)
146 else:
147 rc = os.system("rm "+fmu)
148
149# merge with beauty in a second step
150
151
152# finished one file pythia8_Geant4_10.0_withCharm44000_mu.root 2712798 1113638
153
155 tmp = "pythia8_Geant4_charm_XX-YY_10.0.root"
156 mergedFile = "pythia8_Geant4_charm_102.2B_10.0_mu.root"
157 cmd = "hadd "+mergedFile
158 for cycle in [0,1]:
159 for run in range(0,100,20):
160 crun = run+cycle*1000
161 fname = tmp.replace('XX',str(crun)).replace('YY',str(crun+19))
162 fmu = " $EOSSHIP/eos/experiment/ship/data/Mbias/background-prod-2018/"+fname.replace('.root',"_mu.root")
163 cmd += fmu
164 rc = os.system(cmd)
165 rc = os.system("xrdcp "+mergedFile+" $EOSSHIP/eos/experiment/ship/data/Mbias/background-prod-2018/"+mergedFile)
166
167def mergeMbiasAndCharm(flavour="charm"):
168 done = []
169 timer = ROOT.TStopwatch()
170 timer.Start()
171 pp = os.environ['EOSSHIP']+path
172 Rndm=ROOT.TRandom3()
173 Rndm.SetSeed(0)
174 if flavour=="charm":
175 allFiles = {'charm':"pythia8_Geant4_charm_102.2B_10.0_mu.root"}
176 tmp = "pythia8_Geant4_10.0_cXX_mu.root"
177 else:
178 allFiles = {'beauty':"pythia8_Geant4_beauty_5336B_10.0_mu.root"}
179 tmp = "pythia8_Geant4_10.0_withCharmXX_mu.root"
180 for run in range(0,67000,1000):
181 allFiles[str(run)] = tmp.replace('XX',str(run))
182 nEntries = {}
183 Nall = 0
184 for x in allFiles:
185 f=ROOT.TFile.Open(pp+allFiles[x])
186 nEntries[x]=f.cbmsim.GetEntries()
187 Nall += nEntries[x]
188 nCharm = 0
189 nDone = 0
190 frac = nEntries[flavour]/float(Nall)
191 print("debug",frac)
192 os.system('xrdcp '+pp +allFiles[flavour] +' '+allFiles[flavour])
193 for k in allFiles:
194 if k==flavour: continue
195 if k in done: continue
196 os.system('xrdcp '+pp +allFiles[k] +' ' +allFiles[k])
197 os.system('hadd -f tmp.root '+ allFiles[flavour] + ' '+ allFiles[k] )
198 os.system('rm '+allFiles[k])
199 f = ROOT.TFile('tmp.root')
200 sTree = f.cbmsim
201 sTree.LoadBaskets(30000000000)
202 if flavour=="charm":
203 outFile = tmp.replace('cXX','withCharm'+k)
204 else:
205 outFile = tmp.replace('XX','andBeauty'+k)
206 fmu = ROOT.TFile(outFile,"recreate")
207 newTree = sTree.CloneTree(0)
208 nMbias = 0
209 # 0 - nEntries['charm']-1 , nEntries['charm'] - nEntries[0]-1, nEntries[0] - nEntries[1000]-1
210 # randomList = ROOT.TEntryList(chain)
211 myList = []
212 while nMbias<nEntries[k]:
213 copyEvent = True
214 if Rndm.Rndm() > frac:
215 # rc = randomList.Enter(nMbias+nEntries['charm'])
216 myList.append( nMbias+nEntries[flavour] )
217 nMbias+=1
218 else:
219 if nEntries[flavour]>nCharm:
220 # rc = randomList.Enter(nCharm)
221 myList.append( nCharm )
222 nCharm+=1
223 else: copyEvent = False
224 # chain.SetEntryList(randomList)
225 # nev = randomList.GetN()
226 nev = len(myList)
227 print("start:",outFile,nev)
228 for iev in range(nev) :
229 rc =sTree.GetEntry(myList[iev])
230 rc = newTree.Fill()
231 if (iev)%100000==0:
232 timer.Stop()
233 print("status:",timer.RealTime(),k,iev)
234 timer.Start()
235 newTree.AutoSave()
236 print("finished one file",outFile,nMbias,nCharm)
237 if flavour=="charm":
238 ff = f.FileHeader.Clone('With Charm Merged Muon Background File')
239 else:
240 ff = f.FileHeader.Clone('With Charm and Beauty Merged Muon Background File')
241 txt = ff.GetTitle()
242 fmu.cd()
243 ff.Write("FileHeader", ROOT.TObject.kSingleKey)
244 fmu.Close()
245 f.Close()
246 rc = os.system("xrdcp "+outFile+" $EOSSHIP/eos/experiment/ship/data/Mbias/background-prod-2018/"+outFile)
247 if rc != 0:
248 print("copy to EOS failed",outFile)
249 else:
250 rc = os.system("rm "+outFile)
251
252def testRatio(fname):
253 f=ROOT.TFile.Open(os.environ['EOSSHIP']+path+fname)
254 sTree = f.cbmsim
255 Nall = sTree.GetEntries()
256 charm = 0
257 for n in range(Nall):
258 rc = sTree.GetEvent(n)
259 for m in sTree.MCTrack:
260 pdgID = abs(m.GetPdgCode())
261 if pdgID in charmExtern:
262 charm+=1
263 break
264 print("charm found",charm," ratio ",charm/float(Nall))
265
266
267
268
269
muonUpdateWeight(sTree, diMuboost, xSecboost, noCharm=True)