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-08-04/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::IOscCalculatorAdjustable *osc, const SystShifts &syst=SystShifts::Nominal()) const override
 
virtual void Derivative (osc::IOscCalculator *calc, const SystShifts &shift, std::unordered_map< const ISyst *, double > &dch) const override
 
virtual stan::math::var LogLikelihood (osc::_IOscCalculatorAdjustable< stan::math::var > *osc, const SystShifts &syst=SystShifts::Nominal()) const override
 
virtual void SaveTo (TDirectory *dir) const override
 
 SingleSampleExperiment (const SingleSampleExperiment &)=delete
 
SingleSampleExperimentoperator= (const SingleSampleExperiment &)=delete
 
 SingleSampleExperiment (SingleSampleExperiment &&s)
 
virtual stan::math::var LogLikelihood (osc::IOscCalculatorAdjustableStan *osc, const SystShifts &syst=kNoShift) const
 

Static Public Member Functions

static std::unique_ptr< SingleSampleExperimentLoadFrom (TDirectory *dir)
 

Protected Member Functions

TH1D * PredHistIncCosmics (osc::IOscCalculator *calc, const SystShifts &syst) const
 

Protected Attributes

const IPredictionfMC
 
Spectrum fData
 
TH1D * fCosmic
 
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 22 of file SingleSampleExperiment.cxx.

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

27  : fMC(pred), fData(data),
28  fCosmic(cosmic.ToTH1(data.Livetime(), kLivetime)),
29  fCosmicScaleError(cosmicScaleError),
30  fPoissonError(PoissonError)
31  {
32  // Fill the vector of errors in case of PoissonError = true
33  if(fPoissonError){
34  TH1D* cosmicErrHist = cosmic.ToTH1(cosmic.Livetime(), kLivetime);
35 
36  TFeldmanCousins fc(0.6827);
37  for(int i = 1; i<= cosmicErrHist->GetNbinsX(); i++){
38  double y = cosmicErrHist->GetBinContent(i);
39  double errup = 0;
40  double errlow = 0;
41  if(y!=0){
42  if ( y < 50 ) errlow = (y - fc.CalculateLowerLimit(y,0))/y;
43  else errlow = (sqrt(y))/y;
44  if ( y < 30 ) errup = (fc.CalculateUpperLimit(y,0)-y)/y;
45  else errup = (sqrt(y))/y;
46  }
47  fBinErrors.push_back({errup, errlow});
48  }
49 
50  HistCache::Delete(cosmicErrHist);
51  }
52 
53  }
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.
static void Delete(TH1D *&h)
Definition: HistCache.cxx:220
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 calc, dir, ana::LoadFrom(), ana::LogLikelihood(), ana::SystShifts::Nominal(), ana::ISyst::operator=(), and ana::SaveTo().

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

Definition at line 56 of file SingleSampleExperiment.cxx.

References ana::HistCache::Delete(), fCosmic, and PredHistIncCosmics().

57  {
59  }
static void Delete(TH1D *&h)
Definition: HistCache.cxx:220
ana::SingleSampleExperiment::SingleSampleExperiment ( const SingleSampleExperiment )
delete
ana::SingleSampleExperiment::SingleSampleExperiment ( SingleSampleExperiment &&  s)
inline

Definition at line 61 of file SingleSampleExperiment.h.

62  : fMC(s.fMC), fData(std::move(s.fData)), fCosmic(s.fCosmic), fCosmicScaleError(s.fCosmicScaleError)
63  {
64  s.fMC = nullptr;
65  s.fCosmic = nullptr;
66  s.fCosmicScaleError = 0;
67  };
const XML_Char * s
Definition: expat.h:262

Member Function Documentation

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

Reimplemented from ana::IExperiment.

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

Definition at line 94 of file SingleSampleExperiment.cxx.

References ana::HistCache::Delete(), Derivative(), fData, chisquared::hpred, ana::LogLikelihood(), ana::Spectrum::POT(), PredHistIncCosmics(), and ana::Spectrum::ToTH1().

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

