All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SystShifts.cxx
Go to the documentation of this file.
2 
5 
6 #include <cassert>
7 
8 #include "TDirectory.h"
9 #include "TH1.h"
10 #include "TObjString.h"
11 #include "TRandom3.h"
12 #include "TString.h"
13 
14 namespace ana
15 {
16  // Reserve 0 for unshifted
17  int SystShifts::fgNextID = 1;
18 
19  //----------------------------------------------------------------------
21  {
22  }
23 
24  //----------------------------------------------------------------------
25  SystShifts::SystShifts(const ISyst* syst, double shift)
26  : fID(fgNextID++)
27  {
28  if(shift != 0) fSysts.emplace(syst, shift);
29  }
30 
31  //----------------------------------------------------------------------
32  SystShifts::SystShifts(const std::map<const ISyst*, double>& shifts)
33  : fID(fgNextID++)
34  {
35  for(auto it: shifts) if(it.second != 0) fSysts.emplace(it.first, it.second);
36  }
37 
38  //----------------------------------------------------------------------
39  SystShifts SystShifts::RandomThrow(const std::vector<const ISyst*>& systs)
40  {
41  SystShifts ret;
42  for(const ISyst* s: systs) ret.SetShift(s, gRandom->Gaus(0, 1));
43  return ret;
44  }
45 
46  //----------------------------------------------------------------------
47  void SystShifts::SetShift(const ISyst* syst, double shift)
48  {
49  fID = fgNextID++;
50 
51  fSysts.erase(syst);
52  if(shift != 0) fSysts.emplace(syst, shift);
53  }
54 
55  //----------------------------------------------------------------------
56  double SystShifts::GetShift(const ISyst* syst) const
57  {
58  assert(syst);
59 
60  auto it = fSysts.find(syst);
61  return (it == fSysts.end()) ? 0 : it->second;
62  }
63 
64  //----------------------------------------------------------------------
66  {
67  fID = 0;
68 
69  fSysts.clear();
70  }
71 
72  //----------------------------------------------------------------------
73  double SystShifts::Penalty() const
74  {
75  double ret = 0;
76  // Systematics are all expressed in terms of sigmas
77  for(auto it: fSysts) {
78  // Only apply a penalty for systematics where this has been requested
79  if (it.first->ApplyPenalty())
80  ret += util::sqr(it.second);
81  }
82  return ret;
83  }
84 
85  //----------------------------------------------------------------------
86  void SystShifts::Shift(caf::SRSliceProxy* sr, double& weight) const
87  {
88  for(auto it: fSysts) it.first->Shift(it.second, sr, weight);
89  }
90 
91  //----------------------------------------------------------------------
92  std::string SystShifts::ShortName() const
93  {
94  if(fSysts.empty()) return "nominal";
95 
96  std::string ret;
97  for(auto it: fSysts){
98  if(!ret.empty()) ret += ",";
99  ret += it.first->ShortName() + TString::Format("=%+g", it.second).Data();
100  }
101 
102  return ret;
103  }
104 
105  //----------------------------------------------------------------------
106  std::string SystShifts::LatexName() const
107  {
108  if(fSysts.empty()) return "Nominal";
109 
110  std::string ret;
111  for(auto it: fSysts){
112  if(!ret.empty()) ret += ", ";
113  ret += it.first->LatexName() + TString::Format(" = %+g", it.second).Data();
114  }
115 
116  return ret;
117  }
118 
119  //----------------------------------------------------------------------
120  std::vector<const ISyst*> SystShifts::ActiveSysts() const
121  {
122  std::vector<const ISyst*> ret;
123  for(auto it: fSysts) ret.push_back(it.first);
124  return ret;
125  }
126 
127  //----------------------------------------------------------------------
128  void SystShifts::SaveTo(TDirectory* dir) const
129  {
130  TDirectory* tmp = gDirectory;
131 
132  dir->cd();
133  TObjString("SystShifts").Write("type");
134 
135  // Don't write any histogram for the nominal case
136  if(!fSysts.empty()){
137  TH1D h("", "", fSysts.size(), 0, fSysts.size());
138  int ibin = 0;
139  for(auto it: fSysts){
140  ++ibin;
141  h.GetXaxis()->SetBinLabel(ibin, it.first->ShortName().c_str());
142  h.SetBinContent(ibin, it.second);
143  }
144  h.Write("vals");
145  }
146 
147  tmp->cd();
148  }
149 
150  //----------------------------------------------------------------------
151  std::unique_ptr<SystShifts> SystShifts::LoadFrom(TDirectory* dir)
152  {
153  TObjString* tag = (TObjString*)dir->Get("type");
154  assert(tag);
155  assert(tag->GetString() == "SystShifts");
156 
157  auto ret = std::make_unique<SystShifts>();
158 
159  TH1* h = (TH1*)dir->Get("vals");
160  if(h){ // no histogram means nominal
161  for(int i = 1; i <= h->GetNbinsX(); ++i){
162  ret->SetShift(SystRegistry::ShortNameToSyst(h->GetXaxis()->GetBinLabel(i)),
163  h->GetBinContent(i));
164  }
165  }
166 
167  return ret;
168  }
169 }
std::unordered_map< const ISyst *, double > fSysts
Definition: SystShifts.h:54
static int fgNextID
The next unused ID.
Definition: SystShifts.h:58
std::string ShortName() const
Brief description of component shifts, for printing to screen.
Definition: SystShifts.cxx:92
void SetShift(const ISyst *syst, double shift)
Shifts are 0=nominal, -1,+1 = 1 sigma shifts.
Definition: SystShifts.cxx:47
Simple record of shifts applied to systematic parameters.
Definition: SystShifts.h:16
static std::unique_ptr< SystShifts > LoadFrom(TDirectory *dir)
Definition: SystShifts.cxx:151
process_name opflashCryoW ana
shift
Definition: fcl_checks.sh:26
T sqr(T x)
More efficient square function than pow(x,2)
Definition: MathUtil.h:23
Encapsulate code to systematically shift a caf::StandardRecord.
Definition: ISyst.h:14
caf::Proxy< caf::SRSlice > SRSliceProxy
Definition: EpilogFwd.h:2
void Shift(caf::SRSliceProxy *slc, double &weight) const
Definition: SystShifts.cxx:86
static SystShifts RandomThrow(const std::vector< const ISyst * > &systs)
Definition: SystShifts.cxx:39
while getopts h
std::string LatexName() const
Long description of component shifts, for plot labels.
Definition: SystShifts.cxx:106
double GetShift(const ISyst *syst) const
Definition: SystShifts.cxx:56
tuple dir
Definition: dropbox.py:28
static const ISyst * ShortNameToSyst(const std::string &s, bool allowFail=false)
then echo File list $list not found else cat $list while read file do echo $file sed s
Definition: file_to_url.sh:60
void ResetToNominal()
Definition: SystShifts.cxx:65
void SaveTo(TDirectory *dir) const
Definition: SystShifts.cxx:128
std::vector< const ISyst * > ActiveSysts() const
Definition: SystShifts.cxx:120
double Penalty() const
Penalty term for chi-squared fits.
Definition: SystShifts.cxx:73