NAGASH 0.9.8
Next Generation Analysis System
Loading...
Searching...
No Matches
SystTool.h
Go to the documentation of this file.
1//***************************************************************************************
4//***************************************************************************************
5
6#pragma once
7
8#include "NAGASH/Global.h"
9#include "NAGASH/Tool.h"
10
11namespace NAGASH
12{
13 class SystTool : public Tool
14 {
15 public:
16 SystTool(std::shared_ptr<MSGTool> MSG) : Tool(MSG) {}
17 template <typename T>
18 void BookVariation(const TString &name, const TString &systname, T *var);
19 template <typename T>
20 T Get(const TString &name);
21 void SetVariationCorrelation(const TString &name, double correlation); // if not set, correlation is assumed to be zero
22 double GetVariationCorrelation(const TString &name);
23 void SetToVariation(const TString &name);
24 const TString &CurrentVariation();
25 const std::vector<TString> &GetListOfVariations();
26 std::vector<TString> GetListOfVariations(const TString &name);
27 int GetNVariations();
28 int GetNVariations(const TString &name);
29
30 private:
32 {
33 std::map<TString, std::any> VarMap;
34 std::any CurrentVar;
35 std::any NominalVar;
36 };
37
38 struct AuxVar
39 {
40 std::any *Current;
41 std::any *Variation;
42 };
43
44 bool isSetVar = false;
45 bool isSystListSorted = false;
46 TString currentvar;
47 std::map<TString, VarWithSyst> VarMapFull;
48 std::map<TString, std::vector<AuxVar>> SystMapFull;
49 std::map<TString, double> CorrMap;
50 std::map<TString, std::vector<TString>> SystListMap;
51 std::vector<TString> SystList;
52 };
53
59 template <typename T>
60 void SystTool::BookVariation(const TString &name, const TString &systname, T *var)
61 {
62 auto findresult = VarMapFull.find(name);
63 if (findresult != VarMapFull.end())
64 {
65 if (findresult->second.VarMap.find(systname) == findresult->second.VarMap.end())
66 {
67 SystListMap.find(name)->second.emplace_back(systname);
68 findresult->second.VarMap.emplace(std::pair<TString, std::any>(systname, std::any(var)));
69 isSetVar = false;
70 }
71 else
72 {
73 MSGUser()->StartTitle("SystTool::BookVariation");
74 MSGUser()->MSG(MSGLevel::WARNING, "Variable ", name, " already has variation ", systname, ", this call will be ignored");
75 MSGUser()->EndTitle();
76 return;
77 }
78 }
79 else
80 {
81 VarWithSyst tempVarWithSyst;
82 tempVarWithSyst.VarMap.emplace(std::pair<TString, std::any>(systname, std::any(var)));
83 tempVarWithSyst.CurrentVar = std::any(var);
84 VarMapFull.emplace(std::pair<TString, VarWithSyst>(name, tempVarWithSyst));
85 SystListMap.emplace(std::pair<TString, std::vector<TString>>(name, std::vector<TString>{systname}));
86 isSetVar = false;
87 }
88
89 if (systname == "Nominal")
90 {
91 VarMapFull.find(name)->second.NominalVar = std::any(var);
92 auto listfindresult = SystListMap.find(name);
93 std::swap(listfindresult->second[0], listfindresult->second.back());
94 }
95
96 if (std::find(std::begin(SystList), std::end(SystList), systname) == std::end(SystList))
97 {
98 SystList.emplace_back(systname);
99 CorrMap.insert(std::pair<TString, double>(systname, 0));
100 }
101 }
102
107 template <typename T>
108 T SystTool::Get(const TString &name)
109 {
110 auto findresult = VarMapFull.find(name);
111 if (findresult != VarMapFull.end())
112 {
113 try
114 {
115 auto par = *(std::any_cast<T *>(findresult->second.CurrentVar));
116 return par;
117 }
118 catch (const std::bad_any_cast &e)
119 {
120 MSGUser()->StartTitle("SystTool::Get");
121 MSGUser()->MSG(MSGLevel::ERROR, "Given type of variable ", name, " is wrong, return NULL value");
122 MSGUser()->EndTitle();
123 return T();
124 }
125 }
126 else
127 {
128 MSGUser()->StartTitle("SystTool::Get");
129 MSGUser()->MSG(MSGLevel::ERROR, "Variable ", name, " does not exist, return NULL value");
130 MSGUser()->EndTitle();
131 return T();
132 }
133 }
134
135} // namespace NAGASH
Some global definitions.
Manage systematic variations.
Definition SystTool.h:14
std::map< TString, std::vector< TString > > SystListMap
Definition SystTool.h:50
void BookVariation(const TString &name, const TString &systname, T *var)
Book a systematic variation of a parameter.
Definition SystTool.h:60
double GetVariationCorrelation(const TString &name)
Get the correlation factor of the given systematic variation.
Definition SystTool.cxx:119
void SetVariationCorrelation(const TString &name, double correlation)
Set the correlation factor of the given systematic variation.
Definition SystTool.cxx:102
const std::vector< TString > & GetListOfVariations()
get the list of all systematic variations.
Definition SystTool.cxx:136
TString currentvar
Definition SystTool.h:46
std::vector< TString > SystList
Definition SystTool.h:51
int GetNVariations()
get the number of systematic variations.
Definition SystTool.cxx:237
std::map< TString, std::vector< AuxVar > > SystMapFull
Definition SystTool.h:48
bool isSystListSorted
Definition SystTool.h:45
const TString & CurrentVariation()
Get the name of the current systematic variation.
Definition SystTool.cxx:85
SystTool(std::shared_ptr< MSGTool > MSG)
Definition SystTool.h:16
std::map< TString, double > CorrMap
Definition SystTool.h:49
void SetToVariation(const TString &name)
set the current variation to the given one.
Definition SystTool.cxx:178
std::map< TString, VarWithSyst > VarMapFull
Definition SystTool.h:47
T Get(const TString &name)
Get the value of the parameter with the current systematic variation.
Definition SystTool.h:108
Provide interface for all tools in NAGASH.
Definition Tool.h:72
std::shared_ptr< MSGTool > MSGUser()
return the MSGTool inside.
Definition Tool.h:91
std::map< TString, std::any > VarMap
Definition SystTool.h:33