96  {
97  TH1D* hpred = PredHistIncCosmics(calc, syst);
98  TH1D* hdata = fData.ToTH1(fData.POT());
99 
100  // full namespace qualification to avoid degeneracy with method inherited from IExperiment
101  const double ll = ana::LogLikelihood(hpred, hdata);
102 
103  HistCache::Delete(hpred);
104  HistCache::Delete(hdata);
105 
106  return ll;
107  }
TH1D * ToTH1(double exposure, Color_t col=kBlack, Style_t style=kSolid, EExposureType expotype=kPOT, EBinType bintype=kBinContent) const
Histogram made from this Spectrum, scaled to some exposure.
Definition: Spectrum.cxx:564
stan::math::var LogLikelihood(const std::vector< stan::math::var > &exp, const TH1 *obs)
Variant that handles the prediction in the form of Stan vars.
Definition: StanUtils.cxx:11
static void Delete(TH1D *&h)
Definition: HistCache.cxx:220
double POT() const
Definition: Spectrum.h:263
osc::OscCalculatorDumb calc
TH1D * PredHistIncCosmics(osc::IOscCalculator *calc, const SystShifts &syst) const
void ana::SingleSampleExperiment::Derivative ( osc::IOscCalculator calc,
const SystShifts shift,
std::unordered_map< const ISyst *, double > &  dch 
) const
overridevirtual

Reimplemented from ana::IExperiment.

Definition at line 111 of file SingleSampleExperiment.cxx.

References ana::HistCache::Delete(), ana::IPrediction::Derivative(), exit(), fBinErrors, fCosmic, fCosmicScaleError, fData, fMC, fPoissonError, ana::SystShifts::GetShift(), chisquared::hpred, MECModelEnuComparisons::i, it, ana::LogLikelihoodDerivative(), pot, ana::Spectrum::POT(), PredHistIncCosmics(), sigma(), and ana::Spectrum::ToTH1().

Referenced by ChiSq().

114  {
115 
116  const double pot = fData.POT();
117 
118  std::unordered_map<const ISyst*, std::vector<double>> dp;
119  for(auto it: dchi) dp[it.first] = {};
120  fMC->Derivative(calc, shift, pot, dp);
121 
122  if(dp.empty()){ // prediction doesn't implement derivatives
123  dchi.clear(); // pass on that info to our caller
124  return;
125  }
126 
127  TH1D* hpred = PredHistIncCosmics(calc, shift);
128  TH1D* hdata = fData.ToTH1(pot);
129 
130  for(auto& it: dchi){
131  if(it.first != &kCosmicBkgScaleSyst){
132  it.second += LogLikelihoodDerivative(hpred, hdata, dp[it.first]);
133  }
134  else{
135  const unsigned int N = fCosmic->GetNbinsX()+2;
136  const double* ca = fCosmic->GetArray();
137  std::vector<double> cosErr(N);
138  if(fPoissonError){
139  double sigma = shift.GetShift(&kCosmicBkgScaleSyst);
140  for(unsigned int i = 1; i<= fBinErrors.size(); i++){
141  double err = 0;
142  if(sigma<0) err = fBinErrors[i-1].second;
143  if(sigma>0) err = fBinErrors[i-1].first;
144  cosErr[i] = ca[i]*err;
145  }
146  }
147  else {
148  for(unsigned int i = 1; i <= N; ++i) cosErr[i] = ca[i]*fCosmicScaleError;
149  }
150  it.second += LogLikelihoodDerivative(hpred, hdata, cosErr);
151  }
152  }
153  exit(0);
154  HistCache::Delete(hpred);
155  HistCache::Delete(hdata);
156  }
set< int >::iterator it
TH1D * ToTH1(double exposure, Color_t col=kBlack, Style_t style=kSolid, EExposureType expotype=kPOT, EBinType bintype=kBinContent) const
Histogram made from this Spectrum, scaled to some exposure.
Definition: Spectrum.cxx:564
virtual void Derivative(osc::IOscCalculator *calc, const SystShifts &shift, double pot, std::unordered_map< const ISyst *, std::vector< double >> &dchi) const
Definition: IPrediction.h:92
const CosmicBkgScaleSyst kCosmicBkgScaleSyst
double LogLikelihoodDerivative(double e, double o, double dedx)
Definition: Utilities.cxx:154
static void Delete(TH1D *&h)
Definition: HistCache.cxx:220
#define pot
std::vector< std::pair< double, double > > fBinErrors
double POT() const
Definition: Spectrum.h:263
double sigma(TH1F *hist, double percentile)
TH1D * PredHistIncCosmics(osc::IOscCalculator *calc, const SystShifts &syst) const
exit(0)
std::unique_ptr< SingleSampleExperiment > ana::SingleSampleExperiment::LoadFrom ( TDirectory *  dir)
static

