Public Member Functions | Static Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes | List of all members
ana::Hist Class Reference

#include "/cvmfs/nova.opensciencegrid.org/externals/cafanacore/v01.11/src/CAFAna/Core/Hist.h"

Public Member Functions

 Hist (const Hist &)
 
 Hist (Hist &&)
 
Histoperator= (const Hist &)
 
Histoperator= (Hist &&)
 
 ~Hist ()
 
bool Initialized () const
 
TH1D * ToTH1 (const Binning &bins) const
 
bool HasStan () const
 
const Eigen::ArrayXd & GetEigen () const
 
const Eigen::ArrayXstanGetEigenStan () const
 
int GetNbinsX () const
 
double GetBinError (int i) const
 
double Integral () const
 
void Fill (const Binning &bins, double x, double w)
 
void Scale (double s)
 
void Scale (const stan::math::var &s)
 
void ResetErrors ()
 
double GetBinContent (int i) const
 
void SetBinContent (int i, double x)
 
void Reset ()
 
void Add (const Hist &rhs, double scale=1)
 
void Multiply (const Hist &rhs)
 
void Divide (const Hist &rhs)
 
void Write (const Binning &bins) const
 

Static Public Member Functions

static Hist Uninitialized ()
 
static Hist Zero (int nbins)
 
static Hist ZeroSparse (int nbins)
 
static Hist AdoptSparse (Eigen::SparseVector< double > &&v)
 
static Hist AdoptStan (Eigen::ArrayXstan &&v)
 
static Hist Adopt (Eigen::ArrayXd &&v)
 
static Hist FromDirectory (TDirectory *dir)
 

Protected Types

enum  EType { kUninitialized, kDense, kDenseStan, kSparse }
 

Protected Member Functions

 Hist ()
 
void Add (const Eigen::SparseVector< double > &rhs, double scale)
 
void Add (const Eigen::ArrayXstan &rhs, double scale)
 
void Add (const Eigen::ArrayXd &rhs, double scale)
 

Protected Attributes

EType fType
 
Eigen::SparseVector< double > fDataSparse
 
Eigen::ArrayXstan fDataStan
 
Eigen::ArrayXd fData
 
Eigen::ArrayXd fSumSq
 Accumulate errors, if enabled. More...
 
bool fSqrtErrs
 Special case when filled with unweighted data. More...
 

Detailed Description

Definition at line 29 of file Hist.h.

Member Enumeration Documentation

enum ana::Hist::EType
protected
Enumerator
kUninitialized 
kDense 
kDenseStan 
kSparse 

Definition at line 83 of file Hist.h.

Constructor & Destructor Documentation

ana::Hist::Hist ( const Hist rhs)

Definition at line 61 of file Hist.cxx.

References ana::assert(), fData, fDataSparse, fDataStan, fSqrtErrs, fSumSq, fType, and Initialized().

62  : Hist()
63  {
64  assert(rhs.Initialized());
65 
66  fType = rhs.fType;
67  // Only one of these will actually have contents
68  fDataSparse = rhs.fDataSparse;
69  fDataStan = rhs.fDataStan;
70  fData = rhs.fData;
71  fSumSq = rhs.fSumSq;
72  fSqrtErrs = rhs.fSqrtErrs;
73  }
Eigen::ArrayXd fSumSq
Accumulate errors, if enabled.
Definition: Hist.h:89
Hist()
Definition: Hist.cxx:36
Eigen::SparseVector< double > fDataSparse
Definition: Hist.h:86
bool fSqrtErrs
Special case when filled with unweighted data.
Definition: Hist.h:90
Eigen::ArrayXstan fDataStan
Definition: Hist.h:87
Eigen::ArrayXd fData
Definition: Hist.h:88
EType fType
Definition: Hist.h:84
assert(nhit_max >=nhit_nbins)
ana::Hist::Hist ( Hist &&  rhs)

Definition at line 76 of file Hist.cxx.

References ana::assert(), fData, fDataSparse, fDataStan, fSqrtErrs, fSumSq, fType, and std::swap().

77  : Hist()
78  {
79  assert(rhs.Initialized());
80 
81  fType = rhs.fType;
82  std::swap(fDataSparse, rhs.fDataSparse);
83  std::swap(fDataStan, rhs.fDataStan);
84  std::swap(fData, rhs.fData);
85  std::swap(fSumSq, rhs.fSumSq);
86  fSqrtErrs = rhs.fSqrtErrs;
87  }
Eigen::ArrayXd fSumSq
Accumulate errors, if enabled.
Definition: Hist.h:89
Hist()
Definition: Hist.cxx:36
Eigen::SparseVector< double > fDataSparse
Definition: Hist.h:86
void swap(art::HLTGlobalStatus &lhs, art::HLTGlobalStatus &rhs)
bool fSqrtErrs
Special case when filled with unweighted data.
Definition: Hist.h:90
Eigen::ArrayXstan fDataStan
Definition: Hist.h:87
Eigen::ArrayXd fData
Definition: Hist.h:88
EType fType
Definition: Hist.h:84
assert(nhit_max >=nhit_nbins)
ana::Hist::~Hist ( )

Definition at line 126 of file Hist.cxx.

127  {
128  }
ana::Hist::Hist ( )
protected

