SystShifts.h
Go to the documentation of this file.
1 #pragma once
2 
4 
5 #include <map>
6 #include <memory>
7 #include <string>
8 #include <unordered_map>
9 #include <vector>
10 
11 class TDirectory;
12 
14 
15 namespace ana
16 {
17  class ISyst;
18 
19  /// Simple record of shifts applied to systematic parameters
20  class SystShifts
21  {
22  public:
23  SystShifts();
24  SystShifts(const ISyst* syst, double shift);
25  SystShifts(const ISyst* syst, stan::math::var shift);
26  SystShifts(const std::map<const ISyst*, double>& shifts);
27  SystShifts(const std::map<const ISyst*, stan::math::var>& shifts);
28 
29  virtual ~SystShifts() = default;
30 
31  /// SystShifts with the same set of systs should have the same ID
32  int ID() const {return fID;}
33 
34  static SystShifts Nominal() {return SystShifts();}
35 
36  std::vector<const ISyst*> ActiveSysts() const;
37 
38  /// Allow derived classes to overload so they can copy themselves
39  /// in case they overload Penalty(). Used in IFitter.
40  /// Note that you own the copy...
41  virtual std::unique_ptr<SystShifts> Copy() const;
42 
43  bool IsNominal() const {return fSystsDbl.empty(); } // since there's always a 'double' copy of any stan ones too
44 
45  /// Remove a shift from the systs, regardless of whether it's a double or stan::math::var shift.
46  /// (The double ones can be eliminated by SetShift(syst, 0), but the stan::math::var ones can't.)
47  void RemoveShift(const ISyst* syst);
48 
49  /// shift: 0 = nominal; +-1 = 1sigma shifts etc. Arbitrary shifts allowed
50  /// set force=true to insert a syst even if the shift is 0
51  void SetShift(const ISyst* syst, double shift, bool force=false);
52  void SetShift(const ISyst* syst, stan::math::var shift);
53 
54  /// Templated so that Stan- and not-stan versions have the same interface.
55  /// If you don't specify anything it'll just give you back the double,
56  /// which is probably what you want anyway.
57  /// If you know you want the Stan version write `GetShift<stan::math::var>(syst)`
58  template <typename T=double>
59  T GetShift(const ISyst* syst) const;
60 
61  void ResetToNominal();
62 
63  bool HasStan(const ISyst* s) const {return fSystsStan.count(s);}
64  bool HasAnyStan() const {return !fSystsStan.empty();}
65 
66  /// Penalty term for (frequentist) chi-squared fits
67  double Penalty() const;
68 
69  /// Prior used in Bayesian fitting. Override as needed.
70  /// If it's more efficient to calculate log(prior)
71  /// explicitly, override LogPrior() as well
72  virtual stan::math::var Prior() const { return 1.; }
73 
74  /// If it's more efficient to implement log(prior) directly,
75  /// override this
76  virtual stan::math::var LogPrior() const;
77 
78  void Shift(caf::SRProxy* sr, double& weight) const;
79  void Shift(caf::SRNeutrinoProxy* sr, double& weight) const;
80 
81  /// Brief description of component shifts, for printing to screen
82  std::string ShortName() const;
83  /// Long description of component shifts, for plot labels
84  std::string LatexName() const;
85 
86  void SaveTo(TDirectory* dir, const std::string& name) const;
87  static std::unique_ptr<SystShifts> LoadFrom(TDirectory* dir, const std::string& name);
88 
89  protected:
90  std::unordered_map<const ISyst*, double> fSystsDbl;
91  mutable std::unordered_map<const ISyst*, stan::math::var> fSystsStan;
92 
93  private:
94  int fID;
95  /// The next unused ID
96  static int fgNextID;
97  };
98 
99  //----------------------------------------------------------------------
100  /// Implement a Gaussian prior on syst shift pulls.
101  /// (SystShifts default is uniform)
103  {
104  public:
105  // inherit base constructors...
107 
108  std::unique_ptr<SystShifts> Copy() const override;
109 
110  stan::math::var LogPrior() const override;
111  stan::math::var Prior() const override;
112  };
113 
114  //----------------------------------------------------------------------
115  extern const SystShifts kNoShift;
116 
118 
119  std::vector <SystShifts> GetSystShiftsMultiverse (
120  const std::vector <std::pair <const ISyst*, SystMode> >& systConfigs,
121  const int nUniverses, const int seed = 1001);
122 }
const XML_Char * name
Definition: expat.h:151
bool HasAnyStan() const
Definition: SystShifts.h:64
static int fgNextID
The next unused ID.
Definition: SystShifts.h:96
std::string ShortName() const
Brief description of component shifts, for printing to screen.
Definition: SystShifts.cxx:192
Cuts and Vars for the 2020 FD DiF Study.
Definition: vars.h:6
virtual stan::math::var Prior() const
Definition: SystShifts.h:72
virtual std::unique_ptr< SystShifts > Copy() const
Definition: SystShifts.cxx:67
bool IsNominal() const
Definition: SystShifts.h:43
const Var weight
virtual ~SystShifts()=default
Simple record of shifts applied to systematic parameters.
Definition: SystShifts.h:20
Proxy for caf::StandardRecord.
Definition: SRProxy.h:2109
std::unordered_map< const ISyst *, stan::math::var > fSystsStan
Definition: SystShifts.h:91
SystMode
Definition: SystShifts.h:117
static SystShifts Nominal()
Definition: SystShifts.h:34
Encapsulate code to systematically shift a caf::SRProxy.
Definition: ISyst.h:14
const XML_Char * s
Definition: expat.h:262
static std::unique_ptr< SystShifts > LoadFrom(TDirectory *dir, const std::string &name)
Definition: SystShifts.cxx:256
int ID() const
SystShifts with the same set of systs should have the same ID.
Definition: SystShifts.h:32
unsigned int seed
Definition: runWimpSim.h:102
std::string LatexName() const
Long description of component shifts, for plot labels.
Definition: SystShifts.cxx:206
T GetShift(const ISyst *syst) const
caf::StandardRecord * sr
std::vector< SystShifts > GetSystShiftsMultiverse(const std::vector< std::pair< const ISyst *, SystMode > > &systConfigs, const int nUniverses, const int seed)
Definition: SystShifts.cxx:282
bool HasStan(const ISyst *s) const
Definition: SystShifts.h:63
std::unordered_map< const ISyst *, double > fSystsDbl
Definition: SystShifts.h:90
const SystShifts kNoShift
Definition: SystShifts.cxx:21
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
TDirectory * dir
Definition: macro.C:5
void ResetToNominal()
Definition: SystShifts.cxx:143
virtual stan::math::var LogPrior() const
Definition: SystShifts.cxx:161
std::vector< const ISyst * > ActiveSysts() const
Definition: SystShifts.cxx:220
double Penalty() const
Penalty term for (frequentist) chi-squared fits.
Definition: SystShifts.cxx:152
double T
Definition: Xdiff_gwt.C:5
void SaveTo(TDirectory *dir, const std::string &name) const
Definition: SystShifts.cxx:228
void RemoveShift(const ISyst *syst)
Definition: SystShifts.cxx:73
void SetShift(const ISyst *syst, double shift, bool force=false)
Definition: SystShifts.cxx:80
void Shift(caf::SRProxy *sr, double &weight) const
Definition: SystShifts.cxx:164