ATLAS Offline Software
Loading...
Searching...
No Matches
UncertaintySet.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
5
9
12#include <set>
13
14namespace jet
15{
16
18// //
19// Constructor/destructor/initialization //
20// //
22
23UncertaintySet::UncertaintySet(const std::string& name)
24 : asg::AsgMessaging(name)
25 , m_name(name)
26 , m_isInit(false)
27 , m_groups()
29{ }
30
32= default;
33
34StatusCode UncertaintySet::initialize(const CP::SystematicSet& systConfig, const std::vector<UncertaintyGroup*>& groups)
35{
36 // Ensure it wasn't already initialized
37 if (m_isInit)
38 {
39 ATH_MSG_ERROR("Blocking double-initialization for: " << m_name.c_str());
40 return StatusCode::FAILURE;
41 }
42
43 // Parse all of the names to get the groups
44 std::set<UncertaintyGroup*> compSet;
45 for (CP::SystematicSet::const_iterator iter = systConfig.begin(); iter != systConfig.end(); ++iter)
46 for (size_t iGroup = 0; iGroup < groups.size(); ++iGroup)
47 if (groups.at(iGroup)->getName().CompareTo(iter->basename().c_str()) == 0)
48 {
49 // Watch for double-specified elements
50 if (compSet.count(groups.at(iGroup)))
51 {
52 ATH_MSG_ERROR("Multiple independent shifts requested for a single group: " << groups.at(iGroup)->getName().Data());
53 return StatusCode::FAILURE;
54 }
55 compSet.insert(groups.at(iGroup));
56
57 m_groups.push_back(groups.at(iGroup));
58 m_shiftWeights.push_back(iter->parameter());
59 break;
60 }
61
62
63 // Ensure all of the elements were found
64 if (systConfig.size() != m_groups.size())
65 {
66 ATH_MSG_ERROR(Form("Input SystematicSet is %zu elements, but only found %zu UncertaintyGroups",systConfig.size(),m_groups.size()));
67 return StatusCode::FAILURE;
68 }
69
70 // All good
71 m_isInit = true;
72 return StatusCode::SUCCESS;
73}
74
75std::string UncertaintySet::getName() const
76{
77 return m_name;
78}
79
81// //
82// Uncertainty/validity retrieval methods //
83// //
85
87{
88 std::set<CompScaleVar::TypeEnum> vars = m_groups.at(0)->getScaleVars();
89 if (vars.size() != 1)
91 const CompScaleVar::TypeEnum singleVar = *(vars.begin());
92
93 for (size_t iGroup = 1; iGroup < m_groups.size(); ++iGroup)
94 {
95 std::set<CompScaleVar::TypeEnum> vars2 = m_groups.at(iGroup)->getScaleVars();
96 if (vars2.size() != 1 || singleVar != *(vars2.begin()))
97 {
99 }
100 }
101
102 return singleVar;
103}
104
106{
107 if (m_groups.empty())
108 return true;
109
110 // If the scale var wasn't specified, need to ensure all groups match
111 const bool checkScaleVar = scaleVar == CompScaleVar::UNKNOWN;
112 const CompScaleVar::TypeEnum scaleVariable = checkScaleVar ? getSingleVar() : scaleVar;
113 if (scaleVariable == CompScaleVar::UNKNOWN)
114 {
115 ATH_MSG_ERROR("Asked for validity of a multi-scale variable set without specifying any scale variable: " << m_name);
116 return false;
117 }
118
119 // Checked conditions, now get the validity
120 for (size_t iGroup = 0; iGroup < m_groups.size(); ++iGroup)
121 if (!m_groups.at(iGroup)->getValidity(jet,eInfo,scaleVariable))
122 return false;
123 return true;
124}
125
127{
128 // This is the sum of shifts multiple uncorrelated groups
129 // This could be a positive or negative number
130 // Each individual group uncertainty will always be positive (quad sum of subgroups)
131 // But the weight may be positive or negative (+N sigma or -N sigma variations)
132
133 if (m_groups.empty())
134 return 0;
135
136 // If the scale var wasn't specified, need to ensure all groups match
137 const bool checkScaleVar = scaleVar == CompScaleVar::UNKNOWN;
138 const CompScaleVar::TypeEnum scaleVariable = checkScaleVar ? getSingleVar() : scaleVar;
139 if (scaleVariable == CompScaleVar::UNKNOWN)
140 {
141 ATH_MSG_ERROR("Asked for uncertainty of a multi-scale-variable set without specifying any scale variable: " << m_name);
142 return JESUNC_ERROR_CODE;
143 }
144
145 // Checked conditions, now get the uncertainty
146 double unc = 0;
147 for (size_t iGroup = 0; iGroup < m_groups.size(); ++iGroup)
148 {
149 unc += m_groups.at(iGroup)->getUncertainty(jet,eInfo,scaleVariable)*m_shiftWeights.at(iGroup);
150 }
151
152 return unc;
153}
154
155bool UncertaintySet::getValidUncertainty(double& unc, const xAOD::Jet& jet, const xAOD::EventInfo& eInfo, const CompScaleVar::TypeEnum scaleVar) const
156{
157 // See getUncertainty for discussion
158 // This is not just calling the other two methods for speed, O(n) instead of O(2n)
159
160 if (m_groups.empty())
161 {
162 unc = 0;
163 return true;
164 }
165
166 // If the scale var wasn't specified, need to ensure all groups match
167 const bool checkScaleVar = scaleVar == CompScaleVar::UNKNOWN;
168 const CompScaleVar::TypeEnum scaleVariable = checkScaleVar ? getSingleVar() : scaleVar;
169 if (scaleVariable == CompScaleVar::UNKNOWN)
170 {
171 ATH_MSG_ERROR("Asked for valid uncertainty of a multi-scale-variable set without specifying any scale variable: " << m_name);
172 return false;
173 }
174
175 // Checked conditions, now get the uncertainty and validity
176 double localUnc = 0;
177 for (size_t iGroup = 0; iGroup < m_groups.size(); ++iGroup)
178 {
179 if (!m_groups.at(iGroup)->getValidity(jet,eInfo,scaleVariable))
180 return false;
181 localUnc += m_groups.at(iGroup)->getUncertainty(jet,eInfo,scaleVariable)*m_shiftWeights.at(iGroup);
182 }
183 unc = localUnc;
184 return true;
185}
186
188// //
189// Advanced uncertainty/validity methods //
190// //
192
193std::vector<CompScaleVar::TypeEnum> UncertaintySet::getScaleVars() const
194{
195 // Make a super-set and fill it from each group
196 std::set<CompScaleVar::TypeEnum> scaleVarSet;
197 for (size_t iGroup = 0; iGroup < m_groups.size(); ++iGroup)
198 {
199 std::set<CompScaleVar::TypeEnum> localSet = m_groups.at(iGroup)->getScaleVars();
200 scaleVarSet.insert(localSet.begin(),localSet.end());
201 }
202
203 // Convert to a vector
204 std::vector<CompScaleVar::TypeEnum> scaleVars;
205 std::set<CompScaleVar::TypeEnum>::const_iterator iter;
206 for (iter = scaleVarSet.begin(); iter != scaleVarSet.end(); ++iter)
207 scaleVars.push_back(*(iter));
208 return scaleVars;
209}
210
211std::vector< std::pair<CompScaleVar::TypeEnum,bool> > UncertaintySet::getValiditySet(const xAOD::Jet& jet, const xAOD::EventInfo& eInfo) const
212{
213 std::vector< std::pair<CompScaleVar::TypeEnum,bool> > validity;
214
215 // Simple case
216 if (m_groups.empty())
217 return validity;
218
219 // Get the sets
220 const std::vector<CompScaleVar::TypeEnum> scaleVars = getScaleVars();
221
222 // Process the sets one by one
223 for (size_t iVar = 0; iVar < scaleVars.size(); ++iVar)
224 validity.emplace_back(scaleVars.at(iVar),getValidity(jet,eInfo,scaleVars.at(iVar)));
225
226 // Done, return
227 return validity;
228}
229
230std::vector< std::pair<CompScaleVar::TypeEnum,double> > UncertaintySet::getUncertaintySet(const xAOD::Jet& jet, const xAOD::EventInfo& eInfo) const
231{
232 std::vector< std::pair<CompScaleVar::TypeEnum,double> > unc;
233
234 // Simple case
235 if (m_groups.empty())
236 return unc;
237
238 // Get the sets
239 const std::vector<CompScaleVar::TypeEnum> scaleVars = getScaleVars();
240
241 // Process the sets one by one
242 for (size_t iVar = 0; iVar < scaleVars.size(); ++iVar)
243 unc.emplace_back(scaleVars.at(iVar),getUncertainty(jet,eInfo,scaleVars.at(iVar)));
244
245 // Done, return
246 return unc;
247}
248
249std::vector< std::pair<CompScaleVar::TypeEnum,bool> > UncertaintySet::getValidUncertaintySet(std::vector< std::pair<CompScaleVar::TypeEnum,double> >& unc, const xAOD::Jet& jet, const xAOD::EventInfo& eInfo) const
250{
251 std::vector< std::pair<CompScaleVar::TypeEnum,bool> > validity;
252 std::vector< std::pair<CompScaleVar::TypeEnum,double> > localUnc;
253
254 // Simple case
255 if (m_groups.empty())
256 {
257 unc = std::move(localUnc);
258 return validity;
259 }
260
261 // Get the sets
262 const std::vector<CompScaleVar::TypeEnum> scaleVars = getScaleVars();
263
264 // Process the sets one by one
265 for (size_t iVar = 0; iVar < scaleVars.size(); ++iVar)
266 {
267 double localUncValue = JESUNC_ERROR_CODE;
268 validity.emplace_back(scaleVars.at(iVar),getValidUncertainty(localUncValue,jet,eInfo,scaleVars.at(iVar)));
269 localUnc.emplace_back(scaleVars.at(iVar),localUncValue);
270 }
271
272 // Done, return
273 unc = std::move(localUnc);
274 return validity;
275}
276
277
279// //
280// Specialty methods //
281// //
283
285{
286 // The topology is not normally needed
287 // Most variables are topology-independent, and thus are listed as "UNKNOWN"
288 // Mixing topology-agnostic and topology-specific variables is expected
289 // Mixing topology-specific variables of different topologies is not expected
290
291 // Furthermore, the user can specify either a specific scale variable or not
292 // If the user doesn't specify (or specifies UNKNOWN, the default), then it checks all
293
295
296 for (const UncertaintyGroup* group : m_groups)
297 {
298 if (group->getTopology() != JetTopology::UNKNOWN)
299 {
301 result = group->getTopology();
302 else if (result != group->getTopology(scaleVar))
304 }
306 return result; // If it's mixed, it won't change any further
307 }
308
309 return result; // If we got here, it's not a mixed topology, so return what it is
310}
311
312
313} // end jet namespace
314
#define ATH_MSG_ERROR(x)
Class to wrap a set of SystematicVariations.
const_iterator end() const
description: const iterator to the end of the set
std::set< SystematicVariation >::const_iterator const_iterator
const_iterator begin() const
description: const iterator to the beginning of the set
size_t size() const
returns: size of the set
AsgMessaging(const std::string &name)
Constructor with a name.
virtual std::vector< std::pair< CompScaleVar::TypeEnum, bool > > getValiditySet(const xAOD::Jet &jet, const xAOD::EventInfo &eInfo) const
virtual std::vector< std::pair< CompScaleVar::TypeEnum, bool > > getValidUncertaintySet(std::vector< std::pair< CompScaleVar::TypeEnum, double > > &unc, const xAOD::Jet &jet, const xAOD::EventInfo &eInfo) const
virtual double getUncertainty(const xAOD::Jet &jet, const xAOD::EventInfo &eInfo, const CompScaleVar::TypeEnum scaleVar=CompScaleVar::UNKNOWN) const
std::vector< float > m_shiftWeights
virtual bool getValidity(const xAOD::Jet &jet, const xAOD::EventInfo &eInfo, const CompScaleVar::TypeEnum scaleVar=CompScaleVar::UNKNOWN) const
virtual StatusCode initialize(const CP::SystematicSet &systConfig, const std::vector< UncertaintyGroup * > &groups)
const std::string m_name
UncertaintySet(const std::string &name="")
JetTopology::TypeEnum getTopology(const CompScaleVar::TypeEnum scaleVar=CompScaleVar::UNKNOWN) const
virtual bool getValidUncertainty(double &unc, const xAOD::Jet &jet, const xAOD::EventInfo &eInfo, const CompScaleVar::TypeEnum scaleVar=CompScaleVar::UNKNOWN) const
virtual ~UncertaintySet()
virtual std::string getName() const
virtual std::vector< CompScaleVar::TypeEnum > getScaleVars() const
virtual std::vector< std::pair< CompScaleVar::TypeEnum, double > > getUncertaintySet(const xAOD::Jet &jet, const xAOD::EventInfo &eInfo) const
CompScaleVar::TypeEnum getSingleVar() const
std::vector< UncertaintyGroup * > m_groups
Jet_v1 Jet
Definition of the current "jet version".
EventInfo_v1 EventInfo
Definition of the latest event info version.