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

Functions

 get_pdf_and_systematic_settings (the_base_fragment, isNLO, useNLOotf=False)
 setup_pdf_and_systematic_weights (the_base_fragment, extras, isNLO)
 base_fragment_setup_check (the_base_fragment, extras, isNLO)
 convertSysCalcArguments (extras)
 write_systematics_arguments (systematics_arguments)
 parse_systematics_arguments (sys_args)
 parse_systematics_argument (sys_arg)
 systematics_run_card_options (isNLO)

Variables

 mgsyslog = Logging.logging.getLogger('MadGraphSysUtils')
str SYSTEMATICS_WEIGHT_INFO = "MUR%(mur).1f_MUF%(muf).1f_PDF%(pdf)i"
str SYSTEMATICS_WEIGHT_INFO_ALTDYNSCALES = "MUR%(mur).1f_MUF%(muf).1f_DYNSCALE%(dyn)i_PDF%(pdf)i"

Function Documentation

◆ base_fragment_setup_check()

python.MadGraphSystematicsUtils.base_fragment_setup_check ( the_base_fragment,
extras,
isNLO )

Definition at line 214 of file MadGraphSystematicsUtils.py.

214def base_fragment_setup_check(the_base_fragment,extras,isNLO):
215 # no include: allow it (with warning), as long as lhapdf is used
216 # if not (e.g. because no choice was made and the internal pdf ise used): error
217 if the_base_fragment is None:
218 mgsyslog.warning('!!! No pdf base fragment was included in your job options. PDFs should be set with an include file. You might be unable to follow the PDF4LHC uncertainty prescription. Let\'s hope you know what you doing !!!')
219 if not checkSetting('pdlabel','lhapdf',extras) or not checkSettingExists('lhaid',extras):
220 mgsyslog.warning('!!! No pdf base fragment was included in your job options and you did not specify a LHAPDF yourself -- in the future, this will cause an error !!!')
221 #TODO: in the future this should be an error
222 #raise RuntimeError('No pdf base fragment was included in your job options and you did not specify a LHAPDF yourself')
223 return True
224 else:
225 # if setting is already exactly as it should be -- great!
226 correct_settings=get_pdf_and_systematic_settings(the_base_fragment,isNLO)
227
228 allgood=True
229 for s in correct_settings:
230 if s is None and s in extras:
231 allgood=False
232 break
233 if s not in extras or extras[s]!=correct_settings[s]:
234 allgood=False
235 break
236 if allgood:
237 return True
238 # no error but also nothing set
239 return False
240
241#==================================================================================
242# convert settings from the syscalc syntax to the new systematics syntax which is steered via "systematics_arguments"
243# will modify the dict and also return new setting

◆ convertSysCalcArguments()

python.MadGraphSystematicsUtils.convertSysCalcArguments ( extras)

Definition at line 244 of file MadGraphSystematicsUtils.py.

244def convertSysCalcArguments(extras):
245 if 'systematics_program' in extras:
246 if extras['systematics_program'].lower=='none':
247 mgsyslog.warning('no need to convert systematics arguments if systematcs are not run')
248 return
249 if extras['systematics_program'].lower=='syscalc':
250 mgsyslog.warning('systematics already correct for chosen systematics program SysCalc')
251 return
252
253 if 'systematics_arguments' in extras:
254 mgsyslog.warning('systematics_arguments already defined, will be overwritten')
255 systematics_arguments={}
256 systematics_arguments['dyn']='-1'
257 systematics_arguments['mur']='1'
258 systematics_arguments['muf']='1'
259
260 if 'sys_scalefact' in extras:
261 sys_scalefact=extras['sys_scalefact']
262 extras.pop('sys_scalefact')
263 if len(sys_scalefact.split())>0:
264 systematics_arguments['mur']=','.join(sys_scalefact.split())
265 systematics_arguments['muf']=','.join(sys_scalefact.split())
266
267 systematics_arguments['pdf']='central'
268 if 'sys_pdf' in extras:
269 sys_pdf=extras['sys_pdf']
270 extras.pop('sys_pdf')
271 spl=sys_pdf.replace('&&',' ').split()
272 i=0
273 pdfs=[]
274 while i <len(spl):
275 pdfs.append(spl[i])
276 if i+1<len(spl) and spl[i+1].isdigit() and int(spl[i+1])<1000:
277 pdfs[-1]+='@'+str(int(spl[i+1])-1)
278 i+=1
279 i+=1
280 if len(pdfs)>0:
281 systematics_arguments['pdf']=','.join(pdfs)
282 systematics_arguments['weight_info']=SYSTEMATICS_WEIGHT_INFO
283 extras['systematics_arguments']=write_systematics_arguments(systematics_arguments)
284 return extras['systematics_arguments']
285
std::vector< std::string > split(const std::string &s, const std::string &t=":")
Definition hcg.cxx:177

