Import madgraph command prompt module in python

Asked by David Englert

Dear MadGraph Developers,

I would like to perform a parameter scan with madgraph to have a cross section map, but the usual solutions addressing this assume that by changing a single parameter the other ones don't need to be recomputed, or can be recomputed internally by madwidth (for Gamma's/BR's). In my case I'd need to replace the complete parameter card with a new one when changing parameters. I could do this by generating a really long madevent script, but this solution feels really cumbersome. Instead I started building a python wrapper from which I can call madgraph routines, delete the temporary output, save the cross section and move on to the next param. point.
I gained access to the madevent prompt via:

sys.path.append(os.path.join(workdir,'bin','internal'))
import madevent_interface as ME
launch = ME.MadEventCmd(me_dir=directory)
launch.run_cmd('generate_events')

With this I am able to run the commands available in madevent.

I have a couple of questions inspired by this:
- At this stage this solution is equivalent to just calling generate_events -f from a shell script. I am wondering whether it is possible to access the generate_events prompt in a similar way to issue commands like 'delphes=ON', 'set param val', etc.
- What's the easiest way to extract the cross section after each run? Extracting the float string from the crossx.html seems a bit ugly, is the cross section stored elsewhere maybe?
- Is there any kind of technical documentation for the madgraph prompt by any chance?

Thanks for the help.

Cheers,
David

Question information

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

Hi,

> I would like to perform a parameter scan with madgraph to have a cross section map, but the usual solutions addressing this assume that by changing a single parameter the other ones don’t need to be recomputed, or can be recomputed internally by madwidth (for Gamma's/BR's).

Note that internal parameter of the model are automatically recomputed. So one solution is actually to modify your model to automatically have those relations.

A different method to do the same, would be to modify the functions:
common_run_interface.AskforEditCard.update_dependent
in order to compute the value that need to be computed at that stage.

> - At this stage this solution is equivalent to just calling generate_events -f from a shell script. I am wondering whether it is possible to access the generate_events prompt in a similar way to issue commands like ‘delphes=ON', 'set param val', etc.

Those interfaces are designed as questions and not as user-interfaces.
Actually the two questions are handled in a quite different way:

- For the first question (what type of run), this is really just a question which is asked in a infinite loop.
  the infinite loop and the handling of the answer is done in the function ask_run_configuration of MadEventCmd
- For the second question, you have in that case a real class handling the function
AskforEditCard which is defined in common_run_interface

I have never try to instantiate that class outside of a question framework. So If you try it I would expect that you might face some issue.
My suggestion, would rather to not use those method/class

1) for the “type of run”, everything is actually control by the presence/absence of the card in the Cards directory.
setting “delphes=ON” is basically the same as copy the delphes_card_default.dat card to the delphes_card.dat
putting it on OFF is removing delphes_card.dat.
So you do not really need that interface for scripting

2) for the “edition of the card”. The best is to create a python object of the card.
For the param_card this can be done like:
import model.check_param_card as param_card_mod
param_card = param_card_mod.ParamCard(path)
then to modify an entry of such object you can do
param_card[block].param_dict[lhaid].value = value
where “block” is the name of the block (e.g. ‘mass’)
and lhaid is a tuple with the numbering i.e. for the top it is “(6,)”
for some entry (like mixing matrix) it can have more than one number like “(1,3)”
then you can write the new param_card:
param_card.write(PATH)

Clearly here, you loose a bit of flexibility compare to the interface, where I automatically create a map from the name to the block, lhaid symbol.

For the run_card, the current run_card is likely to be already defined as
self.run_card
if it is not yet defined,
then you can do
import madgraph.various.banner as banner_mod
run_card = banner_mod.RunCard(PATH)
assignment can be done via
run_card[NAME] = value
and to write the file:
run_card.write(PATH)

note that for the run_card
1) “value” is automatically formatted to the expected format (it crashes if it is not possible)
2) NAME is case insensitive
for example
run_card[’gridpack’] = “.true.”
run_card[’GRIDPACK’] = ‘F’
print run_card[“gridpack”] # return False (the python logical)

With such manual modification of the card, you can then call directly “generate_events -f” and have the same effect as the prompt

> - What's the easiest way to extract the cross section after each run? Extracting the float string from the crossx.html seems a bit ugly, is the cross section stored elsewhere maybe?

for the latest run you can use
self.results.lastrun[‘cross’]
you can also use
self.results[RUN_NAME][‘cross’]
where you can access all the previous run of that given directory.

> - Is there any kind of technical documentation for the madgraph prompt by any chance?

Not at that level.
The structure of the prompt follows the module cmd (a standard python library for the interface)
But we have actually deeply modify it such that we do not even include that standard library anymore.

Cheers,

Olivier

> On Dec 1, 2016, at 16:42, David Englert <email address hidden> wrote:
>
> New question #404555 on MadGraph5_aMC@NLO:
> https://answers.launchpad.net/mg5amcnlo/+question/404555
>
> Dear MadGraph Developers,
>
> I would like to perform a parameter scan with madgraph to have a cross section map, but the usual solutions addressing this assume that by changing a single parameter the other ones don't need to be recomputed, or can be recomputed internally by madwidth (for Gamma's/BR's). In my case I'd need to replace the complete parameter card with a new one when changing parameters. I could do this by generating a really long madevent script, but this solution feels really cumbersome. Instead I started building a python wrapper from which I can call madgraph routines, delete the temporary output, save the cross section and move on to the next param. point.
> I gained access to the madevent prompt via:
>
> sys.path.append(os.path.join(workdir,'bin','internal'))
> import madevent_interface as ME
> launch = ME.MadEventCmd(me_dir=directory)
> launch.run_cmd('generate_events')
>
> With this I am able to run the commands available in madevent.
>
> I have a couple of questions inspired by this:
> - At this stage this solution is equivalent to just calling generate_events -f from a shell script. I am wondering whether it is possible to access the generate_events prompt in a similar way to issue commands like 'delphes=ON', 'set param val', etc.
> - What's the easiest way to extract the cross section after each run? Extracting the float string from the crossx.html seems a bit ugly, is the cross section stored elsewhere maybe?
> - Is there any kind of technical documentation for the madgraph prompt by any chance?
>
> Thanks for the help.
>
> Cheers,
> David
>
> --
> You received this question notification because you are an answer
> contact for MadGraph5_aMC@NLO.

Revision history for this message
David Englert (ganzilion) said :
#2

Thanks Olivier for the guidance, this setup seems to work(*). It's very neat to be able to use python as a wrapper for the scans, greatly simplifies things.

(*) Tough I needed to chose
xsec = launch.results.current['cross']
instead of
self.results.lastrun[‘cross’]
to get the cross section.