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