ReweightableSpectrum.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "CAFAna/Core/Spectrum.h"
4 
5 #include <string>
6 
7 class TDirectory;
8 class TH2;
9 class TH2D;
10 
11 namespace ana
12 {
13  template<class T> class _Var;
14  template<class T> class _Cut;
15 
16  template<class T> _Var<T> Unweighted();
17 
18  /// %Spectrum with the value of a second variable, allowing for reweighting
20  {
21  public:
24 
25  template<class T>
27  const _HistAxis<_Var<T>>& recoAxis,
28  const _HistAxis<_Var<T>>& trueAxis,
29  const _Cut<T>& cut,
30  const SystShifts& shift = kNoShift,
31  const _Var<T>& wei = Unweighted<T>());
32 
33  ReweightableSpectrum(const Eigen::MatrixXd&& mat,
34  const LabelsAndBins& recoAxis,
35  const LabelsAndBins& trueAxis,
36  double pot, double livetime);
37 
38  /// The only valid thing to do with such a spectrum is to assign something
39  /// else into it.
41 
42  virtual ~ReweightableSpectrum();
43 
46 
47  void Fill(double x, double y, double w = 1);
48 
49  TH2D* ToTH2(double pot) const;
50 
51  double POT() const{return fPOT;}
52  double Livetime() const{return fLivetime;}
53 
54  Spectrum UnWeighted() const;
55 
57 
58  /// Reco spectrum with truth weights applied
59  Spectrum WeightedBy(const Ratio& weights) const;
60 
61  /// Rescale bins so that \ref WeightingVariable will return \a target
63  /// Recale bins so that \ref Unweighted will return \a target
64  void ReweightToRecoSpectrum(const Spectrum& target);
65 
66  // Arithmetic operators are as if these are unlike samples, each a
67  // contribution to one total, not seperate sources of stats for the same
68  // sample.
71 
74 
75  void Clear();
76 
77  void SaveTo(TDirectory* dir, const std::string& name) const;
78 
79  static std::unique_ptr<ReweightableSpectrum> LoadFrom(TDirectory* dir, const std::string& name);
80 
81  unsigned int NDimensions() const{return fAxisX.NDimensions();}
82  const std::vector<std::string>& GetLabels() const {return fAxisX.GetLabels();}
83  const std::vector<Binning>& GetBinnings() const {return fAxisX.GetBinnings();}
84  const std::vector<Binning>& GetTrueBinnings() const {return fAxisY.GetBinnings();}
85 
86  Eigen::MatrixXd GetEigen(double pot) const {return fMat * pot/fPOT;}
87  protected:
88  // Derived classes can be trusted take care of their own construction
90  const LabelsAndBins& axisY)
91  : fPOT(0), fLivetime(0),
92  fAxisX(axisX), fAxisY(axisY)
93  {
94  }
95 
96  // Constructor for user by Uninitialized()
98  : fPOT(0), fLivetime(0),
99  fAxisX(std::vector<std::string>(), std::vector<Binning>()),
100  fAxisY(std::vector<std::string>(), std::vector<Binning>())
101  {
102  }
103 
105 
108 
109  void _SaveTo(TDirectory* dir,
110  const std::string& name,
111  const std::string& type) const;
112 
113  Eigen::MatrixXd fMat;
114  double fPOT;
115  double fLivetime;
116 
118 
119  /// Things that point at this ReweightableSpectrum. Maintained by
120  /// SpectrumLoader
121  std::set<ReweightableSpectrum**> fReferences;
122  };
123 }
const XML_Char * name
Definition: expat.h:151
static ReweightableSpectrum Uninitialized()
ReweightableSpectrum & operator+=(const ReweightableSpectrum &rhs)
Represent the binning of a Spectrum&#39;s x-axis.
Definition: Binning.h:16
Cuts and Vars for the 2020 FD DiF Study.
Definition: vars.h:6
const XML_Char * target
Definition: expat.h:268
ReweightableSpectrum(const LabelsAndBins &axisX, const LabelsAndBins &axisY)
void Fill(double x, double y, double w=1)
Simple record of shifts applied to systematic parameters.
Definition: SystShifts.h:20
Spectrum with the value of a second variable, allowing for reweighting
static std::unique_ptr< ReweightableSpectrum > LoadFrom(TDirectory *dir, const std::string &name)
void ReweightToRecoSpectrum(const Spectrum &target)
Recale bins so that Unweighted will return target.
ReweightableSpectrum operator-(const ReweightableSpectrum &rhs) const
Representation of a spectrum in any variable, with associated POT.
Definition: Spectrum.h:40
const std::vector< std::string > & GetLabels() const
void RemoveLoader(ReweightableSpectrum **)
ReweightableSpectrum & operator-=(const ReweightableSpectrum &rhs)
Spectrum WeightedBy(const Ratio &weights) const
Reco spectrum with truth weights applied.
#define pot
Var weights
const std::vector< Binning > & GetBinnings() const
loader
Definition: demo0.py:10
Represent the ratio between two spectra.
Definition: Ratio.h:8
const SystShifts kNoShift
Definition: SystShifts.cxx:21
ReweightableSpectrum operator+(const ReweightableSpectrum &rhs) const
Base class for the various types of spectrum loader.
const std::vector< Binning > & GetBinnings() const
Definition: LabelsAndBins.h:69
std::set< ReweightableSpectrum ** > fReferences
const Cut cut
Definition: exporter_fd.C:30
unsigned int NDimensions() const
Definition: LabelsAndBins.h:66
void _SaveTo(TDirectory *dir, const std::string &name, const std::string &type) const
ReweightableSpectrum & PlusEqualsHelper(const ReweightableSpectrum &rhs, int sign)
ReweightableSpectrum & operator=(const ReweightableSpectrum &rhs)
Collect information describing the x-axis of an analysis histogram.
Definition: HistAxis.h:18
TDirectory * dir
Definition: macro.C:5
double livetime
Definition: saveFDMCHists.C:21
void SaveTo(TDirectory *dir, const std::string &name) const
const std::vector< Binning > & GetTrueBinnings() const
_Var< T > Unweighted()
Definition: Var.h:93
void AddLoader(ReweightableSpectrum **)
Eigen::MatrixXd GetEigen(double pot) const
void ReweightToTrueSpectrum(const Spectrum &target)
Rescale bins so that WeightingVariable will return target.
Template for Cut and SpillCut.
Definition: Cut.h:15
Template for Var and SpillVar.
Float_t w
Definition: plot.C:20
const std::vector< std::string > & GetLabels() const
Definition: LabelsAndBins.h:68
Eigen::MatrixXd mat
def sign(x)
Definition: canMan.py:197
unsigned int NDimensions() const
TH2D * ToTH2(double pot) const
enum BeamMode string