FairShip
Loading...
Searching...
No Matches
runCharmHadProd.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 os
5import sys
6
7import ROOT
8
9ncpus = 20
10msel = "4"
11# msel = "5"
12
13if msel == "4":
14 nev = 2000000 # 0.1s / event
15 path = "/afs/cern.ch/project/lbcern/vol1/truf/charm/"
16 chicc = 1.7e-3 # prob to produce primary ccbar pair/pot
17else:
18 nev = 1000000
19 path = "/afs/cern.ch/project/lbcern/vol1/truf/beauty/"
20 chicc = 1.6e-7 # prob to produce primary bbbar pair/pot
21
22# path = "/home/truf/charm/"
23
24# store only charm
25
26# story about weights, run_fixedTarget puts into file header: pot = nrpotspill / wspill
27# with wspill = nrpotspill*chicc/nrcpot*nEvents/nev -> pot = nev * nrcpot / (chicc * nEvents)
28# nEvents nr of events in input file, number of events requested for job, ratio should be 1
29# pot = nrcpot / chicc
30# nrcpot=((TH1F*)fin->Get("2"))->GetBinContent(1)/2.; // pot are counted double, i.e. for each signal, i.e. pot/2.
31
32
33def makeHadrons(run) -> None:
34 for n in range(ncpus):
35 x.Rndm() * 1000000000.0
36 os.system("mkdir run" + str(run))
37 os.chdir("run" + str(run))
38 cmd = (
39 "python $FAIRSHIP/macro/makeCascade.py -m "
40 + msel
41 + " -n "
42 + str(nev)
43 + " -t Cascade-run"
44 + str(run)
45 + "-parp16-MSTP82-1-MSEL"
46 + msel
47 + ".root"
48 )
49 # if not run in runList:
50 os.system(cmd + " >log" + str(run) + " &")
51 os.chdir("../")
52 run += 1
53
54
55def makeBackground(run, cycle: int = 0) -> None:
56 for n in range(ncpus):
57 orun = run + cycle * 1000
58 os.chdir("run" + str(run))
59 inputFile = path + "run" + str(run) + "/Cascade-run" + str(run) + "-parp16-MSTP82-1-MSEL" + msel + ".root"
60 f = ROOT.TFile(inputFile)
61 nt = f.pythia6
62 N = nt.GetEntries()
63 f.Close()
64 if msel == "4":
65 cmd = (
66 "python $FAIRSHIP/muonShieldOptimization/run_fixedTarget.py --force --charm -V -e 10 -P -n "
67 + str(N)
68 + " -r "
69 + str(orun)
70 + " -b 100 -X 100 -I "
71 + inputFile
72 )
73 else:
74 cmd = (
75 "python $FAIRSHIP/muonShieldOptimization/run_fixedTarget.py --force --beauty -V -e 10 -P -n "
76 + str(N)
77 + " -r "
78 + str(orun)
79 + " -b 100 -X 100 -I "
80 + inputFile
81 )
82 os.system(cmd + " >logFT" + str(orun) + " &")
83 os.chdir("../")
84 run += 1
85
86
87cycle = 2
88runList = [20, 28, 30, 33]
89
90
91def makeBackgroundX(runList, cycle: int = 0) -> None:
92 for run in runList:
93 orun = run + cycle * 1000
94 os.chdir("run" + str(run))
95 inputFile = path + "run" + str(run) + "/Cascade-run" + str(run) + "-parp16-MSTP82-1-MSEL" + msel + ".root"
96 f = ROOT.TFile(inputFile)
97 nt = f.pythia6
98 N = nt.GetEntries()
99 f.Close()
100 cmd = (
101 "python $FAIRSHIP/muonShieldOptimization/run_fixedTarget.py --force --charm -V -e 10 -P -n "
102 + str(N)
103 + " -r "
104 + str(orun)
105 + " -b 100 -X 100 -I "
106 + inputFile
107 )
108 if run in runList:
109 os.system(cmd + " >logFT" + str(orun) + " &")
110 os.chdir("../")
111
112
113def merge(run, cycle: int = 0) -> None:
114 fname = "pythia8_Geant4_XX_10.0.root"
115 cmd = " "
116 for n in range(ncpus):
117 for x in os.listdir(path + "/run" + str(run + n)):
118 orun = run + cycle * 1000
119 if not x.find("run_fixedTarget_" + str(orun + n)) < 0:
120 if cycle == 0 and run == 0 and not x.find("1001") < 0:
121 continue
122 if cycle == 0 and run == 0 and not x.find("1010") < 0:
123 continue
124 cmd += path + "/run" + str(run + n) + "/" + x + "/" + fname.replace("XX", str(orun + n)) + " "
125 if msel == "4":
126 outFile = fname.replace("XX", "charm_" + str(orun) + "-" + str(orun + ncpus - 1))
127 else:
128 outFile = fname.replace("XX", "beauty_" + str(orun) + "-" + str(orun + ncpus - 1))
129 rc = os.system("hadd -O " + outFile + " " + cmd)
130 if rc != 0:
131 print("hadd failed, stop", outFile)
132 else:
133 rc = os.system("xrdcp " + outFile + " $EOSSHIP/eos/experiment/ship/data/Mbias/background-prod-2018/" + outFile)
134 if rc != 0:
135 print("copy to EOS failed, stop", outFile)
136 else:
137 rc = os.system("rm " + outFile)
138
139
140def mergeAll() -> None:
141 cmd = "hadd pythia8_Geant4_charm_153.3B_10.0_mu.root "
142 tmp = "/eos/experiment/ship/data/Mbias/background-prod-2018/pythia8_Geant4_charm_XX_10.0_mu.root"
143 for x in [
144 "0-19",
145 "20-39",
146 "40-59",
147 "60-79",
148 "80-99",
149 "1000-1019",
150 "1020-1039",
151 "1040-1059",
152 "1060-1079",
153 "1080-1099",
154 "2000-2019",
155 "2020-2039",
156 "2040-2059",
157 "2060-2079",
158 "2080-2099",
159 ]:
160 cmd += tmp.replace("XX", x) + " "
161 os.system(cmd)
162
163
164def compactifyCascade(run) -> None:
165 ncpus = 20
166 cmd = ""
167 Ntot = 0
168 NperJob = nev
169 for i in range(run, +ncpus):
170 fName = path + "run" + str(i) + "/Cascade-run" + str(i) + "-parp16-MSTP82-1-MSEL" + msel + ".root"
171 with open(path + "run" + str(i) + "/log" + str(i)) as f:
172 success = False
173 for line in f.readlines():
174 if not line.find("Macro finished successfully") < 0:
175 success = True
176 if not success:
177 print("job not finished properly", fName)
178 continue
179 cmd += fName + " "
180 Ntot += NperJob
181 if cmd.find("root") < 0:
182 print("no file found, exit")
183 else:
184 stat = str(int(Ntot / 1e6)) + "Mpot"
185 outFile = (
186 "Cascade-run"
187 + str(run)
188 + "-"
189 + str(run + ncpus - 1)
190 + "-parp16-MSTP82-1-MSEL"
191 + msel
192 + "-"
193 + stat
194 + ".root"
195 )
196 rc = os.system("hadd -O " + outFile + " " + cmd)
197 f = ROOT.TFile(outFile)
198 Npot = f.Get("2").GetBinContent(1) / 2.0 / chicc
199 f.Close()
200 stat = str(int(Npot / 1e9)) + "Bpot"
201 oldOutFile = outFile
202 outFile = (
203 "Cascade-run"
204 + str(run)
205 + "-"
206 + str(run + ncpus - 1)
207 + "-parp16-MSTP82-1-MSEL"
208 + msel
209 + "-"
210 + stat
211 + ".root"
212 )
213 os.system("mv " + oldOutFile + " " + outFile)
214 rc = os.system("xrdcp " + outFile + " $EOSSHIP/eos/experiment/ship/data/Mbias/background-prod-2018/" + outFile)
215 if rc != 0:
216 print("copy to EOS failed, stop", outFile)
217 else:
218 rc = os.system("rm " + outFile)
219
220
221def statistics() -> None:
222 path = os.environ["EOSSHIP"] + "/eos/experiment/ship/data/Mbias/background-prod-2018/"
223 fname = "Cascade-runAA-BB-parp16-MSTP82-1-MSEL4-40Mpot.root"
224 nPot = 0
225 nhadrons = 0
226 for x in [0, 20, 40, 60, 80]:
227 fn = fname.replace("AA", str(x)).replace("BB", str(x + 19))
228 f = ROOT.TFile.Open(path + fn)
229 nPot += f.Get("2").GetBinContent(1) / 2.0
230 nhadrons += f.Get("pythia6").GetEntries()
231 print("total nr of hadrons:", nhadrons, nPot / chicc / 1.0e9, "Billion")
232
233
234def potFromFileHeader(f) -> None:
235 pot = 0
236 for x in f.GetListOfKeys():
237 if x.GetName() == "FileHeader":
238 pot += float(x.GetTitle().split(" ")[3])
239 print("PoT = ", pot)
240
241
242x = ROOT.TRandom3()
243x.SetSeed(0)
244
245run = int(sys.argv[1])
246
247print("following functions exist")
248print(" - makeHadrons(run): will run makeCascade")
249print(" - makeBackground(run): will run fixedTarget generator")
None makeHadrons(run)
None makeBackgroundX(runList, int cycle=0)
None merge(run, int cycle=0)
None potFromFileHeader(f)
None makeBackground(run, int cycle=0)
None compactifyCascade(run)
int open(const char *, int)
Opens a file descriptor.