Definition at line 191 of file SingleSampleExperiment.cxx.

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

Referenced by ana::LoadFrom< IExperiment >().

192  {
193  TObjString* ptag = (TObjString*)dir->Get("type");
194  assert(ptag);
195  assert(ptag->GetString() == "SingleSampleExperiment");
196 
197  assert(dir->GetDirectory("mc"));
198  assert(dir->GetDirectory("data"));
199 
200 
201  const IPrediction* mc = ana::LoadFrom<IPrediction>(dir->GetDirectory("mc")).release();
202  const std::unique_ptr<Spectrum> data = Spectrum::LoadFrom(dir->GetDirectory("data"));
203 
204  TH1D* cosmic = 0;
205  if(dir->Get("cosmic")) cosmic = (TH1D*)dir->Get("cosmic");
206 
207  auto ret = std::make_unique<SingleSampleExperiment>(mc, *data);
208  if(cosmic) ret->fCosmic = cosmic;
209  return ret;
210  }
const XML_Char const XML_Char * data
Definition: expat.h:268
Sum up livetimes from individual cosmic triggers.
std::unique_ptr< IPrediction > LoadFrom< IPrediction >(TDirectory *dir)
Definition: IPrediction.cxx:34
TDirectory * dir
Definition: macro.C:5
assert(nhit_max >=nhit_nbins)
static std::unique_ptr< Spectrum > LoadFrom(TDirectory *dir)
Definition: Spectrum.cxx:1066
virtual stan::math::var ana::IExperiment::LogLikelihood ( osc::IOscCalculatorAdjustableStan osc,
const SystShifts syst = kNoShift 
) const
inlinevirtualinherited

Reimplemented in test::GaussQuadExperiment.

Definition at line 38 of file IExperiment.h.

References ana::assert(), dir, and ana::IExperiment::SaveTo().

Referenced by ana::StanFitter::log_prob().

40  {
41  assert(false && "unimplemented");
42  return 0;
43  };
assert(nhit_max >=nhit_nbins)
stan::math::var ana::SingleSampleExperiment::LogLikelihood ( osc::_IOscCalculatorAdjustable< stan::math::var > *  osc,
const SystShifts syst = SystShifts::Nominal() 
) const
overridevirtual

Definition at line 159 of file SingleSampleExperiment.cxx.

References ana::HistCache::Delete(), fData, fMC, ana::SystShifts::IsNominal(), ana::LogLikelihood(), ana::Spectrum::POT(), plot_validation_datamc::pred, ana::IPrediction::Predict(), ana::IPrediction::PredictSyst(), and ana::Spectrum::ToTH1().

Referenced by test_stanfit_systpulls().

161  {
162  auto pred = syst.IsNominal()
163  ? fMC->Predict(osc).ToBins(fData.POT())
164  : fMC->PredictSyst(osc, syst).ToBins(fData.POT());
165  TH1D* hdata = fData.ToTH1(fData.POT());
166 
167  // fully-qualified so that we get the one in StanUtils.h
168  auto ll = ana::LogLikelihood(pred, hdata) / -2.; // LogLikelihood(), confusingly, returns chi2=-2*LL
169 
170  HistCache::Delete(hdata);
171 
172  return ll;
173  }
TH1D * ToTH1(double exposure, Color_t col=kBlack, Style_t style=kSolid, EExposureType expotype=kPOT, EBinType bintype=kBinContent) const
Histogram made from this Spectrum, scaled to some exposure.
Definition: Spectrum.cxx:564
stan::math::var LogLikelihood(const std::vector< stan::math::var > &exp, const TH1 *obs)
Variant that handles the prediction in the form of Stan vars.
Definition: StanUtils.cxx:11
virtual Spectrum Predict(osc::IOscCalculator *calc) const =0
virtual Spectrum PredictSyst(osc::IOscCalculator *calc, const SystShifts &syst) const
Definition: IPrediction.cxx:86
static void Delete(TH1D *&h)
Definition: HistCache.cxx:220
double POT() const
Definition: Spectrum.h:263
SingleSampleExperiment& ana::SingleSampleExperiment::operator= ( const SingleSampleExperiment )
delete
TH1D * ana::SingleSampleExperiment::PredHistIncCosmics ( osc::IOscCalculator calc,
const SystShifts syst 
) const
protected

