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/Binning.h"
7 #include "CAFAna/Core/Cut.h"
8 
9 #include <functional>
10 
11 namespace ana
12 {
13  /// Template for Var and SpillVar
14  template<class T> class _Var
15  {
16  public:
17  /// The type of the function part of a var
18  typedef double (VarFunc_t)(const T* sr);
19 
20  /// std::function can wrap a real function, function object, or lambda
21  _Var(const std::function<VarFunc_t>& fun);
22 
23  _Var(const _Var<T>& v);
24 
25  _Var& operator=(const _Var<T>& v);
26 
27  ~_Var();
28 
29  /// Allows a variable to be called with double value = myVar(sr) syntax
30  double operator()(const T* sr) const
31  {
32  return fFunc(sr);
33  }
34 
35  /// Vars with the same definition will have the same ID
36  int ID() const {return fID;}
37 
38  static int MaxID() {return fgNextID-1;}
39 
40  _Cut<T> operator<(double c) const;
41  _Cut<T> operator>(double c) const;
42  _Cut<T> operator>=(double c) const;
43  _Cut<T> operator<=(double c) const;
44  _Cut<T> operator==(double c) const;
45  _Cut<T> operator!=(double c) const;
46 
47  _Cut<T> operator>(const _Var<T>& v) const;
48  _Cut<T> operator<(const _Var<T>& v) const;
49  _Cut<T> operator>=(const _Var<T>& v) const;
50  _Cut<T> operator<=(const _Var<T>& v) const;
51  _Cut<T> operator==(const _Var<T>& v) const;
52  _Cut<T> operator!=(const _Var<T>& v) const;
53 
54  // Most useful for combining weights.
55  _Var<T> operator*(const _Var<T>& v) const;
56  _Var<T> operator/(const _Var<T>& v) const;
57  _Var<T> operator+(const _Var<T>& v) const;
58  _Var<T> operator-(const _Var<T>& v) const;
59 
60  protected:
61  _Var(const std::function<VarFunc_t>& fun, int id);
62  std::function<VarFunc_t> fFunc;
63 
64  int fID;
65  /// The next ID that hasn't yet been assigned
66  static int fgNextID;
67  };
68 
69  /// \brief Representation of a variable to be retrieved from a \ref
70  /// caf::StandardRecord object
71  ///
72  /// A Var consists of a function, taking a StandardRecord and returning the
73  /// value of the variable (which may be some complicated function).
75 
76  /// \brief Equivalent of \ref Var acting on \ref caf::SRSpill. For use in
77  /// making plots of POT per run etc
79 
80  /// \brief Var designed to be used over the nuTree, ie all neutrinos, not
81  /// just those that got slices.
83 
84  /// \brief For Vars where literally all you need is a single CAF variable
85  ///
86  /// eg Var myVar = SIMPLEVAR(my.var.str);
87  /// NB lack of quotes quotes around my.var.str
88 #define SIMPLEVAR(CAFNAME) ana::Var([](const caf::SRProxy* sr){return sr->CAFNAME;})
89 #define SIMPLENUTRUTHVAR(CAFNAME) ana::NuTruthVar([](const caf::SRNeutrinoProxy* nu){return nu->CAFNAME;})
90 
91  template<class T> struct One{double operator()(const T*) const {return 1;}};
92 
93  template<class T> _Var<T> Unweighted(){return _Var<T>(One<T>());}
94 
95  /// The simplest possible Var, always 1. Used as a default weight.
97 
99 
101 
102  /// \brief Variable formed from two input variables
103  ///
104  /// The binning of each variable has to be given to allow conversion into a
105  /// 1D binned representation.
106  template<class T> _Var<T>
107  Var2D(const _Var<T>& a, const Binning& binsa,
108  const _Var<T>& b, const Binning& binsb);
109 
110  /// \brief Variable formed from two input variables
111  ///
112  /// The binning of each variable has to be given to allow conversion into a
113  /// 1D binned representation.
114  template<class T> _Var<T>
115  Var2D(const _Var<T>& a, int na, double a0, double a1,
116  const _Var<T>& b, int nb, double b0, double b1);
117 
118  /// This is just like a Var2D, but useful for 3D Spectra
119  template<class T> _Var<T>
120  Var3D(const _Var<T>& a, const Binning& binsa,
121  const _Var<T>& b, const Binning& binsb,
122  const _Var<T>& c, const Binning& binsc);
123 
124  /// This is just like a Var2D, but useful for 3D Spectra
125  template<class T> _Var<T>
126  Var3D(const _Var<T>& a, int na, double a0, double a1,
127  const _Var<T>& b, int nb, double b0, double b1,
128  const _Var<T>& c, int nc, double c0, double c1);
129 
130  /// Use to rescale another variable.
131  Var Scaled(const Var& v, double s);
132 
133  /// Use to weight events up and down by some factor
134  Var Constant(double c);
135 
136  /// Use to take sqrt of a var
137  Var Sqrt(const Var& v);
138 
139  // Variants of the comparisons with the constant on the LHS
140  template<class T> _Cut<T> operator>(double c, const _Var<T>& v){return v < c;}
141  template<class T> _Cut<T> operator<(double c, const _Var<T>& v){return v > c;}
142  template<class T> _Cut<T> operator>=(double c, const _Var<T>& v){return v <= c;}
143  template<class T> _Cut<T> operator<=(double c, const _Var<T>& v){return v >= c;}
144  template<class T> _Cut<T> operator!=(double c, const _Var<T>& v){return v != c;}
145 
146 } // namespace
_Cut< T > operator==(double c) const
std::function< VarFunc_t > fFunc
Definition: Var.h:62
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
static int MaxID()
Definition: Var.h:38
int ID() const
Vars with the same definition will have the same ID.
Definition: Var.h:36
_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:247
_Var< T > operator+(const _Var< T > &v) const
Definition: Var.cxx:380
_Var< T > operator/(const _Var< T > &v) const
Definition: Var.cxx:356
~_Var()
Definition: Var.cxx:29
_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:273
_Var< caf::SRProxy > Var
Representation of a variable to be retrieved from a caf::StandardRecord object.
Definition: Var.h:74
Var Constant(double c)
Use to weight events up and down by some factor.
Definition: Var.cxx:313
const SpillVar kSpillUnweighted
Definition: Var.h:98
const XML_Char * s
Definition: expat.h:262
_Var< caf::SRSpillProxy > SpillVar
Equivalent of Var acting on caf::SRSpill. For use in making plots of POT per run etc.
Definition: Var.h:78
double( VarFunc_t)(const T *sr)
The type of the function part of a var.
Definition: Var.h:18
TH1F * a1
Definition: f2_nu.C:476
const double a
double operator()(const T *sr) const
Allows a variable to be called with double value = myVar(sr) syntax.
Definition: Var.h:30
_Cut< T > operator>=(double c) const
_Cut< T > operator>(double c) const
_Var< caf::SRNeutrinoProxy > NuTruthVar
Var designed to be used over the nuTree, ie all neutrinos, not just those that got slices...
Definition: Var.h:82
caf::StandardRecord * sr
double operator()(const T *) const
Definition: Var.h:91
int fID
Definition: Var.h:64
_Var< T > operator-(const _Var< T > &v) const
Definition: Var.cxx:397
Var Sqrt(const Var &v)
Use to take sqrt of a var.
Definition: Var.cxx:326
_Var< T > operator*(const _Var< T > &v) const
Definition: Var.cxx:339
_Cut< T > operator<=(double c) const
Definition: Var.h:91
const hit & b
Definition: hits.cxx:21
_Var< T > Unweighted()
Definition: Var.h:93
const NuTruthVar kNuTruthUnweighted
Definition: Var.h:100
enum BeamMode nc
_Cut< T > operator<(double c) const
c1
Definition: demo5.py:24
_Var(const std::function< VarFunc_t > &fun)
std::function can wrap a real function, function object, or lambda
Definition: Var.cxx:14
double T
Definition: Xdiff_gwt.C:5
Var Scaled(const Var &v, double s)
Use to rescale another variable.
Definition: Var.cxx:301
Template for Cut and SpillCut.
Definition: Cut.h:15
static const double nb
Definition: Units.h:89
Template for Var and SpillVar.
static int fgNextID
The next ID that hasn&#39;t yet been assigned.
Definition: Var.h:66
_Cut< T > operator!=(double c) const
const Var kUnweighted
The simplest possible Var, always 1. Used as a default weight.
Definition: Var.h:96
_Var & operator=(const _Var< T > &v)
Definition: Var.cxx:56