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

#include "/cvmfs/nova-development.opensciencegrid.org/novasoft/releases/N20-08-14/CAFAna/xsec_tuning/Ana2020/tuning/MECTuningUtils.h"

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

Public Member Functions

 BigChi2SingleSampleExperiment (const IPrediction *pred, const Spectrum &data, double scaleFactor=1)
 
double ChiSq (osc::IOscCalcAdjustable *osc, const SystShifts &syst=SystShifts::Nominal()) const override
 
virtual void Derivative (osc::IOscCalc *calc, const SystShifts &shift, std::unordered_map< const ISyst *, double > &dch) const override
 
virtual stan::math::var LogLikelihood (osc::_IOscCalcAdjustable< stan::math::var > *osc, const SystShifts &syst=SystShifts::Nominal()) const override
 
virtual stan::math::var LogLikelihood (osc::IOscCalcAdjustableStan *osc, const SystShifts &syst=kNoShift) const
 
virtual void SaveTo (TDirectory *dir, const std::string &name) const override
 

Static Public Member Functions

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

Protected Member Functions

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

Protected Attributes

const IPredictionfMC
 
Spectrum fData
 
TH1D * fCosmic
 
double fCosmicScaleError
 
bool fPoissonError
 
std::vector< std::pair< double, double > > fBinErrors
 

Private Attributes

double fScaleFactor
 

Detailed Description

Definition at line 127 of file MECTuningUtils.h.

Constructor & Destructor Documentation

ana::BigChi2SingleSampleExperiment::BigChi2SingleSampleExperiment ( const IPrediction pred,
const Spectrum data,
double  scaleFactor = 1 
)
inline

Definition at line 130 of file MECTuningUtils.h.

133  : SingleSampleExperiment(pred, data), fScaleFactor(scaleFactor)
134  {}
SingleSampleExperiment(const IPrediction *pred, const Spectrum &data, const Spectrum &cosmic, double cosmicScaleError=0, bool PoissonError=false)
const XML_Char const XML_Char * data
Definition: expat.h:268

Member Function Documentation

double ana::BigChi2SingleSampleExperiment::ChiSq ( osc::IOscCalcAdjustable osc,
const SystShifts syst = SystShifts::Nominal() 
) const
inlineoverridevirtual

Reimplemented from ana::SingleSampleExperiment.

Definition at line 136 of file MECTuningUtils.h.

References ana::SingleSampleExperiment::ChiSq(), and demo5::expt.

138  {
140  return fScaleFactor * expt.ChiSq(osc, syst);
141  }
SingleSampleExperiment(const IPrediction *pred, const Spectrum &data, const Spectrum &cosmic, double cosmicScaleError=0, bool PoissonError=false)
expt
Definition: demo5.py:34
void ana::SingleSampleExperiment::Derivative ( osc::IOscCalc calc,
const SystShifts shift,
std::unordered_map< const ISyst *, double > &  dch 
) const
overridevirtualinherited

Reimplemented from ana::IExperiment.

Definition at line 111 of file SingleSampleExperiment.cxx.

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

Referenced by ana::SingleSampleExperiment::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
const CosmicBkgScaleSyst kCosmicBkgScaleSyst
TH1D * PredHistIncCosmics(osc::IOscCalc *calc, const SystShifts &syst) const
virtual void Derivative(osc::IOscCalc *calc, const SystShifts &shift, double pot, std::unordered_map< const ISyst *, std::vector< double >> &dchi) const
Definition: IPrediction.h:92
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)
exit(0)
std::unique_ptr< SingleSampleExperiment > ana::SingleSampleExperiment::LoadFrom ( TDirectory *  dir,
const std::string &  name 
)
staticinherited

Definition at line 196 of file SingleSampleExperiment.cxx.

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

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

