FairShip
Loading...
Searching...
No Matches
run_reco Namespace Reference

Functions

None fitSingleGauss (str x, float|None ba=None, float|None be=None)
 execute_parallel (prefix, int ncpu=4)
list[str] getJobs (prefix)
int checkRunningProcesses ()
None killAll ()
None executeSimple (list[str] prefixes, reset=False)
None executeAna (prefixes)
None mergeHistosMakePlots (str|list[str] p)
None mergeNtuples (list[str] prefixes)
 checkProd (prefixes, quiet=False)
None printFailedJobs (pl)
None execute ()
None removeIntermediateFiles (prefixes)
None copyRecoToEos (pl)

Variables

 ncores = min(multiprocessing.cpu_count(), 4)
 user = getpass.getuser()
dict h = {}
str cmd = os.environ["FAIRSHIP"] + "/macro/ShipReco.py"
str cmdAna = os.environ["FAIRSHIP"] + "/macro/ShipAna.py"
list pl = []
str pref = "muon"
 xx = os.path.abspath(".").lower()

Function Documentation

◆ checkProd()

run_reco.checkProd ( prefixes,
quiet = False )

Definition at line 300 of file run_reco.py.

300def checkProd(prefixes, quiet=False):
301 summary = {"Sim": {}, "Rec": {}, "Ana": {}}
302 for prefix in prefixes:
303 jobs = getJobs(prefix)
304 for x in jobs:
305 try:
306 log = open(x + "/log") # noqa: SIM115
307 except OSError:
308 if not quiet:
309 print("no log file for ", x)
310 summary["Sim"][x] = -1
311 continue
312 rl = log.readlines()
313 log.close()
314 if len(rl) < 1:
315 if not quiet:
316 print("simulation failed log file 0", x)
317 summary["Sim"][x] = 0
318 continue
319 elif "Real time" in rl[len(rl) - 1]:
320 if not quiet:
321 print("simulation step OK ", x)
322 summary["Sim"][x] = 1
323 else:
324 if not quiet:
325 print("simulation failed ", x)
326 summary["Sim"][x] = 0
327 continue
328 try:
329 log = open(x + "/logRec") # noqa: SIM115
330 except OSError:
331 if not quiet:
332 print("no logRec file for ", x)
333 summary["Rec"][x] = -1
334 continue
335 rl = log.readlines()
336 log.close()
337 if "finishing" in rl[len(rl) - 1]:
338 if not quiet:
339 print("reconstruction step OK ", x)
340 summary["Rec"][x] = 1
341 else:
342 if not quiet:
343 print("reconstruction failed ", x)
344 summary["Rec"][x] = 0
345 continue
346 try:
347 log = open(x + "/logAna") # noqa: SIM115
348 except OSError:
349 if not quiet:
350 print("no logAna file for ", x)
351 summary["Ana"][x] = -1
352 continue
353 rl = log.readlines()
354 log.close()
355 if "finished" in rl[len(rl) - 1]:
356 if not quiet:
357 print("analysis step OK ", x)
358 summary["Ana"][x] = 1
359 else:
360 if not quiet:
361 print("analysis failed ", x)
362 summary["Ana"][x] = 0
363 continue
364 return summary
365
366

◆ checkRunningProcesses()

int run_reco.checkRunningProcesses ( )

Definition at line 93 of file run_reco.py.

93def checkRunningProcesses() -> int:
94 processoutput = os.popen("ps -u " + user).read()
95 nproc = 0
96 for x in processoutput.split("\n"):
97 if not x.find("python") < 0 and x.find("defunct") < 0:
98 nproc += 1
99 return nproc
100
101

◆ copyRecoToEos()

None run_reco.copyRecoToEos ( pl)

Definition at line 391 of file run_reco.py.

391def copyRecoToEos(pl) -> None:
392 result = checkProd(pl, quiet=True)
393 eos = "/afs/cern.ch/project/eos/installation/0.3.15/bin/eos.select"
394 for x in result["Rec"]:
395 if result["Rec"][x] < 1:
396 print("Reco failed !", x, result["Rec"][x])
397 else:
398 cmd = (
399 eos
400 + " cp -r "
401 + os.path.abspath(".")
402 + "/"
403 + x
404 + "/ /eos/experiment/ship/data/DAFreco/muonBackground/"
405 + x
406 + "/"
407 )
408 os.system(cmd)
409 print("copied to eos", x)
410
411

