forked from cafana/SRProxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasicTypesProxy.h
476 lines (381 loc) · 12.4 KB
/
BasicTypesProxy.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
#pragma once
#include <array>
#include <cassert>
#include <cmath> // for std::isinf and std::isnan
#include <set>
#include <string>
#include <vector>
class TFormLeafInfo;
class TBranch;
class TLeaf;
class TTreeFormula;
class TTree;
namespace caf
{
/// this constant is passed by reference into the various Proxy constructors.
inline const long kDummyBase = 0;
class SRBranchRegistry
{
public:
static void AddBranch(const std::string& b){fgBranches.insert(b);}
static const std::set<std::string>& GetBranches(){return fgBranches;}
static void clear() {fgBranches.clear();}
static void Print(bool abbrev = true);
static void ToFile(const std::string& fname);
protected:
static std::set<std::string> fgBranches;
};
enum CAFType
{
kNested,
kFlat,
kCopiedRecord // Assigned into, not associated with a file
};
CAFType GetCAFType(TTree* tr);
/// Count the subscripts in the name
int NSubscripts(const std::string& name);
template<class T> struct is_vec {static const bool value = false;};
template<class T> struct is_vec<std::vector<T>>{static const bool value = true; };
template<class T> class Proxy;
class Restorer;
template<class T> class Proxy
{
public:
static_assert(std::is_arithmetic_v<T> || std::is_enum_v<T> || std::is_same_v<T, std::string>, "Invalid type for basic type Proxy");
friend class Restorer;
Proxy(TTree* tr, const std::string& name, const long& base, int offset);
Proxy(TTree* tr, const std::string& name) : Proxy(tr, name, kDummyBase, 0) {}
// Need to be copyable because Vars return us directly
Proxy(const Proxy&);
Proxy(const Proxy&&);
// No need to be assignable though
Proxy& operator=(const Proxy&) = delete;
// Somehow including this helps us not get automatically converted to a
// type we might not want to be in ternary expressions (we now get a type
// error instead).
Proxy(T v) = delete;
~Proxy();
operator T() const {return GetValueChecked();}
T GetValue() const;
// In practice these are the only operations that systematic shifts use
Proxy<T>& operator=(T x);
Proxy<T>& operator+=(T x);
Proxy<T>& operator-=(T x);
Proxy<T>& operator*=(T x);
std::string Name() const {return fName;}
void CheckEquals(const T& x) const;
protected:
// Print a warning on inf or NaN
T GetValueChecked() const;
T GetValueNested() const;
T GetValueFlat() const;
void SetShifted();
// The type to fetch from the TLeaf - get template errors inside of ROOT
// for enums.
typedef typename std::conditional_t<std::is_enum_v<T>, int, T> U;
// Shared
std::string fName;
CAFType fType;
mutable TLeaf* fLeaf;
mutable U fVal;
TTree* fTree;
// Flat
const long& fBase;
int fOffset;
// Nested
mutable TFormLeafInfo* fLeafInfo;
mutable TBranch* fBranch;
mutable TTreeFormula* fTTF;
mutable long fEntry;
mutable int fSubIdx;
};
// Helper functions that don't need to be templated
class ArrayVectorProxyBase
{
public:
std::string Name() const {return fName;}
protected:
ArrayVectorProxyBase(TTree* tr,
const std::string& name,
bool isNestedContainer,
const long& base, int offset);
virtual ~ArrayVectorProxyBase();
void EnsureIdxP() const;
void CheckIndex(size_t i, size_t size) const;
std::string IndexField() const;
/// add [i], or something more complex for nested CAFs
std::string Subscript(int i) const;
std::string SubName() const;
// Trivial, but requires including TTree.h, which we don't want in header
bool TreeHasLeaf(TTree* tr, const std::string& name) const;
TTree* fTree;
std::string fName;
bool fIsNestedContainer;
CAFType fType;
const long& fBase;
int fOffset;
mutable Proxy<long long>* fIdxP;
mutable long fIdx;
};
// Helper functions that don't need to be templated
class VectorProxyBase: public ArrayVectorProxyBase
{
public:
virtual ~VectorProxyBase();
size_t size() const;
bool empty() const;
void resize(size_t i);
protected:
VectorProxyBase(TTree* tr, const std::string& name, bool isNestedContainer, const long& base, int offset);
std::string LengthField() const;
/// Helper for LengthField()
std::string NName() const;
void EnsureSizeExists() const;
mutable Proxy<int>* fSize; ///< only initialized on-demand
};
template<class T> class Proxy<std::vector<T>>: public VectorProxyBase
{
public:
Proxy(TTree* tr, const std::string& name, const long& base, int offset)
: VectorProxyBase(tr, name, is_vec<T>::value || std::is_array_v<T>, base, offset)
{
}
Proxy(TTree* tr, const std::string& name) : Proxy(tr, name, kDummyBase, 0) {}
~Proxy(){for(Proxy<T>* e: fElems) delete e;}
Proxy& operator=(const Proxy<std::vector<T>>&) = delete;
Proxy(const Proxy<std::vector<T>>& v) = delete;
Proxy<T>& at(size_t i) const {EnsureLongEnough(i); return *fElems[i];}
Proxy<T>& at(size_t i) {EnsureLongEnough(i); return *fElems[i];}
Proxy<T>& operator[](size_t i) const {return at(i);}
Proxy<T>& operator[](size_t i) {return at(i);}
template<class U> Proxy<std::vector<T>>& operator=(const std::vector<U>& x)
{
resize(x.size());
for(unsigned int i = 0; i < x.size(); ++i) at(i) = x[i];
return *this;
}
template<class U>
void CheckEquals(const std::vector<U>& x) const
{
EnsureSizeExists();
fSize->CheckEquals(x.size());
for(unsigned int i = 0; i < std::min(size(), x.size()); ++i) at(i).CheckEquals(x[i]);
}
// U should be either T or const T
template<class U> class iterator
{
public:
Proxy<T>& operator*() {return (*fParent)[fIdx];}
iterator<U>& operator++(){++fIdx; return *this;}
bool operator!=(const iterator<U>& it) const {return fIdx != it.fIdx;}
bool operator==(const iterator<U>& it) const {return fIdx == it.fIdx;}
protected:
friend class Proxy<std::vector<T>>;
iterator(const Proxy<std::vector<T>>* p, int i) : fParent(p), fIdx(i) {}
const Proxy<std::vector<T>>* fParent;
size_t fIdx;
};
iterator<const T> begin() const {return iterator<const T>(this, 0 );}
iterator< T> begin() {return iterator< T>(this, 0 );}
iterator<const T> end() const {return iterator<const T>(this, size());}
iterator< T> end() {return iterator< T>(this, size());}
protected:
/// Implies CheckIndex()
void EnsureLongEnough(size_t i) const
{
CheckIndex(i, size());
if(i >= fElems.size()) fElems.resize(i+1);
EnsureIdxP();
if(fIdxP) fIdx = *fIdxP; // store into an actual value we can point to
if(!fElems[i]) fElems[i] = new Proxy<T>(fTree, Subscript(i), fIdx, i);
}
mutable std::vector<Proxy<T>*> fElems;
};
// Retain an alias to the old naming scheme for now
template <class T> using VectorProxy = Proxy<std::vector<T>>;
/// Used in comparison of GENIE version numbers
template<class T> bool operator<(const Proxy<std::vector<T>>& a,
const std::vector<T>& b)
{
const size_t N = a.size();
if(N != b.size()) return N < b.size();
for(size_t i = 0; i < N; ++i){
if(a[i] != b[i]) return a[i] < b[i];
}
return false;
}
template<class T, unsigned int N> class Proxy<T[N]> : public ArrayVectorProxyBase
{
public:
Proxy(TTree* tr, const std::string& name, const long& base, int offset)
: ArrayVectorProxyBase(tr, name, is_vec<T>::value || std::is_array_v<T>, base, offset)
{
fElems.fill(0); // ensure initialized to null
}
Proxy(TTree* tr, const std::string& name) : Proxy(tr, name, kDummyBase, 0) {}
~Proxy()
{
for(Proxy<T>* e: fElems) delete e;
}
Proxy& operator=(const Proxy<T[N]>&) = delete;
Proxy(const Proxy<T[N]>& v) = delete;
const Proxy<T>& operator[](size_t i) const
{
EnsureElem(i);
if(fIdxP) fIdx = *fIdxP;
return *fElems[i];
}
Proxy<T>& operator[](size_t i)
{
EnsureElem(i);
if(fIdxP) fIdx = *fIdxP;
return *fElems[i];
}
Proxy<T[N]>& operator=(const T (&x)[N])
{
for(unsigned int i = 0; i < N; ++i) (*this)[i] = x[i];
return *this;
}
void CheckEquals(const T (&x)[N]) const
{
for(unsigned int i = 0; i < N; ++i) (*this)[i].CheckEquals(x[i]);
}
protected:
void EnsureElem(int i) const
{
CheckIndex(i, N);
if(fElems[i]) return; // element already created
if(fType != kFlat || TreeHasLeaf(fTree, IndexField())){
// Regular out-of-line array, handled the same as a vector.
EnsureIdxP();
fElems[i] = new Proxy<T>(fTree, Subscript(i), fIdx, i);
}
else{
// No ..idx field implies this is an "inline" array where the elements
// are in individual branches like foo.0.bar
const std::string dotname = fName+"."+std::to_string(i);
fElems[i] = new Proxy<T>(fTree, dotname, fBase, fOffset);
}
}
mutable std::array<Proxy<T>*, N> fElems;
};
// Retain an alias to the old naming scheme for now
template <class T, unsigned int N> using ArrayProxy = Proxy<T[N]>;
template<class T> class RestorerT
{
public:
~RestorerT()
{
// Restore values in reverse, i.e. in first-in, last-out order so that if
// a value was edited multiple time it will eventually be restored to its
// original value.
for(auto it = fVals.rbegin(); it != fVals.rend(); ++it)
*it->first = it->second;
}
void Add(T* p, T v)
{
fVals.emplace_back(p, v);
}
protected:
std::vector<std::pair<T*, T>> fVals;
};
class Restorer: public
RestorerT<char>,
RestorerT<short>,
RestorerT<int>,
RestorerT<long>,
RestorerT<long long>,
RestorerT<unsigned char>,
RestorerT<unsigned short>,
RestorerT<unsigned int>,
RestorerT<unsigned long>,
RestorerT<unsigned long long>,
RestorerT<float>,
RestorerT<double>,
RestorerT<long double>,
RestorerT<bool>,
RestorerT<std::string>
{
public:
template<class T> void Add(Proxy<T>& p)
{
RestorerT<typename Proxy<T>::U>::Add(&p.fVal, p.GetValue());
}
};
class SRProxySystController
{
public:
static bool AnyShifted()
{
for(const Restorer* r: fRestorers) if(r) return true;
return false;
}
static void BeginTransaction()
{
fRestorers.push_back(0);
}
static bool InTransaction()
{
return !fRestorers.empty();
}
static void Rollback()
{
assert(!fRestorers.empty());
if(fRestorers.back()) ++fGeneration;
delete fRestorers.back();
fRestorers.pop_back();
}
/// May be useful in the implementation of caches that ought to be
/// invalidated when systematic shifts are applied.
static long long Generation()
{
if(!InTransaction()) return 0; // nominal
return fGeneration;
}
protected:
template<class T> friend class Proxy;
template<class T> static void Backup(Proxy<T>& p)
{
assert(!fRestorers.empty());
if(!fRestorers.back()){
++fGeneration;
fRestorers.back() = new Restorer;
}
fRestorers.back()->Add(p);
}
static std::vector<Restorer*> fRestorers;
static long long fGeneration;
};
} // namespace
namespace std
{
template<class T> T min(const caf::Proxy<T>& a, T b)
{
return std::min(a.GetValue(), b);
}
template<class T> T min(T a, const caf::Proxy<T>& b)
{
return std::min(a, b.GetValue());
}
template<class T> T max(const caf::Proxy<T>& a, T b)
{
return std::max(a.GetValue(), b);
}
template<class T> T max(T a, const caf::Proxy<T>& b)
{
return std::max(a, b.GetValue());
}
// We override these two so that the callers don't trigger the warning
// printout from operator T.
template<class T> bool isnan(const caf::Proxy<T>& x)
{
return std::isnan(x.GetValue());
}
template<class T> bool isinf(const caf::Proxy<T>& x)
{
return std::isinf(x.GetValue());
}
}
// There are also versions of these not in std:: that we want to override
using std::isnan;
using std::isinf;