~maddevelopers/mg5amcnlo/aMCatNLO

« back to all changes in this revision

Viewing changes to madgraph/interface/madgraph_interface.py

  • Committer: Valentin Hirschi
  • Date: 2012-12-18 09:10:58 UTC
  • mfrom: (550.1.11 aMCatNLO)
  • Revision ID: spooner@pb-d-128-141-163-97.cern.ch-20121218091058-x950ogffu0odfard
1. Merged with latest revision of launchpad and made the MadLoop command 
   test safer.

Show diffs side-by-side

added added

removed removed

Lines of Context:
205
205
        "************************************************************")
206
206
        
207
207
        cmd.Cmd.__init__(self, *arg, **opt)
 
208
        
 
209
        self.history = banner_module.ProcCard()
208
210
    
209
211
    def postcmd(self,stop, line):
210
212
        """ finishing a command
2088
2090
                   'gauge','lorentz', 'brs']
2089
2091
    _import_formats = ['model_v4', 'model', 'proc_v4', 'command', 'banner']
2090
2092
    _install_opts = ['pythia-pgs', 'Delphes', 'MadAnalysis', 'ExRootAnalysis', 'MCatNLO-utilities','update']
2091
 
    _v4_export_formats = ['madevent', 'standalone', 'matrix'] 
 
2093
    _v4_export_formats = ['madevent', 'standalone', 'standalone_ms', 'matrix'] 
2092
2094
    _export_formats = _v4_export_formats + ['standalone_cpp', 'pythia8', 'aloha']
2093
2095
    _set_options = ['group_subprocesses',
2094
2096
                    'ignore_six_quark_processes',
2199
2201
        # interfaces
2200
2202
        # Clear history, amplitudes and matrix elements when a model is imported
2201
2203
        # Remove previous imports, generations and outputs from history
2202
 
        self.clean_history(remove_bef_last='import')
 
2204
        self.history.clean(remove_bef_last='import')
2203
2205
        # Reset amplitudes and matrix elements
2204
2206
        self._done_export=False
2205
2207
        self._curr_amps = diagram_generation.AmplitudeList()
2206
2208
        self._curr_matrix_elements = helas_objects.HelasMultiProcess()    
2207
2209
 
2208
 
        self._v4_export_formats = ['madevent', 'standalone', 'matrix'] 
 
2210
        self._v4_export_formats = ['madevent', 'standalone','standalone_ms', 'matrix'] 
2209
2211
        self._export_formats = self._v4_export_formats + ['standalone_cpp', 'pythia8']
2210
2212
        self._nlo_modes_for_completion = ['all','virt','real']
2211
2213
    
2770
2772
                   logging.getLogger('cmdprint'),
2771
2773
                   logging.getLogger('madgraph.model'),
2772
2774
                   logging.getLogger('madgraph.base_objects')]
2773
 
        old_levels = [logger.getEffectiveLevel() for logger in loggers]
 
2775
        old_levels = [logger.level for logger in loggers]
2774
2776
        for logger in loggers:
2775
2777
            logger.setLevel(logging.WARNING)
2776
2778
        
2966
2968
        # Also reset _export_format and _export_dir
2967
2969
        self._export_format = None
2968
2970
 
2969
 
        # Remove previous generations from history
2970
 
        self.clean_history(remove_bef_last='generate', keep_switch=True,
2971
 
                     allow_for_removal= ['generate', 'add process', 'output'])
2972
 
 
2973
2971
 
2974
2972
        # Call add process
2975
2973
        args = self.split_arg(line)
3068
3066
        # Now check for forbidden particles, specified using "/"
3069
3067
        slash = line.find("/")
3070
3068
        dollar = line.find("$")
3071
 
        dollar = line.find("$")
3072
3069
        forbidden_particles = ""
3073
3070
        if slash > 0:
3074
3071
            if dollar > slash:
3196
3193
                              'has_born':HasBorn,
3197
3194
                              'NLO_mode':LoopOption
3198
3195
                              })
3199
 
      #                       'is_decay_chain': decay_process\
 
3196
        #                       'is_decay_chain': decay_process\
 
3197
 
 
3198
    @staticmethod
 
3199
    def split_process_line(procline):
 
3200
        """Takes a valid process and return
 
3201
           a tuple (core_process, options). This removes 
 
3202
             - any NLO specifications.
 
3203
             - any options
 
3204
           [Used by MadSpin]
 