Definition at line 36 of file Hist.cxx.

36  : fType(kUninitialized), fSqrtErrs(false)
37  {
38  }
bool fSqrtErrs
Special case when filled with unweighted data.
Definition: Hist.h:90
EType fType
Definition: Hist.h:84

Member Function Documentation

void ana::Hist::Add ( const Hist rhs,
double  scale = 1 
)

Definition at line 455 of file Hist.cxx.

References ana::assert(), fData, fDataSparse, fDataStan, fSqrtErrs, fSumSq, fType, Initialized(), kDense, kDenseStan, and kSparse.

Referenced by ana::Spectrum::PlusEqualsHelper().

456  {
457  assert(Initialized());
458  assert(rhs.Initialized());
459 
460  switch(rhs.fType){
461  case kSparse: Add(rhs.fDataSparse, scale); break;
462  case kDenseStan: Add(rhs.fDataStan, scale); break;
463  case kDense: Add(rhs.fData, scale); break;
464  default: abort(); // unreachable
465  }
466 
467  if(scale != 1 || !rhs.fSqrtErrs) fSqrtErrs = false;
468 
469  if(fSumSq.size() > 0){
470  if(rhs.fSumSq.size() > 0) fSumSq += scale * rhs.fSumSq;
471  // otherwise nothing to add in
472  }
473  else{
474  fSumSq = scale * rhs.fSumSq;
475  }
476  }
Eigen::ArrayXd fSumSq
Accumulate errors, if enabled.
Definition: Hist.h:89
void Add(const Hist &rhs, double scale=1)
Definition: Hist.cxx:455
bool fSqrtErrs
Special case when filled with unweighted data.
Definition: Hist.h:90
bool Initialized() const
Definition: Hist.h:39
Double_t scale
Definition: plot.C:25
assert(nhit_max >=nhit_nbins)
void ana::Hist::Add ( const Eigen::SparseVector< double > &  rhs,
double  scale 
)
protected

Definition at line 396 of file Hist.cxx.

References fData, fDataSparse, fDataStan, fType, kDense, kDenseStan, kSparse, and scale.

397  {
398  switch(fType){
399  case kSparse: fDataSparse += rhs * scale; break;
400  case kDenseStan: fDataStan += rhs * scale; break;
401  case kDense: fData += rhs * scale; break;
402  default: abort(); // unreachable
403  }
404  }
Eigen::SparseVector< double > fDataSparse
Definition: Hist.h:86
Double_t scale
Definition: plot.C:25
Eigen::ArrayXstan fDataStan
Definition: Hist.h:87
Eigen::ArrayXd fData
Definition: Hist.h:88
EType fType
Definition: Hist.h:84
void ana::Hist::Add ( const Eigen::ArrayXstan rhs,
double  scale 
)
protected

Definition at line 407 of file Hist.cxx.

References fData, fDataSparse, fDataStan, fType, kDense, kDenseStan, kSparse, and scale.

408  {
409  switch(fType){
410  case kSparse:
411  fType = kDenseStan;
412  fDataStan = rhs * scale;
414  fDataSparse.resize(0);
415  break;
416 
417  case kDenseStan:
418  fDataStan += rhs * scale;
419  break;
420 
421  case kDense:
422  fType = kDenseStan;
423  fDataStan = fData + rhs * scale;
424  fData.resize(0);
425  break;
426 
427  default: abort(); // unreachable
428  }
429  }
Eigen::SparseVector< double > fDataSparse
Definition: Hist.h:86
Double_t scale
Definition: plot.C:25
Eigen::ArrayXstan fDataStan
Definition: Hist.h:87
Eigen::ArrayXd fData
Definition: Hist.h:88
EType fType
Definition: Hist.h:84
void ana::Hist::Add ( const Eigen::ArrayXd &  rhs,
double  scale 
)
protected

Definition at line 432 of file Hist.cxx.

References fData, fDataSparse, fDataStan, fType, kDense, kDenseStan, kSparse, and scale.

433  {
434  switch(fType){
435  case kSparse:
436  fType = kDense;
437  fData = rhs * scale;
438  fData += fDataSparse;
439  fDataSparse.resize(0);
440  break;
441 
442  case kDenseStan:
443  fDataStan += rhs * scale;
444  break;
445 
446  case kDense:
447  fData += rhs * scale;
448  break;
449 
450  default: abort(); // unreachable
451  }
452  }
Eigen::SparseVector< double > fDataSparse
Definition: Hist.h:86
Double_t scale
Definition: plot.C:25
Eigen::ArrayXstan fDataStan
Definition: Hist.h:87
Eigen::ArrayXd fData
Definition: Hist.h:88
EType fType
Definition: Hist.h:84
Hist ana::Hist::Adopt ( Eigen::ArrayXd &&  v)
static

Definition at line 149 of file Hist.cxx.

References fData, fType, kDense, runNovaSAM::ret, and registry_explorer::v.

Referenced by ana::ReweightableSpectrum::UnWeighted(), ana::ReweightableSpectrum::WeightedBy(), and ana::ReweightableSpectrum::WeightingVariable().

