ATLAS Offline Software
Loading...
Searching...
No Matches
python.MadGraphUtils Namespace Reference

Functions

 stack_subprocess (command, **kwargs)
 generate_prep (process_dir)
 new_process (process='generate p p > t t~\noutput -f', plugin=None, keepJpegs=False, usePMGSettings=False, pdf_setting=None, devices=None, catch_errors=MADGRAPH_CATCH_ERRORS)
 _should_catch_errors ()
 _write_run_card (runArgs=None, flags=None)
 get_pdf_setting (pdf_setting=None)
 get_default_runcard (process_dir=MADGRAPH_GRIDPACK_LOCATION)
 generate (process_dir='PROC_mssm_0', grid_pack=False, gridpack_compile=False, extlhapath=None, required_accuracy=0.01, runArgs=None, flags=None, bias_module=None, requirePMGSettings=False, pdf_setting=None)
 generate_from_gridpack (runArgs=None, flags=None, extlhapath=None, gridpack_compile=None, requirePMGSettings=False, pdf_setting=None)
 setupFastjet (process_dir=None)
 get_runArgs_info (runArgs=None, flags=None)
 get_output_txt_file (runArgs=None, flags=None)
 setupLHAPDF (process_dir=None, extlhapath=None, allow_links=True)
 setNCores (process_dir, Ncores=None)
 get_mg5_executable ()
 add_lifetimes (process_dir, threshold=None)
 add_madspin (madspin_card=None, process_dir=MADGRAPH_GRIDPACK_LOCATION)
 madspin_on_lhe (input_LHE, madspin_card, runArgs=None, keep_original=False)
 arrange_output (process_dir=MADGRAPH_GRIDPACK_LOCATION, lhe_version=None, saveProcDir=False, runArgs=None, flags=None, fixEventWeightsForBridgeMode=False, pdf_setting=None)
 get_expected_reweight_names (reweight_card_loc)
 get_expected_systematic_names (syst_setting)
 setup_bias_module (bias_module, process_dir)
 get_reweight_card (process_dir=MADGRAPH_GRIDPACK_LOCATION)
 check_reweight_card (process_dir=MADGRAPH_GRIDPACK_LOCATION)
 update_lhe_file (lhe_file_old, param_card_old=None, lhe_file_new=None, masses={}, delete_old_lhe=True)
 print_cards_from_dir (process_dir=MADGRAPH_GRIDPACK_LOCATION)
 print_cards (proc_card='proc_card_mg5.dat', run_card=None, param_card=None, madspin_card=None, reweight_card=None, warn_on_missing=True)
 is_gen_from_gridpack ()
 modify_run_card (run_card_input=None, run_card_backup=None, process_dir=MADGRAPH_GRIDPACK_LOCATION, runArgs=None, flags=None, settings={}, skipBaseFragment=False, pdf_setting=None)
 modify_config_card (config_card_backup=None, process_dir=MADGRAPH_GRIDPACK_LOCATION, settings={}, set_commented=True)
 get_cluster_type ()
 add_reweighting (run_name, reweight_card=None, process_dir=MADGRAPH_GRIDPACK_LOCATION)
 ls_dir (directory)
 fix_fks_makefile (process_dir)
 setup_pdf_and_systematic_weights (the_base_fragment, extras, isNLO)

Variables

 mglog = Logging.logging.getLogger('MadGraphUtils')
 my_MGC_instance = None
str python = 'python'
str MADGRAPH_GRIDPACK_LOCATION = 'madevent'
str MADGRAPH_RUN_NAME = 'run_01'
bool MADGRAPH_CATCH_ERRORS = True
 MADGRAPH_PDFSETTING = None
list MADGRAPH_COMMAND_STACK = []

Function Documentation

◆ _should_catch_errors()

python.MadGraphUtils._should_catch_errors ( )
protected

Definition at line 89 of file MadGraphUtils.py.

89def _should_catch_errors():
90 if my_MGC_instance is not None and hasattr(my_MGC_instance, 'catch_errors'):
91 return bool(my_MGC_instance.catch_errors)
92 return MADGRAPH_CATCH_ERRORS
93
94

◆ _write_run_card()

python.MadGraphUtils._write_run_card ( runArgs = None,
flags = None )
protected

Definition at line 95 of file MadGraphUtils.py.

95def _write_run_card(runArgs=None, flags=None):
96 global my_MGC_instance # noqa: F824
97 if flags is not None:
98 my_MGC_instance.write_runCard(flags=flags)
99 elif runArgs is not None:
100 my_MGC_instance.write_runCard(runArgs=runArgs)
101 else:
102 my_MGC_instance.write_runCard()
103
104

◆ add_lifetimes()

python.MadGraphUtils.add_lifetimes ( process_dir,
threshold = None )
 Add lifetimes to the generated LHE file.  Should be
called after generate_events is called.

Definition at line 823 of file MadGraphUtils.py.

823def add_lifetimes(process_dir,threshold=None):
824 """ Add lifetimes to the generated LHE file. Should be
825 called after generate_events is called.
826 """
827
828 me_exec=get_mg5_executable()
829
830 if len(glob.glob(process_dir+'/Events/*'))<1:
831 mglog.error('Process dir '+process_dir+' does not contain events?')
832 run = glob.glob(process_dir+'/Events/*')[0].split('/')[-1]
833
834 # Note : This slightly clunky implementation is needed for the time being
835 # See : https://answers.launchpad.net/mg5amcnlo/+question/267904
836
837 tof_c = open('time_of_flight_exec_card','w')
838 tof_c.write('launch '+process_dir+''' -i
839add_time_of_flight '''+run+((' --threshold='+str(threshold)) if threshold is not None else ''))
840 tof_c.close()
841
842 mglog.info('Started adding time of flight info '+str(time.asctime()))
843
844 generate = stack_subprocess([python,me_exec,'time_of_flight_exec_card'],stdin=subprocess.PIPE,stderr=subprocess.PIPE if _should_catch_errors() else None)
845 (out,err) = generate.communicate()
846 error_check(err,generate.returncode)
847
848 mglog.info('Finished adding time of flight information at '+str(time.asctime()))
849
850 # Re-zip the file if needed
851 lhe_gz = glob.glob(process_dir+'/Events/*/*lhe.gz')[0]
852 if not os.access(lhe_gz,os.R_OK):
853 mglog.info('LHE file needs to be zipped')
854 lhe = glob.glob(process_dir+'/Events/*/*lhe.gz')[0]
855 rezip = stack_subprocess(['gzip',lhe])
856 rezip.wait()
857 mglog.info('Zipped')
858 else:
859 mglog.info('LHE file zipped by MadGraph automatically. Nothing to do')
860
861 return True
862
863
std::vector< std::string > split(const std::string &s, const std::string &t=":")
Definition hcg.cxx:179

◆ add_madspin()

python.MadGraphUtils.add_madspin ( madspin_card = None,
process_dir = MADGRAPH_GRIDPACK_LOCATION )
 Run madspin on the generated LHE file.  Should be
run when you have inputGeneratorFile set.
Only requires a simplified process with the same model that you are
interested in (needed to set up a process directory for MG5_aMC)

Definition at line 864 of file MadGraphUtils.py.

864def add_madspin(madspin_card=None,process_dir=MADGRAPH_GRIDPACK_LOCATION):
865 """ Run madspin on the generated LHE file. Should be
866 run when you have inputGeneratorFile set.
867 Only requires a simplified process with the same model that you are
868 interested in (needed to set up a process directory for MG5_aMC)
869 """
870
871 me_exec=get_mg5_executable()
872
873 if madspin_card is not None:
874 shutil.copyfile(madspin_card,process_dir+'/Cards/madspin_card.dat')
875
876 if len(glob.glob(process_dir+'/Events/*'))<1:
877 mglog.error('Process dir '+process_dir+' does not contain events?')
878 proc_dir_list = glob.glob(process_dir+'/Events/*')
879 run=None
880 for adir in proc_dir_list:
881 if 'GridRun_' in adir:
882 run=adir.split('/')[-1]
883 break
884 else:
885 run=proc_dir_list[0].split('/')[-1]
886
887 # Note : This slightly clunky implementation is needed for the time being
888 # See : https://answers.launchpad.net/mg5amcnlo/+question/267904
889
890 ms_c = open('madspin_exec_card','w')
891 ms_c.write('launch '+process_dir+''' -i
892decay_events '''+run)
893 ms_c.close()
894
895 mglog.info('Started running madspin at '+str(time.asctime()))
896
897 generate = stack_subprocess([python,me_exec,'madspin_exec_card'],stdin=subprocess.PIPE,stderr=subprocess.PIPE if _should_catch_errors() else None)
898 (out,err) = generate.communicate()
899 error_check(err,generate.returncode)
900 if len(glob.glob(process_dir+'/Events/'+run+'_decayed_*/')) == 0:
901 mglog.error('No '+process_dir+'/Events/'+run+'_decayed_*/ can be found')
902 raise RuntimeError('Problem while running MadSpin')
903
904 mglog.info('Finished running madspin at '+str(time.asctime()))
905
906 # Re-zip the file if needed
907 lhe_gz = glob.glob(process_dir+'/Events/*/*lhe.gz')[0]
908 if not os.access(lhe_gz,os.R_OK):
909 mglog.info('LHE file needs to be zipped')
910 lhe = glob.glob(process_dir+'/Events/*/*lhe.gz')[0]
911 rezip = stack_subprocess(['gzip',lhe])
912 rezip.wait()
913 mglog.info('Zipped')
914 else:
915 mglog.info('LHE file zipped by MadGraph automatically. Nothing to do')
916
917

◆ add_reweighting()

python.MadGraphUtils.add_reweighting ( run_name,
reweight_card = None,
process_dir = MADGRAPH_GRIDPACK_LOCATION )

Definition at line 1802 of file MadGraphUtils.py.

1802def add_reweighting(run_name,reweight_card=None,process_dir=MADGRAPH_GRIDPACK_LOCATION):
1803 mglog.info('Running reweighting module on existing events')
1804 if reweight_card is not None:
1805 mglog.info('Copying new reweight card from '+reweight_card)
1806 shutil.move(reweight_card,process_dir+'/Cards/reweight_card.dat')
1807 reweight_cmd='{}/bin/madevent reweight {} -f'.format(process_dir,run_name)
1808 reweight = stack_subprocess([python]+reweight_cmd.split(),stdin=subprocess.PIPE,stderr=subprocess.PIPE if _should_catch_errors() else None)
1809 (out,err) = reweight.communicate()
1810 error_check(err,reweight.returncode)
1811 mglog.info('Finished reweighting')
1812
1813
1814
1815

◆ arrange_output()

python.MadGraphUtils.arrange_output ( process_dir = MADGRAPH_GRIDPACK_LOCATION,
lhe_version = None,
saveProcDir = False,
runArgs = None,
flags = None,
fixEventWeightsForBridgeMode = False,
pdf_setting = None )

Definition at line 1005 of file MadGraphUtils.py.

