1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 import sys
59 import os
60 import re
61 import stat
62 try:
63 import madgraph.madweight.Cards as Cards
64 except ImportError, error:
65 print error
66 import internal.madweight.Cards as Cards
67
68
69
70
71
72 num_to_tag={1:'param',2:'analyzer',3:'compilation',4:'event',5:'dir',6:'launch',7:'control',8:'collect',9:'plot',\
73 -1:'relaunch',-2:'clean',-3:'refine',-4:'status'}
74 tag_to_num={'param':1,'analyzer':2,'compilation':3,'event':4,'dir':5,'launch':6,'control':7,'collect':8,'plot':9,\
75 'relaunch':-1,'clean':-2,'refine':-3,'status':-4}
76 last_step=9
77
78
79
81 """ move to main position """
82 pos=os.getcwd()
83 last=pos.split(os.sep)[-1]
84 if last=='bin':
85 os.chdir(os.pardir)
86 return
87 if last=='Python':
88 os.chdir(os.pardir+os.sep+os.pardir+os.sep+os.pardir)
89 return
90
91 list_dir=os.listdir('./')
92 if 'bin' in list_dir:
93 return
94 else:
95 sys.exit('Error: script must be executed from the main, bin or Python directory')
96
97
99 """put all card information in a dictionary"""
100
101 card=Cards.Card(name_card)
102 return card.info
103
104
106 """ check if the user use the -h or -help option or simply invalid option """
107
108 opt_pat=re.compile(r'''-?(?P<opt>\w*)[+-]?''',re.I)
109 help=0
110 authorized_opt=tag_to_num.keys()+['version']
111 for i in range(0,len(num_to_tag)):
112 authorized_opt+=[str(i)]
113 for i in range(1,len(opt)):
114 if opt_pat.search(opt[i]):
115 if opt_pat.search(opt[i]).group('opt').lower() not in authorized_opt:
116 try:
117 int(opt_pat.search(opt[i]).group('opt').lower())
118 except:
119 os.system('cat ./Source/MadWeight/Readme.txt')
120 sys.exit()
121 if opt_pat.search(opt[i]).group('opt').lower()=='version':
122 print 'MadWeight Version'
123 os.system('cat ./Source/MadWeight/MW_TemplateVersion.txt')
124 sys.exit()
125
126
128 """ class containing option/routine for standard MG/ME run """
129
130
132 try:
133 return self.Plistdir
134 except:
135 self.Plistdir,nothing=self.detect_SubProcess()
136 return self.Plistdir
137
138
140
141
142 P_SubProcess_list,MW_SubProcess_list=detect_SubProcess(P_mode)
143 return P_SubProcess_list,MW_SubProcess_list
144
145
146
147
149 """ class containing all the option/information from the run """
150
151
178
179
180
182 """ init all the run scheduling paramater to value """
183 self.run_opt={}
184 self.run_opt['param']=value
185 self.run_opt['analyzer']=value
186 self.run_opt['compilation']=value
187 self.run_opt['event']=value
188 self.run_opt['dir']=value
189 self.run_opt['launch']=value
190 self.run_opt['control']=value
191 self.run_opt['collect']=value
192 self.run_opt['plot']=value
193 self.run_opt['madweight_main']=value
194 self.run_opt['relaunch']=0
195 self.run_opt['refine']=0
196 self.run_opt['clean']=0
197 self.run_opt['status']=0
198 self.control_opt()
199
200
202 """ assign default value if not defined already and check the input type
203 those default value and the type are defined in MW_param_default.inc
204 structure of this file:
205 block tag type value #comment
206 additionaly check if the mw_parameter['?3'] is a list
207 """
208
209
210 def pass_in_integer(value):
211 return int(value)
212
213 def pass_in_logical(value):
214 if value in ['1','t','T','.true.',1,True]:
215 return 1
216 else:
217 return 0
218
219 def pass_in_float(value):
220 return float(value)
221
222 for line in open('./Source/MadWeight/Python/MW_param_default.inc'):
223 line=line.split('#')[0]
224 splitline=line.split()
225 if len(splitline)!=4:
226 continue
227
228 block=splitline[0].lower()
229 tag=splitline[1].lower()
230 type=splitline[2].lower()
231 value=splitline[3]
232
233 try:
234 self[block][tag]
235 except:
236 if value.count('@@'):
237 value=value.split('@@')
238 value_2=self[value[0]][value[1]]
239 value=value_2
240 try:
241 self[block][tag]=value
242 except:
243 self[block]={tag:value}
244
245 if type in ['integer','logical','float']:
246 self[block][tag]=eval('pass_in_'+type+'(self[block][tag])')
247 self.check_mw_parameter_list_type()
248
249
251 """ check that self['mw_parameter']['?3'] is a list """
252 nb_param=1
253
254 while self['mw_parameter'].has_key(str(nb_param*10+3)):
255 if not isinstance(self['mw_parameter'][str(nb_param*10+3)], list):
256 self['mw_parameter'][str(nb_param*10+3)]=[self['mw_parameter'][str(nb_param*10+3)]]
257 nb_param+=1
258
259
260
262 """take the run name in run_card"""
263 name="run"
264 Pattern=re.compile(r'''\'(\S*)\'\s*=\s*run_tag''',re.I)
265 card=open("./Cards/run_card.dat")
266
267 while 1:
268 line=card.readline()
269 if line=='':
270 break
271
272 if Pattern.search(line):
273 name=Pattern.search(line).groups()[0]
274 break
275 return name
276
277
282
283
285 "take the info from the existing card in Cards"
286
287
288
289 j=1
290 while 1:
291 if os.path.isfile('Cards/param_card_'+str(j)+'.dat'): j+=1
292 elif(j==1): return j
293 else: return j-1
294
295
296
297
299 "detect the number of events for P and MW run"
300
301 self.P_nevents=self.info['mw_run']['5']
302 self.MW_nevents=self.info['mw_run']['6']
303
304
305
307 """ return the number of modified parameter and the number of different value for each"""
308
309 nb_block=0
310 nb_values=[]
311 k=0
312 while 1:
313 k+=1
314 try:
315 self.info['mw_parameter'][str(10*k+1)]
316 except:
317 break
318 nb_block+=1
319 if type(self.info['mw_parameter'][str(10*k+3)])==list:
320 nb_values.append(len(self.info['mw_parameter'][str(10*k+3)]))
321 else:
322 nb_values.append(1)
323
324 return nb_block,nb_values
325
326
328 """ find from th card number, to which value for each block this card belong
329 num_card is the number of the card in the last generation.
330 card in previous generation are not accessible by this routine
331 (and are not related to this MadWeight card anyway)
332 """
333
334 nb_block,nb_data_by_block=self.give_block_param_info()
335
336 if self['mw_parameter']['1']==2:
337 return [num_card-1]*len(nb_data_by_block)
338
339 tag=[]
340 for k in range(-1,-nb_block-1,-1):
341 tag.append((num_card-1)%nb_data_by_block[k])
342 num_card=1+(num_card-(num_card-1)%nb_data_by_block[k])//nb_data_by_block[k]
343 tag.reverse()
344 return tag
345
346
348 """analyze option for the run"""
349
350 if len(option)>1:
351 self.init_run_opt(0)
352 else:
353 return
354 for i in range(1,len(option)):
355 if option[i][0]=='-' and option[i][-1]=='+':
356 num=int(option[i][1:-1])
357 for j in range(num,last_step+1):
358 self.run_opt[num_to_tag[j]]=1
359 elif option[i][0]=='-' and option[i][-1]=='-':
360 num=int(option[i][1:-1])+1
361 for j in range(1,num):
362 self.run_opt[num_to_tag[j]]=1
363 elif option[i][0]=='-':
364 num=int(option[i][1:])
365 for i in option[i][1:]:
366 self.run_opt[num_to_tag[int(i)]]=1
367 elif option[i][-1]=='+':
368 num=tag_to_num[option[i][:-1]]
369 for j in range(num,last_step+1):
370 self.run_opt[num_to_tag[j]]=1
371 elif option[i][-1]=='-':
372 num=tag_to_num[option[i][:-1]]+1
373 for j in range(1,num):
374 self.run_opt[num_to_tag[j]]=1
375 elif '=' in option[i]:
376 obj=option[i].split('=')
377 tag=obj[0]
378 value=obj[1]
379 self.run_opt[tag]=value
380 else:
381 self.run_opt[option[i]]=1
382
383 self.control_opt()
384
385
387 """analyze option for the run to have coherent input"""
388
389
390 if self.run_opt['refine']:
391 self.run_opt['relaunch']=1
392
393
394 for i in range(3,9)+[-1,-3,-4]:
395 if self.run_opt[num_to_tag[i]]==1:
396 self.run_opt['madweight_main']=1
397 break
398
399 if self.run_opt['relaunch']==1:
400 self.run_opt['control']=1
401
402
404 """ find which card are still actif """
405
406 self.param_is_actif={}
407 try:
408 ff=open('Cards/mapping_card.dat')
409 except:
410 print 'no mapping card found'
411 for i in range(1,self.nb_card+1):
412 self.param_is_actif[i]=1
413 self.actif_param=range(1,self.nb_card+1)
414 return
415
416 self.actif_param=[]
417 for line in ff:
418 split=line.split()
419 if len(split)<2:
420 continue
421 nb=int(split[0])
422 actif=int(split[-1])
423 self.param_is_actif[nb]=actif
424 if actif:
425 self.actif_param.append(nb)
426
427 if self.nb_card!=len(self.actif_param):
428 print 'wrong mapping file or some card are desactivated'
429
430
431
433 """ find how much event are already defined """
434
435 if directory=='':
436 directory='./SubProcesses/'+self.MW_listdir[0]+'/'+self.name+'/'
437 if card_nb=='':
438 card_nb=self.actif_param[0]
439 if os.path.isdir(directory+'/card_'+str(card_nb)+'/'):
440 number=[int(eventdir.split('_')[-1]) for eventdir in os.listdir(directory+'/card_'+str(card_nb)+'/')]
441 else:
442 return 0
443
444 if len(number):
445 return max(number)+1
446 else:
447 return 0
448
449
452 self.initpos=os.getcwd()
453 self.old=self.initpos
454
456
457 old_pos=os.getcwd()+'/'
458 self.old=old_pos
459 if old_pos.count('/SubProcesses/')>1:
460 new_pos=pos.split('/SubProcesses/')[0]+'/SubProcesses/'
461 elif old_pos.count('/SubProcesses/')==0:
462 new_pos=old_pos+'/SubProcesses/'
463 else:
464 new_pos='/SubProcesses/'.join(old_pos.split('/SubProcesses/')[:-1])+'/SubProcesses/'
465
466 if new_pos!=old_pos:
467 self.old=old_pos
468 os.chdir(new_pos)
469
471 self.old=os.getcwd()
472 os.chdir(self.initpos)
473
475 self.store=os.getcwd()
476
480
482 pos,self.old=self.old,os.getcwd()
483 os.chdir(pos)
484
486 old=os.getcwd()
487 try:
488 go_to_main_dir()
489 except:
490 self.to_SubProcesses()
491 os.chdir('..')
492 self.old=old
493
494
496 MW_SubProcess_list=[]
497 if P_mode == 1:
498 print 'WARNING: automatic renormalization is not supported anymore'
499
500 list_dir=os.listdir("./SubProcesses/")
501 for name in list_dir:
502 try:
503 st = os.lstat(os.path.join("./SubProcesses/", name))
504 except os.error:
505 continue
506 if stat.S_ISDIR(st.st_mode):
507 if name[0]=='P':
508 MW_SubProcess_list.append(name)
509
510
511 return [],MW_SubProcess_list
512