3205
        """
 
3206
         
 
3207
        # remove the tag "[*]": this tag is used in aMC@LNO , 
 
3208
        # but it is not a valid syntax for LO
 
3209
        line=procline
 
3210
        pos1=line.find("[")
 
3211
        if pos1>0:
 
3212
            pos2=line.find("]")
 
3213
            if pos2 >pos1:
 
3214
                line=line[:pos1]+line[pos2+1:]
 
3215
        #
 
3216
        # Extract the options:
 
3217
        #
 
3218
        # A. Remove process number (identified by "@")
 
3219
        proc_number_pattern = re.compile("^(.+)@\s*(\d+)\s*(.*)$")
 
3220
        proc_number_re = proc_number_pattern.match(line)
 
3221
        if proc_number_re:
 
3222
            line = proc_number_re.group(1) + proc_number_re.group(3)
 
3223
 
 
3224
        # B. search for the beginning of the option string
 
3225
        pos=1000
 
3226
        # start with order
 
3227
        order_pattern = re.compile("^(.+)\s+(\w+)\s*=\s*(\d+)\s*$")
 
3228
        order_re = order_pattern.match(line)
 
3229
        if (order_re):
 
3230
            pos_order=line.find(order_re.group(2))
 
3231
            if pos_order>0 and pos_order < pos : pos=pos_order
 
3232
 
 
3233
        # then look for slash or dollar
 
3234
        slash = line.find("/")
 
3235
        if slash > 0 and slash < pos: pos=slash
 
3236
        dollar = line.find("$")
 
3237
        if dollar > 0 and dollar < pos: pos=dollar
 
3238
 
 
3239
        if pos<1000:
 
3240
            proc_option=line[pos:]
 
3241
            line=line[:pos]
 
3242
        else:
 
3243
            proc_option=""
 
3244
 
 
3245
        return line, proc_option
 
3246
 
 
3247
    def get_final_part(self, procline):
 
3248
        """Takes a valid process and return
 
3249
           a set of id of final states particles. [Used by MadSpin]
 
3250
        """
 
3251
                
 
3252
        if self._use_lower_part_names:
 
3253
            procline = procline.lower()
 
3254
        pids = self._curr_model.get('name2pdg')
 
3255
            
 
3256
        # method.
 
3257
        # 1) look for decay.
 
3258
        #     in presence of decay call this routine recursively and veto 
 
3259
        #     the particles which are decayed
 
3260
        
 
3261
        # Deal with decay chain
 
3262
        if ',' in procline:
 
3263
            core, decay = procline.split(',', 1)
 
3264
            core_final = self.get_final_part(core)
 
3265
            
 
3266
            #split the decay 
 
3267
            all_decays = decay.split(',')
 
3268
            nb_level,  tmp_decay = 0, ''
 
3269
            decays = []
 
3270
            # deal with ()
 
3271
            for one_decay in all_decays:
 
3272
                if '(' in one_decay:
 
3273
                    nb_level += 1
 
3274
                if ')' in one_decay:
 
3275
                    nb_level -= 1
 
3276
                    
 
3277
                if nb_level:
 
3278
                    if tmp_decay:
 
3279
                        tmp_decay += ', %s' % one_decay
 
3280
                    else: 
 
3281
                        tmp_decay = one_decay
 
3282
                elif tmp_decay:
 
3283
                    final = '%s,%s' % (tmp_decay, one_decay)
 
3284
                    final = final.strip()
 
3285
                    assert final[0] == '(' and final[-1] == ')'
 
3286
                    final = final[1:-1]
 
3287
                    decays.append(final)
 
3288
                    tmp_decay = ''
 
3289
                else:
 
3290
                    decays.append(one_decay)
 
3291
            # remove from the final states all particles which are decayed
 
3292
            for one_decay in decays:
 
3293
                first = one_decay.split('>',1)[0].strip()
 
3294
                if first in pids:
 
3295
                    pid = set([pids[first]])
 
3296
                elif first in self._multiparticles:
 
3297
                    pid = set(self._multiparticles[first])
 
3298
                else:
 
3299
                    raise Exception, 'invalid particle name: %s. ' % first
 
3300
                core_final.difference_update(pid)
 
3301
                core_final.update(self.get_final_part(one_decay))
 
3302
                
 
3303
            return core_final
 
3304
                
 
3305
        # NO DECAY CHAIN
 
3306
        final = set()
 
3307
        final_states = re.search(r'> ([^\/\$\=\@>]*)(\s\S+\=|\$|\/|\@|$)', procline)
 
3308
        particles = final_states.groups()[0]
 
3309
        for particle in particles.split():
 
3310
            if particle in pids:
 
3311
                final.add(pids[particle])
 
3312
            elif particle in self._multiparticles:
 
3313
                final.update(set(self._multiparticles[particle]))
 
3314
        return final
 
3315
            
 
3316
            
 
3317
            
 
3318
        
 
3319
        
 
3320
        
 
3321
        
 
3322
        
 
3323
 
3200
3324
 
3201
3325
    def extract_particle_ids(self, args):
3202
3326
        """Extract particle ids from a list of particle names. If
3348
3472
    # Import files
3349
3473
    def do_import(self, line, force=False):
3350
3474
        """Main commands: Import files with external formats"""
 
3475
        
3351
3476
        args = self.split_arg(line)
3352
3477
        # Check argument's validity
3353
3478
        self.check_import(args)
3354
 
        
3355
3479
        if args[0].startswith('model'):
3356
3480
            self._model_v4_path = None
3357
 
            # Clear history, amplitudes and matrix elements when a model is imported
3358
 
            # Remove previous imports, generations and outputs from history
