All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Var.h
Go to the documentation of this file.
1 #pragma once
2 
3 // This file defines the basic Var object. For specific variables, and examples
4 // of how to implement your own, see Vars.h
5 
6 #include <functional>
7 #include <set>
8 #include <string>
9 
11 
12 #include "sbnanaobj/StandardRecord/Proxy/FwdDeclare.h"
13 
14 namespace caf{class SRSpill; class SRSpillTruthBranch; class SRSlice;}
15 
16 namespace ana
17 {
18 
19  /// Most useful for combining weights.
20  // need to declare these functions first
21  // (this needed because of reasons detailed in https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Making_New_Friends;
22  // we want a 'one-to-one' relationship.)
23  template<class T> class _Var;
24  template<class T> _Var<T> operator*(const _Var<T>& a, const _Var<T>& b);
25  template<class T> _Var<T> operator/(const _Var<T>& a, const _Var<T>& b);
26  template<class T> _Var<T> operator+(const _Var<T>& a, const _Var<T>& b);
27  template<class T> _Var<T> operator-(const _Var<T>& a, const _Var<T>& b);
28 
29  /// Template for Var and SpillVar
30  template<class T> class _Var
31  {
32  public:
33  /// The type of the function part of a var
34  typedef double (VarFunc_t)(const T* sr);
35 
36  /// std::function can wrap a real function, function object, or lambda
37  _Var(const std::function<VarFunc_t>& fun);
38 
39  /// Allows a variable to be called with double value = myVar(sr) syntax
40  double operator()(const T* sr) const
41  {
42  return fFunc(sr);
43  }
44 
45  /// Vars with the same definition will have the same ID
46  int ID() const {return fID;}
47 
48  static int MaxID();
49  protected:
50  // Give this guy access to the constructor that sets ID
51  friend _Var<T> operator*<>(const _Var<T>& a, const _Var<T>& b);
52  friend _Var<T> operator/<>(const _Var<T>& a, const _Var<T>& b);
53  friend _Var<T> operator+<>(const _Var<T>& a, const _Var<T>& b);
54  friend _Var<T> operator-<>(const _Var<T>& a, const _Var<T>& b);
55 
56  _Var(const std::function<VarFunc_t>& fun, int id)
57  : fFunc(fun), fID(id)
58  {
59  }
60 
61  std::function<VarFunc_t> fFunc;
62 
63  int fID;
64  /// The next ID that hasn't yet been assigned
65  static int fgNextID;
66  };
67 
68  /// \brief Representation of a variable to be retrieved from a \ref
69  /// caf::StandardRecord object
70  ///
71  /// A Var consists of a function, taking a StandardRecord and returning the
72  /// value of the variable (which may be some complicated function).
74 
75  /// \brief Equivalent of \ref Var acting on \ref caf::SRSpill
77 
78  /// \brief For Vars where literally all you need is a single CAF variable
79  ///
80  /// eg Var myVar = SIMPLEVAR(my.var.str);
81  /// NB lack of quotes quotes around my.var.str
82 #define SIMPLEVAR(CAFNAME) Var([](const caf::SRSliceProxy* sr) -> double {return sr->CAFNAME;})
83 
84 #define SIMPLESPILLVAR(CAFNAME) SpillVar([](const caf::SRSpillProxy* sr) -> double {return sr->CAFNAME;})
85 
86  /// The simplest possible Var, always 1. Used as a default weight.
87  const Var kUnweighted([](const caf::SRSliceProxy*){return 1;});
88 
89  const SpillVar kSpillUnweighted([](const caf::SRSpillProxy*){return 1;});
90 
91  /// \brief Variable formed from two input variables
92  ///
93  /// The binning of each variable has to be given to allow conversion into a
94  /// 1D binned representation.
95  template<class T> _Var<T>
96  Var2D(const _Var<T>& a, const Binning& binsa,
97  const _Var<T>& b, const Binning& binsb);
98 
99  /// \brief Variable formed from two input variables
100  ///
101  /// The binning of each variable has to be given to allow conversion into a
102  /// 1D binned representation.
103  template<class T> _Var<T>
104  Var2D(const _Var<T>& a, int na, double a0, double a1,
105  const _Var<T>& b, int nb, double b0, double b1);
106 
107  /// This is just like a Var2D, but useful for 3D Spectra
108  template<class T> _Var<T>
109  Var3D(const _Var<T>& a, const Binning& binsa,
110  const _Var<T>& b, const Binning& binsb,
111  const _Var<T>& c, const Binning& binsc);
112 
113  /// This is just like a Var2D, but useful for 3D Spectra
114  template<class T> _Var<T>
115  Var3D(const _Var<T>& a, int na, double a0, double a1,
116  const _Var<T>& b, int nb, double b0, double b1,
117  const _Var<T>& c, int nc, double c0, double c1);
118 
119  // TODO - remove once no more legacy callers exist
120  #define SpillTruthVar2D Var2D
121  #define SpillTruthVar3D Var3D
122 
123  /// Use to rescale another variable.
124  Var Scaled(const Var& v, double s);
125 
126  /// Use to weight events up and down by some factor
127  Var Constant(double c);
128 
129  /// Use to take sqrt of a var
130  Var Sqrt(const Var& v);
131 } // namespace
const SpillVar kSpillUnweighted([](const caf::SRSpillProxy *){return 1;})
std::function< VarFunc_t > fFunc
Definition: Var.h:61
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( VarFunc_t)(const T *sr)
The type of the function part of a var.
Definition: Var.h:34
#define a0
_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
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
process_name gaushit a
_Var< caf::SRSpillProxy > SpillVar
Equivalent of Var acting on caf::SRSpill.
Definition: Var.h:76
double operator()(const T *sr) const
Allows a variable to be called with double value = myVar(sr) syntax.
Definition: Var.h:40
caf::Proxy< caf::StandardRecord > SRSpillProxy
Definition: EpilogFwd.h:3
EnsembleSpectrum operator*(const EnsembleRatio &lhs, const EnsembleSpectrum &rhs)
EnsembleRatio operator/(const EnsembleSpectrum &lhs, const EnsembleSpectrum &rhs)
Definition: EnsembleRatio.h:39
int fID
Definition: Var.h:63
_Var< T > operator+(const _Var< T > &a, const _Var< T > &b)
Definition: Var.cxx:229
Var Sqrt(const Var &v)
Use to take sqrt of a var.
Definition: Var.cxx:172
const Var kUnweighted([](const caf::SRSliceProxy *){return 1;})
The simplest possible Var, always 1. Used as a default weight.
then echo File list $list not found else cat $list while read file do echo $file sed s
Definition: file_to_url.sh:60
_Var(const std::function< VarFunc_t > &fun)
std::function can wrap a real function, function object, or lambda
Definition: Var.cxx:12
Var Scaled(const Var &v, double s)
Use to rescale another variable.
Definition: Var.cxx:159
Most useful for combining weights.
Definition: Var.h:23
static int fgNextID
The next ID that hasn&#39;t yet been assigned.
Definition: Var.h:65
_Var(const std::function< VarFunc_t > &fun, int id)
Definition: Var.h:56
#define a1