1005def arrange_output(process_dir=MADGRAPH_GRIDPACK_LOCATION,lhe_version=None,saveProcDir=False,runArgs=None,flags=None,fixEventWeightsForBridgeMode=False,pdf_setting=None):
1006
1007 # NLO is not *really* the question here, we need to know if we should look for weighted or
1008 # unweighted events in the output directory. MadSpin (above) only seems to give weighted
1009 # results for now?
1010 if len(glob.glob(os.path.join(process_dir, 'Events','*')))<1:
1011 mglog.error('Process dir '+process_dir+' does not contain events?')
1012 proc_dir_list = glob.glob(os.path.join(process_dir, 'Events', '*'))
1013 this_run_name=None
1014 # looping over possible directories to find the right one
1015 for adir in proc_dir_list:
1016 if 'decayed' in adir:# skipping '*decayed*' directories produced by MadSpin, will be picked later if they exist
1017 continue
1018 else:
1019 if 'GridRun_' in adir:
1020 this_run_name=adir
1021 break # GridRun_* directories have priority
1022 elif os.path.join(process_dir, 'Events',MADGRAPH_RUN_NAME) in adir:
1023 this_run_name=adir
1024 if not os.access(this_run_name,os.R_OK):
1025 raise RuntimeError('Unable to locate run directory')
1026
1027 hasUnweighted = os.access(this_run_name+'/unweighted_events.lhe.gz',os.R_OK)
1028
1029 hasRunMadSpin=False
1030 madspinDirs=sorted(glob.glob(this_run_name+'_decayed_*/'))
1031 if len(madspinDirs):
1032 hasRunMadSpin=True
1033 if hasRunMadSpin and not hasUnweighted:
1034 # check again:
1035 hasUnweighted = os.access(madspinDirs[-1]+'/unweighted_events.lhe.gz',os.R_OK)
1036
1037 global MADGRAPH_COMMAND_STACK
1038 if hasRunMadSpin:
1039 if len(madspinDirs):
1040 if hasUnweighted:
1041 # so this is a bit of a mess now...
1042 # if madspin is run from an NLO grid pack the correct lhe events are at both
1043 # madevent/Events/run_01/unweighted_events.lhe.gz
1044 # and madevent/Events/run_01_decayed_1/events.lhe.gz
1045 # so there are unweighted events but not in the madspinDir...
1046 if os.path.exists(madspinDirs[-1]+'/unweighted_events.lhe.gz'):
1047 MADGRAPH_COMMAND_STACK += ['mv '+madspinDirs[-1]+'/unweighted_events.lhe.gz'+' '+this_run_name+'/unweighted_events.lhe.gz']
1048 shutil.move(madspinDirs[-1]+'/unweighted_events.lhe.gz',this_run_name+'/unweighted_events.lhe.gz')
1049 mglog.info('Moving MadSpin events from '+madspinDirs[-1]+'/unweighted_events.lhe.gz to '+this_run_name+'/unweighted_events.lhe.gz')
1050 elif os.path.exists(madspinDirs[-1]+'/events.lhe.gz'):
1051 MADGRAPH_COMMAND_STACK += ['mv '+madspinDirs[-1]+'/events.lhe.gz'+' '+this_run_name+'/unweighted_events.lhe.gz']
1052 shutil.move(madspinDirs[-1]+'/events.lhe.gz',this_run_name+'/unweighted_events.lhe.gz')
1053 mglog.info('Moving MadSpin events from '+madspinDirs[-1]+'/events.lhe.gz to '+this_run_name+'/unweighted_events.lhe.gz')
1054 else:
1055 raise RuntimeError('MadSpin was run but can\'t find files :(')
1056
1057 else:
1058 MADGRAPH_COMMAND_STACK += ['mv '+madspinDirs[-1]+'/events.lhe.gz '+this_run_name+'/events.lhe.gz']
1059 shutil.move(madspinDirs[-1]+'/events.lhe.gz',this_run_name+'/events.lhe.gz')
1060 mglog.info('Moving MadSpin events from '+madspinDirs[-1]+'/events.lhe.gz to '+this_run_name+'/events.lhe.gz')
1061
1062 else:
1063 mglog.error('MadSpin was run but can\'t find output folder '+(this_run_name+'_decayed_1/'))
1064 raise RuntimeError('MadSpin was run but can\'t find output folder '+(this_run_name+'_decayed_1/'))
1065
1066 if fixEventWeightsForBridgeMode:
1067 mglog.info("Fixing event weights after MadSpin... initial checks.")
1068
1069 # get the cross section from the undecayed LHE file
1070 spinmodenone=False
1071 MGnumevents=-1
1072 MGintweight=-1
1073
1074 if hasUnweighted:
1075 eventsfilename="unweighted_events"
1076 else:
1077 eventsfilename="events"
1078 unzip = stack_subprocess(['gunzip','-f',this_run_name+'/%s.lhe.gz' % eventsfilename])
1079 unzip.wait()
1080
1081 for line in open(process_dir+'/Events/'+MADGRAPH_RUN_NAME+'/%s.lhe'%eventsfilename):
1082 if "Number of Events" in line:
1083 sline=line.split()
1084 MGnumevents=int(sline[-1])
1085 elif "Integrated weight (pb)" in line:
1086 sline=line.split()
1087 MGintweight=float(sline[-1])
1088 elif "set spinmode none" in line:
1089 spinmodenone=True
1090 elif "</header>" in line:
1091 break
1092
1093 if spinmodenone and MGnumevents>0 and MGintweight>0:
1094 mglog.info("Fixing event weights after MadSpin... modifying LHE file.")
1095 newlhe=open(this_run_name+'/%s_fixXS.lhe'%eventsfilename,'w')
1096 initlinecount=0
1097 eventlinecount=0
1098 inInit=False
1099 inEvent=False
1100
1101 # new default for MG 2.6.1+ (https://its.cern.ch/jira/browse/AGENE-1725)
1102 # but verified from LHE below.
1103 event_norm_setting="average"
1104
1105 for line in open(this_run_name+'/%s.lhe'%eventsfilename):
1106
1107 newline=line
1108 if "<init>" in line:
1109 inInit=True
1110 initlinecount=0
1111 elif "</init>" in line:
1112 inInit=False
1113 elif inInit and initlinecount==0:
1114 initlinecount=1
1115 # check event_norm setting in LHE file, deteremines how Pythia interprets event weights
1116 sline=line.split()
1117 if abs(int(sline[-2])) == 3:
1118 event_norm_setting="sum"
1119 elif abs(int(sline[-2])) == 4:
1120 event_norm_setting="average"
1121 elif inInit and initlinecount==1:
1122 sline=line.split()
1123 # update the global XS info
1124 relunc=float(sline[1])/float(sline[0])
1125 sline[0]=str(MGintweight)
1126 sline[1]=str(float(sline[0])*relunc)
1127 if event_norm_setting=="sum":
1128 sline[2]=str(MGintweight/MGnumevents)
1129 elif event_norm_setting=="average":
1130 sline[2]=str(MGintweight)
1131 newline=' '.join(sline)
1132 newline+="\n"
1133 initlinecount+=1
1134 elif inInit and initlinecount>1:
1135 initlinecount+=1
1136 elif "<event>" in line:
1137 inEvent=True
1138 eventlinecount=0
1139 elif "</event>" in line:
1140 inEvent=False
1141 elif inEvent and eventlinecount==0:
1142 sline=line.split()
1143 # next change the per-event weights
1144 if event_norm_setting=="sum":
1145 sline[2]=str(MGintweight/MGnumevents)
1146 elif event_norm_setting=="average":
1147 sline[2]=str(MGintweight)
1148 newline=' '.join(sline)
1149 newline+="\n"
1150 eventlinecount+=1
1151 newlhe.write(newline)
1152 newlhe.close()
1153
1154 mglog.info("Fixing event weights after MadSpin... cleaning up.")
1155 shutil.copyfile(this_run_name+'/%s.lhe' % eventsfilename,
1156 this_run_name+'/%s_badXS.lhe' % eventsfilename)
1157
1158 shutil.move(this_run_name+'/%s_fixXS.lhe' % eventsfilename,
1159 this_run_name+'/%s.lhe' % eventsfilename)
1160
1161 rezip = stack_subprocess(['gzip',this_run_name+'/%s.lhe' % eventsfilename])
1162 rezip.wait()
1163
1164 rezip = stack_subprocess(['gzip',this_run_name+'/%s_badXS.lhe' % eventsfilename])
1165 rezip.wait()
1166
1167 # Clean up in case a link or file was already there
1168 if os.path.exists(os.getcwd()+'/events.lhe'):
1169 os.remove(os.getcwd()+'/events.lhe')
1170
1171 mglog.info('Unzipping generated events.')
1172 if hasUnweighted:
1173 unzip = stack_subprocess(['gunzip','-f',this_run_name+'/unweighted_events.lhe.gz'])
1174 unzip.wait()
1175 else:
1176 unzip = stack_subprocess(['gunzip','-f',this_run_name+'/events.lhe.gz'])
1177 unzip.wait()
1178
1179 mglog.info('Putting a copy in place for the transform.')
1180 if hasUnweighted:
1181 orig_input = this_run_name+'/unweighted_events.lhe'
1182 mod_output = open(os.getcwd()+'/events.lhe','w')
1183 else:
1184 orig_input = this_run_name+'/events.lhe'
1185 mod_output = open(os.getcwd()+'/events.lhe','w')
1186
1187 #Removing empty lines and bad comments in LHE
1188 #and check for existence of weights
1189 initrwgt=None
1190 nEmpty=0
1191 lhe_weights=[]
1192 with open(orig_input,'r') as fileobject:
1193 for line in fileobject:
1194 if line.strip():
1195 # search for bad characters (neccessary until at least MG5 2.8.1)
1196 newline=line
1197 if '#' not in newline:
1198 newline=newline
1199 elif '>' not in newline[ newline.find('#'): ]:
1200 newline=newline
1201 else:
1202 mglog.info('Found bad LHE line with an XML mark in a comment: "'+newline.strip()+'"')
1203 newline=newline[:newline.find('#')]+'#'+ (newline[newline.find('#'):].replace('>','-'))
1204 # check for weightnames that should exist, simplify nominal weight names
1205 if initrwgt is False:
1206 pass
1207 elif "</initrwgt>" in newline:
1208 initrwgt=False
1209 elif "<initrwgt>" in newline:
1210 initrwgt=True
1211 elif initrwgt is not None:
1212 newline=newline.replace('_DYNSCALE-1','')
1213 if '</weight>' in newline:
1214 iend=newline.find('</weight>')
1215 istart=newline[:iend].rfind('>')
1216 lhe_weights+=[newline[istart+1:iend].strip()]
1217 mod_output.write(newline)
1218 else:
1219 nEmpty=nEmpty+1
1220 mod_output.close()
1221 mglog.info('Removed '+str(nEmpty)+' empty lines from LHEF')
1222
1223 mglog.info("The following "+str(len(lhe_weights))+" weights have been written to the LHE file: "+",".join(lhe_weights))
1224 expected_weights=get_expected_reweight_names(get_reweight_card(process_dir))
1225 expected_weights+=get_expected_systematic_names(get_pdf_setting(pdf_setting))
1226 mglog.info("Checking whether the following expected weights are in LHE file: "+",".join(expected_weights))
1227 for w in expected_weights:
1228 if w not in lhe_weights:
1229 raise RuntimeError("Did not find expected weight "+w+" in lhe file. Did the reweight or systematics module crash?")
1230 mglog.info("Found all required weights!")
1231
1232 if lhe_version:
1233 mod_output2 = open(os.getcwd()+'/events.lhe','r')
1234 test=mod_output2.readline()
1235 if 'version="' in test:
1236 mglog.info('Applying LHE version hack')
1237 final_file = open(os.getcwd()+'/events.lhe.copy','w')
1238 final_file.write('<LesHouchesEvents version="%i.0">\n'%lhe_version)
1239 shutil.copyfileobj(mod_output2, final_file)
1240 final_file.close()
1241 shutil.copy(os.getcwd()+'/events.lhe.copy',os.getcwd()+'/events.lhe')
1242 # Clean up after ourselves
1243 os.remove(os.getcwd()+'/events.lhe.copy')
1244 mod_output2.close()
1245
1246 # Actually move over the dataset
1247 outputDS = get_output_txt_file(runArgs=runArgs, flags=flags)
1248 if outputDS is None:
1249 outputDS = 'tmp_LHE_events.tar.gz'
1250 if flags is not None and hasattr(flags, 'Generator') and hasattr(flags.Generator, 'avoidExtracting') and flags.Generator.avoidExtracting:
1251 outputDS = 'tmp_LHE_events.gz'
1252 elif runArgs is not None and hasattr(runArgs, 'avoidExtracting') and runArgs.avoidExtracting:
1253 outputDS = 'tmp_LHE_events.gz'
1254
1255 outputStem = outputDS
1256 if '.tar.gz' in outputDS:
1257 outputStem = outputDS.split('.tar.gz')[0]
1258 elif '.tgz' in outputDS:
1259 outputStem = outputDS.split('.tgz')[0]
1260 elif '.gz' in outputDS:
1261 outputStem = outputDS.split('.gz')[0]
1262 else:
1263 mglog.warning(f'Could not figure out what output file type {outputDS} refers to')
1264 outputStem = outputDS.split('.')[0]
1265 outputStem += '.events'
1266
1267 mglog.info('Moving file over to '+outputStem)
1268 shutil.move(os.getcwd()+'/events.lhe',outputStem)
1269
1270 if '.tar.gz' in outputDS or '.tgz' in outputDS:
1271 mglog.info('Re-zipping + tarring into dataset name '+outputDS)
1272 rezip = stack_subprocess(['tar','cvzf',outputDS,outputStem])
1273 rezip.wait()
1274 elif '.gz' in outputDS:
1275 mglog.info('Re-zipping into dataset name '+outputDS)
1276 rezip = stack_subprocess(['gzip',outputStem])
1277 rezip.wait()
1278 shutil.move(outputStem+'.gz',outputDS)
1279 else:
1280 mglog.info(f'Could not understand output type for {outputDS} - will leave uncompressed')
1281
1282 if not saveProcDir:
1283 mglog.info('Removing the process directory')
1284 shutil.rmtree(process_dir,ignore_errors=True)
1285
1286 if os.path.isdir('MGC_LHAPDF/'):
1287 shutil.rmtree('MGC_LHAPDF/',ignore_errors=True)
1288
1289 # shortening the outputDS in the case of an output LHE file
1290 if runArgs is not None and hasattr(runArgs,'outputTXTFile') and runArgs.outputTXTFile is not None:
1291 outputDS = outputDS.split('.TXT')[0]
1292 # Do some fixing up for them
1293 if runArgs is not None:
1294 mglog.debug('Setting inputGenerator file to '+outputDS)
1295 runArgs.inputGeneratorFile=outputDS
1296
1297 mglog.info('All done with output arranging!')
1298 return outputDS
1299
std::string replace(std::string s, const std::string &s2, const std::string &s3)
Definition hcg.cxx:312

◆ check_reweight_card()

python.MadGraphUtils.check_reweight_card ( process_dir = MADGRAPH_GRIDPACK_LOCATION)

Definition at line 1390 of file MadGraphUtils.py.

1390def check_reweight_card(process_dir=MADGRAPH_GRIDPACK_LOCATION):
1391 reweight_card=get_reweight_card(process_dir=process_dir)
1392 shutil.move(reweight_card,reweight_card+'.old')
1393 oldcard = open(reweight_card+'.old','r')
1394 newcard = open(reweight_card,'w')
1395 changed = False
1396 info_expression=r'launch.*--rwgt_info\s*=\s*(\S+).*'
1397 name_expression=info_expression.replace('info','name')
1398 goodname_expression=r'^[A-Za-z0-9_\-.]+$'
1399 for line in oldcard:
1400 # we are only interested in the 'launch' line
1401 if not line.strip().startswith('launch') :
1402 newcard.write(line)
1403 else:
1404 rwgt_name_match=re.match(name_expression,line.strip())
1405 rwgt_info_match=re.match(info_expression,line.strip())
1406 if rwgt_name_match is None and rwgt_info_match is None:
1407 raise RuntimeError('Every reweighting should have a --rwgt_info (see https://cp3.irmp.ucl.ac.be/projects/madgraph/wiki/Reweight), please update your reweight_card accordingly. Line to fix: '+line)
1408 for match in [rwgt_info_match,rwgt_name_match]:
1409 if match is None:
1410 continue
1411 if len(match.groups())!=1:
1412 raise RuntimeError('Unexpected format of reweight card in line: '+line)
1413 if not re.match(goodname_expression,match.group(1)):
1414 raise RuntimeError('No special character in reweighting info/name, only allowing '+goodname_expression)
1415 if rwgt_info_match is not None:
1416 newcard.write(line)
1417 elif rwgt_name_match is not None:
1418 newcard.write(line.strip()+' --rwgt_info={0}\n'.format(rwgt_name_match.group(1)))
1419 changed=True
1420 if changed:
1421 mglog.info('Updated reweight_card')
1422 newcard.close()
1423 oldcard.close()
1424
1425

