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

Compare a single data spectrum to the MC + cosmics expectation. More...

#include "/cvmfs/nova-development.opensciencegrid.org/novasoft/releases/N20-10-23/CAFAna/Experiment/SingleSampleExperiment.h"

Inheritance diagram for ana::SingleSampleExperiment:
ana::IExperiment ana::BigChi2SingleSampleExperiment ana::CovMxExperiment

Public Member Functions

 SingleSampleExperiment (const IPrediction *pred, const Spectrum &data, const Spectrum &cosmic, double cosmicScaleError=0, bool PoissonError=false)
 
 SingleSampleExperiment (const IPrediction *pred, const Spectrum &data)
 In MC studies you might not want to bother with cosmics. More...
 
virtual ~SingleSampleExperiment ()
 
virtual double ChiSq (osc::IOscCalcAdjustable *osc, const SystShifts &syst=SystShifts::Nominal()) const override
 
const IPredictionGetPrediction () const
 
virtual stan::math::var LogLikelihood (osc::IOscCalcAdjustableStan *osc, const SystShifts &syst=kNoShift) const override
 
virtual void SaveTo (TDirectory *dir, const std::string &name) const override
 
 SingleSampleExperiment (const SingleSampleExperiment &)=delete
 
SingleSampleExperimentoperator= (const SingleSampleExperiment &)=delete
 
 SingleSampleExperiment (SingleSampleExperiment &&s)
 

Static Public Member Functions

static std::unique_ptr< SingleSampleExperimentLoadFrom (TDirectory *dir, const std::string &name)
 

Protected Member Functions

template<typename T >
Eigen::Array< T, Eigen::Dynamic, 1 > PredHistIncCosmics (osc::_IOscCalc< T > *calc, const SystShifts &syst) const
 

Protected Attributes

const IPredictionfMC
 
Spectrum fData
 
std::optional< SpectrumfCosmic
 
double fCosmicScaleError
 
bool fPoissonError
 
std::vector< std::pair< double, double > > fBinErrors
 

Detailed Description

Compare a single data spectrum to the MC + cosmics expectation.

Definition at line 21 of file SingleSampleExperiment.h.

Constructor & Destructor Documentation

ana::SingleSampleExperiment::SingleSampleExperiment ( const IPrediction pred,
const Spectrum data,
const Spectrum cosmic,
double  cosmicScaleError = 0,
bool  PoissonError = false 
)
Parameters
predSource of oscillated MC beam predictions
dataData spectrum to compare to
cosmicCosmic ray background component
cosmicScaleErrorfractional uncertainty on cosmic normalization

Definition at line 24 of file SingleSampleExperiment.cxx.

References fBinErrors, make_associated_cosmic_defs::fc, fPoissonError, ana::Spectrum::GetEigen(), MECModelEnuComparisons::i, ana::kLivetime, ana::Spectrum::Livetime(), std::sqrt(), and submit_syst::y.

29  : fMC(pred), fData(data),
30  fCosmic(cosmic),
31  fCosmicScaleError(cosmicScaleError),
32  fPoissonError(PoissonError)
33  {
34  // Fill the vector of errors in case of PoissonError = true
35  if(fPoissonError){
36  // Note this is scaled to the cosmic's intrinsic stats level
37  Eigen::ArrayXd cosmicErrHist = cosmic.GetEigen(cosmic.Livetime(), kLivetime);
38 
39  TFeldmanCousins fc(0.6827);
40  for(int i = 1; i<= cosmicErrHist.size()-2; i++){
41  double y = cosmicErrHist[i];
42  double errup = 0;
43  double errlow = 0;
44  if(y!=0){
45  if ( y < 50 ) errlow = (y - fc.CalculateLowerLimit(y,0))/y;
46  else errlow = (sqrt(y))/y;
47  if ( y < 30 ) errup = (fc.CalculateUpperLimit(y,0)-y)/y;
48  else errup = (sqrt(y))/y;
49  }
50  fBinErrors.push_back({errup, errlow});
51  }
52  }
53 
54  }
std::optional< Spectrum > fCosmic
T sqrt(T number)
Definition: d0nt_math.hpp:156
const XML_Char const XML_Char * data
Definition: expat.h:268
Sum up livetimes from individual cosmic triggers.
std::vector< std::pair< double, double > > fBinErrors
ana::SingleSampleExperiment::SingleSampleExperiment ( const IPrediction pred,
const Spectrum data 
)
inline