◆ execute()

None run_reco.execute ( )

Definition at line 375 of file run_reco.py.

375def execute() -> None:
376 executeSimple(pl, reset=True)
377 mergeHistosMakePlots(pl)
378 mergeNtuples(pl)
379
380

◆ execute_parallel()

run_reco.execute_parallel ( prefix,
int ncpu = 4 )

Definition at line 49 of file run_reco.py.

49def execute_parallel(prefix, ncpu: int = 4):
50 cpus = {}
51 log = {}
52 for i in range(ncpu):
53 cpus[i] = None
54 jobs = []
55 for x in os.listdir("."):
56 if not x.find(prefix) < 0:
57 if os.path.isdir(x):
58 jobs.append(x)
59 k = 0
60 print(jobs)
61 for x in jobs:
62 if k == ncpu:
63 k = 0
64 running = cpus[k]
65 if running is not None:
66 running.communicate()
67 log[k].close()
68 print("change to directory ", k, x)
69 os.chdir("./" + x)
70 for f in os.listdir("."):
71 if not f.find("geofile_full") < 0:
72 inputfile = f.replace("geofile_full", "ship")
73 log[k] = open("logRec", "w") # noqa: SIM115
74 cpus[k] = subprocess.Popen(
75 ["python", cmd, "-n", "9999999", "-f", inputfile, "-g", f],
76 stdout=log[k],
77 )
78 k += 1
79 os.chdir("../")
80 break
81 return cpus, log
82
83

◆ executeAna()

None run_reco.executeAna ( prefixes)

Definition at line 189 of file run_reco.py.

189def executeAna(prefixes) -> None:
190 log = {}
191 for prefix in prefixes:
192 jobs = getJobs(prefix)
193 for x in jobs:
194 print("change to directory ", x)
195 os.chdir("./" + x)
196 for f in os.listdir("."):
197 if not f.find("geofile_full") < 0:
198 inputfile = f.replace("geofile_full", "ship")
199 log[x] = open("logAna", "w") # noqa: SIM115
200 process = subprocess.Popen(
201 [
202 "python",
203 cmdAna,
204 "-n",
205 "9999999",
206 "-f",
207 inputfile.replace(".root", "_rec.root"),
208 "-g",
209 f,
210 ],
211 stdout=log[x],
212 )
213 process.wait()
214 print("finished ", process.returncode)
215 log[x].close()
216 os.chdir("../")
217 break
218
219

◆ executeSimple()

None run_reco.executeSimple ( list[str] prefixes,
reset = False )

Definition at line 111 of file run_reco.py.

111def executeSimple(prefixes: list[str], reset=False) -> None:
112 proc = {}
113 for prefix in prefixes:
114 jobs = getJobs(prefix)
115 for x in jobs:
116 print("change to directory ", x)
117 os.chdir("./" + x)
118 geofile = None
119 for f in os.listdir("."):
120 if not f.find("geofile_full") < 0:
121 geofile = f
122 break
123 if not geofile:
124 print("ERROR: no geofile found", x)
125 os.chdir("../")
126 continue
127 else:
128 inputfile = geofile.replace("geofile_full", "ship")
129 nproc = 100
130 while nproc > ncores:
131 nproc = checkRunningProcesses()
132 if nproc > ncores:
133 print("wait a minute")
134 time.sleep(100)
135 print("launch reco", x)
136 proc[x] = (inputfile, geofile)
137 with contextlib.suppress(Exception):
138 os.system("rm logRec")
139 if reset:
140 os.system(
141 "python " + cmd + " -n 9999999 -f " + inputfile + " -g " + geofile + " --saveDisk >> logRec &"
142 )
143 else:
144 os.system("python " + cmd + " -n 9999999 -f " + inputfile + " -g " + geofile + " >> logRec &")
145 os.chdir("../")
146 time.sleep(10)
147 nJobs = len(proc)
148 while nJobs > 0:
149 nJobs = len(proc)
150 print("debug ", nJobs)
151 for p in sorted(proc.keys()):
152 os.chdir("./" + p)
153 nproc = 100
154 while nproc > ncores:
155 nproc = checkRunningProcesses()
156 if nproc > ncores:
157 print("wait a minute")
158 time.sleep(100)
159 with open("logRec") as log:
160 completed = False
161 rl = log.readlines()
162 if "finishing" in rl[len(rl) - 1]:
163 completed = True
164 if completed:
165 print("analyze ", p, nproc)
166 inputfile, geofile = proc[p]
167 with contextlib.suppress(Exception):
168 os.system("rm logAna")
169 os.system(
170 "python "
171 + cmdAna
172 + " -n 9999999 -f "
173 + inputfile.replace(".root", "_rec.root")
174 + " -g "
175 + geofile
176 + " >> logAna &"
177 )
178 proc.pop(p)
179 time.sleep(10)
180 else:
181 print("Rec job not finished yet", p)
182 nproc = checkRunningProcesses()
183 if nproc == 1:
184 print("rec job probably failed, only when python process running")
185 time.sleep(100)
186 os.chdir("../")
187
188