150  {
151  Hist ret;
152  ret.fType = kDense;
153  ret.fData = std::move(v);
154  return ret;
155  }
Hist()
Definition: Hist.cxx:36
Hist ana::Hist::AdoptSparse ( Eigen::SparseVector< double > &&  v)
static

Definition at line 131 of file Hist.cxx.

References fDataSparse, fType, kSparse, runNovaSAM::ret, and registry_explorer::v.

132  {
133  Hist ret;
134  ret.fType = kSparse;
135  ret.fDataSparse = std::move(v);
136  return ret;
137  }
Hist()
Definition: Hist.cxx:36
Hist ana::Hist::AdoptStan ( Eigen::ArrayXstan &&  v)
static

Definition at line 140 of file Hist.cxx.

References fDataStan, fType, kDenseStan, runNovaSAM::ret, and registry_explorer::v.

Referenced by ana::ReweightableSpectrum::WeightedBy().

141  {
142  Hist ret;
143  ret.fType = kDenseStan;
144  ret.fDataStan = std::move(v);
145  return ret;
146  }
Hist()
Definition: Hist.cxx:36
void ana::Hist::Divide ( const Hist rhs)

Definition at line 531 of file Hist.cxx.

References ana::assert(), om::cout, allTimeWatchdog::endl, fData, fDataStan, fSqrtErrs, fSumSq, fType, Initialized(), kDense, kDenseStan, kSparse, and util::sqr().

Referenced by ana::Ratio::operator/=(), ana::Spectrum::operator/=(), and ana::Ratio::Ratio().

532  {
533  assert(Initialized());
534  assert(rhs.Initialized());
535 
536  if(fType == kSparse || rhs.fType == kSparse){
537  std::cout << "Hist::Divide() not implemented for sparse vectors" << std::endl;
538  abort();
539  }
540 
541  if(fType == kDenseStan){
542  if(rhs.fType == kDenseStan){
543  fDataStan /= rhs.fDataStan;
544  }
545  else{
546  fDataStan /= rhs.fData;
547  }
548  }
549  else{
550  if(rhs.fType == kDenseStan){
551  fType = kDenseStan;
552  fDataStan = fData / rhs.fDataStan;
553  fData.resize(0);
554  }
555  else{
556  fData /= rhs.fData;
557  }
558  }
559 
560  if(fType == kDense && rhs.fType == kDense){
561  if(fSumSq.size() > 0){
562  if(rhs.fSumSq.size() > 0){
563  fSumSq = fSumSq / util::sqr(rhs.fData) + rhs.fSumSq * util::sqr(fData) / util::sqr(util::sqr(rhs.fData));
564  }
565  else{
566  fSumSq /= util::sqr(rhs.fData);
567  }
568  }
569  else if(rhs.fSumSq.size() > 0){
570  fSumSq = rhs.fSumSq * util::sqr(fData) / util::sqr(util::sqr(rhs.fData));
571  }
572  }
573  else{
574  // Didn't bother to implement error prop for dividing stan/sparse
575  // hists
576  fSumSq.resize(0);
577  }
578 
579  fSqrtErrs = false;
580  }
Eigen::ArrayXd fSumSq
Accumulate errors, if enabled.
Definition: Hist.h:89
T sqr(T x)
More efficient square function than pow(x,2)
Definition: MathUtil.h:23
bool fSqrtErrs
Special case when filled with unweighted data.
Definition: Hist.h:90
bool Initialized() const
Definition: Hist.h:39
Eigen::ArrayXstan fDataStan
Definition: Hist.h:87
OStream cout
Definition: OStream.cxx:6
Eigen::ArrayXd fData
Definition: Hist.h:88
EType fType
Definition: Hist.h:84
assert(nhit_max >=nhit_nbins)
void ana::Hist::Fill ( const Binning bins,
double  x,
double  w 
)

Definition at line 265 of file Hist.cxx.

References ana::assert(), bin, om::cout, allTimeWatchdog::endl, fData, fDataSparse, ana::Binning::FindBin(), fSqrtErrs, fSumSq, fType, gStatErrs, Initialized(), kDense, kDenseStan, kSparse, and w.

Referenced by ana::Spectrum::Fill().

266  {
267  assert(Initialized());
268 
269  if(w != 1) fSqrtErrs = false;
270 
271  if(fType == kDense && gStatErrs && fSumSq.size() == 0) fSumSq.resize(fData.size());
272 
273  switch(fType){
274  case kSparse:
275  fDataSparse.coeffRef(bins.FindBin(x)) += w;
276  break;
277  case kDenseStan:
278  std::cout << "Hist::Fill() not supported for stan vars" << std::endl;
279  abort();
280  case kDense:
281  {
282  const int bin = bins.FindBin(x);
283  fData[bin] += w;
284  if(gStatErrs){
285  if(fSumSq.size() == 0) fSumSq.resize(fData.size());
286  fSumSq[bin] += w*w;
287  }
288  }
289  break;
290  default:
291  abort(); // unreachable
292  }
293  }
class StatErrorsEnabled gStatErrs
Eigen::ArrayXd fSumSq
Accumulate errors, if enabled.
Definition: Hist.h:89
int FindBin(double x) const
Definition: Binning.cxx:155
Eigen::SparseVector< double > fDataSparse
Definition: Hist.h:86
bool fSqrtErrs
Special case when filled with unweighted data.
Definition: Hist.h:90
bool Initialized() const
Definition: Hist.h:39
float bin[41]
Definition: plottest35.C:14
OStream cout
Definition: OStream.cxx:6
const Binning bins
Definition: NumuCC_CPiBin.h:8
Eigen::ArrayXd fData
Definition: Hist.h:88
EType fType
Definition: Hist.h:84
assert(nhit_max >=nhit_nbins)
Float_t w
Definition: plot.C:20
Hist ana::Hist::FromDirectory ( TDirectory *  dir)
static