3359
 
            self.clean_history(remove_bef_last='import', keep_switch=True,
3360
 
                        allow_for_removal=['generate', 'add process', 'output'])
3361
3481
            # Reset amplitudes and matrix elements
3362
3482
            self._curr_amps = diagram_generation.AmplitudeList()
3363
3483
            self._curr_matrix_elements = helas_objects.HelasMultiProcess()
3462
3582
                self.exec_cmd('launch')
3463
3583
            
3464
3584
        elif args[0] == 'proc_v4':
3465
 
            self.history = []
3466
3585
 
3467
3586
            if len(args) == 1 and self._export_dir:
3468
3587
                proc_card = pjoin(self._export_dir, 'Cards', \
3613
3732
        # Load file with path of the different program:
3614
3733
        import urllib
3615
3734
        path = {}
 
3735
        
 
3736
        if args[0] == 'MCatNLO-utilities':
 
3737
            logger.warning('For internal testings, please use the MCatNLO-utilities provided in this distribution. Remember to update them when releasing beta2.MZ')
 
3738
            return
3616
3739
 
3617
3740
        name = {'td_mac': 'td', 'td_linux':'td', 'Delphes':'Delphes', 
3618
3741
                'pythia-pgs':'pythia-pgs', 'ExRootAnalysis': 'ExRootAnalysis',
4556
4679
        # Check Argument validity
4557
4680
        self.check_output(args)
4558
4681
 
4559
 
        # Remove previous outputs from history
4560
 
        self.clean_history(allow_for_removal = ['output'], keep_switch=True,
4561
 
                           remove_bef_last='output')
4562
4682
        
4563
4683
        noclean = '-noclean' in args
4564
4684
        force = '-f' in args 
4619
4739
                shutil.rmtree(self._export_dir)
4620
4740
 
4621
4741
        # Make a Template Copy
4622
 
        if self._export_format in ['madevent', 'standalone', 'matrix']:
 
4742
        if self._export_format in ['madevent', 'standalone','standalone_ms', 'matrix']:
4623
4743
            self._curr_exporter = export_v4.ExportV4Factory(self, noclean)
4624
4744
        elif self._export_format == 'standalone_cpp':
4625
4745
            export_cpp.setup_cpp_standalone_dir(self._export_dir, self._curr_model)
4626
4746
        elif not os.path.isdir(self._export_dir):
4627
4747
            os.makedirs(self._export_dir)
4628
4748
 
4629
 
        if self._export_format in ['madevent', 'standalone']:
 
4749
        if self._export_format in ['madevent', 'standalone', 'standalone_ms']:
4630
4750
            self._curr_exporter.copy_v4template(modelname=self._curr_model.get('name'))
4631
4751
 
4632
4752
        # Reset _done_export, since we have new directory
4723
4843
        calls = 0
4724
4844
 
4725
4845
        path = self._export_dir
4726
 
        if self._export_format in ['standalone_cpp', 'madevent', 'standalone']:
 
4846
        if self._export_format in ['standalone_cpp', 'madevent', 'standalone', 'standalone_ms']:
4727
4847
            path = pjoin(path, 'SubProcesses')
4728
4848
            
4729
4849
        cpu_time1 = time.time()
4757
4877
                try:
4758
4878
                    cmd.Cmd.onecmd(self, 'history .')
4759
4879
                except Exception:
4760
 
                    misc.sprint('command history fails.')
 
4880
                    misc.sprint('command history fails.', 10)
4761
4881
                    pass
4762
4882
                
4763
4883
        # Pythia 8
4794
4914
                        self._curr_matrix_elements.get_matrix_elements()
4795
4915
 
4796
4916
        # Fortran MadGraph Standalone
4797
 
        if self._export_format == 'standalone':
 
4917
        if self._export_format in ['standalone', 'standalone_ms']:
4798
4918
            for me in matrix_elements:
4799
4919
                calls = calls + \
4800
4920
                        self._curr_exporter.generate_subprocess_directory_v4(\
4855
4975
        """Make the html output, write proc_card_mg5.dat and create
4856
4976
        madevent.tar.gz for a MadEvent directory"""
4857
4977
        
4858
 
        if self._export_format in ['madevent', 'standalone', 'NLO']:
 
4978
        if self._export_format in ['madevent', 'standalone', 'standalone_ms', 'NLO']:
4859
4979
            # For v4 models, copy the model/HELAS information.
4860
4980
            if self._model_v4_path:
4861
4981
                logger.info('Copy %s model files to directory %s' % \
4964
5084
            self.do_save('options %s' % filename.replace(' ', '\ '), check=False, 
4965
5085
                         to_keep={'mg5_path':MG5DIR})
4966
5086
 
4967
 
        if self._export_format in ['madevent', 'standalone']:
 
5087
        if self._export_format in ['madevent', 'standalone', 'standalone_ms']:
4968
5088
            
4969
5089
            self._curr_exporter.finalize_v4_directory( \
4970
5090
                                           self._curr_matrix_elements,