ATLAS Offline Software
Loading...
Searching...
No Matches
python.AsgAnalysisConfig.CommonServicesConfig Class Reference
Inheritance diagram for python.AsgAnalysisConfig.CommonServicesConfig:
Collaboration diagram for python.AsgAnalysisConfig.CommonServicesConfig:

Public Member Functions

 __init__ (self)
 instanceName (self)
 makeAlgs (self, config)

Public Attributes

 runSystematics = not config.noSystematics()
list onlySystematicsCategories = ['JER']
 separateWeightSystematics
 metadataHistogram
 enableExpertMode
 setupONNX

Static Protected Member Functions

 _ensureNominalAllowed (userRegex)

Detailed Description

the ConfigBlock for common services

The idea here is that all algorithms need some common services, and I should
provide configuration blocks for those.  For now there is just a single
block, but in the future I might break out e.g. the systematics service.

Definition at line 37 of file AsgAnalysisConfig.py.

Constructor & Destructor Documentation

◆ __init__()

python.AsgAnalysisConfig.CommonServicesConfig.__init__ ( self)

Definition at line 45 of file AsgAnalysisConfig.py.

45 def __init__ (self) :
46 super().__init__ ()
47 self.addOption ('runSystematics', None, type=bool,
48 info="whether to turn on the computation of systematic variations. "
49 "The default is to run them on MC.")
50 self.addOption ('filterSystematics', None, type=str,
51 info="a regexp string against which the systematics names will be "
52 "matched. Only positive matches are retained and used in the evaluation "
53 "of the various algorithms.")
54 self.addOption ('onlySystematicsCategories', None, type=list,
55 info="a list of strings defining categories of systematics to enable "
56 "(only recommended for studies / partial ntuple productions). Choose amongst: "
57 "`jets`, `JER`, `FTag`, `electrons`, `muons`, `photons`, `taus`, `met`, `tracks`, `generator`, `PRW`, `event`. "
58 "This option is overridden by `filterSystematics`.",
59 meta={'choices':(['jets', 'JER', 'FTag', 'electrons', 'muons', 'photons', 'taus', 'met', 'tracks', 'generator', 'PRW', 'event'], None)})
60 self.addOption ('systematicsHistogram', None , type=str,
61 info="the name of the histogram to which a list of executed "
62 "systematics will be printed. If left empty, the histogram is not written at all.")
63 self.addOption ('separateWeightSystematics', False, type=bool,
64 info="if `systematicsHistogram` is enabled, whether to create a separate "
65 "histogram holding only the names of weight-based systematics. This is useful "
66 "to help make histogramming frameworks more efficient by knowing in advance which "
67 "systematics need to recompute the observable and which don't.")
68 self.addOption ('metadataHistogram', 'metadata' , type=str,
69 info="the name of the metadata histogram which contains information about "
70 "data type, campaign, etc. If left empty, the histogram is not written at all.")
71 self.addOption ('enableExpertMode', False, type=bool,
72 info="allows CP experts and CPAlgorithm devs to use non-recommended configurations. "
73 "DO NOT USE FOR ANALYSIS.")
74 self.addOption ('streamName', None, type=str,
75 info="name of the output stream to save metadata histograms in.")
76 self.addOption ('setupONNX', False, type=bool,
77 info="creates an instance of `AthOnnx::OnnxRuntimeSvc`.")
78

Member Function Documentation

◆ _ensureNominalAllowed()

python.AsgAnalysisConfig.CommonServicesConfig._ensureNominalAllowed ( userRegex)
staticprotected
Make sure the systematics regex also accepts the nominal.

Definition at line 84 of file AsgAnalysisConfig.py.

84 def _ensureNominalAllowed (userRegex) :
85 """Make sure the systematics regex also accepts the nominal."""
86 if re.fullmatch (userRegex, '') is not None:
87 return userRegex
88 return f"(?:{userRegex})|^$"
89

◆ instanceName()

python.AsgAnalysisConfig.CommonServicesConfig.instanceName ( self)
Return the instance name for this block

Definition at line 79 of file AsgAnalysisConfig.py.

79 def instanceName (self) :
80 """Return the instance name for this block"""
81 return '' # no instance name, this is a singleton
82

◆ makeAlgs()

python.AsgAnalysisConfig.CommonServicesConfig.makeAlgs ( self,
config )

Definition at line 90 of file AsgAnalysisConfig.py.

