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 import os
36 import popen2
37 import re
38 import sys
39 import time
40 import math
41
42 try:
43 import madgraph.madweight.diagram_class as diagram_class
44 import madgraph.madweight.substructure_class as substructure_class
45 import madgraph.madweight.MW_info as MW_param
46
47 except ImportError:
48 import internal.madweight.diagram_class as diagram_class
49 import internal.madweight.substructure_class as substructure_class
50 import internal.madweight.MW_info as MW_param
51
52 go_to_main_dir = MW_param.go_to_main_dir
53 pjoin = os.path.join
54
55
57
58
59
60 go_to_main_dir()
61 try:
62 os.mkdir('./Events/'+MWparam.name)
63 except:
64 pass
65 os.system('cp ./Events/input.lhco ./Events/'+MWparam.name+'/')
66
67
68 for MW_dir in MWparam.MW_listdir:
69 start=time.time()
70 select=Lhco_filter(MW_dir,'input.lhco',MWparam)
71 print 'time Lhco_filter',time.time()-start
72
74 """ return the number of events in a (previous) run which pass the 'new' cut """
75
76
77
78 go_to_main_dir()
79 try:
80 os.mkdir('./Events/'+MWparam.name)
81 except:
82 pass
83 os.system('cp ./SubProcesses/'+MWparam.MW_listdir[0]+'/'+MWparam.old_name+'/verif.lhco ./Events/'+MWparam.name+'/')
84
85
86 MW_dir=MWparam.MW_listdir[0]
87 filter=Lhco_filter(MW_dir,MWparam=MWparam,auto=0)
88 return filter.verif_event(MWparam.name+'/verif.lhco',output=1)
89
90
91
92
94
96
97
98 - def __init__(self,directory,lhco_file='',MWparam='',auto=1,write_mode=1):
99 """ input is either a file containing particule number info or a SubProcesses directory """
100
101 start=time.time()
102 if MWparam:
103 self.MWparam=MWparam
104 else:
105 import MW_param
106 self.MWparam=MW_param.MW_info('MadWeight_card.dat')
107 self.write_mode=write_mode
108
109
110 if directory.count('SubProcesses'):
111 self.directory=directory
112 else:
113 self.directory='./SubProcesses/'+directory
114
115 self.lhco_file=lhco_file
116 self.partdef=self.find_particle_number()
117
118 self.write_events=0
119 if lhco_file and auto:
120 self.verif_event(lhco_file,self.partdef)
121
122
124
125
126
127 self.load_particle_number(self.directory)
128
129 partdef=lhco_all_particles_def()
130 if not self.MWparam.info['mw_perm']['bjet_is_jet_for_selection']:
131 partdef.use_bjet()
132 if self.MWparam.info.has_key('eventselection'):
133 partdef.update_hlt_cut(self.MWparam.info['eventselection'])
134
135 return partdef
136
137
138 - def load_particle_number(self,directory):
139 """ extract the number of particule from the iconfigs """
140
141 diag=diagram_class.MG_diagram(directory,1)
142
143 list=['jet','bjet','electron','positron','muon','amuon','tau','atau', 'miss','photon']
144 content=diag.output_type_info()
145
146 total=0
147 data={}
148 for i in range(0,len(list)):
149 data[list[i]]=content[i]
150 total+=content[i]
151
152
153
154 if not self.MWparam.info['mw_perm']['bjet_is_jet_for_selection']:
155 self.use_bjet=1
156 else:
157 self.use_bjet=0
158 data['jet']+=data['bjet']
159 data['bjet']=0
160
161 data['begin']=[0,1]
162 data['miss']=[0,1]
163 data['unknow']=range(0,10)
164 self.nb_part=data
165 return data
166
167
169
170 if dir:
171 os.chdir('./SubProcesses/'+dir)
172
173 ff=open('./info_part.dat','r')
174
175 text=ff.readline()
176 ff.close()
177 list=['jet','bjet','electron','positron','muon','amuon','tau','atau','miss']
178 info=text.split()[1:]
179
180 data={}
181 total=0
182 for i in range(0,len(list)):
183 data[list[i]]=int(info[i])
184 total+=int(info[i])
185 data['n_out']=total
186 os.chdir('../..')
187
188
189 if not self.MWparam.info['mw_perm']['bjet_is_jet_for_selection']:
190 self.use_bjet=1
191 else:
192 self.use_bjet=0
193 data['jet']+=data['bjet']
194 data['bjet']=0
195
196
197
198 self.nb_part=data
199 return data
200
201
202 - def define_particle_number(self,particle,number):
203
204 list=['jet','bjet','electron','positron','muon','amuon','tau','atau','miss']
205 if particle not in list:
206 print 'unknown type of particle'
207 return
208 else:
209 self.nb_part[particle]=int(number)
210 return
211
212
214 """ use the cuts to select event in file
215 output defines what returns the routine
216 0: write the file + returns how many events pass
217 1: returns the list of events passing cuts
218 """
219 start=time.time()
220
221
222 if not part_def:
223 print 'use default part_def'
224 part_def=self.partdef
225 if not file:
226 file='./Events/'+self.lhco_file
227
228 if os.path.isfile(file): f_in=open(file,'r')
229 elif os.path.isfile('./Events/'+file): f_in=open('./Events/'+file,'r')
230 else: sys.exit('FATAL ERROR: No experimental file \"'+file+'\" in Events directory.')
231
232
233
234 if self.MWparam.info['mw_run'].has_key('21'):
235 self.start=int(self.MWparam.info['mw_run']['21'])
236 print 'start', self.start
237 else:
238 self.start=0
239
240
241 if output==0:
242 os.system('mkdir '+self.directory+'/'+self.MWparam.name+' &>/dev/null')
243 self.f_out=open(self.directory+'/'+self.MWparam.name+'/verif_0.lhco','w')
244 elif output==1:
245 self.accepted_list=[]
246
247
248
249
250
251 list_part=[]
252 nb_part={}
253 self.event_position=0
254 lhco_id_tag = set()
255 nb_accepted = 0
256
257 for line in f_in:
258 if nb_accepted >= self.MWparam.info['mw_run']['nb_exp_events']:
259 break
260 if line[0]=='#':
261 continue
262 try:
263 start2=time.time()
264 part=lhco_part(line)
265 identity=part_def.identify_particle(part)
266 part.def_identity(identity)
267 if identity=='begin':
268 lhco_id_tag.add(line.split()[1])
269 if self.check_valid(nb_part):
270 nb_accepted += 1
271 self.write(list_part)
272
273
274
275 list_part=[part]
276 nb_part={'begin':1,'unknow':0}
277 self.event_position+=1
278 else:
279 list_part.append(part)
280 if nb_part.has_key(identity):
281 nb_part[identity]+=1
282 else:
283 nb_part[identity]=1
284 except lhco_part.ErrorNotLHCOformat:
285
286
287
288
289 list_part=[]
290 nb_part={}
291
292
293 if self.check_valid(nb_part):
294 if nb_accepted < self.MWparam.info['mw_run']['nb_exp_events']:
295 self.write(list_part)
296 print 'time verif event Lhco_filter',time.time()-start
297 print self.write_events-self.start,'selected events for ',self.directory,' subprocess'
298
299 if self.write_events-self.start<self.MWparam.nb_event:
300 name = self.directory.split('/')[-1]
301 self.MWparam.nb_event_MW[name] = self.write_events-self.start
302 if output==0:
303 return self.write_events
304 elif output==1:
305 return self.accepted_list
306
307
309 """ check if we have the correct number of input for each type of particle """
310
311 list_key=self.nb_part.keys()+[key for key in nb_part if key not in self.nb_part.keys()]
312 try:
313 for key in list_key:
314 if self.nb_part[key]==0:
315 if not nb_part.has_key(key):
316 continue
317 elif nb_part[key]==0:
318 continue
319 else:
320 return 0
321
322 if not nb_part.has_key(key):
323 return 0
324
325 if type(self.nb_part[key])==list:
326 if nb_part[key] not in self.nb_part[key] :
327 return 0
328 elif nb_part[key]!=self.nb_part[key]:
329 return 0
330 return 1
331 except KeyError:
332 print nb_part
333 print self.nb_part
334 print key
335 if self.write_events: print 'key error'
336 return 0
337
338 write_order=['begin','jet','bjet','electron','positron','muon','amuon','tau','atau', 'photon','miss','init']
339
340 - def write(self,list_part):
341 """ write the output file """
342
343 if hasattr(self, 'f_out') and self.write_events and \
344 self.write_events % (self.MWparam['mw_run']['nb_event_by_node'] * self.MWparam['mw_run']['event_packing']) == 0:
345
346 i = self.write_events // (self.MWparam['mw_run']['nb_event_by_node'] * self.MWparam['mw_run']['event_packing'])
347 name = self.f_out.name
348 base, name = os.path.split(name)
349 name = os.path.join(base, name.replace('_%i' % (i-1), '_%i' % i ))
350 self.f_out = open(name,'w')
351
352
353 self.write_events+=1
354 if self.write_mode==0:
355 return
356 if self.write_events<=self.start:
357 return
358
359
360 if hasattr(self,'f_out'):
361
362 write_order = self.write_order
363
364 for i in range(0,len(write_order)):
365 for j in range(0,len(list_part)):
366 if list_part[j].name==write_order[i]:
367 self.f_out.write(list_part[j].line)
368
369 if hasattr(self,'accepted_list'):
370 self.accepted_list.append(self.event_position-1)
371
372
374 """
375 a class containing all the particles definition
376 """
377
378
380 """ a class containing the LHCO definition-restriction of on each lhco particles
381 this defines rules to know of wich type a particle is
382 """
383 eta_max=1e2
384 pt_max=1e6
385 ntrk_max=1e3
386
387
389 """ set a minimum and a maximum value for a parameter """
390
391 - def __init__(self,vmin,vmax):
392 self.vmin=vmin
393 self.vmax=vmax
394
395
396 - def redefine(self,min,max):
397 self.__init__(min,max)
398
399
400 - def inlimit(self,value):
401 """ check if value is between min and max """
402 value=float(value)
403 if value>=self.vmin and value<=self.vmax:
404 return 1
405 else:
406
407 return 0
408
409
410 - def __init__(self,name,type,pid):
411 """ initialize the object.
412 name is the name of the particle described
413 type is the type value in the lhco file
414 """
415
416 self.name=name
417 self.lhcoid=str(type)
418 self.init_default()
419 self.pid=pid
420
421
422 - def init_default(self):
423 """ put the zero cut on the particle """
424
425 self.eta=self.bound_limit(-self.eta_max,self.eta_max)
426 self.phi=self.bound_limit(-math.pi,2*math.pi)
427 self.pt=self.bound_limit(0,self.pt_max)
428 self.jmass=self.bound_limit(-1e-5,self.pt_max)
429 self.ntrk=self.bound_limit(-self.ntrk_max,self.ntrk_max)
430 self.btag=self.bound_limit(-100,100)
431 self.hadem=self.bound_limit(-1e99,1e99)
432 self.dum1=self.bound_limit(-1e99,1e99)
433 self.dum2=self.bound_limit(-1e99,1e99)
434
435 self.E=self.bound_limit(0,1e99)
436
437
438 - def restrict(self,tag,min_val,max_val):
439 """ add a restriction on a parameter """
440
441 eval('self.'+tag+'.redefine('+str(min_val)+','+str(max_val)+')')
442
443
444 - def check(self,particle):
445 """ check if a particle is of this type or not """
446
447 if (particle.lhcoid!=self.lhcoid):
448 self.failed_reason='lhcoid'
449 return 0
450 elif(not self.ntrk.inlimit(particle.ntrk)):
451 self.failed_reason='ntrk'
452 return 0
453 elif(not self.btag.inlimit(particle.btag)):
454 self.failed_reason='btag'
455 return 0
456 elif(not self.eta.inlimit(particle.eta)):
457 self.failed_reason='eta'
458 return 0
459 elif(not self.phi.inlimit(particle.phi)):
460 self.failed_reason='phi'
461 return 0
462 elif(not self.pt.inlimit(particle.pt)):
463 self.failed_reason='pt'
464 return 0
465 elif(not self.jmass.inlimit(particle.jmass)):
466 self.failed_reason='jmass'
467 return 0
468 elif(not self.hadem.inlimit(particle.hadem)):
469 self.failed_reason='hadem'
470 return 0
471 elif(not self.E.inlimit(particle.E)):
472 self.failed_reason='E'
473 return 0
474 elif(not self.dum1.inlimit(particle.dum1)):
475 self.failed_reason='dum1'
476 return 0
477 elif(not self.dum2.inlimit(particle.dum2)):
478 self.failed_reason='dum2'
479 return 0
480 else:
481 self.failed_reason='no'
482 return 1
483
484
485
486
487 if (particle.lhcoid==self.lhcoid and
488 self.ntrk.inlimit(particle.ntrk) and
489 self.btag.inlimit(particle.btag) and
490 self.hadem.inlimit(particle.hadem) and
491 self.eta.inlimit(particle.eta) and
492 self.phi.inlimit(particle.phi) and
493 self.pt.inlimit(particle.pt) and
494 self.jmass.inlimit(particle.jmass) and
495 self.dum1.inlimit(particle.dum1) and
496 self.dum2.inlimit(particle.dum2) and
497 self.E.inlimit(particle.E)):
498 return 1
499 else:
500 return 0
501
502
503
504
505 - def __init__(self):
506
507
508 self['electron']=self.lhco_id('electron',1,11)
509 self.electron=self['electron']
510 self.electron.restrict('ntrk',-1,-1)
511 self['positron']=self.lhco_id('positron',1,-11)
512 self.positron=self['positron']
513 self.positron.restrict('ntrk',1,1)
514 self['muon']=self.lhco_id('muon',2,13)
515 self.muon=self['muon']
516 self.muon.restrict('ntrk',-1,-1)
517 self['amuon']=self.lhco_id('amuon',2,-13)
518 self.amuon=self['amuon']
519 self.amuon.restrict('ntrk',1,1)
520 self['tau']=self.lhco_id('tau',3,15)
521 self.tau=self['tau']
522 self.tau.restrict('ntrk',-3,-1)
523 self['atau']=self.lhco_id('atau',3,15)
524 self.atau=self['atau']
525 self.atau.restrict('ntrk',1,3)
526
527
528 self['photon']=self.lhco_id('photon',0,22)
529 self.photon=self['photon']
530
531
532
533 self['jet']=self.lhco_id('light_jet',4,1)
534 self.jet=self['jet']
535
536 self['miss']=self.lhco_id('miss',6,0)
537 self.miss=self['miss']
538
539 self['init']=self.lhco_id('init',7,0)
540 self.init=self['init']
541
542 self['begin']=self.lhco_id('begin',99,0)
543 self.begin=self['begin']
544
545
546 self.nb_warning = 0
547
548
549 - def use_bjet(self):
550 """ separate the class jet between jet and bjet """
551 self['jet'].restrict('btag',0,0)
552 self['bjet']=self.lhco_id('bjet',4,5)
553 self['bjet'].restrict('btag',1,4)
554 self.bjet=self['bjet']
555
556
557 - def update_hlt_cut(self,hltcut):
558 """ take the hlt cut from the Madweight card """
559
560 print 'update cut :',hltcut
561 for key in hltcut:
562 name,param=key.split('_')
563 if(type(hltcut[key])==list):
564 self[name].restrict(param,hltcut[key][0],hltcut[key][1])
565 else:
566 self[name].restrict(param,hltcut[key],9e99)
567
568
569 - def identify_particle(self,particle):
570 """ find in wich category the particles belongs """
571
572
573 for name in ['begin','jet','miss','electron','photon']:
574 if self[name].check(particle):
575
576 return name
577
578 for name in [name for name in self.keys() if name not in ['begin','jet','miss','electron','photon']]:
579 if self[name].check(particle):
580
581 return name
582
583 if not self.nb_warning:
584 print 'Some particles are not identified to any types. This could occur if you specify some cuts.'
585 print 'Following lines shows a sample of those unidentified lines:'
586
587 if self.nb_warning<10:
588 self.nb_warning+=1
589 print particle.line[:-1]
590 return 'unknow'
591
592
593
594
595
596 pat_lhco_line=re.compile(r'''^\s*(?P<card>\d*)\s+ #cardinal
597 (?P<type>\d*) # type: 0 = photon,1 = elec,2 = muon,3 = hadronic tau,4 = jet,6 =MTE
598 \s+(?P<eta>[+-\.\de]+) # pseudorapidity
599 \s+(?P<phi>[+-\.\de]+)\s+ # phi
600 (?P<pt>[+-\.\de]*)\s+ #pt
601 (?P<jmass>[+-\.\de]+)\s+ #invariant mass of the object
602 (?P<ntrk>[+-\.\de]+)\s+ #number of tracks associated( muliplied by charge for lepton)
603 (?P<btag>[+-\.\de]+)\s+ #jet: !=0 taggued b//muon: closest jet
604 (?P<hadem>[+-\.\de]+)\s+ #hadronic versus electromagnetic energy deposited
605 (?P<dum1>[+-\.\de]+)\s+ # user free at this stage
606 (?P<dum2>[+-\.\de]+)\s+$ # user free at this stage
607 ''',re.I+re.VERBOSE)
608
609
610 pat_new_block=re.compile(r'''^\s*0\s+ #cardinal
611 (?P<type>\S*) # type: 0 = photon,1 = elec,2 = muon,3 = hadronic tau,4 = jet,6 =MTE
612 \s+(?P<eta>[+-\.\de]+)\s+
613 ''',re.I+re.VERBOSE)
614
615
616
617
618
620 """ a class for a particle from the lhco line """
621
623
624
625
627 """ charge a particle """
628 self.line=line
629 reobj=pat_lhco_line.search(line)
630 if not reobj:
631 if not self.beginblok(line):
632 raise self.ErrorNotLHCOformat
633 else:
634 return
635 self.card=reobj.group('card')
636 self.lhcoid=reobj.group('type')
637 self.eta=reobj.group('eta')
638 self.phi=reobj.group('phi')
639 self.pt=reobj.group('pt')
640 self.jmass=reobj.group('jmass')
641 self.ntrk=reobj.group('ntrk')
642 self.btag=reobj.group('btag')
643 self.hadem=reobj.group('hadem')
644 self.dum1=reobj.group('dum1')
645 self.dum2=reobj.group('dum2')
646
647 self.px=float(self.pt)*math.cos(float(self.phi))
648 self.py=float(self.pt)*math.sin(float(self.phi))
649 self.pz=float(self.pt)*math.sinh(float(self.eta))
650 self.E =math.sqrt(self.px**2+self.py**2+self.pz**2+float(self.jmass)**2)
651
652
654 """ charge a particle """
655
656 reobj=pat_new_block.search(line)
657 if not reobj:
658 return 0
659
660 self.card=0
661 self.lhcoid='99'
662 self.eta='0'
663 self.phi='0'
664 self.pt='0'
665 self.jmass='0'
666 self.ntrk='0'
667 self.btag='0'
668 self.hadem='0'
669 self.dum1='0'
670 self.dum2='0'
671 self.E='0'
672 return 1
673
674
676 """ def name of the type of line """
677
678 self.name=name
679
681
683 self.lhco_file=raw_input('enter the name of the file to test : ').split()[0]
684 Card_pos=raw_input('give position to a MadWeight_card.dat: ').split()[0]
685 import MW_param
686 self.MWparam=MW_param.MW_info(Card_pos)
687 val=''
688 while val not in ['0','1']: val=raw_input('use the file info_part.dat (0/1)?')
689 if val=='1':
690 self.partdef=self.extract_file_info()
691 else:
692 self.nb_part={}
693 for element in ['jet','bjet','electron','positron','muon','amuon','tau','atau','miss']:
694 value=raw_input('enter authorize value for the nb of particule of type '+element+' : ')
695 value.split()
696 if len(value)==1: self.nb_part[element]=int(value[0])
697 else: self.nb_part[element]=value
698 self.nb_part['photon']=0
699
700 partdef=lhco_all_particles_def()
701 if not self.MWparam.info['mw_perm']['bjet_is_jet_for_selection']:
702 partdef.use_bjet()
703 if self.MWparam.info.has_key('eventselection'):
704 partdef.update_hlt_cut(self.MWparam.info['eventselection'])
705 self.partdef=partdef
706
707 self.write_events=0
708 self.verif_event(self.lhco_file,self.partdef,output=1)
709
710
711
712
713 if(__name__=="__main__"):
714
715
716
717 from MW_param import go_to_main_dir
718 opt=sys.argv
719 if len(opt)==1:
720 go_to_main_dir()
721 Lhco_filter('proc_card.dat')
722 else:
723 pos='/'.join(opt[0].split('/')[:-1])
724 print pos
725 sys.path.append(pos)
726 print sys.path
727 Test_one_file()
728