~maddevelopers/mg5amcnlo/WWW5_caching

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
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
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
################################################################################
#
# Copyright (c) 2011 The MadGraph Development team and Contributors
#
# This file is a part of the MadGraph 5 project, an application which 
# automatically generates Feynman diagrams and matrix elements for arbitrary
# high-energy processes in the Standard Model and beyond.
#
# It is subject to the MadGraph license which should accompany this 
# distribution.
#
# For more information, please visit: http://madgraph.phys.ucl.ac.be
#
################################################################################
"""A File for splitting"""

import sys
import re
import os

pjoin = os.path.join

try:
    import madgraph.various.misc as misc
    import madgraph.iolibs.file_writers as file_writers
    import models.check_param_card as param_card_reader
    from madgraph import MG5DIR
    MADEVENT = False
except ImportError:
    MADEVENT = True
    import internal.file_writers as file_writers
    import internal.check_param_card as param_card_reader
    MEDIR = os.path.split(os.path.dirname(os.path.realpath( __file__ )))[0]
    MEDIR = os.path.split(MEDIR)[0]

import logging

logger = logging.getLogger('madevent.cards')

#dict
class Banner(dict):
    """ """

    ordered_items = ['mgversion', 'mg5proccard', 'mgproccard', 'mgruncard',
                     'slha', 'MGGenerationInfo', 'mgpythiacard', 'mgpgscard',
                     'mgdelphescard', 'mgdelphestrigger']
    
    def __init__(self, banner_path=None):
        """ """
        dict.__init__(self)
        
        #Look at the version
        if MADEVENT:
            self['mgversion'] = '#%s\n' % open(pjoin(MEDIR, 'MGMEVersion.txt')).read()
        else:
            info = misc.get_pkg_info()
            self['mgversion'] = info['version']+'\n'
            
        if banner_path:
            self.read_banner(banner_path)

    ############################################################################
    #  READ BANNER
    ############################################################################
    pat_begin=re.compile('<(?P<name>\w*)>')
    pat_end=re.compile('</(?P<name>\w*)>')

    tag_to_file={'slha':'param_card.dat',
      'mgruncard':'run_card.dat',
      'mgpythiacard':'pythia_card.dat',
      'mgpgscard' : 'pgs_card.dat',
      'mgdelphescard':'delphes_card.dat',      
      'mgdelphestrigger':'delphes_trigger.dat',
      'mg5proccard':'proc_card_mg5.dat',
      'mgproccard': 'proc_card.dat',
      'init': '',
      'mggenerationinfo':'',
      'montecarlomasses':'',
      'madspin':'madspin_card.dat' 
      }
    
    def read_banner(self, input_path):
        """read a banner"""

        if isinstance(input_path, str):
            input_path = open(input_path)

        text = ''
        store = False
        for line in input_path:
            if self.pat_begin.search(line):
                tag = self.pat_begin.search(line).group('name').lower()
                if tag in self.tag_to_file:
                    store = True
                    continue
            if store and self.pat_end.search(line):
                if tag == self.pat_end.search(line).group('name').lower():
                    self[tag] = text
                    text = ''
                    store = False
            if store:
                text += line
                
            #reaching end of the banner in a event file avoid to read full file 
            if "</init>" in line:
                break
            elif "<event>" in line:
                break
                
    def load_basic(self, medir):
        """ Load the proc_card /param_card and run_card """
        
        self.add(pjoin(medir,'Cards', 'param_card.dat'))
        self.add(pjoin(medir,'Cards', 'run_card.dat'))
        if os.path.exists(pjoin(medir, 'SubProcesses', 'procdef_mg5.dat')):
            self.add(pjoin(medir,'SubProcesses', 'procdef_mg5.dat'))
            self.add(pjoin(medir,'Cards', 'proc_card_mg5.dat'))
        else:
            self.add(pjoin(medir,'Cards', 'proc_card.dat'))
    
    
    def change_seed(self, seed):
        """Change the seed value in the banner"""
        #      0       = iseed
        p = re.compile(r'''^\s*\d+\s*=\s*iseed''', re.M)
        new_seed_str = " %s = iseed" % seed
        self['mgruncard'] = p.sub(new_seed_str, self['mgruncard'])
    
    def add_generation_info(self, cross, nb_event):
        """add info on MGGeneration"""
        
        text = """
#  Number of Events        :       %s
#  Integrated weight (pb)  :       %s
""" % (nb_event, cross)
        self['MGGenerationInfo'] = text
    
    ############################################################################
    #  SPLIT BANNER
    ############################################################################
    def split(self, me_dir, proc_card=True):
        """write the banner in the Cards directory.
        proc_card argument is present to avoid the overwrite of proc_card 
        information"""

        for tag, text in self.items():
            if tag == 'mgversion':
                continue
            if not proc_card and tag in ['mg5proccard','mgproccard']:
                continue
            if not self.tag_to_file[tag]:
                continue
            ff = open(pjoin(me_dir, 'Cards', self.tag_to_file[tag]), 'w')
            ff.write(text)
            ff.close()

    ############################################################################
    #  WRITE BANNER
    ############################################################################
    def check_pid(self, pid2label):
        """special routine removing width/mass of particles not present in the model
        This is usefull in case of loop model card, when we want to use the non
        loop model."""
        
        if not hasattr(self, 'param_card'):
            self.charge_card('slha')
            
        for tag in ['mass', 'decay']:
            block = self.param_card.get(tag)
            for data in block:
                pid = data.lhacode[0]
                if pid not in pid2label.keys(): 
                    block.remove((pid,))

    ############################################################################
    #  WRITE BANNER
    ############################################################################
    def write(self, output_path, close_tag=True):
        """write the banner"""
        
        if isinstance(output_path, str):
            ff = open(output_path, 'w')
        else:
            ff = output_path
            
        if MADEVENT:
            header = open(pjoin(MEDIR, 'Source', 'banner_header.txt')).read()
        else:
            header = open(pjoin(MG5DIR,'Template', 'LO', 'Source', 'banner_header.txt')).read()
        
        ff.write(header)


        for tag in [t for t in self.ordered_items if t in self.keys()]:
            ff.write('<%(tag)s>\n%(text)s\n</%(tag)s>\n' % \
                     {'tag':tag, 'text':self[tag].strip()})
        for tag in [t for t in self.keys() if t not in self.ordered_items]:
            if tag in ['init']:
                continue
            ff.write('<%(tag)s>\n%(text)s\n</%(tag)s>\n' % \
                     {'tag':tag, 'text':self[tag].strip()})

        ff.write('</header>\n')    

        if 'init' in self:
            text = self['init']
            ff.write('<%(tag)s>\n%(text)s\n</%(tag)s>\n' % \
                     {'tag':'init', 'text':text.strip()})  
        if close_tag:          
            ff.write('</LesHouchesEvents>\n')
        
        
    ############################################################################
    # BANNER
    ############################################################################
    def add(self, path, tag=None):
        """Add the content of the file to the banner"""
        
        if not tag:
            card_name = os.path.basename(path)
            if 'param_card' in card_name:
                tag = 'slha'
            elif 'run_card' in card_name:
                tag = 'MGRunCard'
            elif 'pythia_card' in card_name:
                tag = 'MGPythiaCard'
            elif 'pgs_card' in card_name:
                tag = 'MGPGSCard'
            elif 'delphes_card' in card_name:
                tag = 'MGDelphesCard'
            elif 'delphes_trigger' in card_name:
                tag = 'MGDelphesTrigger'
            elif 'proc_card_mg5' in card_name:
                tag = 'MG5ProcCard'
            elif 'proc_card' in card_name:
                tag = 'MGProcCard'
            elif 'procdef_mg5' in card_name:
                tag = 'MGProcCard'
            else:
                raise Exception, 'Impossible to know the type of the card'

            self.add_text(tag.lower(), open(path).read())

    def add_text(self, tag, text):
        """Add the content of the file to the banner"""
        
        self[tag.lower()] = text
    
    
    def charge_card(self, tag):
        """Build the python object associated to the card"""
        
        if tag == 'param_card':
            tag = 'slha'
        elif tag == 'run_card':
            tag = 'mgruncard' 
        elif tag == 'proc_card':
            tag = 'mg5proccard' 
        
        assert tag in ['slha', 'mgruncard', 'mg5proccard'], 'invalid card %s' % tag
        
        if tag == 'slha':
            param_card = self[tag].split('\n')
            self.param_card = param_card_reader.ParamCard(param_card)
            return self.param_card
        elif tag == 'mgruncard':
            run_card = self[tag].split('\n') 
            if 'parton_shower' in self[tag]:
                self.run_card = RunCardNLO(run_card)
            else:
                self.run_card = RunCard(run_card)
            return self.run_card
        elif tag == 'mg5proccard':
            proc_card = self[tag].split('\n')
            self.proc_card = ProcCard(proc_card)
            return self.proc_card

        
    def get_detail(self, tag, *arg):
        """return a specific """
                
        if tag == 'param_card':
            tag = 'slha'
            attr_tag = 'param_card'
        elif tag == 'run_card':
            tag = 'mgruncard' 
            attr_tag = 'run_card'
        elif tag == 'proc_card':
            tag = 'mg5proccard' 
            attr_tag = 'proc_card'
        elif tag == 'model':
            tag = 'mg5proccard' 
            attr_tag = 'proc_card'
            arg = ('model',)
        elif tag == 'generate':
            tag = 'mg5proccard' 
            attr_tag = 'proc_card'
            arg = ('generate',)
        assert tag in ['slha', 'mgruncard', 'mg5proccard'], 'not recognized'
        
        if not hasattr(self, attr_tag):
            self.charge_card(attr_tag) 
            
        card = getattr(self, attr_tag)
        if len(arg) == 1:
            if tag == 'mg5proccard':
                return card.info[arg[0]]
            return card[arg[0]]
        elif len(arg) == 2 and tag == 'slha':
            return card[arg[0]].get(arg[1:])
        else:
            raise Exception, "Unknow command"
    
    #convenient alias
    get = get_detail
        
