IFitVar.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 
6 #include "CAFAna/Core/Registry.h"
8 
10 
11 namespace ana
12 {
13  //----------------------------------------------------------------------
14 
15  /// Interface definition for fittable variables
16  class IFitVar
17  {
18  public:
20  : fShortName(std::move(shortName)),
21  fLatexName(std::move(latexName))
22  {
24  }
25 
26  virtual ~IFitVar()
27  {
29  }
30 
31  virtual double GetValue(const osc::IOscCalcAdjustable* osc) const = 0;
32  virtual void SetValue(osc::IOscCalcAdjustable* osc, double val) const = 0;
33 
34  virtual double Penalty(double, osc::IOscCalcAdjustable*) const {return 0;}
35 
36  const std::string & ShortName() const { return fShortName; };
37  const std::string & LatexName() const { return fLatexName; };
38 
39  private:
42  };
43 
44 
45  //----------------------------------------------------------------------
46 
48  {
49  public:
50  using IFitVar::IFitVar;
51 
52  virtual double LowLimit() const = 0;
53  virtual double HighLimit() const = 0;
54 
55  /// Override the default implementation with one that pushes back towards the center of the constrained space
56  double Penalty(double val, osc::IOscCalcAdjustable*) const override;
57 
58  protected:
59  template <typename T>
60  T Clamp(T val) const
61  {
62  return std::max(util::GetValAs<T>(LowLimit()), std::min(val, util::GetValAs<T>(HighLimit())));
63  }
64 
65  };
66 
67  // We want the callers of these to be inlined below so the class can work
68  // templated over any type, but we need the implementation in the .cxx so we
69  // don't have to include the full Stan.h here. This is ugly :(
72 
73  //----------------------------------------------------------------------
74  /// Mixin class to indicate that Stan support should be expected in a given FitVar.
75  /// Inherit from this <i>in addition to</i> either IFitVar or IConstrainedFitVar.
76  /// For more on this idiom consult, e.g., http://www.drdobbs.com/cpp/mixin-based-programming-in-c/184404445?pgno=1
77  template <typename VarClass>
78  class StanFitSupport : public VarClass
79  {
80  public:
81  // just pass all the interesting constructor work to the base class. we have no data.
82  using VarClass::VarClass;
83 
84 
85 
86  // also make sure we don't lose its implementations of GetValue() and SetValue()
87  using VarClass::GetValue;
88  using VarClass::SetValue;
89 
90  // suggest implementing these by making a templated private method
91  // which both the <double> and <stan::math::var> versions forward to
92  virtual stan::math::var GetValue(const osc::IOscCalcAdjustableStan * osc) const = 0;
93  virtual void SetValue(osc::IOscCalcAdjustableStan * osc, stan::math::var val) const = 0;
94 
95  /// The prior probability density distribution on this variable (which can depend on others if needed).
96  /// If there are optimizations that can make log(Prior()) more expensive than a direct implementation,
97  /// consider implementing LogPrior() directly instead.
98  virtual stan::math::var Prior(const stan::math::var& var, const osc::IOscCalcAdjustableStan* calc) const {return StanExp(LogPrior(var, calc));}
99 
100  /// Log of the prior probability density. Default implementation just does log(Prior()),
101  /// but this can be overridden if there optimizations that speed up the calculation.
102  virtual stan::math::var LogPrior(const stan::math::var& var, const osc::IOscCalcAdjustableStan* calc) const {return StanLog(Prior(var, calc));}
103  };
104 
105 } // namespace
T max(const caf::Proxy< T > &a, T b)
const std::string & LatexName() const
Definition: IFitVar.h:37
Cuts and Vars for the 2020 FD DiF Study.
Definition: vars.h:6
IFitVar(std::string shortName, std::string latexName)
Definition: IFitVar.h:19
std::string fShortName
Definition: IFitVar.h:37
static void UnRegister(const T *s)
Definition: Registry.cxx:44
osc::OscCalcDumb calc
static void Register(const T *s)
Definition: Registry.cxx:20
virtual stan::math::var Prior(const stan::math::var &var, const osc::IOscCalcAdjustableStan *calc) const
Definition: IFitVar.h:98
virtual ~IFitVar()
Definition: IFitVar.h:26
std::string fLatexName
Definition: IFitVar.h:41
stan::math::var StanExp(const stan::math::var &x)
Definition: IFitVar.cxx:29
virtual double Penalty(double, osc::IOscCalcAdjustable *) const
Definition: IFitVar.h:34
T Clamp(T val) const
Definition: IFitVar.h:60
Oscillation probability calculators.
Definition: Calcs.h:5
virtual stan::math::var LogPrior(const stan::math::var &var, const osc::IOscCalcAdjustableStan *calc) const
Definition: IFitVar.h:102
const std::string & ShortName() const
Definition: IFitVar.h:36
virtual void SetValue(osc::IOscCalcAdjustable *osc, double val) const =0
Interface definition for fittable variables.
Definition: IFitVar.h:16
string shortName
THUMBNAIL BLOCK: We need to make a thumbnail for each.
double T
Definition: Xdiff_gwt.C:5
T min(const caf::Proxy< T > &a, T b)
stan::math::var StanLog(const stan::math::var &x)
Definition: IFitVar.cxx:30
virtual double GetValue(const osc::IOscCalcAdjustable *osc) const =0
enum BeamMode string