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/N19-12-08/CAFAna/Experiment/SingleSampleExperiment.h"

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

Public Member Functions

 SingleSampleExperiment (const IPrediction *pred, const Spectrum &data, const Spectrum &cosmic, double cosmicScaleError=0)
 
 SingleSampleExperiment (const IPrediction *pred, const Spectrum &data, const TH1D *cosmic, double cosmicScaleError=0, double fromlivetime=1., double topotlivetime=1.)
 Fallback to manual cosmic scaling. More...
 
 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 void SaveTo (TDirectory *dir) const override
 
 SingleSampleExperiment (const SingleSampleExperiment &)=delete
 
SingleSampleExperimentoperator= (const SingleSampleExperiment &)=delete
 
 SingleSampleExperiment (SingleSampleExperiment &&s)
 

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
 

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 
)
Parameters
predSource of oscillated MC beam predictions
dataData spectrum to compare to
cosmicCosmic ray background component
cosmicScaleErrorfractional uncertainty on cosmic normalization

Definition at line 18 of file SingleSampleExperiment.cxx.

22  : fMC(pred), fData(data),
23  fCosmic(cosmic.ToTH1(data.Livetime(), kLivetime)),
24  fCosmicScaleError(cosmicScaleError)
25  {
26  }
const XML_Char const XML_Char * data
Definition: expat.h:268
Sum up livetimes from individual cosmic triggers.
ana::SingleSampleExperiment::SingleSampleExperiment ( const IPrediction pred,
const Spectrum data,
const TH1D *  cosmic,
double  cosmicScaleError = 0,
double  fromlivetime = 1.,
double  topotlivetime = 1. 
)

Fallback to manual cosmic scaling.

cosmic must be already scaled so that its bin contents can be directly summed onto data. If you're using the out-of-time part of the beam spill, the easiest thing to do is to pass kTimingSidebandWeight as the weight argument when you fill it.

Definition at line 29 of file SingleSampleExperiment.cxx.

References om::cout, allTimeWatchdog::endl, and fCosmic.

36  fCosmicScaleError(cosmicScaleError)
37  {
38  //default for backward capacity
39  if(fromlivetime ==1 && tolivetime ==1){
40 // std::cout<< "Single Experiment Warning: using TH1D for cosmic, please double check POT/Livetime... And switch to Spectrum if possible."<<std::endl;
41  }
42  //other cases, always positive livetimes
43  else if(fromlivetime > 0 && tolivetime > 0){
44 
45  std::cout<<"Single Experiment rescaling cosmic Livetime from: "<<
46  fromlivetime<<" to: "<<
47  tolivetime<<
48  "Please use Spectrum for cosmic next time."<<std::endl;
49 
50  fCosmic->Scale(tolivetime/fromlivetime);
51  }
52  // others not allowed
53  else{
54  std::cout<<"Single Experiment Warning: please double check cosmic POT/Livetime setting."<<std::endl;
55  abort();
56  }
57 
58  }
static TH1D * Copy(const TH1D *h)
Definition: HistCache.cxx:175
const XML_Char const XML_Char * data
Definition: expat.h:268
Sum up livetimes from individual cosmic triggers.
OStream cout
Definition: OStream.cxx:6
ana::SingleSampleExperiment::SingleSampleExperiment ( const IPrediction pred,
const Spectrum data 
)
inline

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

Definition at line 47 of file SingleSampleExperiment.h.

References calc, dir, ana::LoadFrom(), ana::SystShifts::Nominal(), ana::ISyst::operator=(), ana::SaveTo(), plotSysts::shift, and plotSysts::syst.

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

Definition at line 61 of file SingleSampleExperiment.cxx.

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

62  {
64  }
static void Delete(TH1D *&h)
Definition: HistCache.cxx:215
ana::SingleSampleExperiment::SingleSampleExperiment ( const SingleSampleExperiment )
delete
ana::SingleSampleExperiment::SingleSampleExperiment ( SingleSampleExperiment &&  s)
inline

Definition at line 70 of file SingleSampleExperiment.h.

References plotSysts::syst.

71  : fMC(s.fMC), fData(std::move(s.fData)), fCosmic(s.fCosmic), fCosmicScaleError(s.fCosmicScaleError)
72  {
73  s.fMC = nullptr;
74  s.fCosmic = nullptr;
75  s.fCosmicScaleError = 0;
76  };
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

Implements ana::IChiSqExperiment.

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

Definition at line 92 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().

94  {
96  TH1D* hdata = fData.ToTH1(fData.POT());
97 
98  const double ll = LogLikelihood(hpred, hdata);
99 
100  HistCache::Delete(hpred);
101  HistCache::Delete(hdata);
102 
103  return ll;
104  }
osc::OscCalculatorDumb calc
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:553
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:215
double POT() const
Definition: Spectrum.h:263
TH1D * PredHistIncCosmics(osc::IOscCalculator *calc, const SystShifts &syst) const
string syst
Definition: plotSysts.py:176
void ana::SingleSampleExperiment::Derivative ( osc::IOscCalculator calc,
const SystShifts shift,
std::unordered_map< const ISyst *, double > &  dch 
) const
overridevirtual

Reimplemented from ana::IChiSqExperiment.