def split_banner(banner_path, me_dir, proc_card=True):
    """a simple way to split a banner"""
    
    banner = Banner(banner_path)
    banner.split(me_dir, proc_card)
    
def recover_banner(results_object, level):
    """as input we receive a gen_crossxhtml.AllResults object.
       This define the current banner and load it
    """
    try:  
        run = results_object.current['run_name']    
        tag = results_object.current['tag'] 
    except Exception:
        return Banner()                                  
    path = results_object.path
    banner_path = pjoin(path,'Events',run,'%s_%s_banner.txt' % (run, tag))
    
    if not os.path.exists(banner_path):
        # security if the banner was remove (or program canceled before created it)
        return Banner()  
    
    banner = Banner(banner_path)
    
    
    
    if level == 'pythia':
        if 'mgpythiacard' in banner:
            del banner['mgpythiacard']
    if level in ['pythia','pgs','delphes']:
        for tag in ['mgpgscard', 'mgdelphescard', 'mgdelphestrigger']:
            if tag in banner:
                del banner[tag]
    return banner
    

class RunCard(dict):
    """A class object for the run_card"""

    def __init__(self, run_card):
        """ """
        
        if isinstance(run_card, str):
            run_card = file(run_card,'r')
        else:
            pass # use in banner loading

        for line in run_card:
            line = line.split('#')[0]
            line = line.split('!')[0]
            line = line.split('=')
            if len(line) != 2:
                continue
            self[line[1].strip()] = line[0].replace('\'','').strip()

    def get_default(self, name, default, log_level):
        """return self[name] if exist otherwise default. log control if we 
        put a warning or not if we use the default value"""

        try:
            return self[name]
        except KeyError:
            if log_level:
                logger.log(log_level, 'run_card missed argument %s. Takes default: %s'
                                   % (name, default))
                self[name] = default
                return default
        
    @staticmethod
    def format(format, value):
        """format the value"""
        
        if format == 'bool':
            if str(value) in ['1','T','.true.','True']:
                return '.true.'
            else:
                return '.false.'
            
        elif format == 'int':
            return str(int(value))
        
        elif format == 'float':
            if isinstance(value, str):
                value = value.replace('d','e')
            return ('%.10e' % float(value)).replace('e','d')
        
        elif format == 'str':
            return "'%s'" % value
    

        
    def write(self, output_file, template=None):
        """Write the run_card in output_file according to template 
           (a path to a valid run_card)"""
        
        if not template:
            template = output_file
        
        text = ""
        for line in file(template,'r'):
            nline = line.split('#')[0]
            nline = nline.split('!')[0]
            comment = line[len(nline):]
            nline = nline.split('=')
            if len(nline) != 2:
                text += line
            else:
                text += '  %s\t= %s %s' % (self[nline[1].strip()],nline[1], comment)        
        
        fsock = open(output_file,'w')
        fsock.write(text)
        fsock.close()


    def write_include_file(self, output_path):
        """writing the run_card.inc file""" 
        
        self.fsock = file_writers.FortranWriter(output_path)    
