DAS  3.0
Das Analysis System
Teddy.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <functional>
4 #include <algorithm>
5 #include <memory>
6 
7 #include <Eigen/Dense>
8 
9 #include <TRandom3.h>
10 #include <TH2.h>
11 
12 class TUnfoldBinning;
13 
14 namespace DAS {
15 
20 Eigen::VectorXd identity (const Eigen::VectorXd& x);
21 
40 class Teddy {
41 
42  static Eigen::VectorXd H2V (const std::unique_ptr<TH1D>&);
43  static Eigen::MatrixXd H2M (const std::unique_ptr<TH2D>&);
44 
47  static int GuessOutDim (int, std::function<Eigen::VectorXd(const Eigen::VectorXd&)>);
48 
51  static void SanityCheck (const Eigen::VectorXd&, Eigen::VectorXd&, double = 0);
52 
53  const Eigen::VectorXd vec;
54  const Eigen::MatrixXd cov;
55 
56  const int nOldBins,
58  long long N;
59 
60  Eigen::VectorXd eigVals;
61  Eigen::MatrixXd rotation;
62 
63  Eigen::VectorXd sigma;
64 
65  Eigen::VectorXd sum;
66  Eigen::MatrixXd sum2;
67 
68  std::function<Eigen::VectorXd(const Eigen::VectorXd &)> func;
69 
70  TRandom3 random;
71 
72 public:
73 
74  static bool verbose;
75 
80  (const Eigen::VectorXd&,
81  const Eigen::MatrixXd&,
82  std::function<Eigen::VectorXd(const Eigen::VectorXd &)> = identity,
83  int = 42
84  );
85 
90  (const std::unique_ptr<TH1D>&,
91  const std::unique_ptr<TH2D>&,
92  std::function<Eigen::VectorXd(const Eigen::VectorXd &)> = identity,
93  int = 42
94  );
95 
96  void play ();
97  std::pair<Eigen::VectorXd, Eigen::MatrixXd> buy () const;
98 
103  template<typename... Args>
104  std::pair<std::unique_ptr<TH1D>,std::unique_ptr<TH2D>> buy
105  (TString name, Args... args) const
106  {
107  using namespace std;
108  using namespace Eigen;
109 
110  TAxis * axis = new TAxis(args...);
111  int ndiv = axis->GetNbins();
112  double * edges = new double[ndiv];
113  axis->GetLowEdge(edges);
114  edges[ndiv-1] = axis->GetBinUpEdge(ndiv);
115  auto h = make_unique<TH1D>(name , name, ndiv-1, edges);
116  auto cov = make_unique<TH2D>(name + "_cov", name, ndiv-1, edges, ndiv-1, edges);
117 
118  VectorXd x(nNewBins);
119  MatrixXd err(nNewBins, nNewBins);
120  tie(x,err) = buy();
121 
122  for (int i=0; i<nNewBins; ++i) {
123  double content = x(i),
124  error = sqrt(err(i,i));
125 
126  h->SetBinContent(i+1, content);
127  h->SetBinError (i+1, error );
128 
129  for (int j=0; j<nNewBins; ++j)
130  cov->SetBinContent(i+1,j+1, err(i,j));
131  }
132 
133  return make_pair<unique_ptr<TH1D>,unique_ptr<TH2D>>(move(h),move(cov));
134  }
135 };
136 bool Teddy::verbose = true;
137 
138 } // end of DAS namespace
DYToLL_M-50_13TeV_pythia8_cff_GEN_SIM_RECOBEFMIX_DIGI_L1_DIGI2RAW_L1Reco_RECO.name
name
Definition: DYToLL_M-50_13TeV_pythia8_cff_GEN_SIM_RECOBEFMIX_DIGI_L1_DIGI2RAW_L1Reco_RECO.py:48
DAS::Teddy::verbose
static bool verbose
Definition: Teddy.h:74
DAS::Teddy::cov
const Eigen::MatrixXd cov
covariance matrix
Definition: Teddy.h:54
DAS
Definition: applyBTagSF.cc:31
DAS::Teddy::func
std::function< Eigen::VectorXd(const Eigen::VectorXd &)> func
transformation defining output as a function of the input
Definition: Teddy.h:68
Ntupliser_cfg.args
args
Definition: Ntupliser_cfg.py:11
DAS::Teddy::SanityCheck
static void SanityCheck(const Eigen::VectorXd &, Eigen::VectorXd &, double=0)
Check the number of negative eigenvalues and fail if any is found.
Definition: Teddy.cc:54
DAS::Teddy::buy
std::pair< std::unique_ptr< TH1D >, std::unique_ptr< TH2D > > buy(TString name, Args... args) const
Definition: Teddy.h:105
DAS::Teddy::random
TRandom3 random
random generator
Definition: Teddy.h:70
DAS::Teddy::vec
const Eigen::VectorXd vec
distribution
Definition: Teddy.h:53
DAS::Teddy::sum
Eigen::VectorXd sum
sum of events, increased at each call and from which output distribution is estimated
Definition: Teddy.h:65
DAS::Teddy::N
long long N
number of calls
Definition: Teddy.h:58
DAS::Teddy::nNewBins
const int nNewBins
number of bins of output distributions
Definition: Teddy.h:57
DAS::Teddy::buy
std::pair< Eigen::VectorXd, Eigen::MatrixXd > buy() const
Get output distributions, can be called several times.
Definition: Teddy.cc:153
DAS::Teddy::GuessOutDim
static int GuessOutDim(int, std::function< Eigen::VectorXd(const Eigen::VectorXd &)>)
Guess size of the output distribution by calling once the function with dummy input.
Definition: Teddy.cc:46
DAS::Teddy::sum2
Eigen::MatrixXd sum2
sum of tensor products of each event, increased at each call and from which output covariance is esti...
Definition: Teddy.h:66
DAS::Teddy::sigma
Eigen::VectorXd sigma
Gaussian widths in diagonal base.
Definition: Teddy.h:63
DAS::identity
double identity(double x)
Definition: Greta.h:23
DAS::Teddy::nOldBins
const int nOldBins
number of bins of input distributions
Definition: Teddy.h:56
DAS::Teddy
Definition: Teddy.h:40
DAS::Teddy::H2M
static Eigen::MatrixXd H2M(const std::unique_ptr< TH2D > &)
conversion from (2D) histogram to matrix
Definition: Teddy.cc:34
DAS::Teddy::eigVals
Eigen::VectorXd eigVals
eigenvalues of input covariance matrix
Definition: Teddy.h:60
DAS::Teddy::H2V
static Eigen::VectorXd H2V(const std::unique_ptr< TH1D > &)
conversion from (1D) histogram to vector
Definition: Teddy.cc:25
DAS::Teddy::Teddy
Teddy(const Eigen::VectorXd &, const Eigen::MatrixXd &, std::function< Eigen::VectorXd(const Eigen::VectorXd &)>=identity, int=42)
DAS::Teddy::Teddy
Teddy(const std::unique_ptr< TH1D > &, const std::unique_ptr< TH2D > &, std::function< Eigen::VectorXd(const Eigen::VectorXd &)>=identity, int=42)
DAS::Teddy::play
void play()
Needs to be called from a loop, but requires no argument.
Definition: Teddy.cc:130
DAS::Teddy::rotation
Eigen::MatrixXd rotation
rotation from diagonal basis to original basis
Definition: Teddy.h:61