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 
13 namespace caf{class SRProxy; class SRNeutrinoProxy;}
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  /// shift: 0 = nominal; +-1 = 1sigma shifts etc. Arbitrary shifts allowed
46  /// set force=true to insert a syst even if the shift is 0
47  void SetShift(const ISyst* syst, double shift, bool force=false);
48  void SetShift(const ISyst* syst, stan::math::var shift);
49 
50  /// Templated so that Stan- and not-stan versions have the same interface.
51  /// If you don't specify anything it'll just give you back the double,
52  /// which is probably what you want anyway.
53  /// If you know you want the Stan version write `GetShift<stan::math::var>(syst)`
54  template <typename T=double>
55  T GetShift(const ISyst* syst) const;
56 
57  void ResetToNominal();
58 
59  /// Penalty term for (frequentist) chi-squared fits
60  double Penalty() const;
61 
62  /// Prior used in Bayesian fitting. Override as needed.
63  /// If it's more efficient to calculate log(prior)
64  /// explicitly, override LogPrior() as well
65  virtual stan::math::var Prior() const { return 1.; }
66 
67  /// If it's more efficient to implement log(prior) directly,
68  /// override this
69  virtual stan::math::var LogPrior() const;
70 
71  void Shift(caf::SRProxy* sr, double& weight) const;
72  void Shift(caf::SRNeutrinoProxy* sr, double& weight) const;
73 
74  /// Brief description of component shifts, for printing to screen
75  std::string ShortName() const;
76  /// Long description of component shifts, for plot labels
77  std::string LatexName() const;
78 
79 
80  void SaveTo(TDirectory* dir) const;
81  static std::unique_ptr<SystShifts> LoadFrom(TDirectory* dir);
82 
83 
84  protected:
85  std::unordered_map<const ISyst*, double> fSystsDbl;
86  mutable std::unordered_map<const ISyst*, stan::math::var> fSystsStan;
87 
88  private:
89  int fID;
90  /// The next unused ID
91  static int fgNextID;
92 
93  };
94 
95  //----------------------------------------------------------------------
96  /// Implement a Gaussian prior on syst shift pulls.
97  /// (SystShifts default is uniform)
99  {
100  public:
101  // inherit base constructors...
102  using SystShifts::SystShifts;
103 
104  std::unique_ptr<SystShifts> Copy() const override;
105 
106  stan::math::var LogPrior() const override;
107  stan::math::var Prior() const override;
108  };
109 
110  //----------------------------------------------------------------------
111 
112  const SystShifts kNoShift = SystShifts::Nominal();
113 
115 
116  std::vector <SystShifts> GetSystShiftsMultiverse (
117  const std::vector <std::pair <const ISyst*, SystMode> > systConfigs,
118  const int nUniverses, const int seed = 1001);
119 }
SystShifts GetShift(std::string shiftName, double sigma)
static int fgNextID
The next unused ID.
Definition: SystShifts.h:91
Oscillation analysis framework, runs over CAF files outside of ART.
virtual stan::math::var Prior() const
Definition: SystShifts.h:65
bool IsNominal() const
Definition: SystShifts.h:43
Simple record of shifts applied to systematic parameters.
Definition: SystShifts.h:20
float weight
Definition: plotSysts.py:333
std::unordered_map< const ISyst *, stan::math::var > fSystsStan
Definition: SystShifts.h:86
SystMode
Definition: SystShifts.h:114
var
Proxy for StandardRecord.
Definition: SRProxy.h:2237
static SystShifts Nominal()
Definition: SystShifts.h:34
Encapsulate code to systematically shift a caf::SRProxy.
Definition: ISyst.h:14
std::vector< SystShifts > GetSystShiftsMultiverse(const std::vector< std::pair< const ISyst *, SystMode > > systConfigs, const int nUniverses, const int seed)
Definition: SystShifts.cxx:250
void SaveTo(const osc::IOscCalculator &x, TDirectory *dir)
Definition: Shift.h:6
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
Proxy for SRNeutrino.
Definition: SRProxy.h:1568
std::unordered_map< const ISyst *, double > fSystsDbl
Definition: SystShifts.h:85
const SystShifts kNoShift
Definition: SystShifts.h:112
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
TDirectory * dir
Definition: macro.C:5
string syst
Definition: plotSysts.py:176
double T
Definition: Xdiff_gwt.C:5
This module creates Common Analysis Files.
Definition: CSVMaker.h:8
std::unique_ptr< T > LoadFrom(TDirectory *dir)
Definition: LoadFromFile.h:18
static constexpr Double_t sr
Definition: Munits.h:164