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
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()
in [Campaign.MC21a, Campaign.MC23a]
or is_data_from(config, 2022):
52 elif config.campaign()
in [Campaign.MC23c, Campaign.MC23d]
or is_data_from(config, 2023):
59 """the ConfigBlock for trigger analysis"""
61 super(TriggerAnalysisSFBlock, self).
__init__()
62 self.addDependency(
'Electrons', required=
False)
63 self.addDependency(
'Photons', required=
False)
64 self.addDependency(
'Muons', required=
False)
65 self.addDependency(
'Taus', required=
False)
66 self.addDependency(
'OverlapRemoval', required=
False)
68 self.addOption (
'triggerChainsPerYear', {}, type=
None,
69 info=
"a dictionary with key (string) the year and value (list of "
70 "strings) the trigger chains. You can also use || within a string "
71 "to enforce an OR of triggers without looking up the individual "
72 "triggers. Used for both trigger selection and SFs. "
73 "The default is {} (empty dictionary).")
74 self.addOption (
'multiTriggerChainsPerYear', {}, type=
None,
75 info=
"a dictionary with key (string) a trigger set name and value a "
76 "triggerChainsPerYear dictionary, following the previous convention. "
77 "Relevant for analyses using different triggers in different categories, "
78 "where the trigger global scale factors shouldn't be combined. "
79 "The default is {} (empty dictionary).")
80 self.addOption (
'noFilter',
False, type=bool,
81 info=
"do not apply an event filter. The default is False, i.e. "
82 "remove events not passing trigger selection and matching.")
83 self.addOption (
'electronID',
'', type=str,
84 info=
"the electron ID WP (string) to use.")
85 self.addOption (
'electronIsol',
'', type=str,
86 info=
"the electron isolation WP (string) to use.")
87 self.addOption (
'photonIsol',
'', type=str,
88 info=
"the photon isolation WP (string) to use.")
89 self.addOption (
'muonID',
'', type=str,
90 info=
"the muon quality WP (string) to use.")
91 self.addOption (
'electrons',
'', type=str,
92 info=
"the input electron container, with a possible selection, in "
93 "the format container or container.selection.")
94 self.addOption (
'muons',
'', type=str,
95 info=
"the input muon container, with a possible selection, in the "
96 "format container or container.selection.")
97 self.addOption (
'photons',
'', type=str,
98 info=
"the input photon container, with a possible selection, in "
99 "the format container or container.selection.")
100 self.addOption (
'taus',
'', type=str,
101 info=
"the input tau container, with a possible selection, in "
102 "the format container or container.selection.")
103 self.addOption (
'noEffSF',
False, type=bool,
104 info=
"disables the calculation of efficiencies and scale factors. "
105 "Experimental! only useful to test a new WP for which scale "
106 "factors are not available. Still performs the global trigger "
107 "matching (same behaviour as on data). The default is False.")
108 self.addOption (
'noGlobalTriggerEff',
False, type=bool,
109 info=
"disables the global trigger efficiency tool (including "
110 "matching), which is only suited for electron/muon/photon "
111 "trigger legs. The default is False.")
112 self.addOption (
'triggerMatchingChainsPerYear', {}, type=
None,
113 info=
"a dictionary with key (string) the year and value (list of "
114 "strings) the trigger chains. The default is {} (empty dictionary).")
115 self.addOption(
"includeAllYears",
False, type=bool,
116 info=
"if True, trigger matching will include all configured years "
117 "in all jobs. The default is False.")
118 self.addOption (
'postfix',
'', type=str,
119 info=
"a unique identifier for the trigger matching decorations. Only "
120 "useful when defining multiple setups. The default is '' (empty string).")
124 config: ConfigAccumulator,
127 triggerSuffix: str =
''
129 alg = config.createAlgorithm(
'CP::TrigGlobalEfficiencyAlg',
'TrigGlobalSFAlg' + triggerSuffix + self.postfix)
130 if config.geometry()
is LHCPeriod.Run3:
136 if not alg.triggers_2022:
137 raise ValueError(
'TriggerAnalysisConfig: you must provide a set of triggers for the year 2022!')
139 if not alg.triggers_2023:
140 raise ValueError(
'TriggerAnalysisConfig: you must provide a set of triggers for the year 2023!')
147 if not (alg.triggers_2015
and alg.triggers_2016):
148 raise ValueError(
'TriggerAnalysisConfig: you must provide a set of triggers for the years 2015 and 2016!')
150 if not alg.triggers_2015:
151 raise ValueError(
'TriggerAnalysisConfig: you must provide a set of triggers for the year 2015!')
153 if not alg.triggers_2016:
154 raise ValueError(
'TriggerAnalysisConfig: you must provide a set of triggers for the year 2016!')
156 if not alg.triggers_2017:
157 raise ValueError(
'TriggerAnalysisConfig: you must provide a set of triggers for the year 2017!')
159 if not alg.triggers_2018:
160 raise ValueError(
'TriggerAnalysisConfig: you must provide a set of triggers for the year 2018!')
162 alg.matchingTool =
'%s/%s' % ( matchingTool.getType(), matchingTool.getName() )
163 alg.isRun3Geo = config.geometry()
is LHCPeriod.Run3
164 alg.scaleFactorDecoration =
'globalTriggerEffSF' + triggerSuffix + self.postfix +
'_%SYS%'
165 alg.matchingDecoration =
'globalTriggerMatch' + triggerSuffix + self.postfix +
'_%SYS%'
166 alg.eventDecisionOutputDecoration =
'globalTriggerMatch' + triggerSuffix + self.postfix +
'_dontsave_%SYS%'
167 alg.doMatchingOnly = config.dataType()
is DataType.Data
or noSF
168 alg.noFilter = self.noFilter
169 alg.electronID = self.electronID
170 alg.electronIsol = self.electronIsol
171 alg.photonIsol = self.photonIsol
172 alg.muonID = self.muonID
174 alg.electrons, alg.electronSelection = config.readNameAndSelection(self.electrons)
176 alg.muons, alg.muonSelection = config.readNameAndSelection(self.muons)
178 alg.photons, alg.photonSelection = config.readNameAndSelection(self.photons)
179 if not (self.electrons
or self.muons
or self.photons):
180 raise ValueError(
'TriggerAnalysisConfig: at least one object collection must be provided! (electrons, muons, photons)' )
182 if config.dataType()
is not DataType.Data
and not alg.doMatchingOnly:
183 config.addOutputVar(
'EventInfo', alg.scaleFactorDecoration,
'globalTriggerEffSF' + triggerSuffix + self.postfix)
184 config.addOutputVar(
'EventInfo', alg.matchingDecoration,
'globalTriggerMatch' + triggerSuffix + self.postfix, noSys=
False)
190 config: ConfigAccumulator,
193 trig_string: str =
"",
194 trig_chains: set =
None,
195 trig_chains_dummy: set =
None,
197 if not particles
or (
not trig_chains
and not trig_chains_dummy):
200 if not any(trig_string
in trig
for trig
in trig_chains) \
201 and not any(trig_string
in trig
for trig
in trig_chains_dummy):
204 alg = config.createAlgorithm(
"CP::TrigMatchingAlg", f
"TrigMatchingAlg_{trig_string}{self.postfix}")
205 alg.matchingTool = f
"{matchingTool.getType()}/{matchingTool.getName()}"
206 alg.matchingDecoration = f
"trigMatched{self.postfix}"
207 alg.trigSingleMatchingList = [trig
for trig
in trig_chains
if trig_string
in trig]
208 alg.trigSingleMatchingListDummy = [trig
for trig
in trig_chains_dummy
if trig_string
in trig]
209 alg.particles, alg.particleSelection = config.readNameAndSelection(particles)
211 for trig
in list(alg.trigSingleMatchingList) +
list(alg.trigSingleMatchingListDummy):
213 if trig_string
in trig:
214 config.addOutputVar(particles.split(
".")[0], f
"trigMatched_{self.postfix}{trig}", f
"trigMatched_{self.postfix}{trig}", noSys=
True)
218 config: ConfigAccumulator,
223 triggerMatchingChains =
set()
224 triggerMatchingChainsDummy =
set()
226 for trig
in get_year_data(self.triggerMatchingChainsPerYear, year):
227 trig = trig.replace(
' || ',
'_OR_')
228 triggerMatchingChains.update(trig.split(
'_OR_'))
229 if self.includeAllYears:
230 triggerMatchingChainsAll =
set()
231 for year
in self.triggerMatchingChainsPerYear:
232 for trig
in get_year_data(self.triggerMatchingChainsPerYear, year):
233 trig = trig.replace(
' || ',
'_OR_')
234 triggerMatchingChainsAll.update(trig.split(
'_OR_'))
235 triggerMatchingChainsDummy = triggerMatchingChainsAll - triggerMatchingChains
237 self.
createTrigMatching(config, matchingTool, self.electrons,
'HLT_e', triggerMatchingChains, triggerMatchingChainsDummy)
238 self.
createTrigMatching(config, matchingTool, self.muons,
'HLT_mu', triggerMatchingChains, triggerMatchingChainsDummy)
239 self.
createTrigMatching(config, matchingTool, self.photons,
'HLT_g', triggerMatchingChains, triggerMatchingChainsDummy)
240 self.
createTrigMatching(config, matchingTool, self.taus,
'HLT_tau', triggerMatchingChains, triggerMatchingChainsDummy)
244 def makeAlgs(self, config: ConfigAccumulator) ->
None:
250 raise Exception(
'multiTriggerChainsPerYear and triggerChainsPerYear cannot be configured at the same time!')
256 decisionTool = TriggerAnalysisBlock.makeTriggerDecisionTool(config)
259 matchingTool = TriggerAnalysisBlock.makeTriggerMatchingTool(config, decisionTool)
268 if self.triggerMatchingChainsPerYear: