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