1
2
3
4 import string,os,sys,re,popen2,time,stat,filecmp
5
6 try:
7 import madgraph.madweight.mod_file as mod_file
8 import madgraph.madweight.change_tf as change_tf
9
10 except ImportError:
11 import internal.madweight.mod_file as mod_file
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 opt=sys.argv
41 write = sys.stdout.write
42
43
46
47
48
49
50
52 """ remove the call to the cuts if asked in MadWeight_Card.dat """
53
54
55 return
56
57
58 if (MW_param.info['mw_run']['use_cut']+cut_is_active('cuts.f'))%2:
59 file_to_mod=['./SubProcesses/cuts.f']
60 rule=['./Source/MadWeight/mod_file/suppress_cuts_MG']
61
62 mod_file.mod_file(file_to_mod,rule,opt={'nowarning':"""['PASSCUTS','MW_NEW_DEF','DESACTIVATE_BW_CUT']"""})
63
64 check_Subprocesses_update('cuts.f',MW_param.P_listdir,'./Source/MadWeight/mod_file/suppress_cuts_MG')
65
66
67 if (MW_param.info['mw_run']['use_cut']+cut_is_active('cuts_MW.f'))%2:
68 file_to_mod=['./SubProcesses/cuts_MW.f']
69 rule=['./Source/MadWeight/mod_file/suppress_cuts_MW']
70
71 mod_file.mod_file(file_to_mod,rule,opt={'nowarning':"""['PASSCUTS','TO_SPECISA','DESACTIVATE_BW_CUT']"""})
72
73 check_Subprocesses_update('cuts_MW.f',MW_param.MW_listdir,'./Source/MadWeight/mod_file/suppress_cuts_MW')
74
75
76 if (MW_param['mw_run']['bw_cut']+bw_cut_is_active('cuts.f'))%2:
77 file_to_mod=['./SubProcesses/cuts.f']
78 rule=['./Source/MadWeight/mod_file/suppress_BW_cuts']
79
80 mod_file.mod_file(file_to_mod,rule,opt={'nowarning':"""['PASSCUTS','MW_NEW_DEF']"""})
81
82 check_Subprocesses_update('cuts.f',MW_param.P_listdir,'./Source/MadWeight/mod_file/suppress_BW_cuts')
83
84
85 if (MW_param.info['mw_run']['bw_cut']+bw_cut_is_active('cuts_MW.f'))%2:
86 file_to_mod=['./SubProcesses/cuts_MW.f']
87 rule=['./Source/MadWeight/mod_file/suppress_BW_cuts']
88
89 mod_file.mod_file(file_to_mod,rule,opt={'nowarning':"""['PASSCUTS','TO_SPECISA']"""})
90
91 check_Subprocesses_update('cuts_MW.f',MW_param.MW_listdir,'./Source/MadWeight/mod_file/suppress_BW_cuts')
92
93
94
95
96
98 """ check is the cut is active or not """
99
100
101 pass
102
103
104
105
106
107
108
110 """ check is the bw_cutt is active or not """
111 for line in file('./SubProcesses/'+filename):
112 if 'DESACTIVATE_BW_CUT' in line:
113 return 1
114 elif 'ACTIVATE_BW_CUT' in line:
115 return 0
116 raise Exception
117
118
119
121 """ check if Subprocces/'listdir' files follows the last version of
122 ./SubProcesses/'filename'. This is a protection against copy files
123 -normally they are just linked-
124
125 different mode are available
126 warning:
127 0: no warning apply modification
128 1: warning and apply modification
129 2: warning and user choice
130 3: warning but no modification
131 4: no warning and no modification
132 5: warning and raising error
133 modifrule:
134 '', copy the original file
135 else: apply the modification asked in modifrule
136
137 """
138
140
141
142
143 - def __init__(self,filelist,listdir,modifrule='',warning=1,run=1):
144
145
146 if type(filelist)==str:
147 self.filelist=[filelist]
148 else:
149 self.filelist=filelist
150 if type(listdir)==str:
151 self.listdir=[listdir]
152 else: self.listdir=listdir
153
154 self.modifrule=modifrule
155
156
157
158 usewarning=0
159 if int(warning) in [1,2,3,5]:
160 self.usewarning=1
161
162 self.usemodif=0
163 self.intmode=0
164 self.raiseerror=0
165 if int(warning) in [0,1]:
166 self.usemodif=1
167 elif int(warning)==2:
168 self.intmode=1
169 elif int(warning)==5:
170 self.raiseerror==1
171
172 if run:
173 self.compare()
174
175
177
178 for dirname in self.listdir:
179 for filename in self.filelist:
180 status=filecmp.cmp('./SubProcesses/'+filename,'./SubProcesses/'+dirname+'/'+filename)
181
182 if status:
183 continue
184
185
186 if self.usewarning:
187 self.printwarning('./SubProcesses/'+filename,'./SubProcesses/'+dirname+'/'+filename)
188
189
190 if self.raiseerror:
191 raise self.ERROR_DifferenceInFile
192
193
194 if self.intmode:
195 self.usemodif=self.printintmode('./SubProcesses/'+dirname+'/'+filename)
196
197
198 if self.usemodif:
199 self.modiffile('./SubProcesses/'+dirname+'/'+filename,self.modifrule)
200
201
203
204 print """ WARNING: those file are supposed to be identical (symbolic link):\n\
205 ./SubProcesses/"""+filename1+"""\n./SubProcesses/"""+filename2
206 print """ define the tag MW_run/901 to change the rule for modification """
207 if self.usemodif==0:
208 print """ no modification to the file are done """
209 elif self.usemodif==1:
210 print """ modify the file """
211
213
214 a=raw_input('modify file '+file+'with rule'+self.modifrule+'? (y/n)')
215 if a=='y':
216 return 1
217 elif a=='n':
218 return 0
219 else:
220 a=self.printintmode(file)
221 return a
222
225
226
227
228
230
232 """ 1. reactivate the write of events .lhe
233 2. modify combine_event.f
234 3. modify pythia-pgs/src/pythia.f --> Pass in standard in special package
235 4. modify pythia-pgs/src/pgs.f --> Pass in standard in special package
236 5. compile those modification
237 """
238
239 if not( os.path.isfile('./Cards/pythia_card.dat') and
240 os.path.isfile('./Cards/pgs_card.dat') and
241 os.path.isdir('../MW_pythia-pgs')):
242 raise AcceptanceError, 'Cards or MW_pythia-pgs missing... impossible to load acceptance module'
243
244
245 if os.path.isfile('./Source/MadWeight_file/acc.in'):
246 return
247 os.system('touch ./Source/MadWeight_file/acc.in')
248
249
250 mod_file.mod_file('./SubProcesses/unwgt.f',{'S-DECOMMENT_C':''})
251
252
253 mod_file.mod_file('./Source/combine_events.f','./Source/MadWeight/mod_file/mod_combine_events')
254
255
256 mod_file.mod_file('./Cards/pythia_card.dat','./Source/MadWeight/mod_file/mod_combine_events')
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281 os.system("cd Source;make ../bin/combine_events;cd -")
282 os.system("cd ../MW_pythia-pgs;make &>/dev/null")
283
284
286 """ check that the event are not written """
287
288 if os.path.isfile('./Source/MadWeight_file/acc.in'):
289 import expand_MadWeight
290 expand_MadWeight.expand_all(echap=['MadWeight_card.dat'])
291 os.system('rm ./Source/MadWeight_file/acc.in')
292
293
296
297
298
299
300
301
302
303
304
305
307 """create all the directory for the run """
308
309
311
312 self.MWparam=MWparam
313 self.dir_name=MWparam.name
314
315 self.ref_card=self.MWparam.actif_param[0]
316 self.created=0
317
318
320
321 print 'creating all directories'
322
323
324
325
326
327
328
329
330 if self.MWparam.nb_event:
331 for dir in self.MWparam.MW_listdir:
332 self.all_dir(dir)
333
334
336 """creates the directory for standard run """
337
338 self.dir_type='M'
339 self.Sdir_pos='./SubProcesses/'+dir
340 try:
341 os.mkdir('./SubProcesses/'+dir)
342 except:
343 pass
344
345
346 if self.dir_type=='M':
347 if self.MWparam['mw_run']['22']:
348 self.add_events()
349 else:
350 print 'create M dir'
351 self.create_M_dir()
352 else:
353 if self.MWparam['mw_run']['22']:
354 return
355 self.create_P_dir()
356
357 print 'created',self.created,'directories'
358 self.created=0
359
360
362 """creates the directory for standard run
363 number_of_event[-1]:-1:automatic (number of event of cardref if !=-1, highest existing event in other case
364 """
365
366
367 list_card=[num for num in self.MWparam.actif_param]
368
369 pbar = progressbar.progbar('update Pdir',len(list_card)*len(self.MWparam.P_listdir))
370 for directory in self.MWparam.P_listdir:
371 self.dir_type=directory[0]
372 self.Sdir_pos='./SubProcesses/'+directory
373 for card in list_card:
374 self.create_one_P_dir(card,remove_old=0)
375 pbar.update()
376 pbar.finish()
377
378
379 if number_of_event==-1 and cardref==-1:
380 self.dir_type='M'
381 self.Sdir_pos='./SubProcesses/'+self.MWparam.MW_listdir[0]
382 for card in self.MWparam.actif_param:
383 event=self.find_exist_event(card)
384 if event>=number_of_event:
385 cardref=card
386 number_of_event=event
387 elif cardref==-1:
388 ref_num=number_of_event-1
389 for card in self.MWparam.actif_param:
390 if self.find_exist_event(card)>=ref_num:
391 cardref=card
392
393 if cardref==-1 or number_of_event==-1:
394 sys.exit('impossible to update card and/or number of event not defined')
395 else:
396 self.ref_card=cardref
397
398
399
400 list_card=self.MWparam.actif_param
401 list_event=range(0,number_of_event)
402 if (len(list_card))*(len(list_event))*len(self.MWparam.MW_listdir)>0:
403 pbar = progressbar.progbar('update MWdir',(len(list_card))*(len(list_event))*len(self.MWparam.P_listdir))
404 for dir in self.MWparam.MW_listdir:
405 self.dir_type=dir[0]
406 self.Sdir_pos='./SubProcesses/'+dir
407
408
409 for card in list_card:
410 for event in list_event:
411 self.create_one_M_dir(card,event,remove_old=0)
412 pbar.update()
413 pbar.finish()
414
415
416
418
419 self.file_event=open(self.Sdir_pos+'/'+self.MWparam.name+'/verif.lhco')
420 self.line_event=self.file_event.readline()
421
422 nb_exist_event=self.find_exist_event()
423 list_card=self.MWparam.actif_param
424 list_event=range(nb_exist_event,nb_exist_event+self.MWparam.nb_event)
425 pbar = progressbar.progbar('create_dir',(len(list_card))*(len(list_event)))
426 for card in list_card:
427 for event in list_event:
428 self.create_one_M_dir(card,event)
429 pbar.update()
430 pbar.finish()
431
432
433
434
436 """ find the maximal event number in this SubProcess"""
437
438 if card_nb==-1:
439 card_nb=self.ref_card
440 directory=self.Sdir_pos+'/'+self.MWparam.name
441
442 return self.MWparam.find_existing_events(directory,card_nb)
443
444
446
447 self.file_event=open(self.Sdir_pos+'/'+self.MWparam.name+'/verif.lhco')
448 self.line_event=self.file_event.readline()
449 dirname = self.Sdir_pos.split('/')[-1]
450 self.del_old_dir()
451 list_card=self.MWparam.actif_param
452
453
454 pbar = progressbar.progbar('create_dir',(len(list_card))*(self.MWparam.nb_event_MW[dirname]))
455 for card in list_card:
456 list_event=range(0, self.MWparam.nb_event_MW[dirname])
457 try:
458 os.mkdir(self.Sdir_pos+'/'+self.MWparam.name+'/card_'+str(card))
459 except:
460 pass
461 for event in list_event:
462 try:
463 self.create_one_M_dir(card,event)
464 except NoMoreEvent:
465 os.system('rm -rf %s' % self.Sdir_pos+'/'+self.MWparam.name+'/card_'+str(card))
466 self.MWparam.nb_event_MW[dirname] = event
467 break
468
469 pbar.update()
470 pbar.finish()
471
472
483
484
486 """ create the directory for the event \"event\" and the card nb \"card\"
487 """
488 dir_name=self.MWparam.name
489 pos=self.Sdir_pos+'/'+dir_name+'/card_'+str(card)+'/event_'+str(event)+'/'
490
491 if not os.path.isdir(self.Sdir_pos+'/'+dir_name+'/card_'+str(card)):
492 os.mkdir(self.Sdir_pos+'/'+dir_name+'/card_'+str(card))
493
494
495
496
497 try:
498 os.mkdir(pos)
499 except OSError:
500 if remove_old:
501 os.system('rm '+pos+'/* >/dev/null')
502
503
504
505
506 ff=open(pos+'/param.dat','w')
507 ff.writelines('param_card_'+str(card)+'.dat\n')
508 ff.writelines(str(self.MWparam['mw_run']['MW_int_points'])+'\n')
509 ff.close()
510
511 if card==self.ref_card:
512 data=self.give_new_exp_point()
513 hh=open(pos+'/verif.lhco','w')
514 hh.writelines(data)
515 hh.close()
516 else:
517 try:
518 os.symlink('../../card_'+str(self.ref_card)+'/event_'+str(event)+'/verif.lhco',pos+'/verif.lhco')
519 except OSError:
520 pass
521 self.created+=1
522
523
524
526 """ create the directory for the event \"event\" and the card nb \"card\"
527 """
528 dir_name=self.MWparam.name
529 pos=self.Sdir_pos+'/'+dir_name+'/card_'+str(card)
530 try:
531 os.mkdir(pos)
532 except:
533 if remove_old:
534 os.system('rm '+pos+'/* >/dev/null')
535 else:
536 return
537 ff=open(pos+'/param.dat','w')
538 ff.writelines('param_card_'+str(card)+'.dat\n')
539 ff.writelines(str(self.MWparam['mw_run']['MW_int_points'])+'\n')
540 ff.close()
541 os.system('ln -s ../../madevent.py '+pos+'/madevent.py')
542 os.system('ln -s ../../madevent '+pos+'/madevent')
543 os.system('ln -s ../../input_app.txt '+pos+'/input_app.txt')
544 os.system('ln -s ../../symfact.dat '+pos+'/symfact.dat')
545
546 for file in os.listdir(pos+'/../../'):
547 if len(file)>4 and file[:4]=='ajob':
548 os.system('ln -s ../../'+file+' '+pos)
549
550 self.created+=1
551
552 Pattern=re.compile(r'''^\s*0\s+\d+\s+\d+\s*$''',re.I)
553
555
556 data=self.line_event
557 while 1:
558 try:
559 line=self.file_event.readline()
560 except ValueError:
561 raise NoMoreEvent
562
563 if line=='':
564 self.file_event.close()
565 if data == '':
566 raise NoMoreEvent
567 return data
568 if self.Pattern.search(line):
569 self.line_event=line
570 return data
571 else:
572 data+=line
573
574
576 ''' delete old event directory '''
577
578 output=1
579
580 list_dir=os.listdir(os.pardir)
581 if self.MWparam.info['mw_run']['22']:
582 print self.Sdir_pos.split('/')[-1],': no deleting'
583 return output
584
585 print self.Sdir_pos.split('/')[-1],': deleting old run directory'
586 print os.getcwd()
587 os.system('mkdir '+self.Sdir_pos+'/'+self.MWparam.name+'/')
588 for datafile in os.listdir(self.Sdir_pos+'/'+self.MWparam.name):
589 if datafile not in ['verif.lhco']:
590 try: os.system('rm '+self.Sdir_pos+'/'+self.MWparam.name+'/'+datafile+' -rf')
591 except:
592 print "WARNING: this directory ", os.getcwd()+'/'+self.Sdir_pos+'/'+self.MWparam.name," are not deleted"
593 output=0
594
595
596
597 return output
598
599
601 pos=os.getcwd()
602 init_dir=os.sep+pos.split(os.sep)[-1]
603
604 title=[]
605 content=[]
606
607 for file in list:
608 title.append(file.split('/')[-1])
609 ff=open(file,'r')
610 content.append(ff.readlines())
611 ff.close()
612
613
614 os.chdir('../'+dir_name+str(i))
615 for i in range(0,len(title)):
616 ff=open('./'+title[i],'w')
617 ff.writelines(content[i])
618 ff.close()
619 os.chdir(os.pardir+init_dir)
620
622 """ restore run info for controlling """
623
624 pattern=re.compile(r'''condor_id/\s*(?P<run_list>.+)\s*/job/\s*(?P<job>\d*)''',re.DOTALL)
625 launch_job=0
626 run_job=[]
627 dir_list=[]
628
629 for dir in MW_param.P_listdir+MW_param.MW_listdir:
630 ff=open('./SubProcesses/'+dir+'/schedular/condor.log','r')
631 text=ff.readline()
632 ff.close()
633 pat=pattern.search(text)
634 if pat:
635 launch_job+=int(pat.group('job'))
636 prov_job=eval(pat.group('run_list'))
637 dir_list+=[dir]*len(prov_job)
638 run_job+=prov_job
639 return launch_job,run_job,dir_list
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
699
700 for directory in MWparam.MW_listdir+MWparam.P_listdir:
701 try:
702 os.mkdir('./SubProcesses/'+directory+'/schedular')
703 except:
704 pass
705
706
707
708
709
710
711
712
713
714 if __name__=='__main__':
715
716
717
718
719 import MW_param
720 MW_param.go_to_main_dir()
721 MWparam=MW_param.MW_info('MadWeight_card.dat')
722
723
724 create_obj=create_dir(MWparam)
725 create_obj.update_card_status()
726
727
728
729