◆ fitSingleGauss()

None run_reco.fitSingleGauss ( str x,
float | None ba = None,
float | None be = None )

Definition at line 21 of file run_reco.py.

21def fitSingleGauss(x: str, ba: float | None = None, be: float | None = None) -> None:
22 name = "myGauss_" + x
23 myGauss = h[x].GetListOfFunctions().FindObject(name)
24 if not myGauss:
25 if not ba:
26 ba = h[x].GetBinCenter(1)
27 if not be:
28 be = h[x].GetBinCenter(h[x].GetNbinsX())
29 bw = h[x].GetBinWidth(1)
30 mean = h[x].GetMean()
31 sigma = h[x].GetRMS()
32 norm = h[x].GetEntries() * 0.3
33 myGauss = ROOT.TF1(name, "[0]*" + str(bw) + "/([2]*sqrt(2*pi))*exp(-0.5*((x-[1])/[2])**2)+[3]", 4)
34 myGauss.SetParameter(0, norm)
35 myGauss.SetParameter(1, mean)
36 myGauss.SetParameter(2, sigma)
37 myGauss.SetParameter(3, 1.0)
38 myGauss.SetParName(0, "Signal")
39 myGauss.SetParName(1, "Mean")
40 myGauss.SetParName(2, "Sigma")
41 myGauss.SetParName(3, "bckgr")
42 h[x].Fit(myGauss, "", "", ba, be)
43
44

◆ getJobs()

list[str] run_reco.getJobs ( prefix)

Definition at line 84 of file run_reco.py.

84def getJobs(prefix) -> list[str]:
85 jobs = []
86 for x in os.listdir("."):
87 if not x.find(prefix) < 0:
88 if os.path.isdir(x):
89 jobs.append(x)
90 return jobs
91
92

◆ killAll()

None run_reco.killAll ( )

Definition at line 102 of file run_reco.py.

102def killAll() -> None:
103 processoutput = os.popen("ps -u " + user).read()
104 for x in processoutput.split("\n"):
105 if not x.find("python") < 0:
106 pid = int(x[:5])
107 print("kill " + str(pid))
108 os.system("kill " + str(pid))
109
110

◆ mergeHistosMakePlots()

None run_reco.mergeHistosMakePlots ( str | list[str] p)

Definition at line 223 of file run_reco.py.