◆ fix_fks_makefile()

python.MadGraphUtils.fix_fks_makefile ( process_dir)

Definition at line 1824 of file MadGraphUtils.py.

1824def fix_fks_makefile(process_dir):
1825 makefile_fks=process_dir+'/SubProcesses/makefile_fks_dir'
1826 mglog.info('Fixing '+makefile_fks)
1827 shutil.move(makefile_fks,makefile_fks+'_orig')
1828 fin=open(makefile_fks+'_orig')
1829 fout=open(makefile_fks,'w')
1830 edit=False
1831 for line in fin:
1832 if 'FKSParams.mod' in line:
1833 fout.write(line.replace('FKSParams.mod','FKSParams.o'))
1834 edit=True
1835 elif edit and 'driver_mintFO' in line:
1836 fout.write('driver_mintFO.o: weight_lines.o mint_module.o FKSParams.o\n')
1837 elif edit and 'genps_fks.o' in line:
1838 fout.write('genps_fks.o: mint_module.o FKSParams.o\n')
1839 elif edit and 'test_soft_col_limits' in line:
1840 fout.write(line)
1841 fout.write('madfks_plot.o: mint_module.o\n')
1842 fout.write('cluster.o: weight_lines.o\n')
1843 else:
1844 fout.write(line)
1845 fin.close()
1846 fout.close()
1847
1848#==================================================================================
1849# this function is called during build_run card to check the consistency of user-provided arguments with the inlude
1850# and throw errors, warnings, or corrects the input as is appropriate

◆ generate()

python.MadGraphUtils.generate ( process_dir = 'PROC_mssm_0',
grid_pack = False,
gridpack_compile = False,
extlhapath = None,
required_accuracy = 0.01,
runArgs = None,
flags = None,
bias_module = None,
requirePMGSettings = False,
pdf_setting = None )

Definition at line 133 of file MadGraphUtils.py.

133def generate(process_dir='PROC_mssm_0', grid_pack=False, gridpack_compile=False, extlhapath=None, required_accuracy=0.01, runArgs=None, flags=None, bias_module=None, requirePMGSettings=False, pdf_setting=None):
134 global my_MGC_instance # noqa: F824
135
136
137 # Just in case
138 my_MGC_instance.setup_path_protection()
139
140 # Set consistent mode and number of jobs
141 mode = 0
142 njobs = 1
143 if 'ATHENA_CORE_NUMBER' in os.environ and int(os.environ['ATHENA_CORE_NUMBER'])>0:
144 njobs = int(os.environ['ATHENA_CORE_NUMBER'])
145 mglog.info('Lucky you - you are running on a full node queue. Will re-configure for '+str(njobs)+' jobs.')
146 mode = 2
147
148 cluster_type = get_cluster_type()
149 if cluster_type is not None:
150 mode = 1
151
152 if is_gen_from_gridpack():
153 mglog.info('Running event generation from gridpack (using smarter mode from generate() function)')
154 if flags is not None:
155 generate_from_gridpack(flags=flags,extlhapath=extlhapath,gridpack_compile=gridpack_compile,requirePMGSettings=requirePMGSettings,pdf_setting=pdf_setting)
156 else:
157 generate_from_gridpack(runArgs=runArgs,extlhapath=extlhapath,gridpack_compile=gridpack_compile,requirePMGSettings=requirePMGSettings,pdf_setting=pdf_setting)
158 return
159 else:
160 mglog.info('Did not identify an input gridpack.')
161 if grid_pack:
162 mglog.info('The grid_pack flag is set, so I am expecting to create a gridpack in this job')
163
164 # Now get beam energy and random seed out of runArgs or flags
165 beamEnergy,random_seed = get_runArgs_info(runArgs=runArgs, flags=flags)
166
167 # Check if process is NLO or LO
168 isNLO = my_MGC_instance.isNLO
169
170 # Setup PDF and systematics
171 setup_pdf_and_systematic_weights(get_pdf_setting(pdf_setting),my_MGC_instance.runCardDict,isNLO)
172
173
174 # temporary fix of makefile, needed for 3.3.1., remove in future
175 if isNLO:
176 fix_fks_makefile(process_dir=process_dir)
177
178 # if f2py not available
179 if get_reweight_card(process_dir=process_dir) is not None:
180 if shutil.which('f2py') is not None:
181 mglog.info('Found f2py, will use it for reweighting')
182 else:
183 raise RuntimeError('Could not find f2py, needed for reweighting')
184 check_reweight_card(process_dir)
185
186 global MADGRAPH_COMMAND_STACK
187
188 if grid_pack:
189 #Running in gridpack mode
190 mglog.info('Started generating gridpack at '+str(time.asctime()))
191 mglog.warning(' >>>>>> THIS KIND OF JOB SHOULD ONLY BE RUN LOCALLY - NOT IN GRID JOBS <<<<<<')
192
193 # Some events required if we specify MadSpin usage!
194 my_settings = {'nevents':'1000'}
195
196 if isNLO:
197 my_settings['req_acc']=str(required_accuracy)
198 else:
199 # At LO, no events are generated. That means we need to move the MS card aside and back.
200 LO_has_madspin = False
201 if os.access(f'{process_dir}/Cards/madspin_card.dat',os.R_OK):
202 MADGRAPH_COMMAND_STACK += [f'mv {process_dir}/Cards/madspin_card.dat {process_dir}/Cards/madspin_card.tmp.dat']
203 os.rename(f'{process_dir}/Cards/madspin_card.dat',f'{process_dir}/Cards/madspin_card.tmp.dat')
204 LO_has_madspin = True
205 my_settings = {'gridpack':'true'}
206 my_MGC_instance.runCardDict.update(my_settings)
207 else:
208 #Running in on-the-fly mode
209 mglog.info('Started generating at '+str(time.asctime()))
210
211 mglog.info('Run '+MADGRAPH_RUN_NAME+' will be performed in mode '+str(mode)+' with '+str(njobs)+' jobs in parallel.')
212
213 # Ensure that things are set up normally
214 if not os.access(process_dir,os.R_OK):
215 raise RuntimeError('No process directory found at '+process_dir)
216 if not os.access(process_dir+'/bin/generate_events',os.R_OK):
217 raise RuntimeError('No generate_events module found in '+process_dir)
218
219 mglog.info('For your information, the libraries available are (should include LHAPDF):')
220 ls_dir(process_dir+'/lib')
221
222 setupFastjet(process_dir=process_dir)
223 if bias_module is not None:
224 setup_bias_module(bias_module,process_dir)
225
226 mglog.info('Now I will hack the make files a bit. Apologies, but there seems to be no good way around this.')
227 shutil.copyfile(process_dir+'/Source/make_opts',process_dir+'/Source/make_opts_old')
228 old_opts = open(process_dir+'/Source/make_opts_old','r')
229 new_opts = open(process_dir+'/Source/make_opts','w')
230 for aline in old_opts:
231 if 'FC=g' in aline:
232 mglog.info('Configuring the fancy gfortran compiler instead of g77 / f77')
233 new_opts.write(' FC=gfortran\n')
234 else:
235 new_opts.write(aline)
236 old_opts.close()
237 new_opts.close()
238 mglog.info('Make file hacking complete.')
239
240 # Change directories
241 currdir=os.getcwd()
242 os.chdir(process_dir)
243 # Record the change
244 MADGRAPH_COMMAND_STACK += [ 'cd ${MGaMC_PROCESS_DIR}' ]
245
246 # Check the run card
247 my_MGC_instance.run_card_consistency_check()
248
249
250 # For grid packs we also need to move the systematics program aside
251 if grid_pack:
252 original_systematics_program = None if 'systematics_program' not in my_MGC_instance.runCardDict else my_MGC_instance.runCardDict['systematics_program']
253 my_MGC_instance.runCardDict['systematics_program'] = 'None'
254 mglog.info('systematics set to NONE')
255
256
257
258
259
260 # Check the param card
261 code = check_PMG_updates(process_dir=os.getcwd())
262 if requirePMGSettings and code!=0:
263 raise RuntimeError('Settings are not compliant with PMG defaults! Please use do_PMG_updates function to get PMG default params.')
264
265 # Build up the generate command
266 # Use the new-style way of passing things: just --name, everything else in config
267 command = [python,'bin/generate_events']
268 if isNLO:
269 command += ['--name='+MADGRAPH_RUN_NAME]
270 mglog.info('Removing Cards/shower_card.dat to ensure we get parton level events only')
271 try:
272 os.unlink('Cards/shower_card.dat')
273 except FileNotFoundError:
274 mglog.info('Cannot find Cards/shower_card.dat.')
275 else:
276 command += [MADGRAPH_RUN_NAME]
277 # Set the number of cores to be used
278 setNCores(process_dir=os.getcwd(), Ncores=njobs)
279 # Special handling for mode 1
280 if mode==1:
281 mglog.info('Setting up cluster running')
282 my_MGC_instance.configCardDict['run_mode'] = 1
283
284 if cluster_type=='pbs':
285 mglog.info('Modifying bin/internal/cluster.py for PBS cluster running')
286 os.system("sed -i \"s:text += prog:text += './'+prog:g\" bin/internal/cluster.py")
287 elif mode==2:
288 mglog.info('Setting up multi-core running on '+os.environ['ATHENA_CORE_NUMBER']+' cores')
289 elif mode==0:
290 mglog.info('Setting up serial generation.')
291
292 # Writing cards to disk
293 my_MGC_instance.write_configCard()
294 _write_run_card(runArgs=runArgs, flags=flags)
295
296 print_cards_from_dir(process_dir=my_MGC_instance.process_dir)
297
298
299 generate_prep(process_dir=os.getcwd())
300 generate = stack_subprocess(command,stdin=subprocess.PIPE, stderr=subprocess.PIPE if _should_catch_errors() else None)
301 (out,err) = generate.communicate()
302 error_check(err,generate.returncode)
303
304 # Get back to where we came from
305 os.chdir(currdir)
306 MADGRAPH_COMMAND_STACK += [ 'cd -' ]
307
308 if grid_pack:
309 # Name dictacted by https://twiki.cern.ch/twiki/bin/viewauth/AtlasProtected/PmgMcSoftware
310 # MG version included for traceability (e.g. MG351 for version 3.5.1)
311 energy = '%1.1f'%(beamEnergy*2./1000.)
312 energy = energy.replace('.0','').replace('.','p')
313 gridpack_name='mc_'+energy+'TeV.'+get_physics_short()+'.MG'+get_mg5_version().replace('.','')+'.GRID.tar.gz'
314 mglog.info('Tidying up gridpack '+gridpack_name)
315
316 # Return the setting for the systematics_program
317 my_MGC_instance.runCardDict.update({'systematics_program':original_systematics_program})
318 # Write out run Card Dictionary
319 _write_run_card(runArgs=runArgs, flags=flags)
320
321 if not isNLO:
322 # At LO, no events are generated. That means we need to move the MS card aside and back.
323 if LO_has_madspin:
324 MADGRAPH_COMMAND_STACK += [f'mv {process_dir}/Cards/madspin_card.tmp.dat {process_dir}/Cards/madspin_card.dat']
325 os.rename(f'{process_dir}/Cards/madspin_card.tmp.dat',f'{process_dir}/Cards/madspin_card.dat')
326
327
328 MADGRAPH_COMMAND_STACK += ['cp '+glob.glob(process_dir+'/'+MADGRAPH_RUN_NAME+'_*gridpack.tar.gz')[0]+' '+gridpack_name]
329 shutil.copy(glob.glob(process_dir+'/'+MADGRAPH_RUN_NAME+'_*gridpack.tar.gz')[0],gridpack_name)
330
331 if gridpack_compile:
332 MADGRAPH_COMMAND_STACK += ['mkdir tmp%i/'%os.getpid(),'cd tmp%i/'%os.getpid()]
333 os.mkdir('tmp%i/'%os.getpid())
334 os.chdir('tmp%i/'%os.getpid())
335 mglog.info('untar gridpack')
336 untar = stack_subprocess(['tar','xvzf',('../'+gridpack_name)])
337 untar.wait()
338 mglog.info('compile and clean up')
339 MADGRAPH_COMMAND_STACK += ['cd madevent']
340 os.chdir('madevent/')
341 compilep = stack_subprocess(['./bin/compile'],stderr=subprocess.PIPE if _should_catch_errors() else None)
342 (out,err) = compilep.communicate()
343 error_check(err,compilep.returncode)
344 clean = stack_subprocess(['./bin/clean4grid'],stderr=subprocess.PIPE if _should_catch_errors() else None)
345 (out,err) = clean.communicate()
346 error_check(err,clean.returncode)
347 clean.wait()
348 MADGRAPH_COMMAND_STACK += ['cd ..','rm ../'+gridpack_name]
349 os.chdir('../')
350 mglog.info('remove old tarball')
351 os.unlink('../'+gridpack_name)
352 mglog.info('Package up new tarball')
353 tar = stack_subprocess(['tar','--exclude=SubProcesses/P*/G*/*_results.dat','--exclude=SubProcesses/P*/G*/*.log','--exclude=SubProcesses/P*/G*/*.txt','-cvsf','../'+gridpack_name,'.'])
354 tar.wait()
355 MADGRAPH_COMMAND_STACK += ['cd ..','rm -r tmp%i/'%os.getpid()]
356 os.chdir('../')
357 mglog.info('Remove temporary directory')
358 shutil.rmtree('tmp%i/'%os.getpid())
359 mglog.info('Tidying up complete!')
360
361 else:
362 _write_run_card(runArgs=runArgs, flags=flags)
363
364 mglog.info('Package up process_dir')
365 MADGRAPH_COMMAND_STACK += ['mv '+process_dir+' '+MADGRAPH_GRIDPACK_LOCATION]
366 os.rename(process_dir,MADGRAPH_GRIDPACK_LOCATION)
367 tar = stack_subprocess(['tar','--exclude=Events/*/*events*gz','--exclude=SubProcesses/P*/G*/log*txt','--exclude=SubProcesses/P*/G*/events.lhe*','--exclude=*/*.o','--exclude=*/*/*.o','--exclude=*/*/*/*.o','--exclude=*/*/*/*/*.o','-czf',gridpack_name,MADGRAPH_GRIDPACK_LOCATION])
368 tar.wait()
369 MADGRAPH_COMMAND_STACK += ['mv '+MADGRAPH_GRIDPACK_LOCATION+' '+process_dir]
370 os.rename(MADGRAPH_GRIDPACK_LOCATION,process_dir)
371
372 mglog.info('Gridpack sucessfully created, exiting the transform')
373 output_txt_file = get_output_txt_file(runArgs=runArgs, flags=flags)
374 if output_txt_file:
375 mglog.info('Touching output TXT (LHE) file for the transform')
376 open(output_txt_file, 'w').close()
377 from AthenaCommon.AppMgr import theApp
378 theApp.finalize()
379 theApp.exit()
380
381 mglog.info('Finished at '+str(time.asctime()))
382 return 0
383
384

