FairShip
Loading...
Searching...
No Matches
compactingBackgroundProduction.py
Go to the documentation of this file.
1# SPDX-License-Identifier: LGPL-3.0-or-later
2# SPDX-FileCopyrightText: Copyright CERN for the benefit of the SHiP Collaboration
3
4import datetime
5import os
6import pickle
7import sys
8
9import ROOT
10import rootUtils as ut
11
12pdg = ROOT.TDatabasePDG()
13charm = False
14
15# functions, PoT: statistics per file
16# TotStat: total statistics in given directory
17# GetGoodAndBadRuns: list of good and bad runs within given dates
18# addRuns: concatenate good runs
19# YandexProd(): specify start end date, select good runs, write to pickle file, concatenate runs requires start number !
20
21# prod = ''
22# globalPath="/eos/experiment/ship/skygrid/background-prod-2018/"
23# fnames = 'pythia8_Geant4_1_10.0.root'
24# ecut = '10.0'
25
26# final statistics: 65041000000 pot -> weight 768.75
27
28prod = "1GeV"
29globalPath = "/eos/experiment/ship/skygrid/background-prod-2018-1gev/"
30fnames = "pythia8_Geant4_1_1.0.root"
31ecut = "1.0"
32
33
34def GetGoodAndBadRuns(startDate, endDate) -> tuple[list[str], list[int]]:
35 # find bad runs, try to recover
36 goodRuns = []
37 badRuns = []
38 fileName = "pythia8_Geant4_1_" + ecut + ".root"
39 os.system("ls -l --full-time " + globalPath + " >inventory.lst")
40 with open("inventory.lst") as f:
41 runs = f.readlines()
42 N = 0
43 for r in runs:
44 tmp = r.split(" ")
45 if len(tmp) != 9:
46 print("wrong format", tmp)
47 continue
48 date = tmp[5].split("-")
49 time = tmp[6].split(":")
50 fileDate = datetime.datetime(
51 int(date[0]), int(date[1]), int(date[2]), int(time[0]), int(time[1]), int(time[2].split(".")[0])
52 )
53 if fileDate < startDate or fileDate > endDate:
54 continue
55 theRun = int(tmp[8].replace("\n", ""))
56 test = os.listdir(globalPath + str(theRun))
57 if len(test) < 2:
58 continue
59 N += 1
60 rc = os.system(
61 'grep -q "Number of events produced with activity after hadron absorber" '
62 + globalPath
63 + str(theRun)
64 + "/stdout"
65 )
66 if rc != 0:
67 badRuns.append(theRun)
68 continue
69 for x in test:
70 if x.find("run_fixedTarget") > 0:
71 test2 = os.listdir(globalPath + str(theRun) + "/" + x)
72 f = globalPath + str(theRun) + "/" + x + "/" + fileName
73 if fnames + "tmp" in test2:
74 f = f + "tmp"
75 elif fnames in test2:
76 f = f
77 else:
78 badRuns.append(theRun)
79 continue
80 try:
81 t = ROOT.TFile.Open(os.environ["EOSSHIP"] + f)
82 if not t:
83 badRuns.append(theRun)
84 continue
85 if N % 1000 == 0:
86 print(N, t.GetName())
87 if t.ReadKeys() == 0:
88 t.Close()
89 badRuns.append(theRun)
90 continue
91 if t.FindObjectAny("cbmsim"):
92 goodRuns.append(f)
93 t.Close()
94 except Exception:
95 badRuns.append(theRun)
96 continue
97 return goodRuns, badRuns
98
99
100def addRuns(goodRuns, Nstart: int = 0) -> None:
101 N = 0
102 while 1 > 0:
103 cmd = ""
104 for i in range(N, min(N + 1000, len(goodRuns))):
105 cmd += " $EOSSHIP" + goodRuns[i]
106 tmpFile = "pythia8_Geant4_" + ecut + "_c" + str(N + Nstart) + ".root"
107 rc = os.system("hadd -j 10 -O " + tmpFile + " " + cmd)
108 if rc != 0:
109 print("hadd failed, stop", N)
110 return
111 rc = os.system("xrdcp " + tmpFile + " $EOSSHIP/eos/experiment/ship/data/Mbias/background-prod-2018/" + tmpFile)
112 if rc != 0:
113 print("copy to EOS failed, stop", N, N + Nstart)
114 else:
115 rc = os.system("rm " + tmpFile)
116 N += 1000
117 if len(goodRuns) < N:
118 break
119
120
121def YandexProd(startDate, endDate) -> None:
122 # startDate = datetime.datetime(2018, 1, 1, 0, 0) # N=0
123 # endDate = datetime.datetime(2018, 2, 1, 0, 0)
124 # startDate = datetime.datetime(2018, 2, 1, 0, 0) # N=23000
125 # endDate = datetime.datetime(2018, 2, 6, 0, 0)
126 # startDate = datetime.datetime(2018, 2, 6, 0, 0) # N=33000
127 # endDate = datetime.datetime(2018, 2, 12, 0, 0)
128 # startDate = datetime.datetime(2018, 2, 12, 0, 0) # N=45000
129 # endDate = datetime.datetime(2018, 2, 15, 0, 0)
130 # startDate = datetime.datetime(2018, 2, 15, 0, 0) # N=51000
131 # endDate = datetime.datetime(2018, 2, 21, 0, 0)
132 # startDate = datetime.datetime(2018, 2, 21, 0, 0) # N=63000
133 # endDate = datetime.datetime(2018, 2, 2, 0, 0)
134 # startDate = datetime.datetime(2018, 1, 1, 0, 0) # start with 1 GeV production
135 # endDate = datetime.datetime(2018, 2, 27, 0, 0) # N=0
136 # startDate = datetime.datetime(2018, 2, 27, 0, 0) #
137 # endDate = datetime.datetime(2018, 3, 6, 0, 0) # N=3000
138 # startDate = datetime.datetime(2018, 3, 6, 0, 0) #
139 # endDate = datetime.datetime(2018, 3, 12, 0, 0) # N=9000
140 goodRuns, badRuns = GetGoodAndBadRuns(startDate, endDate)
141 pName = (
142 "goodAndBadRuns"
143 + prod
144 + "_"
145 + startDate.__str__().split(" ")[0]
146 + "_"
147 + endDate.__str__().split(" ")[0]
148 + ".pkl"
149 )
150 with open(pName, "w") as fpi:
151 database = {}
152 database["goodruns"] = goodRuns
153 database["badRuns"] = badRuns
154 pickle.dump(database, fpi)
155 with open(pName) as fpi:
156 database = pickle.load(fpi)
157 addRuns(database["goodruns"], 20000) # next cycle
158
159
160def addAllHistograms() -> None:
161 h = {}
162 ecut = "10.0"
163 Nmax = 45000
164 path = os.environ["EOSSHIP"] + "/eos/experiment/ship/data/Mbias/background-prod-2018/"
165 ut.bookCanvas(h, key="canvas", title="debug", nx=1600, ny=1200, cx=1, cy=1)
166 h["canvas"].SetLogy(1)
167 for i in range(0, Nmax, 1000):
168 fname = "pythia8_Geant4_" + ecut + "_c" + str(i) + ".root"
169 ut.readHists(h, path + fname)
170 if i == 0:
171 h[1012].Draw()
172 for x in h:
173 if h[x].GetName().find("proj") > 0:
174 h.pop(x)
175 ut.writeHists(h, "pythia8_Geant4_" + ecut + "_c" + str(Nmax) + "-histos.root")
176
177
178def compactifyCascade(cycle) -> None:
179 ncpus = 20
180 path = "/afs/cern.ch/project/lbcern/vol1/truf/charm/"
181 cmd = ""
182 Ntot = 0
183 NperJob = 2000000
184 for i in range(cycle, cycle + ncpus):
185 fName = path + "run" + str(i) + "/Cascade-run" + str(i) + "-parp16-MSTP82-1-MSEL4.root"
186 with open(path + "run" + str(i) + "/log" + str(i)) as f:
187 success = False
188 for line in f.readlines():
189 if not line.find("Macro finished successfully") < 0:
190 success = True
191 if not success:
192 print("job not finished properly", fName)
193 continue
194 cmd += fName + " "
195 Ntot += NperJob
196 if cmd.find("root") < 0:
197 print("no file found, exit")
198 else:
199 stat = str(int(Ntot / 1e6)) + "Mpot"
200 outFile = "Cascade-run" + str(cycle) + "-" + str(cycle + ncpus - 1) + "-parp16-MSTP82-1-MSEL4-" + stat + ".root"
201 rc = os.system("hadd -O " + outFile + " " + cmd)
202 rc = os.system("xrdcp " + outFile + " $EOSSHIP/eos/experiment/ship/data/Mbias/background-prod-2018/" + outFile)
203 if rc != 0:
204 print("copy to EOS failed, stop", outFile)
205 else:
206 rc = os.system("rm " + outFile)
207
208
209# some old stuff
210def compactify(charm: bool | str, runMin=0, runMax=0, checkOnly=False) -> None:
211 globalPath = "/afs/cern.ch/project/lbcern/vol2/truf/muonBackground"
212 ecut = "10.0"
213 if charm:
214 allDirs = os.listdir(globalPath + "/charm")
215 allFiles = []
216 for r in range(int(runMin), int(runMax) + 1):
217 # collect the 20 subdirectories connected to a run
218 nr = "9000" + str(r)
219 subruns = []
220 badFiles = []
221 for x in allDirs:
222 for k in range(20):
223 sr = f"{k:0>2}"
224 if not x.find(nr + sr) < 0:
225 if not x.find("log") < 0:
226 continue
227 fn = "pythia8_Geant4_" + nr + sr + "_" + ecut + ".root"
228 file_path = globalPath + "/charm/" + x + "/" + fn
229 if os.path.exists(file_path):
230 try:
231 t = ROOT.TFile.Open(file_path)
232 if not t:
233 badFiles.append(file_path)
234 continue
235 if t.ReadKeys() == 0:
236 badFiles.append(file_path)
237 continue
238 if t.FindObjectAny("cbmsim"):
239 subruns.append(file_path)
240 except Exception:
241 badFiles.append(file_path)
242 continue
243 ldir = " "
244 for x in subruns:
245 ldir += x + " "
246 outputr = "pythia8_Geant4_charm_" + nr + "_" + ecut + ".root"
247 allFiles += outputr + " "
248 os.system("hadd " + outputr + " " + ldir)
249 output = "pythia8_Geant4_charm_" + str(runMin) + "_" + str(runMax) + "_" + ecut + ".root"
250 os.system("hadd " + output + " " + allFiles)
251 makeHistos(output)
252 else:
253 output = "pythia8_Geant4_" + str(runMin) + "-" + str(runMax) + "_" + ecut + ".root"
254 if not checkOnly:
255 ldir = ""
256 badFiles = []
257 for d in os.listdir(globalPath):
258 if d.find("run_fixedTarget") < 0:
259 continue
260 srun = d.split("run_fixedTarget_")[1]
261 run = int(srun)
262 if not run > runMax and not run < runMin:
263 f = globalPath + "/" + d + "/pythia8_Geant4_" + srun + "_" + ecut + ".root "
264 ftmp = f.replace(".root ", ".roottmp")
265 if os.path.exists(ftmp):
266 f = ftmp + " "
267 try:
268 t = ROOT.TFile.Open(f)
269 if not t:
270 badFiles.append(d)
271 continue
272 if t.ReadKeys() == 0:
273 badFiles.append(d)
274 continue
275 if t.FindObjectAny("cbmsim"):
276 ldir += f
277 except Exception:
278 badFiles.append(d)
279 continue
280 os.system("hadd " + output + " " + ldir)
281 makeHistos(output)
282
283
284def makeHistos(rfile: str) -> None:
285 f = ROOT.TFile.Open(rfile)
286 sTree = f.Get("cbmsim")
287 nTot = 0
288 for k in f.GetListOfKeys():
289 if k.GetName() == "FileHeader":
290 tmp = k.GetTitle().split("=")[1]
291 tmp2 = tmp.split("with")[0]
292 if tmp2.find("E") < 0:
293 nTot += int(tmp2)
294 else:
295 nTot += float(tmp2)
296 print("POT = ", nTot, " number of events:", sTree.GetEntries())
297 # particle statistics
298 h = {}
299 ut.bookHist(h, "pids", "pid", 19999, -9999.5, 9999.5)
300 ut.bookHist(h, "test", "muon p/pt", 100, 0.0, 400.0, 100, 0.0, 5.0)
301 for n in range(sTree.GetEntries()):
302 sTree.GetEvent(n)
303 for p in sTree.vetoPoint:
304 t = sTree.MCTrack[p.GetTrackID()]
305 pid = t.GetPdgCode()
306 h["pids"].Fill(pid)
307 if abs(pid) == 13:
308 procID = t.GetProcName().Data()
309 mother = t.GetMotherId()
310 if not mother < 0:
311 moPid = sTree.MCTrack[mother].GetPdgCode()
312 name = pdg.GetParticle(moPid).GetName()
313 name = procID + " " + name
314 if name not in h:
315 h[name] = h["test"].Clone(name)
316 h[name].Fill(t.GetP(), t.GetPt())
317 if procID not in h:
318 h[procID] = h["test"].Clone(procID)
319 h[procID].Fill(t.GetP(), t.GetPt())
320 for x in h:
321 h[x].Scale(1.0 / nTot)
322 tmp = rfile.split("/")
323 hname = tmp[len(tmp) - 1].replace("pythia8_Geant4", "Histos")
324 ut.writeHists(h, hname)
325
326
327hunbiased = {}
328hbiased = {}
329import operator
330
331
332def makePrintout() -> None:
333 # Histos_2000000-2001500_10.0.root
334 ut.readHists(hunbiased, "/media/microdisk/HNL/muonBackground/Histos_1000000-1000600_10.0.root")
335 ut.readHists(hbiased, "hadded_Histos_1_10.0.root")
336
337 unbiased = {}
338 biased = {}
339 for key in hunbiased:
340 unbiased[key] = hunbiased[key].GetSumOfWeights()
341 for key in hbiased:
342 if key.find("proj") < 0 and key.find("test") < 0 and key.find("pids") < 0:
343 biased[key] = hbiased[key].GetSumOfWeights()
344 p = {}
345 for i in range(1, hbiased["pids"].GetNbinsX() + 1):
346 c = hbiased["pids"].GetBinContent(i)
347 if c > 0:
348 p[int(hbiased["pids"].GetBinCenter(i))] = c
349
350 sorted_p = sorted(p.items(), key=operator.itemgetter(1))
351 for p in sorted_p:
352 print("%25s : %5.2G" % (pdg.GetParticle(p[0]).GetName(), float(p[1])))
353 sorted_pr = sorted(biased.items(), key=operator.itemgetter(1))
354 print("origin of muons")
355 for p in sorted_pr:
356 if not p[0].find("Hadronic inelastic") < 0:
357 if len(p[0]) > len("Hadronic inelastic"):
358 continue
359 denom = 0
360 if p[0] in unbiased:
361 denom = unbiased[p[0]]
362 if denom > 0:
363 fac = float(p[1]) / denom
364 print("%40s : %5.2G %5.1F" % (p[0], float(p[1]), fac))
365 else:
366 print("%40s : %5.2G " % (p[0], float(p[1])))
367
368
369if len(sys.argv) > 1:
370 runMin = sys.argv[1]
371 runMax = sys.argv[2]
372 if len(sys.argv) > 3:
373 charm = sys.argv[3]
374 compactify(charm)
375else:
376 # production without boost factor
377 # runMin = 1000000
378 # runMax = 1000600
379 # production with boost factor 100
380 # runMin = 2001001
381 # runMax = 2001500 # more does not work, maybe limit of hadd? 2001359
382 # charm run_fixedTarget_9000119/pythia8_Geant4_9000219_10.0.root
383 print("methods to run: makeHistos(rfile),makePrintout()")
384
385# yandex compactification, Feb 8
386# ... startDate = datetime.datetime(2018, 1, 1, 0, 0)
387# ... endDate = datetime.datetime(2018, 2, 1, 0, 0)
388# ... goodAndBadRuns_2018-01-01_2018-02-01.pkl
389# hadd Source file 339: root://eospublic.cern.ch//eos/experiment/ship/skygrid/background-prod-2018/424121/80eefea19104_run_fixedTarget_1/pythia8_Geant4_1_10.0.root
390# hadd Target path: pythia8_Geant4_10.0_c22000.root:/
391# len(database['goodruns']) .22339
392# database['goodruns'][22338] '/eos/experiment/ship/skygrid/background-prod-2018/424121/80eefea19104_run_fixedTarget_1/pythia8_Geant4_1_10.0.root'
393# ... startDate = datetime.datetime(2018, 2, 1, 0, 0)
394# ... endDate = datetime.datetime(2018, 2, 6, 0, 0)
395
396# for charm
397# chicc=1.7e-3; //prob to produce primary ccbar pair/pot
398# 20*2000000 are equal to 23.5E9
399# for beauty
400# chibb=1.6e-7;
401
402
403# something went wrong Yandex2018Prod-23000.root onwards up to 43000, only have of yield, 44000 ok !!!
404def removeStupidFiles() -> None:
405 path = "/eos/experiment/ship/data/Mbias/background-prod-2018"
406 for f in os.listdir(path):
407 ff = path + "/" + f
408 x = os.path.getsize(ff)
409 if x < 500:
410 os.remove(ff)
411
412
413def check4DoubleRuns() -> None:
414 allRuns = [
415 "goodAndBadRuns_2018-01-01_2018-02-01.pkl",
416 "goodAndBadRuns_2018-02-01_2018-02-06.pkl",
417 "goodAndBadRuns_2018-02-06_2018-02-12.pkl",
418 "goodAndBadRuns_2018-02-12_2018-02-15.pkl",
419 "goodAndBadRuns_2018-02-15_2018-02-21.pkl",
420 "goodAndBadRuns_2018-02-21_2018-02-26.pkl",
421 ]
422 Nruns = 0
423 for x in allRuns:
424 with open(x) as fn:
425 dn = pickle.load(fn)
426 Nruns += len(dn["goodruns"])
427 print("Total number of runs:", Nruns)
428
429 for n in range(len(allRuns) - 1):
430 with open(allRuns[n]) as fn:
431 dn = pickle.load(fn)
432 for m in range(n + 1, len(allRuns)):
433 with open(allRuns[m]) as fm:
434 dm = pickle.load(fm)
435 for rn in dn["goodruns"]:
436 for rm in dm["goodruns"]:
437 if rn == rm:
438 print("double entry found", rn, allRuns[n], allRuns[m])
None compactify(bool|str charm, runMin=0, runMax=0, checkOnly=False)
tuple[list[str], list[int]] GetGoodAndBadRuns(startDate, endDate)
int open(const char *, int)
Opens a file descriptor.