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 "CAFAna/Core/VarBase.h"
7 
8 #include "CAFAna/Core/Cut.h"
9 
10 namespace ana
11 {
12  /// Template for Vars applied to any type of object
13  template<class T> class _Var : protected VarBase
14  {
15  public:
16  /// The type of the function part of a var
17  typedef double (VarFunc_t)(const T* sr);
18 
19  /// User function wants to be called with a specific type, but to pass the
20  /// user function to CutBase it needs to accept void*
21  template<class FuncT, class ArgT> struct AddType
22  {
23  static_assert(std::is_invocable_r_v<double, FuncT, const ArgT*>);
24 
25  // In the case the user passes a bare function, we need to store the
26  // function pointer.
27  typedef typename std::conditional_t<std::is_function_v<FuncT>,
28  typename std::add_pointer_t<FuncT>,
29  FuncT> FuncPT;
30 
31  AddType(const FuncT& f) : fFunc(f) {}
32  double operator()(const void* x){return fFunc((const ArgT*)x);}
33  FuncPT fFunc;
34  };
35 
36  /// std::function can wrap a real function, function object, or lambda
37  _Var(const std::function<VarFunc_t>& func)
38  : VarBase(AddType<decltype(func), T>(func))
39  {
40  }
41 
42  template<class FuncT> _Var(const FuncT& func)
43  : VarBase(AddType<FuncT, T>(func))
44  {
45  }
46 
47  /// \brief Variable formed from two input variables
48  ///
49  /// The binning of each variable has to be given to allow conversion into a
50  /// 1D binned representation.
51  _Var(const _Var& a, const Binning& binsa,
52  const _Var& b, const Binning& binsb)
53  : VarBase(a, binsa, b, binsb)
54  {
55  }
56 
57  _Var(const _Var& a, int na, double a0, double a1,
58  const _Var& b, int nb, double b0, double b1)
59  : VarBase(a, Binning::Simple(na, a0, a1),
60  b, Binning::Simple(nb, b0, b1))
61  {
62  }
63 
64  _Var(const _Var& a, const Binning& binsa,
65  const _Var& b, const Binning& binsb,
66  const _Var& c, const Binning& binsc)
67  : VarBase(a, binsa, b, binsb, c, binsc)
68  {
69  }
70 
71  _Var(const _Var& a, int na, double a0, double a1,
72  const _Var& b, int nb, double b0, double b1,
73  const _Var& c, int nc, double c0, double c1)
74  : VarBase(a, Binning::Simple(na, a0, a1),
75  b, Binning::Simple(nb, b0, b1),
76  c, Binning::Simple(nc, c0, c1))
77  {
78  }
79 
80  /// Allows a variable to be called with double value = myVar(rec) syntax
81  double operator()(const T* rec) const
82  {
83  return fFunc(rec);
84  }
85 
86  /// Vars with the same definition will have the same ID
87  using VarBase::ID;
88  using VarBase::MaxID;
89 
90  _Cut<T> operator<(double c) const {return VarBase::operator<(c);}
91  _Cut<T> operator>(double c) const {return VarBase::operator>(c);}
92  _Cut<T> operator>=(double c) const {return VarBase::operator>=(c);}
93  _Cut<T> operator<=(double c) const {return VarBase::operator<=(c);}
94  _Cut<T> operator==(double c) const {return VarBase::operator==(c);}
95  _Cut<T> operator!=(double c) const {return VarBase::operator!=(c);}
96 
97  _Cut<T> operator>(const _Var& v) const {return VarBase::operator>(v);}
98  _Cut<T> operator<(const _Var& v) const {return VarBase::operator<(v);}
99  _Cut<T> operator>=(const _Var& v) const {return VarBase::operator>=(v);}
100  _Cut<T> operator<=(const _Var& v) const {return VarBase::operator<=(v);}
101  _Cut<T> operator==(const _Var& v) const {return VarBase::operator==(v);}
102  _Cut<T> operator!=(const _Var& v) const {return VarBase::operator!=(v);}
103 
104  // Most useful for combining weights.
105  _Var operator*(const _Var& v) const {return VarBase::operator*(v);}
106  _Var operator/(const _Var& v) const {return VarBase::operator/(v);}
107  _Var operator+(const _Var& v) const {return VarBase::operator+(v);}
108  _Var operator-(const _Var& v) const {return VarBase::operator-(v);}
109 
110  protected:
111  _Var(const VarBase& v) : VarBase(v) {}
112  };
113 
114  template<class T> struct One{double operator()(const T*) const {return 1;}};
115 
116  template<class T> _Var<T> Unweighted(){return _Var<T>(One<T>());}
117 
118  // Variants of the comparisons with the constant on the LHS
119  template<class T> _Cut<T> operator>(double c, const _Var<T>& v){return v < c;}
120  template<class T> _Cut<T> operator<(double c, const _Var<T>& v){return v > c;}
121  template<class T> _Cut<T> operator>=(double c, const _Var<T>& v){return v <= c;}
122  template<class T> _Cut<T> operator<=(double c, const _Var<T>& v){return v >= c;}
123  template<class T> _Cut<T> operator!=(double c, const _Var<T>& v){return v != c;}
124 
125 } // namespace
_Cut< T > operator==(double c) const
Definition: Var.h:94
Represent the binning of a Spectrum&#39;s x-axis.
Definition: Binning.h:16
Cuts and Vars for the 2020 FD DiF Study.
Definition: vars.h:6
std::conditional_t< std::is_function_v< FuncT >, typename std::add_pointer_t< FuncT >, FuncT > FuncPT
Definition: Var.h:23
FuncPT fFunc
Definition: Var.h:33
caf::StandardRecord * rec
Definition: tutCAFMacro.C:20
VarBase operator-(const VarBase &v) const
Definition: VarBase.cxx:323
VarBase operator*(const VarBase &v) const
Definition: VarBase.cxx:268
_Var(const _Var &a, int na, double a0, double a1, const _Var &b, int nb, double b0, double b1)
Definition: Var.h:57
_Cut< T > operator>=(const _Var &v) const
Definition: Var.h:99
_Var(const FuncT &func)
Definition: Var.h:42
double operator()(const T *rec) const
Allows a variable to be called with double value = myVar(rec) syntax.
Definition: Var.h:81
_Cut< T > operator!=(const _Var &v) const
Definition: Var.h:102
_Var operator*(const _Var &v) const
Definition: Var.h:105
double operator()(const void *x)
Definition: Var.h:32
double( VarFunc_t)(const T *sr)
The type of the function part of a var.
Definition: Var.h:17
TH1F * a1
Definition: f2_nu.C:476
AddType(const FuncT &f)
Definition: Var.h:31
const double a
CutBase operator==(double c) const
_Var(const _Var &a, const Binning &binsa, const _Var &b, const Binning &binsb, const _Var &c, const Binning &binsc)
Definition: Var.h:64
_Cut< T > operator>=(double c) const
Definition: Var.h:92
_Cut< T > operator>(double c) const
Definition: Var.h:91
caf::StandardRecord * sr
double func(double x, double y)
_Var operator/(const _Var &v) const
Definition: Var.h:106
double operator()(const T *) const
Definition: Var.h:114
_Var(const _Var &a, int na, double a0, double a1, const _Var &b, int nb, double b0, double b1, const _Var &c, int nc, double c0, double c1)
Definition: Var.h:71
_Var operator+(const _Var &v) const
Definition: Var.h:107
_Var(const std::function< VarFunc_t > &func)
std::function can wrap a real function, function object, or lambda
Definition: Var.h:37
CutBase operator<(double c) const
_Cut< T > operator<=(double c) const
Definition: Var.h:93
VarBase operator+(const VarBase &v) const
Definition: VarBase.cxx:307
Definition: Var.h:114
_Cut< T > operator==(const _Var &v) const
Definition: Var.h:101
const hit & b
Definition: hits.cxx:21
_Var< T > Unweighted()
Definition: Var.h:116
CutBase operator>=(double c) const
enum BeamMode nc
_Cut< T > operator<(double c) const
Definition: Var.h:90
_Var(const _Var &a, const Binning &binsa, const _Var &b, const Binning &binsb)
Variable formed from two input variables.
Definition: Var.h:51
c1
Definition: demo5.py:24
CutBase operator>(double c) const
double T
Definition: Xdiff_gwt.C:5
_Var operator-(const _Var &v) const
Definition: Var.h:108
VarBase operator/(const VarBase &v) const
Definition: VarBase.cxx:284
static const double nb
Definition: Units.h:89
Template for Vars applied to any type of object.
Definition: FwdDeclare.h:12
CutBase operator!=(double c) const
_Var(const VarBase &v)
Definition: Var.h:111
_Cut< T > operator!=(double c) const
Definition: Var.h:95
static int MaxID()
Definition: VarBase.h:50
CutBase operator<=(double c) const
int ID() const
Vars with the same definition will have the same ID.
Definition: VarBase.h:48
_Cut< T > operator>(const _Var &v) const
Definition: Var.h:97