Definition at line 158 of file Hist.cxx.

References ana::assert(), d, fData, fDataSparse, fSumSq, fType, gStatErrs, make_syst_table_plots::h, MECModelEnuComparisons::i, compare_h5_caf::idx, kDense, kSparse, nbins, runNovaSAM::ret, util::sqr(), and submit_syst::y.

Referenced by ana::Spectrum::LoadFrom().

159  {
160  Hist ret;
161 
162  TH1D* h = (TH1D*)dir->Get("hist");
163  THnSparseD* hSparse = (THnSparseD*)dir->Get("hist_sparse");
164  assert(bool(h) != bool(hSparse));
165 
166  if(h){
167  ret.fType = kDense;
168  ret.fData = Eigen::Map<Eigen::ArrayXd>(h->GetArray(), h->GetNbinsX()+2);
169 
170  if(gStatErrs){
171  const int N = h->GetNbinsX()+2;
172  ret.fSumSq.resize(N);
173  for(int i = 0; i < N; ++i) ret.fSumSq[i] = util::sqr(h->GetBinError(i));
174  }
175  }
176  if(hSparse){
177  ret.fType = kSparse;
178 
179  // Number of bins there would be in a dense version
180  int nbins = 1;
181  const int N = hSparse->GetNdimensions();
182  for(int d = 0; d < N; ++d)
183  nbins *= hSparse->GetAxis(d)->GetNbins()+2;
184 
185  ret.fDataSparse.resize(nbins);
186  ret.fDataSparse.setZero();
187  for(int i = 0; i < hSparse->GetNbins(); ++i){
188  int idx[N];
189  const double y = hSparse->GetBinContent(i, idx);
190  int idx1d = idx[0];
191  int mult = 1;
192  for(int d = 1; d < N; ++d){
193  mult *= hSparse->GetAxis(d-1)->GetNbins()+2;
194  idx1d += mult*idx[d];
195  }
196  ret.fDataSparse.coeffRef(idx1d) = y;
197  }
198  }
199 
200  delete h;
201  delete hSparse;
202 
203  return ret;
204  }
class StatErrorsEnabled gStatErrs
Hist()
Definition: Hist.cxx:36
T sqr(T x)
More efficient square function than pow(x,2)
Definition: MathUtil.h:23
const int nbins
Definition: cellShifts.C:15
Float_t d
Definition: plot.C:236
TDirectory * dir
Definition: macro.C:5
assert(nhit_max >=nhit_nbins)
double ana::Hist::GetBinContent ( int  i) const

Definition at line 349 of file Hist.cxx.

References ana::assert(), fData, fDataSparse, fDataStan, fType, MECModelEnuComparisons::i, Initialized(), kDense, kDenseStan, and kSparse.

Referenced by GetBinError(), ana::Spectrum::Mean(), ana::Spectrum::MockData(), and ana::Ratio::Ratio().

350  {
351  assert(Initialized());
352 
353  switch(fType){
354  case kSparse: return fDataSparse.coeff(i);
355  case kDenseStan: return fDataStan[i].val();
356  case kDense: return fData[i];
357  default:
358  abort(); // unreachable
359  }
360  }
Eigen::SparseVector< double > fDataSparse
Definition: Hist.h:86
bool Initialized() const
Definition: Hist.h:39
Eigen::ArrayXstan fDataStan
Definition: Hist.h:87
Eigen::ArrayXd fData
Definition: Hist.h:88
EType fType
Definition: Hist.h:84
assert(nhit_max >=nhit_nbins)
double ana::Hist::GetBinError ( int  i) const

Definition at line 241 of file Hist.cxx.

References ana::assert(), fSqrtErrs, fSumSq, GetBinContent(), Initialized(), and std::sqrt().

Referenced by ana::Spectrum::Integral(), and ToTH1().

242  {
243  assert(Initialized());
244 
245  if(fSqrtErrs) return sqrt(GetBinContent(i));
246  if(fSumSq.size() > 0) return sqrt(fSumSq[i]);
247 
248  return 0;
249  }
Eigen::ArrayXd fSumSq
Accumulate errors, if enabled.
Definition: Hist.h:89
T sqrt(T number)
Definition: d0nt_math.hpp:156
bool fSqrtErrs
Special case when filled with unweighted data.
Definition: Hist.h:90
bool Initialized() const
Definition: Hist.h:39
double GetBinContent(int i) const
Definition: Hist.cxx:349
assert(nhit_max >=nhit_nbins)
const Eigen::ArrayXd& ana::Hist::GetEigen ( ) const
inline

Definition at line 53 of file Hist.h.

References ana::assert().

Referenced by ana::Ratio::GetEigen(), and ana::Spectrum::GetEigen().

