python script within Madgraph

Asked by chahrazed

Hi,

I am trying to run a python script which includes commands to generate events processes but I have message errors .

Please can you tell me how to execute the script on Madgraph (the right command on Mac terminal) and where I should put the script , inside the bin directory ?

Thanks

Question information

Language:
English Edit question
Status:
Answered
For:
MadGraph5_aMC@NLO Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Olivier Mattelaer (olivier-mattelaer) said :
#1

Did you check the FAQ?
For where you have to put the file, it really does not matter where you put it.
I would avoid to put it in the bin directory but it should work if you do.

Cheers,

Olivier
FAQ #2186: “How to script MG5 run?”.

Revision history for this message
chahrazed (chahra-guella) said :
#2

Thanks four your answer.

Yes I have seen FAQ #2186 and it is ok how to run it , but I would like to work with a python code to generate multiple processes , a part of the code is written below.

It is correct to run the code with this command python ./mg5_aMc code.py ??

import random
import os
import sys
# General settings
nevents=100
seed=random.randint(1,100) #note that banner.py in madgraph/various needs to be modified to be able to handle this.
beamenergy = 13000.

ickkw=1

nJetMax=4

segfault = False
# default/average value
xqcut=20

pythiaprocess = ""
#metmin
metmin=0.
possibilities=["2jets","3jets","4jets","njets","z_jets","znuall_jets","w_jets"]
event_number = str(sys.argv[1])
if event_number not in possibilities:
 print "Wrong event indicated!:"+event_number
 print "choose one from: "
 print possibilities
 print "for SUSY processes, use susycommands.py"
 exit()

# QCD DOES NOT WORK
if event_number == "njets":
    mgproc="""generate p p > j j @0
add process p p > j j j @1
add process p p > j j j j @2
"""
    name='njets'
    nJetMax=2
# z > nubar nu + jets
if event_number == "znuall_jets":
    mgproc="""generate p p > nuall nuall @0
add process p p > nuall nuall j @1
add process p p > nuall nuall j j @2
"""
    name='znuall_jets'
    nJetMax=2
    xqcut = 10

# z + jets
if event_number == "z_jets":
    mgproc="""generate p p > z @0
add process p p > z j @1
add process p p > z j j @2
"""
    name='z_jets'
    nJetMax=2
    xqcut = 10

if event_number == "w_jets":
    mgproc="""generate p p > w @0
add process p p > w j @1
add process p p > w j j @2
"""
    name='w_jets'
    nJetMax=2
    xqcut = 10

if event_number == "Wene_jets":
    mgproc="""generate p p > eall veall @0
add process p p > eall veall j @1
add process p p > eall veall j j @2
"""
    name='Wene_jets'
    pythiaprocess='pp>LEPTONS,NEUTRINOS'
    nJetMax=2
    xqcut = 10

# W > mu nu + jets
if event_number == "Wmune_jets":
    mgproc="""
generate p p > muall vmall @0
add process p p > muall vmall j @1
add process p p > muall vmall j j @2
"""
    name='Wmune_jets'
    pythiaprocess='pp>LEPTONS,NEUTRINOS'
    nJetMax=2
    xqcut = 10

.
.
.
.
.fcard = open('generate_'+event_number+'.dat','w')
fcard.write("""define eall = e+ e-
define veall = ve ve~
define muall = mu+ mu-
define vmall = vm vm~
define taall = ta+ ta-
define vtall = vt vt~
define nuall = ve ve~ vm vm~ vt vt~
define w = w+ w-
define tall = t t~
define ball = b b~
define p = g u c d s b u~ c~ d~ s~ b~
define j = g u c d s b u~ c~ d~ s~ b~
"""+mgproc+"""
output """+name)
fcard.close()
fcard = open('launch_'+event_number+'.dat','w')
fcard.write("""launch """+name+"""
shower=Pythia8
detector=Delphes
analysis=off
0
run_card_modified_"""+event_number+""".dat
pythia8_card_"""+event_number+""".dat
delphes_card_ATLAS.dat
""")