◆ generate_from_gridpack()

python.MadGraphUtils.generate_from_gridpack ( runArgs = None,
flags = None,
extlhapath = None,
gridpack_compile = None,
requirePMGSettings = False,
pdf_setting = None )

Definition at line 385 of file MadGraphUtils.py.

385def generate_from_gridpack(runArgs=None, flags=None, extlhapath=None, gridpack_compile=None, requirePMGSettings=False, pdf_setting=None):
386 global my_MGC_instance # noqa: F824
387
388 # Get beam energy and random seed out of runArgs or flags
389 beamEnergy,random_seed = get_runArgs_info(runArgs=runArgs, flags=flags)
390
391 # Just in case
392 my_MGC_instance.setup_path_protection()
393
394 isNLO = my_MGC_instance.isNLO
395
396 setupFastjet(process_dir=MADGRAPH_GRIDPACK_LOCATION)
397
398 # This is hard-coded as a part of MG5_aMC :'(
399 gridpack_run_name = 'GridRun_'+str(random_seed)
400
401 # Ensure that we only do madspin at the end
402 if os.access(MADGRAPH_GRIDPACK_LOCATION+'/Cards/madspin_card.dat',os.R_OK):
403 os.rename(MADGRAPH_GRIDPACK_LOCATION+'/Cards/madspin_card.dat',MADGRAPH_GRIDPACK_LOCATION+'/Cards/backup_madspin_card.dat')
404 do_madspin=True
405 else:
406 do_madspin=False
407
408 if get_reweight_card(process_dir=MADGRAPH_GRIDPACK_LOCATION) is not None:
409 check_reweight_card(MADGRAPH_GRIDPACK_LOCATION)
410
411 # Check the param card
412 code = check_PMG_updates(process_dir=MADGRAPH_GRIDPACK_LOCATION)
413 if requirePMGSettings and code!=0:
414 raise RuntimeError('Settings are not compliant with PMG defaults! Please use do_PMG_updates function to get PMG default params.')
415
416 # Modify run card, then print
417 settings={'iseed':str(random_seed)}
418 if not isNLO:
419 settings['python_seed']=str(random_seed)
420 my_MGC_instance.runCardDict.update(settings)
421
422 mglog.info('Generating events from gridpack')
423
424 # Ensure that things are set up normally
425 if not os.path.exists(MADGRAPH_GRIDPACK_LOCATION):
426 raise RuntimeError('Gridpack directory not found at '+MADGRAPH_GRIDPACK_LOCATION)
427
428 nevents = my_MGC_instance.runCardDict['nevents']
429 mglog.info('>>>> FOUND GRIDPACK <<<< <- This will be used for generation')
430 mglog.info('Generation of '+str(int(nevents))+' events will be performed using the supplied gridpack with random seed '+str(random_seed))
431 mglog.info('Started generating events at '+str(time.asctime()))
432
433 #Remove addmasses if it's there
434 if os.access(MADGRAPH_GRIDPACK_LOCATION+'/bin/internal/addmasses.py',os.R_OK):
435 os.remove(MADGRAPH_GRIDPACK_LOCATION+'/bin/internal/addmasses.py')
436
437 currdir=os.getcwd()
438
439 # Make sure we've set the number of processes appropriately
440 setNCores(process_dir=MADGRAPH_GRIDPACK_LOCATION)
441
442 # Run the consistency check, print some useful info
443 ls_dir(currdir)
444 ls_dir(MADGRAPH_GRIDPACK_LOCATION)
445
446 # Update the run card according to consistency checks
447 my_MGC_instance.run_card_consistency_check()
448
449 # Now all done with updates, so print the cards with the final settings
450 print_cards_from_dir(process_dir=MADGRAPH_GRIDPACK_LOCATION)
451
452 if isNLO:
453 #turn off systematics for gridpack generation and store settings for standalone run
454 systematics_settings=None
455 if my_MGC_instance.runCardDict.get('systematics_program',None) == 'systematics':
456 if not my_MGC_instance.runCardDict.get('store_rwgt_info',None):
457 raise RuntimeError('Trying to run NLO systematics but reweight info not stored')
458 if 'systematics_arguments' in my_MGC_instance.runCardDict:
459 systematics_settings=MadGraphControl.MadGraphSystematicsUtils.parse_systematics_arguments(my_MGC_instance.runCardDict['systematics_arguments'])
460 else:
461 systematics_settings={}
462 mglog.info('Turning off systematics for now, running standalone later')
463
464 my_MGC_instance.runCardDict['systematics_program'] = 'none'
465
466 # Writing run card to disk
467 _write_run_card(runArgs=runArgs, flags=flags)
468
469 global MADGRAPH_COMMAND_STACK
470 if not isNLO:
471
472 if not os.access(MADGRAPH_GRIDPACK_LOCATION+'/bin/gridrun',os.R_OK):
473 mglog.error('/bin/gridrun not found at '+MADGRAPH_GRIDPACK_LOCATION)
474 raise RuntimeError('Could not find gridrun executable')
475 else:
476 mglog.info('Found '+MADGRAPH_GRIDPACK_LOCATION+'/bin/gridrun, starting generation.')
477 generate_prep(MADGRAPH_GRIDPACK_LOCATION)
478 granularity=1
479 mglog.info("Now generating {} events with random seed {} and granularity {}".format(int(nevents),int(random_seed),granularity))
480 # not sure whether this is needed but it is done in the old "run.sh" script
481 new_ld_path=":".join([os.environ['LD_LIBRARY_PATH'],os.getcwd()+'/'+MADGRAPH_GRIDPACK_LOCATION+'/madevent/lib',os.getcwd()+'/'+MADGRAPH_GRIDPACK_LOCATION+'/HELAS/lib'])
482 os.environ['LD_LIBRARY_PATH']=new_ld_path
483 MADGRAPH_COMMAND_STACK+=["export LD_LIBRARY_PATH="+":".join(['${LD_LIBRARY_PATH}',new_ld_path])]
484 generate = stack_subprocess([python,MADGRAPH_GRIDPACK_LOCATION+'/bin/gridrun',str(int(nevents)),str(int(random_seed)),str(granularity)],stdin=subprocess.PIPE,stderr=subprocess.PIPE if _should_catch_errors() else None)
485 (out,err) = generate.communicate()
486 error_check(err,generate.returncode)
487 gp_events=MADGRAPH_GRIDPACK_LOCATION+"/Events/GridRun_{}/unweighted_events.lhe.gz".format(int(random_seed))
488 if not os.path.exists(gp_events):
489 mglog.error('Error in gp generation, did not find events at '+gp_events)
490
491 # add reweighting, which is not run automatically from LO GPs
492 reweight_card=get_reweight_card(MADGRAPH_GRIDPACK_LOCATION)
493 if reweight_card is not None:
494 pythonpath_backup=os.environ['PYTHONPATH']
495 # workaround as madevent crashes when path to mg in PYTHONPATH
496 os.environ['PYTHONPATH']=':'.join([p for p in pythonpath_backup.split(':') if 'madgraph5amc' not in p])
497 add_reweighting('GridRun_{}'.format(int(random_seed)))
498 os.environ['PYTHONPATH']=pythonpath_backup
499
500 shutil.move(gp_events,'events.lhe.gz')
501
502 else:
503
504 if not os.access(MADGRAPH_GRIDPACK_LOCATION+'/bin/generate_events',os.R_OK):
505 raise RuntimeError('Could not find generate_events executable at '+MADGRAPH_GRIDPACK_LOCATION)
506 else:
507 mglog.info('Found '+MADGRAPH_GRIDPACK_LOCATION+'/bin/generate_events, starting generation.')
508
509 ls_dir(MADGRAPH_GRIDPACK_LOCATION+'/Events/')
510 if os.access(MADGRAPH_GRIDPACK_LOCATION+'/Events/'+gridpack_run_name, os.F_OK):
511 mglog.info('Removing '+MADGRAPH_GRIDPACK_LOCATION+'/Events/'+gridpack_run_name+' directory from gridpack generation')
512 MADGRAPH_COMMAND_STACK += ['rm -rf '+MADGRAPH_GRIDPACK_LOCATION+'/Events/'+gridpack_run_name]
513 shutil.rmtree(MADGRAPH_GRIDPACK_LOCATION+'/Events/'+gridpack_run_name)
514
515 # Delete events generated when setting up MadSpin during gridpack generation
516 if os.access(MADGRAPH_GRIDPACK_LOCATION+'/Events/'+gridpack_run_name+'_decayed_1', os.F_OK):
517 mglog.info('Removing '+MADGRAPH_GRIDPACK_LOCATION+'/Events/'+gridpack_run_name+'_decayed_1 directory from gridpack generation')
518 MADGRAPH_COMMAND_STACK += ['rm -rf '+MADGRAPH_GRIDPACK_LOCATION+'/Events/'+gridpack_run_name+'_decayed_1']
519 shutil.rmtree(MADGRAPH_GRIDPACK_LOCATION+'/Events/'+gridpack_run_name+'_decayed_1')
520
521 ls_dir(MADGRAPH_GRIDPACK_LOCATION+'/Events/')
522
523 if not gridpack_compile:
524 mglog.info('Copying make_opts from Template')
525 shutil.copy(os.environ['MADPATH']+'/Template/LO/Source/make_opts',MADGRAPH_GRIDPACK_LOCATION+'/Source/')
526
527 generate_prep(MADGRAPH_GRIDPACK_LOCATION)
528 generate = stack_subprocess([python,MADGRAPH_GRIDPACK_LOCATION+'/bin/generate_events','--parton','--nocompile','--only_generation','-f','--name='+gridpack_run_name],stdin=subprocess.PIPE,stderr=subprocess.PIPE if _should_catch_errors() else None)
529 (out,err) = generate.communicate()
530 error_check(err,generate.returncode)
531 else:
532 mglog.info('Allowing recompilation of gridpack')
533 if os.path.islink(MADGRAPH_GRIDPACK_LOCATION+'/lib/libLHAPDF.a'):
534 mglog.info('Unlinking '+MADGRAPH_GRIDPACK_LOCATION+'/lib/libLHAPDF.a')
535 os.unlink(MADGRAPH_GRIDPACK_LOCATION+'/lib/libLHAPDF.a')
536
537 generate_prep(MADGRAPH_GRIDPACK_LOCATION)
538 generate = stack_subprocess([python,MADGRAPH_GRIDPACK_LOCATION+'/bin/generate_events','--parton','--only_generation','-f','--name='+gridpack_run_name],stdin=subprocess.PIPE,stderr=subprocess.PIPE if _should_catch_errors() else None)
539 (out,err) = generate.communicate()
540 error_check(err,generate.returncode)
541 if isNLO and systematics_settings is not None:
542 # run systematics
543 mglog.info('Running systematics standalone')
544 systematics_path=MADGRAPH_GRIDPACK_LOCATION+'/bin/internal/systematics.py'
545 events_location=MADGRAPH_GRIDPACK_LOCATION+'/Events/'+gridpack_run_name+'/events.lhe.gz'
546 syst_cmd=[python,systematics_path]+[events_location]*2+["--"+k+"="+systematics_settings[k] for k in systematics_settings]
547 mglog.info('running: '+' '.join(syst_cmd))
548 systematics = stack_subprocess(syst_cmd)
549 systematics.wait()
550
551
552 # See if MG5 did the job for us already
553 if not os.access('events.lhe.gz',os.R_OK):
554 mglog.info('Copying generated events to '+currdir)
555 if not os.path.exists(MADGRAPH_GRIDPACK_LOCATION+'Events/'+gridpack_run_name):
556 shutil.copy(MADGRAPH_GRIDPACK_LOCATION+'/Events/'+gridpack_run_name+'/events.lhe.gz','events.lhe.gz')
557 else:
558 mglog.info('Events were already in place')
559
560 ls_dir(currdir)
561
562 mglog.info('Moving generated events to be in correct format for arrange_output().')
563 mglog.info('Unzipping generated events.')
564 unzip = stack_subprocess(['gunzip','-f','events.lhe.gz'])
565 unzip.wait()
566
567 mglog.info('Moving file over to '+MADGRAPH_GRIDPACK_LOCATION+'/Events/'+gridpack_run_name+'/unweighted_events.lhe')
568 mkdir = stack_subprocess(['mkdir','-p',(MADGRAPH_GRIDPACK_LOCATION+'/Events/'+gridpack_run_name)])
569 mkdir.wait()
570 shutil.move('events.lhe',MADGRAPH_GRIDPACK_LOCATION+'/Events/'+gridpack_run_name+'/unweighted_events.lhe')
571
572 mglog.info('Re-zipping into dataset name '+MADGRAPH_GRIDPACK_LOCATION+'/Events/'+gridpack_run_name+'/unweighted_events.lhe.gz')
573 rezip = stack_subprocess(['gzip',MADGRAPH_GRIDPACK_LOCATION+'/Events/'+gridpack_run_name+'/unweighted_events.lhe'])
574 rezip.wait()
575
576 os.chdir(currdir)
577
578 # Now consider MadSpin:
579 if do_madspin:
580 # Move card back
581 os.rename(MADGRAPH_GRIDPACK_LOCATION+'/Cards/backup_madspin_card.dat',MADGRAPH_GRIDPACK_LOCATION+'/Cards/madspin_card.dat')
582 mglog.info('Decaying with MadSpin.')
583 add_madspin(process_dir=MADGRAPH_GRIDPACK_LOCATION)
584
585 mglog.info('Finished at '+str(time.asctime()))
586
587 return 0
588
589

