SND@LHC Software
Loading...
Searching...
No Matches
run_reco Namespace Reference

Functions

 fitSingleGauss (x, ba=None, be=None)
 
 execute (ncpu=4)
 
 getJobs (prefix)
 
 checkRunningProcesses ()
 
 killAll ()
 
 executeSimple (prefixes, reset=False)
 
 executeAna (prefixes)
 
 mergeHistosMakePlots (p)
 
 mergeNtuples (prefixes)
 
 checkProd (prefixes, quiet=False)
 
 printFailedJobs (pl)
 
 execute ()
 
 removeIntermediateFiles (prefixes)
 
 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 233 of file run_reco.py.

233def checkProd(prefixes,quiet=False):
234 summary = {'Sim':{},'Rec':{},'Ana':{}}
235 for prefix in prefixes:
236 jobs = getJobs(prefix)
237 for x in jobs:
238 try: log = open( x+'/log')
239 except:
240 if not quiet: print('no log file for ',x)
241 summary['Sim'][x] = -1
242 continue
243 rl = log.readlines()
244 log.close()
245 if len(rl)<1 :
246 if not quiet: print("simulation failed log file 0",x)
247 summary['Sim'][x] = 0
248 continue
249 elif "Real time" in rl[len(rl)-1] :
250 if not quiet: print('simulation step OK ',x)
251 summary['Sim'][x] = 1
252 else:
253 if not quiet: print("simulation failed ",x)
254 summary['Sim'][x] = 0
255 continue
256 try: log = open( x+'/logRec')
257 except:
258 if not quiet: print('no logRec file for ',x)
259 summary['Rec'][x] = -1
260 continue
261 rl = log.readlines()
262 log.close()
263 if "finishing" in rl[len(rl)-1] :
264 if not quiet: print('reconstruction step OK ',x)
265 summary['Rec'][x] = 1
266 else:
267 if not quiet: print("reconstruction failed ",x)
268 summary['Rec'][x] = 0
269 continue
270 try: log = open( x+'/logAna')
271 except:
272 if not quiet: print('no logAna file for ',x)
273 summary['Ana'][x] = -1
274 continue
275 rl = log.readlines()
276 log.close()
277 if "finished" in rl[len(rl)-1] :
278 if not quiet: print('analysis step OK ',x)
279 summary['Ana'][x] = 1
280 else:
281 if not quiet: print("analysis failed ",x)
282 summary['Ana'][x] = 0
283 continue
284 return summary

◆ checkRunningProcesses()

run_reco.checkRunningProcesses ( )

Definition at line 68 of file run_reco.py.

68def checkRunningProcesses():
69 processoutput = os.popen("ps -u "+user).read()
70 nproc = 0
71 for x in processoutput.split('\n'):
72 if not x.find('python')<0 and x.find('defunct')<0 :
73 nproc+=1
74 return nproc

◆ copyRecoToEos()

run_reco.copyRecoToEos (   pl)

Definition at line 305 of file run_reco.py.

305def copyRecoToEos(pl):
306 result = checkProd(pl,quiet=True)
307 eos = "/afs/cern.ch/project/eos/installation/0.3.15/bin/eos.select"
308 for x in result['Rec']:
309 if result['Rec'][x]<1 : print('Reco failed !',x,result['Rec'][x])
310 else:
311 cmd = eos+' cp -r '+os.path.abspath('.')+'/'+x+'/ /eos/experiment/ship/data/DAFreco/muonBackground/'+x+'/'
312 os.system(cmd)
313 print('copied to eos',x)
314

◆ execute() [1/2]

run_reco.execute ( )

Definition at line 292 of file run_reco.py.

292def execute():
293 executeSimple(pl,reset=True)
294 mergeHistosMakePlots(pl)
295 mergeNtuples(pl)

◆ execute() [2/2]

run_reco.execute (   ncpu = 4)

Definition at line 33 of file run_reco.py.

33def execute( ncpu = 4 ):
34 cpus = {}
35 log = {}
36 for i in range(ncpu): cpus[i]={}
37 jobs = []
38 for x in os.listdir('.'):
39 if not x.find(prefix)<0:
40 if os.path.isdir(x) :
41 jobs.append(x)
42 k = 0
43 print(jobs)
44 for x in jobs:
45 if k==ncpu: k = 0
46 if 'child' in cpus[k]:
47 rc = child.communicate()[0]
48 log[k]['log'].close()
49 print("change to directory ",k,x)
50 os.chdir('./'+x)
51 for f in os.listdir('.'):
52 if not f.find("geofile_full")<0:
53 inputfile = f.replace("geofile_full","ship")
54 log[k] = open("logRec",'w')
55 cpus[k] = subprocess.Popen(["python",cmd,"-n 9999999 -f "+inputfile], stdout=log[k],)
56 k+=1
57 os.chdir('../')
58 break
59 return cpus,log
60

◆ executeAna()

run_reco.executeAna (   prefixes)

Definition at line 145 of file run_reco.py.

