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
list MADGRAPH_COMMAND_STACK = []

Function Documentation

◆ _should_catch_errors()

python.MadGraphUtils._should_catch_errors ( )
protected

Definition at line 87 of file MadGraphUtils.py.

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

◆ _write_run_card()

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

Definition at line 93 of file MadGraphUtils.py.

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

◆ 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 820 of file MadGraphUtils.py.

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

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

◆ add_reweighting()

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

Definition at line 1799 of file MadGraphUtils.py.

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

◆ 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 1002 of file MadGraphUtils.py.

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

1387def check_reweight_card(process_dir=MADGRAPH_GRIDPACK_LOCATION):
1388 reweight_card=get_reweight_card(process_dir=process_dir)
1389 shutil.move(reweight_card,reweight_card+'.old')
1390 oldcard = open(reweight_card+'.old','r')
1391 newcard = open(reweight_card,'w')
1392 changed = False
1393 info_expression=r'launch.*--rwgt_info\s*=\s*(\S+).*'
1394 name_expression=info_expression.replace('info','name')
1395 goodname_expression=r'^[A-Za-z0-9_\-.]+$'
1396 for line in oldcard:
1397 # we are only interested in the 'launch' line
1398 if not line.strip().startswith('launch') :
1399 newcard.write(line)
1400 else:
1401 rwgt_name_match=re.match(name_expression,line.strip())
1402 rwgt_info_match=re.match(info_expression,line.strip())
1403 if rwgt_name_match is None and rwgt_info_match is None:
1404 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)
1405 for match in [rwgt_info_match,rwgt_name_match]:
1406 if match is None:
1407 continue
1408 if len(match.groups())!=1:
1409 raise RuntimeError('Unexpected format of reweight card in line: '+line)
1410 if not re.match(goodname_expression,match.group(1)):
1411 raise RuntimeError('No special character in reweighting info/name, only allowing '+goodname_expression)
1412 if rwgt_info_match is not None:
1413 newcard.write(line)
1414 elif rwgt_name_match is not None:
1415 newcard.write(line.strip()+' --rwgt_info={0}\n'.format(rwgt_name_match.group(1)))
1416 changed=True
1417 if changed:
1418 mglog.info('Updated reweight_card')
1419 newcard.close()
1420 oldcard.close()
1421
1422

◆ fix_fks_makefile()

python.MadGraphUtils.fix_fks_makefile ( process_dir)

Definition at line 1821 of file MadGraphUtils.py.

1821def fix_fks_makefile(process_dir):
1822 makefile_fks=process_dir+'/SubProcesses/makefile_fks_dir'
1823 mglog.info('Fixing '+makefile_fks)
1824 shutil.move(makefile_fks,makefile_fks+'_orig')
1825 fin=open(makefile_fks+'_orig')
1826 fout=open(makefile_fks,'w')
1827 edit=False
1828 for line in fin:
1829 if 'FKSParams.mod' in line:
1830 fout.write(line.replace('FKSParams.mod','FKSParams.o'))
1831 edit=True
1832 elif edit and 'driver_mintFO' in line:
1833 fout.write('driver_mintFO.o: weight_lines.o mint_module.o FKSParams.o\n')
1834 elif edit and 'genps_fks.o' in line:
1835 fout.write('genps_fks.o: mint_module.o FKSParams.o\n')
1836 elif edit and 'test_soft_col_limits' in line:
1837 fout.write(line)
1838 fout.write('madfks_plot.o: mint_module.o\n')
1839 fout.write('cluster.o: weight_lines.o\n')
1840 else:
1841 fout.write(line)
1842 fin.close()
1843 fout.close()
1844
1845#==================================================================================
1846# this function is called during build_run card to check the consistency of user-provided arguments with the inlude
1847# 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 131 of file MadGraphUtils.py.

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

◆ 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 382 of file MadGraphUtils.py.

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

◆ generate_prep()

python.MadGraphUtils.generate_prep ( process_dir)

Definition at line 48 of file MadGraphUtils.py.

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

◆ get_cluster_type()

python.MadGraphUtils.get_cluster_type ( )

Definition at line 1789 of file MadGraphUtils.py.

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

◆ 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 110 of file MadGraphUtils.py.

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

◆ get_expected_reweight_names()

python.MadGraphUtils.get_expected_reweight_names ( reweight_card_loc)

Definition at line 1297 of file MadGraphUtils.py.

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

◆ get_expected_systematic_names()