◆ generate_prep()

python.MadGraphUtils.generate_prep ( process_dir)

Definition at line 50 of file MadGraphUtils.py.

50def generate_prep(process_dir):
51 global MADGRAPH_COMMAND_STACK
52 if not os.access('Cards_bkup',os.R_OK):
53 shutil.copytree(process_dir+'/Cards','Cards_bkup')
54 shutil.copyfile(process_dir+'/Source/make_opts','Cards_bkup/make_opts_bkup')
55 MADGRAPH_COMMAND_STACK += ['# In case this fails, Cards_bkup should be in your original run directory']
56 MADGRAPH_COMMAND_STACK += ['# And ${MGaMC_PROCESS_DIR} can be replaced with whatever process directory exists in your stand-alone test']
57 MADGRAPH_COMMAND_STACK += ['cp '+os.getcwd()+'/Cards_bkup/*dat ${MGaMC_PROCESS_DIR}/Cards/']
58 MADGRAPH_COMMAND_STACK += ['cp '+os.getcwd()+'/Cards_bkup/make_opts_bkup ${MGaMC_PROCESS_DIR}/Source/make_opts']
59 else:
60 mglog.warning('Found Cards_bkup directory existing. Suggests you are either running generation twice (a little funny) or are not using a clean directory.')
61 bkup_v = 1
62 while os.access('Cards_bkup_'+str(bkup_v),os.R_OK) and bkup_v<100:
63 bkup_v += 1
64 if bkup_v<100:
65 shutil.copytree(process_dir+'/Cards','Cards_bkup_'+str(bkup_v))
66 shutil.copyfile(process_dir+'/Source/make_opts','Cards_bkup_'+str(bkup_v)+'/make_opts_bkup')
67 MADGRAPH_COMMAND_STACK += ['# In case this fails, Cards_bkup should be in your original run directory']
68 MADGRAPH_COMMAND_STACK += ['# And ${MGaMC_PROCESS_DIR} can be replaced with whatever process directory exists in your stand-alone test']
69 MADGRAPH_COMMAND_STACK += ['cp '+os.getcwd()+'/Cards_bkup_'+str(bkup_v)+'/*dat ${MGaMC_PROCESS_DIR}/Cards/']
70 MADGRAPH_COMMAND_STACK += ['cp '+os.getcwd()+'/Cards_bkup_'+str(bkup_v)+'/make_opts_bkup ${MGaMC_PROCESS_DIR}/Source/make_opts']
71 else:
72 mglog.warning('Way too many Cards_bkup* directories found. Giving up -- standalone script may not work.')
73
74

◆ get_cluster_type()

python.MadGraphUtils.get_cluster_type ( )

Definition at line 1792 of file MadGraphUtils.py.

1792def get_cluster_type():
1793 global my_MGC_instance # noqa: F824
1794 if 'cluster_type' in my_MGC_instance.configCardDict:
1795 return my_MGC_instance.configCardDict['cluster_type']
1796 else:
1797 return None
1798
1799
1800
1801

◆ get_default_runcard()

python.MadGraphUtils.get_default_runcard ( process_dir = MADGRAPH_GRIDPACK_LOCATION)
 Copy the default runcard from one of several locations
to a local file with name run_card.tmp.dat

Definition at line 112 of file MadGraphUtils.py.

112def get_default_runcard(process_dir=MADGRAPH_GRIDPACK_LOCATION):
113 """ Copy the default runcard from one of several locations
114 to a local file with name run_card.tmp.dat"""
115 output_name = 'run_card.tmp.dat'
116
117 # Get the run card from the installation
118 run_card=process_dir+'/Cards/run_card.dat'
119 if os.access(run_card,os.R_OK):
120 mglog.info('Copying default run_card.dat from '+str(run_card))
121 shutil.copy(run_card,output_name)
122 return output_name
123 else:
124 run_card=process_dir+'/Cards/run_card_default.dat'
125 mglog.info('Fetching default run_card.dat from '+str(run_card))
126 if os.access(run_card,os.R_OK):
127 shutil.copy(run_card,output_name)
128 return output_name
129 else:
130 raise RuntimeError('Cannot find default run_card.dat or run_card_default.dat! I was looking here: %s'%run_card)
131
132

◆ get_expected_reweight_names()

python.MadGraphUtils.get_expected_reweight_names ( reweight_card_loc)

Definition at line 1300 of file MadGraphUtils.py.

1300def get_expected_reweight_names(reweight_card_loc):
1301 if reweight_card_loc is None:
1302 return []
1303 names=[]
1304 f_rw=open(reweight_card_loc)
1305 for line in f_rw:
1306 if 'launch' not in line:
1307 continue
1308 match=re.match(r'launch.*--rwgt_info\s*=\s*(\S+).*',line.strip())
1309 if len(match.groups())!=1:
1310 raise RuntimeError('Unexpected format of reweight card in line'+line)
1311 else:
1312 names+=[match.group(1)]
1313 f_rw.close()
1314 return names
1315

◆ get_expected_systematic_names()

python.MadGraphUtils.get_expected_systematic_names ( syst_setting)

Definition at line 1316 of file MadGraphUtils.py.

1316def get_expected_systematic_names(syst_setting):
1317 names=[]
1318 if syst_setting is None or 'central_pdf' not in syst_setting:
1319 mglog.warning("Systematics have not been defined via base fragment or explicit PDF settings; cannot check for expected weights")
1320 return []
1321 if 'pdf_variations' in syst_setting and isinstance(syst_setting['pdf_variations'],list):
1322 names+=[MadGraphControl.MadGraphSystematicsUtils.SYSTEMATICS_WEIGHT_INFO%{'mur':1.0,'muf':1.0,'pdf':syst_setting['central_pdf']}]
1323 for pdf in syst_setting['pdf_variations']:
1324 names+=[MadGraphControl.MadGraphSystematicsUtils.SYSTEMATICS_WEIGHT_INFO%{'mur':1.0,'muf':1.0,'pdf':pdf+1}]
1325 if 'alternative_pdfs' in syst_setting and isinstance(syst_setting['alternative_pdfs'],list):
1326 for pdf in syst_setting['alternative_pdfs']:
1327 names+=[MadGraphControl.MadGraphSystematicsUtils.SYSTEMATICS_WEIGHT_INFO%{'mur':1.0,'muf':1.0,'pdf':pdf}]
1328 if 'scale_variations' in syst_setting and isinstance(syst_setting['scale_variations'],list):
1329 for mur in syst_setting['scale_variations']:
1330 for muf in syst_setting['scale_variations']:
1331 names+=[MadGraphControl.MadGraphSystematicsUtils.SYSTEMATICS_WEIGHT_INFO%{'mur':mur,'muf':muf,'pdf':syst_setting['central_pdf']}]
1332 return names
1333

◆ get_mg5_executable()

python.MadGraphUtils.get_mg5_executable ( )

Definition at line 816 of file MadGraphUtils.py.

816def get_mg5_executable():
817 madpath=os.environ['MADPATH']
818 if not os.access(madpath+'/bin/mg5_aMC',os.R_OK):
819 raise RuntimeError('mg5_aMC executable not found in '+madpath)
820 return madpath+'/bin/mg5_aMC'
821
822

◆ get_output_txt_file()

python.MadGraphUtils.get_output_txt_file ( runArgs = None,
flags = None )
Return output TXT file path from runArgs or flags if available.

Definition at line 640 of file MadGraphUtils.py.

640def get_output_txt_file(runArgs=None, flags=None):
641 """Return output TXT file path from runArgs or flags if available."""
642 if flags is not None:
643 if flags.Output.TXTFileName:
644 return flags.Output.TXTFileName
645 if runArgs is not None and hasattr(runArgs, 'outputTXTFile'):
646 if runArgs.outputTXTFile:
647 return runArgs.outputTXTFile
648 return None
649
650

◆ get_pdf_setting()

python.MadGraphUtils.get_pdf_setting ( pdf_setting = None)

Definition at line 105 of file MadGraphUtils.py.

105def get_pdf_setting(pdf_setting=None):
106 if pdf_setting is not None:
107 return pdf_setting
108 if my_MGC_instance is not None and hasattr(my_MGC_instance, 'pdf_setting'):
109 return my_MGC_instance.pdf_setting
110 return MADGRAPH_PDFSETTING
111

◆ get_reweight_card()

python.MadGraphUtils.get_reweight_card ( process_dir = MADGRAPH_GRIDPACK_LOCATION)

Definition at line 1384 of file MadGraphUtils.py.

1384def get_reweight_card(process_dir=MADGRAPH_GRIDPACK_LOCATION):
1385 if os.access(process_dir+'/Cards/reweight_card.dat',os.R_OK):
1386 return process_dir+'/Cards/reweight_card.dat'
1387 return None
1388
1389

◆ get_runArgs_info()

python.MadGraphUtils.get_runArgs_info ( runArgs = None,
flags = None )
Return beam energy and random seed from runArgs or flags.
Adding flags compatibility while clients migrate.

Definition at line 621 of file MadGraphUtils.py.

621def get_runArgs_info(runArgs=None, flags=None):
622 """Return beam energy and random seed from runArgs or flags.
623 Adding flags compatibility while clients migrate.
624 """
625 global my_MGC_instance # noqa: F824
626 if runArgs is not None:
627 if flags is not None:
628 mglog.warning('Both runArgs and flags were provided to get_runArgs_info. Using flags.')
629 my_MGC_instance.get_flags_info(flags)
630 else:
631 my_MGC_instance.get_runArgs_info(runArgs)
632 elif flags is not None:
633 my_MGC_instance.get_flags_info(flags)
634 else:
635 raise RuntimeError('Must provide runArgs or flags to get run information.')
636
637 return my_MGC_instance.beamEnergy, my_MGC_instance.random_seed
638
639

◆ is_gen_from_gridpack()

python.MadGraphUtils.is_gen_from_gridpack ( )
 Simple function for checking if there is a grid pack.
Relies on the specific location of the unpacked gridpack (madevent)
which is here set as a global variable. The gridpack is untarred by
the transform (Gen_tf.py) and no sign is sent to the job itself
that there is a gridpack in use except the file's existence

Definition at line 1584 of file MadGraphUtils.py.

1584def is_gen_from_gridpack():
1585 """ Simple function for checking if there is a grid pack.
1586 Relies on the specific location of the unpacked gridpack (madevent)
1587 which is here set as a global variable. The gridpack is untarred by
1588 the transform (Gen_tf.py) and no sign is sent to the job itself
1589 that there is a gridpack in use except the file's existence"""
1590 if os.access(MADGRAPH_GRIDPACK_LOCATION,os.R_OK):
1591 mglog.info('Located input grid pack area')
1592 return True
1593 return False
1594
1595
1596

◆ ls_dir()

python.MadGraphUtils.ls_dir ( directory)

Definition at line 1816 of file MadGraphUtils.py.

1816def ls_dir(directory):
1817 mglog.info('For your information, ls of '+directory+':')
1818 mglog.info( sorted( os.listdir( directory ) ) )
1819
1820# Final import of some code used in these functions

◆ madspin_on_lhe()

python.MadGraphUtils.madspin_on_lhe ( input_LHE,
madspin_card,
runArgs = None,
keep_original = False )
 Run MadSpin on an input LHE file. Takes the process
from the LHE file, so you don't need to have a process directory
set up in advance. Runs MadSpin and packs the LHE file up appropriately
Needs runArgs for the file handling

Definition at line 918 of file MadGraphUtils.py.