197  {
198  dir = dir->GetDirectory(name.c_str()); // switch to subdir
199  assert(dir);
200 
201  TObjString* ptag = (TObjString*)dir->Get("type");
202  assert(ptag);
203  assert(ptag->GetString() == "SingleSampleExperiment");
204  delete ptag;
205 
206  const IPrediction* mc = ana::LoadFrom<IPrediction>(dir, "mc").release();
207  const std::unique_ptr<Spectrum> data = Spectrum::LoadFrom(dir, "data");
208 
209  TH1D* cosmic = 0;
210  if(dir->Get("cosmic")) cosmic = (TH1D*)dir->Get("cosmic");
211 
212  delete dir;
213 
214  auto ret = std::make_unique<SingleSampleExperiment>(mc, *data);
215  if(cosmic) ret->fCosmic = cosmic;
216  return ret;
217  }
const XML_Char * name
Definition: expat.h:151
const XML_Char const XML_Char * data
Definition: expat.h:268
Sum up livetimes from individual cosmic triggers.
static std::unique_ptr< Spectrum > LoadFrom(TDirectory *dir, const std::string &name)
Definition: Spectrum.cxx:1071
std::unique_ptr< IPrediction > LoadFrom< IPrediction >(TDirectory *dir, const std::string &label)
Definition: IPrediction.cxx:33
TDirectory * dir
Definition: macro.C:5
assert(nhit_max >=nhit_nbins)
virtual stan::math::var ana::IExperiment::LogLikelihood ( osc::IOscCalcAdjustableStan 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::_IOscCalcAdjustable< stan::math::var > *  osc,
const SystShifts syst = SystShifts::Nominal() 
) const
overridevirtualinherited

Definition at line 159 of file SingleSampleExperiment.cxx.

References ana::HistCache::Delete(), ana::SingleSampleExperiment::fData, ana::SingleSampleExperiment::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
virtual Spectrum Predict(osc::IOscCalc *calc) const =0
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
virtual Spectrum PredictSyst(osc::IOscCalc *calc, const SystShifts &syst) const
Definition: IPrediction.cxx:85
double POT() const
Definition: Spectrum.h:263
TH1D * ana::SingleSampleExperiment::PredHistIncCosmics ( osc::IOscCalc calc,
const SystShifts syst 
) const
protectedinherited

Definition at line 63 of file SingleSampleExperiment.cxx.

References ana::HistCache::Copy(), ana::HistCache::Delete(), ana::SingleSampleExperiment::fBinErrors, ana::SingleSampleExperiment::fCosmic, ana::SingleSampleExperiment::fCosmicScaleError, ana::SingleSampleExperiment::fData, ana::SingleSampleExperiment::fMC, ana::SingleSampleExperiment::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 ana::SingleSampleExperiment::ChiSq(), ana::SingleSampleExperiment::Derivative(), and ana::SingleSampleExperiment::~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
Double_t scale
Definition: plot.C:25
static void Delete(TH1D *&h)
Definition: HistCache.cxx:220
virtual Spectrum PredictSyst(osc::IOscCalc *calc, const SystShifts &syst) const
Definition: IPrediction.cxx:85
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 std::string &  name 
) const
overridevirtualinherited

Reimplemented from ana::IExperiment.

Definition at line 175 of file SingleSampleExperiment.cxx.

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

176  {
177  TDirectory* tmp = dir;
178 
179  dir = dir->mkdir(name.c_str()); // switch to subdir
180  dir->cd();
181 
182  TObjString("SingleSampleExperiment").Write("type");
183 
184  fMC->SaveTo(dir, "mc");
185  fData.SaveTo(dir, "data");
186 
187  if(fCosmic) fCosmic->Write("cosmic");
188 
189  dir->Write();
190  delete dir;
191 
192  tmp->cd();
193  }
const XML_Char * name
Definition: expat.h:151
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:1040
TDirectory * dir
Definition: macro.C:5

Member Data Documentation

std::vector< std::pair <double, double> > ana::SingleSampleExperiment::fBinErrors
protectedinherited
TH1D* ana::SingleSampleExperiment::fCosmic
protectedinherited
double ana::SingleSampleExperiment::fCosmicScaleError
protectedinherited
Spectrum ana::SingleSampleExperiment::fData
protectedinherited
const IPrediction* ana::SingleSampleExperiment::fMC
protectedinherited
bool ana::SingleSampleExperiment::fPoissonError
protectedinherited
double ana::BigChi2SingleSampleExperiment::fScaleFactor
private

Definition at line 144 of file MECTuningUtils.h.


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