python.MadGraphUtils.get_expected_systematic_names ( syst_setting)

Definition at line 1313 of file MadGraphUtils.py.

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

◆ get_mg5_executable()

python.MadGraphUtils.get_mg5_executable ( )

Definition at line 813 of file MadGraphUtils.py.

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

◆ 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 637 of file MadGraphUtils.py.

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

◆ get_pdf_setting()

python.MadGraphUtils.get_pdf_setting ( pdf_setting = None)

Definition at line 103 of file MadGraphUtils.py.

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

◆ get_reweight_card()

python.MadGraphUtils.get_reweight_card ( process_dir = MADGRAPH_GRIDPACK_LOCATION)

Definition at line 1381 of file MadGraphUtils.py.

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

◆ 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 618 of file MadGraphUtils.py.

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

◆ 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 1581 of file MadGraphUtils.py.

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

◆ ls_dir()

python.MadGraphUtils.ls_dir ( directory)

Definition at line 1813 of file MadGraphUtils.py.

1813def ls_dir(directory):
1814 mglog.info('For your information, ls of '+directory+':')
1815 mglog.info( sorted( os.listdir( directory ) ) )
1816
1817# 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 915 of file MadGraphUtils.py.

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

◆ 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 1729 of file MadGraphUtils.py.

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

◆ 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 1594 of file MadGraphUtils.py.

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

◆ 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 73 of file MadGraphUtils.py.

73def 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):
74 global my_MGC_instance
75 my_MGC_instance = MGControl(
76 process,
77 plugin,
78 keepJpegs,
79 usePMGSettings,
80 pdf_setting=pdf_setting,
81 devices=devices,
82 catch_errors=catch_errors,
83 )
84 return my_MGC_instance.process_dir
85
86

◆ 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 1536 of file MadGraphUtils.py.

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

◆ print_cards_from_dir()

python.MadGraphUtils.print_cards_from_dir ( process_dir = MADGRAPH_GRIDPACK_LOCATION)

Definition at line 1530 of file MadGraphUtils.py.

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

◆ setNCores()

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

Definition at line 795 of file MadGraphUtils.py.

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

◆ setup_bias_module()

python.MadGraphUtils.setup_bias_module ( bias_module,
process_dir )

Definition at line 1331 of file MadGraphUtils.py.

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

◆ setup_pdf_and_systematic_weights()

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

Definition at line 1848 of file MadGraphUtils.py.

1848def setup_pdf_and_systematic_weights(the_base_fragment,extras,isNLO):
1849
1850 global my_MGC_instance # noqa: F824
1851
1852
1853 list = []
1854 tmp_dict = {}
1855 for k in extras:
1856 k_clean=k.lower().replace("'",'').replace('"','')
1857 if k_clean!=k and k_clean in systematics_run_card_options(isNLO):
1858 list.append(k)
1859 tmp_dict[k_clean] = extras[k]
1860 # Removing systematics with incorrect formatting
1861 for o in list:
1862 if o in extras:
1863 extras.pop(o,None)
1864 # Adding cleaned up systematics into dictionary
1865 extras.update(tmp_dict)
1866
1867 if my_MGC_instance.base_fragment_setup_check(the_base_fragment,extras,isNLO):
1868 return
1869
1870 new_settings=get_pdf_and_systematic_settings(the_base_fragment,isNLO)
1871
1872 user_set_extras=dict(extras)
1873 for s in new_settings:
1874 if s is not None:
1875 extras[s]=new_settings[s]
1876
1877
1878 mglog.info('PDF and scale settings were set as follows:')
1879 for p in systematics_run_card_options(isNLO):
1880 user_set='not set'
1881 if p in user_set_extras:
1882 user_set=str(user_set_extras[p])
1883 new_value='not set'
1884 if p in extras:
1885 new_value=str(extras[p])
1886 mglog.info('MadGraphUtils set '+str(p)+' to "'+new_value+'", was set to "'+user_set+'"')

◆ setupFastjet()

python.MadGraphUtils.setupFastjet ( process_dir = None)

Definition at line 587 of file MadGraphUtils.py.

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

◆ setupLHAPDF()

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

Definition at line 648 of file MadGraphUtils.py.

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

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

◆ 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 1423 of file MadGraphUtils.py.

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

◆ MADGRAPH_GRIDPACK_LOCATION

str python.MadGraphUtils.MADGRAPH_GRIDPACK_LOCATION = 'madevent'

Definition at line 28 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.