918def madspin_on_lhe(input_LHE,madspin_card,runArgs=None,keep_original=False):
919 """ Run MadSpin on an input LHE file. Takes the process
920 from the LHE file, so you don't need to have a process directory
921 set up in advance. Runs MadSpin and packs the LHE file up appropriately
922 Needs runArgs for the file handling"""
923 if not os.access(input_LHE,os.R_OK):
924 raise RuntimeError('Could not find LHE file '+input_LHE)
925 if not os.access(madspin_card,os.R_OK):
926 raise RuntimeError('Could not find input MadSpin card '+madspin_card)
927 if keep_original:
928 shutil.copy(input_LHE,input_LHE+'.original')
929 mglog.info('Put backup copy of LHE file at '+input_LHE+'.original')
930 # Start writing the card for execution
931 madspin_exec_card = open('madspin_exec_card','w')
932 madspin_exec_card.write('import '+input_LHE+'\n')
933 # Based on the original card
934 input_madspin_card = open(madspin_card,'r')
935 has_launch = False
936 for l in input_madspin_card.readlines():
937 commands = l.split('#')[0].split()
938 # Skip import of a file name that isn't our file
939 if len(commands)>1 and 'import'==commands[0] and not 'model'==commands[1]:
940 continue
941 # Check for a launch command
942 if len(commands)>0 and 'launch' == commands[0]:
943 has_launch = True
944 madspin_exec_card.write(l.strip()+'\n')
945 if not has_launch:
946 madspin_exec_card.write('launch\n')
947 madspin_exec_card.close()
948 input_madspin_card.close()
949 # Now get the madspin executable
950 madpath=os.environ['MADPATH']
951 if not os.access(madpath+'/MadSpin/madspin',os.R_OK):
952 raise RuntimeError('madspin executable not found in '+madpath)
953 mglog.info('Starting madspin at '+str(time.asctime()))
954 generate = stack_subprocess([python,madpath+'/MadSpin/madspin','madspin_exec_card'],stdin=subprocess.PIPE,stderr=subprocess.PIPE if _should_catch_errors() else None)
955 (out,err) = generate.communicate()
956 error_check(err,generate.returncode)
957 mglog.info('Done with madspin at '+str(time.asctime()))
958 # Should now have a re-zipped LHE file
959 # We now have to do a shortened version of arrange_output below
960 # Clean up in case a link or file was already there
961 if os.path.exists(os.getcwd()+'/events.lhe'):
962 os.remove(os.getcwd()+'/events.lhe')
963
964 mglog.info('Unzipping generated events.')
965 unzip = stack_subprocess(['gunzip','-f',input_LHE+'.gz'])
966 unzip.wait()
967
968 mglog.info('Putting a copy in place for the transform.')
969 mod_output = open(os.getcwd()+'/events.lhe','w')
970
971 #Removing empty lines in LHE
972 nEmpty=0
973 with open(input_LHE,'r') as fileobject:
974 for line in fileobject:
975 if line.strip():
976 mod_output.write(line)
977 else:
978 nEmpty=nEmpty+1
979 mod_output.close()
980
981 mglog.info('Removed '+str(nEmpty)+' empty lines from LHEF')
982
983 # Actually move over the dataset - this first part is horrible...
984 if runArgs is None:
985 raise RuntimeError('Must provide runArgs to madspin_on_lhe')
986
987 outputDS = runArgs.outputTXTFile if hasattr(runArgs,'outputTXTFile') else 'tmp_LHE_events.tar.gz'
988
989 mglog.info('Moving file over to '+outputDS.split('.tar.gz')[0]+'.events')
990 shutil.move(os.getcwd()+'/events.lhe',outputDS.split('.tar.gz')[0]+'.events')
991
992 mglog.info('Re-zipping into dataset name '+outputDS)
993 rezip = stack_subprocess(['tar','cvzf',outputDS,outputDS.split('.tar.gz')[0]+'.events'])
994 rezip.wait()
995
996 # shortening the outputDS in the case of an output TXT file
997 if hasattr(runArgs,'outputTXTFile') and runArgs.outputTXTFile is not None:
998 outputDS = outputDS.split('.TXT')[0]
999 # Do some fixing up for them
1000 if runArgs is not None:
1001 mglog.debug('Setting inputGenerator file to '+outputDS)
1002 runArgs.inputGeneratorFile=outputDS
1003
1004

◆ modify_config_card()

python.MadGraphUtils.modify_config_card ( config_card_backup = None,
process_dir = MADGRAPH_GRIDPACK_LOCATION,
settings = {},
set_commented = True )
Build a new configuration from an existing one.
This function can get a fresh runcard from DATAPATH or start from the process directory.
Settings is a dictionary of keys (no spaces needed) and values to replace.

Definition at line 1732 of file MadGraphUtils.py.

1732def modify_config_card(config_card_backup=None,process_dir=MADGRAPH_GRIDPACK_LOCATION,settings={},set_commented=True):
1733 """Build a new configuration from an existing one.
1734 This function can get a fresh runcard from DATAPATH or start from the process directory.
1735 Settings is a dictionary of keys (no spaces needed) and values to replace.
1736 """
1737 global my_MGC_instance # noqa: F824
1738
1739 my_MGC_instance.configCardDict.update(settings)
1740 # Check for the default config card location
1741 config_card = my_MGC_instance.config_path
1742
1743 # The format is similar to the run card, but backwards
1744 mglog.info('Modifying config card located at '+config_card)
1745 if config_card_backup is not None:
1746 mglog.info('Keeping backup of original config card at '+config_card_backup)
1747 config_card_old = config_card_backup
1748 else:
1749 config_card_old = config_card+'.old_to_be_deleted'
1750 mglog.debug('Modifying config card settings: '+str(settings))
1751 if os.path.isfile(config_card_old):
1752 os.unlink(config_card_old) # delete old backup
1753 os.rename(config_card, config_card_old) # change name of original card
1754
1755 oldCard = open(config_card_old, 'r')
1756 newCard = open(config_card, 'w')
1757 used_settings = []
1758 for line in iter(oldCard):
1759 lmod = line if set_commented else line.split('#')[0]
1760 if '=' in lmod:
1761 modified = False
1762 for setting in settings:
1763 if setting not in lmod:
1764 continue
1765 # Assume we hit
1766 mglog.info('Setting '+setting.strip()+' to '+str(settings[setting]))
1767 newCard.write(' '+str(setting.strip())+' = '+str(settings[setting])+'\n')
1768 used_settings += [ setting.strip() ]
1769 modified = True
1770 break
1771 if modified:
1772 continue
1773 newCard.write(line)
1774
1775 # Clean up unused options
1776 for asetting in settings:
1777 if asetting in used_settings:
1778 continue
1779 if settings[asetting] is None:
1780 continue
1781 mglog.warning('Option '+asetting+' was not in the default config card. Adding by hand a setting to '+str(settings[asetting]) )
1782 newCard.write(' '+str(asetting)+' = '+str(settings[asetting])+'\n')
1783 # close files
1784 oldCard.close()
1785 newCard.close()
1786 mglog.info('Finished modification of config card.')
1787 if config_card_backup is None:
1788 os.unlink(config_card_old)
1789
1790
1791

◆ modify_run_card()

python.MadGraphUtils.modify_run_card ( run_card_input = None,
run_card_backup = None,
process_dir = MADGRAPH_GRIDPACK_LOCATION,
runArgs = None,
flags = None,
settings = {},
skipBaseFragment = False,
pdf_setting = None )
Build a new run_card.dat from an existing one.
This function can get a fresh runcard from DATAPATH or start from the process directory.
Settings is a dictionary of keys (no spaces needed) and values to replace.

Definition at line 1597 of file MadGraphUtils.py.

1597def modify_run_card(run_card_input=None,run_card_backup=None,process_dir=MADGRAPH_GRIDPACK_LOCATION,runArgs=None,flags=None,settings={},skipBaseFragment=False,pdf_setting=None):
1598 """Build a new run_card.dat from an existing one.
1599 This function can get a fresh runcard from DATAPATH or start from the process directory.
1600 Settings is a dictionary of keys (no spaces needed) and values to replace.
1601 """
1602
1603 global my_MGC_instance # noqa: F824
1604 # my_MGC_instance.getRunCardDict(card_loc=process_dir+'/Cards/run_card.dat')
1605 my_MGC_instance.runCardDict.update(settings)
1606 # my_MGC_instance.write_runCard()
1607
1608 # Operate on lower case settings, and choose the capitalization MG5 has as the default (or all lower case)
1609 settings_lower = {}
1610 for s in list(settings.keys()):
1611 settings_lower[s.lower()] = settings[s]
1612
1613 # Check for the default run card location
1614 if run_card_input is None:
1615 run_card_input=get_default_runcard(process_dir)
1616 elif run_card_input is not None and not os.access(run_card_input,os.R_OK):
1617 runcard = subprocess.Popen(['get_files','-data',run_card_input])
1618 runcard.wait()
1619 if not os.access(run_card_input,os.R_OK):
1620 raise RuntimeError('Could not get run card '+run_card_input)
1621
1622 # guess NLO
1623 isNLO = my_MGC_instance.isNLO
1624 # add gobal PDF and scale uncertainty config to extras, except PDF or weights for syscal config are explictly set
1625 if not skipBaseFragment:
1626 setup_pdf_and_systematic_weights(get_pdf_setting(pdf_setting),settings_lower,isNLO)
1627
1628 # Get some info out of runArgs or flags
1629 if runArgs is not None or flags is not None:
1630 beamEnergy,rand_seed = get_runArgs_info(runArgs=runArgs, flags=flags)
1631 if 'iseed' not in settings_lower:
1632 settings_lower['iseed']=rand_seed
1633 if not isNLO and 'python_seed' not in settings_lower:
1634 settings_lower['python_seed']=rand_seed
1635 if 'beamenergy' in settings_lower:
1636 mglog.warning('Do not set beam energy in MG settings. The variables are ebeam1 and ebeam2. Will use your setting of '+str(settings_lower['beamenergy']))
1637 beamEnergy=settings_lower['beamenergy']
1638 settings_lower.pop('beamenergy')
1639 if 'ebeam1' not in settings_lower:
1640 settings_lower['ebeam1']=beamEnergy
1641 if 'ebeam2' not in settings_lower:
1642 settings_lower['ebeam2']=beamEnergy
1643 # Make sure nevents is an integer
1644 if 'nevents' in settings_lower:
1645 settings_lower['nevents'] = int(settings_lower['nevents'])
1646
1647 # Normalise custom_fcts early so the rewritten run_card uses the full path
1648 if 'custom_fcts' in settings_lower and settings_lower['custom_fcts']:
1649 raw_name = str(settings_lower['custom_fcts']).split()[0]
1650 # Determine jobConfig directory
1651 cfgdir = None
1652 if flags is not None and hasattr(flags, 'Generator') and hasattr(flags.Generator, 'jobConfig') and flags.Generator.jobConfig:
1653 cfgdir = flags.Generator.jobConfig[0] if isinstance(flags.Generator.jobConfig, (list, tuple)) else flags.Generator.jobConfig
1654 elif runArgs is not None and hasattr(runArgs, 'jobConfig'):
1655 cfgdir = runArgs.jobConfig[0] if isinstance(runArgs.jobConfig, (list, tuple)) else runArgs.jobConfig
1656 elif flags is not None and 'JOBOPTSEARCHPATH' in os.environ:
1657 cfgdir = os.environ['JOBOPTSEARCHPATH'].split(':')[0]
1658
1659 if cfgdir:
1660 # Build full path and make absolute
1661 full_path = os.path.join(cfgdir, raw_name)
1662 settings_lower['custom_fcts'] = os.path.abspath(full_path)
1663 mglog.info(f"Using custom function(s), specified in custom_fcts with path: {settings_lower['custom_fcts']}")
1664 else:
1665 # For internal tests, where jobConfig is not set
1666 settings_lower['custom_fcts'] = os.path.abspath(raw_name)
1667
1668 mglog.info('Modifying run card located at '+run_card_input)
1669 if run_card_backup is not None:
1670 mglog.info('Keeping backup of original run card at '+run_card_backup)
1671 run_card_old = run_card_backup
1672 else:
1673 run_card_old = run_card_input+'.old_to_be_deleted'
1674 mglog.debug('Modifying runcard settings: '+str(settings_lower))
1675 if os.path.isfile(run_card_old):
1676 os.unlink(run_card_old) # delete old backup
1677 os.rename(run_card_input, run_card_old) # change name of original card
1678
1679 oldCard = open(run_card_old, 'r')
1680 newCard = open(process_dir+'/Cards/run_card.dat', 'w')
1681 used_settings = []
1682 for line in iter(oldCard):
1683 if not line.strip().startswith('#'): # line commented out
1684 command = line.split('!', 1)[0]
1685 comment = line.split('!', 1)[1] if '!' in line else ''
1686 if '=' in command:
1687 setting = command.split('=')[-1] #.strip()
1688 stripped_setting = setting.strip()
1689 oldValue = '='.join(command.split('=')[:-1])
1690 if stripped_setting.lower() in settings_lower:
1691 # if setting set to 'None' it will be removed from run_card
1692 if settings_lower[stripped_setting.lower()] is None:
1693 line=''
1694 mglog.info('Removing '+stripped_setting+'.')
1695 used_settings += [ stripped_setting.lower() ]
1696 else:
1697 if stripped_setting.lower() == 'custom_fcts':
1698 # Overwrite completely to avoid duplicating in custom_fcts, else MadGraph will crash
1699 line = ' '+str(settings_lower[stripped_setting.lower()])+' = '+setting
1700 if comment != '':
1701 line += ' !'+comment
1702 else:
1703 line = oldValue.replace(oldValue.strip(), str(settings_lower[stripped_setting.lower()]))+'='+setting
1704 if comment != '':
1705 line += ' !' + comment
1706 mglog.info('Setting '+stripped_setting+' = '+str(settings_lower[stripped_setting.lower()]))
1707 used_settings += [ stripped_setting.lower() ]
1708 newCard.write(line.strip()+'\n')
1709
1710 # Check whether mcatnlo_delta is applied to setup pythia8 path
1711 if 'mcatnlo_delta' in settings_lower:
1712 if settings_lower['mcatnlo_delta'] == 'True':
1713 modify_config_card(process_dir=process_dir,settings={'pythia8_path':os.getenv("PY8PATH")})
1714
1715
1716 # Clean up unused options
1717 for asetting in settings_lower:
1718 if asetting in used_settings:
1719 continue
1720 if settings_lower[asetting] is None:
1721 continue
1722 mglog.info('Option '+asetting+' was not in the default run_card (normal for hidden options). Adding by hand a setting to '+str(settings_lower[asetting]) )
1723 newCard.write( ' '+str(settings_lower[asetting])+' = '+str(asetting)+'\n')
1724 # close files
1725 oldCard.close()
1726 newCard.close()
1727 mglog.info('Finished modification of run card.')
1728 if run_card_backup is None:
1729 os.unlink(run_card_old)
1730
1731

◆ new_process()

