Hist.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "CAFAna/Core/StanVar.h"
4 
5 // These numeric limits need to be included before we actually instantiate any
6 // eigen+stan objects.
7 #pragma GCC diagnostic push
8 #pragma GCC diagnostic ignored "-Wunused-function"
11 #pragma GCC diagnostic pop
12 
13 class TDirectory;
14 
15 class TH1D;
16 
17 #include <Eigen/Dense>
18 #include <Eigen/SparseCore>
19 
20 namespace Eigen{
21  using ArrayXstan = Eigen::Array<stan::math::var, Eigen::Dynamic, 1>;
22  using VectorXstan = Eigen::Matrix<stan::math::var, Eigen::Dynamic, 1>;
23 }
24 
25 namespace ana
26 {
27  class Binning;
28 
29  class Hist
30  {
31  public:
32  Hist(const Hist&);
33  Hist(Hist&&);
34  Hist& operator=(const Hist&);
35  Hist& operator=(Hist&&);
36  ~Hist();
37 
38  static Hist Uninitialized(){return Hist();}
39  bool Initialized() const {return fType != kUninitialized;}
40 
41  static Hist Zero(int nbins);
42  static Hist ZeroSparse(int nbins);
43 
44  static Hist AdoptSparse(Eigen::SparseVector<double>&& v);
45  static Hist AdoptStan(Eigen::ArrayXstan&& v);
46  static Hist Adopt(Eigen::ArrayXd&& v);
47 
48  static Hist FromDirectory(TDirectory* dir);
49 
50  TH1D* ToTH1(const Binning& bins) const;
51 
52  bool HasStan() const {return fType == kDenseStan;}
53  const Eigen::ArrayXd& GetEigen() const {assert(fType == kDense); return fData;}
54  const Eigen::ArrayXstan& GetEigenStan() const {assert(fType == kDenseStan); return fDataStan;}
55 
56  int GetNbinsX() const;
57  double GetBinError(int i) const;
58  double Integral() const;
59 
60  void Fill(const Binning& bins, double x, double w);
61  void Scale(double s);
62  void Scale(const stan::math::var& s);
63  void ResetErrors();
64 
65  double GetBinContent(int i) const;
66  void SetBinContent(int i, double x);
67  void Reset();
68 
69  void Add(const Hist& rhs, double scale = 1);
70 
71  void Multiply(const Hist& rhs);
72  void Divide(const Hist& rhs);
73 
74  void Write(const Binning& bins) const;
75  protected:
76  Hist();
77 
78  // Helpers for the public Add() function
79  void Add(const Eigen::SparseVector<double>& rhs, double scale);
80  void Add(const Eigen::ArrayXstan& rhs, double scale);
81  void Add(const Eigen::ArrayXd& rhs, double scale);
82 
83  enum EType{kUninitialized, kDense, kDenseStan, kSparse};
85 
86  Eigen::SparseVector<double> fDataSparse;
88  Eigen::ArrayXd fData;
89  Eigen::ArrayXd fSumSq; ///< Accumulate errors, if enabled
90  bool fSqrtErrs; ///< Special case when filled with unweighted data
91  };
92 }
Eigen::ArrayXd fSumSq
Accumulate errors, if enabled.
Definition: Hist.h:89
ratio_hxv Divide(hxv, goal_hxv)
const Eigen::ArrayXd & GetEigen() const
Definition: Hist.h:53
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
double Integral(const Spectrum &s, const double pot, int cvnregion)
Eigen::Matrix< stan::math::var, Eigen::Dynamic, 1 > VectorXstan
Definition: Hist.h:22
Definition: Hist.h:29
Definition: StanUtils.h:6
Eigen::SparseVector< double > fDataSparse
Definition: Hist.h:86
EType
Definition: Hist.h:83
bool fSqrtErrs
Special case when filled with unweighted data.
Definition: Hist.h:90
const XML_Char * s
Definition: expat.h:262
bool Initialized() const
Definition: Hist.h:39
const int nbins
Definition: cellShifts.C:15
Double_t scale
Definition: plot.C:25
correl_yv Fill(-(dy[iP-1][iC-1]), hyv->GetBinContent(iP, iC))
void Reset()
const Binning bins
Eigen::ArrayXstan fDataStan
Definition: Hist.h:87
cout<< "--"<< endl;for(Int_t iP=1;iP<=hyz->GetNbinsX();iP++){for(Int_t iC=1;iC<=hyz->GetNbinsY();iC++){if(hyv->GetBinContent(iP, iC)>-999){goal_hyv-> SetBinContent(iP, iC,-(dy[iP-1][iC-1]))
Eigen::ArrayXd fData
Definition: Hist.h:88
EType fType
Definition: Hist.h:84
Eigen::Array< stan::math::var, Eigen::Dynamic, 1 > ArrayXstan
Definition: StanUtils.h:7
TDirectory * dir
Definition: macro.C:5
void Zero()
assert(nhit_max >=nhit_nbins)
Float_t w
Definition: plot.C:20
simulatedPeak Scale(1/simulationNormalisationFactor)
static Hist Uninitialized()
Definition: Hist.h:38
bool HasStan() const
Definition: Hist.h:52
const Eigen::ArrayXstan & GetEigenStan() const
Definition: Hist.h:54
gm Write()