◆ get_pdf_and_systematic_settings()

python.MadGraphSystematicsUtils.get_pdf_and_systematic_settings ( the_base_fragment,
isNLO,
useNLOotf = False )

Definition at line 22 of file MadGraphSystematicsUtils.py.

22def get_pdf_and_systematic_settings(the_base_fragment,isNLO,useNLOotf=False):
23
24 if not isNLO:
25 useNLOotf=False
26
27
28 runcard_settings={}
29 runcard_systematics_arguments={}
30
31 basefragment_settings={}
32 basefragment_settings['central_pdf']=None
33 basefragment_settings['alternative_pdfs']=None
34 basefragment_settings['pdf_variations']=None
35 basefragment_settings['scale_variations']=None
36 basefragment_settings['alternative_dynamic_scales']=None
37 for s in basefragment_settings:
38 if s in the_base_fragment:
39 if isinstance(basefragment_settings[s],list) and len(basefragment_settings[s])==0:
40 continue
41 basefragment_settings[s]=the_base_fragment[s]
42 else:
43 if s=='central_pdf':
44 raise RuntimeError('No "central_pdf" configured in base fragment')
45 if s=='alternative_dynamic_scales':
46 continue
47 mgsyslog.warning('base fragment or pdf settings does not define "'+s+'"')
48
49
50 for s in basefragment_settings:
51 if s=='central_pdf':
52 if not isinstance(basefragment_settings[s],int):
53 raise RuntimeError(s+', configured in base fragment, has to be an integer')
54 continue
55 if basefragment_settings[s] is None:
56 continue
57 if s=='scale_variations':
58 if not isinstance(basefragment_settings[s],list):
59 raise RuntimeError(s+', configured in base fragment, has to be a list of numbers')
60 for pdf in basefragment_settings[s]:
61 if not isinstance(pdf,float) and not isinstance(pdf,int):
62 raise RuntimeError(s+', configured in base fragment, has to be a list of numbers')
63 else:
64 if not isinstance(basefragment_settings[s],list):
65 raise RuntimeError(s+', configured in base fragment, has to be a list of integers')
66 for element in basefragment_settings[s]:
67 if not isinstance(element,int) or not element>0:
68 raise RuntimeError(s+', configured in base fragment, has to be a list of positive integers')
69
70
71 if basefragment_settings['alternative_pdfs'] is not None:
72 # if a PDF set is included as variation (i.e. nominal + error pdf) there is no need to have it as alternative pdf (i.e. only nominal)
73 if basefragment_settings['pdf_variations'] is not None:
74 basefragment_settings['alternative_pdfs']=[ a for a in basefragment_settings['alternative_pdfs'] if a not in basefragment_settings['pdf_variations'] ]
75 # the central pdf does not need to be included as alternative PDF
76 if basefragment_settings['central_pdf'] in basefragment_settings['alternative_pdfs']:
77 basefragment_settings['alternative_pdfs'].remove(basefragment_settings['central_pdf'])
78
79
80 runcard_settings['pdlabel']='lhapdf'
81 runcard_settings['lhaid']=str(basefragment_settings['central_pdf'])
82
83 # turn on LO systematics and use new systematics script
84 if not useNLOotf:
85 if basefragment_settings['alternative_dynamic_scales'] is not None:
86 runcard_systematics_arguments['weight_info']=SYSTEMATICS_WEIGHT_INFO_ALTDYNSCALES
87 else:
88 runcard_systematics_arguments['weight_info']=SYSTEMATICS_WEIGHT_INFO
89 runcard_systematics_arguments['remove_wgts']='".*MUR0.5_MUF2.0.*|.*MUR2.0_MUF0.5.*"'
90
91 runcard_settings['systematics_program']='none'
92 for s in ['alternative_pdfs','pdf_variations','scale_variations']:
93 if basefragment_settings[s] is not None and len(basefragment_settings[s])>0:
94 # set use_syst true if some variations are used
95 if isNLO:
96 runcard_settings['store_rwgt_info']='True'
97 else:
98 runcard_settings['use_syst']='True'
99 # use the MadGraph systematics program != syscalc
100 runcard_settings['systematics_program']='systematics'
101 break
102
103
104 if useNLOotf:
105 # pdf weights with NLO syntax
106 if basefragment_settings['pdf_variations'] is not None and basefragment_settings['central_pdf'] in basefragment_settings['pdf_variations']:
107 runcard_settings['reweight_pdf']='True'
108 else:
109 runcard_settings['reweight_pdf']='False'
110 if basefragment_settings['pdf_variations'] is not None:
111 for v in basefragment_settings['pdf_variations']:
112 if v==basefragment_settings['central_pdf']:
113 continue
114 runcard_settings['lhaid']+=' '+str(v)
115 runcard_settings['reweight_pdf']+=' True'
116 if basefragment_settings['alternative_pdfs'] is not None:
117 for a in basefragment_settings['alternative_pdfs']:
118 runcard_settings['lhaid']+=' '+str(a)
119 runcard_settings['reweight_pdf']+=' False'
120
121 else: #use the new python systematics module
122 sys_pdfs=[]
123 if basefragment_settings['pdf_variations'] is not None:
124 for v in basefragment_settings['pdf_variations']:
125 sys_pdfs.append(get_lhapdf_id_and_name(v)[1])
126 if basefragment_settings['alternative_pdfs'] is not None:
127 for a in basefragment_settings['alternative_pdfs']:
128 sys_pdfs.append(get_lhapdf_id_and_name(a)[1]+'@0')
129 if len(sys_pdfs)>0:
130 runcard_systematics_arguments['pdf']=','.join(sys_pdfs)
131 if isNLO:
132 runcard_settings['reweight_pdf']='False'
133
134
135
136 if basefragment_settings['scale_variations'] is None and isNLO:
137 runcard_settings['reweight_scale']='False'
138 if basefragment_settings['scale_variations'] is not None:
139 if useNLOotf:
140 runcard_settings['reweight_scale']='True'
141 runcard_settings['rw_rscale']=' '.join([str(s) for s in basefragment_settings['scale_variations']])
142 runcard_settings['rw_fscale']=' '.join([str(s) for s in basefragment_settings['scale_variations']])
143 else:
144 runcard_systematics_arguments['muf']=','.join([str(s) for s in basefragment_settings['scale_variations']])
145 runcard_systematics_arguments['mur']=','.join([str(s) for s in basefragment_settings['scale_variations']])
146 runcard_systematics_arguments['dyn']='-1'
147 if isNLO:
148 runcard_settings['reweight_scale']='False'
149
150 if basefragment_settings['alternative_dynamic_scales'] is not None:
151 if useNLOotf:
152 raise RuntimeError('Cannot reweight to alternative dynamic scales using the NLO OTF module')
153 else:
154 runcard_systematics_arguments['dyn']=','.join([str(s) for s in [-1]+basefragment_settings['alternative_dynamic_scales']])
155
156 if not useNLOotf:
157 runcard_settings['systematics_arguments']=write_systematics_arguments(runcard_systematics_arguments)
158
159 return runcard_settings
160
161#==================================================================================
162# this function is called during build_run card to check the consistency of user-provided arguments with the inlude
163# and throw errors, warnings, or corrects the input as is appropriate