################################################################################
#      Writing the lines corresponding to the cuts
################################################################################
    
        self.add_line('maxjetflavor', 'int', 4)
        self.add_line('auto_ptj_mjj', 'bool', True)
        self.add_line('cut_decays', 'bool', True)
        # minimum pt
        self.add_line('ptj', 'float', 20)
        self.add_line('ptb', 'float', 20)
        self.add_line('pta', 'float', 20)
        self.add_line('ptl', 'float', 20)
        self.add_line('misset', 'float', 0)
        self.add_line('ptonium', 'float', 0.0)
        # maximal pt
        self.add_line('ptjmax', 'float', -1)
        self.add_line('ptbmax', 'float', -1)
        self.add_line('ptamax', 'float', -1)
        self.add_line('ptlmax', 'float', -1)
        self.add_line('missetmax', 'float', -1)
        # maximal rapidity (absolute value)
        self.add_line('etaj', 'float', 4.0)
        self.add_line('etab', 'float', 4.0)
        self.add_line('etaa', 'float', 4.0)
        self.add_line('etal', 'float', 4.0)
        # minimal rapidity (absolute value)
        self.add_line('etajmin', 'float', 0.0)
        self.add_line('etabmin', 'float', 0.0)
        self.add_line('etaamin', 'float', 0.0)
        self.add_line('etalmin', 'float', 0.0)
        self.add_line('etaonium', 'float', 100.0)
        # Minimul E's
        self.add_line('ej', 'float', 0.0)
        self.add_line('eb', 'float', 0.0)
        self.add_line('ea', 'float', 0.0)
        self.add_line('el', 'float', 0.0)
        # Maximum E's
        self.add_line('ejmax', 'float', -1)
        self.add_line('ebmax', 'float', -1)
        self.add_line('eamax', 'float', -1)
        self.add_line('elmax', 'float', -1)     
        # minimum delta_r
        self.add_line('drjj', 'float', 0.4)     
        self.add_line('drbb', 'float', 0.4)     
        self.add_line('drll', 'float', 0.4)     
        self.add_line('draa', 'float', 0.4)     
        self.add_line('drbj', 'float', 0.4)     
        self.add_line('draj', 'float', 0.4)     
        self.add_line('drjl', 'float', 0.4)     
        self.add_line('drab', 'float', 0.4)     
        self.add_line('drbl', 'float', 0.4)     
        self.add_line('dral', 'float', 0.4)     
        # maximum delta_r
        self.add_line('drjjmax', 'float', -1)
        self.add_line('drbbmax', 'float', -1)
        self.add_line('drllmax', 'float', -1)
        self.add_line('draamax', 'float', -1)
        self.add_line('drbjmax', 'float', -1)
        self.add_line('drajmax', 'float', -1)
        self.add_line('drjlmax', 'float', -1)
        self.add_line('drabmax', 'float', -1)
        self.add_line('drblmax', 'float', -1)
        self.add_line('dralmax', 'float', -1)
        # minimum invariant mass for pairs
        self.add_line('mmjj', 'float', 0.0)
        self.add_line('mmbb', 'float', 0.0)
        self.add_line('mmaa', 'float', 0.0)
        self.add_line('mmll', 'float', 0.0)
        # maximum invariant mall for pairs
        self.add_line('mmjjmax', 'float', -1)
        self.add_line('mmbbmax', 'float', -1)
        self.add_line('mmaamax', 'float', -1)
        self.add_line('mmllmax', 'float', -1)
        #Min Maxi invariant mass for all leptons 
        self.add_line("mmnl", 'float', 0.0)
        self.add_line("mmnlmax", 'float', -1)
        #inclusive cuts
        self.add_line("xptj", 'float', 0.0)
        self.add_line("xptb", 'float', 0.0)
        self.add_line("xpta", 'float', 0.0)
        self.add_line("xptl", 'float', 0.0)
        self.add_line("xmtcentral", 'float', 0.0, fortran_name='xmtc', log=10)
        # WBT cuts
        self.add_line("xetamin", 'float', 0.0)
        self.add_line("deltaeta", 'float', 0.0)
        # Jet measure cuts 
        self.add_line("xqcut", 'float', 0.0)
        self.add_line("d", 'float', 1.0, log=10)
        # Set min pt of one heavy particle 
        self.add_line("ptheavy", 'float', 0.0)
        # Pt of pairs of leptons (CHARGED AND NEUTRALS)
        self.add_line("ptllmin", "float", 0.0)
        self.add_line("ptllmax", "float", -1)
        # Check   the pt's of the jets sorted by pt
        self.add_line("ptj1min", "float", 0.0)
        self.add_line("ptj1max", "float", -1)
        self.add_line("ptj2min", "float", 0.0)
        self.add_line("ptj2max", "float", -1)
        self.add_line("ptj3min", "float", 0.0)
        self.add_line("ptj3max", "float", -1)
        self.add_line("ptj4min", "float", 0.0)
        self.add_line("ptj4max", "float", -1)
        self.add_line("cutuse", "float", 0.0)
        # Check   the pt's of leptons sorted by pt
        self.add_line("ptl1min", "float", 0.0)
        self.add_line("ptl1max", "float", -1)
        self.add_line("ptl2min", "float", 0.0)
        self.add_line("ptl2max", "float", -1)
        self.add_line("ptl3min", "float", 0.0)
        self.add_line("ptl3max", "float", -1)
        self.add_line("ptl4min", "float", 0.0)
        self.add_line("ptl4max", "float", -1)
        # Check  Ht
        self.add_line("ht2min", 'float', 0.0)
        self.add_line("ht3min", 'float', 0.0)
        self.add_line("ht4min", 'float', 0.0)
        self.add_line("ht2max", 'float', -1)
        self.add_line("ht3max", 'float', -1)
        self.add_line("ht4max", 'float', -1)        
        self.add_line("htjmin", 'float', 0.0)
        self.add_line("htjmax", 'float', -1)        
        self.add_line("ihtmin", 'float', 0.0)
        self.add_line("ihtmax", 'float', -1)

