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