In MC studies you might not want to bother with cosmics.

Definition at line 35 of file SingleSampleExperiment.h.

References ana::SystShifts::Nominal().

37  : fMC(pred), fData(data)
38  {
39  }
const XML_Char const XML_Char * data
Definition: expat.h:268
ana::SingleSampleExperiment::~SingleSampleExperiment ( )
virtual

Definition at line 57 of file SingleSampleExperiment.cxx.

58  {
59  }
ana::SingleSampleExperiment::SingleSampleExperiment ( const SingleSampleExperiment )
delete
ana::SingleSampleExperiment::SingleSampleExperiment ( SingleSampleExperiment &&  s)
inline

Definition at line 59 of file SingleSampleExperiment.h.

References calc.

60  : fMC(s.fMC), fData(std::move(s.fData)), fCosmic(std::move(s.fCosmic)), fCosmicScaleError(s.fCosmicScaleError)
61  {
62  s.fMC = nullptr;
63  };
std::optional< Spectrum > fCosmic
const XML_Char * s
Definition: expat.h:262

Member Function Documentation

double ana::SingleSampleExperiment::ChiSq ( osc::IOscCalcAdjustable osc,
const SystShifts syst = SystShifts::Nominal() 
) const
overridevirtual

Reimplemented from ana::IExperiment.

Reimplemented in ana::BigChi2SingleSampleExperiment, and ana::CovMxExperiment.

Definition at line 111 of file SingleSampleExperiment.cxx.

References fData, ana::Spectrum::GetEigen(), ana::LogLikelihood(), ana::Spectrum::POT(), plot_validation_datamc::pred, and PredHistIncCosmics().

Referenced by ana::BigChi2SingleSampleExperiment::ChiSq().

113  {
114  Eigen::ArrayXd pred = PredHistIncCosmics(calc, syst);
115  Eigen::ArrayXd data = fData.GetEigen(fData.POT());
116 
117  // full namespace qualification to avoid degeneracy with method inherited from IExperiment
118  return ana::LogLikelihood(pred, data);
119  }
const Eigen::ArrayXd & GetEigen() const
NB these don&#39;t have POT scaling. For expert high performance ops only!
Definition: Spectrum.h:188
osc::OscCalcDumb calc
double LogLikelihood(const Eigen::ArrayXd &ea, const Eigen::ArrayXd &oa, bool useOverflow)
The log-likelihood formula from the PDG.
Definition: EigenUtils.cxx:36
const XML_Char const XML_Char * data
Definition: expat.h:268
double POT() const
Definition: Spectrum.h:219
Eigen::Array< T, Eigen::Dynamic, 1 > PredHistIncCosmics(osc::_IOscCalc< T > *calc, const SystShifts &syst) const
const IPrediction* ana::SingleSampleExperiment::GetPrediction ( ) const
inline
std::unique_ptr< SingleSampleExperiment > ana::SingleSampleExperiment::LoadFrom ( TDirectory *  dir,
const std::string &  name 
)
static

Definition at line 154 of file SingleSampleExperiment.cxx.

References ana::assert(), dir, ana::Spectrum::LoadFrom(), ana::LoadFrom< IPrediction >(), mc, runNovaSAM::release, and runNovaSAM::ret.