################################################################################
#      Writing the lines corresponding to anything but cuts
################################################################################
        # seed
        self.add_line("gridpack","bool", False)
        self.add_line("gridrun",'bool', False, log=10)
        if str(self['gridrun']) in ['1','T','.true','True'] and \
           str(self['gridpack']) in ['1','T','.true','True']:
            self.add_line('gseed', 'int', 0, fortran_name='iseed')
        else:
            self.add_line('iseed', 'int', 0, fortran_name='iseed')
        # Renormalizrion and factorization scales
        self.add_line('fixed_ren_scale', 'bool', True)
        self.add_line('fixed_fac_scale', 'bool', True)
        self.add_line('scale', 'float', 'float', 91.188)
        self.add_line('dsqrt_q2fact1','float', 91.188, fortran_name='sf1')
        self.add_line('dsqrt_q2fact2', 'float', 91.188, fortran_name='sf2')
        self.add_line('scalefact', 'float', 1.0)
        self.add_line('fixed_couplings', 'bool', True, log=10)
        self.add_line('ickkw', 'int', 0)
        self.add_line('chcluster', 'bool', False)
        self.add_line('ktscheme', 'int', 1)
        self.add_line('asrwgtflavor', 'int', 5)
        if int(self['ickkw'])>0:
            self.add_line('alpsfact', 'float', 1.0)
            self.add_line('pdfwgt', 'bool', True)
        if int(self['ickkw'])==2:
            self.add_line('highestmult','int', 0, fortran_name='nhmult')
            self.add_line('issgridfile','str','issudgrid.dat')
        # Collider energy and type
        self.add_line('lpp1', 'int', 1, fortran_name='lpp(1)')
        self.add_line('lpp2', 'int', 1, fortran_name='lpp(2)')
        self.add_line('ebeam1', 'float', 7000, fortran_name='ebeam(1)')
        self.add_line('ebeam2', 'float', 7000, fortran_name='ebeam(2)')
        # Beam polarization
        self.add_line('polbeam1', 'float', 0.0, fortran_name='pb1')
        self.add_line('polbeam2', 'float', 0.0, fortran_name='pb2')
        # BW cutoff (M+/-bwcutoff*Gamma)
        self.add_line('bwcutoff', 'float', 15.0)
        #  Collider pdf
        self.add_line('pdlabel','str','cteq6l1')
        if self['pdlabel'] == 'lhapdf':
            self.add_line('lhaid', 'int', 10042)
        else:
            self.add_line('lhaid', 'int', 10042, log=10)
        
        self.fsock.close()



        
    def add_line(self, card_name, type, default, log=30, fortran_name=None):
        """get the line for the .inc file""" 
         
        value = self.get_default(card_name, default, log)
        if not fortran_name:
            fortran_name = card_name
        self.fsock.writelines(' %s = %s \n' % (fortran_name, self.format(type, value)))


