ATLAS Offline Software
Loading...
Searching...
No Matches
SystematicVariation.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3*/
4
6
7
8//
9// includes
10//
11
13
14#include <cmath>
15#include <regex>
16#include <sstream>
17#include <stdexcept>
19
21{
22 return a.name() < b.name();
23}
24
25
27{
28 return a.name() == b.name();
29}
30
31//
32// method implementations
33//
34
35namespace CP
36{
37 namespace
38 {
45 std::string packUnsigned (unsigned value)
46 {
47 std::ostringstream result;
48 result << value;
49 return result.str();
50 }
51
52
60 unsigned unpackUnsigned (const std::string& value)
61 {
62 std::istringstream str (value);
63 unsigned result = 0;
64 if (!(str >> result))
65 throw std::runtime_error ("failed to parse \"" + value + "\" into an unsigned");
66 char c;
67 if (str >> c)
68 throw std::runtime_error ("failed to parse \"" + value + "\" into an unsigned");
69 return result;
70 }
71
72
73
80 std::string packFloat (float value, const std::string& plus,
81 const std::string& minus)
82 {
83 std::string separator = plus;
84 if (value < 0)
85 {
86 separator = minus;
87 value *= -1;
88 }
89 RCU_ASSERT (!separator.empty());
90
91 std::ostringstream str;
92 str << rint (value * 10000);
93 std::string number = str.str();
94
95 while (number.size() < 5)
96 number = "0" + number;
97 return number.substr (0, number.size() - 4) + separator + number.substr (number.size() - 4);
98 }
99
100
101
110 bool unpackFloatHelper (const std::string& value,
111 const std::string& separator,
112 float& result)
113 {
114 if (!separator.empty())
115 {
116 std::string::size_type split = value.rfind (separator);
117 if (split != std::string::npos)
118 {
119 std::istringstream str (value.substr (0, split) + "." + value.substr (split + separator.size()));
120 if (!(str >> result))
121 throw std::runtime_error ("failed to parse into a float: " + value);
122 char c;
123 if (str >> c)
124 throw std::runtime_error ("failed to parse into a float: " + value);
125 return true;
126 }
127 }
128 return false;
129 }
130
131
132
140 float unpackFloat (const std::string& value, const std::string& plus,
141 const std::string& minus)
142 {
143 float result = 0;
144 if (unpackFloatHelper (value, plus, result))
145 return result;
146 if (unpackFloatHelper (value, minus, result))
147 {
148 result *= -1;
149 return result;
150 }
151 throw std::runtime_error ("failed to parse into a float: " + value);
152 }
153 }
154
155
156
157 void SystematicVariation ::
158 testInvariant () const
159 {}
160
161
162
163 SystematicVariation ::
164 SystematicVariation ()
165 {
166 RCU_NEW_INVARIANT (this);
167 }
168
169
170
171 SystematicVariation ::
172 SystematicVariation (const std::string& val_name)
173 : m_name (val_name)
174 {
175 RCU_NEW_INVARIANT (this);
176 }
177
178
179
180 SystematicVariation ::
181 SystematicVariation (const std::string& val_basename,
182 const std::string& val_subvariation)
183 : m_name (val_basename + "__" + val_subvariation)
184 {
185 RCU_NEW_INVARIANT (this);
186 }
187
188
189
190 SystematicVariation ::
191 SystematicVariation (const std::string& val_basename,
192 float val_parameter)
193 {
194 std::string separator = "up";
195 if (val_parameter < 0)
196 {
197 separator = "down";
198 val_parameter *= -1;
199 }
200 std::ostringstream str;
201 str << rint (val_parameter * 10000);
202 std::string number = str.str();
203
204 while (number.size() < 5)
205 number = "0" + number;
206
210 if (number != "00000")
211 {
212 m_name = val_basename + "__" + number.substr (0, number.size()-4)
213 + separator + number.substr (number.size()-4);
214 while (m_name[m_name.size()-1] == '0')
215 m_name.resize (m_name.size()-1);
216 }
217
218 RCU_NEW_INVARIANT (this);
219 }
220
221
222
223 #ifndef __CINT__
224 SystematicVariation ::
225 SystematicVariation (const std::string& val_basename, CONTINUOUS_ARG)
226 : m_name (val_basename + "__continuous")
227 {
228 RCU_NEW_INVARIANT (this);
229 }
230 #endif
231
232
233
234 SystematicVariation SystematicVariation ::
235 makeToyVariation (const std::string& basename,
236 unsigned toyIndex, float toyScale)
237 {
238 RCU_REQUIRE (toyIndex > 0);
239 RCU_REQUIRE (toyScale > 0);
241 (basename, "toy_" + packUnsigned (toyIndex) + "_" + packFloat (toyScale, "scale", ""));
242 }
243
244
245
246 SystematicVariation SystematicVariation ::
247 makeContinuousEnsemble (const std::string& basename)
248 {
250 }
251
252
253
254 SystematicVariation SystematicVariation ::
255 makeToyEnsemble (const std::string& basename)
256 {
257 return SystematicVariation (basename, "toy_ensemble");
258 }
259
260
261
262 SystematicVariation SystematicVariation ::
263 makeContinuous (const std::string& basename)
264 {
265 return SystematicVariation (basename + "__continuous");
266 }
267
268
269
270 SystematicVariation ::
271 SystematicVariation (const std::set<SystematicVariation>& systematics,
272 const std::string& val_basename)
273 {
274 RCU_NEW_INVARIANT (this);
275
276 for (std::set<SystematicVariation>::const_iterator
277 sys = systematics.begin(),
278 end = systematics.end(); sys != end; ++ sys)
279 {
280 if (sys->basename() == val_basename)
281 {
282 *this = *sys;
283 return;
284 }
285 }
286 }
287
288
289
290 bool SystematicVariation ::
291 empty () const
292 {
293 RCU_READ_INVARIANT (this);
294 return m_name.empty();
295 }
296
297
298
299 const std::string& SystematicVariation ::
300 name () const
301 {
302 RCU_READ_INVARIANT (this);
303 return m_name;
304 }
305
306
307
308 std::string SystematicVariation ::
309 basename () const
310 {
311 RCU_READ_INVARIANT (this);
312
313 std::string::size_type split = m_name.rfind ("__");
314 if (split != std::string::npos)
315 return m_name.substr (0, split);
316 else
317 return m_name;
318 }
319
320
321
322 std::string SystematicVariation ::
323 subvariation () const
324 {
325 RCU_READ_INVARIANT (this);
326
327 std::string::size_type split = m_name.rfind ("__");
328 if (split != std::string::npos)
329 return m_name.substr (split + 2);
330 else
331 return "";
332 }
333
334
335
336 float SystematicVariation ::
337 parameter () const
338 {
339 RCU_READ_INVARIANT (this);
340
341 std::string subvariation = this->subvariation();
342
343 std::string::size_type split = subvariation.rfind ("up");
344 if (split != std::string::npos)
345 {
346 std::istringstream str (subvariation.substr (0, split) + "." + subvariation.substr (split + 2));
347 float result;
348 if (str >> result)
349 return result;
350 else
351 return 0;
352 }
353
354 split = subvariation.rfind ("down");
355 if (split != std::string::npos)
356 {
357 std::istringstream str (subvariation.substr (0, split) + "." + subvariation.substr (split + 4));
358 float result;
359 if (str >> result)
360 return -result;
361 else
362 return 0;
363 }
364
365 return 0;
366 }
367
368
369
370 std::pair<unsigned,float> SystematicVariation ::
371 getToyVariation () const
372 {
373 // no invariant used
374
375 std::string variation = subvariation();
376
377 const std::size_t prefixSize = 4;
378 const auto split1 = variation.find ("toy_");
379 if (split1 != 0)
380 throw std::runtime_error ("not a toy variation: " + name());
381 const auto split2 = variation.find ("_", split1 + prefixSize);
382 if (split2 == std::string::npos)
383 throw std::runtime_error ("not a toy variation: " + name());
384
385 const auto sub1 = variation.substr (prefixSize, split2 - prefixSize);
386 const auto sub2 = variation.substr (split2 + 1);
387 return std::make_pair (unpackUnsigned (sub1), unpackFloat (sub2, "scale", ""));
388 }
389
390
391
392 bool SystematicVariation ::
393 isToyEnsemble () const
394 {
395 // no invariant used
396 return subvariation() == "toy_ensemble";
397 }
398
399
400
401 bool SystematicVariation ::
402 isContinuousEnsemble () const
403 {
404 // no invariant used
405 return subvariation() == "continuous";
406 }
407
408
409
410 bool SystematicVariation ::
411 isEnsemble () const
412 {
413 // no invariant used
415 }
416
417
418
419 bool SystematicVariation ::
420 ensembleContains (const SystematicVariation& sys) const
421 {
422 if (basename() != sys.basename())
423 return false;
424 if (isContinuousEnsemble() && sys.isContinuousVariation())
425 return true;
426 if (isToyEnsemble() && sys.isToyVariation())
427 return true;
428 return false;
429 }
430
431
432
433 bool SystematicVariation ::
434 isToyVariation () const
435 {
436 static const std::regex pattern (".*__toy_[0-9]+_[0-9]+((scale)[0-9]{1,4})?");
437 return regex_match (m_name, pattern);
438 }
439
440
441
442 bool SystematicVariation ::
443 isContinuousVariation () const
444 {
445 static const std::regex pattern (".*__[0-9]+((up)|(down))([0-9]{1,4})?");
446 return regex_match (m_name, pattern);
447 }
448
449
450
451 std::ostream& operator << (std::ostream& str, const CP::SystematicVariation& obj)
452 {
453 return str << obj.name();
454 }
455}
#define RCU_ASSERT(x)
Definition Assert.h:210
#define RCU_NEW_INVARIANT(x)
Definition Assert.h:221
#define RCU_REQUIRE(x)
Definition Assert.h:196
#define RCU_READ_INVARIANT(x)
Definition Assert.h:217
static Double_t a
static const std::vector< std::string > systematics
bool operator==(const CP::SystematicVariation &a, const CP::SystematicVariation &b)
bool operator<(const CP::SystematicVariation &a, const CP::SystematicVariation &b)
SystematicVariation()
effects: standard default constructor guarantee: no-fail
CONTINUOUS_ARG
effects: constructor for indicating continuous systematics guarantee: strong failures: out of memory ...
std::string subvariation() const
description: the part of the name that indicates by how many sigmas we varied guarantee: strong failu...
std::string basename() const
description: the base name, i.e.
std::string m_name
description: members directly corresponding to accessors
bool isToyEnsemble() const
whether this represents a toy ensemble
bool isContinuousEnsemble() const
whether this represents a continuous ensemble
std::vector< std::string > split(const std::string &s, const std::string &t=":")
Definition hcg.cxx:179
Select isolated Photons, Electrons and Muons.
std::ostream & operator<<(std::ostream &out, const std::pair< FIRST, SECOND > &pair)
Helper print operator.
std::string str(const TrigT2MbtsBits_v1 &trigT2MbtsBits)
std::string basename(std::string name)
Definition utils.cxx:207
std::string number(const double &d, const std::string &s)
Definition utils.cxx:186