Definition at line 108 of file SingleSampleExperiment.cxx.

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

Referenced by ChiSq().

111  {
112  const double pot = fData.POT();
113 
114  std::unordered_map<const ISyst*, std::vector<double>> dp;
115  for(auto it: dchi) dp[it.first] = {};
116  fMC->Derivative(calc, shift, pot, dp);
117 
118  if(dp.empty()){ // prediction doesn't implement derivatives
119  dchi.clear(); // pass on that info to our caller
120  return;
121  }
122 
123  TH1D* hpred = PredHistIncCosmics(calc, shift);
124  TH1D* hdata = fData.ToTH1(pot);
125 
126  for(auto& it: dchi){
127  if(it.first != &kCosmicBkgScaleSyst){
128  it.second += LogLikelihoodDerivative(hpred, hdata, dp[it.first]);
129  }
130  else{
131  const unsigned int N = fCosmic->GetNbinsX()+2;
132  const double* ca = fCosmic->GetArray();
133  std::vector<double> cosErr(N);
134  for(unsigned int i = 0; i < N; ++i) cosErr[i] = ca[i]*fCosmicScaleError;
135  it.second += LogLikelihoodDerivative(hpred, hdata, cosErr);
136  }
137  }
138 
139  HistCache::Delete(hpred);
140  HistCache::Delete(hdata);
141  }
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:553
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:215
#define pot
double POT() const
Definition: Spectrum.h:263
TH1D * PredHistIncCosmics(osc::IOscCalculator *calc, const SystShifts &syst) const
std::unique_ptr< SingleSampleExperiment > ana::SingleSampleExperiment::LoadFrom ( TDirectory *  dir)
static

Definition at line 160 of file SingleSampleExperiment.cxx.

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

Referenced by getHists_FNEX(), and ana::LoadFrom< IChiSqExperiment >().

161  {
162  TObjString* ptag = (TObjString*)dir->Get("type");
163  assert(ptag);
164  assert(ptag->GetString() == "SingleSampleExperiment");
165 
166  assert(dir->GetDirectory("mc"));
167  assert(dir->GetDirectory("data"));
168 
169 
170  const IPrediction* mc = ana::LoadFrom<IPrediction>(dir->GetDirectory("mc")).release();
171  const std::unique_ptr<Spectrum> data = Spectrum::LoadFrom(dir->GetDirectory("data"));
172 
173  TH1D* cosmic = 0;
174  if(dir->Get("cosmic")) cosmic = (TH1D*)dir->Get("cosmic");
175 
176  auto ret = std::make_unique<SingleSampleExperiment>(mc, *data);
177  if(cosmic) ret->fCosmic = cosmic;
178  return ret;
179  }
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:38
TDirectory * dir
Definition: macro.C:5
static std::unique_ptr< Spectrum > LoadFrom(TDirectory *dir)
Definition: Spectrum.cxx:1055
SingleSampleExperiment& ana::SingleSampleExperiment::operator= ( const SingleSampleExperiment )
delete
TH1D * ana::SingleSampleExperiment::PredHistIncCosmics ( osc::IOscCalculator calc,
const SystShifts syst 
) const
protected

Definition at line 68 of file SingleSampleExperiment.cxx.

References fCosmic, fCosmicScaleError, fData, fMC, ana::SystShifts::GetShift(), chisquared::hpred, ana::Spectrum::POT(), plot_validation_datamc::pred, ana::IPrediction::PredictSyst(), scale, ana::SystShifts::SetShift(), plotSysts::syst, and ana::Spectrum::ToTH1().

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

70  {
71  SystShifts systNoCosmic = syst;
72  systNoCosmic.SetShift(&kCosmicBkgScaleSyst, 0);
73 
74  const Spectrum pred = fMC->PredictSyst(calc, systNoCosmic);
75 
76  TH1D* hpred = pred.ToTH1(fData.POT());
77 
78  if(fCosmic){
79  if(fCosmicScaleError != 0){
80  const double scale = 1 + syst.GetShift(&kCosmicBkgScaleSyst) * fCosmicScaleError;
81  hpred->Add(fCosmic, scale);
82  }
83  else{
84  hpred->Add(fCosmic);
85  }
86  }
87 
88  return hpred;
89  }
const CosmicBkgScaleSyst kCosmicBkgScaleSyst
virtual Spectrum PredictSyst(osc::IOscCalculator *calc, const SystShifts &syst) const
Definition: IPrediction.cxx:98
Double_t scale
Definition: plot.C:25
double POT() const
Definition: Spectrum.h:263
string syst
Definition: plotSysts.py:176
void ana::SingleSampleExperiment::SaveTo ( TDirectory *  dir) const
overridevirtual

Reimplemented from ana::IChiSqExperiment.

Definition at line 144 of file SingleSampleExperiment.cxx.

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

Referenced by joint_fit_make_experiments().

145  {
146  TDirectory* tmp = dir;
147 
148  dir->cd();
149  TObjString("SingleSampleExperiment").Write("type");
150 
151  fMC->SaveTo(dir->mkdir("mc"));
152  fData.SaveTo(dir->mkdir("data"));
153 
154  if(fCosmic) fCosmic->Write("cosmic");
155 
156  tmp->cd();
157  }
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:1029

Member Data Documentation

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

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