53 {assert(fType == kDense); return fData;}
Eigen::ArrayXd fData
Definition: Hist.h:88
EType fType
Definition: Hist.h:84
assert(nhit_max >=nhit_nbins)
const Eigen::ArrayXstan& ana::Hist::GetEigenStan ( ) const
inline

Definition at line 54 of file Hist.h.

References ana::assert(), Divide(), Fill(), MECModelEnuComparisons::i, Integral(), slidt::Reset(), scale, Scale(), SetBinContent(), w, Write(), and submit_syst::x.

Referenced by ana::Ratio::GetEigenStan(), and ana::Spectrum::GetEigenStan().

54 {assert(fType == kDenseStan); return fDataStan;}
Eigen::ArrayXstan fDataStan
Definition: Hist.h:87
EType fType
Definition: Hist.h:84
assert(nhit_max >=nhit_nbins)
int ana::Hist::GetNbinsX ( ) const

Definition at line 228 of file Hist.cxx.

References ana::assert(), fData, fDataSparse, fDataStan, fType, Initialized(), kDense, kDenseStan, and kSparse.

Referenced by ana::Spectrum::Integral(), ana::Spectrum::MockData(), and ana::Ratio::Ratio().

229  {
230  assert(Initialized());
231 
232  switch(fType){
233  case kSparse: return fDataSparse.size()-2;
234  case kDenseStan: return fDataStan .size()-2;
235  case kDense: return fData .size()-2;
236  default: abort(); // unreachable
237  }
238  }
Eigen::SparseVector< double > fDataSparse
Definition: Hist.h:86
bool Initialized() const
Definition: Hist.h:39
Eigen::ArrayXstan fDataStan
Definition: Hist.h:87
Eigen::ArrayXd fData
Definition: Hist.h:88
EType fType
Definition: Hist.h:84
assert(nhit_max >=nhit_nbins)
bool ana::Hist::HasStan ( ) const
inline

Definition at line 52 of file Hist.h.

Referenced by ana::Ratio::HasStan(), and ana::Spectrum::HasStan().

52 {return fType == kDenseStan;}
EType fType
Definition: Hist.h:84
bool ana::Hist::Initialized ( ) const
inline
double ana::Hist::Integral ( ) const

Definition at line 252 of file Hist.cxx.

References ana::assert(), fData, fDataSparse, fDataStan, fType, Initialized(), kDense, kDenseStan, and kSparse.

Referenced by ana::Spectrum::Integral(), and ana::Spectrum::PlusEqualsHelper().

253  {
254  assert(Initialized());
255 
256  switch(fType){
257  case kSparse: return fDataSparse.sum();
258  case kDenseStan: return fDataStan .sum().val();
259  case kDense: return fData .sum();
260  default: abort(); // unreachable
261  }
262  }
Eigen::SparseVector< double > fDataSparse
Definition: Hist.h:86
bool Initialized() const
Definition: Hist.h:39
Eigen::ArrayXstan fDataStan
Definition: Hist.h:87
Eigen::ArrayXd fData
Definition: Hist.h:88
EType fType
Definition: Hist.h:84
assert(nhit_max >=nhit_nbins)
void ana::Hist::Multiply ( const Hist rhs)

Definition at line 479 of file Hist.cxx.

References ana::assert(), om::cout, allTimeWatchdog::endl, fData, fDataStan, fSqrtErrs, fSumSq, fType, Initialized(), kDense, kDenseStan, kSparse, and util::sqr().

Referenced by ana::Ratio::operator*=(), and ana::Spectrum::operator*=().

480  {
481  assert(Initialized());
482  assert(rhs.Initialized());
483 
484  if(fType == kSparse || rhs.fType == kSparse){
485  std::cout << "Hist::Multiply() not implemented for sparse vectors" << std::endl;
486  abort();
487  }
488 
489  if(fType == kDenseStan){
490  if(rhs.fType == kDenseStan){
491  fDataStan *= rhs.fDataStan;
492  }
493  else{
494  fDataStan *= rhs.fData;
495  }
496  }
497  else{
498  if(rhs.fType == kDenseStan){
499  fType = kDenseStan;
500  fDataStan = fData * rhs.fDataStan;
501  fData.resize(0);
502  }
503  else{
504  fData *= rhs.fData;
505  }
506  }
507 
508  if(fType == kDense && rhs.fType == kDense){
509  if(fSumSq.size() > 0){
510  if(rhs.fSumSq.size() > 0){
511  fSumSq = fSumSq * util::sqr(rhs.fData) + rhs.fSumSq * util::sqr(fData);
512  }
513  else{
514  fSumSq *= util::sqr(rhs.fData);
515  }
516  }
517  else if(rhs.fSumSq.size() > 0){
518  fSumSq = rhs.fSumSq * util::sqr(fData);
519  }
520  }
521  else{
522  // Didn't bother to implement error prop for multiplying stan/sparse
523  // hists
524  fSumSq.resize(0);
525  }
526 
527  fSqrtErrs = false;
528  }
Eigen::ArrayXd fSumSq
Accumulate errors, if enabled.
Definition: Hist.h:89
T sqr(T x)
More efficient square function than pow(x,2)
Definition: MathUtil.h:23
bool fSqrtErrs
Special case when filled with unweighted data.
Definition: Hist.h:90
bool Initialized() const
Definition: Hist.h:39
Eigen::ArrayXstan fDataStan
Definition: Hist.h:87
OStream cout
Definition: OStream.cxx:6
Eigen::ArrayXd fData
Definition: Hist.h:88
EType fType
Definition: Hist.h:84
assert(nhit_max >=nhit_nbins)
Hist & ana::Hist::operator= ( const Hist rhs)