python.MadGraphUtils.new_process ( process = 'generate p p > t t~\noutput -f',
plugin = None,
keepJpegs = False,
usePMGSettings = False,
pdf_setting = None,
devices = None,
catch_errors = MADGRAPH_CATCH_ERRORS )

Definition at line 75 of file MadGraphUtils.py.

75def new_process(process='generate p p > t t~\noutput -f', plugin=None, keepJpegs=False, usePMGSettings=False, pdf_setting=None, devices=None, catch_errors=MADGRAPH_CATCH_ERRORS):
76 global my_MGC_instance
77 my_MGC_instance = MGControl(
78 process,
79 plugin,
80 keepJpegs,
81 usePMGSettings,
82 pdf_setting=pdf_setting,
83 devices=devices,
84 catch_errors=catch_errors,
85 )
86 return my_MGC_instance.process_dir
87
88

◆ print_cards()

python.MadGraphUtils.print_cards ( proc_card = 'proc_card_mg5.dat',
run_card = None,
param_card = None,
madspin_card = None,
reweight_card = None,
warn_on_missing = True )

Definition at line 1539 of file MadGraphUtils.py.

1539def print_cards(proc_card='proc_card_mg5.dat',run_card=None,param_card=None,madspin_card=None,reweight_card=None,warn_on_missing=True):
1540 if os.access(proc_card,os.R_OK):
1541 mglog.info("proc_card:")
1542 procCard = subprocess.Popen(['cat',proc_card])
1543 procCard.wait()
1544 elif warn_on_missing:
1545 mglog.warning('No proc_card: '+proc_card+' found')
1546
1547 if run_card is not None and os.access(run_card,os.R_OK):
1548 mglog.info("run_card:")
1549 runCard = subprocess.Popen(['cat',run_card])
1550 runCard.wait()
1551 elif run_card is not None and warn_on_missing:
1552 mglog.warning('No run_card: '+run_card+' found')
1553 else:
1554 mglog.info('Default run card in use')
1555
1556 if param_card is not None and os.access(param_card,os.R_OK):
1557 mglog.info("param_card:")
1558 paramCard = subprocess.Popen(['cat',param_card])
1559 paramCard.wait()
1560 elif param_card is not None and warn_on_missing:
1561 mglog.warning('No param_card: '+param_card+' found')
1562 else:
1563 mglog.info('Default param card in use')
1564
1565 if madspin_card is not None and os.access(madspin_card,os.R_OK):
1566 mglog.info("madspin_card:")
1567 madspinCard = subprocess.Popen(['cat',madspin_card])
1568 madspinCard.wait()
1569 elif madspin_card is not None and warn_on_missing:
1570 mglog.warning('No madspin_card: '+madspin_card+' found')
1571 else:
1572 mglog.info('No madspin card in use')
1573
1574 if reweight_card is not None and os.access(reweight_card,os.R_OK):
1575 mglog.info("reweight_card:")
1576 madspinCard = subprocess.Popen(['cat',reweight_card])
1577 madspinCard.wait()
1578 elif reweight_card is not None and warn_on_missing:
1579 mglog.warning('No reweight_card: '+reweight_card+' found')
1580 else:
1581 mglog.info('No reweight card in use')
1582
1583

◆ print_cards_from_dir()

python.MadGraphUtils.print_cards_from_dir ( process_dir = MADGRAPH_GRIDPACK_LOCATION)

Definition at line 1533 of file MadGraphUtils.py.

1533def print_cards_from_dir(process_dir=MADGRAPH_GRIDPACK_LOCATION):
1534 card_dir=process_dir+'/Cards/'
1535 print_cards(proc_card=card_dir+'proc_card_mg5.dat',run_card=card_dir+'run_card.dat',param_card=card_dir+'param_card.dat',\
1536 madspin_card=card_dir+'madspin_card.dat',reweight_card=card_dir+'reweight_card.dat',warn_on_missing=False)
1537
1538

◆ setNCores()

python.MadGraphUtils.setNCores ( process_dir,
Ncores = None )

Definition at line 798 of file MadGraphUtils.py.

798def setNCores(process_dir, Ncores=None):
799 global my_MGC_instance # noqa: F824
800
801 my_Ncores = Ncores
802 my_runMode = 2 if 'ATHENA_CORE_NUMBER' in os.environ else 0
803 if Ncores is None and 'ATHENA_CORE_NUMBER' in os.environ and int(os.environ['ATHENA_CORE_NUMBER'])>0:
804 my_Ncores = int(os.environ['ATHENA_CORE_NUMBER'])
805 my_runMode = 2
806 if my_Ncores is None:
807 mglog.info('Setting up for serial run')
808 my_Ncores = 1
809 my_MGC_instance.configCardDict.update({'nb_core':my_Ncores,'run_mode':my_runMode,'automatic_html_opening':'False'})
810
811
812
813
814
815

◆ setup_bias_module()

python.MadGraphUtils.setup_bias_module ( bias_module,
process_dir )

Definition at line 1334 of file MadGraphUtils.py.

1334def setup_bias_module(bias_module,process_dir):
1335 run_card = process_dir+'/Cards/run_card.dat'
1336 if isinstance(bias_module,tuple):
1337 mglog.info('Using bias module '+bias_module[0])
1338 the_run_card = open(run_card,'r')
1339 for line in the_run_card:
1340 if 'bias_module' in line and not bias_module[0] in line:
1341 raise RuntimeError('You need to add the bias module '+bias_module[0]+' to the run card to actually run it')
1342 the_run_card.close()
1343 if len(bias_module)!=3:
1344 raise RuntimeError('Please give a 3-tuple of strings containing bias module name, bias module, and makefile. Alternatively, give path to bias module tarball.')
1345 bias_module_newpath=process_dir+'/Source/BIAS/'+bias_module[0]
1346 os.makedirs(bias_module_newpath)
1347 bias_module_file=open(bias_module_newpath+'/'+bias_module[0]+'.f','w')
1348 bias_module_file.write(bias_module[1])
1349 bias_module_file.close()
1350 bias_module_make_file=open(bias_module_newpath+'/Makefile','w')
1351 bias_module_make_file.write(bias_module[2])
1352 bias_module_make_file.close()
1353 else:
1354 mglog.info('Using bias module '+bias_module)
1355 bias_module_name=bias_module.split('/')[-1].replace('.gz','')
1356 bias_module_name=bias_module_name.replace('.tar','')
1357 the_run_card = open(run_card,'r')
1358 for line in the_run_card:
1359 if 'bias_module' in line and bias_module_name not in line:
1360 raise RuntimeError('You need to add the bias module '+bias_module_name+' to the run card to actually run it')
1361 the_run_card.close()
1362
1363 if os.path.exists(bias_module+'.tar.gz'):
1364 bias_module_path=bias_module+'.tar.gz'
1365 elif os.path.exists(bias_module+'.gz'):
1366 bias_module_path=bias_module+'.gz'
1367 elif os.path.exists(bias_module):
1368 bias_module_path=bias_module
1369 else:
1370 mglog.error('Did not find bias module '+bias_module+' , this path should point to folder or tarball. Alternatively give a tuple of strings containing module name, module, and makefile')
1371 return 1
1372 bias_module_newpath=process_dir+'/Source/BIAS/'+bias_module_path.split('/')[-1]
1373 mglog.info('Copying bias module into place: '+bias_module_newpath)
1374 shutil.copy(bias_module_path,bias_module_newpath)
1375 mglog.info('Unpacking bias module')
1376 if bias_module_newpath.endswith('.tar.gz'):
1377 untar = stack_subprocess(['tar','xvzf',bias_module_newpath,'--directory='+process_dir+'/Source/BIAS/'])
1378 untar.wait()
1379 elif bias_module_path.endswith('.gz'):
1380 gunzip = stack_subprocess(['gunzip',bias_module_newpath])
1381 gunzip.wait()
1382
1383

◆ setup_pdf_and_systematic_weights()

python.MadGraphUtils.setup_pdf_and_systematic_weights ( the_base_fragment,
extras,
isNLO )

Definition at line 1851 of file MadGraphUtils.py.

1851def setup_pdf_and_systematic_weights(the_base_fragment,extras,isNLO):
1852
1853 global my_MGC_instance # noqa: F824
1854
1855
1856 list = []
1857 tmp_dict = {}
1858 for k in extras:
1859 k_clean=k.lower().replace("'",'').replace('"','')
1860 if k_clean!=k and k_clean in systematics_run_card_options(isNLO):
1861 list.append(k)
1862 tmp_dict[k_clean] = extras[k]
1863 # Removing systematics with incorrect formatting
1864 for o in list:
1865 if o in extras:
1866 extras.pop(o,None)
1867 # Adding cleaned up systematics into dictionary
1868 extras.update(tmp_dict)
1869
1870 if my_MGC_instance.base_fragment_setup_check(the_base_fragment,extras,isNLO):
1871 return
1872 # if something is set that contradicts the base fragment: bad!
1873 for o in systematics_run_card_options(isNLO):
1874 if o in extras:
1875 mglog.warning('You tried to set "'+str(o)+'" by hand, but you should trust the base fragment with the following options: '+', '.join(systematics_run_card_options(isNLO)))
1876 mglog.info('We will update "'+str(o))
1877
1878 new_settings=get_pdf_and_systematic_settings(the_base_fragment,isNLO)
1879
1880 user_set_extras=dict(extras)
1881 for s in new_settings:
1882 if s is not None:
1883 extras[s]=new_settings[s]
1884
1885
1886 mglog.info('PDF and scale settings were set as follows:')
1887 for p in systematics_run_card_options(isNLO):
1888 user_set='not set'
1889 if p in user_set_extras:
1890 user_set=str(user_set_extras[p])
1891 new_value='not set'
1892 if p in extras:
1893 new_value=str(extras[p])
1894 mglog.info('MadGraphUtils set '+str(p)+' to "'+new_value+'", was set to "'+user_set+'"')

◆ setupFastjet()

python.MadGraphUtils.setupFastjet ( process_dir = None)

Definition at line 590 of file MadGraphUtils.py.

590def setupFastjet(process_dir=None):
591 global my_MGC_instance # noqa: F824
592
593 isNLO = my_MGC_instance.isNLO
594
595 mglog.info('Path to fastjet install dir: '+os.environ['FASTJETPATH'])
596 fastjetconfig = os.environ['FASTJETPATH']+'/bin/fastjet-config'
597
598 mglog.info('fastjet-config --version: '+str(subprocess.Popen([fastjetconfig, '--version'],stdout = subprocess.PIPE).stdout.read().strip()))
599 mglog.info('fastjet-config --prefix: '+str(subprocess.Popen([fastjetconfig, '--prefix'],stdout = subprocess.PIPE).stdout.read().strip()))
600
601 if not isNLO:
602 config_card=process_dir+'/Cards/me5_configuration.txt'
603 else:
604 config_card=process_dir+'/Cards/amcatnlo_configuration.txt'
605
606 oldcard = open(config_card,'r')
607 newcard = open(config_card+'.tmp','w')
608
609 for line in oldcard:
610 if 'fastjet = ' in line:
611 newcard.write('fastjet = '+fastjetconfig+'\n')
612 mglog.info('Setting fastjet = '+fastjetconfig+' in '+config_card)
613 else:
614 newcard.write(line)
615 oldcard.close()
616 newcard.close()
617 shutil.move(config_card+'.tmp',config_card)
618
619 return
620

◆ setupLHAPDF()

python.MadGraphUtils.setupLHAPDF ( process_dir = None,
extlhapath = None,
allow_links = True )

Definition at line 651 of file MadGraphUtils.py.

