Belle II Software  release-05-01-25
MadGraph_sm.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 
12 
13 from basf2 import *
14 from beamparameters import add_beamparameters
15 import os
16 import subprocess
17 
18 # parameters that can be modified
19 mg_nevents = '100'
20 mg_beamenergy = '10.355/2.'
21 mg_generate = 'e+ e- > mu+ mu-'
22 
23 # full path to steering template file (full path to model must be inside the template steering file)
24 mg_steeringtemplate = \
25  os.path.expandvars('$BELLE2_LOCAL_DIR/generators/madgraph/examples/run_sm.steeringtemplate'
26  )
27 
28 # full path to output directory
29 mg_outputdir = \
30  os.path.expandvars('$BELLE2_LOCAL_DIR/generators/madgraph/output/sm_mumu')
31 
32 # -------------------------------------------------------
33 # no user input needed below this line
34 
35 # full path to MadGraph externals
36 mg_externals = 'mg5_aMC'
37 
38 # full path to steering file (generated automatically, will be overwritten (if existing) or created)
39 mg_steeringfile = \
40  os.path.expandvars('$BELLE2_LOCAL_DIR/generators/madgraph/examples/run_sm.steering'
41  )
42 
43 # full path to run card (param_card is generated automatically, defaults are overwritten in the steering file)
44 mg_runcard = \
45  os.path.expandvars('$BELLE2_LOCAL_DIR/generators/madgraph/cards/run_card.dat'
46  )
47 
48 # Replace the output directory and the run card
49 mydict = {}
50 mydict['MGOUTPUT'] = mg_outputdir
51 mydict['MGRUNDCARD'] = mg_runcard
52 mydict['MGNEVENTS'] = mg_nevents
53 mydict['MGBEAMENERGY'] = mg_beamenergy
54 mydict['MGGENERATE'] = mg_generate
55 
56 fp1 = open(mg_steeringfile, 'w')
57 fp2 = open(mg_steeringtemplate, 'r')
58 data = fp2.read()
59 fp2.close()
60 for (key, value) in mydict.items():
61  data = data.replace(key, value)
62 fp1.write(data)
63 fp1.close()
64 
65 # run MadGraph
66 subprocess.check_call([mg_externals, mg_steeringfile])
67 
68 # gunzip the unweighted output file
69 subprocess.check_call(['gunzip', mg_outputdir + '/Events/run_01/unweighted_events.lhe.gz'])
70 
71 # read in via basf2
72 set_log_level(LogLevel.ERROR)
73 lhereader = register_module('LHEInput')
74 lhereader.param('makeMaster', True)
75 lhereader.param('runNum', 1)
76 lhereader.param('expNum', 1)
77 lhereader.param('inputFileList', [mg_outputdir + '/Events/run_01/unweighted_events.lhe'])
78 lhereader.param('useWeights', False)
79 lhereader.param('nInitialParticles', 2)
80 lhereader.param('nVirtualParticles', 0)
81 lhereader.param('boost2Lab', True)
82 lhereader.param('wrongSignPz', True)
83 
84 # creating the path for the processing
85 set_log_level(LogLevel.ERROR)
86 
87 # creating the path for the processing
88 main = create_path()
89 
90 # beam parameters
91 beamparameters = add_beamparameters(main, "Y3S")
92 
93 # Add lhereader module
94 main.add_module(lhereader)
95 print_params(lhereader)
96 
97 # Add progress module
98 progress = register_module('Progress')
99 progress.set_log_level(LogLevel.INFO)
100 main.add_module(progress)
101 
102 # Add rootoutput module
103 rootoutput = register_module('RootOutput')
104 rootoutput.param('outputFileName', 'LHEReaderMasterOutputSM.root')
105 main.add_module(rootoutput)
106 
107 # Add mcparticleprinter module
108 main.add_module('PrintMCParticles', logLevel=LogLevel.DEBUG,
109  onlyPrimaries=False)
110 
111 # Process
112 process(main)