951def arrange_output(process_dir=MADGRAPH_GRIDPACK_LOCATION,lhe_version=None,saveProcDir=False,runArgs=None,fixEventWeightsForBridgeMode=False):
952
953
954
955
956 if len(glob.glob(os.path.join(process_dir, 'Events','*')))<1:
957 mglog.error('Process dir '+process_dir+' does not contain events?')
958 proc_dir_list = glob.glob(os.path.join(process_dir, 'Events', '*'))
959 this_run_name=None
960
961 for adir in proc_dir_list:
962 if 'decayed' in adir:
963 continue
964 else:
965 if 'GridRun_' in adir:
966 this_run_name=adir
967 break
968 elif os.path.join(process_dir, 'Events',MADGRAPH_RUN_NAME) in adir:
969 this_run_name=adir
970 if not os.access(this_run_name,os.R_OK):
971 raise RuntimeError('Unable to locate run directory')
972
973 hasUnweighted = os.access(this_run_name+'/unweighted_events.lhe.gz',os.R_OK)
974
975 hasRunMadSpin=False
976 madspinDirs=sorted(glob.glob(this_run_name+'_decayed_*/'))
977 if len(madspinDirs):
978 hasRunMadSpin=True
979 if hasRunMadSpin and not hasUnweighted:
980
981 hasUnweighted = os.access(madspinDirs[-1]+'/unweighted_events.lhe.gz',os.R_OK)
982
983 global MADGRAPH_COMMAND_STACK
984 if hasRunMadSpin:
985 if len(madspinDirs):
986 if hasUnweighted:
987
988
989
990
991
992 if os.path.exists(madspinDirs[-1]+'/unweighted_events.lhe.gz'):
993 MADGRAPH_COMMAND_STACK += ['mv '+madspinDirs[-1]+'/unweighted_events.lhe.gz'+' '+this_run_name+'/unweighted_events.lhe.gz']
994 shutil.move(madspinDirs[-1]+'/unweighted_events.lhe.gz',this_run_name+'/unweighted_events.lhe.gz')
995 mglog.info('Moving MadSpin events from '+madspinDirs[-1]+'/unweighted_events.lhe.gz to '+this_run_name+'/unweighted_events.lhe.gz')
996 elif os.path.exists(madspinDirs[-1]+'/events.lhe.gz'):
997 MADGRAPH_COMMAND_STACK += ['mv '+madspinDirs[-1]+'/events.lhe.gz'+' '+this_run_name+'/unweighted_events.lhe.gz']
998 shutil.move(madspinDirs[-1]+'/events.lhe.gz',this_run_name+'/unweighted_events.lhe.gz')
999 mglog.info('Moving MadSpin events from '+madspinDirs[-1]+'/events.lhe.gz to '+this_run_name+'/unweighted_events.lhe.gz')
1000 else:
1001 raise RuntimeError('MadSpin was run but can\'t find files :(')
1002
1003 else:
1004 MADGRAPH_COMMAND_STACK += ['mv '+madspinDirs[-1]+'/events.lhe.gz '+this_run_name+'/events.lhe.gz']
1005 shutil.move(madspinDirs[-1]+'/events.lhe.gz',this_run_name+'/events.lhe.gz')
1006 mglog.info('Moving MadSpin events from '+madspinDirs[-1]+'/events.lhe.gz to '+this_run_name+'/events.lhe.gz')
1007
1008 else:
1009 mglog.error('MadSpin was run but can\'t find output folder '+(this_run_name+'_decayed_1/'))
1010 raise RuntimeError('MadSpin was run but can\'t find output folder '+(this_run_name+'_decayed_1/'))
1011
1012 if fixEventWeightsForBridgeMode:
1013 mglog.info("Fixing event weights after MadSpin... initial checks.")
1014
1015
1016 spinmodenone=False
1017 MGnumevents=-1
1018 MGintweight=-1
1019
1020 if hasUnweighted:
1021 eventsfilename="unweighted_events"
1022 else:
1023 eventsfilename="events"
1024 unzip = stack_subprocess(['gunzip','-f',this_run_name+'/%s.lhe.gz' % eventsfilename])
1025 unzip.wait()
1026
1027 for line in open(process_dir+'/Events/'+MADGRAPH_RUN_NAME+'/%s.lhe'%eventsfilename):
1028 if "Number of Events" in line:
1029 sline=line.split()
1030 MGnumevents=int(sline[-1])
1031 elif "Integrated weight (pb)" in line:
1032 sline=line.split()
1033 MGintweight=float(sline[-1])
1034 elif "set spinmode none" in line:
1035 spinmodenone=True
1036 elif "</header>" in line:
1037 break
1038
1039 if spinmodenone and MGnumevents>0 and MGintweight>0:
1040 mglog.info("Fixing event weights after MadSpin... modifying LHE file.")
1041 newlhe=open(this_run_name+'/%s_fixXS.lhe'%eventsfilename,'w')
1042 initlinecount=0
1043 eventlinecount=0
1044 inInit=False
1045 inEvent=False
1046
1047
1048
1049 event_norm_setting="average"
1050
1051 for line in open(this_run_name+'/%s.lhe'%eventsfilename):
1052
1053 newline=line
1054 if "<init>" in line:
1055 inInit=True
1056 initlinecount=0
1057 elif "</init>" in line:
1058 inInit=False
1059 elif inInit and initlinecount==0:
1060 initlinecount=1
1061
1062 sline=line.split()
1063 if abs(int(sline[-2])) == 3:
1064 event_norm_setting="sum"
1065 elif abs(int(sline[-2])) == 4:
1066 event_norm_setting="average"
1067 elif inInit and initlinecount==1:
1068 sline=line.split()
1069
1070 relunc=float(sline[1])/float(sline[0])
1071 sline[0]=
str(MGintweight)
1072 sline[1]=
str(float(sline[0])*relunc)
1073 if event_norm_setting=="sum":
1074 sline[2]=
str(MGintweight/MGnumevents)
1075 elif event_norm_setting=="average":
1076 sline[2]=
str(MGintweight)
1077 newline=' '.join(sline)
1078 newline+="\n"
1079 initlinecount+=1
1080 elif inInit and initlinecount>1:
1081 initlinecount+=1
1082 elif "<event>" in line:
1083 inEvent=True
1084 eventlinecount=0
1085 elif "</event>" in line:
1086 inEvent=False
1087 elif inEvent and eventlinecount==0:
1088 sline=line.split()
1089
1090 if event_norm_setting=="sum":
1091 sline[2]=
str(MGintweight/MGnumevents)
1092 elif event_norm_setting=="average":
1093 sline[2]=
str(MGintweight)
1094 newline=' '.join(sline)
1095 newline+="\n"
1096 eventlinecount+=1
1097 newlhe.write(newline)
1098 newlhe.close()
1099
1100 mglog.info("Fixing event weights after MadSpin... cleaning up.")
1101 shutil.copyfile(this_run_name+'/%s.lhe' % eventsfilename,
1102 this_run_name+'/%s_badXS.lhe' % eventsfilename)
1103
1104 shutil.move(this_run_name+'/%s_fixXS.lhe' % eventsfilename,
1105 this_run_name+'/%s.lhe' % eventsfilename)
1106
1107 rezip = stack_subprocess(['gzip',this_run_name+'/%s.lhe' % eventsfilename])
1108 rezip.wait()
1109
1110 rezip = stack_subprocess(['gzip',this_run_name+'/%s_badXS.lhe' % eventsfilename])
1111 rezip.wait()
1112
1113
1114 if os.path.exists(os.getcwd()+'/events.lhe'):
1115 os.remove(os.getcwd()+'/events.lhe')
1116
1117 mglog.info('Unzipping generated events.')
1118 if hasUnweighted:
1119 unzip = stack_subprocess(['gunzip','-f',this_run_name+'/unweighted_events.lhe.gz'])
1120 unzip.wait()
1121 else:
1122 unzip = stack_subprocess(['gunzip','-f',this_run_name+'/events.lhe.gz'])
1123 unzip.wait()
1124
1125 mglog.info('Putting a copy in place for the transform.')
1126 if hasUnweighted:
1127 orig_input = this_run_name+'/unweighted_events.lhe'
1128 mod_output = open(os.getcwd()+'/events.lhe','w')
1129 else:
1130 orig_input = this_run_name+'/events.lhe'
1131 mod_output = open(os.getcwd()+'/events.lhe','w')
1132
1133
1134
1135 initrwgt=None
1136 nEmpty=0
1137 lhe_weights=[]
1138 with open(orig_input,'r') as fileobject:
1139 for line in fileobject:
1140 if line.strip():
1141
1142 newline=line
1143 if '#' not in newline:
1144 newline=newline
1145 elif '>' not in newline[ newline.find('#'): ]:
1146 newline=newline
1147 else:
1148 mglog.info('Found bad LHE line with an XML mark in a comment: "'+newline.strip()+'"')
1149 newline=newline[:newline.find(
'#')]+
'#'+ (newline[newline.find(
'#'):].
replace(
'>',
'-'))
1150
1151 if initrwgt is False:
1152 pass
1153 elif "</initrwgt>" in newline:
1154 initrwgt=False
1155 elif "<initrwgt>" in newline:
1156 initrwgt=True
1157 elif initrwgt is not None:
1158 newline=newline.replace('_DYNSCALE-1','')
1159 if '</weight>' in newline:
1160 iend=newline.find('</weight>')
1161 istart=newline[:iend].rfind('>')
1162 lhe_weights+=[newline[istart+1:iend].
strip()]
1163 mod_output.write(newline)
1164 else:
1165 nEmpty=nEmpty+1
1166 mod_output.close()
1167 mglog.info(
'Removed '+
str(nEmpty)+
' empty lines from LHEF')
1168
1169 mglog.info(
"The following "+
str(len(lhe_weights))+
" weights have been written to the LHE file: "+
",".join(lhe_weights))
1170 expected_weights=get_expected_reweight_names(get_reweight_card(process_dir))
1171 expected_weights+=get_expected_systematic_names(MADGRAPH_PDFSETTING)
1172 mglog.info("Checking whether the following expected weights are in LHE file: "+",".join(expected_weights))
1173 for w in expected_weights:
1174 if w not in lhe_weights:
1175 raise RuntimeError("Did not find expected weight "+w+" in lhe file. Did the reweight or systematics module crash?")
1176 mglog.info("Found all required weights!")
1177
1178 if lhe_version:
1179 mod_output2 = open(os.getcwd()+'/events.lhe','r')
1180 test=mod_output2.readline()
1181 if 'version="' in test:
1182 mglog.info('Applying LHE version hack')
1183 final_file = open(os.getcwd()+'/events.lhe.copy','w')
1184 final_file.write('<LesHouchesEvents version="%i.0">\n'%lhe_version)
1185 shutil.copyfileobj(mod_output2, final_file)
1186 final_file.close()
1187 shutil.copy(os.getcwd()+'/events.lhe.copy',os.getcwd()+'/events.lhe')
1188
1189 os.remove(os.getcwd()+'/events.lhe.copy')
1190 mod_output2.close()
1191
1192
1193 if runArgs is None:
1194 raise RuntimeError('Must provide runArgs to arrange_output')
1195
1196 if hasattr(runArgs,'outputTXTFile'):
1197 outputDS = runArgs.outputTXTFile
1198 else:
1199 outputDS = 'tmp_LHE_events.tar.gz'
1200 if hasattr(runArgs, "avoidExtracting") and runArgs.avoidExtracting:
1201 outputDS = 'tmp_LHE_events.gz'
1202
1203 outputStem = outputDS
1204 if '.tar.gz' in outputDS:
1205 outputStem = outputDS.split('.tar.gz')[0]
1206 elif '.tgz' in outputDS:
1207 outputStem = outputDS.split('.tgz')[0]
1208 elif '.gz' in outputDS:
1209 outputStem = outputDS.split('.gz')[0]
1210 else:
1211 mglog.warning(f'Could not figure out what output file type {outputDS} refers to')
1212 outputStem = outputDS.split('.')[0]
1213 outputStem += '.events'
1214
1215 mglog.info('Moving file over to '+outputStem)
1216 shutil.move(os.getcwd()+'/events.lhe',outputStem)
1217
1218 if '.tar.gz' in outputDS or '.tgz' in outputDS:
1219 mglog.info('Re-zipping + tarring into dataset name '+outputDS)
1220 rezip = stack_subprocess(['tar','cvzf',outputDS,outputStem])
1221 rezip.wait()
1222 elif '.gz' in outputDS:
1223 mglog.info('Re-zipping into dataset name '+outputDS)
1224 rezip = stack_subprocess(['gzip',outputStem])
1225 rezip.wait()
1226 shutil.move(outputStem+'.gz',outputDS)
1227 else:
1228 mglog.info(f'Could not understand output type for {outputDS} - will leave uncompressed')
1229
1230 if not saveProcDir:
1231 mglog.info('Removing the process directory')
1232 shutil.rmtree(process_dir,ignore_errors=True)
1233
1234 if os.path.isdir('MGC_LHAPDF/'):
1235 shutil.rmtree('MGC_LHAPDF/',ignore_errors=True)
1236
1237
1238 if hasattr(runArgs,'outputTXTFile') and runArgs.outputTXTFile is not None:
1239 outputDS = outputDS.split('.TXT')[0]
1240
1241 if runArgs is not None:
1242 mglog.debug('Setting inputGenerator file to '+outputDS)
1243 runArgs.inputGeneratorFile=outputDS
1244
1245 mglog.info('All done with output arranging!')
1246 return outputDS
1247
std::string replace(std::string s, const std::string &s2, const std::string &s3)