155  {
156  dir = dir->GetDirectory(name.c_str()); // switch to subdir
157  assert(dir);
158 
159  TObjString* ptag = (TObjString*)dir->Get("type");
160  assert(ptag);
161  assert(ptag->GetString() == "SingleSampleExperiment");
162  delete ptag;
163 
164  const IPrediction* mc = ana::LoadFrom<IPrediction>(dir, "mc").release();
165  const std::unique_ptr<Spectrum> data = Spectrum::LoadFrom(dir, "data");
166 
167  auto ret = std::make_unique<SingleSampleExperiment>(mc, *data);
168 
169  if(dir->Get("cosmic")){
170  ret->fCosmic = *ana::LoadFrom<Spectrum>(dir, "cosmic");
171  }
172 
173  delete dir;
174 
175  return ret;
176  }
const XML_Char * name
Definition: expat.h:151
const XML_Char const XML_Char * data
Definition: expat.h:268
static std::unique_ptr< Spectrum > LoadFrom(TDirectory *dir, const std::string &name)
Definition: Spectrum.cxx:546
std::unique_ptr< IPrediction > LoadFrom< IPrediction >(TDirectory *dir, const std::string &label)
Definition: IPrediction.cxx:18
TDirectory * dir
Definition: macro.C:5
assert(nhit_max >=nhit_nbins)
stan::math::var ana::SingleSampleExperiment::LogLikelihood ( osc::IOscCalcAdjustableStan osc,
const SystShifts syst = kNoShift 
) const
overridevirtual

Reimplemented from ana::IExperiment.

Definition at line 122 of file SingleSampleExperiment.cxx.

References fData, ana::Spectrum::GetEigen(), ana::LogLikelihood(), ana::Spectrum::POT(), plot_validation_datamc::pred, and PredHistIncCosmics().

Referenced by test_stanfit_systpulls().

124  {
126  Eigen::ArrayXd data = fData.GetEigen(fData.POT());
127 
128  // fully-qualified so that we get the one in StanUtils.h
129  return ana::LogLikelihood(pred, data) / -2.; // LogLikelihood(), confusingly, returns chi2=-2*LL
130  }
const Eigen::ArrayXd & GetEigen() const
NB these don&#39;t have POT scaling. For expert high performance ops only!
Definition: Spectrum.h:188
double LogLikelihood(const Eigen::ArrayXd &ea, const Eigen::ArrayXd &oa, bool useOverflow)
The log-likelihood formula from the PDG.
Definition: EigenUtils.cxx:36
const XML_Char const XML_Char * data
Definition: expat.h:268
double POT() const
Definition: Spectrum.h:219
Eigen::Array< stan::math::var, Eigen::Dynamic, 1 > ArrayXstan
Definition: StanUtils.h:7
Eigen::Array< T, Eigen::Dynamic, 1 > PredHistIncCosmics(osc::_IOscCalc< T > *calc, const SystShifts &syst) const
SingleSampleExperiment& ana::SingleSampleExperiment::operator= ( const SingleSampleExperiment )
delete
template<typename T >
template Eigen::ArrayXstan ana::SingleSampleExperiment::PredHistIncCosmics ( osc::_IOscCalc< T > *  calc,
const SystShifts syst 
) const
protected

Definition at line 64 of file SingleSampleExperiment.cxx.

References calc, fBinErrors, fCosmic, fCosmicScaleError, fData, fMC, fPoissonError, ana::Spectrum::GetEigen(), ana::Spectrum::GetEigenStan(), ana::SystShifts::GetShift(), ana::Spectrum::HasStan(), MECModelEnuComparisons::i, ana::kLivetime, ana::Spectrum::Livetime(), ana::Spectrum::POT(), ana::IPrediction::PredictSyst(), ana::SystShifts::RemoveShift(), scale, sigma(), and T.

Referenced by ChiSq(), and LogLikelihood().

