All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
EnsembleSpectrum.cxx
Go to the documentation of this file.
2 
4 
5 #include "TDirectory.h"
6 #include "TGraphAsymmErrors.h"
7 #include "TH1.h"
8 #include "TObjString.h"
9 #include "TPad.h"
10 
11 namespace ana
12 {
13  //----------------------------------------------------------------------
15  const HistAxis& axis,
16  const SpillCut& spillcut,
17  const Cut& cut,
18  const std::vector<SystShifts>& univ_shifts,
19  const Var& cv_wei)
20  : fNom(loader, axis, spillcut, cut, kNoShift, cv_wei)
21  {
22  fUnivs.reserve(univ_shifts.size());
23  for(const SystShifts& ss: univ_shifts){
24  fUnivs.emplace_back(loader, axis, spillcut, cut, ss, cv_wei);
25  }
26  }
27 
28  //----------------------------------------------------------------------
30  const HistAxis& axis,
31  const SpillCut& spillcut,
32  const Cut& cut,
33  const std::vector<Var>& univ_weis,
34  const Var& cv_wei)
35  : fNom(loader, axis, spillcut, cut, kNoShift, cv_wei)
36  {
37  fUnivs.reserve(univ_weis.size());
38  for(const Var& w: univ_weis){
39  fUnivs.emplace_back(loader, axis, spillcut, cut, kNoShift, cv_wei * w);
40  }
41  }
42 
43  //----------------------------------------------------------------------
44  TGraphAsymmErrors* EnsembleSpectrum::ErrorBand(double exposure,
45  EExposureType expotype,
46  EBinType bintype) const
47  {
48  std::unique_ptr<TH1D> hnom(fNom.ToTH1(exposure, expotype, bintype));
49 
50  std::vector<std::unique_ptr<TH1D>> hunivs;
51  hunivs.reserve(fUnivs.size());
52  for(const Spectrum& u: fUnivs) hunivs.emplace_back(u.ToTH1(exposure, expotype, bintype));
53 
54  TGraphAsymmErrors* g = new TGraphAsymmErrors;
55 
56  for(int binIdx = 0; binIdx < hnom->GetNbinsX()+2; ++binIdx){
57  const double xnom = hnom->GetXaxis()->GetBinCenter(binIdx);
58  const double ynom = hnom->GetBinContent(binIdx);
59  g->SetPoint(binIdx, xnom, ynom);
60 
61  const double dx = hnom->GetXaxis()->GetBinWidth(binIdx);
62 
63  std::vector<double> ys;
64  ys.reserve(hunivs.size());
65  for(const std::unique_ptr<TH1D>& hu: hunivs){
66  ys.push_back(hu->GetBinContent(binIdx));
67  }
68 
69  // 1 sigma
70  const double y0 = FindQuantile(.5-0.6827/2, ys);
71  const double y1 = FindQuantile(.5+0.6827/2, ys);
72 
73  // It's theoretically possible for the central value to be outside the
74  // error bands - clamp to zero in that case
75  g->SetPointError(binIdx, dx/2, dx/2,
76  std::max(ynom-y0, 0.),
77  std::max(y1-ynom, 0.));
78  } // end for binIdx
79 
80  return g;
81  }
82 
83  //----------------------------------------------------------------------
84  void EnsembleSpectrum::Scale(double c)
85  {
86  fNom.Scale(c);
87  for(Spectrum& s: fUnivs) s.Scale(c);
88  }
89 
90  //----------------------------------------------------------------------
92  {
93  fNom += rhs.fNom;
94  assert(fUnivs.size() == rhs.fUnivs.size());
95  for(unsigned int i = 0; i < fUnivs.size(); ++i) fUnivs[i] += rhs.fUnivs[i];
96  return *this;
97  }
98 
99  //----------------------------------------------------------------------
101  {
102  EnsembleSpectrum ret = *this;
103  ret += rhs;
104  return ret;
105  }
106 
107  //----------------------------------------------------------------------
109  {
110  fNom -= rhs.fNom;
111  assert(fUnivs.size() == rhs.fUnivs.size());
112  for(unsigned int i = 0; i < fUnivs.size(); ++i) fUnivs[i] -= rhs.fUnivs[i];
113  return *this;
114  }
115 
116  //----------------------------------------------------------------------
118  {
119  EnsembleSpectrum ret = *this;
120  ret -= rhs;
121  return ret;
122  }
123 
124  //----------------------------------------------------------------------
126  {
127  fNom *= rhs.Nominal();
128  assert(rhs.NUniverses() == fUnivs.size());
129  for(unsigned int i = 0; i < fUnivs.size(); ++i) fUnivs[i] *= rhs.Universe(i);
130  return *this;
131  }
132 
133  //----------------------------------------------------------------------
135  {
136  EnsembleSpectrum ret = *this;
137  ret *= rhs;
138  return ret;
139  }
140 
141  //----------------------------------------------------------------------
143  {
144  fNom /= rhs.Nominal();
145  assert(rhs.NUniverses() == fUnivs.size());
146  for(unsigned int i = 0; i < fUnivs.size(); ++i) fUnivs[i] /= rhs.Universe(i);
147  return *this;
148  }
149 
150  //----------------------------------------------------------------------
152  {
153  EnsembleSpectrum ret = *this;
154  ret /= rhs;
155  return ret;
156  }
157 
158  //----------------------------------------------------------------------
159  void EnsembleSpectrum::SaveTo(TDirectory* dir) const
160  {
161  TDirectory* tmp = gDirectory;
162  dir->cd();
163 
164  TObjString("EnsembleSpectrum").Write("type");
165 
166  fNom.SaveTo(dir->mkdir("nom"));
167  for(unsigned int i = 0; i < fUnivs.size(); ++i){
168  fUnivs[i].SaveTo(dir->mkdir(("univ"+std::to_string(i)).c_str()));
169  }
170 
171  tmp->cd();
172  }
173 
174  //----------------------------------------------------------------------
175  std::unique_ptr<EnsembleSpectrum> EnsembleSpectrum::LoadFrom(TDirectory* dir)
176  {
177  DontAddDirectory guard;
178 
179  TObjString* tag = (TObjString*)dir->Get("type");
180  assert(tag);
181  assert(tag->GetString() == "EnsembleSpectrum");
182  delete tag;
183 
184  std::unique_ptr<EnsembleSpectrum> ret(new EnsembleSpectrum(*Spectrum::LoadFrom(dir->GetDirectory("nom"))));
185 
186  for(unsigned int i = 0; ; ++i){
187  TDirectory* d = dir->GetDirectory(("univ"+std::to_string(i)).c_str());
188  if(!d) break;
189  ret->fUnivs.push_back(*Spectrum::LoadFrom(d));
190  }
191 
192  return ret;
193  }
194 
195  //----------------------------------------------------------------------
196  void DrawErrorBand(TH1* nom, TGraphAsymmErrors* band, int bandCol, double alpha)
197  {
198  if(bandCol == -1) bandCol = nom->GetLineColor()-10; // hopefully a lighter version
199 
200  // Check if this pad has already been drawn in
201  const bool same = gPad && !gPad->GetListOfPrimitives()->IsEmpty();
202 
203  nom->Draw(same ? "hist same" : "hist");
204 
205  band->SetFillColorAlpha(bandCol, alpha);
206  band->Draw("e2 same");
207 
208  nom->Draw("hist same");
209 
210  // If we are the first spectrum to draw, scale the y-axis appropriately to
211  // fit the error band as well as the nominal
212  if(!same){
213  double maxY = 0;
214  // Don't consider underflow or overflow bins when determining maximum
215  for(int i = 1; i < band->GetN()-1; ++i){
216  maxY = std::max(maxY, band->GetY()[i] + band->GetErrorYhigh(i));
217  }
218 
219  // Use non-zero lower value so that SetLogy() still works
220  nom->GetYaxis()->SetRangeUser(1e-10, 1.1 * maxY);
221  }
222 
223  gPad->RedrawAxis();
224  }
225 }
TH1D * ToTH1(double exposure, Color_t col=kBlack, Style_t style=kSolid, EExposureType expotype=kPOT, EBinType bintype=kBinContent) const
Histogram made from this Spectrum, scaled to some exposure.
static std::unique_ptr< Spectrum > LoadFrom(TDirectory *dir)
EnsembleSpectrum operator-(const EnsembleSpectrum &rhs) const
EnsembleSpectrum operator+(const EnsembleSpectrum &rhs) const
EnsembleSpectrum(SpectrumLoaderBase &loader, const HistAxis &axis, const SpillCut &spillcut, const Cut &cut, const std::vector< SystShifts > &univ_shifts, const Var &cv_wei=kUnweighted)
tuple loader
Definition: demo.py:7
Simple record of shifts applied to systematic parameters.
Definition: SystShifts.h:16
static std::unique_ptr< EnsembleSpectrum > LoadFrom(TDirectory *dir)
double FindQuantile(double frac, std::vector< double > &xs)
EnsembleSpectrum & operator+=(const EnsembleSpectrum &rhs)
process_name opflashCryoW ana
BEGIN_PROLOG g
void Scale(double c)
Multiply this spectrum by a constant c.
Representation of a spectrum in any variable, with associated POT.
Definition: Spectrum.h:30
EnsembleSpectrum & operator*=(const EnsembleRatio &rhs)
void SaveTo(TDirectory *dir) const
EnsembleSpectrum & operator/=(const EnsembleRatio &rhs)
EExposureType
For use as an argument to Spectrum::ToTH1.
Ratio Universe(unsigned int i) const
Definition: EnsembleRatio.h:20
EnsembleSpectrum operator/(const EnsembleRatio &rhs) const
Ratio Nominal() const
Definition: EnsembleRatio.h:18
const SystShifts kNoShift
Definition: SystShifts.h:61
tuple dir
Definition: dropbox.py:28
unsigned int NUniverses() const
Definition: EnsembleRatio.h:19
Base class for the various types of spectrum loader.
std::vector< Spectrum > fUnivs
TGraphAsymmErrors * ErrorBand(double exposure, EExposureType expotype=kPOT, EBinType bintype=kBinContent) const
Result can be painted prettily with DrawErrorBand.
EnsembleSpectrum & operator-=(const EnsembleSpectrum &rhs)
std::string to_string(WindowPattern const &pattern)
then echo File list $list not found else cat $list while read file do echo $file sed s
Definition: file_to_url.sh:60
do i e
Prevent histograms being added to the current directory.
EnsembleSpectrum operator*(const EnsembleRatio &rhs) const
void DrawErrorBand(TH1 *nom, TGraphAsymmErrors *band, int bandCol, double alpha)
void SaveTo(TDirectory *dir) const