GamR  0.0.0
GammaROOT
Loading...
Searching...
No Matches
DetDefn.tt
Go to the documentation of this file.
1/* STD */
2#include <cmath>
3#include <cstdlib>
4#include <iostream>
5#include <numeric>
6#include <sstream>
7#include <stdexcept>
8
9namespace GamR {
10namespace Tree {
11
12template <class... Ts>
13struct TDetDefn<Ts...>::UpdateGoodness {
16 template <typename R, typename T>
17 void operator()(R raw, T &dat)
18 {
19 parent->fMeasCount += ((dat.second) > 0) && (*raw > 0);
20 }
21};
22
23template <class... Ts>
26 std::map<std::string, void *> PointerMap;
27 std::vector<std::string> Keys;
28 int idxKey;
29 InterpretPointers(TDetDefn *p, std::map<std::string, void *> &m, std::vector<std::string> &k)
30 : p(p), PointerMap(m), Keys(k), idxKey(0){};
31 template <typename T>
32 void operator()(T &dat)
33 {
34 dat = static_cast<T>(PointerMap[Keys[idxKey]]);
35 // dat.reset(static_cast<decltype(dat.get())>(PointerMap[Keys[idxKey]]));
36 ++idxKey;
37 }
38};
39
40template <class... Ts>
42 template <typename Raw, typename Datum>
43 void operator()(Raw r, Datum &d)
44 {
45 d.first = *r;
46 }
47};
48
49// template <class ...Ts>
50// struct TDetDefn<Ts...>::FillHistogram
51// {
52// TDetDefn* p;
53// size_t n; size_t max;
54// FillHistogram(TDetDefn* p) : p(p), n(0), max(50000) {};
55// template <typename T>
56// void operator()(T d)
57// {
58// if (p->fPollGate.Eval(d.first, d.second))
59// {
60// p -> fRawHists[n] -> Fill(d.first);
61// if (p->fRawHists[n]->GetEntries() >= max) {
62// if (p->fMean[n].first && p->fStdDev[n].first) {
63// p->fMean[n].second = p->fRawHists[n]->GetMean();
64// p->fStdDev[n].second = p->fRawHists[n]->GetStdDev();
65// } else {
66// p->fMean[n].first = p->fRawHists[n]->GetMean();
67// p->fStdDev[n].first = p->fRawHists[n]->GetStdDev();
68// }
69// p->fRawHists[n] -> Reset();
70// }
71// }
72// ++n;
73// }
74// };
75
76template <class... Ts>
77struct TDetDefn<Ts...>::CalFunctor {
81 Double_t dRand1;
82 Double_t dRand2;
83 Double_t dStdDev;
84 Double_t dMean;
85
86 struct CalFunctorInner;
88 template <typename T, typename R, typename N>
89 void operator()(T &datum, R &running, N n)
90 {
92 CalFunctorInner inner(p, this);
94 dRand1 = (int)(1000 * p->fRnd.Rndm()) / 1000.0;
95 dRand2 = (int)(1000 * p->fRnd.Gaus()) / 1000.0;
96
97 if (p->fPollGate[CalFuncN]->Eval(0)) {
98 p->fMean[CalFuncN].second +=
99 (static_cast<double>(datum.first) - static_cast<double>(running[(p->fRunningIndex)[CalFuncN]])) /
100 running.size();
101 p->fRunningSquared[CalFuncN] += (static_cast<double>(datum.first) * static_cast<double>(datum.first) -
102 (static_cast<double>(running[(p->fRunningIndex)[CalFuncN]])) *
103 (static_cast<double>(running[(p->fRunningIndex)[CalFuncN]]))) /
104 running.size();
105
106 p->fStdDev[CalFuncN].second =
107 std::sqrt(p->fRunningSquared[CalFuncN] - (p->fMean[CalFuncN].second) * (p->fMean[CalFuncN].second));
108
109 running[(p->fRunningIndex)[CalFuncN]] = static_cast<double>(datum.first);
110
111 (p->fRunningIndex)[CalFuncN] = ((p->fRunningIndex)[CalFuncN] + 1) % (p->fPollFreq);
112
113 if ((p->fRunningIndex)[CalFuncN] == 0 && p->fMean[CalFuncN].first == 0) {
114 // p->fMean[CalFuncN].second = static_cast<double>
115 // (std::accumulate((running).begin(), (running).end(), 0))/
116 // (running).size();
117
118 p->fMean[CalFuncN].first = p->fMean[CalFuncN].second;
119 p->fStdDev[CalFuncN].first = p->fStdDev[CalFuncN].second;
120 }
121 // if (p->fID == 1)
122 // std::cout << (p->fRunningIndex)[CalFuncN] << " " <<
123 // p->fMean[CalFuncN].first << " " << p->fMean[CalFuncN].second << "
124 // "<< p->fStdDev[CalFuncN].first << " " <<
125 // p->fStdDev[CalFuncN].second<< std::endl;
126 }
127
128 if ((p->fMean[CalFuncN].first) && (p->fMean[CalFuncN].second)) {
129 dMean = p->fMean[CalFuncN].first - p->fMean[CalFuncN].second;
130 } else {
131 dMean = 0;
132 }
133
134 if ((p->fStdDev[CalFuncN].first) && (p->fStdDev[CalFuncN].second)) {
135 dStdDev = p->fStdDev[CalFuncN].first / p->fStdDev[CalFuncN].second;
136 } else {
137 dStdDev = 1;
138 }
139
140 int CalFuncNLast = std::tuple_size<decltype(p->fDatum)>::value;
141 p->fCalFunc[CalFuncN]->SetParameter(CalFuncNLast, n); //event number as final "branch"
142 datum.second = p->fCalFunc[CalFuncN]->Eval(dRand1, dRand2, dStdDev, dMean);
143 p->fEffFunc.SetParameter(CalFuncN, datum.second);
144 ++CalFuncN;
145 }
146};
147
148template <class... Ts>
152 CalFunctorInner(TDetDefn *p, CalFunctor *outer) : p(p), o(outer){};
153 template <typename T>
154 void operator()(T par)
155 {
156 if (o->CalParN < o->CalFuncN) { // if possible to use calibrated datum
157 p->fCalFunc[o->CalFuncN]->SetParameter(
158 o->CalParN, p->fCalFunc[o->CalParN]->Eval(o->dRand1, o->dRand2, o->dStdDev, o->dMean));
159 p->fPollGate[o->CalFuncN]->SetParameter(o->CalParN, p->fCalFunc[o->CalParN]->Eval(o->dRand1, o->dRand2));
160
161 } else {
162 p->fCalFunc[o->CalFuncN]->SetParameter(o->CalParN, par.first);
163 p->fPollGate[o->CalFuncN]->SetParameter(o->CalParN, par.first);
164 }
165 ++(o->CalParN);
166 }
167};
168
169// ------------------------------------------------------------
170template <class... Ts>
172{
173 fMeasCount = 0;
174 GamR::Utils::for_both_in_tuple(fRawDatum, fDatum, UpdateGoodness(this));
175 return (fMeasCount >= fAtleast) ? kTRUE : kFALSE;
176}
177
178// ------------------------------------------------------------
179template <class... Ts>
180void TDetDefn<Ts...>::SetPtrs(std::map<std::string, void *> &branchmap, std::string branches)
181{
182 std::vector<std::string> branchvec;
183 std::stringstream ss(branches);
184 std::string branch;
185
186 while (ss >> branch) {
187 if (branchmap.find(branch) == branchmap.end()) {
188 throw std::runtime_error("Branch " + branch + " defined in " + fName + " not in raw tree");
189 }
190 branchvec.push_back(branch);
191 // auto hist = std::make_shared<TH1D>(branch.c_str(),
192 // branch.c_str(),
193 // 20000, 0, 10);
194 // hist -> SetCanExtend(TH1::kAllAxes);
195 // fRawHists.push_back(hist);
196 }
197
198 if (branchvec.size() != sizeof...(Ts)) {
199 throw std::length_error("Number branches found (" + std::to_string(branchvec.size()) + ") in: [" + branches +
200 "] does not equal detector declaration size" + "in detector ID " + std::to_string(fID));
201 }
202
203 std::cout << fType << "::" << fID << "::" << fName << " [" << branches << "]" << std::endl;
204
205 GamR::Utils::for_each_in_tuple(fRawDatum, InterpretPointers(this, branchmap, branchvec));
206}
207
208// ------------------------------------------------------------
209template <class... Ts>
210void TDetDefn<Ts...>::SetCalFuncs(std::string funcstrs)
211{
212 // Sets the expression of measurement calibration functions
213 std::stringstream ss(funcstrs);
214 std::string func;
215 std::vector<std::string> functions;
216 while (ss >> func) {
217 functions.push_back(func);
218 // if (ss.peek() == delim) { ss.ignore(); };
219 }
220
221 if (functions.size() != sizeof...(Ts)) {
222 throw std::length_error("Number calibration functions (" + std::to_string(functions.size()) +
223 ") does not equal detector declaration size.");
224 }
225
226 int num = 0;
227 for (const auto &itFuncStr : functions) {
228 ss.str("");
229 ss << fType << fID << "_" << fName << "_" << num;
230 fCalFunc.push_back(std::make_shared<TFormula>(ss.str().c_str(), itFuncStr.c_str()));
231 ++num;
232 }
233}
234
235// ------------------------------------------------------------
236template <class... Ts>
237void TDetDefn<Ts...>::SetPollGates(std::string pollstrs)
238{
239 // Sets the expression of the poll gate for means etc.
240 std::stringstream ss(pollstrs);
241 std::string func;
242 std::vector<std::string> functions;
243 while (ss >> func) {
244 functions.push_back(func);
245 // if (ss.peek() == delim) { ss.ignore(); };
246 }
247 if (functions.empty()) {
248 for (UInt_t i = 0; i < sizeof...(Ts); ++i) {
249 functions.push_back("0");
250 }
251 }
252 if (functions.size() != sizeof...(Ts)) {
253 throw std::length_error("Number poll gates (" + std::to_string(functions.size()) +
254 ") does not equal detector declaration size.");
255 }
256
257 int num = 0;
258 for (const auto &itFuncStr : functions) {
259 ss.str("");
260 ss << fType << fID << "_" << fName << "_" << num;
261 fPollGate.push_back(std::make_shared<TFormula>(ss.str().c_str(), itFuncStr.c_str()));
262 ++num;
263 }
264}
265
266// ------------------------------------------------------------
267template <class... Ts>
268void TDetDefn<Ts...>::SetEffFunc(std::string function)
269{
270 std::stringstream ss;
271 ss << fType << fID << "_" << fName << "_eff";
272 fEffFunc = TFormula(ss.str().c_str(), function.c_str());
273}
274
275// ------------------------------------------------------------
276template <class... Ts>
277void TDetDefn<Ts...>::Calibrate(ULong64_t eventNum)
278{
280 // GamR::Utils::for_each_in_tuple(fDatum, FillHistogram(this));
281 GamR::Utils::for_both_in_tuple(fDatum, fRunning, CalFunctor(this), eventNum);
282
283}
284
285} // namespace Tree
286} // namespace GamR
void ss(std::vector< int > indexes, TCanvas *canvas, Option_t *option)
TDetDefn(std::string t="", int id=-1, std::string n="", int I=50000)
Definition DetDefn.hh:56
void SetEffFunc(std::string function)
Definition DetDefn.tt:268
void Calibrate(ULong64_t eventNum=0)
Definition DetDefn.tt:277
void SetPtrs(std::map< std::string, void * > &branchmap, std::string branches)
Definition DetDefn.tt:180
void SetCalFuncs(std::string funcstrs)
Definition DetDefn.tt:210
void SetPollGates(std::string pollstrs)
Definition DetDefn.tt:237
void for_each_in_tuple(std::tuple< Ts... > &t, F f)
Applies function on each element of tuple.
Definition Tuples.hh:67
void for_both_in_tuple(T1 &t1, T2 &t2, F f)
Applies function taking both elements of tuple as arguments.
Definition Tuples.hh:83
Definition Gain.cc:19
CalFunctorInner(TDetDefn *p, CalFunctor *outer)
Definition DetDefn.tt:152
void operator()(T &datum, R &running, N n)
Definition DetDefn.tt:89
std::vector< std::string > Keys
Definition DetDefn.tt:27
InterpretPointers(TDetDefn *p, std::map< std::string, void * > &m, std::vector< std::string > &k)
Definition DetDefn.tt:29
std::map< std::string, void * > PointerMap
Definition DetDefn.tt:26
void operator()(R raw, T &dat)
Definition DetDefn.tt:17