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), []))
36 """the ConfigBlock for trigger analysis"""
38 super(TriggerAnalysisSFBlock, self).
__init__()
39 self.addDependency(
'Electrons', required=
False)
40 self.addDependency(
'Photons', required=
False)
41 self.addDependency(
'Muons', required=
False)
42 self.addDependency(
'Taus', required=
False)
43 self.addDependency(
'OverlapRemoval', required=
False)
45 self.addOption (
'triggerChainsPerYear', {}, type=
None,
46 info=
"a dictionary with key (string) the year and value (list of "
47 "strings) the trigger chains. You can also use || within a string "
48 "to enforce an OR of triggers without looking up the individual "
49 "triggers. Used for both trigger selection and SFs. "
50 "The default is {} (empty dictionary).")
51 self.addOption (
'multiTriggerChainsPerYear', {}, type=
None,
52 info=
"a dictionary with key (string) a trigger set name and value a "
53 "triggerChainsPerYear dictionary, following the previous convention. "
54 "Relevant for analyses using different triggers in different categories, "
55 "where the trigger global scale factors shouldn't be combined. "
56 "The default is {} (empty dictionary).")
57 self.addOption (
'noFilter',
False, type=bool,
58 info=
"do not apply an event filter. The default is False, i.e. "
59 "remove events not passing trigger selection and matching.")
60 self.addOption (
'electronID',
'', type=str,
61 info=
"the electron ID WP (string) to use.")
62 self.addOption (
'electronIsol',
'', type=str,
63 info=
"the electron isolation WP (string) to use.")
64 self.addOption (
'photonIsol',
'', type=str,
65 info=
"the photon isolation WP (string) to use.")
66 self.addOption (
'muonID',
'', type=str,
67 info=
"the muon quality WP (string) to use.")
68 self.addOption (
'electrons',
'', type=str,
69 info=
"the input electron container, with a possible selection, in "
70 "the format container or container.selection.")
71 self.addOption (
'muons',
'', type=str,
72 info=
"the input muon container, with a possible selection, in the "
73 "format container or container.selection.")
74 self.addOption (
'photons',
'', type=str,
75 info=
"the input photon container, with a possible selection, in "
76 "the format container or container.selection.")
77 self.addOption (
'taus',
'', type=str,
78 info=
"the input tau container, with a possible selection, in "
79 "the format container or container.selection.")
80 self.addOption (
'noEffSF',
False, type=bool,
81 info=
"disables the calculation of efficiencies and scale factors. "
82 "Experimental! only useful to test a new WP for which scale "
83 "factors are not available. Still performs the global trigger "
84 "matching (same behaviour as on data). The default is False.")
85 self.addOption (
'noGlobalTriggerEff',
False, type=bool,
86 info=
"disables the global trigger efficiency tool (including "
87 "matching), which is only suited for electron/muon/photon "
88 "trigger legs. The default is False.")
89 self.addOption (
'triggerMatchingChainsPerYear', {}, type=
None,
90 info=
"a dictionary with key (string) the year and value (list of "
91 "strings) the trigger chains. The default is {} (empty dictionary).")
92 self.addOption(
"includeAllYears",
False, type=bool,
93 info=
"if True, trigger matching will include all configured years "
94 "in all jobs. The default is False.")
95 self.addOption (
'postfix',
'', type=str,
96 info=
"a unique identifier for the trigger matching decorations. Only "
97 "useful when defining multiple setups. The default is '' (empty string).")
101 config: ConfigAccumulator,
104 triggerSuffix: str =
''
106 alg = config.createAlgorithm(
'CP::TrigGlobalEfficiencyAlg',
'TrigGlobalSFAlg' + triggerSuffix + self.postfix)
107 if config.geometry()
is LHCPeriod.Run3:
113 if not alg.triggers_2022:
114 raise ValueError(
'TriggerAnalysisConfig: you must provide a set of triggers for the year 2022!')
116 if not alg.triggers_2023:
117 raise ValueError(
'TriggerAnalysisConfig: you must provide a set of triggers for the year 2023!')
124 if not (alg.triggers_2015
and alg.triggers_2016):
125 raise ValueError(
'TriggerAnalysisConfig: you must provide a set of triggers for the years 2015 and 2016!')
127 if not alg.triggers_2015:
128 raise ValueError(
'TriggerAnalysisConfig: you must provide a set of triggers for the year 2015!')
130 if not alg.triggers_2016:
131 raise ValueError(
'TriggerAnalysisConfig: you must provide a set of triggers for the year 2016!')
133 if not alg.triggers_2017:
134 raise ValueError(
'TriggerAnalysisConfig: you must provide a set of triggers for the year 2017!')
136 if not alg.triggers_2018:
137 raise ValueError(
'TriggerAnalysisConfig: you must provide a set of triggers for the year 2018!')
139 alg.matchingTool =
'%s/%s' % ( matchingTool.getType(), matchingTool.getName() )
140 alg.isRun3Geo = config.geometry()
is LHCPeriod.Run3
141 alg.scaleFactorDecoration =
'globalTriggerEffSF' + triggerSuffix + self.postfix +
'_%SYS%'
142 alg.matchingDecoration =
'globalTriggerMatch' + triggerSuffix + self.postfix +
'_%SYS%'
143 alg.eventDecisionOutputDecoration =
'globalTriggerMatch' + triggerSuffix + self.postfix +
'_dontsave_%SYS%'
144 alg.doMatchingOnly = config.dataType()
is DataType.Data
or noSF
145 alg.noFilter = self.noFilter
146 alg.electronID = self.electronID
147 alg.electronIsol = self.electronIsol
148 alg.photonIsol = self.photonIsol
149 alg.muonID = self.muonID
151 alg.electrons, alg.electronSelection = config.readNameAndSelection(self.electrons)
153 alg.muons, alg.muonSelection = config.readNameAndSelection(self.muons)
155 alg.photons, alg.photonSelection = config.readNameAndSelection(self.photons)
156 if not (self.electrons
or self.muons
or self.photons):
157 raise ValueError(
'TriggerAnalysisConfig: at least one object collection must be provided! (electrons, muons, photons)' )
159 if config.dataType()
is not DataType.Data
and not alg.doMatchingOnly:
160 config.addOutputVar(
'EventInfo', alg.scaleFactorDecoration,
'globalTriggerEffSF' + triggerSuffix + self.postfix)
161 config.addOutputVar(
'EventInfo', alg.matchingDecoration,
'globalTriggerMatch' + triggerSuffix + self.postfix, noSys=
False)
167 config: ConfigAccumulator,
170 trig_string: str =
"",
171 trig_chains: set =
None,
172 trig_chains_dummy: set =
None,
174 if not particles
or (
not trig_chains
and not trig_chains_dummy):
177 if not any(trig_string
in trig
for trig
in trig_chains) \
178 and not any(trig_string
in trig
for trig
in trig_chains_dummy):
181 alg = config.createAlgorithm(
"CP::TrigMatchingAlg", f
"TrigMatchingAlg_{trig_string}{self.postfix}")
182 alg.matchingTool = f
"{matchingTool.getType()}/{matchingTool.getName()}"
183 alg.matchingDecoration = f
"trigMatched{self.postfix}"
184 alg.trigSingleMatchingList = [trig
for trig
in trig_chains
if trig_string
in trig]
185 alg.trigSingleMatchingListDummy = [trig
for trig
in trig_chains_dummy
if trig_string
in trig]
186 alg.particles, alg.particleSelection = config.readNameAndSelection(particles)
188 for trig
in list(alg.trigSingleMatchingList) +
list(alg.trigSingleMatchingListDummy):
190 if trig_string
in trig:
191 config.addOutputVar(particles.split(
".")[0], f
"trigMatched_{self.postfix}{trig}", f
"trigMatched_{self.postfix}{trig}", noSys=
True)
195 config: ConfigAccumulator,
199 if config.campaign()
is Campaign.MC20a: years = [2015, 2016]
202 elif config.campaign()
is Campaign.MC20d
or is_data_from(config, 2017): years = [2017]
203 elif config.campaign()
is Campaign.MC20e
or is_data_from(config, 2018): years = [2018]
204 elif config.campaign()
in [Campaign.MC21a, Campaign.MC23a]
or is_data_from(config, 2022): years = [2022]
205 elif config.campaign()
in [Campaign.MC23c, Campaign.MC23d]
or is_data_from(config, 2023): years = [2023]
207 triggerMatchingChains =
set()
208 triggerMatchingChainsDummy =
set()
210 for trig
in get_year_data(self.triggerMatchingChainsPerYear, year):
211 trig = trig.replace(
' || ',
'_OR_')
212 triggerMatchingChains.update(trig.split(
'_OR_'))
213 if self.includeAllYears:
214 triggerMatchingChainsAll =
set()
215 for year
in self.triggerMatchingChainsPerYear:
216 for trig
in get_year_data(self.triggerMatchingChainsPerYear, year):
217 trig = trig.replace(
' || ',
'_OR_')
218 triggerMatchingChainsAll.update(trig.split(
'_OR_'))
219 triggerMatchingChainsDummy = triggerMatchingChainsAll - triggerMatchingChains
221 self.
createTrigMatching(config, matchingTool, self.electrons,
'HLT_e', triggerMatchingChains, triggerMatchingChainsDummy)
222 self.
createTrigMatching(config, matchingTool, self.muons,
'HLT_mu', triggerMatchingChains, triggerMatchingChainsDummy)
223 self.
createTrigMatching(config, matchingTool, self.photons,
'HLT_g', triggerMatchingChains, triggerMatchingChainsDummy)
224 self.
createTrigMatching(config, matchingTool, self.taus,
'HLT_tau', triggerMatchingChains, triggerMatchingChainsDummy)
228 def makeAlgs(self, config: ConfigAccumulator) ->
None:
234 raise Exception(
'multiTriggerChainsPerYear and triggerChainsPerYear cannot be configured at the same time!')
240 decisionTool = TriggerAnalysisBlock.makeTriggerDecisionTool(config)
243 matchingTool = TriggerAnalysisBlock.makeTriggerMatchingTool(config, decisionTool)
252 if self.triggerMatchingChainsPerYear: