Ratio.cxx
Go to the documentation of this file.
1 #include "CAFAna/Core/Ratio.h"
2 
3 #include "TH2.h"
4 
5 #include <cassert>
6 #include <iostream>
7 
8 namespace ana
9 {
10  //----------------------------------------------------------------------
11  Ratio::Ratio(const Spectrum& num, const Spectrum& denom,
12  bool purOrEffErrs)
13  : fHist(num.fHist), fBins(num.GetBinnings()), fLabels(num.GetLabels())
14  {
15  fHist.Divide(denom.fHist);
16  fHist.Scale(denom.POT()/num.POT());
17 
18  // This is clumsy, but the old histogram operation considered 0/0 = 0,
19  // which is actually pretty useful (at least PredictionInterp relies on
20  // this).
21  for(int i = 0; i < fHist.GetNbinsX()+2; ++i){
22  if(num.fHist.GetBinContent(i) == 0 &&
23  denom.fHist.GetBinContent(i) == 0){
24  fHist.SetBinContent(i, 0);
25  }
26  }
27 
28  // TODO do something with purOrEffErrs
29  }
30 
31  //----------------------------------------------------------------------
33  {
34  }
35 
36  //----------------------------------------------------------------------
38  {
39  fHist.Multiply(rhs.fHist);
40  return *this;
41  }
42 
43  //----------------------------------------------------------------------
44  Ratio Ratio::operator*(const Ratio& rhs) const
45  {
46  Ratio ret = *this;
47  ret *= rhs;
48  return ret;
49  }
50 
51  //----------------------------------------------------------------------
53  {
54  fHist.Divide(rhs.fHist);
55  return *this;
56  }
57 
58  //----------------------------------------------------------------------
59  Ratio Ratio::operator/(const Ratio& rhs) const
60  {
61  Ratio ret = *this;
62  ret /= rhs;
63  return ret;
64  }
65 
66  //----------------------------------------------------------------------
67  TH1D* Ratio::ToTH1(Color_t col, Style_t style) const
68  {
69  TH1D* ret = fHist.ToTH1(fBins[0]); // TODO
70  ret->SetLineColor(col);
71  ret->SetLineStyle(style);
72  ret->GetXaxis()->SetTitle(fLabels[0].c_str()); // TODO
73  ret->GetYaxis()->SetTitle("Ratio");
74  return ret;
75  }
76 
77  //----------------------------------------------------------------------
78  TH2* Ratio::ToTH2() const
79  {
80  if(fBins.size() != 2){
81  std::cout << "Error: This Ratio does not appear to be 2D." << std::endl;
82  abort();
83  }
84 
85  TH2* ret = ana::ToTH2Helper(std::unique_ptr<TH1>(ToTH1()), fBins[0], fBins[1]);
86 
87  ret->GetXaxis()->SetTitle(fLabels[0].c_str());
88  ret->GetYaxis()->SetTitle(fLabels[1].c_str());
89 
90  // Allow GetMean() and friends to work even if this histogram never had any
91  // explicit Fill() calls made.
92  if(ret->GetEntries() == 0) ret->SetEntries(1);
93 
94  return ret;
95  }
96 } // namespace
void SetBinContent(int i, double x)
Definition: Hist.cxx:363
Cuts and Vars for the 2020 FD DiF Study.
Definition: vars.h:6
TH2 * ToTH2() const
Definition: Ratio.cxx:78
TH1D * ToTH1(Color_t col=kBlack, Style_t style=kSolid) const
Definition: Ratio.cxx:67
Ratio & operator*=(const Ratio &rhs)
Definition: Ratio.cxx:37
int GetNbinsX() const
Definition: Hist.cxx:228
TH1D * ToTH1(const Binning &bins) const
Definition: Hist.cxx:207
Ratio & operator/=(const Ratio &rhs)
Definition: Ratio.cxx:52
Representation of a spectrum in any variable, with associated POT.
Definition: Spectrum.h:40
Ratio operator*(const Ratio &rhs) const
Definition: Ratio.cxx:44
Ratio(const Spectrum &num, const Spectrum &denom, bool purOrEffErrs=false)
Definition: Ratio.cxx:11
Int_t col[ntarg]
Definition: Style.C:29
std::vector< std::string > fLabels
Definition: Ratio.h:58
void Scale(double s)
Definition: Hist.cxx:296
Ratio operator/(const Ratio &rhs) const
Definition: Ratio.cxx:59
double POT() const
Definition: Spectrum.h:227
std::vector< Binning > fBins
Definition: Ratio.h:57
Represent the ratio between two spectra.
Definition: Ratio.h:8
OStream cout
Definition: OStream.cxx:6
virtual ~Ratio()
Definition: Ratio.cxx:32
void Divide(const Hist &rhs)
Definition: Hist.cxx:531
Hist fHist
Definition: Ratio.h:56
int num
Definition: f2_nu.C:119
double GetBinContent(int i) const
Definition: Hist.cxx:349
TH2 * ToTH2Helper(std::unique_ptr< TH1 > h1, const Binning &binsx, const Binning &binsy, ana::EBinType bintype)
Helper for ana::ToTH2.
Definition: UtilsExt.cxx:135
void Multiply(const Hist &rhs)
Definition: Hist.cxx:479