145def executeAna(prefixes):
146 cpus = {}
147 log = {}
148 for prefix in prefixes:
149 jobs = getJobs(prefix)
150 for x in jobs:
151 print("change to directory ",x)
152 os.chdir('./'+x)
153 for f in os.listdir('.'):
154 if not f.find("geofile_full")<0:
155 inputfile = f.replace("geofile_full","ship")
156 log[x] = open("logAna",'w')
157 process = subprocess.Popen(["python",cmdAna,"-n 9999999", "-f "+inputfile.replace('.root','_rec.root')], stdout=log[x])
158 process.wait()
159 print('finished ',process.returncode)
160 log[x].close()
161 os.chdir('../')
162 break
163

◆ executeSimple()

run_reco.executeSimple (   prefixes,
  reset = False 
)

Definition at line 82 of file run_reco.py.

82def executeSimple(prefixes,reset=False):
83 proc = {}
84 for prefix in prefixes:
85 jobs = getJobs(prefix)
86 for x in jobs:
87 print("change to directory ",x)
88 os.chdir('./'+x)
89 geofile = None
90 for f in os.listdir('.'):
91 if not f.find("geofile_full")<0:
92 geofile = f
93 break
94 if not geofile:
95 print("ERROR: no geofile found",x)
96 os.chdir('../')
97 continue
98 else:
99 inputfile = geofile.replace("geofile_full","ship")
100 nproc = 100
101 while nproc > ncores :
102 nproc = checkRunningProcesses()
103 if nproc > ncores:
104 print('wait a minute')
105 time.sleep(100)
106 print('launch reco',x)
107 proc[x] = 1
108 try: os.system("rm logRec")
109 except: pass
110 if reset: os.system("python "+cmd+" -n 9999999 -f "+inputfile + " --saveDisk >> logRec &")
111 else: os.system("python "+cmd+" -n 9999999 -f "+inputfile + " >> logRec &")
112 os.chdir('../')
113 time.sleep(10)
114 nJobs = len(proc)
115 while nJobs > 0:
116 nJobs = len(proc)
117 print('debug ',nJobs)
118 for p in sorted(proc.keys()):
119 os.chdir('./'+p)
120 nproc = 100
121 while nproc > ncores :
122 nproc = checkRunningProcesses()
123 if nproc > ncores:
124 print('wait a minute')
125 time.sleep(100)
126 log = open('logRec')
127 completed = False
128 rl = log.readlines()
129 log.close()
130 if "finishing" in rl[len(rl)-1] : completed = True
131 if completed:
132 print('analyze ',p,nproc)
133 try: os.system("rm logAna")
134 except: pass
135 os.system("python "+cmdAna+" -n 9999999 -f "+inputfile.replace('.root','_rec.root')+ " >> logAna &")
136 rc = proc.pop(p)
137 time.sleep(10)
138 else:
139 print('Rec job not finished yet',p)
140 nproc = checkRunningProcesses()
141 if nproc == 1 : print("rec job probably failed, only when python process running")
142 time.sleep(100)
143 os.chdir('../')
144

◆ fitSingleGauss()

run_reco.fitSingleGauss (   x,
  ba = None,
  be = None 
)

Definition at line 10 of file run_reco.py.

10def fitSingleGauss(x,ba=None,be=None):
11 name = 'myGauss_'+x
12 myGauss = h[x].GetListOfFunctions().FindObject(name)
13 if not myGauss:
14 if not ba : ba = h[x].GetBinCenter(1)
15 if not be : be = h[x].GetBinCenter(h[x].GetNbinsX())
16 bw = h[x].GetBinWidth(1)
17 mean = h[x].GetMean()
18 sigma = h[x].GetRMS()
19 norm = h[x].GetEntries()*0.3
20 myGauss = ROOT.TF1(name,'[0]*'+str(bw)+'/([2]*sqrt(2*pi))*exp(-0.5*((x-[1])/[2])**2)+[3]',4)
21 myGauss.SetParameter(0,norm)
22 myGauss.SetParameter(1,mean)
23 myGauss.SetParameter(2,sigma)
24 myGauss.SetParameter(3,1.)
25 myGauss.SetParName(0,'Signal')
26 myGauss.SetParName(1,'Mean')
27 myGauss.SetParName(2,'Sigma')
28 myGauss.SetParName(3,'bckgr')
29 h[x].Fit(myGauss,'','',ba,be)
30

◆ getJobs()

run_reco.getJobs (   prefix)

Definition at line 61 of file run_reco.py.

61def getJobs(prefix):
62 jobs = []
63 for x in os.listdir('.'):
64 if not x.find(prefix)<0:
65 if os.path.isdir(x) :
66 jobs.append(x)
67 return jobs

◆ killAll()

run_reco.killAll ( )

Definition at line 75 of file run_reco.py.

75def killAll():
76 processoutput = os.popen("ps -u "+user).read()
77 for x in processoutput.split('\n'):
78 if not x.find('python')<0:
79 pid = int(x[:5])
80 print('kill '+str(pid))
81 os.system('kill '+str(pid))

