All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Var.cxx
Go to the documentation of this file.
2 
3 #include <algorithm>
4 #include <cmath>
5 #include <map>
6 #include <set>
7 
8 namespace ana
9 {
10  //----------------------------------------------------------------------
11  template<class T> _Var<T>::
12  _Var(const std::function<VarFunc_t>& fun)
13  : fFunc(fun), fID(fgNextID++)
14  {
15  }
16 
17  //----------------------------------------------------------------------
18  template<class T> int _Var<T>::MaxID()
19  {
20  return fgNextID-1;
21  }
22 
23  //----------------------------------------------------------------------
24  /// Helper for \ref Var2D
25  template<class T> class Var2DFunc
26  {
27  public:
28  Var2DFunc(const _Var<T>& a, const Binning binsa,
29  const _Var<T>& b, const Binning binsb)
30  : fA(a), fBinsA(binsa),
31  fB(b), fBinsB(binsb)
32  {
33  }
34 
35  double operator()(const T* sr) const
36  {
37  // Calculate current values of the variables in StandardRecord once
38  const double va = fA(sr);
39  const double vb = fB(sr);
40 
41  // Since there are no overflow/underflow bins, check the range
42  if(va < fBinsA.Min() || vb < fBinsB.Min()) return -1;
43  if(va > fBinsA.Max() || vb > fBinsB.Max()) return fBinsA.NBins() * fBinsB.NBins();
44 
45  // FindBin uses root convention, first bin is bin 1, bin 0 is underflow
46  const int ia = fBinsA.FindBin(va) - 1;
47  const int ib = fBinsB.FindBin(vb) - 1;
48 
49  const int i = ia*fBinsB.NBins()+ib;
50 
51  return i+.5;
52  }
53 
54  protected:
55  const _Var<T> fA;
56  const Binning fBinsA;
57  const _Var<T> fB;
58  const Binning fBinsB;
59  };
60 
61  /// Helper for \ref Var3D
62  template<class T> class Var3DFunc
63  {
64  public:
65  Var3DFunc(const _Var<T>& a, const Binning binsa,
66  const _Var<T>& b, const Binning binsb,
67  const _Var<T>& c, const Binning binsc)
68  : fA(a), fBinsA(binsa),
69  fB(b), fBinsB(binsb),
70  fC(c), fBinsC(binsc)
71  {
72  }
73 
74  double operator()(const T* sr) const
75  {
76  /// Calculate current values of the variables in StandardRecord once
77  const double va = fA(sr);
78  const double vb = fB(sr);
79  const double vc = fC(sr);
80 
81  /// Since there are no overflow/underflow bins, check the range
82  if(va < fBinsA.Min() || vb < fBinsB.Min() || vc < fBinsC.Min()){
83  return -1.0;
84  }
85 
86  if(va > fBinsA.Max() || vb > fBinsB.Max() || vc > fBinsC.Max()){
87  return fBinsA.NBins() * fBinsB.NBins() * fBinsC.NBins();
88  }
89 
90  const int ia = fBinsA.FindBin(va) - 1;
91  const int ib = fBinsB.FindBin(vb) - 1;
92  const int ic = fBinsC.FindBin(vc) - 1;
93  const int i = ia*fBinsB.NBins()*fBinsC.NBins() + ib*fBinsC.NBins() + ic;
94 
95  return i+.5;
96  }
97 
98  protected:
99  const _Var<T> fA;
101  const _Var<T> fB;
103  const _Var<T> fC;
105  };
106 
107  //----------------------------------------------------------------------
108  template<class T> _Var<T>
109  Var2D(const _Var<T>& a, const Binning& binsa,
110  const _Var<T>& b, const Binning& binsb)
111  {
112  return _Var<T>(Var2DFunc<T>(a, binsa, b, binsb));
113  }
114 
115  //----------------------------------------------------------------------
116  template<class T> _Var<T>
117  Var2D(const _Var<T>& a, int na, double a0, double a1,
118  const _Var<T>& b, int nb, double b0, double b1)
119  {
120  return Var2D(a, Binning::Simple(na, a0, a1),
121  b, Binning::Simple(nb, b0, b1));
122  }
123 
124  // explicitly instantiate the template for the types we know we have
125  template Var Var2D(const Var&, const Binning&, const Var&, const Binning&);
126  template SpillVar Var2D(const SpillVar&, const Binning&, const SpillVar&, const Binning&);
127 
128  template Var Var2D(const Var&, int, double, double, const Var&, int, double, double);
129  template SpillVar Var2D(const SpillVar&, int, double, double, const SpillVar&, int, double, double);
130 
131  //----------------------------------------------------------------------
132  template<class T> _Var<T>
133  Var3D(const _Var<T>& a, const Binning& binsa,
134  const _Var<T>& b, const Binning& binsb,
135  const _Var<T>& c, const Binning& binsc)
136  {
137  return _Var<T>(Var3DFunc<T>(a, binsa, b, binsb, c, binsc));
138  }
139 
140  //----------------------------------------------------------------------
141  template<class T> _Var<T>
142  Var3D(const _Var<T>& a, int na, double a0, double a1,
143  const _Var<T>& b, int nb, double b0, double b1,
144  const _Var<T>& c, int nc, double c0, double c1)
145  {
146  return Var3D(a, Binning::Simple(na, a0, a1),
147  b, Binning::Simple(nb, b0, b1),
148  c, Binning::Simple(nc, c0, c1));
149  }
150 
151  // explicitly instantiate the template for the types we know we have
152  template Var Var3D(const Var&, const Binning&, const Var&, const Binning&, const Var&, const Binning&);
153  template SpillVar Var3D(const SpillVar&, const Binning&, const SpillVar&, const Binning&, const SpillVar&, const Binning&);
154 
155  template Var Var3D(const Var&, int, double, double, const Var&, int, double, double, const Var&, int, double, double);
156  template SpillVar Var3D(const SpillVar&, int, double, double, const SpillVar&, int, double, double, const SpillVar&, int, double, double);
157 
158  //----------------------------------------------------------------------
159  Var Scaled(const Var& v, double s)
160  {
161  return Var([v, s](const caf::SRSliceProxy* sr){return s*v(sr);});
162  }
163 
164  //----------------------------------------------------------------------
165  Var Constant(double c)
166  {
167  return Var([c](const caf::SRSliceProxy*){return c;});
168  }
169 
170  //--------------------------------------------------------------------
171 
172  Var Sqrt(const Var& v)
173  {
174  return Var([v](const caf::SRSliceProxy* sr){return sqrt(v(sr));});
175  }
176 
177  //----------------------------------------------------------------------
178  template<class T> _Var<T>
179  operator*(const _Var<T>& a, const _Var<T>& b)
180  {
181  static std::map<std::pair<int, int>, int> ids;
182  const std::pair<int, int> key(a.ID(), b.ID());
183 
184  if(ids.count(key)){
185  return _Var<T>([a, b](const T* sr){return a(sr) * b(sr);},
186  ids[key]);
187  }
188  else{
189  const _Var<T> ret([a, b](const T* sr){return a(sr) * b(sr);});
190  ids[key] = ret.ID();
191  return ret;
192  }
193  }
194 
195  //----------------------------------------------------------------------
196  template<class T> _Var<T>
197  operator/(const _Var<T>& a, const _Var<T>& b)
198  {
199  static std::map<std::pair<int, int>, int> ids;
200  const std::pair<int, int> key(a.ID(), b.ID());
201 
202  if(ids.count(key)){
203  return _Var<T>([a, b](const T* sr)
204  {
205  const double denom = b(sr);
206  if(denom != 0)
207  return a(sr) / denom;
208  else
209  return 0.0;
210  },
211  ids[key]);
212  }
213  else{
214  const _Var<T> ret([a, b](const T* sr)
215  {
216  const double denom = b(sr);
217  if(denom != 0)
218  return a(sr) / denom;
219  else
220  return 0.0;
221  });
222  ids[key] = ret.ID();
223  return ret;
224  }
225  }
226 
227  //----------------------------------------------------------------------
228  template<class T> _Var<T>
229  operator+(const _Var<T>& a, const _Var<T>& b)
230  {
231  static std::map<std::pair<int, int>, int> ids;
232  const std::pair<int, int> key(a.ID(), b.ID());
233 
234  if(ids.count(key)){
235  return _Var<T>([a, b](const T* sr){return a(sr) + b(sr);},
236  ids[key]);
237  }
238  else{
239  const _Var<T> ret([a, b](const T* sr){return a(sr) + b(sr);});
240  ids[key] = ret.ID();
241  return ret;
242  }
243  }
244 
245  //----------------------------------------------------------------------
246  template<class T> _Var<T>
247  operator-(const _Var<T>& a, const _Var<T>& b)
248  {
249  static std::map<std::pair<int, int>, int> ids;
250  const std::pair<int, int> key(a.ID(), b.ID());
251 
252  if(ids.count(key)){
253  return _Var<T>([a, b](const T* sr){return a(sr) - b(sr);},
254  ids[key]);
255  }
256  else{
257  const _Var<T> ret([a, b](const T* sr){return a(sr) - b(sr);});
258  ids[key] = ret.ID();
259  return ret;
260  }
261  }
262 
263  // explicitly instantiate the templates for the types we know we have
264  template class _Var<caf::SRSpillProxy>;
265  template class _Var<caf::SRSliceProxy>;
266 
267  template<class T> int _Var<T>::fgNextID = 0;
268 
269  template Var operator*(const Var&, const Var&);
270  template Var operator/(const Var&, const Var&);
271  template Var operator+(const Var&, const Var&);
272  template Var operator-(const Var&, const Var&);
273  template SpillVar operator*(const SpillVar&, const SpillVar&);
274  template SpillVar operator/(const SpillVar&, const SpillVar&);
275  template SpillVar operator+(const SpillVar&, const SpillVar&);
276  template SpillVar operator-(const SpillVar&, const SpillVar&);
277 }
Represent the binning of a Spectrum&#39;s x-axis.
Definition: Binning.h:18
int ID() const
Vars with the same definition will have the same ID.
Definition: Var.h:46
static int MaxID()
Definition: Var.cxx:18
double operator()(const T *sr) const
Definition: Var.cxx:74
#define a0
const _Var< T > fB
Definition: Var.cxx:101
_Var< T > Var2D(const _Var< T > &a, const Binning &binsa, const _Var< T > &b, const Binning &binsb)
Variable formed from two input variables.
Definition: Var.cxx:109
double operator()(const T *sr) const
Definition: Var.cxx:35
const Binning fBinsA
Definition: Var.cxx:100
process_name opflashCryoW ana
_Var< T > operator-(const _Var< T > &a, const _Var< T > &b)
Definition: Var.cxx:247
caf::Proxy< caf::SRSlice > SRSliceProxy
Definition: EpilogFwd.h:2
_Var< caf::SRSliceProxy > Var
Representation of a variable to be retrieved from a caf::StandardRecord object.
Definition: Var.h:73
_Var< T > Var3D(const _Var< T > &a, const Binning &binsa, const _Var< T > &b, const Binning &binsb, const _Var< T > &c, const Binning &binsc)
This is just like a Var2D, but useful for 3D Spectra.
Definition: Var.cxx:133
Var Constant(double c)
Use to weight events up and down by some factor.
Definition: Var.cxx:165
const _Var< T > fC
Definition: Var.cxx:103
process_name gaushit a
_Var< caf::SRSpillProxy > SpillVar
Equivalent of Var acting on caf::SRSpill.
Definition: Var.h:76
double Min() const
Definition: Binning.h:28
const Binning fBinsB
Definition: Var.cxx:102
Helper for Var3D.
Definition: Var.cxx:62
EnsembleSpectrum operator*(const EnsembleRatio &lhs, const EnsembleSpectrum &rhs)
int FindBin(float x) const
Definition: Binning.cxx:100
EnsembleRatio operator/(const EnsembleSpectrum &lhs, const EnsembleSpectrum &rhs)
Definition: EnsembleRatio.h:39
const _Var< T > fA
Definition: Var.cxx:55
double Max() const
Definition: Binning.h:29
const _Var< T > fB
Definition: Var.cxx:57
_Var< T > operator+(const _Var< T > &a, const _Var< T > &b)
Definition: Var.cxx:229
const Binning fBinsA
Definition: Var.cxx:56
Var Sqrt(const Var &v)
Use to take sqrt of a var.
Definition: Var.cxx:172
then echo File list $list not found else cat $list while read file do echo $file sed s
Definition: file_to_url.sh:60
int NBins() const
Definition: Binning.h:27
const _Var< T > fA
Definition: Var.cxx:99
_Var(const std::function< VarFunc_t > &fun)
std::function can wrap a real function, function object, or lambda
Definition: Var.cxx:12
Var2DFunc(const _Var< T > &a, const Binning binsa, const _Var< T > &b, const Binning binsb)
Definition: Var.cxx:28
Var Scaled(const Var &v, double s)
Use to rescale another variable.
Definition: Var.cxx:159
const Binning fBinsB
Definition: Var.cxx:58
Most useful for combining weights.
Definition: Var.h:23
static Binning Simple(int n, double lo, double hi, const std::vector< std::string > &labels={})
Definition: Binning.cxx:38
Helper for Var2D.
Definition: Var.cxx:25
Var3DFunc(const _Var< T > &a, const Binning binsa, const _Var< T > &b, const Binning binsb, const _Var< T > &c, const Binning binsc)
Definition: Var.cxx:65
#define a1
const Binning fBinsC
Definition: Var.cxx:104