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