◆ parse_systematics_argument()

python.MadGraphSystematicsUtils.parse_systematics_argument ( sys_arg)

Definition at line 303 of file MadGraphSystematicsUtils.py.

303def parse_systematics_argument(sys_arg):
304 spl=sys_arg.strip().split('=')
305 return (spl[0].strip()[2:],spl[1].strip())
306
307#==================================================================================
308# these arguments steer systematics

◆ parse_systematics_arguments()

python.MadGraphSystematicsUtils.parse_systematics_arguments ( sys_args)

Definition at line 291 of file MadGraphSystematicsUtils.py.

291def parse_systematics_arguments(sys_args):
292 parsed={}
293 if sys_args.strip().startswith('['):
294 argument_list = ast.literal_eval(sys_args)
295 for a in argument_list:
296 key,value=parse_systematics_argument(a)
297 parsed[key]=value
298 else:
299 arg=sys_args.replace("'",'').replace('"','')
300 key,value=parse_systematics_argument(arg)
301 return parsed
302
std::string replace(std::string s, const std::string &s2, const std::string &s3)
Definition hcg.cxx:310

◆ setup_pdf_and_systematic_weights()

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

Definition at line 164 of file MadGraphSystematicsUtils.py.

164def setup_pdf_and_systematic_weights(the_base_fragment,extras,isNLO):
165
166
167
168 list = []
169 tmp_dict = {}
170 for k in extras:
171 k_clean=k.lower().replace("'",'').replace('"','')
172 if k_clean!=k and k_clean in systematics_run_card_options(isNLO):
173 list.append(k)
174 tmp_dict[k_clean] = extras[k]
175 # Removing systematics with incorrect formatting
176 for o in list:
177 if o in extras:
178 extras.pop(o,None)
179 # Adding cleaned up systematics into dictionary
180 extras.update(tmp_dict)
181
182 if base_fragment_setup_check(the_base_fragment,extras,isNLO):
183 return
184 # if something is set that contradicts the base fragment: bad!
185 for o in systematics_run_card_options(isNLO):
186 if o in extras:
187 mgsyslog.warning('You tried to set "'+str(o)+'" by hand, but you should trust the base fragment with the following options: '+', '.join(systematics_run_card_options(isNLO)))
188 mgsyslog.info('We will update "'+str(o))
189
190 new_settings=get_pdf_and_systematic_settings(the_base_fragment,isNLO)
191
192 user_set_extras=dict(extras)
193 for s in new_settings:
194 if s is not None:
195 extras[s]=new_settings[s]
196
197
198 mgsyslog.info('PDF and scale settings were set as follows:')
199 for p in systematics_run_card_options(isNLO):
200 user_set='not set'
201 if p in user_set_extras:
202 user_set=str(user_set_extras[p])
203 new_value='not set'
204 if p in extras:
205 new_value=str(extras[p])
206 mgsyslog.info('MadGraphUtils set '+str(p)+' to "'+new_value+'", was set to "'+user_set+'"')
207
208
209#==================================================================================
210# check whether a configuration is in agreement with base fragment
211# true if nothing needs to be done
212# false if still needs setup
213# error if inconsistent config

