2 from typing
import Iterable, Union
4 from AnalysisAlgorithmsConfig.ConfigBlock
import ConfigBlock
5 from AnalysisAlgorithmsConfig.ConfigAccumulator
import DataType, ConfigAccumulator
6 from AthenaConfiguration.Enums
import LHCPeriod
7 from Campaigns.Utils
import Campaign
8 from TriggerAnalysisAlgorithms.TriggerAnalysisConfig
import TriggerAnalysisBlock, is_year_in_current_period
11 def is_mc_from(config: ConfigAccumulator, campaign_list: Union[Campaign, Iterable[Campaign]]) -> bool:
13 Utility function to check whether the data type is mc and whether the campaign is in desired list of campaigns
14 without causing an invalid FlugEnum comparison
16 campaign_list = [campaign_list]
if isinstance(campaign_list, Campaign)
else campaign_list
17 return config.dataType()
is not DataType.Data
and config.campaign()
in campaign_list
20 def is_data_from(config, data_year_list: Union[int, Iterable[int]]) -> bool:
22 Utility function to check whether the data type is data and whether the year is in desired list of years
24 data_year_list = [data_year_list]
if isinstance(data_year_list, int)
else data_year_list
25 return config.dataType()
is DataType.Data
and config.dataYear()
in data_year_list
30 Utility function to get the data for a specific year from a dictionary
32 return dictionary.get(
int(year), dictionary.get(
str(year), []))
37 Utility function to get the list of years that the input corresponds to
40 if config.campaign()
is Campaign.MC20a:
46 elif config.campaign()
is Campaign.MC20d
or is_data_from(config, 2017):
48 elif config.campaign()
is Campaign.MC20e
or is_data_from(config, 2018):
50 elif config.campaign()
is Campaign.MC23a
or is_data_from(config, 2022):
52 elif config.campaign()
is Campaign.MC23d
or is_data_from(config, 2023):
54 elif config.campaign()
is Campaign.MC23e
or is_data_from(config, 2024):
56 elif config.campaign()
is Campaign.MC23g
or is_data_from(config, 2025):
59 raise ValueError(
'TriggerAnalysisConfig: unable to deduce data taking year for input file')
65 """the ConfigBlock for trigger analysis"""
67 super(TriggerAnalysisSFBlock, self).
__init__()
68 self.addDependency(
'Electrons', required=
False)
69 self.addDependency(
'Photons', required=
False)
70 self.addDependency(
'Muons', required=
False)
71 self.addDependency(
'Taus', required=
False)
72 self.addDependency(
'OverlapRemoval', required=
False)
74 self.addOption (
'triggerChainsPerYear', {}, type=
None,
75 info=
"a dictionary with key (string) the year and value (list of "
76 "strings) the trigger chains. You can also use || within a string "
77 "to enforce an OR of triggers without looking up the individual "
78 "triggers. Used for both trigger selection and SFs. "
79 "The default is {} (empty dictionary).")
80 self.addOption (
'multiTriggerChainsPerYear', {}, type=
None,
81 info=
"a dictionary with key (string) a trigger set name and value a "
82 "triggerChainsPerYear dictionary, following the previous convention. "
83 "Relevant for analyses using different triggers in different categories, "
84 "where the trigger global scale factors shouldn't be combined. "
85 "The default is {} (empty dictionary).")
86 self.addOption (
'noFilter',
False, type=bool,
87 info=
"do not apply an event filter. The default is False, i.e. "
88 "remove events not passing trigger selection and matching.")
89 self.addOption (
'electronID',
'', type=str,
90 info=
"the electron ID WP (string) to use.")
91 self.addOption (
'electronIsol',
'', type=str,
92 info=
"the electron isolation WP (string) to use.")
93 self.addOption (
'photonIsol',
'', type=str,
94 info=
"the photon isolation WP (string) to use.")
95 self.addOption (
'muonID',
'', type=str,
96 info=
"the muon quality WP (string) to use.")
97 self.addOption (
'electrons',
'', type=str,
98 info=
"the input electron container, with a possible selection, in "
99 "the format container or container.selection.")
100 self.addOption (
'muons',
'', type=str,
101 info=
"the input muon container, with a possible selection, in the "
102 "format container or container.selection.")
103 self.addOption (
'photons',
'', type=str,
104 info=
"the input photon container, with a possible selection, in "
105 "the format container or container.selection.")
106 self.addOption (
'taus',
'', type=str,
107 info=
"the input tau container, with a possible selection, in "
108 "the format container or container.selection.")
109 self.addOption (
'numberOfToys', 0, type=int,
110 info=
"Number of toy experiments to run to estimate the trigger efficiencies, "
111 "instead of using explicit formulas. The default is 0 (not using toys).")
112 self.addOption (
'noEffSF',
False, type=bool,
113 info=
"disables the calculation of efficiencies and scale factors. "
114 "Experimental! only useful to test a new WP for which scale "
115 "factors are not available. Still performs the global trigger "
116 "matching (same behaviour as on data). The default is False.")
117 self.addOption (
'noGlobalTriggerEff',
False, type=bool,
118 info=
"disables the global trigger efficiency tool (including "
119 "matching), which is only suited for electron/muon/photon "
120 "trigger legs. The default is False.")
121 self.addOption (
'separateChainMatching',
False, type=bool,
122 info=
"Store the matching status for each trigger separately")
123 self.addOption (
'triggerMatchingChainsPerYear', {}, type=
None,
124 info=
"a dictionary with key (string) the year and value (list of "
125 "strings) the trigger chains. The default is {} (empty dictionary).")
126 self.addOption(
"includeAllYearsPerRun",
False, type=bool,
127 info=
"if True, trigger matching will include all configured years "
128 "in the LHC run in all jobs. The default is False.")
129 self.addOption (
'postfix',
'', type=str,
130 info=
"a unique identifier for the trigger matching decorations. Only "
131 "useful when defining multiple setups. The default is '' (empty string).")
135 config: ConfigAccumulator,
138 triggerSuffix: str =
''
140 alg = config.createAlgorithm(
'CP::TrigGlobalEfficiencyAlg',
'TrigGlobalSFAlg' + triggerSuffix + self.postfix)
141 if config.geometry()
is LHCPeriod.Run3:
147 if not alg.triggers_2022:
148 raise ValueError(
'TriggerAnalysisConfig: you must provide a set of triggers for the year 2022!')
150 if not alg.triggers_2023:
151 raise ValueError(
'TriggerAnalysisConfig: you must provide a set of triggers for the year 2023!')
158 if not (alg.triggers_2015
and alg.triggers_2016):
159 raise ValueError(
'TriggerAnalysisConfig: you must provide a set of triggers for the years 2015 and 2016!')
161 if not alg.triggers_2015:
162 raise ValueError(
'TriggerAnalysisConfig: you must provide a set of triggers for the year 2015!')
164 if not alg.triggers_2016:
165 raise ValueError(
'TriggerAnalysisConfig: you must provide a set of triggers for the year 2016!')
167 if not alg.triggers_2017:
168 raise ValueError(
'TriggerAnalysisConfig: you must provide a set of triggers for the year 2017!')
170 if not alg.triggers_2018:
171 raise ValueError(
'TriggerAnalysisConfig: you must provide a set of triggers for the year 2018!')
173 triggerMatchingChains =
set()
174 if self.separateChainMatching:
177 triggerMatchingChains.update(trig.replace(
' || ',
'_OR_').
split(
'_OR_'))
178 alg.separateMatchingTriggers =
list(triggerMatchingChains)
179 alg.separateMatchingDecorationSuffix = triggerSuffix + self.postfix
181 alg.matchingTool =
'%s/%s' % ( matchingTool.getType(), matchingTool.getName() )
182 alg.isRun3Geo = config.geometry()
is LHCPeriod.Run3
183 alg.numberOfToys = self.numberOfToys
184 alg.scaleFactorDecoration =
'globalTriggerEffSF' + triggerSuffix + self.postfix +
'_%SYS%'
185 alg.matchingDecoration =
'globalTriggerMatch' + triggerSuffix + self.postfix +
'_%SYS%'
186 alg.eventDecisionOutputDecoration =
'globalTriggerMatch' + triggerSuffix + self.postfix +
'_dontsave_%SYS%'
187 alg.doMatchingOnly = config.dataType()
is DataType.Data
or noSF
188 alg.noFilter = self.noFilter
189 alg.electronID = self.electronID
190 alg.electronIsol = self.electronIsol
191 alg.photonIsol = self.photonIsol
192 alg.muonID = self.muonID
194 alg.electrons, alg.electronSelection = config.readNameAndSelection(self.electrons)
196 alg.muons, alg.muonSelection = config.readNameAndSelection(self.muons)
198 alg.photons, alg.photonSelection = config.readNameAndSelection(self.photons)
199 if not (self.electrons
or self.muons
or self.photons):
200 raise ValueError(
'TriggerAnalysisConfig: at least one object collection must be provided! (electrons, muons, photons)' )
202 if config.dataType()
is not DataType.Data
and not alg.doMatchingOnly:
203 config.addOutputVar(
'EventInfo', alg.scaleFactorDecoration,
'globalTriggerEffSF' + triggerSuffix + self.postfix)
204 config.addOutputVar(
'EventInfo', alg.matchingDecoration,
'globalTriggerMatch' + triggerSuffix + self.postfix, noSys=
False)
206 for trig
in triggerMatchingChains:
207 var = f
'triggerMatch_{trig.replace("-", "_").replace(".", "p")}{alg.separateMatchingDecorationSuffix}'
208 config.addOutputVar(
'EventInfo', f
'{var}_%SYS%', var, noSys=
False)
214 config: ConfigAccumulator,
217 trig_string: str =
"",
218 trig_chains: set =
None,
219 trig_chains_dummy: set =
None,
221 if not particles
or (
not trig_chains
and not trig_chains_dummy):
224 if not any(trig_string
in trig
for trig
in trig_chains) \
225 and not any(trig_string
in trig
for trig
in trig_chains_dummy):
228 alg = config.createAlgorithm(
"CP::TrigMatchingAlg", f
"TrigMatchingAlg_{trig_string}{self.postfix}")
229 alg.matchingTool = f
"{matchingTool.getType()}/{matchingTool.getName()}"
230 alg.matchingDecoration = f
"trigMatched{self.postfix}"
231 alg.trigSingleMatchingList = [trig
for trig
in trig_chains
if trig_string
in trig]
232 alg.trigSingleMatchingListDummy = [trig
for trig
in trig_chains_dummy
if trig_string
in trig]
233 alg.particles, alg.particleSelection = config.readNameAndSelection(particles)
235 for trig
in list(alg.trigSingleMatchingList) +
list(alg.trigSingleMatchingListDummy):
237 if trig_string
in trig:
238 config.addOutputVar(particles.split(
".")[0], f
"trigMatched_{self.postfix}{trig}", f
"trigMatched_{self.postfix}{trig}", noSys=
True)
242 config: ConfigAccumulator,
247 triggerMatchingChains =
set()
248 triggerMatchingChainsDummy =
set()
250 for trig
in get_year_data(self.triggerMatchingChainsPerYear, year):
251 trig = trig.replace(
' || ',
'_OR_')
252 triggerMatchingChains.update(trig.split(
'_OR_'))
253 if self.includeAllYearsPerRun:
254 triggerMatchingChainsAll =
set()
255 for year
in self.triggerMatchingChainsPerYear:
258 for trig
in get_year_data(self.triggerMatchingChainsPerYear, year):
259 trig = trig.replace(
' || ',
'_OR_')
260 triggerMatchingChainsAll.update(trig.split(
'_OR_'))
261 triggerMatchingChainsDummy = triggerMatchingChainsAll - triggerMatchingChains
263 self.
createTrigMatching(config, matchingTool, self.electrons,
'HLT_e', triggerMatchingChains, triggerMatchingChainsDummy)
264 self.
createTrigMatching(config, matchingTool, self.muons,
'HLT_mu', triggerMatchingChains, triggerMatchingChainsDummy)
265 self.
createTrigMatching(config, matchingTool, self.photons,
'HLT_g', triggerMatchingChains, triggerMatchingChainsDummy)
266 self.
createTrigMatching(config, matchingTool, self.taus,
'HLT_tau', triggerMatchingChains, triggerMatchingChainsDummy)
270 def makeAlgs(self, config: ConfigAccumulator) ->
None:
276 raise Exception(
'multiTriggerChainsPerYear and triggerChainsPerYear cannot be configured at the same time!')
282 decisionTool = TriggerAnalysisBlock.makeTriggerDecisionTool(config)
285 matchingTool = TriggerAnalysisBlock.makeTriggerMatchingTool(config, decisionTool)
294 if self.triggerMatchingChainsPerYear: