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
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','unsigned'),
246 ('eventNumber','unsigned_long'),
247 ('actualInteractionsPerCrossing','float'),
248 ('averageInteractionsPerCrossing','float')]
249 if config.dataType() is not DataType.Data:
250 eventInfoVar += [('mcChannelNumber','unsigned')]
252 # This is not strictly necessary, as the columnar users
253 # could recreate this, but it is also a single constant int,
254 # that should compress exceedingly well.
255 eventInfoVar += [('eventTypeBitmask','int')]
256
257 if config.isPhyslite() and not self.alternativeConfig:
258 # PHYSLITE already has these variables defined, just need to copy them to the output
259 log.info(f'Physlite does not need pileup reweighting. Variables will be copied from input instead. {config.isPhyslite}')
260 for var_name,var_type in eventInfoVar:
261 config.addOutputVar ('EventInfo', var_name, var_name, noSys=True, auxType=var_type)
262
263 if config.dataType() is not DataType.Data:
264 config.addOutputVar ('EventInfo', 'PileupWeight_%SYS%', 'weight_pileup', auxType='float')
265 if config.geometry() is LHCPeriod.Run2:
266 config.addOutputVar ('EventInfo', 'beamSpotWeight', 'weight_beamspot', noSys=True, auxType='float')
267 return
268
269 # check files from flags
270 if self.files is None and config.flags is not None:
271 self.files = config.flags.Input.Files
272
273 campaign = self.campaign
274 # if user didn't explicitly configure campaign, let's try setting it from metadata
275 # only needed on MC
276 if config.dataType() is not DataType.Data and self.campaign is None:
277 # if we used flags, campaign is auto-determined
278 if config.campaign() is not None and config.campaign() is not Campaign.Unknown:
279 campaign = config.campaign()
280 log.info(f'Auto-configuring campaign for PRW from flags: {campaign.value}')
281 else:
282 # we try to determine campaign from files if above failed
283 if self.files is not None:
284 from Campaigns.Utils import getMCCampaign
285 campaign = getMCCampaign(self.files)
286 if campaign and campaign is not Campaign.Unknown:
287 log.info(f'Auto-configuring campaign for PRW from files: {campaign.value}')
288 else:
289 log.info('Campaign could not be determined.')
290
291
292 toolConfigFiles = []
293 toolLumicalcFiles = []
294
295 # PRW config files should only be configured if we run on MC
296 # Run 4 not supported yet
297 if (config.dataType() is not DataType.Data and
298 config.geometry() is not LHCPeriod.Run4):
299 # check if user provides per-campaign pileup config list
300 if self.userPileupConfigs is not None and self.userPileupConfigsPerCampaign is not None:
301 raise ValueError('Both userPileupConfigs and userPileupConfigsPerCampaign specified, '
302 'use only one of the options!')
303 if self.userPileupConfigsPerCampaign is not None:
304 if not campaign:
305 raise Exception('userPileupConfigsPerCampaign requires campaign to be configured!')
306 if campaign is Campaign.Unknown:
307 raise Exception('userPileupConfigsPerCampaign used, but campaign = Unknown!')
308 try:
309 toolConfigFiles = self.userPileupConfigsPerCampaign[campaign.value][:]
310 log.info('Using user provided per-campaign PRW configuration')
311 except KeyError as e:
312 raise KeyError(f'Unconfigured campaign {e} for userPileupConfigsPerCampaign!')
313
314 elif self.userPileupConfigs is not None:
315 toolConfigFiles = self.userPileupConfigs[:]
316 log.info('Using user provided PRW configuration')
317
318 else:
319 if self.useDefaultConfig and self.files is None:
320 raise ValueError('useDefaultConfig requires files to be configured! '
321 'Either pass them as an option or use flags.')
322
323 from PileupReweighting.AutoconfigurePRW import getConfigurationFiles
324 if campaign and campaign is not Campaign.Unknown:
325 toolConfigFiles = getConfigurationFiles(campaign=campaign,
326 files=self.files,
327 useDefaultConfig=self.useDefaultConfig,
328 data_type=config.dataType())
330 log.info('Auto-configuring universal/default PRW config')
331 else:
332 log.info('Auto-configuring per-sample PRW config files based on input files')
333 else:
334 log.info('No campaign specified, no PRW config files configured')
335
336 # check if user provides per-campaign lumical config list
337 if self.userLumicalcFilesPerCampaign is not None and self.userLumicalcFiles is not None:
338 raise ValueError('Both userLumicalcFiles and userLumicalcFilesYear specified, '
339 'use only one of the options!')
340 if self.userLumicalcFilesPerCampaign is not None:
341 try:
342 toolLumicalcFiles = self.userLumicalcFilesPerCampaign[campaign.value][:]
343 log.info('Using user-provided per-campaign lumicalc files')
344 except KeyError as e:
345 raise KeyError(f'Unconfigured campaign {e} for userLumicalcFilesPerCampaign!')
346 elif self.userLumicalcFiles is not None:
347 toolLumicalcFiles = self.userLumicalcFiles[:]
348 log.info('Using user-provided lumicalc files')
349 else:
350 if campaign and campaign is not Campaign.Unknown:
351 from PileupReweighting.AutoconfigurePRW import getLumicalcFiles
352 toolLumicalcFiles = getLumicalcFiles(campaign)
353 log.info('Using auto-configured lumicalc files')
354 else:
355 log.info('No campaign specified, no lumicalc files configured for PRW')
356 else:
357 log.info('Data needs no lumicalc and PRW configuration files')
358
359 # Set up the only algorithm of the sequence:
360 if config.geometry() is LHCPeriod.Run4:
361 warnings.warn_explicit(
362 'Pileup reweighting is not yet supported for Run 4 geometry',
363 Run4FallbackWarning, filename='', lineno=0)
364 alg = config.createAlgorithm( 'CP::EventDecoratorAlg', 'EventDecoratorAlg' )
365 alg.uint32Decorations = { 'RandomRunNumber' :
366 config.flags.Input.RunNumbers[0] }
367
368 else:
369 alg = config.createAlgorithm( 'CP::PileupReweightingAlg',
370 'PileupReweightingAlg' )
371 config.addPrivateTool( 'pileupReweightingTool', 'CP::PileupReweightingTool' )
372 alg.pileupReweightingTool.ConfigFiles = toolConfigFiles
373 if not toolConfigFiles and config.dataType() is not DataType.Data:
374 log.info("No PRW config files provided. Disabling reweighting")
375 # Setting the weight decoration to the empty string disables the reweighting
376 alg.pileupWeightDecoration = ""
377 else:
378 alg.pileupWeightDecoration = "PileupWeight" + self.postfix + "_%SYS%"
379 alg.pileupReweightingTool.LumiCalcFiles = toolLumicalcFiles
380 alg.pileupReweightingTool.UnrepresentedDataWarningThreshold = (
381 self.unrepresentedDataWarningThreshold)
382
383 if not self.alternativeConfig:
384 for var_name,var_type in eventInfoVar:
385 config.addOutputVar ('EventInfo', var_name, var_name, noSys=True, auxType=var_type)
386
387 if config.dataType() is not DataType.Data and config.geometry() is LHCPeriod.Run2:
388 config.addOutputVar ('EventInfo', 'beamSpotWeight', 'weight_beamspot', noSys=True, auxType='float')
389
390 if config.dataType() is not DataType.Data and toolConfigFiles:
391 config.addOutputVar ('EventInfo', 'PileupWeight' + self.postfix + '_%SYS%',
392 'weight_pileup'+self.postfix, auxType='float')
393
394
395class GeneratorAnalysisBlock (ConfigBlock):
396 """the ConfigBlock for generator algorithms"""
397
398 def __init__ (self) :
399 super (GeneratorAnalysisBlock, self).__init__ ()
400 self.addOption ('saveCutBookkeepers', True, type=bool,
401 info="whether to save the cut bookkeepers information into the "
402 "output file.")
403 self.addOption ('runNumber', None, type=int,
404 info="the MC `runNumber`. If left empty, autoconfigure from the sample metadata.")
405 self.addOption ('cutBookkeepersSystematics', None, type=bool,
406 info="whether to also save the cut bookkeepers systematics. The "
407 "default is `None` (follows the global systematics flag). Set to "
408 "`False` or `True` to override.")
409 self.addOption ('histPattern', None, type=str,
410 info="the histogram name pattern for the cut-bookkeeper histogram names.")
411 self.addOption ('streamName', None, type=str,
412 info="name of the output stream to save the cut bookkeeper in.")
413 self.addOption ('detailedPDFinfo', False, type=bool,
414 info="save the necessary information to run the LHAPDF tool offline.")
415 self.addOption ('doPDFReweighting', False, type=bool,
416 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).")
417 self.addOption ('outPDFName', [
418 "CT14nnloIC/0", "CT14nnloIC/1", "CT14nnloIC/2",
419 "CT18FC/0", "CT18FC/3", "CT18FC/6", "CT18FC/9",
420 "CT18NNLO/0", "CT18XNNLO/0",
421 "NNPDF40_nnlo_pch_as_01180/0", "NNPDF40_nnlo_as_01180/0"
422 ], type=list, info="list of PDF sets to use for PDF reweighting.")
423 self.addOption ('doHFProdFracReweighting', False, type=bool,
424 info="whether to apply HF production fraction reweighting.")
425 self.addOption ('truthParticleContainer', 'TruthParticles', type=str,
426 info="the name of the truth particle container to use for HF production fraction reweighting.")
427
428 def instanceName (self) :
429 """Return the instance name for this block"""
430 return self.streamName or "DEFAULT"
431
432 def makeAlgs (self, config) :
433
434 if config.dataType() is DataType.Data:
435 # there are no generator weights in data!
436 return
437 log = logging.getLogger('makeGeneratorAnalysisSequence')
438
439 # Setup stream name
440 streamName = self.streamName or config.defaultHistogramStream()
441
442 if self.runNumber is None:
443 self.runNumber = config.runNumber()
444
445 if self.saveCutBookkeepers and not self.runNumber:
446 raise ValueError ("invalid run number: " + str(self.runNumber))
447
448 # Set up the CutBookkeepers algorithm:
450 alg = config.createAlgorithm('CP::AsgCutBookkeeperAlg', 'CutBookkeeperAlg')
451 alg.RootStreamName = streamName
452 alg.runNumber = self.runNumber
453 if self.cutBookkeepersSystematics is None:
454 alg.enableSystematics = not config.noSystematics()
455 else:
456 alg.enableSystematics = self.cutBookkeepersSystematics
457 if self.histPattern:
458 alg.histPattern = self.histPattern
459 config.addPrivateTool( 'truthWeightTool', 'PMGTools::PMGTruthWeightTool' )
460
461 # Set up the weights algorithm:
462 alg = config.createAlgorithm( 'CP::PMGTruthWeightAlg', 'PMGTruthWeightAlg' )
463 config.addPrivateTool( 'truthWeightTool', 'PMGTools::PMGTruthWeightTool' )
464 alg.decoration = 'generatorWeight_%SYS%'
465 config.addOutputVar ('EventInfo', 'generatorWeight_%SYS%', 'weight_mc')
466
468 alg = config.createAlgorithm( 'CP::PDFinfoAlg', 'PDFinfoAlg', reentrant=True )
469 for var in ["PDFID1","PDFID2","PDGID1","PDGID2","Q","X1","X2","XF1","XF2"]:
470 config.addOutputVar ('EventInfo', var, 'PDFinfo_' + var, noSys=True)
471
473 alg = config.createAlgorithm( 'CP::PDFReweightAlg', 'PDFReweightAlg', reentrant=True )
474
475 for pdf_set in self.outPDFName:
476 config.addOutputVar('EventInfo', f'PDFReweightSF_{pdf_set.replace("/", "_")}',
477 f'PDFReweightSF_{pdf_set.replace("/", "_")}', noSys=True)
478
479
481 generatorInfo = config.flags.Input.GeneratorsInfo
482 log.info(f"Loaded generator info: {generatorInfo}")
483
484 DSID = "000000"
485
486 if not generatorInfo:
487 warnings.warn_explicit(
488 "No generator info found.",
489 GeneratorWeightWarning, filename='', lineno=0)
490 DSID = "000000"
491 elif isinstance(generatorInfo, dict):
492 if "Pythia8" in generatorInfo:
493 DSID = "410470"
494 elif "Sherpa" in generatorInfo and "2.2.8" in generatorInfo["Sherpa"]:
495 DSID = "421152"
496 elif "Sherpa" in generatorInfo and "2.2.10" in generatorInfo["Sherpa"]:
497 DSID = "700122"
498 elif "Sherpa" in generatorInfo and "2.2.11" in generatorInfo["Sherpa"]:
499 warnings.warn_explicit(
500 "HF production fraction reweighting is not configured"
501 " for Sherpa 2.2.11. Using weights for Sherpa 2.2.10"
502 " instead.",
503 GeneratorWeightWarning, filename='', lineno=0)
504 DSID = "700122"
505 elif "Sherpa" in generatorInfo and "2.2.12" in generatorInfo["Sherpa"]:
506 warnings.warn_explicit(
507 "HF production fraction reweighting is not configured"
508 " for Sherpa 2.2.12. Using weights for Sherpa 2.2.10"
509 " instead.",
510 GeneratorWeightWarning, filename='', lineno=0)
511 DSID = "700122"
512 elif "Sherpa" in generatorInfo and "2.2.14" in generatorInfo["Sherpa"]:
513 warnings.warn_explicit(
514 "HF production fraction reweighting is not configured"
515 " for Sherpa 2.2.14. New weights need to be"
516 " calculated.",
517 GeneratorWeightWarning, filename='', lineno=0)
518 DSID = "000000"
519 elif "Sherpa" in generatorInfo and "2.2.1" in generatorInfo["Sherpa"]:
520 DSID = "410250"
521 elif "Herwig7" in generatorInfo and "7.1.3" in generatorInfo["Herwig7"]:
522 DSID = "411233"
523 elif "Herwig7" in generatorInfo and "7.2.1" in generatorInfo["Herwig7"]:
524 DSID = "600666"
525 elif "Herwig7" in generatorInfo and "7." in generatorInfo["Herwig7"]:
526 DSID = "410558"
527 elif "amc@NLO" in generatorInfo:
528 DSID = "410464"
529 else:
530 warnings.warn_explicit(
531 f"HF production fraction reweighting is not configured"
532 f" for this generator: {generatorInfo}."
533 f" New weights need to be calculated.",
534 GeneratorWeightWarning, filename='', lineno=0)
535 DSID = "000000"
536 else:
537 warnings.warn_explicit(
538 "Failed to determine generator from metadata",
539 GeneratorWeightWarning, filename='', lineno=0)
540 DSID = "000000"
541
542 log.info(f"Using HF production fraction weights calculated using DSID {DSID}")
543 if DSID == "000000":
544 warnings.warn_explicit(
545 "HF production fraction reweighting will return dummy"
546 " weights of 1.0",
547 GeneratorWeightWarning, filename='', lineno=0)
548
549 alg = config.createAlgorithm( 'CP::SysTruthWeightAlg', f'SysTruthWeightAlg_{streamName}' )
550 config.addPrivateTool( 'sysTruthWeightTool', 'PMGTools::PMGHFProductionFractionTool' )
551 alg.decoration = 'prodFracWeight_%SYS%'
552 alg.TruthParticleContainer = self.truthParticleContainer
553 alg.sysTruthWeightTool.ShowerGenerator = DSID
554 config.addOutputVar ('EventInfo', 'prodFracWeight_%SYS%', 'weight_HF_prod_frac')
555
556class PtEtaSelectionBlock (ConfigBlock):
557 """the ConfigBlock for a pt-eta selection"""
558
559 def __init__ (self) :
560 super (PtEtaSelectionBlock, self).__init__ ()
561 self.addOption ('containerName', '', type=str,
562 noneAction='error',
563 info="the name of the input container.")
564 self.addOption ('selectionName', '', type=str,
565 noneAction='error',
566 info="the name of the selection to append this to. If left empty, "
567 "the cuts are applied to every "
568 "object within the container. Specifying a name (e.g. `loose`) "
569 "applies the cut only to those object who also pass that selection.")
570 self.addOption ('minPt', None, type=float,
571 info=r"minimum $p_\mathrm{T}$ value to cut on (in MeV).")
572 self.addOption ('maxPt', None, type=float,
573 info=r"maximum $p_\mathrm{T}$ value to cut on (in MeV).")
574 self.addOption ('minEta', None, type=float,
575 info=r"minimum $\vert\eta\vert$ value to cut on.")
576 self.addOption ('maxEta', None, type=float,
577 info=r"maximum $\vert\eta\vert$ value to cut on.")
578 self.addOption ('maxRapidity', None, type=float,
579 info="maximum rapidity value to cut on.")
580 self.addOption ('etaGapLow', None, type=float,
581 info=r"low end of the $\vert\eta\vert$ gap.")
582 self.addOption ('etaGapHigh', None, type=float,
583 info=r"high end of the $\vert\eta\vert$ gap.")
584 self.addOption ('selectionDecoration', None, type=str,
585 info="the name of the decoration to set. If `None`, will be set "
586 "to `selectPtEta` followed by the selection name.")
587 self.addOption ('useClusterEta', False, type=bool,
588 info=r"whether to use the cluster $\eta$ (`etaBE(2)`) instead of the object "
589 r"$\eta$ (for electrons and photons).")
590 self.addOption ('useDressedProperties', False, type=bool,
591 info="whether to use the dressed kinematic properties "
592 "(for truth particles only).")
593
594 def instanceName (self) :
595 """Return the instance name for this block"""
596 return self.containerName + "_" + self.selectionName
597
598 def makeAlgs (self, config) :
599
600 alg = config.createAlgorithm( 'CP::AsgSelectionAlg', 'PtEtaSelectionAlg' )
601 config.addPrivateTool( 'selectionTool', 'CP::AsgPtEtaSelectionTool' )
602 if self.minPt is not None :
603 alg.selectionTool.minPt = self.minPt
604 if self.maxPt is not None:
605 alg.selectionTool.maxPt = self.maxPt
606 if self.minEta is not None:
607 alg.selectionTool.minEta = self.minEta
608 if self.maxEta is not None :
609 alg.selectionTool.maxEta = self.maxEta
610 if self.maxRapidity is not None :
611 alg.selectionTool.maxRapidity = self.maxRapidity
612 if self.etaGapLow is not None:
613 alg.selectionTool.etaGapLow = self.etaGapLow
614 if self.etaGapHigh is not None:
615 alg.selectionTool.etaGapHigh = self.etaGapHigh
616 if self.selectionDecoration is None:
617 self.selectionDecoration = 'selectPtEta' + (f'_{self.selectionName}' if self.selectionName else '')
618 alg.selectionTool.useClusterEta = self.useClusterEta
619 alg.selectionTool.useDressedProperties = self.useDressedProperties
620 alg.selectionDecoration = self.selectionDecoration
621 alg.particles = config.readName (self.containerName)
622 alg.preselection = config.getPreselection (self.containerName, '')
623 config.addSelection (self.containerName, self.selectionName, alg.selectionDecoration)
624
625
626
627class ObjectCutFlowBlock (ConfigBlock):
628 """the ConfigBlock for an object cutflow"""
629
630 def __init__ (self) :
631 super (ObjectCutFlowBlock, self).__init__ ()
632 self.addOption ('containerName', '', type=str,
633 noneAction='error',
634 info="the name of the input container.")
635 self.addOption ('selectionName', '', type=str,
636 noneAction='error',
637 info="the name of the selection to perform the cutflow for. If left empty, "
638 "the cutflow is "
639 "performed for every object within the container. Specifying a "
640 "name (e.g. `loose`) generates the cutflow only for those objects "
641 "that also pass that selection.")
642 self.addOption ('forceCutSequence', False, type=bool,
643 info="whether to force the cut sequence and not accept objects "
644 "if previous cuts failed.")
645 self.addOption ('streamName', None, type=str,
646 info="name of the output stream to save the cutflow histogram in.")
647
648 def instanceName (self) :
649 """Return the instance name for this block"""
650 return self.containerName + '_' + self.selectionName
651
652 def makeAlgs (self, config) :
653 streamName = self.streamName or config.defaultHistogramStream()
654
655 alg = config.createAlgorithm( 'CP::ObjectCutFlowHistAlg', 'CutFlowDumperAlg' )
656 alg.RootStreamName = streamName
657 alg.histPattern = 'cflow_' + self.containerName + "_" + self.selectionName + '_%SYS%'
658 alg.selections = config.getSelectionCutFlow (self.containerName, self.selectionName)
659 alg.input = config.readName (self.containerName)
660 alg.histTitle = "Object Cutflow: " + self.containerName + "." + self.selectionName
661 alg.forceCutSequence = self.forceCutSequence
662
663
664class EventCutFlowBlock (ConfigBlock):
665 """the ConfigBlock for an event-level cutflow"""
666
667 def __init__(self):
668 super(EventCutFlowBlock, self).__init__()
669 self.addOption('selectionName', '', type=str,
670 noneAction='error',
671 info="the name of the event selection to generate cutflow histograms for. "
672 "If left blank, all selections on EventInfo will be used.")
673 self.addOption('customSelections', [], type=None,
674 info="explicit list of selection decorations to use for the cutflow. "
675 "If provided, takes precedence over selectionName.")
676 self.addOption('cutFlowHistograms', True, type=bool,
677 info="whether to generate cutflow histograms for the selection cuts.")
678 self.addOption ('streamName', None, type=str,
679 info="name of the output stream to save the cut bookkeeper in.")
680
681 def instanceName(self):
682 return 'EventInfo_' + self.selectionName
683
684 def makeAlgs(self, config):
685 if not self.cutFlowHistograms:
686 return
687
688 # Setup stream name
689 streamName = self.streamName or config.defaultHistogramStream()
690
691 postfix = ('_' + self.selectionName) if self.selectionName else ''
692
693 alg = config.createAlgorithm('CP::EventCutFlowHistAlg', 'CutFlowDumperAlg')
694 alg.RootStreamName = streamName
695 alg.histPattern = 'cflow_EventInfo' + postfix + '_%SYS%'
696 alg.eventInfo = config.readName('EventInfo')
697 alg.histTitle = 'Event Cutflow: EventInfo.' + self.selectionName
698
699 if isinstance(self.customSelections, list) and len(self.customSelections) > 0:
700 # user provides a hardcoded list of selections
701 alg.selections = self.customSelections
702 elif self.selectionName:
703 # resolve selectionName to the list of cuts registered by EventSelectionConfig
704 alg.selections = config.getEventCutFlow(self.selectionName)
705 else:
706 # fallback: get all available selections from EventInfo
707 alg.selections = config.getSelectionCutFlow('EventInfo', '')
708
709 alg.selections = [sel + ',as_char' for sel in alg.selections]
710
711class OutputThinningBlock (ConfigBlock):
712 """the ConfigBlock for output thinning"""
713
714 def __init__ (self) :
715 super (OutputThinningBlock, self).__init__ ()
716 self.setBlockName('Thinning')
717 self.addOption ('containerName', '', type=str,
718 noneAction='error',
719 info="the name of the input container.")
720 self.addOption ('postfix', '', type=str,
721 info="a postfix to apply to decorations and algorithm names. "
722 "Typically not needed here.")
723 self.addOption ('selection', '', type=str,
724 info="the name of an optional selection decoration to use.")
725 self.addOption ('selectionName', '', type=str,
726 info="the name of the selection to append this to. If left empty, "
727 "the cuts are applied to every "
728 "object within the container. Specifying a name (e.g. `loose`) "
729 "applies the cut only to those object who also pass that selection.")
730 self.addOption ('outputName', None, type=str,
731 info="an optional name for the output container.")
732 self.addOption ('deepCopy', False, type=bool,
733 info="run a deep copy of the container.")
734 self.addOption ('sortPt', False, type=bool,
735 info=r"whether to sort objects in $p_\mathrm{T}$.")
736 self.addOption ('noUniformSelection', False, type=bool,
737 info="do not run the union over all selections.")
738 self.addOption ('containerType', None, type=str,
739 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.")
740
741 def instanceName (self) :
742 """Return the instance name for this block"""
743 return self.containerName + '_' + self.selectionName + self.postfix
744
745 def makeAlgs (self, config) :
746
747 postfix = self.postfix
748 if postfix != '' and postfix[0] != '_' :
749 postfix = '_' + postfix
750
751 selection = config.getFullSelection (self.containerName, self.selectionName)
752 if selection == '' :
753 selection = self.selection
754 elif self.selection != '' :
755 selection = selection + '&&' + self.selection
756
757 if selection != '' and not self.noUniformSelection :
758 alg = config.createAlgorithm( 'CP::AsgUnionSelectionAlg', 'UnionSelectionAlg')
759 alg.preselection = selection
760 alg.particles = config.readName (self.containerName)
761 alg.selectionDecoration = 'outputSelect' + postfix
762 config.addSelection (self.containerName, alg.selectionDecoration, selection)
763 selection = 'outputSelect' + postfix
764
765 alg = config.createAlgorithm( 'CP::AsgViewFromSelectionAlg', 'DeepCopyAlg' )
766 alg.input = config.readName (self.containerName)
767 if self.outputName is not None :
768 alg.output = self.outputName + '_%SYS%'
769 config.addOutputContainer (self.containerName, self.outputName)
770 else :
771 alg.output = config.copyName (self.containerName)
772 if self.containerType is not None :
773 alg.outputType = self.containerType
774 if selection != '' :
775 alg.selection = [selection]
776 else :
777 alg.selection = []
778 alg.deepCopy = self.deepCopy
779 if self.sortPt and not config.noSystematics() :
780 raise ValueError ("Sorting by pt is not supported with systematics")
781 alg.sortPt = self.sortPt
782
783
784class IFFLeptonDecorationBlock (ConfigBlock):
785 """the ConfigBlock for the IFF classification of leptons"""
786
787 def __init__ (self) :
788 super (IFFLeptonDecorationBlock, self).__init__()
789 self.addOption ('containerName', '', type=str,
790 noneAction='error',
791 info="the name of the input electron or muon container.")
792 self.addOption ('separateChargeFlipElectrons', True, type=bool,
793 info="whether to consider charged-flip electrons as a separate class.")
794 self.addOption ('decoration', 'IFFClass_%SYS%', type=str,
795 info="the name of the decoration set by the IFF "
796 "`TruthClassificationTool`.")
797 # Always skip on data
798 self.setOptionValue('skipOnData', True)
799
800 def instanceName (self) :
801 """Return the instance name for this block"""
802 return self.containerName
803
804 def makeAlgs (self, config) :
805 particles = config.readName(self.containerName)
806
807 alg = config.createAlgorithm( 'CP::AsgClassificationDecorationAlg', 'IFFClassifierAlg' )
808 # the IFF classification tool
809 config.addPrivateTool( 'tool', 'TruthClassificationTool')
810 # label charge-flipped electrons as such
811 alg.tool.separateChargeFlipElectrons = self.separateChargeFlipElectrons
812 alg.decoration = self.decoration
813 alg.particles = particles
814
815 # write the decoration only once to the output
816 config.addOutputVar(self.containerName, alg.decoration, alg.decoration.split("_%SYS%")[0], noSys=True)
817
818
819class MCTCLeptonDecorationBlock (ConfigBlock):
820
821 def __init__ (self) :
822 super (MCTCLeptonDecorationBlock, self).__init__ ()
823
824 self.addOption ("containerName", '', type=str,
825 noneAction='error',
826 info="the input lepton container, with a possible selection, "
827 "in the format `container` or `container.selection`.")
828 self.addOption ("prefix", 'MCTC_', type=str,
829 info="the prefix of the decorations based on the MCTC "
830 "classification.")
831 # Always skip on data
832 self.setOptionValue('skipOnData', True)
833
834 def instanceName (self) :
835 """Return the instance name for this block"""
836 return self.containerName
837
838 def makeAlgs (self, config) :
839 particles, selection = config.readNameAndSelection(self.containerName)
840
841 alg = config.createAlgorithm ("CP::MCTCDecorationAlg", "MCTCDecorationAlg")
842 alg.particles = particles
843 alg.preselection = selection
844 alg.affectingSystematicsFilter = '.*'
845 config.addOutputVar (self.containerName, "MCTC_isPrompt", f"{self.prefix}isPrompt", noSys=True)
846 config.addOutputVar (self.containerName, "MCTC_fromHadron", f"{self.prefix}fromHadron", noSys=True)
847 config.addOutputVar (self.containerName, "MCTC_fromBSM", f"{self.prefix}fromBSM", noSys=True)
848 config.addOutputVar (self.containerName, "MCTC_fromTau", f"{self.prefix}fromTau", noSys=True)
849
850
851class PerEventSFBlock (ConfigBlock):
852 """the ConfigBlock for the AsgEventScaleFactorAlg"""
853
854 def __init__ (self):
855 super(PerEventSFBlock, self).__init__()
856 self.addOption('algoName', None, type=str,
857 info="unique name given to the underlying algorithm computing the "
858 "per-event scale factors.")
859 self.addOption('particles', '', type=str,
860 info="the input object container, with a possible selection, in the "
861 "format `container` or `container.selection`.")
862 self.addOption('objectSF', '', type=str,
863 info="the name of the per-object SF decoration to be used.")
864 self.addOption('eventSF', '', type=str,
865 info="the name of the per-event SF decoration.")
866
867 def instanceName (self) :
868 """Return the instance name for this block"""
869 return self.particles + '_' + self.objectSF + '_' + self.eventSF
870
871 def makeAlgs(self, config):
872 if config.dataType() is DataType.Data:
873 return
874 particles, selection = config.readNameAndSelection(self.particles)
875 alg = config.createAlgorithm('CP::AsgEventScaleFactorAlg', self.algoName if self.algoName else 'AsgEventScaleFactorAlg')
876 alg.particles = particles
877 alg.preselection = selection
878 alg.scaleFactorInputDecoration = self.objectSF
879 alg.scaleFactorOutputDecoration = self.eventSF
880
881 config.addOutputVar('EventInfo', alg.scaleFactorOutputDecoration,
882 alg.scaleFactorOutputDecoration.split("_%SYS%")[0])
883
884
885class SelectionDecorationBlock (ConfigBlock):
886 """the ConfigBlock to add selection decoration to a container"""
887
888 def __init__ (self) :
889 super (SelectionDecorationBlock, self).__init__ ()
890 # TODO: add info string
891 self.addOption('containers', [], type=list,
892 noneAction='error',
893 info="")
894
895 def instanceName (self) :
896 """Return the instance name for this block"""
897 return ''
898
899 def makeAlgs(self, config):
900 for container in self.containers:
901 originContainerName = config.getOutputContainerOrigin(container)
902 selectionNames = config.getSelectionNames(originContainerName)
903 for selectionName in selectionNames:
904 # skip default selection
905 if selectionName == '':
906 continue
907 alg = config.createAlgorithm(
908 'CP::AsgSelectionAlg',
909 f'SelectionDecoration_{originContainerName}_{selectionName}')
910 selectionDecoration = f'baselineSelection_{selectionName}_%SYS%'
911 alg.selectionDecoration = f'{selectionDecoration},as_char'
912 alg.particles = config.readName (originContainerName)
913 alg.preselection = config.getFullSelection (originContainerName,
914 selectionName)
915 config.addOutputVar(
916 originContainerName, selectionDecoration, selectionName)
STL class.