fcard.close()
if(segfault == True):
 ickkw = 0
fcard1 = open('run_card_modified.dat')
fcard = open('run_card_modified_'+event_number+'.dat','w')
for line in fcard1:
 if "nevents ! Number of unweighted" in line:
  fcard.write(" "+str(nevents)+" = nevents ! Number of unweighted events requested \n")
 elif "iseed ! rnd seed (0=assigned automatically=default))" in line:
  fcard.write(" "+str(seed)+" = iseed ! rnd seed (0=assigned automatically=default)) \n")
 elif "ebeam1 ! beam 1 total energy in GeV" in line:
  fcard.write(" "+str(beamenergy/2.)+" = ebeam1 ! beam 1 total energy in GeV \n")
 elif "ebeam2 ! beam 2 total energy in GeV" in line:
  fcard.write(" "+str(beamenergy/2.)+" = ebeam2 ! beam 2 total energy in GeV \n")
 elif "ickkw ! 0 no matching, 1 MLM" in line:
  fcard.write(" "+str(ickkw)+" = ickkw ! 0 no matching, 1 MLM \n")
 elif "xqcut ! minimum kt jet measure between partons" in line:
  fcard.write(" "+str(xqcut)+" = xqcut ! minimum kt jet measure between partons\n")
 elif "! minimum missing Et (sum of neutrino's momenta)" in line:
  fcard.write(" "+str(metmin)+" = misset ! minimum missing Et (sum of neutrino's momenta)\n")
 else:
  fcard.write(line)
fcard.close()
fcard1.close()

fcard1 = open('pythia8_card.dat')
fcard = open('pythia8_card_'+event_number+'.dat','w')
for line in fcard1:
 if "Main:numberOfEvents" in line:
  fcard.write("Main:numberOfEvents = "+str(nevents)+"\n")
 elif "JetMatching:nJetMax" in line: #needed for the matching, safe to set it
  fcard.write("JetMatching:nJetMax = "+str(nJetMax)+"\n")
 elif "partonlevel:mpi = off" in line: #MPI
  fcard.write("!partonlevel:mpi = off\n")
 else:
  fcard.write(line)
fcard.close()
fcard1.close()

# check if the directory already exists
if (os.path.isdir(event_number)==False):
 os.system("./mg5_aMC < generate_"+event_number+".dat")
# launch madgraph, do showering, do detector simulation
os.system("./mg5_aMC < launch_"+event_number+".dat")

Revision history for this message
Olivier Mattelaer (olivier-mattelaer) said :
#3

Hi,

No this is not the way that madgraph handles command line.

If you are the author of the script you should know how to use it.

If you are not, I would suggest to not use such code since you will likely use it as a back box and therefore the probability of a mistake is quite high. It would be better to just write a couple of command file following the above FAQ.
and in particular use the "set" command part of madgraph to edit the various file.
This will make those code more readable and therefore more easy to pass to other users.

Cheers,

Olivier

Revision history for this message
chahrazed (chahra-guella) said :
#4

No I am not the author of this code , I just wanted to write a similar code including processes I need .
Thank you for your propositions.

Revision history for this message
Stanley Mercado (morstraut) said :
#5

A Python script can be run within MadGraph to perform various tasks, such as manipulating the output files or automating certain processes. To do so, you first need to launch MadGraph in interactive mode by typing ./bin/mg5_aMC in your terminal from the directory where MadGraph is installed. https://freeidmcracked.com/idm-crack-download

Once you are in the MadGraph prompt, you can use the python command to start a Python interpreter session. From there, you can either type in your Python commands directly or load a Python script file using the execfile() function.

For example, suppose you have a Python script file named my_script.py located in the current working directory. To load and run the script, you can type in the following command in the MadGraph prompt:

Can you help with this problem?

Provide an answer of your own, or ask chahrazed for more information if necessary.

To post a message you must log in.