651def setupLHAPDF(process_dir=None, extlhapath=None, allow_links=True):
652 global my_MGC_instance # noqa: F824
653
654 isNLO = my_MGC_instance.isNLO
655
656 origLHAPATH=os.environ['LHAPATH']
657 origLHAPDF_DATA_PATH=os.environ['LHAPDF_DATA_PATH']
658
659 LHAPATH,LHADATAPATH=get_LHAPDF_PATHS()
660
661 pdfname=''
662 pdfid=-999
663
664
665 mydict= my_MGC_instance.runCardDict
666 if mydict["pdlabel"].replace("'","") == 'lhapdf':
667 #Make local LHAPDF dir
668 mglog.info('creating local LHAPDF dir: MGC_LHAPDF/')
669 if os.path.islink('MGC_LHAPDF/'):
670 os.unlink('MGC_LHAPDF/')
671 elif os.path.isdir('MGC_LHAPDF/'):
672 shutil.rmtree('MGC_LHAPDF/')
673
674 newMGCLHA='MGC_LHAPDF/'
675
676 mkdir = subprocess.Popen(['mkdir','-p',newMGCLHA])
677 mkdir.wait()
678
679 pdfs_used=[ int(x) for x in mydict['lhaid'].replace(' ',',').split(',') ]
680 # included systematics pdfs here
681 if 'sys_pdf' in mydict:
682 sys_pdf=mydict['sys_pdf'].replace('&&',' ').split()
683 for s in sys_pdf:
684 if s.isdigit():
685 idx=int(s)
686 if idx>1000: # the sys_pdf syntax is such that small numbers are used to specify the subpdf index
687 pdfs_used.append(idx)
688 else:
689 pdfs_used.append(s)
690 if 'systematics_arguments' in mydict:
691 systematics_arguments=MadGraphControl.MadGraphSystematicsUtils.parse_systematics_arguments(mydict['systematics_arguments'])
692 if 'pdf' in systematics_arguments:
693 sys_pdf=systematics_arguments['pdf'].replace(',',' ').replace('@',' ').split()
694 for s in sys_pdf:
695 if s.isdigit():
696 idx=int(s)
697 if idx>1000: # the sys_pdf syntax is such that small numbers are used to specify the subpdf index
698 pdfs_used.append(idx)
699 else:
700 pdfs_used.append(s)
701 for pdf in pdfs_used:
702 if isinstance(pdf,str) and (pdf.lower()=='errorset' or pdf.lower()=='central'):
703 continue
704 # new function to get both lhapdf id and name
705 pdfid,pdfname=get_lhapdf_id_and_name(pdf)
706 mglog.info("Found LHAPDF ID="+str(pdfid)+", name="+pdfname)
707
708 if not os.path.exists(newMGCLHA+pdfname) and not os.path.lexists(newMGCLHA+pdfname):
709 if not os.path.exists(LHADATAPATH+'/'+pdfname):
710 mglog.warning('PDF not installed at '+LHADATAPATH+'/'+pdfname)
711 if allow_links:
712 mglog.info('linking '+LHADATAPATH+'/'+pdfname+' --> '+newMGCLHA+pdfname)
713 os.symlink(LHADATAPATH+'/'+pdfname,newMGCLHA+pdfname)
714 else:
715 mglog.info('copying '+LHADATAPATH+'/'+pdfname+' --> '+newMGCLHA+pdfname)
716 shutil.copytree(LHADATAPATH+'/'+pdfname,newMGCLHA+pdfname)
717
718 if allow_links:
719 mglog.info('linking '+LHADATAPATH+'/pdfsets.index --> '+newMGCLHA+'pdfsets.index')
720 os.symlink(LHADATAPATH+'/pdfsets.index',newMGCLHA+'pdfsets.index')
721
722 atlasLHADATAPATH=LHADATAPATH.replace('sft.cern.ch/lcg/external/lhapdfsets/current','atlas.cern.ch/repo/sw/Generators/lhapdfsets/current')
723 mglog.info('linking '+atlasLHADATAPATH+'/lhapdf.conf --> '+newMGCLHA+'lhapdf.conf')
724 os.symlink(atlasLHADATAPATH+'/lhapdf.conf',newMGCLHA+'lhapdf.conf')
725 else:
726 mglog.info('copying '+LHADATAPATH+'/pdfsets.index --> '+newMGCLHA+'pdfsets.index')
727 shutil.copy2(LHADATAPATH+'/pdfsets.index',newMGCLHA+'pdfsets.index')
728
729 atlasLHADATAPATH=LHADATAPATH.replace('sft.cern.ch/lcg/external/lhapdfsets/current','atlas.cern.ch/repo/sw/Generators/lhapdfsets/current')
730 mglog.info('copying '+atlasLHADATAPATH+'/lhapdf.conf -->'+newMGCLHA+'lhapdf.conf')
731 shutil.copy2(atlasLHADATAPATH+'/lhapdf.conf',newMGCLHA+'lhapdf.conf')
732
733
734 LHADATAPATH=os.getcwd()+'/MGC_LHAPDF'
735
736 else:
737 mglog.info('Not using LHAPDF')
738 return (LHAPATH,origLHAPATH,origLHAPDF_DATA_PATH)
739
740
741 if isNLO:
742 os.environ['LHAPDF_DATA_PATH']=LHADATAPATH
743
744 mglog.info('Path to LHAPDF install dir: '+LHAPATH)
745 mglog.info('Path to LHAPDF data dir: '+LHADATAPATH)
746 if not os.path.isdir(LHADATAPATH):
747 raise RuntimeError('LHAPDF data dir not accesible: '+LHADATAPATH)
748 if not os.path.isdir(LHAPATH):
749 raise RuntimeError('LHAPDF path dir not accesible: '+LHAPATH)
750
751 # Dealing with LHAPDF
752 if extlhapath:
753 lhapdfconfig=extlhapath
754 if not os.access(lhapdfconfig,os.X_OK):
755 raise RuntimeError('Failed to find valid external lhapdf-config at '+lhapdfconfig)
756 LHADATAPATH=subprocess.Popen([lhapdfconfig, '--datadir'],stdout = subprocess.PIPE).stdout.read().strip()
757 mglog.info('Changing LHAPDF_DATA_PATH to '+LHADATAPATH)
758 os.environ['LHAPDF_DATA_PATH']=LHADATAPATH
759 else:
760 getlhaconfig = subprocess.Popen(['get_files','-data','lhapdf-config'])
761 getlhaconfig.wait()
762 #Get custom lhapdf-config
763 if not os.access(os.getcwd()+'/lhapdf-config',os.X_OK):
764 mglog.error('Failed to get lhapdf-config from MadGraphControl')
765 return 1
766 lhapdfconfig = os.getcwd()+'/lhapdf-config'
767
768 mglog.info('lhapdf-config --version: '+str(subprocess.Popen([lhapdfconfig, '--version'],stdout = subprocess.PIPE).stdout.read().strip()))
769 mglog.info('lhapdf-config --prefix: '+str(subprocess.Popen([lhapdfconfig, '--prefix'],stdout = subprocess.PIPE).stdout.read().strip()))
770 mglog.info('lhapdf-config --libdir: '+str(subprocess.Popen([lhapdfconfig, '--libdir'],stdout = subprocess.PIPE).stdout.read().strip()))
771 mglog.info('lhapdf-config --datadir: '+str(subprocess.Popen([lhapdfconfig, '--datadir'],stdout = subprocess.PIPE).stdout.read().strip()))
772 mglog.info('lhapdf-config --pdfsets-path: '+str(subprocess.Popen([lhapdfconfig, '--pdfsets-path'],stdout = subprocess.PIPE).stdout.read().strip()))
773
774
775 my_MGC_instance.configCardDict.update({'lhapdf':lhapdfconfig,'lhapdf_py3':lhapdfconfig})
776
777 mglog.info('Creating links for LHAPDF')
778 if os.path.islink(process_dir+'/lib/PDFsets'):
779 os.unlink(process_dir+'/lib/PDFsets')
780 elif os.path.isdir(process_dir+'/lib/PDFsets'):
781 shutil.rmtree(process_dir+'/lib/PDFsets')
782 if allow_links:
783 os.symlink(LHADATAPATH,process_dir+'/lib/PDFsets')
784 else:
785 shutil.copytree(LHADATAPATH,process_dir+'/lib/PDFsets')
786 mglog.info('Available PDFs are:')
787 mglog.info( sorted( [ x for x in os.listdir(process_dir+'/lib/PDFsets') if ".tar.gz" not in x ] ) )
788
789 global MADGRAPH_COMMAND_STACK
790 MADGRAPH_COMMAND_STACK += [ '# Copy the LHAPDF files locally' ]
791 MADGRAPH_COMMAND_STACK += [ 'cp -r '+os.getcwd()+'/MGC_LHAPDF .' ]
792 MADGRAPH_COMMAND_STACK += [ 'cp -r '+process_dir+'/lib/PDFsets ${MGaMC_PROCESS_DIR}/lib/' ]
793
794 return (LHAPATH,origLHAPATH,origLHAPDF_DATA_PATH)
795
796
797# Function to set the number of cores and the running mode in the run card

◆ stack_subprocess()

python.MadGraphUtils.stack_subprocess ( command,
** kwargs )

Definition at line 45 of file MadGraphUtils.py.

45def stack_subprocess(command,**kwargs):
46 global MADGRAPH_COMMAND_STACK
47 MADGRAPH_COMMAND_STACK += [' '.join(command)]
48 return subprocess.Popen(command,**kwargs)
49

◆ update_lhe_file()

python.MadGraphUtils.update_lhe_file ( lhe_file_old,
param_card_old = None,
lhe_file_new = None,
masses = {},
delete_old_lhe = True )
Build a new LHE file from an old one and an updated param card.
The masses of some particles can be changed via the masses dictionary.  No particles that appear in the events
may have their masses changed.
If the param card is provided, the decay block in the LHE file will be replaced with the one in the param card.
By default, the old LHE file is removed.
If None is provided as a new LHE file name, the new file will replace the old one.

Definition at line 1426 of file MadGraphUtils.py.

1426def update_lhe_file(lhe_file_old,param_card_old=None,lhe_file_new=None,masses={},delete_old_lhe=True):
1427 """Build a new LHE file from an old one and an updated param card.
1428 The masses of some particles can be changed via the masses dictionary. No particles that appear in the events
1429 may have their masses changed.
1430 If the param card is provided, the decay block in the LHE file will be replaced with the one in the param card.
1431 By default, the old LHE file is removed.
1432 If None is provided as a new LHE file name, the new file will replace the old one."""
1433 # If we want to just use a temp file, then put in a little temp holder
1434 lhe_file_new_tmp = lhe_file_new if lhe_file_new is not None else lhe_file_old+'.tmp'
1435 # Make sure the LHE file is there
1436 if not os.access(lhe_file_old,os.R_OK):
1437 raise RuntimeError('Could not access old LHE file at '+str(lhe_file_old)+'. Please check the file location.')
1438 # Grab the old param card
1439 if param_card_old is not None:
1440 paramcard = subprocess.Popen(['get_files','-data',param_card_old])
1441 paramcard.wait()
1442 if not os.access(param_card_old,os.R_OK):
1443 raise RuntimeError('Could not get param card '+param_card_old)
1444 # Don't overwrite old param cards
1445 if os.access(lhe_file_new_tmp,os.R_OK):
1446 raise RuntimeError('Old file at'+str(lhe_file_new_tmp)+' in the current directory. Dont want to clobber it. Please move it first.')
1447
1448 newlhe = open(lhe_file_new_tmp,'w')
1449 blockName = None
1450 decayEdit = False
1451 eventRead = False
1452 particles_in_events = []
1453 # Decay block ends with </slha>
1454
1455 with open(lhe_file_old,'r') as fileobject:
1456 for line in fileobject:
1457 if decayEdit and '</slha>' not in line:
1458 continue
1459 if decayEdit and '</slha>' in line:
1460 decayEdit = False
1461 if line.strip().upper().startswith('BLOCK') or line.strip().upper().startswith('DECAY')\
1462 and len(line.strip().split()) > 1:
1463 pos = 0 if line.strip().startswith('DECAY') else 1
1464 blockName = line.strip().upper().split()[pos]
1465
1466 akey = None
1467 if blockName != 'DECAY' and len(line.strip().split()) > 0:
1468 akey = line.strip().split()[0]
1469 elif blockName == 'DECAY' and len(line.strip().split()) > 1:
1470 akey = line.strip().split()[1]
1471
1472 # Replace the masses with those in the dictionary
1473 if akey is not None and blockName == 'MASS' and akey in masses:
1474 newlhe.write(' '+akey+' '+str(masses[akey])+' # \n')
1475 mglog.info(' '+akey+' '+str(masses[akey])+' #')
1476 decayEdit = False
1477 continue
1478
1479 # Replace the entire decay section of the LHE file with the one from the param card
1480 if blockName == 'DECAY' and param_card_old is not None:
1481 # We are now reading the decay blocks! Take them from the param card
1482 oldparam = open(param_card_old,'r')
1483 newDecays = False
1484 for old_line in oldparam.readlines():
1485 newBlockName = None
1486 if old_line.strip().upper().startswith('DECAY') and len(old_line.strip().split()) > 1:
1487 newBlockName = line.strip().upper().split()[pos]
1488 if newDecays:
1489 newlhe.write(old_line)
1490 elif newBlockName == 'DECAY':
1491 newDecays = True
1492 newlhe.write(old_line)
1493 oldparam.close()
1494 # Done adding the decays
1495 decayEdit = True
1496 blockName = None
1497 continue
1498
1499 # Keep a record of the particles that are in the events
1500 if not eventRead and '<event>' in line:
1501 eventRead = True
1502 if eventRead:
1503 if len(line.split())==11:
1504 aparticle = line.split()[0]
1505 if aparticle not in particles_in_events:
1506 particles_in_events += [aparticle]
1507
1508 # Otherwise write the line again
1509 newlhe.write(line)
1510
1511 # Check that none of the particles that we were setting the masses of appear in the LHE events
1512 for akey in masses:
1513 if akey in particles_in_events:
1514 mglog.error('Attempted to change mass of a particle that was in an LHE event! This is not allowed!')
1515 return -1
1516
1517 # Close up and return
1518 newlhe.close()
1519
1520 # Move the new file to the old file location
1521 if lhe_file_new is None:
1522 os.remove(lhe_file_old)
1523 shutil.move(lhe_file_new_tmp,lhe_file_old)
1524 lhe_file_new_tmp = lhe_file_old
1525 # Delete the old file if requested
1526 elif delete_old_lhe:
1527 os.remove(lhe_file_old)
1528
1529 return lhe_file_new_tmp
1530
1531
1532
int upper(int c)

Variable Documentation

◆ MADGRAPH_CATCH_ERRORS

bool python.MadGraphUtils.MADGRAPH_CATCH_ERRORS = True

Definition at line 32 of file MadGraphUtils.py.

◆ MADGRAPH_COMMAND_STACK

list python.MadGraphUtils.MADGRAPH_COMMAND_STACK = []

Definition at line 35 of file MadGraphUtils.py.

◆ MADGRAPH_GRIDPACK_LOCATION

str python.MadGraphUtils.MADGRAPH_GRIDPACK_LOCATION = 'madevent'

Definition at line 28 of file MadGraphUtils.py.

◆ MADGRAPH_PDFSETTING

python.MadGraphUtils.MADGRAPH_PDFSETTING = None

Definition at line 34 of file MadGraphUtils.py.

◆ MADGRAPH_RUN_NAME

str python.MadGraphUtils.MADGRAPH_RUN_NAME = 'run_01'

Definition at line 30 of file MadGraphUtils.py.

◆ mglog

python.MadGraphUtils.mglog = Logging.logging.getLogger('MadGraphUtils')

Definition at line 19 of file MadGraphUtils.py.

◆ my_MGC_instance

python.MadGraphUtils.my_MGC_instance = None

Definition at line 20 of file MadGraphUtils.py.

◆ python

str python.MadGraphUtils.python = 'python'

Definition at line 26 of file MadGraphUtils.py.