◆ systematics_run_card_options()

python.MadGraphSystematicsUtils.systematics_run_card_options ( isNLO)

Definition at line 309 of file MadGraphSystematicsUtils.py.

309def systematics_run_card_options(isNLO):
310 if isNLO:
311 return ['pdlabel','lhaid','reweight_pdf','reweight_scale','rw_rscale','rw_fscale','store_rwgt_info','systematics_arguments' ]
312 else:
313 return ['pdlabel','lhaid','use_syst','sys_scalefact','sys_pdf','systematics_arguments']

◆ write_systematics_arguments()

python.MadGraphSystematicsUtils.write_systematics_arguments ( systematics_arguments)

Definition at line 286 of file MadGraphSystematicsUtils.py.

286def write_systematics_arguments(systematics_arguments):
287 return '['+','.join(["'--"+k+"="+systematics_arguments[k]+"'" for k in systematics_arguments])+']'
288
289#==================================================================================
290# create a map from the 'systematics_arguments' run card argument

Variable Documentation

◆ mgsyslog

python.MadGraphSystematicsUtils.mgsyslog = Logging.logging.getLogger('MadGraphSysUtils')

Definition at line 11 of file MadGraphSystematicsUtils.py.

◆ SYSTEMATICS_WEIGHT_INFO

str python.MadGraphSystematicsUtils.SYSTEMATICS_WEIGHT_INFO = "MUR%(mur).1f_MUF%(muf).1f_PDF%(pdf)i"

Definition at line 13 of file MadGraphSystematicsUtils.py.

◆ SYSTEMATICS_WEIGHT_INFO_ALTDYNSCALES

str python.MadGraphSystematicsUtils.SYSTEMATICS_WEIGHT_INFO_ALTDYNSCALES = "MUR%(mur).1f_MUF%(muf).1f_DYNSCALE%(dyn)i_PDF%(pdf)i"

Definition at line 14 of file MadGraphSystematicsUtils.py.