class RunCardNLO(RunCard):
    """A class object for the run_card for a (aMC@)NLO pocess"""

        
    def write_include_file(self, output_path):
        """writing the run_card.inc file""" 
        
        self.fsock = file_writers.FortranWriter(output_path)    
################################################################################
#      Writing the lines corresponding to the cuts
################################################################################
    
        self.add_line('maxjetflavor', 'int', 5)
        # minimum pt
        self.add_line('ptj', 'float', 20)
        self.add_line('etaj', 'float', -1.0)
        self.add_line('ptl', 'float', 20)
        self.add_line('etal', 'float', -1.0)
        # minimum delta_r
        self.add_line('drll', 'float', 0.4)     
        # minimum invariant mass for pairs
        self.add_line('mll', 'float', 0.0)
        #inclusive cuts
        # Jet measure cuts 
        self.add_line("jetradius", 'float', 0.7, log=10)

################################################################################
#      Writing the lines corresponding to anything but cuts
################################################################################
        # seed
        self.add_line('iseed', 'int', 0)
        self.add_line('parton_shower', 'str', 'HERWIG6', fortran_name='shower_mc')
        self.add_line('nevents', 'int', 10000)
        self.add_line('event_norm', 'str', 'average', fortran_name='event_norm')
        # Renormalizrion and factorization scales
        self.add_line('fixed_ren_scale', 'bool', True)
        self.add_line('fixed_fac_scale', 'bool', True)
        self.add_line('fixed_QES_scale', 'bool', True)
        self.add_line('muR_ref_fixed', 'float', 91.188)
        self.add_line('muF1_ref_fixed','float', 91.188)
        self.add_line('muF2_ref_fixed', 'float', 91.188)
        self.add_line('QES_ref_fixed', 'float', 91.188)
        self.add_line('muR_over_ref', 'float', 1.0)
        self.add_line('muF1_over_ref', 'float', 1.0)
        self.add_line('muF2_over_ref', 'float', 1.0)
        self.add_line('QES_over_ref', 'float', 1.0)
        #reweight block
        self.add_line('reweight_scale', 'bool', True, fortran_name='do_rwgt_scale')
        self.add_line('rw_Rscale_up', 'float', 2.0)
        self.add_line('rw_Rscale_down', 'float', 0.5)
        self.add_line('rw_Fscale_up', 'float', 2.0)
        self.add_line('rw_Fscale_down', 'float', 0.5)
        self.add_line('reweight_PDF', 'bool', True, fortran_name='do_rwgt_pdf')
        self.add_line('PDF_set_min', 'int', 21101)
        self.add_line('PDF_set_max', 'int', 21140)

       # self.add_line('fixed_couplings', 'bool', True, log=10)
        self.add_line('jetalgo', 'int', 1)
        # Collider energy and type
        self.add_line('lpp1', 'int', 1, fortran_name='lpp(1)')
        self.add_line('lpp2', 'int', 1, fortran_name='lpp(2)')
        self.add_line('ebeam1', 'float', 4000, fortran_name='ebeam(1)')
        self.add_line('ebeam2', 'float', 4000, fortran_name='ebeam(2)')
        # BW cutoff (M+/-bwcutoff*Gamma)
        self.add_line('bwcutoff', 'float', 15.0)
        #  Collider pdf
        self.add_line('pdlabel','str','cteq6_m')
        if self['pdlabel'] == 'lhapdf':
            self.add_line('lhaid', 'int', 10042)
        else:
            self.add_line('lhaid', 'int', 10042, log=10)
        
        self.fsock.close()