Definition at line 90 of file Hist.cxx.

References ana::assert(), fData, fDataSparse, fDataStan, fSqrtErrs, fSumSq, fType, and Initialized().

91  {
92  if(this == &rhs) return *this;
93 
94  DontAddDirectory guard;
95 
96  assert(rhs.Initialized());
97 
98  fType = rhs.fType;
99  fDataSparse = rhs.fDataSparse;
100  fDataStan = rhs.fDataStan;
101  fData = rhs.fData;
102  fSumSq = rhs.fSumSq;
103  fSqrtErrs = rhs.fSqrtErrs;
104 
105  return *this;
106  }
Eigen::ArrayXd fSumSq
Accumulate errors, if enabled.
Definition: Hist.h:89
Eigen::SparseVector< double > fDataSparse
Definition: Hist.h:86
bool fSqrtErrs
Special case when filled with unweighted data.
Definition: Hist.h:90
Eigen::ArrayXstan fDataStan
Definition: Hist.h:87
Eigen::ArrayXd fData
Definition: Hist.h:88
EType fType
Definition: Hist.h:84
assert(nhit_max >=nhit_nbins)
Hist & ana::Hist::operator= ( Hist &&  rhs)

Definition at line 109 of file Hist.cxx.

References ana::assert(), fData, fDataSparse, fDataStan, fSqrtErrs, fSumSq, fType, and std::swap().

110  {
111  if(this == &rhs) return *this;
112 
113  assert(rhs.Initialized());
114 
115  fType = rhs.fType;
116  std::swap(fDataSparse, rhs.fDataSparse);
117  std::swap(fDataStan, rhs.fDataStan);
118  std::swap(fData, rhs.fData);
119  std::swap(fSumSq, rhs.fSumSq);
120  fSqrtErrs = rhs.fSqrtErrs;
121 
122  return *this;
123  }
Eigen::ArrayXd fSumSq
Accumulate errors, if enabled.
Definition: Hist.h:89
Eigen::SparseVector< double > fDataSparse
Definition: Hist.h:86
void swap(art::HLTGlobalStatus &lhs, art::HLTGlobalStatus &rhs)
bool fSqrtErrs
Special case when filled with unweighted data.
Definition: Hist.h:90
Eigen::ArrayXstan fDataStan
Definition: Hist.h:87
Eigen::ArrayXd fData
Definition: Hist.h:88
EType fType
Definition: Hist.h:84
assert(nhit_max >=nhit_nbins)
void ana::Hist::Reset ( void  )

Definition at line 382 of file Hist.cxx.

References fData, fDataSparse, fDataStan, fSqrtErrs, fSumSq, fType, kDense, kDenseStan, and kSparse.

Referenced by ana::Spectrum::Clear().

383  {
384  switch(fType){
385  case kSparse: fDataSparse.setZero(); break;
386  case kDenseStan: fDataStan .setZero(); break;
387  case kDense: fData .setZero(); break;
388  default: ; // OK?
389  }
390 
391  fSqrtErrs = true;
392  fSumSq.resize(0);
393  }
Eigen::ArrayXd fSumSq
Accumulate errors, if enabled.
Definition: Hist.h:89
Eigen::SparseVector< double > fDataSparse
Definition: Hist.h:86
bool fSqrtErrs
Special case when filled with unweighted data.
Definition: Hist.h:90
Eigen::ArrayXstan fDataStan
Definition: Hist.h:87
Eigen::ArrayXd fData
Definition: Hist.h:88
EType fType
Definition: Hist.h:84
void ana::Hist::ResetErrors ( )

Definition at line 342 of file Hist.cxx.

References fSqrtErrs, and fSumSq.

Referenced by ana::Spectrum::AsimovData(), and ana::Spectrum::MockData().

343  {
344  fSumSq.resize(0);
345  fSqrtErrs = true;
346  }
Eigen::ArrayXd fSumSq
Accumulate errors, if enabled.
Definition: Hist.h:89
bool fSqrtErrs
Special case when filled with unweighted data.
Definition: Hist.h:90
void ana::Hist::Scale ( double  s)

Definition at line 296 of file Hist.cxx.

References ana::assert(), fData, fDataSparse, fDataStan, fSqrtErrs, fSumSq, fType, Initialized(), kDense, kDenseStan, and kSparse.

Referenced by ana::Spectrum::AsimovData(), ana::Ratio::Ratio(), and ana::Spectrum::Scale().

