ATLAS Offline Software
SystematicsSvc.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 
7 
8 //
9 // includes
10 //
11 
13 
17 #include <cassert>
18 #include <cmath>
19 #include <regex>
20 #include <functional>
21 
22 //
23 // method implementations
24 //
25 
26 namespace CP
27 {
29  SystematicsSvc (const std::string& name,
30  ISvcLocator* pSvcLocator)
31  : AsgService (name, pSvcLocator)
32  {
33  declareServiceInterface<ISystematicsSvc>();
34  }
35 
36 
37 
39  initialize ()
40  {
41  if (!std::isfinite (m_sigmaRecommended) ||
43  {
44  ANA_MSG_ERROR ("invalid value for sigmaRecommended: " << m_sigmaRecommended);
45  return StatusCode::FAILURE;
46  }
47 
48  if (m_sigmaRecommended != 0)
49  {
50  if (!m_systematicsList.empty())
51  {
52  ANA_MSG_ERROR ("can't specify both sigmaRecommended and systematicsList");
53  return StatusCode::FAILURE;
54  }
55  }
56  return StatusCode::SUCCESS;
57  }
58 
59 
60 
61  std::vector<CP::SystematicSet> SystematicsSvc ::
63  {
64  std::lock_guard<std::mutex> lock (m_systematicsMutex);
65  std::vector<CP::SystematicSet> systematicsVector;
66 
67  if (m_sigmaRecommended != 0)
68  {
69  assert (m_sigmaRecommended > 0);
70 
72  sys.setSigma (m_sigmaRecommended);
73  sys.calc (m_recommendedSystematics);
74 
75  std::regex expr (m_systematicsRegex.value());
76  for (const CP::SystematicSet& mysys : sys.result(""))
77  {
78  if (!regex_match (mysys.name(), expr))
79  {
80  ANA_MSG_DEBUG ("skipping systematic: " << mysys.name());
81  } else {
82  ANA_MSG_DEBUG ("configuring systematic: " << mysys.name());
83  systematicsVector.push_back (mysys);
84  }
85  }
86  } else if (m_systematicsList.empty())
87  {
88  // take an empty property as running just the central
89  // systematics set. implication is that you can't really run
90  // doing nothing, but that ought to be Ok.
91  systematicsVector.push_back (CP::SystematicSet ());
92  } else
93  {
94  for (const std::string& sysName : m_systematicsList)
95  systematicsVector.push_back (CP::SystematicSet (sysName));
96  }
97 
98  assert (!systematicsVector.empty());
99  return systematicsVector;
100  }
101 
102 
103 
105  addSystematics (const CP::SystematicSet& recommended,
106  const CP::SystematicSet& affecting) const
107  {
108  std::lock_guard<std::mutex> lock (m_systematicsMutex);
109 
110  for (const CP::SystematicVariation& mysys : recommended)
111  {
112  if (affecting.find (mysys) == affecting.end())
113  {
114  ANA_MSG_ERROR ("systematic is only registered as recommended, not affecting: " << mysys);
115  return StatusCode::FAILURE;
116  }
117  }
118 
119  m_affectingSystematics.insert (affecting);
120  m_recommendedSystematics.insert (recommended);
121  return StatusCode::SUCCESS;
122  }
123 
124 
125 
127  getObjectSystematics (const std::string& name) const
128  {
129  std::lock_guard<std::mutex> lock (m_systematicsMutex);
130  auto iter = m_objectSystematics.find (name);
131  if (iter != m_objectSystematics.end())
132  return iter->second;
133  else
134  return CP::SystematicSet ();
135  }
136 
137 
138 
140  setObjectSystematics (const std::string& name,
141  const CP::SystematicSet& systematics) const
142  {
143  auto mysystematics = systematics;
144  std::lock_guard<std::mutex> lock (m_systematicsMutex);
145  for (const CP::SystematicVariation& mysys : systematics)
146  {
147  if (m_affectingSystematics.find (mysys) == m_affectingSystematics.end())
148  {
149  ANA_MSG_ERROR ("systematic is set as object systematic, but not affecting: " << mysys);
150  return StatusCode::FAILURE;
151  }
152  }
153  m_objectSystematics[name] = std::move (mysystematics);
154  return StatusCode::SUCCESS;
155  }
156 
157 
158 
160  getDecorSystematics (const std::string& objectName,
161  const std::string& decorName) const
162  {
163  const std::string name = objectName + "." + decorName;
164  std::unique_lock<std::mutex> lock (m_systematicsMutex);
165  auto iter = m_decorSystematics.find (name);
166  if (iter != m_decorSystematics.end())
167  return iter->second;
168  auto jter = m_copies.find (objectName);
169  if (jter != m_copies.end())
170  {
171  const std::string& copyName = jter->second;
172  lock.unlock();
173  return getDecorSystematics (copyName, decorName);
174  }
175  return CP::SystematicSet ();
176  }
177 
178 
179 
181  setDecorSystematics (const std::string& objectName,
182  const std::string& decorName,
183  const CP::SystematicSet& systematics) const
184  {
185  const std::string name = objectName + "." + decorName;
186  auto mysystematics = systematics;
187  std::lock_guard<std::mutex> lock (m_systematicsMutex);
188  for (const CP::SystematicVariation& mysys : systematics)
189  {
190  if (m_affectingSystematics.find (mysys) == m_affectingSystematics.end())
191  {
192  ANA_MSG_ERROR ("systematic is set as object systematic, but not affecting: " << mysys);
193  return StatusCode::FAILURE;
194  }
195  }
196  m_decorSystematics[name] = std::move (mysystematics);
197  return StatusCode::SUCCESS;
198  }
199 
200 
201 
203  registerCopy (const std::string& fromName,
204  const std::string& toName) const
205  {
206  std::lock_guard<std::mutex> lock (m_systematicsMutex);
207  auto emplace_result = m_copies.try_emplace (toName, fromName);
208  if (emplace_result.second == false)
209  {
210  ANA_MSG_ERROR ("duplicate copy registered for name " << toName);
211  return StatusCode::FAILURE;
212  }
213  return StatusCode::SUCCESS;
214  }
215 
216 
217 
218  std::string SystematicsSvc ::
219  getCopySource (const std::string& toName) const
220  {
221  std::lock_guard<std::mutex> lock (m_systematicsMutex);
222  auto iter = m_copies.find (toName);
223  if (iter == m_copies.end())
224  return "";
225  else
226  return iter->second;
227  }
228 
229 
230 
232  makeSystematicsName (std::string& result,
233  const std::string& name,
234  const CP::SystematicSet& sys) const
235  {
236  if (name.empty())
237  {
238  ANA_MSG_ERROR ("not allowed to make systematic name for empty string");
239  return StatusCode::FAILURE;
240  }
241 
242  result = name;
243 
244  const auto sysSplit = result.find ("%SYS%");
245  if (sysSplit == std::string::npos)
246  {
247  if (!sys.empty())
248  {
249  ANA_MSG_ERROR ("can't set systematics on name without \"%SYS%\": \"" << name << "\" sys=" << sys.name());
250  return StatusCode::FAILURE;
251  }
252  } else
253  {
254  std::string sysName = sys.name();
255  if (sysName.empty())
257  result.replace (sysSplit, 5, sysName);
258  }
259 
260  return StatusCode::SUCCESS;
261  }
262 
263 
264 
266  finalize ()
267  {
268  if (m_systematicsList.empty()) {
269  ANA_MSG_INFO ("no systematics were run.");
270  }
271  else{
272  for (const CP::SystematicVariation& mysys : m_affectingSystematics)
273  {
274  // this logic checks whether a systematic is recommended and
275  // affecting, or only affecting. if it is only the later, it
276  // reports the systematic in parenthesis to set it apart.
277  if (m_recommendedSystematics.find (mysys) == m_recommendedSystematics.end())
278  ANA_MSG_INFO ("found systematic: (" << mysys << ")");
279  else
280  ANA_MSG_INFO ("found systematic: " << mysys);
281  }
282 
283  if(m_systematicsRegex!=".*") {
284  ANA_MSG_INFO("Systematics regex '" << m_systematicsRegex << "' matched:");
285  for(const CP::SystematicSet& mysys : makeSystematicsVector()) {
286  ANA_MSG_INFO (" '" << mysys.name() << "'");
287  }
288  }
289  }
290  return StatusCode::SUCCESS;
291  }
292 }
CP::SystematicsSvc::makeSystematicsName
virtual StatusCode makeSystematicsName(std::string &result, const std::string &name, const CP::SystematicSet &sys) const override
make the name for the given systematics
Definition: SystematicsSvc.cxx:232
CaloCondBlobAlgs_fillNoiseFromASCII.sysName
sysName
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:93
CP::SystematicsSvc::finalize
virtual StatusCode finalize() override
Definition: SystematicsSvc.cxx:266
CP::MakeSystematicsVector
This class handles turning the list of systematics into the actual list of nuisance parameter points ...
Definition: MakeSystematicsVector.h:34
get_generator_info.result
result
Definition: get_generator_info.py:21
CP::SystematicsSvc::getDecorSystematics
virtual CP::SystematicSet getDecorSystematics(const std::string &objectName, const std::string &decorName) const override
get the systematics for the given object in the event store
Definition: SystematicsSvc.cxx:160
CP::SystematicsSvc::m_systematicsMutex
std::mutex m_systematicsMutex
a mutex for accessing the above mutable members
Definition: SystematicsSvc.h:124
CP::SystematicSet
Class to wrap a set of SystematicVariations.
Definition: SystematicSet.h:31
CP::SystematicsSvc::SystematicsSvc
SystematicsSvc(const std::string &name, ISvcLocator *pSvcLocator)
standard constructor
Definition: SystematicsSvc.cxx:29
ANA_MSG_ERROR
#define ANA_MSG_ERROR(xmsg)
Macro printing error messages.
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:294
CP::SystematicsSvc::setObjectSystematics
virtual StatusCode setObjectSystematics(const std::string &name, const CP::SystematicSet &systematics) const override
set the systematics for the given object in the event store
Definition: SystematicsSvc.cxx:140
CP::SystematicVariation
Definition: SystematicVariation.h:47
CP::SystematicsSvc::m_systematicsList
Gaudi::Property< std::vector< std::string > > m_systematicsList
the names of the systematics to request
Definition: SystematicsSvc.h:82
CP
Select isolated Photons, Electrons and Muons.
Definition: Control/xAODRootAccess/xAODRootAccess/TEvent.h:48
StringUtil.h
CP::SystematicsSvc::initialize
virtual StatusCode initialize() override
set up/tear down functions
Definition: SystematicsSvc.cxx:39
mapkey::sys
@ sys
Definition: TElectronEfficiencyCorrectionTool.cxx:42
PrepareReferenceFile.regex
regex
Definition: PrepareReferenceFile.py:43
CP::SystematicsSvc::addSystematics
virtual StatusCode addSystematics(const CP::SystematicSet &recommended, const CP::SystematicSet &affecting) const override
register a set of recommended and affecting systematics
Definition: SystematicsSvc.cxx:105
CP::SystematicsSvc::getObjectSystematics
virtual CP::SystematicSet getObjectSystematics(const std::string &name) const override
get the systematics for the given object in the event store
Definition: SystematicsSvc.cxx:127
CP::SystematicsSvc::registerCopy
virtual StatusCode registerCopy(const std::string &fromName, const std::string &toName) const override
register a (shallow) copy from one object to the next
Definition: SystematicsSvc.cxx:203
CP::SystematicsSvc::m_sigmaRecommended
Gaudi::Property< float > m_sigmaRecommended
load all recommended systematics at the given number of sigmas
Definition: SystematicsSvc.h:95
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ANA_MSG_INFO
#define ANA_MSG_INFO(xmsg)
Macro printing info messages.
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:290
MessageCheck.h
macros for messaging and checking status codes
CP::SystematicSet::end
const_iterator end() const
description: const iterator to the end of the set
Definition: SystematicSet.h:59
CP::SystematicsSvc::makeSystematicsVector
virtual std::vector< CP::SystematicSet > makeSystematicsVector() const override
get the list of systematics
Definition: SystematicsSvc.cxx:62
CP::SystematicsSvc::getCopySource
virtual std::string getCopySource(const std::string &toName) const override
get a source object name from which a target object has been copied
Definition: SystematicsSvc.cxx:219
MakeSystematicsVector.h
CheckAppliedSFs.systematics
def systematics
Definition: CheckAppliedSFs.py:231
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
CP::SystematicSet::find
iterator find(const SystematicVariation &sys) const
description: find an element in the set
Definition: SystematicSet.h:63
CP::SystematicsSvc::m_nominalSystematicsName
Gaudi::Property< std::string > m_nominalSystematicsName
nominal systematics name
Definition: SystematicsSvc.h:99
CP::SystematicsSvc::setDecorSystematics
virtual StatusCode setDecorSystematics(const std::string &objectName, const std::string &decorName, const CP::SystematicSet &systematics) const override
set the systematics for the given object in the event store
Definition: SystematicsSvc.cxx:181
SystematicsSvc.h
CP::SystematicsSvc::m_systematicsRegex
Gaudi::Property< std::string > m_systematicsRegex
the regular expression for filterinf systematics
Definition: SystematicsSvc.h:86
ANA_MSG_DEBUG
#define ANA_MSG_DEBUG(xmsg)
Macro printing debug messages.
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:288