Definition at line 63 of file SingleSampleExperiment.cxx.

References ana::HistCache::Copy(), ana::HistCache::Delete(), fBinErrors, fCosmic, fCosmicScaleError, fData, fMC, fPoissonError, ana::SystShifts::GetShift(), chisquared::hpred, MECModelEnuComparisons::i, ana::Spectrum::POT(), plot_validation_datamc::pred, ana::IPrediction::PredictSyst(), scale, ana::SystShifts::SetShift(), sigma(), and ana::Spectrum::ToTH1().

Referenced by ChiSq(), Derivative(), and ~SingleSampleExperiment().

65  {
66  SystShifts systNoCosmic = syst;
67  systNoCosmic.SetShift(&kCosmicBkgScaleSyst, 0);
68 
69  const Spectrum pred = fMC->PredictSyst(calc, systNoCosmic);
70  TH1D* hpred = pred.ToTH1(fData.POT());
71  if(fCosmic){
72  if(fCosmicScaleError != 0 && !fPoissonError){
73  const double scale = 1 + syst.GetShift(&kCosmicBkgScaleSyst) * fCosmicScaleError;
74  hpred->Add(fCosmic, scale);
75  }
76  if(fPoissonError){
77  TH1D* hcosm = HistCache::Copy(fCosmic);
78  double sigma = syst.GetShift(&kCosmicBkgScaleSyst);
79  for(int i = 1; i<= hcosm->GetNbinsX(); i++){
80  if (sigma>0) hcosm->SetBinContent(i, hcosm->GetBinContent(i)*(1+sigma*fBinErrors[i-1].first));
81  if (sigma<0) hcosm->SetBinContent(i, hcosm->GetBinContent(i)*(1+sigma*fBinErrors[i-1].second));
82  }
83  hpred->Add(hcosm);
84  HistCache::Delete(hcosm);
85  }
86  else{
87  hpred->Add(fCosmic);
88  }
89  }
90  return hpred;
91  }
static TH1D * Copy(const TH1D *h)
Definition: HistCache.cxx:138
const CosmicBkgScaleSyst kCosmicBkgScaleSyst
virtual Spectrum PredictSyst(osc::IOscCalculator *calc, const SystShifts &syst) const
Definition: IPrediction.cxx:86
Double_t scale
Definition: plot.C:25
static void Delete(TH1D *&h)
Definition: HistCache.cxx:220
std::vector< std::pair< double, double > > fBinErrors
double POT() const
Definition: Spectrum.h:263
double sigma(TH1F *hist, double percentile)
void ana::SingleSampleExperiment::SaveTo ( TDirectory *  dir) const
overridevirtual

Reimplemented from ana::IExperiment.

Definition at line 175 of file SingleSampleExperiment.cxx.

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

176  {
177  TDirectory* tmp = dir;
178 
179  dir->cd();
180  TObjString("SingleSampleExperiment").Write("type");
181 
182  fMC->SaveTo(dir->mkdir("mc"));
183  fData.SaveTo(dir->mkdir("data"));
184 
185  if(fCosmic) fCosmic->Write("cosmic");
186 
187  tmp->cd();
188  }
virtual void SaveTo(TDirectory *dir) const
Float_t tmp
Definition: plot.C:36
TDirectory * dir
Definition: macro.C:5
void SaveTo(TDirectory *dir) const
Definition: Spectrum.cxx:1040

Member Data Documentation

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

Definition at line 77 of file SingleSampleExperiment.h.

Referenced by Derivative(), PredHistIncCosmics(), and SingleSampleExperiment().

TH1D* ana::SingleSampleExperiment::fCosmic
protected
double ana::SingleSampleExperiment::fCosmicScaleError
protected
Spectrum ana::SingleSampleExperiment::fData
protected
const IPrediction* ana::SingleSampleExperiment::fMC
protected
bool ana::SingleSampleExperiment::fPoissonError
protected

Definition at line 76 of file SingleSampleExperiment.h.

Referenced by Derivative(), PredHistIncCosmics(), and SingleSampleExperiment().


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