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