FitVarWithPrior.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "CAFAna/Core/IFitVar.h"
5 
6 namespace osc
7 {
8  template <typename T>
10 }
11 
12 namespace ana
13 {
14  /// \brief A convenience FitVar_StanSupport that wraps a pre-existing FitVar_StanSupport with a function you specify (enables re-use of functions)
15  ///
16  /// Warning: the function you pass as a prior *must* be normalizable (i.e., finite integral)
17  /// when multiplied by the likelihood over the domain of your Var.
18  /// This class can't check for you (the MCMC is going to explore that space);
19  /// if your prior*likelihood isn't normalizable, you are likely to get unexpected results.
20  class FitVarWithPrior : virtual public StanFitSupport<IFitVar>
21  {
22  public:
23 #ifdef DICT_FOR_PYTHON
24  // Hacky Workaround for genreflex error: Error in <CloseStreamerInfoROOTFile>: Cannot find class function<var(var,const _IOscCalcAdjustable<var>*>.
25  typedef stan::math::var(*PriorFnType)(stan::math::var, const void*);
26 #else
27  /// Function that will return the *log* of the value of the prior density for a given parameter
28  typedef std::function<typename stan::math::var(stan::math::var, const osc::_IOscCalcAdjustable <stan::math::var>*)> PriorFnType;
29 #endif
30 
31  /// Constructor. Ensure that \a var doesn't go out of scope before this object does!
33  PriorFnType priorFn,
34  const std::string &priorName)
35  : StanFitSupport<IFitVar>(dynamic_cast<const IFitVar*>(var)->ShortName() + "_" + priorName,
36  dynamic_cast<const IFitVar*>(var)->LatexName()),
37  fVar(var), fPriorFn(priorFn)
38  {}
39 
40  /// Forward to wrapped Var's GetValue()
41  double GetValue(const osc::IOscCalcAdjustable *osc) const override
42  {
43  return fVar->GetValue(osc);
44  }
45  stan::math::var GetValue(const osc::_IOscCalcAdjustable<stan::math::var> *osc) const override
46  {
47  return fVar->GetValue(osc);
48  };
49 
50  /// Forward to wrapped Var's SetValue()
51  void SetValue(osc::IOscCalcAdjustable *osc, double val) const override
52  {
53  fVar->SetValue(osc, val);
54  };
55  void SetValue(osc::_IOscCalcAdjustable<stan::math::var> *osc, stan::math::var val) const override
56  {
57  fVar->SetValue(osc, val);
58  };
59 
60  /// Implement calculation of log-prior by forwarding to stored function
61  stan::math::var
62  LogPrior(const stan::math::var &val, const osc::_IOscCalcAdjustable<stan::math::var> *calc) const override
63  {
64  return fPriorFn(val, calc);
65  }
66 
67  private:
69 
71  };
72 
73 /// Version of FitVarWithPrior for use with constrained FitVar_StanSupports
74  class ConstrainedFitVarWithPrior : virtual public StanFitSupport<IConstrainedFitVar>
75  {
76  public:
77 #ifdef DICT_FOR_PYTHON
78  // Hacky Workaround for genreflex error: Error in <CloseStreamerInfoROOTFile>: Cannot find class function<var(var,const _IOscCalcAdjustable<var>*>.
79  typedef stan::math::var(*PriorFnType)(stan::math::var, const void*);
80 #else
81  typedef std::function<typename stan::math::var(const stan::math::var &,
83 #endif
84 
87  const std::string &priorName)
88  : StanFitSupport<IConstrainedFitVar>(dynamic_cast<const IFitVar*>(var)->ShortName() + "_" + priorName,
89  dynamic_cast<const IFitVar*>(var)->LatexName()),
90  fVar(var),
91  fPriorFn(priorFn),
92  fPriorFnName(priorName)
93  {}
94 
95  double GetValue(const osc::IOscCalcAdjustable *osc) const override
96  {
97  return fVar->GetValue(osc);
98  }
100  {
101  return fVar->GetValue(osc);
102  };
103 
104  void SetValue(osc::IOscCalcAdjustable *osc, double val) const override
105  {
106  fVar->SetValue(osc, val);
107  };
109  {
110  fVar->SetValue(osc, val);
111  };
112 
115  {
116  return fPriorFn(val, calc);
117  }
118 
119  double LowLimit() const override
120  {
121  return fVar->LowLimit();
122  };
123 
124  double HighLimit() const override
125  {
126  return fVar->HighLimit();
127  };
128 
129  const std::string &PriorName() const
130  {
131  return fPriorFnName;
132  };
133 
134  private:
136 
137  PriorFnType fPriorFn;
138 
140 
141  };
142 } // namespace ana
const StanFitSupport< IConstrainedFitVar > * fVar
std::function< typename stan::math::var(stan::math::var, const osc::_IOscCalcAdjustable< stan::math::var > *)> PriorFnType
Function that will return the log of the value of the prior density for a given parameter.
Cuts and Vars for the 2020 FD DiF Study.
Definition: vars.h:6
const std::string & PriorName() const
double GetValue(const osc::IOscCalcAdjustable *osc) const override
double GetValue(const osc::IOscCalcAdjustable *osc) const override
Forward to wrapped Var&#39;s GetValue()
double LowLimit() const override
var_value< double > var
Definition: StanTypedefs.h:14
FitVarWithPrior(const StanFitSupport< IFitVar > *var, PriorFnType priorFn, const std::string &priorName)
Constructor. Ensure that var doesn&#39;t go out of scope before this object does!
void SetValue(osc::IOscCalcAdjustable *osc, double val) const override
Forward to wrapped Var&#39;s SetValue()
ConstrainedFitVarWithPrior(const StanFitSupport< IConstrainedFitVar > *var, FitVarWithPrior::PriorFnType priorFn, const std::string &priorName)
osc::OscCalcDumb calc
stan::math::var GetValue(const osc::IOscCalcAdjustableStan *osc) const override
double HighLimit() const override
stan::math::var GetValue(const osc::_IOscCalcAdjustable< stan::math::var > *osc) const override
void SetValue(osc::IOscCalcAdjustableStan *osc, stan::math::var val) const override
void SetValue(osc::IOscCalcAdjustable *osc, double val) const override
const StanFitSupport< IFitVar > * fVar
Oscillation probability calculators.
Definition: Calcs.h:5
stan::math::var LogPrior(const stan::math::var &val, const osc::_IOscCalcAdjustable< stan::math::var > *calc) const override
Implement calculation of log-prior by forwarding to stored function.
General interface to any calculator that lets you set the parameters.
Interface definition for fittable variables.
Definition: IFitVar.h:17
Version of FitVarWithPrior for use with constrained FitVar_StanSupports.
std::function< typename stan::math::var(const stan::math::var &, const osc::_IOscCalcAdjustable< stan::math::var > *)> PriorFnType
A convenience FitVar_StanSupport that wraps a pre-existing FitVar_StanSupport with a function you spe...
void SetValue(osc::_IOscCalcAdjustable< stan::math::var > *osc, stan::math::var val) const override
stan::math::var LogPrior(const stan::math::var &val, const osc::_IOscCalcAdjustable< stan::math::var > *calc) const override
enum BeamMode string