◆ mergeHistosMakePlots()

run_reco.mergeHistosMakePlots (   p)

Definition at line 165 of file run_reco.py.

165def mergeHistosMakePlots(p):
166 if not type(p)==type([]): pl=[p]
167 else: pl = p
168 hlist = ''
169 for p in pl:
170 prefix = str(p)
171 for x in os.listdir('.'):
172 if not x.find(prefix)<0:
173 if os.path.isdir(x) :
174 hlist += x+'/ShipAna.root '
175 print("-->",hlist)
176 os.system('hadd -f ShipAna.root '+hlist)
177 ut.readHists(h,"ShipAna.root")
178 print(h['meanhits'].GetEntries())
179 if 1>0:
180 ut.bookCanvas(h,key='strawanalysis',title='Distance to wire and mean nr of hits',nx=1200,ny=600,cx=2,cy=1)
181#
182 cv = h['strawanalysis'].cd(1)
183 h['disty'].DrawCopy()
184 h['distu'].DrawCopy('same')
185 h['distv'].DrawCopy('same')
186 cv = h['strawanalysis'].cd(2)
187 h['meanhits'].DrawCopy()
188 print(h['meanhits'].GetEntries())
189
190 ut.bookCanvas(h,key='fitresults',title='Fit Results',nx=1600,ny=1200,cx=2,cy=2)
191 cv = h['fitresults'].cd(1)
192 h['delPOverP'].Draw('box')
193 cv = h['fitresults'].cd(2)
194 cv.SetLogy(1)
195 h['chi2'].Draw()
196 cv = h['fitresults'].cd(3)
197 h['delPOverP_proj'] = h['delPOverP'].ProjectionY()
198 ROOT.gStyle.SetOptFit(11111)
199 h['delPOverP_proj'].Draw()
200 h['delPOverP_proj'].Fit('gaus')
201 cv = h['fitresults'].cd(4)
202 h['delPOverP2_proj'] = h['delPOverP2'].ProjectionY()
203 h['delPOverP2_proj'].Draw()
204 fitSingleGauss('delPOverP2_proj')
205 h['fitresults'].Print('fitresults.gif')
206 ut.bookCanvas(h,key='fitresults2',title='Fit Results',nx=1600,ny=1200,cx=2,cy=2)
207 print('finished with first canvas')
208 cv = h['fitresults2'].cd(1)
209 h['Doca'].Draw()
210 cv = h['fitresults2'].cd(2)
211 h['IP0'].Draw()
212 cv = h['fitresults2'].cd(3)
213 h['HNL'].Draw()
214 fitSingleGauss('HNL',0.,2.)
215 cv = h['fitresults2'].cd(4)
216 h['IP0/mass'].Draw('box')
217 h['fitresults2'].Print('fitresults2.gif')
218 h['strawanalysis'].Print('strawanalysis.gif')
219 print('finished making plots')
220

◆ mergeNtuples()

run_reco.mergeNtuples (   prefixes)

Definition at line 221 of file run_reco.py.

221def mergeNtuples(prefixes):
222 for prefix in prefixes:
223 jobs = getJobs(prefix)
224 haddCommand = ''
225 for x in jobs:
226 for f in os.listdir(x):
227 if not f.find("geofile_full")<0:
228 inputfile = (f.replace("geofile_full","ship")).replace('.root','_rec.root')
229 haddCommand+= ' '+ x + '/' + inputfile
230 break
231 cmd = 'hadd -f '+inputfile.replace('.root','_'+prefix+'.root') + haddCommand
232 os.system(cmd)

◆ printFailedJobs()

run_reco.printFailedJobs (   pl)

Definition at line 285 of file run_reco.py.

285def printFailedJobs(pl):
286 result = checkProd(pl,quiet=True)
287 for p in result:
288 for x in result[p]:
289 if result[p][x]<1 : print(p,x,result[p][x])
290
291

◆ removeIntermediateFiles()

run_reco.removeIntermediateFiles (   prefixes)

Definition at line 296 of file run_reco.py.

296def removeIntermediateFiles(prefixes):
297 for prefix in prefixes:
298 jobs = getJobs(prefix)
299 for x in jobs:
300 for f in os.listdir(x):
301 if not f.find("geofile_full")<0:
302 inputfile = (f.replace("geofile_full","ship")).replace('.root','_rec.root')
303 os.system('rm '+x+'/' + inputfile )
304

Variable Documentation

◆ cmd

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

Definition at line 31 of file run_reco.py.

◆ cmdAna

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

Definition at line 32 of file run_reco.py.

◆ h

dict run_reco.h = {}

Definition at line 8 of file run_reco.py.

◆ ncores

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

Definition at line 4 of file run_reco.py.

◆ pl

list run_reco.pl = []

Definition at line 315 of file run_reco.py.

◆ pref

run_reco.pref = 'muon'

Definition at line 317 of file run_reco.py.

◆ user

run_reco.user = getpass.getuser()

Definition at line 5 of file run_reco.py.

◆ xx

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

Definition at line 318 of file run_reco.py.