223def mergeHistosMakePlots(p: str | list[str]) -> None:
224 if not isinstance(p, list):
225 pl = [p]
226 else:
227 pl = p
228 hlist = ""
229 for p in pl:
230 prefix = str(p)
231 for x in os.listdir("."):
232 if not x.find(prefix) < 0:
233 if os.path.isdir(x):
234 hlist += x + "/ShipAna.root "
235 print("-->", hlist)
236 os.system("hadd -f ShipAna.root " + hlist)
237 ut.readHists(h, "ShipAna.root")
238 print(h["meanhits"].GetEntries())
239 if 1 > 0:
240 ut.bookCanvas(h, key="strawanalysis", title="Distance to wire and mean nr of hits", nx=1200, ny=600, cx=2, cy=1)
241 #
242 cv = h["strawanalysis"].cd(1)
243 h["disty"].DrawCopy()
244 h["distu"].DrawCopy("same")
245 h["distv"].DrawCopy("same")
246 cv = h["strawanalysis"].cd(2)
247 h["meanhits"].DrawCopy()
248 print(h["meanhits"].GetEntries())
249
250 ut.bookCanvas(h, key="fitresults", title="Fit Results", nx=1600, ny=1200, cx=2, cy=2)
251 cv = h["fitresults"].cd(1)
252 h["delPOverP"].Draw("box")
253 cv = h["fitresults"].cd(2)
254 cv.SetLogy(1)
255 h["chi2"].Draw()
256 cv = h["fitresults"].cd(3)
257 h["delPOverP_proj"] = h["delPOverP"].ProjectionY()
258 ROOT.gStyle.SetOptFit(11111)
259 h["delPOverP_proj"].Draw()
260 h["delPOverP_proj"].Fit("gaus")
261 cv = h["fitresults"].cd(4)
262 h["delPOverP2_proj"] = h["delPOverP2"].ProjectionY()
263 h["delPOverP2_proj"].Draw()
264 fitSingleGauss("delPOverP2_proj")
265 h["fitresults"].Print("fitresults.gif")
266 ut.bookCanvas(h, key="fitresults2", title="Fit Results", nx=1600, ny=1200, cx=2, cy=2)
267 print("finished with first canvas")
268 cv = h["fitresults2"].cd(1)
269 h["Doca"].Draw()
270 cv = h["fitresults2"].cd(2)
271 h["IP0"].Draw()
272 cv = h["fitresults2"].cd(3)
273 h["HNL"].Draw()
274 fitSingleGauss("HNL", 0.0, 2.0)
275 cv = h["fitresults2"].cd(4)
276 h["IP0/mass"].Draw("box")
277 h["fitresults2"].Print("fitresults2.gif")
278 h["strawanalysis"].Print("strawanalysis.gif")
279 print("finished making plots")
280
281

◆ mergeNtuples()

None run_reco.mergeNtuples ( list[str] prefixes)

Definition at line 282 of file run_reco.py.

282def mergeNtuples(prefixes: list[str]) -> None:
283 for prefix in prefixes:
284 jobs = getJobs(prefix)
285 haddCommand = ""
286 inputfile = None
287 for x in jobs:
288 for f in os.listdir(x):
289 if not f.find("geofile_full") < 0:
290 inputfile = (f.replace("geofile_full", "ship")).replace(".root", "_rec.root")
291 haddCommand += " " + x + "/" + inputfile
292 break
293 if inputfile is None:
294 print("ERROR: no geofile found for prefix", prefix)
295 continue
296 cmd = "hadd -f " + inputfile.replace(".root", "_" + prefix + ".root") + haddCommand
297 os.system(cmd)
298
299

◆ printFailedJobs()

None run_reco.printFailedJobs ( pl)

Definition at line 367 of file run_reco.py.

367def printFailedJobs(pl) -> None:
368 result = checkProd(pl, quiet=True)
369 for p in result:
370 for x in result[p]:
371 if result[p][x] < 1:
372 print(p, x, result[p][x])
373
374

◆ removeIntermediateFiles()

None run_reco.removeIntermediateFiles ( prefixes)

Definition at line 381 of file run_reco.py.

381def removeIntermediateFiles(prefixes) -> None:
382 for prefix in prefixes:
383 jobs = getJobs(prefix)
384 for x in jobs:
385 for f in os.listdir(x):
386 if not f.find("geofile_full") < 0:
387 inputfile = (f.replace("geofile_full", "ship")).replace(".root", "_rec.root")
388 os.system("rm " + x + "/" + inputfile)
389
390

Variable Documentation

◆ cmd

str run_reco.cmd = os.environ["FAIRSHIP"] + "/macro/ShipReco.py"

Definition at line 45 of file run_reco.py.

◆ cmdAna

str run_reco.cmdAna = os.environ["FAIRSHIP"] + "/macro/ShipAna.py"

Definition at line 46 of file run_reco.py.

◆ h

dict run_reco.h = {}

Definition at line 18 of file run_reco.py.

◆ ncores

run_reco.ncores = min(multiprocessing.cpu_count(), 4)

Definition at line 14 of file run_reco.py.

◆ pl

list run_reco.pl = []

Definition at line 412 of file run_reco.py.

◆ pref

str run_reco.pref = "muon"

Definition at line 414 of file run_reco.py.

◆ user

run_reco.user = getpass.getuser()

Definition at line 15 of file run_reco.py.

◆ xx

run_reco.xx = os.path.abspath(".").lower()

Definition at line 415 of file run_reco.py.