90 def makeAlgs (self, config) :
91
92 sysService = config.createService( 'CP::SystematicsSvc', 'SystematicsSvc' )
93
94 # Setup stream name
95 streamName = config.requestOutputStream(self.streamName)
96
97 # Handle all possible configuration options for systematics
98 if self.runSystematics is False:
99 runSystematics = self.runSystematics
100 elif config.noSystematics() is not None:
101 # if option not set:
102 # check to see if set in config accumulator
103 self.runSystematics = not config.noSystematics()
104 runSystematics = self.runSystematics
105 else:
106 runSystematics = True
107
108 # Now update the global configuration
109 config._noSystematics = not runSystematics
110
111 if runSystematics:
112 sysService.sigmaRecommended = 1
113 if config.dataType() is DataType.Data:
114 # Only one type of allowed systematics on data: the JER variations!
115 self.onlySystematicsCategories = ['JER']
116 if self.onlySystematicsCategories is not None:
117 # Convert strings to enums and validate
118 requested_categories = set()
119 for category_str in self.onlySystematicsCategories:
120 try:
121 category_enum = SystematicsCategories[category_str.upper()]
122 requested_categories |= category_enum.value
123 except KeyError:
124 raise ValueError(f"Invalid systematics category passed to option 'onlySystematicsCategories': {category_str}. Must be one of {', '.join(category.name for category in SystematicsCategories)}")
125 # Construct regex pattern as logical-OR of category names
126 if len(requested_categories):
127 sysService.systematicsRegex = "^(?=.*(" + "|".join(sorted(requested_categories)) + ")|$).*"
128 if self.filterSystematics is not None:
129 sysService.systematicsRegex = self._ensureNominalAllowed(self.filterSystematics)
130 config.createService( 'CP::SelectionNameSvc', 'SelectionNameSvc')
131
132 if self.systematicsHistogram is not None:
133 # print out all systematics
134 allSysDumper = config.createAlgorithm( 'CP::SysListDumperAlg', 'SystematicsPrinter' )
135 allSysDumper.histogramName = self.systematicsHistogram
136 allSysDumper.RootStreamName = streamName
137
138 if self.separateWeightSystematics:
139 # print out only the weight systematics (for more efficient histogramming down the line)
140 weightSysDumper = config.createAlgorithm( 'CP::SysListDumperAlg', 'OnlyWeightSystematicsPrinter' )
141 weightSysDumper.histogramName = f"{self.systematicsHistogram}OnlyWeights"
142 weightSysDumper.systematicsRegex = "^(GEN_|EL_EFF_|MUON_EFF_|PH_EFF_|TAUS_TRUEHADTAU_EFF_|FT_EFF_|JET_.*JvtEfficiency_|PRW_).*"
143 weightSysDumper.RootStreamName = streamName
144
145 if self.metadataHistogram:
146 # add histogram with metadata
147 if not config.flags:
148 raise ValueError ("Writing out the metadata histogram requires to pass config flags")
149 metadataHistAlg = config.createAlgorithm( 'CP::MetadataHistAlg', 'MetadataHistAlg' )
150 metadataHistAlg.histogramName = self.metadataHistogram
151 metadataHistAlg.dataType = str(config.dataType().value)
152 metadataHistAlg.campaign = str(config.dataYear()) if config.dataType() is DataType.Data else str(config.campaign().value)
153 metadataHistAlg.mcChannelNumber = str(config.dsid())
154 metadataHistAlg.RootStreamName = streamName
155 if config.dataType() is DataType.Data:
156 etag = "unavailable"
157 else:
158 from AthenaConfiguration.AutoConfigFlags import GetFileMD
159 metadata = GetFileMD(config.flags.Input.Files)
160 amiTags = metadata.get("AMITag", "not found!")
161 etag = str(amiTags.split("_")[0])
162 metadataHistAlg.etag = etag
163
164 if self.enableExpertMode:
165 # set any expert-mode errors to be ignored instead
166 warnings.simplefilter('ignore', ExpertModeWarning)
167 # just warning users they might be doing something dangerous
168 log = logging.getLogger('CommonServices')
169 bold = "\033[1m"
170 red = "\033[91m"
171 yellow = "\033[93m"
172 reset = "\033[0m"
173 log.warning(red +r"""
174 ________ _______ ______ _____ _______ __ __ ____ _____ ______ ______ _ _ ____ _ ______ _____
175 | ____\ \ / / __ \| ____| __ \__ __| | \/ |/ __ \| __ \| ____| | ____| \ | | /\ | _ \| | | ____| __ \
176 | |__ \ V /| |__) | |__ | |__) | | | | \ / | | | | | | | |__ | |__ | \| | / \ | |_) | | | |__ | | | |
177 | __| > < | ___/| __| | _ / | | | |\/| | | | | | | | __| | __| | . ` | / /\ \ | _ <| | | __| | | | |
178 | |____ / . \| | | |____| | \ \ | | | | | | |__| | |__| | |____ | |____| |\ |/ ____ \| |_) | |____| |____| |__| |
179 |______/_/ \_\_| |______|_| \_\ |_| |_| |_|\____/|_____/|______| |______|_| \_/_/ \_\____/|______|______|_____/
180
181"""
182 +reset)
183 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}")
184
185 if self.setupONNX:
186 config.createService('AthOnnx::OnnxRuntimeSvc', 'OnnxRuntimeSvc')
187 config.setContainerMeta ('EventInfo', 'OnnxEnabled', self.setupONNX)
188
189@groupBlocks
STL class.

Member Data Documentation

◆ enableExpertMode

python.AsgAnalysisConfig.CommonServicesConfig.enableExpertMode

Definition at line 164 of file AsgAnalysisConfig.py.

◆ metadataHistogram

python.AsgAnalysisConfig.CommonServicesConfig.metadataHistogram

Definition at line 145 of file AsgAnalysisConfig.py.

◆ onlySystematicsCategories

list python.AsgAnalysisConfig.CommonServicesConfig.onlySystematicsCategories = ['JER']

Definition at line 115 of file AsgAnalysisConfig.py.

◆ runSystematics

python.AsgAnalysisConfig.CommonServicesConfig.runSystematics = not config.noSystematics()

Definition at line 103 of file AsgAnalysisConfig.py.

◆ separateWeightSystematics

python.AsgAnalysisConfig.CommonServicesConfig.separateWeightSystematics

Definition at line 138 of file AsgAnalysisConfig.py.

◆ setupONNX

python.AsgAnalysisConfig.CommonServicesConfig.setupONNX

Definition at line 185 of file AsgAnalysisConfig.py.


The documentation for this class was generated from the following file: