ATLAS Offline Software
Loading...
Searching...
No Matches
AsgAnalysisConfig.py
Go to the documentation of this file.
1# Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
2
3# AnaAlgorithm import(s):
4from AnalysisAlgorithmsConfig.ConfigBlock import ConfigBlock
5from AnalysisAlgorithmsConfig.ConfigSequence import groupBlocks
6from AthenaConfiguration.Enums import LHCPeriod
7from AnalysisAlgorithmsConfig.ConfigAccumulator import (
8 DataType, ExpertModeWarning,
9 Run4FallbackWarning, GeneratorWeightWarning)
10from enum import Enum
11import warnings
12
13try:
14 from AthenaCommon.Logging import logging
15except ImportError:
16 import logging
17
19 JETS = {'JET_'}
20 JER = {'JET_JER'}
21 FTAG = {'FT_'}
22 ELECTRONS = {'EG_', 'EL_'}
23 MUONS = {'MUON_'}
24 PHOTONS = {'EG_', 'PH_'}
25 TAUS = {'TAUS_'}
26 MET = {'MET_'}
27 TRACKS = {'TRK_'}
28 GENERATOR = {'GEN_'}
29 PRW = {'PRW_'}
30 EVENT = {'GEN_', 'PRW_'}
31
32class CommonServicesConfig (ConfigBlock) :
33 """the ConfigBlock for common services
34
35 The idea here is that all algorithms need some common services, and I should
36 provide configuration blocks for those. For now there is just a single
37 block, but in the future I might break out e.g. the systematics service.
38 """
39
40 def __init__ (self) :
41 super (CommonServicesConfig, self).__init__ ()
42 self.addOption ('runSystematics', None, type=bool,
43 info="whether to turn on the computation of systematic variations. "
44 "The default is to run them on MC.")
45 self.addOption ('filterSystematics', None, type=str,
46 info="a regexp string against which the systematics names will be "
47 "matched. Only positive matches are retained and used in the evaluation "
48 "of the various algorithms.")
49 self.addOption ('onlySystematicsCategories', None, type=list,
50 info="a list of strings defining categories of systematics to enable "
51 "(only recommended for studies / partial ntuple productions). Choose amongst: "
52 "`jets`, `JER`, `FTag`, `electrons`, `muons`, `photons`, `taus`, `met`, `tracks`, `generator`, `PRW`, `event`. "
53 "This option is overridden by `filterSystematics`.")
54 self.addOption ('systematicsHistogram', None , type=str,
55 info="the name of the histogram to which a list of executed "
56 "systematics will be printed. If left empty, the histogram is not written at all.")
57 self.addOption ('separateWeightSystematics', False, type=bool,
58 info="if `systematicsHistogram` is enabled, whether to create a separate "
59 "histogram holding only the names of weight-based systematics. This is useful "
60 "to help make histogramming frameworks more efficient by knowing in advance which "
61 "systematics need to recompute the observable and which don't.")
62 self.addOption ('metadataHistogram', 'metadata' , type=str,
63 info="the name of the metadata histogram which contains information about "
64 "data type, campaign, etc. If left empty, the histogram is not written at all.")
65 self.addOption ('enableExpertMode', False, type=bool,
66 info="allows CP experts and CPAlgorithm devs to use non-recommended configurations. "
67 "DO NOT USE FOR ANALYSIS.")
68 self.addOption ('streamName', None, type=str,
69 info="name of the output stream to save metadata histograms in.")
70 self.addOption ('setupONNX', False, type=bool,
71 info="creates an instance of `AthOnnx::OnnxRuntimeSvc`.")
72
73 def instanceName (self) :
74 """Return the instance name for this block"""
75 return '' # no instance name, this is a singleton
76
77 def makeAlgs (self, config) :
78
79 sysService = config.createService( 'CP::SystematicsSvc', 'SystematicsSvc' )
80
81 # Setup stream name
82 streamName = self.streamName or config.defaultHistogramStream()
83
84 # Handle all possible configuration options for systematics
85 if self.runSystematics is False:
86 runSystematics = self.runSystematics
87 elif config.noSystematics() is not None:
88 # if option not set:
89 # check to see if set in config accumulator
90 self.runSystematics = not config.noSystematics()
91 runSystematics = self.runSystematics
92 else:
93 runSystematics = True
94
95 # Now update the global configuration
96 config._noSystematics = not runSystematics
97
98 if runSystematics:
99 sysService.sigmaRecommended = 1
100 if config.dataType() is DataType.Data:
101 # Only one type of allowed systematics on data: the JER variations!
103 if self.onlySystematicsCategories is not None:
104 # Convert strings to enums and validate
105 requested_categories = set()
106 for category_str in self.onlySystematicsCategories:
107 try:
108 category_enum = SystematicsCategories[category_str.upper()]
109 requested_categories |= category_enum.value
110 except KeyError:
111 raise ValueError(f"Invalid systematics category passed to option 'onlySystematicsCategories': {category_str}. Must be one of {', '.join(category.name for category in SystematicsCategories)}")
112 # Construct regex pattern as logical-OR of category names
113 if len(requested_categories):
114 sysService.systematicsRegex = "^(?=.*(" + "|".join(requested_categories) + ")|$).*"
115 if self.filterSystematics is not None:
116 sysService.systematicsRegex = self.filterSystematics
117 config.createService( 'CP::SelectionNameSvc', 'SelectionNameSvc')
118
119 if self.systematicsHistogram is not None:
120 # print out all systematics
121 allSysDumper = config.createAlgorithm( 'CP::SysListDumperAlg', 'SystematicsPrinter' )
122 allSysDumper.histogramName = self.systematicsHistogram
123 allSysDumper.RootStreamName = streamName
124
126 # print out only the weight systematics (for more efficient histogramming down the line)
127 weightSysDumper = config.createAlgorithm( 'CP::SysListDumperAlg', 'OnlyWeightSystematicsPrinter' )
128 weightSysDumper.histogramName = f"{self.systematicsHistogram}OnlyWeights"
129 weightSysDumper.systematicsRegex = "^(GEN_|EL_EFF_|MUON_EFF_|PH_EFF_|TAUS_TRUEHADTAU_EFF_|FT_EFF_|JET_.*JvtEfficiency_|PRW_).*"
130
132 # add histogram with metadata
133 if not config.flags:
134 raise ValueError ("Writing out the metadata histogram requires to pass config flags")
135 metadataHistAlg = config.createAlgorithm( 'CP::MetadataHistAlg', 'MetadataHistAlg' )
136 metadataHistAlg.histogramName = self.metadataHistogram
137 metadataHistAlg.dataType = str(config.dataType().value)
138 metadataHistAlg.campaign = str(config.dataYear()) if config.dataType() is DataType.Data else str(config.campaign().value)
139 metadataHistAlg.mcChannelNumber = str(config.dsid())
140 metadataHistAlg.RootStreamName = streamName
141 if config.dataType() is DataType.Data:
142 etag = "unavailable"
143 else:
144 from AthenaConfiguration.AutoConfigFlags import GetFileMD
145 metadata = GetFileMD(config.flags.Input.Files)
146 amiTags = metadata.get("AMITag", "not found!")
147 etag = str(amiTags.split("_")[0])
148 metadataHistAlg.etag = etag
149
150 if self.enableExpertMode and config._pass == 0:
151 # set any expert-mode errors to be ignored instead
152 warnings.simplefilter('ignore', ExpertModeWarning)
153 # just warning users they might be doing something dangerous
154 log = logging.getLogger('CommonServices')
155 bold = "\033[1m"
156 red = "\033[91m"
157 yellow = "\033[93m"
158 reset = "\033[0m"
159 log.warning(red +r"""
160 ________ _______ ______ _____ _______ __ __ ____ _____ ______ ______ _ _ ____ _ ______ _____
161 | ____\ \ / / __ \| ____| __ \__ __| | \/ |/ __ \| __ \| ____| | ____| \ | | /\ | _ \| | | ____| __ \
162 | |__ \ V /| |__) | |__ | |__) | | | | \ / | | | | | | | |__ | |__ | \| | / \ | |_) | | | |__ | | | |
163 | __| > < | ___/| __| | _ / | | | |\/| | | | | | | | __| | __| | . ` | / /\ \ | _ <| | | __| | | | |
164 | |____ / . \| | | |____| | \ \ | | | | | | |__| | |__| | |____ | |____| |\ |/ ____ \| |_) | |____| |____| |__| |
165 |______/_/ \_\_| |______|_| \_\ |_| |_| |_|\____/|_____/|______| |______|_| \_/_/ \_\____/|______|______|_____/
166
167"""
168 +reset)
169 log.warning(f"{bold}{yellow}These settings are not recommended for analysis. Make sure you know what you're doing, or disable them with `enableExpertMode: False` in `CommonServices`.{reset}")
170
171 if self.setupONNX:
172 config.createService('AthOnnx::OnnxRuntimeSvc', 'OnnxRuntimeSvc')
173
174@groupBlocks
176 seq.append(CommonServicesConfig())
177 from AsgAnalysisAlgorithms.TruthCollectionsFixerConfig import TruthCollectionsFixerBlock
178 seq.append(TruthCollectionsFixerBlock())
179
180class IOStatsBlock(ConfigBlock):
181 """Print what branches are used in analysis"""
182
183 def __init__(self):
184 super(IOStatsBlock, self).__init__()
185 self.addOption("printOption", "Summary", type=str,
186 info='option to pass the standard ROOT printing function. Can be `Summary`, `ByEntries` or `ByBytes`.')
187
188 def instanceName (self) :
189 """Return the instance name for this block"""
190 return '' # no instance name, this is a singleton
191
192 def makeAlgs(self, config):
193 alg = config.createAlgorithm('CP::IOStatsAlg', 'IOStatsAlg')
194 alg.printOption = self.printOption
195
196
197class PileupReweightingBlock (ConfigBlock):
198 """the ConfigBlock for pileup reweighting"""
199
200 def __init__ (self) :
201 super (PileupReweightingBlock, self).__init__ ()
202 self.addOption ('campaign', None, type=None,
203 info="the MC campaign for the PRW auto-configuration.")
204 self.addOption ('files', None, type=list,
205 info="the input files being processed (list of strings). "
206 "Alternative to auto-configuration.")
207 self.addOption ('useDefaultConfig', True, type=bool,
208 info="whether to use the central PRW files.")
209 self.addOption ('userLumicalcFiles', None, type=list,
210 info="user-provided lumicalc files (list of strings). Alternative "
211 "to auto-configuration.")
212 self.addOption ('userLumicalcFilesPerCampaign', None, type=dict,
213 info="user-provided lumicalc files (dictionary of list of strings, "
214 "with MC campaigns as the keys). Alternative to auto-configuration.")
215 self.addOption ('userPileupConfigs', None, type=list,
216 info="user-provided PRW files (list of strings). Alternative to "
217 "auto-configuration.")
218 self.addOption ('userPileupConfigsPerCampaign', None, type=dict,
219 info="user-provided PRW files (dictionary of list of strings, with "
220 "MC campaigns as the keys).")
221 self.addOption ('postfix', '', type=str,
222 info="a postfix to apply to decorations and algorithm names. "
223 "Typically not needed unless several instances of `PileupReweighting` are scheduled.")
224 self.addOption ('alternativeConfig', False, type=bool,
225 info="whether this is used as an additional alternative config for `PileupReweighting`. "
226 "Will only store the alternative pileup weight in that case.")
227 self.addOption ('unrepresentedDataWarningThreshold', 1e-4, type=float,
228 info="suppress the unrepresented-data WARNING when the unrepresented "
229 "fraction is below this value (default 0.01%). Set to 0 to always "
230 "warn.")
231 self.addOption ('writeColumnarToolVariables', False, type=bool,
232 info="whether to add `EventInfo` variables needed for running the columnar tool(s) on the output n-tuple. (EXPERIMENTAL).",
233 expertMode=True)
234
235 def instanceName (self) :
236 """Return the instance name for this block"""
237 return self.postfix
238
239 def makeAlgs (self, config) :
240
241 from Campaigns.Utils import Campaign
242
243 log = logging.getLogger('makePileupAnalysisSequence')
244
245 eventInfoVar = ['runNumber', 'eventNumber', 'actualInteractionsPerCrossing', 'averageInteractionsPerCrossing']
246 if config.dataType() is not DataType.Data:
247 eventInfoVar += ['mcChannelNumber']
249 # This is not strictly necessary, as the columnar users
250 # could recreate this, but it is also a single constant int,
251 # that should compress exceedingly well.
252 eventInfoVar += ['eventTypeBitmask']
253
254 if config.isPhyslite() and not self.alternativeConfig:
255 # PHYSLITE already has these variables defined, just need to copy them to the output
256 log.info(f'Physlite does not need pileup reweighting. Variables will be copied from input instead. {config.isPhyslite}')
257 for var in eventInfoVar:
258 config.addOutputVar ('EventInfo', var, var, noSys=True)
259
260 if config.dataType() is not DataType.Data:
261 config.addOutputVar ('EventInfo', 'PileupWeight_%SYS%', 'weight_pileup', auxType='float')
262 if config.geometry() is LHCPeriod.Run2:
263 config.addOutputVar ('EventInfo', 'beamSpotWeight', 'weight_beamspot', noSys=True)
264 return
265
266 # check files from flags
267 if self.files is None and config.flags is not None:
268 self.files = config.flags.Input.Files
269
270 campaign = self.campaign
271 # if user didn't explicitly configure campaign, let's try setting it from metadata
272 # only needed on MC
273 if config.dataType() is not DataType.Data and self.campaign is None:
274 # if we used flags, campaign is auto-determined
275 if config.campaign() is not None and config.campaign() is not Campaign.Unknown:
276 campaign = config.campaign()
277 log.info(f'Auto-configuring campaign for PRW from flags: {campaign.value}')
278 else:
279 # we try to determine campaign from files if above failed
280 if self.files is not None:
281 from Campaigns.Utils import getMCCampaign
282 campaign = getMCCampaign(self.files)
283 if campaign and campaign is not Campaign.Unknown:
284 log.info(f'Auto-configuring campaign for PRW from files: {campaign.value}')
285 else:
286 log.info('Campaign could not be determined.')
287
288
289 toolConfigFiles = []
290 toolLumicalcFiles = []
291
292 # PRW config files should only be configured if we run on MC
293 # Run 4 not supported yet
294 if (config.dataType() is not DataType.Data and
295 config.geometry() is not LHCPeriod.Run4):
296 # check if user provides per-campaign pileup config list
297 if self.userPileupConfigs is not None and self.userPileupConfigsPerCampaign is not None:
298 raise ValueError('Both userPileupConfigs and userPileupConfigsPerCampaign specified, '
299 'use only one of the options!')
300 if self.userPileupConfigsPerCampaign is not None:
301 if not campaign:
302 raise Exception('userPileupConfigsPerCampaign requires campaign to be configured!')
303 if campaign is Campaign.Unknown:
304 raise Exception('userPileupConfigsPerCampaign used, but campaign = Unknown!')
305 try:
306 toolConfigFiles = self.userPileupConfigsPerCampaign[campaign.value][:]
307 log.info('Using user provided per-campaign PRW configuration')
308 except KeyError as e:
309 raise KeyError(f'Unconfigured campaign {e} for userPileupConfigsPerCampaign!')
310
311 elif self.userPileupConfigs is not None:
312 toolConfigFiles = self.userPileupConfigs[:]
313 log.info('Using user provided PRW configuration')
314
315 else:
316 if self.useDefaultConfig and self.files is None:
317 raise ValueError('useDefaultConfig requires files to be configured! '
318 'Either pass them as an option or use flags.')
319
320 from PileupReweighting.AutoconfigurePRW import getConfigurationFiles
321 if campaign and campaign is not Campaign.Unknown:
322 toolConfigFiles = getConfigurationFiles(campaign=campaign,
323 files=self.files,
324 useDefaultConfig=self.useDefaultConfig,
325 data_type=config.dataType())
327 log.info('Auto-configuring universal/default PRW config')
328 else:
329 log.info('Auto-configuring per-sample PRW config files based on input files')
330 else:
331 log.info('No campaign specified, no PRW config files configured')
332
333 # check if user provides per-campaign lumical config list
334 if self.userLumicalcFilesPerCampaign is not None and self.userLumicalcFiles is not None:
335 raise ValueError('Both userLumicalcFiles and userLumicalcFilesYear specified, '
336 'use only one of the options!')
337 if self.userLumicalcFilesPerCampaign is not None:
338 try:
339 toolLumicalcFiles = self.userLumicalcFilesPerCampaign[campaign.value][:]
340 log.info('Using user-provided per-campaign lumicalc files')
341 except KeyError as e:
342 raise KeyError(f'Unconfigured campaign {e} for userLumicalcFilesPerCampaign!')
343 elif self.userLumicalcFiles is not None:
344 toolLumicalcFiles = self.userLumicalcFiles[:]
345 log.info('Using user-provided lumicalc files')
346 else:
347 if campaign and campaign is not Campaign.Unknown:
348 from PileupReweighting.AutoconfigurePRW import getLumicalcFiles
349 toolLumicalcFiles = getLumicalcFiles(campaign)
350 log.info('Using auto-configured lumicalc files')
351 else:
352 log.info('No campaign specified, no lumicalc files configured for PRW')
353 else:
354 log.info('Data needs no lumicalc and PRW configuration files')
355
356 # Set up the only algorithm of the sequence:
357 if config.geometry() is LHCPeriod.Run4:
358 warnings.warn_explicit(
359 'Pileup reweighting is not yet supported for Run 4 geometry',
360 Run4FallbackWarning, filename='', lineno=0)
361 alg = config.createAlgorithm( 'CP::EventDecoratorAlg', 'EventDecoratorAlg' )
362 alg.uint32Decorations = { 'RandomRunNumber' :
363 config.flags.Input.RunNumbers[0] }
364
365 else:
366 alg = config.createAlgorithm( 'CP::PileupReweightingAlg',
367 'PileupReweightingAlg' )
368 config.addPrivateTool( 'pileupReweightingTool', 'CP::PileupReweightingTool' )
369 alg.pileupReweightingTool.ConfigFiles = toolConfigFiles
370 if not toolConfigFiles and config.dataType() is not DataType.Data:
371 log.info("No PRW config files provided. Disabling reweighting")
372 # Setting the weight decoration to the empty string disables the reweighting
373 alg.pileupWeightDecoration = ""
374 else:
375 alg.pileupWeightDecoration = "PileupWeight" + self.postfix + "_%SYS%"
376 alg.pileupReweightingTool.LumiCalcFiles = toolLumicalcFiles
377 alg.pileupReweightingTool.UnrepresentedDataWarningThreshold = (
378 self.unrepresentedDataWarningThreshold)
379
380 if not self.alternativeConfig:
381 for var in eventInfoVar:
382 config.addOutputVar ('EventInfo', var, var, noSys=True)
383
384 if config.dataType() is not DataType.Data and config.geometry() is LHCPeriod.Run2:
385 config.addOutputVar ('EventInfo', 'beamSpotWeight', 'weight_beamspot', noSys=True)
386
387 if config.dataType() is not DataType.Data and toolConfigFiles:
388 config.addOutputVar ('EventInfo', 'PileupWeight' + self.postfix + '_%SYS%',
389 'weight_pileup'+self.postfix)
390
391
392class GeneratorAnalysisBlock (ConfigBlock):
393 """the ConfigBlock for generator algorithms"""
394
395 def __init__ (self) :
396 super (GeneratorAnalysisBlock, self).__init__ ()
397 self.addOption ('saveCutBookkeepers', True, type=bool,
398 info="whether to save the cut bookkeepers information into the "
399 "output file.")
400 self.addOption ('runNumber', None, type=int,
401 info="the MC `runNumber`. If left empty, autoconfigure from the sample metadata.")
402 self.addOption ('cutBookkeepersSystematics', None, type=bool,
403 info="whether to also save the cut bookkeepers systematics. The "
404 "default is `None` (follows the global systematics flag). Set to "
405 "`False` or `True` to override.")
406 self.addOption ('histPattern', None, type=str,
407 info="the histogram name pattern for the cut-bookkeeper histogram names.")
408 self.addOption ('streamName', None, type=str,
409 info="name of the output stream to save the cut bookkeeper in.")
410 self.addOption ('detailedPDFinfo', False, type=bool,
411 info="save the necessary information to run the LHAPDF tool offline.")
412 self.addOption ('doPDFReweighting', False, type=bool,
413 info="perform the PDF reweighting to do the PDF sensitivity studies with the existing sample, intrinsic charm PDFs as the default here. WARNING: the reweighting closure should be validated within analysis (it has been proved to be good for Madgraph, aMC@NLO, Pythia8, Herwig, and Alpgen, but not good for Sherpa and Powheg).")
414 self.addOption ('outPDFName', [
415 "CT14nnloIC/0", "CT14nnloIC/1", "CT14nnloIC/2",
416 "CT18FC/0", "CT18FC/3", "CT18FC/6", "CT18FC/9",
417 "CT18NNLO/0", "CT18XNNLO/0",
418 "NNPDF40_nnlo_pch_as_01180/0", "NNPDF40_nnlo_as_01180/0"
419 ], type=list, info="list of PDF sets to use for PDF reweighting.")
420 self.addOption ('doHFProdFracReweighting', False, type=bool,
421 info="whether to apply HF production fraction reweighting.")
422 self.addOption ('truthParticleContainer', 'TruthParticles', type=str,
423 info="the name of the truth particle container to use for HF production fraction reweighting.")
424
425 def instanceName (self) :
426 """Return the instance name for this block"""
427 return self.streamName or "DEFAULT"
428
429 def makeAlgs (self, config) :
430
431 if config.dataType() is DataType.Data:
432 # there are no generator weights in data!
433 return
434 log = logging.getLogger('makeGeneratorAnalysisSequence')
435
436 # Setup stream name
437 streamName = self.streamName or config.defaultHistogramStream()
438
439 if self.runNumber is None:
440 self.runNumber = config.runNumber()
441
442 if self.saveCutBookkeepers and not self.runNumber:
443 raise ValueError ("invalid run number: " + str(self.runNumber))
444
445 # Set up the CutBookkeepers algorithm:
447 alg = config.createAlgorithm('CP::AsgCutBookkeeperAlg', 'CutBookkeeperAlg')
448 alg.RootStreamName = streamName
449 alg.runNumber = self.runNumber
450 if self.cutBookkeepersSystematics is None:
451 alg.enableSystematics = not config.noSystematics()
452 else:
453 alg.enableSystematics = self.cutBookkeepersSystematics
454 if self.histPattern:
455 alg.histPattern = self.histPattern
456 config.addPrivateTool( 'truthWeightTool', 'PMGTools::PMGTruthWeightTool' )
457
458 # Set up the weights algorithm:
459 alg = config.createAlgorithm( 'CP::PMGTruthWeightAlg', 'PMGTruthWeightAlg' )
460 config.addPrivateTool( 'truthWeightTool', 'PMGTools::PMGTruthWeightTool' )
461 alg.decoration = 'generatorWeight_%SYS%'
462 config.addOutputVar ('EventInfo', 'generatorWeight_%SYS%', 'weight_mc')
463
465 alg = config.createAlgorithm( 'CP::PDFinfoAlg', 'PDFinfoAlg', reentrant=True )
466 for var in ["PDFID1","PDFID2","PDGID1","PDGID2","Q","X1","X2","XF1","XF2"]:
467 config.addOutputVar ('EventInfo', var, 'PDFinfo_' + var, noSys=True)
468
470 alg = config.createAlgorithm( 'CP::PDFReweightAlg', 'PDFReweightAlg', reentrant=True )
471
472 for pdf_set in self.outPDFName:
473 config.addOutputVar('EventInfo', f'PDFReweightSF_{pdf_set.replace("/", "_")}',
474 f'PDFReweightSF_{pdf_set.replace("/", "_")}', noSys=True)
475
476
478 generatorInfo = config.flags.Input.GeneratorsInfo
479 log.info(f"Loaded generator info: {generatorInfo}")
480
481 DSID = "000000"
482
483 if not generatorInfo:
484 warnings.warn_explicit(
485 "No generator info found.",
486 GeneratorWeightWarning, filename='', lineno=0)
487 DSID = "000000"
488 elif isinstance(generatorInfo, dict):
489 if "Pythia8" in generatorInfo:
490 DSID = "410470"
491 elif "Sherpa" in generatorInfo and "2.2.8" in generatorInfo["Sherpa"]:
492 DSID = "421152"
493 elif "Sherpa" in generatorInfo and "2.2.10" in generatorInfo["Sherpa"]:
494 DSID = "700122"
495 elif "Sherpa" in generatorInfo and "2.2.11" in generatorInfo["Sherpa"]:
496 warnings.warn_explicit(
497 "HF production fraction reweighting is not configured"
498 " for Sherpa 2.2.11. Using weights for Sherpa 2.2.10"
499 " instead.",
500 GeneratorWeightWarning, filename='', lineno=0)
501 DSID = "700122"
502 elif "Sherpa" in generatorInfo and "2.2.12" in generatorInfo["Sherpa"]:
503 warnings.warn_explicit(
504 "HF production fraction reweighting is not configured"
505 " for Sherpa 2.2.12. Using weights for Sherpa 2.2.10"
506 " instead.",
507 GeneratorWeightWarning, filename='', lineno=0)
508 DSID = "700122"
509 elif "Sherpa" in generatorInfo and "2.2.14" in generatorInfo["Sherpa"]:
510 warnings.warn_explicit(
511 "HF production fraction reweighting is not configured"
512 " for Sherpa 2.2.14. New weights need to be"
513 " calculated.",
514 GeneratorWeightWarning, filename='', lineno=0)
515 DSID = "000000"
516 elif "Sherpa" in generatorInfo and "2.2.1" in generatorInfo["Sherpa"]:
517 DSID = "410250"
518 elif "Herwig7" in generatorInfo and "7.1.3" in generatorInfo["Herwig7"]:
519 DSID = "411233"
520 elif "Herwig7" in generatorInfo and "7.2.1" in generatorInfo["Herwig7"]:
521 DSID = "600666"
522 elif "Herwig7" in generatorInfo and "7." in generatorInfo["Herwig7"]:
523 DSID = "410558"
524 elif "amc@NLO" in generatorInfo:
525 DSID = "410464"
526 else:
527 warnings.warn_explicit(
528 f"HF production fraction reweighting is not configured"
529 f" for this generator: {generatorInfo}."
530 f" New weights need to be calculated.",
531 GeneratorWeightWarning, filename='', lineno=0)
532 DSID = "000000"
533 else:
534 warnings.warn_explicit(
535 "Failed to determine generator from metadata",
536 GeneratorWeightWarning, filename='', lineno=0)
537 DSID = "000000"
538
539 log.info(f"Using HF production fraction weights calculated using DSID {DSID}")
540 if DSID == "000000":
541 warnings.warn_explicit(
542 "HF production fraction reweighting will return dummy"
543 " weights of 1.0",
544 GeneratorWeightWarning, filename='', lineno=0)
545
546 alg = config.createAlgorithm( 'CP::SysTruthWeightAlg', f'SysTruthWeightAlg_{streamName}' )
547 config.addPrivateTool( 'sysTruthWeightTool', 'PMGTools::PMGHFProductionFractionTool' )
548 alg.decoration = 'prodFracWeight_%SYS%'
549 alg.TruthParticleContainer = self.truthParticleContainer
550 alg.sysTruthWeightTool.ShowerGenerator = DSID
551 config.addOutputVar ('EventInfo', 'prodFracWeight_%SYS%', 'weight_HF_prod_frac')
552
553class PtEtaSelectionBlock (ConfigBlock):
554 """the ConfigBlock for a pt-eta selection"""
555
556 def __init__ (self) :
557 super (PtEtaSelectionBlock, self).__init__ ()
558 self.addOption ('containerName', '', type=str,
559 noneAction='error',
560 info="the name of the input container.")
561 self.addOption ('selectionName', '', type=str,
562 noneAction='error',
563 info="the name of the selection to append this to. If left empty, "
564 "the cuts are applied to every "
565 "object within the container. Specifying a name (e.g. `loose`) "
566 "applies the cut only to those object who also pass that selection.")
567 self.addOption ('minPt', None, type=float,
568 info=r"minimum $p_\mathrm{T}$ value to cut on (in MeV).")
569 self.addOption ('maxPt', None, type=float,
570 info=r"maximum $p_\mathrm{T}$ value to cut on (in MeV).")
571 self.addOption ('minEta', None, type=float,
572 info=r"minimum $\vert\eta\vert$ value to cut on.")
573 self.addOption ('maxEta', None, type=float,
574 info=r"maximum $\vert\eta\vert$ value to cut on.")
575 self.addOption ('maxRapidity', None, type=float,
576 info="maximum rapidity value to cut on.")
577 self.addOption ('etaGapLow', None, type=float,
578 info=r"low end of the $\vert\eta\vert$ gap.")
579 self.addOption ('etaGapHigh', None, type=float,
580 info=r"high end of the $\vert\eta\vert$ gap.")
581 self.addOption ('selectionDecoration', None, type=str,
582 info="the name of the decoration to set. If `None`, will be set "
583 "to `selectPtEta` followed by the selection name.")
584 self.addOption ('useClusterEta', False, type=bool,
585 info=r"whether to use the cluster $\eta$ (`etaBE(2)`) instead of the object "
586 r"$\eta$ (for electrons and photons).")
587 self.addOption ('useDressedProperties', False, type=bool,
588 info="whether to use the dressed kinematic properties "
589 "(for truth particles only).")
590
591 def instanceName (self) :
592 """Return the instance name for this block"""
593 return self.containerName + "_" + self.selectionName
594
595 def makeAlgs (self, config) :
596
597 alg = config.createAlgorithm( 'CP::AsgSelectionAlg', 'PtEtaSelectionAlg' )
598 config.addPrivateTool( 'selectionTool', 'CP::AsgPtEtaSelectionTool' )
599 if self.minPt is not None :
600 alg.selectionTool.minPt = self.minPt
601 if self.maxPt is not None:
602 alg.selectionTool.maxPt = self.maxPt
603 if self.minEta is not None:
604 alg.selectionTool.minEta = self.minEta
605 if self.maxEta is not None :
606 alg.selectionTool.maxEta = self.maxEta
607 if self.maxRapidity is not None :
608 alg.selectionTool.maxRapidity = self.maxRapidity
609 if self.etaGapLow is not None:
610 alg.selectionTool.etaGapLow = self.etaGapLow
611 if self.etaGapHigh is not None:
612 alg.selectionTool.etaGapHigh = self.etaGapHigh
613 if self.selectionDecoration is None:
614 self.selectionDecoration = 'selectPtEta' + (f'_{self.selectionName}' if self.selectionName else '')
615 alg.selectionTool.useClusterEta = self.useClusterEta
616 alg.selectionTool.useDressedProperties = self.useDressedProperties
617 alg.selectionDecoration = self.selectionDecoration
618 alg.particles = config.readName (self.containerName)
619 alg.preselection = config.getPreselection (self.containerName, '')
620 config.addSelection (self.containerName, self.selectionName, alg.selectionDecoration)
621
622
623
624class ObjectCutFlowBlock (ConfigBlock):
625 """the ConfigBlock for an object cutflow"""
626
627 def __init__ (self) :
628 super (ObjectCutFlowBlock, self).__init__ ()
629 self.addOption ('containerName', '', type=str,
630 noneAction='error',
631 info="the name of the input container.")
632 self.addOption ('selectionName', '', type=str,
633 noneAction='error',
634 info="the name of the selection to perform the cutflow for. If left empty, "
635 "the cutflow is "
636 "performed for every object within the container. Specifying a "
637 "name (e.g. `loose`) generates the cutflow only for those objects "
638 "that also pass that selection.")
639 self.addOption ('forceCutSequence', False, type=bool,
640 info="whether to force the cut sequence and not accept objects "
641 "if previous cuts failed.")
642 self.addOption ('streamName', None, type=str,
643 info="name of the output stream to save the cutflow histogram in.")
644
645 def instanceName (self) :
646 """Return the instance name for this block"""
647 return self.containerName + '_' + self.selectionName
648
649 def makeAlgs (self, config) :
650 streamName = self.streamName or config.defaultHistogramStream()
651
652 alg = config.createAlgorithm( 'CP::ObjectCutFlowHistAlg', 'CutFlowDumperAlg' )
653 alg.RootStreamName = streamName
654 alg.histPattern = 'cflow_' + self.containerName + "_" + self.selectionName + '_%SYS%'
655 alg.selections = config.getSelectionCutFlow (self.containerName, self.selectionName)
656 alg.input = config.readName (self.containerName)
657 alg.histTitle = "Object Cutflow: " + self.containerName + "." + self.selectionName
658 alg.forceCutSequence = self.forceCutSequence
659
660
661class EventCutFlowBlock (ConfigBlock):
662 """the ConfigBlock for an event-level cutflow"""
663
664 def __init__(self):
665 super(EventCutFlowBlock, self).__init__()
666 self.addOption('selectionName', '', type=str,
667 noneAction='error',
668 info="the name of the event selection to generate cutflow histograms for. "
669 "If left blank, all selections on EventInfo will be used.")
670 self.addOption('customSelections', [], type=None,
671 info="explicit list of selection decorations to use for the cutflow. "
672 "If provided, takes precedence over selectionName.")
673 self.addOption('cutFlowHistograms', True, type=bool,
674 info="whether to generate cutflow histograms for the selection cuts.")
675
676 def instanceName(self):
677 return 'EventInfo_' + self.selectionName
678
679 def makeAlgs(self, config):
680
681 if not self.cutFlowHistograms:
682 return
683
684 postfix = ('_' + self.selectionName) if self.selectionName else ''
685
686 alg = config.createAlgorithm('CP::EventCutFlowHistAlg', 'CutFlowDumperAlg')
687 alg.histPattern = 'cflow_EventInfo' + postfix + '_%SYS%'
688 alg.eventInfo = config.readName('EventInfo')
689 alg.histTitle = 'Event Cutflow: EventInfo.' + self.selectionName
690
691 if isinstance(self.customSelections, list) and len(self.customSelections) > 0:
692 # user provides a hardcoded list of selections
693 alg.selections = self.customSelections
694 elif self.selectionName:
695 # resolve selectionName to the list of cuts registered by EventSelectionConfig
696 alg.selections = config.getEventCutFlow(self.selectionName)
697 else:
698 # fallback: get all available selections from EventInfo
699 alg.selections = config.getSelectionCutFlow('EventInfo', '')
700
701 alg.selections = [sel + ',as_char' for sel in alg.selections]
702
703class OutputThinningBlock (ConfigBlock):
704 """the ConfigBlock for output thinning"""
705
706 def __init__ (self) :
707 super (OutputThinningBlock, self).__init__ ()
708 self.setBlockName('Thinning')
709 self.addOption ('containerName', '', type=str,
710 noneAction='error',
711 info="the name of the input container.")
712 self.addOption ('postfix', '', type=str,
713 info="a postfix to apply to decorations and algorithm names. "
714 "Typically not needed here.")
715 self.addOption ('selection', '', type=str,
716 info="the name of an optional selection decoration to use.")
717 self.addOption ('selectionName', '', type=str,
718 info="the name of the selection to append this to. If left empty, "
719 "the cuts are applied to every "
720 "object within the container. Specifying a name (e.g. `loose`) "
721 "applies the cut only to those object who also pass that selection.")
722 self.addOption ('outputName', None, type=str,
723 info="an optional name for the output container.")
724 self.addOption ('deepCopy', False, type=bool,
725 info="run a deep copy of the container.")
726 self.addOption ('sortPt', False, type=bool,
727 info=r"whether to sort objects in $p_\mathrm{T}$.")
728 self.addOption ('noUniformSelection', False, type=bool,
729 info="do not run the union over all selections.")
730 self.addOption ('containerType', None, type=str,
731 info="the type of the container to thin. Only needed in AthenaMT, and only if subsequent code has a data dependency on the created container under that type.")
732
733 def instanceName (self) :
734 """Return the instance name for this block"""
735 return self.containerName + '_' + self.selectionName + self.postfix
736
737 def makeAlgs (self, config) :
738
739 postfix = self.postfix
740 if postfix != '' and postfix[0] != '_' :
741 postfix = '_' + postfix
742
743 selection = config.getFullSelection (self.containerName, self.selectionName)
744 if selection == '' :
745 selection = self.selection
746 elif self.selection != '' :
747 selection = selection + '&&' + self.selection
748
749 if selection != '' and not self.noUniformSelection :
750 alg = config.createAlgorithm( 'CP::AsgUnionSelectionAlg', 'UnionSelectionAlg')
751 alg.preselection = selection
752 alg.particles = config.readName (self.containerName)
753 alg.selectionDecoration = 'outputSelect' + postfix
754 config.addSelection (self.containerName, alg.selectionDecoration, selection)
755 selection = 'outputSelect' + postfix
756
757 alg = config.createAlgorithm( 'CP::AsgViewFromSelectionAlg', 'DeepCopyAlg' )
758 alg.input = config.readName (self.containerName)
759 if self.outputName is not None :
760 alg.output = self.outputName + '_%SYS%'
761 config.addOutputContainer (self.containerName, self.outputName)
762 else :
763 alg.output = config.copyName (self.containerName)
764 if self.containerType is not None :
765 alg.outputType = self.containerType
766 if selection != '' :
767 alg.selection = [selection]
768 else :
769 alg.selection = []
770 alg.deepCopy = self.deepCopy
771 if self.sortPt and not config.noSystematics() :
772 raise ValueError ("Sorting by pt is not supported with systematics")
773 alg.sortPt = self.sortPt
774
775
776class IFFLeptonDecorationBlock (ConfigBlock):
777 """the ConfigBlock for the IFF classification of leptons"""
778
779 def __init__ (self) :
780 super (IFFLeptonDecorationBlock, self).__init__()
781 self.addOption ('containerName', '', type=str,
782 noneAction='error',
783 info="the name of the input electron or muon container.")
784 self.addOption ('separateChargeFlipElectrons', True, type=bool,
785 info="whether to consider charged-flip electrons as a separate class.")
786 self.addOption ('decoration', 'IFFClass_%SYS%', type=str,
787 info="the name of the decoration set by the IFF "
788 "`TruthClassificationTool`.")
789 # Always skip on data
790 self.setOptionValue('skipOnData', True)
791
792 def instanceName (self) :
793 """Return the instance name for this block"""
794 return self.containerName
795
796 def makeAlgs (self, config) :
797 particles = config.readName(self.containerName)
798
799 alg = config.createAlgorithm( 'CP::AsgClassificationDecorationAlg', 'IFFClassifierAlg' )
800 # the IFF classification tool
801 config.addPrivateTool( 'tool', 'TruthClassificationTool')
802 # label charge-flipped electrons as such
803 alg.tool.separateChargeFlipElectrons = self.separateChargeFlipElectrons
804 alg.decoration = self.decoration
805 alg.particles = particles
806
807 # write the decoration only once to the output
808 config.addOutputVar(self.containerName, alg.decoration, alg.decoration.split("_%SYS%")[0], noSys=True)
809
810
811class MCTCLeptonDecorationBlock (ConfigBlock):
812
813 def __init__ (self) :
814 super (MCTCLeptonDecorationBlock, self).__init__ ()
815
816 self.addOption ("containerName", '', type=str,
817 noneAction='error',
818 info="the input lepton container, with a possible selection, "
819 "in the format `container` or `container.selection`.")
820 self.addOption ("prefix", 'MCTC_', type=str,
821 info="the prefix of the decorations based on the MCTC "
822 "classification.")
823 # Always skip on data
824 self.setOptionValue('skipOnData', True)
825
826 def instanceName (self) :
827 """Return the instance name for this block"""
828 return self.containerName
829
830 def makeAlgs (self, config) :
831 particles, selection = config.readNameAndSelection(self.containerName)
832
833 alg = config.createAlgorithm ("CP::MCTCDecorationAlg", "MCTCDecorationAlg")
834 alg.particles = particles
835 alg.preselection = selection
836 alg.affectingSystematicsFilter = '.*'
837 config.addOutputVar (self.containerName, "MCTC_isPrompt", f"{self.prefix}isPrompt", noSys=True)
838 config.addOutputVar (self.containerName, "MCTC_fromHadron", f"{self.prefix}fromHadron", noSys=True)
839 config.addOutputVar (self.containerName, "MCTC_fromBSM", f"{self.prefix}fromBSM", noSys=True)
840 config.addOutputVar (self.containerName, "MCTC_fromTau", f"{self.prefix}fromTau", noSys=True)
841
842
843class PerEventSFBlock (ConfigBlock):
844 """the ConfigBlock for the AsgEventScaleFactorAlg"""
845
846 def __init__ (self):
847 super(PerEventSFBlock, self).__init__()
848 self.addOption('algoName', None, type=str,
849 info="unique name given to the underlying algorithm computing the "
850 "per-event scale factors.")
851 self.addOption('particles', '', type=str,
852 info="the input object container, with a possible selection, in the "
853 "format `container` or `container.selection`.")
854 self.addOption('objectSF', '', type=str,
855 info="the name of the per-object SF decoration to be used.")
856 self.addOption('eventSF', '', type=str,
857 info="the name of the per-event SF decoration.")
858
859 def instanceName (self) :
860 """Return the instance name for this block"""
861 return self.particles + '_' + self.objectSF + '_' + self.eventSF
862
863 def makeAlgs(self, config):
864 if config.dataType() is DataType.Data:
865 return
866 particles, selection = config.readNameAndSelection(self.particles)
867 alg = config.createAlgorithm('CP::AsgEventScaleFactorAlg', self.algoName if self.algoName else 'AsgEventScaleFactorAlg')
868 alg.particles = particles
869 alg.preselection = selection
870 alg.scaleFactorInputDecoration = self.objectSF
871 alg.scaleFactorOutputDecoration = self.eventSF
872
873 config.addOutputVar('EventInfo', alg.scaleFactorOutputDecoration,
874 alg.scaleFactorOutputDecoration.split("_%SYS%")[0])
875
876
877class SelectionDecorationBlock (ConfigBlock):
878 """the ConfigBlock to add selection decoration to a container"""
879
880 def __init__ (self) :
881 super (SelectionDecorationBlock, self).__init__ ()
882 # TODO: add info string
883 self.addOption('containers', [], type=list,
884 noneAction='error',
885 info="")
886
887 def instanceName (self) :
888 """Return the instance name for this block"""
889 return ''
890
891 def makeAlgs(self, config):
892 for container in self.containers:
893 originContainerName = config.getOutputContainerOrigin(container)
894 selectionNames = config.getSelectionNames(originContainerName)
895 for selectionName in selectionNames:
896 # skip default selection
897 if selectionName == '':
898 continue
899 alg = config.createAlgorithm(
900 'CP::AsgSelectionAlg',
901 f'SelectionDecoration_{originContainerName}_{selectionName}')
902 selectionDecoration = f'baselineSelection_{selectionName}_%SYS%'
903 alg.selectionDecoration = f'{selectionDecoration},as_char'
904 alg.particles = config.readName (originContainerName)
905 alg.preselection = config.getFullSelection (originContainerName,
906 selectionName)
907 config.addOutputVar(
908 originContainerName, selectionDecoration, selectionName)
STL class.