66  {
67  using EigenArray = Eigen::Array<T, Eigen::Dynamic, 1>;
68 
69  SystShifts systNoCosmic = syst;
70  systNoCosmic.RemoveShift(&kCosmicBkgScaleSyst);
71 
72  Spectrum specPred = fMC->PredictSyst(calc, systNoCosmic);
73  EigenArray apred;
74  if (specPred.HasStan())
75  {
76  if constexpr (std::is_same_v<T, stan::math::var>)
77  apred = specPred.GetEigenStan(fData.POT());
78  }
79  else
80  apred = specPred.GetEigen(fData.POT());
81 
82  if(fCosmic.has_value()){
83  // note the conversion here from double to stan::math::var if T is stan::math::var
84  EigenArray acosm = fCosmic->GetEigen(fData.Livetime(), kLivetime);
85  if(fCosmicScaleError != 0 && !fPoissonError){
86  const T scale = 1 + syst.GetShift(&kCosmicBkgScaleSyst) * fCosmicScaleError;
87  acosm *= scale;
88  }
89  if(fPoissonError){
90  T sigma = syst.GetShift(&kCosmicBkgScaleSyst);
91  for(int i = 1; i<= acosm.size()-2; i++){
92  if (sigma>0) acosm[i] *= (1+sigma*fBinErrors[i-1].first);
93  if (sigma<0) acosm[i] *= (1+sigma*fBinErrors[i-1].second);
94  }
95  }
96 
97  apred += acosm;
98  }
99 
100  return apred;
101  }
std::optional< Spectrum > fCosmic
const CosmicBkgScaleSyst kCosmicBkgScaleSyst
Double_t scale
Definition: plot.C:25
if(dump)
virtual Spectrum PredictSyst(osc::IOscCalc *calc, const SystShifts &syst) const
Definition: IPrediction.cxx:49
std::vector< std::pair< double, double > > fBinErrors
std::vector< float > Spectrum
Definition: Constants.h:570
double sigma(TH1F *hist, double percentile)
std::vector< double > POT
double T
Definition: Xdiff_gwt.C:5
double Livetime() const
Seconds. For informational purposes only. No calculations use this.
Definition: Spectrum.h:222
void ana::SingleSampleExperiment::SaveTo ( TDirectory *  dir,
const std::string &  name 
) const
overridevirtual

Reimplemented from ana::IExperiment.

Definition at line 133 of file SingleSampleExperiment.cxx.

References dir, fCosmic, fData, fMC, ana::IPrediction::SaveTo(), ana::Spectrum::SaveTo(), and tmp.

134  {
135  TDirectory* tmp = dir;
136 
137  dir = dir->mkdir(name.c_str()); // switch to subdir
138  dir->cd();
139 
140  TObjString("SingleSampleExperiment").Write("type");
141 
142  fMC->SaveTo(dir, "mc");
143  fData.SaveTo(dir, "data");
144 
145  if(fCosmic.has_value()) fCosmic->SaveTo(dir, "cosmic");
146 
147  dir->Write();
148  delete dir;
149 
150  tmp->cd();
151  }
const XML_Char * name
Definition: expat.h:151
std::optional< Spectrum > fCosmic
Float_t tmp
Definition: plot.C:36
virtual void SaveTo(TDirectory *dir, const std::string &name) const
void SaveTo(TDirectory *dir, const std::string &name) const
Definition: Spectrum.cxx:517
TDirectory * dir
Definition: macro.C:5

Member Data Documentation

std::vector< std::pair <double, double> > ana::SingleSampleExperiment::fBinErrors
protected

Definition at line 74 of file SingleSampleExperiment.h.

Referenced by PredHistIncCosmics(), and SingleSampleExperiment().

std::optional<Spectrum> ana::SingleSampleExperiment::fCosmic
protected
double ana::SingleSampleExperiment::fCosmicScaleError
protected

Definition at line 72 of file SingleSampleExperiment.h.

Referenced by ana::CovMxExperiment::ChiSq(), and PredHistIncCosmics().

Spectrum ana::SingleSampleExperiment::fData
protected
const IPrediction* ana::SingleSampleExperiment::fMC
protected
bool ana::SingleSampleExperiment::fPoissonError
protected

Definition at line 73 of file SingleSampleExperiment.h.

Referenced by PredHistIncCosmics(), and SingleSampleExperiment().


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