297  {
298  assert(Initialized());
299 
300  switch(fType){
301  case kSparse: fDataSparse *= s; break;
302  case kDenseStan: fDataStan *= s; break;
303  case kDense: fData *= s; break;
304  default:
305  abort(); // unreachable
306  }
307 
308  if(s != 1) fSqrtErrs = false;
309  fSumSq *= s;
310  }
Eigen::ArrayXd fSumSq
Accumulate errors, if enabled.
Definition: Hist.h:89
Eigen::SparseVector< double > fDataSparse
Definition: Hist.h:86
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
Eigen::ArrayXstan fDataStan
Definition: Hist.h:87
Eigen::ArrayXd fData
Definition: Hist.h:88
EType fType
Definition: Hist.h:84
assert(nhit_max >=nhit_nbins)
void ana::Hist::Scale ( const stan::math::var s)

Definition at line 313 of file Hist.cxx.

References ana::assert(), fData, fDataSparse, fDataStan, fSqrtErrs, fSumSq, fType, Initialized(), kDense, kDenseStan, kSparse, and stan::math::var::val().

314  {
315  assert(Initialized());
316  switch (fType){
317  case kSparse:
318  fDataStan = fDataSparse * s;
319  fDataSparse.resize(0);
320  break;
321 
322  case kDense:
323  fDataStan = fData * s;
324  fData.resize(0);
325  break;
326 
327  case kDenseStan:
328  fDataStan *= s;
329  break;
330 
331  default:
332  abort(); // unreachable
333  }
334 
335  fType = kDenseStan;
336 
337  if(s != 1) fSqrtErrs = false;
338  fSumSq *= s.val();
339  }
Eigen::ArrayXd fSumSq
Accumulate errors, if enabled.
Definition: Hist.h:89
Eigen::SparseVector< double > fDataSparse
Definition: Hist.h:86
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
Eigen::ArrayXstan fDataStan
Definition: Hist.h:87
Eigen::ArrayXd fData
Definition: Hist.h:88
EType fType
Definition: Hist.h:84
assert(nhit_max >=nhit_nbins)
void ana::Hist::SetBinContent ( int  i,
double  x 
)

Definition at line 363 of file Hist.cxx.

References ana::assert(), om::cout, allTimeWatchdog::endl, fData, fDataSparse, fSqrtErrs, fSumSq, fType, MECModelEnuComparisons::i, Initialized(), kDense, kDenseStan, kSparse, and submit_syst::x.

Referenced by ana::Spectrum::MockData(), and ana::Ratio::Ratio().

364  {
365  assert(Initialized());
366 
367  switch(fType){
368  case kSparse: fDataSparse.coeffRef(i) = x; break;
369  case kDenseStan:
370  std::cout << "Hist::SetBinContent() not implemented for stan vars" << std::endl;
371  abort();
372  case kDense: fData[i] = x; break;
373  default:
374  abort(); // unreachable
375  }
376 
377  fSqrtErrs = false;
378  if(fSumSq.size() > 0) fSumSq[i] = 0;
379  }
Eigen::ArrayXd fSumSq
Accumulate errors, if enabled.
Definition: Hist.h:89
Eigen::SparseVector< double > fDataSparse
Definition: Hist.h:86
bool fSqrtErrs
Special case when filled with unweighted data.
Definition: Hist.h:90
bool Initialized() const
Definition: Hist.h:39
OStream cout
Definition: OStream.cxx:6
Eigen::ArrayXd fData
Definition: Hist.h:88
EType fType
Definition: Hist.h:84
assert(nhit_max >=nhit_nbins)
TH1D * ana::Hist::ToTH1 ( const Binning bins) const

Definition at line 207 of file Hist.cxx.

References ana::assert(), fData, fDataSparse, fDataStan, fSqrtErrs, fSumSq, fType, GetBinError(), MECModelEnuComparisons::i, Initialized(), kDense, kDenseStan, kSparse, ana::MakeTH1D(), ana::Binning::NBins(), runNovaSAM::ret, ana::UniqueName(), and febshutoff_auto::val.

Referenced by ana::Ratio::ToTH1(), ana::Spectrum::ToTH1(), and Write().

208  {
209  assert(Initialized());
210 
211  TH1D* ret = MakeTH1D(UniqueName().c_str(), "", bins);
212 
213  for(int i = 0; i < bins.NBins()+2; ++i){
214  switch(fType){
215  case kDense: ret->SetBinContent(i, fData[i]); break;
216  case kDenseStan: ret->SetBinContent(i, fDataStan[i].val()); break;
217  // Interface requires returning literally a TH1D in any case
218  case kSparse: ret->SetBinContent(i, fDataSparse.coeff(i)); break;
219  default:
220  abort(); // unreachable
221  }
222  if(fSumSq.size() > 0 || fSqrtErrs) ret->SetBinError(i, GetBinError(i));
223  }
224  return ret;
225  }
Eigen::ArrayXd fSumSq
Accumulate errors, if enabled.
Definition: Hist.h:89
TH1D * MakeTH1D(const char *name, const char *title, const Binning &bins)
Utility function to avoid need to switch on bins.IsSimple()
Definition: UtilsExt.cxx:74
Eigen::SparseVector< double > fDataSparse
Definition: Hist.h:86
bool fSqrtErrs
Special case when filled with unweighted data.
Definition: Hist.h:90
bool Initialized() const
Definition: Hist.h:39
Eigen::ArrayXstan fDataStan
Definition: Hist.h:87
const Binning bins
Definition: NumuCC_CPiBin.h:8
Eigen::ArrayXd fData
Definition: Hist.h:88
EType fType
Definition: Hist.h:84
int NBins() const
Definition: Binning.h:29
assert(nhit_max >=nhit_nbins)
double GetBinError(int i) const
Definition: Hist.cxx:241
std::string UniqueName()
Return a different string each time, for creating histograms.
Definition: Utilities.cxx:29
static Hist ana::Hist::Uninitialized ( )
inlinestatic

