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