All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Spectrum.h
Go to the documentation of this file.
1 #pragma once
2 
10 
11 #include "TAttLine.h"
12 #include "THnSparse.h"
13 
14 #include <memory>
15 #include <string>
16 #include <vector>
17 
18 class TDirectory;
19 class TH1;
20 class TH2;
21 class TH3;
22 class TH1D;
23 
24 /// Oscillation analysis framework, runs over CAF files outside of ART
25 namespace ana
26 {
27  class Ratio;
28 
29  /// Representation of a spectrum in any variable, with associated POT
30  class Spectrum
31  {
32  public:
33  friend class SpectrumLoaderBase;
34  friend class SpectrumLoader;
35  friend class NullLoader;
36 
38 
39  Spectrum(const std::string& label, const Binning& bins,
41  const Var& var,
42  const SpillCut& spillcut,
43  const Cut& cut,
44  const SystShifts& shift = kNoShift,
45  const Var& wei = kUnweighted);
46 
47  Spectrum(const std::string& label, const Binning& bins,
49  const Var& var,
50  const Cut& cut,
51  const SystShifts& shift = kNoShift,
52  const Var& wei = kUnweighted)
53  : Spectrum(label, bins, loader, var, kNoSpillCut, cut, shift, wei)
54  {
55  }
56 
57  Spectrum(const std::string& label, const Binning& bins,
59  const SpillVar& var,
60  const SpillCut& cut,
61  const SpillVar& wei = kSpillUnweighted);
62 
63  /// The only \ref MultiVar variant available
64  Spectrum(const std::string& label, const Binning& bins,
66  const MultiVar& var,
67  const SpillCut& spillcut,
68  const Cut& cut,
69  const SystShifts& shift = kNoShift,
70  const Var& wei = kUnweighted);
71 
72  // And the only SpillMultiVar
73  Spectrum(const std::string& label, const Binning& bins,
75  const SpillMultiVar& var,
76  const SpillCut& cut,
77  const SpillVar& wei = kSpillUnweighted);
78 
80  const HistAxis& axis,
81  const SpillCut& spillcut,
82  const Cut& cut,
83  const SystShifts& shift = kNoShift,
84  const Var& wei = kUnweighted);
85 
87  const HistAxis& axis,
88  const Cut& cut,
89  const SystShifts& shift = kNoShift,
90  const Var& wei = kUnweighted)
91  : Spectrum(loader, axis, kNoSpillCut, cut, shift, wei)
92  {
93  }
94 
95  Spectrum(const std::string& label, const Binning& bins, ESparse sparse = kDense);
96  Spectrum(const std::string& label, double pot, double livetime, const Binning& bins);
97 
98  /// Copies \a h
99  Spectrum(TH1* h,
100  const std::vector<std::string>& labels,
101  const std::vector<Binning>& bins,
102  double pot, double livetime);
103 
104  /// Takes possession of \a h
105  Spectrum(std::unique_ptr<TH1D> h,
106  const std::vector<std::string>& labels,
107  const std::vector<Binning>& bins,
108  double pot, double livetime);
109 
110  /// 2D Spectrum of two Vars
111  Spectrum(const std::string& label, SpectrumLoaderBase& loader,
112  const Binning& binsx, const Var& varx,
113  const Binning& binsy, const Var& vary,
114  const SpillCut& spillcut,
115  const Cut& cut,
116  const SystShifts& shift = kNoShift,
117  const Var& wei = kUnweighted);
118 
119  /// 2D Spectrum of two SpillVars
120  Spectrum(const std::string& label, SpectrumLoaderBase& loader,
121  const Binning& binsx, const SpillVar& varx,
122  const Binning& binsy, const SpillVar& vary,
123  const SpillCut& spillcut,
124  const SpillVar& wei = kSpillUnweighted);
125 
126  /// 2D Spectrum of two MultiVars
127  Spectrum(const std::string& label, SpectrumLoaderBase& loader,
128  const Binning& binsx, const MultiVar& varx,
129  const Binning& binsy, const MultiVar& vary,
130  const SpillCut& spillcut,
131  const Cut& cut,
132  const SystShifts& shift = kNoShift,
133  const Var& wei = kUnweighted);
134 
135  /// 2D Spectrum of two SpillMultiVars
136  Spectrum(const std::string& label, SpectrumLoaderBase& loader,
137  const Binning& binsx, const SpillMultiVar& varx,
138  const Binning& binsy, const SpillMultiVar& vary,
139  const SpillCut& spillcut,
140  const SpillVar& wei = kSpillUnweighted);
141 
142  /// 2D Spectrum taking 2 HistAxis
144  const HistAxis& xAxis,
145  const HistAxis& yAxis,
146  const SpillCut& spillcut,
147  const Cut& cut,
148  const SystShifts& shift = kNoShift,
149  const Var& wei = kUnweighted);
150 
151  Spectrum(const std::string& xLabel,
152  const std::string& yLabel,
154  const Binning& binsx, const Var& varx,
155  const Binning& binsy, const Var& vary,
156  const SpillCut& spillcut,
157  const Cut& cut,
158  const SystShifts& shift = kNoShift,
159  const Var& wei = kUnweighted);
160 
161  Spectrum(const std::string& xLabel,
162  const std::string& yLabel,
164  const Binning& binsx, const SpillVar& varx,
165  const Binning& binsy, const SpillVar& vary,
166  const SpillCut& spillcut,
167  const SpillVar& wei = kSpillUnweighted);
168 
169  Spectrum(const std::string& xLabel,
170  const std::string& yLabel,
172  const Binning& binsx, const MultiVar& varx,
173  const Binning& binsy, const MultiVar& vary,
174  const SpillCut& spillcut,
175  const Cut& cut,
176  const SystShifts& shift = kNoShift,
177  const Var& wei = kUnweighted);
178 
179  Spectrum(const std::string& xLabel,
180  const std::string& yLabel,
182  const Binning& binsx, const SpillMultiVar& varx,
183  const Binning& binsy, const SpillMultiVar& vary,
184  const SpillCut& spillcut,
185  const SpillVar& wei = kSpillUnweighted);
186 
187  /// 3D Spectrum of three Vars
188  Spectrum(const std::string& label, SpectrumLoaderBase& loader,
189  const Binning& binsx, const Var& varx,
190  const Binning& binsy, const Var& vary,
191  const Binning& binsz, const Var& varz,
192  const SpillCut& spillcut,
193  const Cut& cut,
194  const SystShifts& shift = kNoShift,
195  const Var& wei = kUnweighted,
196  ESparse sparse = kDense);
197 
198  Spectrum(const std::string& xLabel,
199  const std::string& yLabel,
200  const std::string& zLabel,
202  const Binning& binsx, const Var& varx,
203  const Binning& binsy, const Var& vary,
204  const Binning& binsz, const Var& varz,
205  const SpillCut& spillcut,
206  const Cut& cut,
207  const SystShifts& shift = kNoShift,
208  const Var& wei = kUnweighted,
209  ESparse sparse = kDense);
210 
211  /// 3D Spectrum taking 3 HistAxis
213  const HistAxis& xAxis,
214  const HistAxis& yAxis,
215  const HistAxis& zAxis,
216  const SpillCut& spillcut,
217  const Cut& cut,
218  const SystShifts& shift = kNoShift,
219  const Var& wei = kUnweighted,
220  ESparse sparse = kDense);
221 
222 
223  virtual ~Spectrum();
224 
225  Spectrum(const Spectrum& rhs);
226  Spectrum(Spectrum&& rhs);
227  Spectrum& operator=(const Spectrum& rhs);
228  Spectrum& operator=(Spectrum&& rhs);
229 
230  void Fill(double x, double w = 1);
231 
232  /// \brief Histogram made from this Spectrum, scaled to some exposure
233  ///
234  /// \param exposure POT or livetime (seconds)
235  /// \param col Histogram color (default black)
236  /// \param style Histogram line style (default solid)
237  /// \param expotype How to interpret exposure (kPOT (default) or kLivetime)
238  TH1D* ToTH1(double exposure,
239  Color_t col = kBlack,
240  Style_t style = kSolid,
241  EExposureType expotype = kPOT,
242  EBinType bintype = kBinContent) const;
243 
244  /// \brief Histogram made from this Spectrum, scaled to some exposure
245  ///
246  /// \param exposure POT or livetime (seconds)
247  /// \param expotype How to interpret exposure (kPOT (default) or kLivetime)
248  TH1D* ToTH1(double exposure,
249  EExposureType expotype,
250  EBinType bintype = kBinContent) const
251  {
252  return ToTH1(exposure, kBlack, kSolid, expotype, bintype);
253  }
254 
255  /// Spectrum must be 2D to obtain TH2
256  TH2* ToTH2 (double exposure, EExposureType expotype = kPOT,
257  EBinType bintype = kBinContent) const;
258  /// Spectrum must be 2D to obtain TH2. Normalized to X axis.
259  TH2* ToTH2NormX(double exposure, EExposureType expotype = kPOT) const;
260 
261  /// Spectrum must be 3D to obtain TH3
262  TH3* ToTH3 (double exposure, EExposureType expotype = kPOT,
263  EBinType bintype = kBinContent) const;
264 
265  /// Function decides what is the appropriate projection based on fBins, and does that
266  TH1* ToTHX (double exposure, bool force1D = false, EExposureType expotype = kPOT) const;
267 
268  /// \brief Return total number of events scaled to \a pot
269  ///
270  /// \param exposure POT/livetime to scale to
271  /// \param err The statistical error on this total (optional)
272  /// \param expotype What the first parameter represents
273  double Integral(double exposure, double* err = 0,
274  EExposureType expotype = kPOT) const;
275 
276  /// \brief Return mean of 1D histogram
277  double Mean() const;
278 
279  /// \brief Mock data is \ref FakeData with Poisson fluctuations applied
280  ///
281  /// Use for low-budget MDCs, or just getting a sense of the expected scale
282  /// of statistical variation
283  Spectrum MockData(double pot, bool makethrow=true, int seed=0) const;
284  /// \brief Fake data is a MC spectrum scaled to the POT expected in the data
285  ///
286  /// Use for sensitivity plots and testing fit convergence
287  Spectrum FakeData(double pot) const;
288 
289  double POT() const {return fPOT;}
290 
291  /// Seconds. For informational purposes only. No calculations use this.
292  double Livetime() const {return fLivetime;}
293 
294  /// DO NOT USE UNLESS YOU ARE 110% CERTAIN THERE ISN'T A BETTER WAY!
295  void OverridePOT(double newpot) {fPOT = newpot;}
296 
297  /// DO NOT USE UNLESS YOU ARE 110% CERTAIN THERE ISN'T A BETTER WAY!
298  void OverrideLivetime(double newlive) {fLivetime = newlive;}
299 
300  void Clear();
301 
302  /// Multiply this spectrum by a constant c
303  void Scale(double c);
304 
305  // Arithmetic operators are as if these are unlike samples, each a
306  // contribution to one total, not seperate sources of stats for the same
307  // sample.
308  Spectrum& operator+=(const Spectrum& rhs);
309  Spectrum operator+(const Spectrum& rhs) const;
310 
311  Spectrum& operator-=(const Spectrum& rhs);
312  Spectrum operator-(const Spectrum& rhs) const;
313 
314  Spectrum& operator*=(const Ratio& rhs);
315  Spectrum operator*(const Ratio& rhs) const;
316 
317  Spectrum& operator/=(const Ratio& rhs);
318  Spectrum operator/(const Ratio& rhs) const;
319 
320  void SaveTo(TDirectory* dir) const;
321  static std::unique_ptr<Spectrum> LoadFrom(TDirectory* dir);
322 
323  unsigned int NDimensions() const{return fLabels.size();}
324  std::vector<std::string> GetLabels() const {return fLabels;}
325  std::vector<Binning> GetBinnings() const {return fBins;}
326 
327  protected:
328  Spectrum(const std::vector<std::string>& labels,
329  const std::vector<Binning>& bins,
330  ESparse sparse = kDense);
331 
332  void ConstructHistogram(ESparse sparse = kDense);
333 
336 
337  /// Helper for operator+= and operator-=
338  Spectrum& PlusEqualsHelper(const Spectrum& rhs, int sign);
339 
340  TH1D* fHist;
341  THnSparseD* fHistSparse;
342  double fPOT;
343  double fLivetime;
344 
345  /// This count is maintained by SpectrumLoader, as a sanity check
346  std::set<SpectrumLoaderBase*> fLoaderCount;
347 
348  std::vector<std::string> fLabels;
349  std::vector<Binning> fBins;
350  };
351 
352  // Commutative
353  inline Spectrum operator*(const Ratio& lhs, const Spectrum& rhs){return rhs*lhs;}
354  inline Spectrum operator/(const Ratio& lhs, const Spectrum& rhs){return rhs/lhs;}
355 }
const SpillVar kSpillUnweighted([](const caf::SRSpillProxy *){return 1;})
Spectrum & operator-=(const Spectrum &rhs)
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.
* labels
Spectrum operator*(const Ratio &rhs) const
Represent the binning of a Spectrum&#39;s x-axis.
Definition: Binning.h:18
static std::unique_ptr< Spectrum > LoadFrom(TDirectory *dir)
process_name opflash particleana ie x
tuple loader
Definition: demo.py:7
Simple record of shifts applied to systematic parameters.
Definition: SystShifts.h:16
void OverridePOT(double newpot)
DO NOT USE UNLESS YOU ARE 110% CERTAIN THERE ISN&#39;T A BETTER WAY!
Definition: Spectrum.h:295
TH3 * ToTH3(double exposure, EExposureType expotype=kPOT, EBinType bintype=kBinContent) const
Spectrum must be 3D to obtain TH3.
EResult err(const char *call)
Spectrum & PlusEqualsHelper(const Spectrum &rhs, int sign)
Helper for operator+= and operator-=.
TH2 * ToTH2NormX(double exposure, EExposureType expotype=kPOT) const
Spectrum must be 2D to obtain TH2. Normalized to X axis.
void AddLoader(SpectrumLoaderBase *)
void ConstructHistogram(ESparse sparse=kDense)
double fPOT
Definition: Spectrum.h:342
void Fill(double x, double w=1)
def style
Definition: util.py:237
process_name opflashCryoW ana
double Integral(double exposure, double *err=0, EExposureType expotype=kPOT) const
Return total number of events scaled to pot.
shift
Definition: fcl_checks.sh:26
Spectrum & operator=(const Spectrum &rhs)
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
while getopts h
std::vector< std::string > fLabels
Definition: Spectrum.h:348
std::set< SpectrumLoaderBase * > fLoaderCount
This count is maintained by SpectrumLoader, as a sanity check.
Definition: Spectrum.h:346
Spectrum FakeData(double pot) const
Fake data is a MC spectrum scaled to the POT expected in the data.
Spectrum operator/(const Ratio &rhs) const
EExposureType
For use as an argument to Spectrum::ToTH1.
TH1 * ToTHX(double exposure, bool force1D=false, EExposureType expotype=kPOT) const
Function decides what is the appropriate projection based on fBins, and does that.
EnsembleSpectrum operator*(const EnsembleRatio &lhs, const EnsembleSpectrum &rhs)
Spectrum & operator+=(const Spectrum &rhs)
double fLivetime
Definition: Spectrum.h:343
unsigned int seed
const SpillCut kNoSpillCut([](const caf::SRSpillProxy *){return true;})
The simplest possible cut: pass everything, used as a default.
void OverrideLivetime(double newlive)
DO NOT USE UNLESS YOU ARE 110% CERTAIN THERE ISN&#39;T A BETTER WAY!
Definition: Spectrum.h:298
TH1D * fHist
Definition: Spectrum.h:340
unsigned int NDimensions() const
Definition: Spectrum.h:323
void RemoveLoader(SpectrumLoaderBase *)
double POT() const
Definition: Spectrum.h:289
EnsembleRatio operator/(const EnsembleSpectrum &lhs, const EnsembleSpectrum &rhs)
Definition: EnsembleRatio.h:39
THnSparseD * fHistSparse
Definition: Spectrum.h:341
Spectrum(SpectrumLoaderBase &loader, const HistAxis &axis, const Cut &cut, const SystShifts &shift=kNoShift, const Var &wei=kUnweighted)
Definition: Spectrum.h:86
Represent the ratio between two spectra.
Definition: Ratio.h:8
const SystShifts kNoShift
Definition: SystShifts.h:61
tuple dir
Definition: dropbox.py:28
Base class for the various types of spectrum loader.
int sign(double val)
Definition: UtilFunc.cxx:104
Spectrum(const std::string &label, const Binning &bins, SpectrumLoaderBase &loader, const Var &var, const SpillCut &spillcut, const Cut &cut, const SystShifts &shift=kNoShift, const Var &wei=kUnweighted)
Definition: Spectrum.cxx:43
Spectrum operator+(const Spectrum &rhs) const
Collaborates with Spectrum and OscillatableSpectrum to fill spectra from CAF files.
const Var kUnweighted([](const caf::SRSliceProxy *){return 1;})
The simplest possible Var, always 1. Used as a default weight.
TH2 * ToTH2(double exposure, EExposureType expotype=kPOT, EBinType bintype=kBinContent) const
Spectrum must be 2D to obtain TH2.
std::vector< Binning > fBins
Definition: Spectrum.h:349
Spectrum & operator/=(const Ratio &rhs)
Dummy loader that doesn&#39;t load any files.
TH1D * ToTH1(double exposure, EExposureType expotype, EBinType bintype=kBinContent) const
Histogram made from this Spectrum, scaled to some exposure.
Definition: Spectrum.h:248
std::vector< std::string > GetLabels() const
Definition: Spectrum.h:324
Spectrum MockData(double pot, bool makethrow=true, int seed=0) const
Mock data is FakeData with Poisson fluctuations applied.
virtual ~Spectrum()
Spectrum operator-(const Spectrum &rhs) const
double Livetime() const
Seconds. For informational purposes only. No calculations use this.
Definition: Spectrum.h:292
std::vector< Binning > GetBinnings() const
Definition: Spectrum.h:325
Spectrum & operator*=(const Ratio &rhs)
void SaveTo(TDirectory *dir) const
double Mean() const
Return mean of 1D histogram.
Spectrum(const std::string &label, const Binning &bins, SpectrumLoaderBase &loader, const Var &var, const Cut &cut, const SystShifts &shift=kNoShift, const Var &wei=kUnweighted)
Definition: Spectrum.h:47