Definition at line 38 of file Hist.h.

38 {return Hist();}
Hist()
Definition: Hist.cxx:36
void ana::Hist::Write ( const Binning bins) const

Definition at line 583 of file Hist.cxx.

References ana::assert(), om::cout, allTimeWatchdog::endl, fDataSparse, fSqrtErrs, fType, make_syst_table_plots::h, compare_h5_caf::idx, Initialized(), ana::Binning::IsSimple(), it, kDense, kDenseStan, kSparse, ana::Binning::Max(), ana::Binning::Min(), getGoodRuns4SAM::n, ana::Binning::NBins(), std::sqrt(), ToTH1(), and x1.

Referenced by ana::Spectrum::SaveTo().

584  {
585  assert(Initialized());
586 
587  if(fType == kDenseStan){
588  std::cout << "Hist::Write() not implemented (impossible?) for stan vars" << std::endl;
589  abort();
590  }
591  if(fType == kDense){
592  TH1D* h = ToTH1(bins);
593  h->Write("hist");
594  delete h;
595  }
596  if(fType == kSparse){
597  const int n = bins.NBins();
598  const double x0 = bins.IsSimple() ? bins.Min() : 0;
599  const double x1 = bins.IsSimple() ? bins.Max() : bins.NBins();
600  THnSparseD* h = new THnSparseD("", "", 1, &n, &x0, &x1);
601 
602  for(Eigen::SparseVector<double>::InnerIterator it(fDataSparse); it; ++it){
603  const int idx = it.index();
604  h->SetBinContent(&idx, it.value());
605  if(fSqrtErrs) h->SetBinError(&idx, sqrt(it.value()));
606  }
607 
608  h->Write("hist_sparse");
609  delete h;
610  }
611  }
set< int >::iterator it
Float_t x1[n_points_granero]
Definition: compare.C:5
T sqrt(T number)
Definition: d0nt_math.hpp:156
TH1D * ToTH1(const Binning &bins) const
Definition: Hist.cxx:207
Eigen::SparseVector< double > fDataSparse
Definition: Hist.h:86
bool IsSimple() const
Definition: Binning.h:33
bool fSqrtErrs
Special case when filled with unweighted data.
Definition: Hist.h:90
bool Initialized() const
Definition: Hist.h:39
double Min() const
Definition: Binning.h:30
OStream cout
Definition: OStream.cxx:6
double Max() const
Definition: Binning.h:31
const Binning bins
Definition: NumuCC_CPiBin.h:8
EType fType
Definition: Hist.h:84
int NBins() const
Definition: Binning.h:29
assert(nhit_max >=nhit_nbins)
Hist ana::Hist::Zero ( int  nbins)
static

Definition at line 41 of file Hist.cxx.

References fData, fSqrtErrs, fType, kDense, runNovaSAM::ret, and Zero().

Referenced by ana::Spectrum::Spectrum().

42  {
43  Hist ret;
44  ret.fType = kDense;
45  ret.fData = Eigen::ArrayXd::Zero(nbins+2);
46  ret.fSqrtErrs = true;
47  return ret;
48  }
Hist()
Definition: Hist.cxx:36
const int nbins
Definition: cellShifts.C:15
void Zero()
Hist ana::Hist::ZeroSparse ( int  nbins)
static

Definition at line 51 of file Hist.cxx.

References fDataSparse, fSqrtErrs, fType, kSparse, and runNovaSAM::ret.

Referenced by ana::Spectrum::Spectrum().

52  {
53  Hist ret;
54  ret.fType = kSparse;
55  ret.fDataSparse = Eigen::SparseVector<double>(nbins+2);
56  ret.fSqrtErrs = true;
57  return ret;
58  }
Hist()
Definition: Hist.cxx:36
const int nbins
Definition: cellShifts.C:15

Member Data Documentation

Eigen::ArrayXd ana::Hist::fData
protected
Eigen::SparseVector<double> ana::Hist::fDataSparse
protected
Eigen::ArrayXstan ana::Hist::fDataStan
protected
bool ana::Hist::fSqrtErrs
protected

Special case when filled with unweighted data.

Definition at line 90 of file Hist.h.

Referenced by Add(), Divide(), Fill(), GetBinError(), Hist(), Multiply(), operator=(), Reset(), ResetErrors(), Scale(), SetBinContent(), ToTH1(), Write(), Zero(), and ZeroSparse().

Eigen::ArrayXd ana::Hist::fSumSq
protected

Accumulate errors, if enabled.

Definition at line 89 of file Hist.h.

Referenced by Add(), Divide(), Fill(), FromDirectory(), GetBinError(), Hist(), Multiply(), operator=(), Reset(), ResetErrors(), Scale(), SetBinContent(), and ToTH1().

EType ana::Hist::fType
protected

The documentation for this class was generated from the following files: