ATLAS Offline Software
SystematicSet.cxx
Go to the documentation of this file.
1 // Author: Steve Farrell (steven.farrell@cern.ch)
2 
3 
4 //
5 // includes
6 //
7 
8 #include <stdexcept>
9 #include <map>
10 
11 #include <boost/functional/hash.hpp>
12 
15 
16 // Comparison operator for std::map sorting
18 {
19  return a.name() < b.name();
20 }
21 
22 // Equality operator for unordered containers
24 {
25  return a.name() == b.name();
26 }
27 
28 
29 //
30 // method implementations
31 //
32 
33 namespace CP
34 {
35 
36  // Default constructor
38  {
39  }
40 
41  // Constructor for splitting a single systematics string
43  : SystematicSet()
44  {
45  if (!systematics.empty()) {
46  std::string::size_type split = 0, split2 = 0;
47  while ((split = systematics.find ("-", split2)) != std::string::npos) {
48  m_sysVariations.insert (systematics.substr (split2, split - split2));
49  split2 = split + 1;
50  }
51  m_sysVariations.insert (systematics.substr (split2, split - split2));
52  }
53  }
54 
55 
56  // Constructor for a vector of systematic names
57  SystematicSet::SystematicSet(const std::vector<std::string>& systematics)
58  : SystematicSet()
59  {
60  for (const auto & sys : systematics) {
61  m_sysVariations.insert(sys);
62  }
63  }
64 
65  // Constructor for a vector of SystematicVariation
67  (const std::vector<SystematicVariation>& systematics)
68  : SystematicSet()
69  {
70  for (const auto & sys : systematics) {
71  m_sysVariations.insert(sys);
72  }
73  }
74 
75 
76  // Constructor for an initializer_list of SystematicVariation
78  (const std::initializer_list<SystematicVariation>& systematics)
79  : SystematicSet()
80  {
81  for (const auto & sys : systematics) {
82  m_sysVariations.insert(sys);
83  }
84  }
85 
86 
87  // Insert a systematic into the set
89  {
90  // If insert doesn't change the set, don't change cache flag
91  if(m_sysVariations.insert(systematic).second) {
93  m_hash.reset();
94  }
95  }
96 
97 
98  // Insert a SystematicSet into this set
100  {
101  for (const auto & sys : systematics) {
102  insert(sys);
103  }
104  }
105 
106 
107  // Swap elements of the set
109  {
110  m_sysVariations.swap(otherSet.m_sysVariations);
112  otherSet.m_joinedName.reset();
113  m_hash.reset();
114  otherSet.m_hash.reset();
115  }
116 
117 
118  // Clear the systematics and the rest of the state
120  {
121  m_sysVariations.clear();
123  m_hash.reset();
124  }
125 
126 
127  // Match full systematic or continuous systematic
129  MATCHTYPE type) const
130  {
131  if (m_sysVariations.find(systematic) != m_sysVariations.end()) {
132  return true;
133  }
134  if (type == FULLORCONTINUOUS) {
135  const SystematicVariation continuous(systematic.basename(),
137  if (m_sysVariations.find(continuous) != m_sysVariations.end()) {
138  return true;
139  }
140  }
141  return false;
142  }
143 
144 
145  // Get subset of systematics matching basename
147  (const std::string& basename) const
148  {
149  SystematicSet filteredSysts;
150  for (const auto & sys : m_sysVariations) {
151  if (sys.basename() == basename)
152  filteredSysts.insert(sys);
153  }
154  return filteredSysts;
155  }
156 
157 
158  // Get the set of systematic base names
159  std::set<std::string> SystematicSet::getBaseNames() const
160  {
161  std::set<std::string> baseNames;
162  for (const auto & sys : m_sysVariations) {
163  baseNames.insert(sys.basename());
164  }
165  return baseNames;
166  }
167 
168 
169  // Get the first systematic that matches basename
172  {
173  const SystematicVariation* sysMatched = NULL;
174  for (const auto & sys : m_sysVariations) {
175  if(sys.basename() == basename) {
176  if(!sysMatched) sysMatched = &sys;
177  else {
178  std::string error = "SystematicSet::getSystematicByBaseName ERROR: ";
179  error += "Multiple matches for requested basename ";
180  error += basename;
181  // Redundant?
182  //std::cerr << error << std::endl;
183  throw std::runtime_error(error);
184  }
185  }
186  }
187  if(sysMatched) return *sysMatched;
188  return SystematicVariation();
189  }
190 
191 
193  getParameterByBaseName(const std::string& basename) const
194  {
196  }
197 
198 
199 
200  std::pair<unsigned,float> SystematicSet ::
201  getToyVariationByBaseName (const std::string& basename) const
202  {
203  const auto var = getSystematicByBaseName (basename);
204  if (var.empty())
205  return std::make_pair (0, 0);
206  return var.getToyVariation();
207  }
208 
209 
210 
211  // Filter requested systematics with affecting systematics
213  (const SystematicSet& systConfig, const SystematicSet& affectingSysts,
214  SystematicSet& filteredSysts)
215  {
216  using namespace msgSystematics;
217 
218  // the final filtered systematics to report
220 
221  // the map of all requested systematics by base name
222  std::map<std::string,SystematicVariation> requestedMap;
223 
224  // the list of all inconsistent systematics we encountered
225  std::set<SystematicVariation> inconsistentList;
226 
227  // fill requestedMap, reporting errors in case of duplication
228  for (auto& sys : systConfig)
229  {
230  std::string basename = sys.basename();
231  auto iter = requestedMap.find (basename);
232  if (iter != requestedMap.end())
233  {
234  ANA_MSG_ERROR ("inconsistent systematic variations requested: " << sys << " and " << iter->second);
235  return StatusCode::FAILURE;
236  }
237  requestedMap.insert (std::make_pair (basename, sys));
238  }
239 
240  // check which of the systematics match the affecting
241  for (auto& sys : affectingSysts)
242  {
243  std::string basename = sys.basename();
244  auto iter = requestedMap.find (basename);
245  if (iter != requestedMap.end())
246  {
247  if (iter->second == sys ||
248  sys.ensembleContains (iter->second))
249  {
250  result.insert (iter->second);
251  } else
252  {
253  // let's remember this as a potential problem
254  inconsistentList.insert (iter->second);
255  }
256  }
257  }
258 
259  // check whether any of of the requested variations matched the
260  // base names of our systematics, but not the systematics
261  // supported
262  for (auto& sys : inconsistentList)
263  {
264  if (result.find (sys) == result.end())
265  {
266  ANA_MSG_ERROR ("unsupported systematic variation " << sys << " requested for systematic " << sys.basename());
267  return StatusCode::FAILURE;
268  }
269  }
270 
271  // everything worked out, let's commit now
272  result.swap (filteredSysts);
273  return StatusCode::SUCCESS;
274  }
275 
276 
277  // Return the cached joined systematic name
278  std::string SystematicSet::name() const
279  {
280  if(!m_joinedName.isValid())
281  {
283  }
284  return *m_joinedName.ptr();
285  }
286 
287 
288  // Return the cached hash value
289  std::size_t SystematicSet::hash() const
290  {
291  if(!m_hash.isValid()) {
292  m_hash.set (computeHash());
293  }
294  return *m_hash.ptr();
295  }
296 
297 
298  // Join systematics into a single string
299  std::string SystematicSet::joinNames() const
300  {
301  std::string joinedName;
302  for (const auto & sys : m_sysVariations) {
303  if (!joinedName.empty()) {
304  joinedName += "-";
305  }
306  joinedName += sys.name();
307  }
308  return joinedName;
309  }
310 
311 
312  // Compute the hash value for this SystematicSet
313  std::size_t SystematicSet::computeHash() const
314  {
315  static const std::hash<std::string> hashFunction;
316  //static boost::hash<std::string> hashFunction;
317  return hashFunction(name());
318  }
319 
320  // Hash function for boost hash
321  std::size_t hash_value(const SystematicSet& sysSet)
322  {
323  return sysSet.hash();
324  }
325 
326 }
beamspotnt.var
var
Definition: bin/beamspotnt.py:1394
CxxUtils::CachedValue::reset
void reset()
Reset the value to invalid.
CP::SystematicVariation::CONTINUOUS
@ CONTINUOUS
Definition: SystematicVariation.h:79
CP::SystematicSet::getToyVariationByBaseName
std::pair< unsigned, float > getToyVariationByBaseName(const std::string &basename) const
the toy variation for the given basename
Definition: SystematicSet.cxx:201
CxxUtils::CachedValue::ptr
const T * ptr() const
Return a pointer to the cached value.
get_generator_info.result
result
Definition: get_generator_info.py:21
CP::SystematicSet::swap
void swap(SystematicSet &otherSet)
description: swap elements of a set
Definition: SystematicSet.cxx:108
CP::SystematicVariation::basename
std::string basename() const
description: the base name, i.e.
Definition: SystematicVariation.cxx:312
operator<
bool operator<(const CP::SystematicSet &a, const CP::SystematicSet &b)
Definition: SystematicSet.cxx:17
CP::SystematicSet::joinNames
std::string joinNames() const
description: join systematic names into single string
Definition: SystematicSet.cxx:299
CxxUtils::CachedValue::isValid
bool isValid() const
Test to see if the value is valid.
CP::SystematicSet::computeHash
std::size_t computeHash() const
description: compute and store the hash value
Definition: SystematicSet.cxx:313
CP::SystematicSet::m_hash
CxxUtils::CachedValue< std::size_t > m_hash
description: cached hash value for quick retrieval in unordered containers
Definition: SystematicSet.h:169
SystematicSet.h
CP::SystematicSet
Class to wrap a set of SystematicVariations.
Definition: SystematicSet.h:31
CP::SystematicSet::name
std::string name() const
returns: the systematics joined into a single string.
Definition: SystematicSet.cxx:278
CP::SystematicSet::filterByBaseName
SystematicSet filterByBaseName(const std::string &basename) const
description: get the subset of systematics matching basename Should this return a StatusCode instead?
Definition: SystematicSet.cxx:147
ANA_MSG_ERROR
#define ANA_MSG_ERROR(xmsg)
Macro printing error messages.
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:294
MessageCheck.h
CP::SystematicVariation
Definition: SystematicVariation.h:47
CP::SystematicSet::MATCHTYPE
MATCHTYPE
description: match systematic or continuous version
Definition: SystematicSet.h:90
CP
Select isolated Photons, Electrons and Muons.
Definition: Control/xAODRootAccess/xAODRootAccess/TEvent.h:48
CP::SystematicSet::getParameterByBaseName
float getParameterByBaseName(const std::string &basename) const
returns: the parameter value for the given basename
Definition: SystematicSet.cxx:193
mapkey::sys
@ sys
Definition: TElectronEfficiencyCorrectionTool.cxx:42
CP::SystematicSet::m_sysVariations
std::set< SystematicVariation > m_sysVariations
description: the set of systematics encapsulated in this class
Definition: SystematicSet.h:162
CP::SystematicSet::hash
std::size_t hash() const
returns: hash value for the joined string.
Definition: SystematicSet.cxx:289
CP::SystematicSet::getSystematicByBaseName
SystematicVariation getSystematicByBaseName(const std::string &basename) const
description: get the first systematic matching basename
Definition: SystematicSet.cxx:171
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
CP::SystematicSet::FULLORCONTINUOUS
@ FULLORCONTINUOUS
Definition: SystematicSet.h:90
CP::SystematicSet::getBaseNames
std::set< std::string > getBaseNames() const
description: get the set of base systematic names from this set
Definition: SystematicSet.cxx:159
CP::SystematicSet::m_joinedName
CxxUtils::CachedValue< std::string > m_joinedName
description: cache the joined string, useful for hash
Definition: SystematicSet.h:165
CheckAppliedSFs.systematics
def systematics
Definition: CheckAppliedSFs.py:231
CP::SystematicSet::insert
void insert(const SystematicVariation &systematic)
description: insert a systematic into the set
Definition: SystematicSet.cxx:88
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
CxxUtils::CachedValue::set
void set(const T &val) const
Set the value, assuming it is currently invalid.
CP::SystematicSet::clear
void clear()
description: clear the set
Definition: SystematicSet.cxx:119
a
TList * a
Definition: liststreamerinfos.cxx:10
CP::SystematicSet::SystematicSet
SystematicSet()
construct an empty set
Definition: SystematicSet.cxx:37
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
CP::SystematicSet::matchSystematic
bool matchSystematic(const SystematicVariation &systematic, MATCHTYPE type=FULL) const
Definition: SystematicSet.cxx:128
error
Definition: IImpactPoint3dEstimator.h:70
Trk::split
@ split
Definition: LayerMaterialProperties.h:38
CP::SystematicSet::filterForAffectingSystematics
static StatusCode filterForAffectingSystematics(const SystematicSet &systConfig, const SystematicSet &affectingSystematics, SystematicSet &filteredSystematics)
description: filter the systematics for the affected systematics returns: success guarantee: strong f...
Definition: SystematicSet.cxx:213
CP::SystematicVariation::parameter
float parameter() const
description: the numeric parameter contained in the subvariation(), or 0 if the subvariation can't be...
Definition: SystematicVariation.cxx:340
CP::hash_value
std::size_t hash_value(const SystematicSet &)
Hash function specifically for boost::hash.
Definition: SystematicSet.cxx:321
beamspotman.basename
basename
Definition: beamspotman.py:640
operator==
bool operator==(const CP::SystematicSet &a, const CP::SystematicSet &b)
Definition: SystematicSet.cxx:23