class ProcCard(list):
    """Basic Proccard object"""
    
    def __init__(self, init=None):
        """ initialize a basic proc_card"""
        self.info = {'model': 'sm', 'generate':None,
                     'full_model_line':'import model sm'}
        list.__init__(self)
        if init:
            self.read(init)

            
    def read(self, init):
        """read the proc_card and save the information"""
        
        if isinstance(init, str): #path to file
            init = file(init, 'r')
        
        for line in init:
            self.append(line)
            
    def move_to_last(self, cmd):
        """move an element to the last history."""
        for line in self[:]:
            if line.startswith(cmd):
                self.remove(line)
                list.append(self, line)
    
    def append(self, line):
        """"add a line in the proc_card perform automatically cleaning"""
        
        line = line.strip()
        cmds = line.split()
        if len(cmds) == 0:
            return
        
        list.append(self, line)
        
        # command type:
        cmd = cmds[0]
        
        if cmd == 'output':
            # Remove previous outputs from history
            self.clean(allow_for_removal = ['output'], keep_switch=True,
                           remove_bef_last='output')
        elif cmd == 'generate':
            # Remove previous generations from history
            self.clean(remove_bef_last='generate', keep_switch=True,
                     allow_for_removal= ['generate', 'add process', 'output'])
            self.info['generate'] = ' '.join(cmds[1:])
        elif cmd == 'import':
            if len(cmds) < 2:
                return
            if cmds[1].startswith('model'):
                self.info['full_model_line'] = line
                self.clean(remove_bef_last='import', keep_switch=True,
                        allow_for_removal=['generate', 'add process', 'output'])
                if cmds[1] == 'model':
                    self.info['model'] = cmds[2]
                else:
                    self.info['model'] = None # not UFO model
            elif cmds[1] == 'proc_v4':
                #full cleaning
                self[:] = []
                

    def clean(self, to_keep=['set','add','load'],
                            remove_bef_last=None,
                            to_remove=['open','display','launch', 'check','history'],
                            allow_for_removal=None,
                            keep_switch=False):
        """Remove command in arguments from history.
        All command before the last occurrence of  'remove_bef_last'
        (including it) will be removed (but if another options tells the opposite).                
        'to_keep' is a set of line to always keep.
        'to_remove' is a set of line to always remove (don't care about remove_bef_ 
        status but keep_switch acts.).
        if 'allow_for_removal' is define only the command in that list can be 
        remove of the history for older command that remove_bef_lb1. all parameter
        present in to_remove are always remove even if they are not part of this 
        list.
        keep_switch force to keep the statement remove_bef_??? which changes starts
        the removal mode.
        """
        
        #check consistency
        if __debug__ and allow_for_removal:
            for arg in to_keep:
                assert arg not in allow_for_removal
            
    
        nline = -1
        removal = False
        #looping backward
        while nline > -len(self):
            switch  = False # set in True when removal pass in True

            #check if we need to pass in removal mode
            if not removal and remove_bef_last:
                    if self[nline].startswith(remove_bef_last):
                        removal = True
                        switch = True  

            # if this is the switch and is protected pass to the next element
            if switch and keep_switch:
                nline -= 1
                continue

            # remove command in to_remove (whatever the status of removal)
            if any([self[nline].startswith(arg) for arg in to_remove]):
                self.pop(nline)
                continue
            
            # Only if removal mode is active!
            if removal:
                if allow_for_removal:
                    # Only a subset of command can be removed
                    if any([self[nline].startswith(arg) 
                                                 for arg in allow_for_removal]):
                        self.pop(nline)
                        continue
                elif not any([self[nline].startswith(arg) for arg in to_keep]):
                    # All command have to be remove but protected
                    self.pop(nline)
                    continue
            
            # update the counter to pass to the next element
            nline -= 1
        
        def __getattr__(self, tag):
            if isinstance(tag, int):
                list.__getattr__(self, tag)
            else:
                return self.info[tag]