ATLAS Offline Software
Loading...
Searching...
No Matches
JetUncertaintiesConfig.py
Go to the documentation of this file.
1# Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
2
3
4
5# AnaAlgorithm import(s):
6from AnalysisAlgorithmsConfig.ConfigBlock import ConfigBlock
7from AnalysisAlgorithmsConfig.ConfigAccumulator import (
8 DataType, JetUncertaintyWarning)
9from AthenaConfiguration.Enums import LHCPeriod
10import re
11import warnings
12
13
14class JetUncertaintiesConfig (ConfigBlock) :
15 """the ConfigBlock for the common preprocessing of jet uncertainties"""
16
17 def __init__ (self) :
18 super (JetUncertaintiesConfig, self).__init__ ()
19 self.setBlockName('Uncertainties')
20 self.addOption ('containerName', '', type=str,
21 noneAction='error',
22 info="the name of the output container after calibration.")
23 self.addOption ('jetInput', '', type=str,
24 noneAction='error',
25 info="the type of jet input. Refer to the corresponding small- or large-R jet options.")
26 self.addOption('analysisJetSelection', '', type=str,
27 info="the jet selection to use when calculating the jet multiplicity for an analysis specific "
28 "jet flavor composition uncertainty. Of the form `jvt_selection,as_char&&passesOR,as_char...`.")
29 self.addOption('analysisFile', '', type=str,
30 info="the file containing gluon fraction histograms needed to calculate an analysis specific "
31 "jet flavor composition uncertainty.")
32 self.addOption ('largeRMass', "Comb", type=str,
33 info="the large-R mass definition to use. Supported options are: `Comb`, `Calo`, `TA`.")
34 self.addOption ('systematicsModelJES', "Category", type=str,
35 info="the NP reduction scheme to use for JES: `All`, `Global`, `Category`, "
36 "`Scenario`.")
37 self.addOption ('systematicsModelJER', "Full", type=str,
38 info="the NP reduction scheme to use for JER: `All`, `Full`, `Simple`.")
39 self.addOption ('systematicsModelJMS', "Full", type=str,
40 info="the NP reduction scheme to use for JMS: `Full`, `Simple`.")
41 self.addOption ('runJERsystematicsOnData', False, type=bool,
42 info="whether to run the `All`/`Full` JER model variations also on data samples.",
43 expertMode=True)
44 # Uncertainties tool options
45 self.addOption ('uncertToolConfigPath', None, type=str,
46 info="name of the config file to use for the jet uncertainty "
47 "tool. Expert option to override JetETmiss recommendations.")
48 self.addOption ('uncertToolCalibArea', None, type=str,
49 info="name of the CVMFS area to use for the jet uncertainty "
50 "tool. Expert option to override JetETmiss recommendations.")
51 self.addOption ('uncertToolMCType', None, type=str,
52 info="data type to use for the jet uncertainty tool (e.g. "
53 "`AF3` or `MC16`). Expert option to override JetETmiss "
54 "recommendations.")
55
56 def instanceName (self) :
57 """Return the instance name for this block"""
58 return self.containerName
59
61
62 # Retrieve appropriate JES/JER recommendations for the JetUncertaintiesTool.
63 # We do this separately from the tool declaration, as we may need to set uo
64 # two such tools, but they have to be private.
65
66 # Config file:
67 config_file = None
68 if self.systematicsModelJES == "All" and self.systematicsModelJER == "All":
69 config_file = "R4_AllNuisanceParameters_AllJERNP.config"
70 elif "Scenario" in self.systematicsModelJES:
71 if self.systematicsModelJER != "Simple":
72 raise ValueError(
73 "Invalid uncertainty configuration - Scenario* systematicsModelJESs can "
74 "only be used together with the Simple systematicsModelJER")
75 config_file = "R4_{0}_SimpleJER.config".format(self.systematicsModelJES)
76 elif self.systematicsModelJES in ["Global", "Category"] and self.systematicsModelJER in ["Simple", "Full"]:
77 config_file = "R4_{0}Reduction_{1}JER.config".format(self.systematicsModelJES, self.systematicsModelJER)
78 else:
79 raise ValueError(
80 "Invalid combination of systematicsModelJES and systematicsModelJER settings: "
81 "systematicsModelJES: {0}, systematicsModelJER: {1}".format(self.systematicsModelJES, self.systematicsModelJER) )
82
83 # Calibration area:
84 calib_area = None
85 if self.uncertToolCalibArea is not None:
86 calib_area = self.uncertToolCalibArea
87
88 # Expert override for config path:
89 if self.uncertToolConfigPath is not None:
90 config_file = self.uncertToolConfigPath
91 else:
92 if config.geometry() is LHCPeriod.Run2:
93 if config.dataType() is DataType.FastSim:
94 config_file = "rel22/Fall2024_PreRec/" + config_file
95 else:
96 if self.jetInput == "HI":
97 config_file = "HIJetUncertainties/Spring2023/HI" + config_file
98 else:
99 config_file = "rel22/Summer2023_PreRec/" + config_file
100 else:
101 if config.dataType() is DataType.FastSim:
102 config_file = "rel22/Winter2025_AF3_PreRec/" + config_file
103 else:
104 if self.jetInput == "HI":
105 config_file = "HIJetUncertainties/Spring2023/HI" + config_file
106 else:
107 config_file = "rel22/Winter2025_PreRec/" + config_file
108
109 # MC type:
110 mc_type = None
111 if self.uncertToolMCType is not None:
112 mc_type = self.uncertToolMCType
113 else:
114 if config.geometry() is LHCPeriod.Run2:
115 if config.dataType() is DataType.FastSim:
116 mc_type = "AF3"
117 else:
118 mc_type = "MC20"
119 else:
120 if config.dataType() is DataType.FastSim:
121 mc_type = "MC23AF3"
122 else:
123 if self.jetInput == "HI":
124 mc_type = "MC16"
125 else:
126 mc_type = "MC23"
127
128 return config_file, calib_area, mc_type
129
130
131 def createUncertaintyToolSmallRJets(self, jetUncertaintiesAlg, config, jetCollectionName, doPseudoData=False):
132
133 # Create an instance of JetUncertaintiesTool, following JetETmiss recommendations.
134 # To run Jet Energy Resolution (JER) uncertainties in the "Full" or "All" schemes,
135 # we need two sets of tools: one configured as normal (MC), the other with the
136 # exact same settings but pretending to run on data (pseudo-data).
137 # This is achieved by passing "isPseudoData=True" to the arguments.
138
139 # Retrieve the common configuration settings
140 configFile, calibArea, mcType = self.getUncertaintyToolSettingsSmallRJets(config)
141
142 # The main tool for all JES+JER combinations
143 config.addPrivateTool( 'uncertaintiesTool', 'JetUncertaintiesTool' )
144 jetUncertaintiesAlg.uncertaintiesTool.JetDefinition = jetCollectionName[:-4]
145 jetUncertaintiesAlg.uncertaintiesTool.ConfigFile = configFile
146 if calibArea is not None:
147 jetUncertaintiesAlg.uncertaintiesTool.CalibArea = calibArea
148 jetUncertaintiesAlg.uncertaintiesTool.MCType = mcType
149 jetUncertaintiesAlg.uncertaintiesTool.IsData = (config.dataType() is DataType.Data)
150 jetUncertaintiesAlg.uncertaintiesTool.PseudoDataJERsmearingMode = False
151
152 if config.dataType() is DataType.Data and not (doPseudoData and self.runJERsystematicsOnData):
153 # we don't want any systematics on data if we're not using the right JER model!
154 jetUncertaintiesAlg.affectingSystematicsFilter = '.*'
155 if config.dataType() is not DataType.Data and doPseudoData and not self.runJERsystematicsOnData:
156 # The secondary tool for pseudo-data JER smearing
157 config.addPrivateTool( 'uncertaintiesToolPD', 'JetUncertaintiesTool' )
158 jetUncertaintiesAlg.uncertaintiesToolPD.JetDefinition = jetCollectionName[:-4]
159 jetUncertaintiesAlg.uncertaintiesToolPD.ConfigFile = configFile
160 if calibArea is not None:
161 jetUncertaintiesAlg.uncertaintiesToolPD.CalibArea = calibArea
162 jetUncertaintiesAlg.uncertaintiesToolPD.MCType = mcType
163
164 # This is the part that is different!
165 jetUncertaintiesAlg.uncertaintiesToolPD.IsData = True
166 jetUncertaintiesAlg.uncertaintiesToolPD.PseudoDataJERsmearingMode = True
167
169 # Retrieve appropriate JES/JER recommendations for the JetUncertaintiesTool.
170 # We do this separately from the tool declaration, as we may need to set uo
171 # two such tools, but they have to be private.
172
173 # Config file:
174 config_file = None
175 if self.systematicsModelJER in ["Simple", "Full"] and self.systematicsModelJMS in ["Simple", "Full"]:
176 config_file = "R10_CategoryJES_{0}JER_{1}JMS.config".format(self.systematicsModelJER, self.systematicsModelJMS)
177 else:
178 raise ValueError(
179 "Invalid request for systematicsModelJER/JMS settings: "
180 "systematicsModelJER = '{0}', "
181 "systematicsModelJMS = '{1}'".format(self.systematicsModelJER, self.systematicsModelJMS) )
182 if self.uncertToolConfigPath is not None:
183 # Expert override
184 config_file = self.uncertToolConfigPath
185 else:
186 if config.geometry() in [LHCPeriod.Run2, LHCPeriod.Run3]:
187 config_file = "rel22/Summer2025_PreRec/" + config_file
188 else:
189 warnings.warn_explicit(
190 "Uncertainties for UFO jets are not for Run 4!",
191 JetUncertaintyWarning, filename='', lineno=0)
192
193 # Calibration area:
194 calib_area = None
195 if self.uncertToolCalibArea is not None:
196 calib_area = self.uncertToolCalibArea
197
198 # MC type:
199 if self.uncertToolMCType is not None:
200 mc_type = self.uncertToolMCType
201 else:
202 if config.dataType() is DataType.FastSim:
203 if config.geometry() is LHCPeriod.Run2:
204 mc_type = "MC20AF3"
205 else:
206 mc_type = "MC23AF3"
207 else:
208 if config.geometry() is LHCPeriod.Run2:
209 mc_type = "MC20"
210 else:
211 mc_type = "MC23"
212
213 return config_file, calib_area, mc_type
214
215 def createUncertaintyToolLargeRJets(self, jetUncertaintiesAlg, config, jetCollectionName, doPseudoData=False):
216 '''
217 Create instance(s) of JetUncertaintiesTool following JetETmiss recommendations.
218
219 JER uncertainties under the "Full" scheme must be run on MC samples twice:
220 1. Normal (MC) mode,
221 2. Pseudodata (PD) mode, as if the events are Data.
222 '''
223
224 # Retrieve the common configuration settings
225 configFile, calibArea, mcType = self.getUncertaintyToolSettingsLargeRJets(config)
226
227 # The main tool for all JER combinations
228 config.addPrivateTool( 'uncertaintiesTool', 'JetUncertaintiesTool' )
229 jetUncertaintiesAlg.uncertaintiesTool.JetDefinition = jetCollectionName[:-4]
230 jetUncertaintiesAlg.uncertaintiesTool.ConfigFile = configFile
231 from PathResolver import PathResolver
233 jetUncertaintiesAlg.uncertaintiesTool.AnalysisFile = PathResolver.FindCalibFile(self.analysisFile)
234 if calibArea is not None:
235 jetUncertaintiesAlg.uncertaintiesTool.CalibArea = calibArea
236 jetUncertaintiesAlg.uncertaintiesTool.MCType = mcType
237 jetUncertaintiesAlg.uncertaintiesTool.IsData = (config.dataType() is DataType.Data)
238 jetUncertaintiesAlg.uncertaintiesTool.PseudoDataJERsmearingMode = False
239 jetUncertaintiesAlg.uncertaintiesTool.NJetAccessorName = "Njet_NOSYS"
240
241 # JER smearing on data
242 if config.dataType() is DataType.Data and not (config.isPhyslite() and doPseudoData and self.runJERsystematicsOnData):
243 # we don't want any systematics on data if we're not using the right JER model!
244 jetUncertaintiesAlg.affectingSystematicsFilter = '.*'
245
246 if config.dataType() is not (DataType.Data and config.isPhyslite()) and doPseudoData and not self.runJERsystematicsOnData:
247 # The secondary tool for pseudo-data JER smearing
248 config.addPrivateTool( 'uncertaintiesToolPD', 'JetUncertaintiesTool' )
249 jetUncertaintiesAlg.uncertaintiesToolPD.JetDefinition = jetCollectionName[:-4]
250 jetUncertaintiesAlg.uncertaintiesToolPD.ConfigFile = configFile
251 if calibArea is not None:
252 jetUncertaintiesAlg.uncertaintiesToolPD.CalibArea = calibArea
253 jetUncertaintiesAlg.uncertaintiesToolPD.MCType = mcType
254 jetUncertaintiesAlg.uncertaintiesToolPD.IsData = True
255 jetUncertaintiesAlg.uncertaintiesToolPD.PseudoDataJERsmearingMode = True
256
257 def makeAlgs (self, config) :
258
259 jetCollectionName=config.originalName(self.containerName)
260 if(config.originalName(self.containerName)=="AnalysisJets") :
261 jetCollectionName="AntiKt4EMPFlowJets"
262 if(config.originalName(self.containerName)=="AnalysisLargeRJets") :
263 jetCollectionName="AntiKt10UFOCSSKSoftDropBeta100Zcut10Jets"
264
265 # interpret the jet collection
266 collection_pattern = re.compile(
267 r"AntiKt(\d+)(EMTopo|EMPFlow|LCTopo|TrackCaloCluster|UFO|Track|HI)(TrimmedPtFrac5SmallR20|CSSKSoftDropBeta100Zcut10)?Jets")
268 match = collection_pattern.match(jetCollectionName)
269 if not match:
270 raise ValueError(
271 "Jet collection {0} does not match expected pattern!".format(jetCollectionName) )
272 radius = int(match.group(1) )
273 if radius not in [2, 4, 6, 10]:
274 raise ValueError("Jet collection has an unsupported radius '{0}'!".format(radius) )
275
276 if (self.analysisJetSelection!= ''):
277 alg = config.createAlgorithm( 'CP::NJetDecoratorAlg', 'NJetDecoratorAlg' )
278 alg.jets = config.readName(self.containerName)
279 alg.jetSelection = self.analysisJetSelection
280 config.addOutputVar('EventInfo', 'Njet_%SYS%', 'Njet')
281
282 # Jet uncertainties
283 if (radius == 4):
284 alg = config.createAlgorithm( 'CP::JetUncertaintiesAlg', 'JetUncertaintiesAlg' )
285 self.createUncertaintyToolSmallRJets(alg, config, jetCollectionName, doPseudoData=( self.systematicsModelJER in ["Full","All"] ))
286 alg.jets = config.readName (self.containerName)
287 alg.jetsOut = config.copyName (self.containerName)
288 alg.preselection = config.getPreselection (self.containerName, '')
289
290 # Additional decorations
291 alg = config.createAlgorithm( 'CP::AsgEnergyDecoratorAlg', 'AsgEnergyDecoratorAlg' )
292 alg.particles = config.readName (self.containerName)
293
294 config.addOutputVar (self.containerName, 'e_%SYS%', 'e')
295
296 elif (radius == 10):
297 if self.jetInput == "UFO" and config.dataType() in [DataType.FullSim, DataType.FastSim]:
298 alg = config.createAlgorithm( 'CP::JetUncertaintiesAlg', 'JetUncertaintiesAlg' )
299 self.createUncertaintyToolLargeRJets(alg, config, jetCollectionName, doPseudoData=( self.systematicsModelJER in ["Full","All"] ))
300
301 alg.uncertaintiesTool.JetDefinition = jetCollectionName[:-4]
302
303 # R=1.0 jets have a validity range
304 alg.outOfValidity = 2 # SILENT
305 alg.outOfValidityDeco = 'outOfValidity'
306
307 alg.jets = config.readName (self.containerName)
308 alg.jetsOut = config.copyName (self.containerName)
309 alg.preselection = config.getPreselection (self.containerName, '')
310
311 if self.jetInput != "UFO":
312 alg = config.createAlgorithm( 'CP::JetUncertaintiesAlg', 'JetUncertaintiesAlg' )
313
314 # R=1.0 jets have a validity range
315 alg.outOfValidity = 2 # SILENT
316 alg.outOfValidityDeco = 'outOfValidity'
317 config.addPrivateTool( 'uncertaintiesTool', 'JetUncertaintiesTool' )
318
319 alg.uncertaintiesTool.JetDefinition = jetCollectionName[:-4]
320 alg.uncertaintiesTool.ConfigFile = \
321 "rel21/Moriond2018/R10_{0}Mass_all.config".format(self.largeRMass)
322 alg.uncertaintiesTool.MCType = "MC16a"
323 alg.uncertaintiesTool.IsData = (config.dataType() is DataType.Data)
324
325 alg.jets = config.readName (self.containerName)
326 alg.jetsOut = config.copyName (self.containerName)
327 alg.preselection = config.getPreselection (self.containerName, '')
328 config.addSelection (self.containerName, '', 'outOfValidity')
329
if(pathvar)
static std::string FindCalibFile(const std::string &logical_file_name)
createUncertaintyToolLargeRJets(self, jetUncertaintiesAlg, config, jetCollectionName, doPseudoData=False)
createUncertaintyToolSmallRJets(self, jetUncertaintiesAlg, config, jetCollectionName, doPseudoData=False)