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

Simple record of shifts applied to systematic parameters. More...

#include "/cvmfs/nova-development.opensciencegrid.org/novasoft/releases/N20-01-22/CAFAna/Core/SystShifts.h"

Inheritance diagram for ana::SystShifts:
ana::GaussianPriorSystShifts

Public Member Functions

 SystShifts ()
 
 SystShifts (const ISyst *syst, double shift)
 
 SystShifts (const ISyst *syst, stan::math::var shift)
 
 SystShifts (const std::map< const ISyst *, double > &shifts)
 
 SystShifts (const std::map< const ISyst *, stan::math::var > &shifts)
 
virtual ~SystShifts ()=default
 
int ID () const
 SystShifts with the same set of systs should have the same ID. More...
 
std::vector< const ISyst * > ActiveSysts () const
 
virtual std::unique_ptr< SystShiftsCopy () const
 
bool IsNominal () const
 
void SetShift (const ISyst *syst, double shift, bool force=false)
 
void SetShift (const ISyst *syst, stan::math::var shift)
 
template<typename T = double>
T GetShift (const ISyst *syst) const
 
void ResetToNominal ()
 
double Penalty () const
 Penalty term for (frequentist) chi-squared fits. More...
 
virtual stan::math::var Prior () const
 
virtual stan::math::var LogPrior () const
 
void Shift (caf::SRProxy *sr, double &weight) const
 
void Shift (caf::SRNeutrinoProxy *sr, double &weight) const
 
std::string ShortName () const
 Brief description of component shifts, for printing to screen. More...
 
std::string LatexName () const
 Long description of component shifts, for plot labels. More...
 
void SaveTo (TDirectory *dir) const
 
template<>
double GetShift (const ISyst *syst) const
 
template<>
stan::math::var GetShift (const ISyst *syst) const
 

Static Public Member Functions

static SystShifts Nominal ()
 
static std::unique_ptr< SystShiftsLoadFrom (TDirectory *dir)
 

Protected Attributes

std::unordered_map< const ISyst *, double > fSystsDbl
 
std::unordered_map< const ISyst *, stan::math::varfSystsStan
 

Private Attributes

int fID
 

Static Private Attributes

static int fgNextID = 1
 The next unused ID. More...
 

Detailed Description

Simple record of shifts applied to systematic parameters.

Definition at line 20 of file SystShifts.h.

Constructor & Destructor Documentation

ana::SystShifts::SystShifts ( )

Definition at line 24 of file SystShifts.cxx.

25  : fID(fgNextID++)
26  {}
static int fgNextID
The next unused ID.
Definition: SystShifts.h:91
ana::SystShifts::SystShifts ( const ISyst syst,
double  shift 
)

Definition at line 29 of file SystShifts.cxx.

References fSystsDbl.

30  : fID(fgNextID++)
31  {
32  fSystsDbl.emplace(syst, shift);
33  }
static int fgNextID
The next unused ID.
Definition: SystShifts.h:91
std::unordered_map< const ISyst *, double > fSystsDbl
Definition: SystShifts.h:85
string syst
Definition: plotSysts.py:176
ana::SystShifts::SystShifts ( const ISyst syst,
stan::math::var  shift 
)

Definition at line 36 of file SystShifts.cxx.

References fSystsDbl, and fSystsStan.

37  : fID(fgNextID++)
38  {
39  fSystsStan.emplace(syst, shift);
40  // we're always going to maintain a "double" copy
41  // so that when the Stan cache gets invalidated we can still return something usable.
42  fSystsDbl.emplace(syst, util::GetValAs<double>(shift));
43  }
static int fgNextID
The next unused ID.
Definition: SystShifts.h:91
std::unordered_map< const ISyst *, stan::math::var > fSystsStan
Definition: SystShifts.h:86
std::unordered_map< const ISyst *, double > fSystsDbl
Definition: SystShifts.h:85
string syst
Definition: plotSysts.py:176
ana::SystShifts::SystShifts ( const std::map< const ISyst *, double > &  shifts)

Definition at line 46 of file SystShifts.cxx.

References fSystsDbl, and it.

47  : fID(fgNextID++)
48  {
49  for(auto it: shifts) fSystsDbl.emplace(it.first, it.second);
50  }
static int fgNextID
The next unused ID.
Definition: SystShifts.h:91
set< int >::iterator it
std::unordered_map< const ISyst *, double > fSystsDbl
Definition: SystShifts.h:85
ana::SystShifts::SystShifts ( const std::map< const ISyst *, stan::math::var > &  shifts)

Definition at line 53 of file SystShifts.cxx.

References fSystsDbl, fSystsStan, and it.

54  : fID(fgNextID++)
55  {
56  for(auto it: shifts)
57  {
58  fSystsStan.emplace(it.first, it.second);
59  fSystsDbl.emplace(it.first, util::GetValAs<double>(it.second));
60  }
61  }
static int fgNextID
The next unused ID.
Definition: SystShifts.h:91
set< int >::iterator it
std::unordered_map< const ISyst *, stan::math::var > fSystsStan
Definition: SystShifts.h:86
std::unordered_map< const ISyst *, double > fSystsDbl
Definition: SystShifts.h:85
virtual ana::SystShifts::~SystShifts ( )
virtualdefault

Member Function Documentation

std::vector< const ISyst * > ana::SystShifts::ActiveSysts ( ) const

Definition at line 199 of file SystShifts.cxx.

References fSystsDbl, it, and runNovaSAM::ret.

Referenced by ana::PredictionInterp::_PredictComponentSyst(), ana::OscCovMxExperiment::ChiSq(), ana::DeriveFlux(), test::GetNumuPrediction(), ana::SystConcat::GetShifts(), and ana::PlotSystShifts().

200  {
201  std::vector<const ISyst*> ret;
202  for(auto it: fSystsDbl) ret.push_back(it.first);
203  return ret;
204  }
set< int >::iterator it
std::unordered_map< const ISyst *, double > fSystsDbl
Definition: SystShifts.h:85
std::unique_ptr< SystShifts > ana::SystShifts::Copy ( ) const
virtual

Allow derived classes to overload so they can copy themselves in case they overload Penalty(). Used in IFitter. Note that you own the copy...

Reimplemented in ana::GaussianPriorSystShifts.

Definition at line 64 of file SystShifts.cxx.

Referenced by ana::IFitter::FitHelper(), ana::StanFitter::FitHelperSeeded(), and ana::StanFitter::TestGradients().

65  {
66  return std::make_unique<SystShifts>(*this);
67  }
template<typename T = double>
T ana::SystShifts::GetShift ( const ISyst syst) const
template<>
double ana::SystShifts::GetShift ( const ISyst syst) const

Definition at line 102 of file SystShifts.cxx.

References fSystsDbl, and it.

103  {
104  assert(syst);
105 
106  auto it = fSystsDbl.find(syst);
107  return (it == fSystsDbl.end()) ? 0 : it->second;
108  }
set< int >::iterator it
std::unordered_map< const ISyst *, double > fSystsDbl
Definition: SystShifts.h:85
string syst
Definition: plotSysts.py:176
template<>
stan::math::var ana::SystShifts::GetShift ( const ISyst syst) const

Definition at line 112 of file SystShifts.cxx.

References om::cout, allTimeWatchdog::endl, fSystsDbl, fSystsStan, it, ana::ISyst::ShortName(), and plotSysts::syst.

113  {
114  assert(syst);
115 
116  auto it = fSystsStan.find(syst);
117  //assert ( (it == fSystsStan.end()) == (fSystsDbl.find(syst) == fSystsDbl.end()) ); // if you're asking for a Stan syst, and it's not there but a double one is, something went wrong
118  if (it == fSystsStan.end())
119  {
120  auto itDbl = fSystsDbl.find(syst);
121  if (itDbl != fSystsDbl.end())
122  {
123  std::cout << "Warning: creating stan::math::var out of double value for syst '" << syst->ShortName() << "'." << std::endl;
124  std::cout << " If you see this repeatedly, it's likely a problem. (A few times during startup is probably harmless.)" << std::endl;
125  fSystsStan[syst] = stan::math::var(itDbl->second);
126  it = fSystsStan.find(syst);
127  }
128  }
129  return (it == fSystsStan.end()) ? 0 : it->second;
130  }
set< int >::iterator it
std::unordered_map< const ISyst *, stan::math::var > fSystsStan
Definition: SystShifts.h:86
var
std::unordered_map< const ISyst *, double > fSystsDbl
Definition: SystShifts.h:85
OStream cout
Definition: OStream.cxx:6
string syst
Definition: plotSysts.py:176
int ana::SystShifts::ID ( ) const
inline

SystShifts with the same set of systs should have the same ID.

Definition at line 32 of file SystShifts.h.

32 {return fID;}
bool ana::SystShifts::IsNominal ( ) const
inline

Definition at line 43 of file SystShifts.h.

References GetShift(), and T.

Referenced by ana::CovMxExperiment::ChiSq(), ana::DataMCComparison(), ana::SpectrumLoader::HandleRecord(), LatexName(), ana::BinnedLkhdExperiment::LogLikelihood(), ana::IPrediction::PredictComponentSyst(), ana::IPrediction::PredictSyst(), and ShortName().

43 {return fSystsDbl.empty(); } // since there's always a 'double' copy of any stan ones too
std::unordered_map< const ISyst *, double > fSystsDbl
Definition: SystShifts.h:85
std::string ana::SystShifts::LatexName ( ) const

Long description of component shifts, for plot labels.

Definition at line 185 of file SystShifts.cxx.

References genie::utils::style::Format(), fSystsDbl, IsNominal(), it, and runNovaSAM::ret.

186  {
187  if(IsNominal()) return "Nominal";
188 
190  for(auto it: fSystsDbl){
191  if(!ret.empty()) ret += ", ";
192  ret += it.first->LatexName() + TString::Format(" = %+g", it.second).Data();
193  }
194 
195  return ret;
196  }
set< int >::iterator it
bool IsNominal() const
Definition: SystShifts.h:43
std::unordered_map< const ISyst *, double > fSystsDbl
Definition: SystShifts.h:85
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
void Format(TGraph *gr, int lcol, int lsty, int lwid, int mcol, int msty, double msiz)
Definition: Style.cxx:154
std::unique_ptr< SystShifts > ana::SystShifts::LoadFrom ( TDirectory *  dir)
static

Definition at line 230 of file SystShifts.cxx.

References demo3::h, MECModelEnuComparisons::i, runNovaSAM::ret, and getGoodRuns4SAM::tag.

Referenced by test_stanfit_withsysts().

231  {
232  TObjString* tag = (TObjString*)dir->Get("type");
233  assert(tag);
234  assert(tag->GetString() == "SystShifts");
235 
236  auto ret = std::make_unique<SystShifts>();
237 
238  TH1* h = (TH1*)dir->Get("vals");
239  if(h){ // no histogram means nominal
240  for(int i = 1; i <= h->GetNbinsX(); ++i){
241  ret->SetShift(Registry<ISyst>::ShortNameToPtr(h->GetXaxis()->GetBinLabel(i)),
242  h->GetBinContent(i));
243  }
244  }
245 
246  return ret;
247  }
TDirectory * dir
Definition: macro.C:5
static const T * ShortNameToPtr(const std::string &s, bool allowFail=false)
Definition: Registry.cxx:60
h
Definition: demo3.py:41
stan::math::var ana::SystShifts::LogPrior ( ) const
virtual

If it's more efficient to implement log(prior) directly, override this

Reimplemented in ana::GaussianPriorSystShifts.

Definition at line 151 of file SystShifts.cxx.

References test_ParserArtEvents::log, and Prior().

151 { return log(Prior()); }
virtual stan::math::var Prior() const
Definition: SystShifts.h:65
static SystShifts ana::SystShifts::Nominal ( )
inlinestatic

Definition at line 34 of file SystShifts.h.

Referenced by ana::MultiExperiment::Add(), CAF_makeCAFSensitivities_for_FNEX(), ana::ChiSqToLklhdExptWrapper::ChiSqToLklhdExptWrapper(), ana::CountingExperiment::CountingExperiment(), ana::CovMxExperiment::CovMxExperiment(), demoFitContours(), demoFitSlices(), ana::Dmsq32Constraint::Dmsq32Constraint(), ana::IFitter::ExpandSeeds(), fake_future_data(), ana::FrequentistSurface::FindMinimum(), ana::FindValley(), ana::GaussianConstraint::GaussianConstraint(), getHists_FNEX(), getPredictions(), getPredictions_updatedAna(), ana::SystConcat::GetShifts(), goodness_of_fit(), joint_fit_2017_contours(), joint_fit_2017_slices(), joint_fit_2018_contours(), joint_fit_2018_slices(), joint_fit_2019_bestfit(), joint_fit_2019_contours(), joint_fit_2019_slices(), jointsensitivity(), make_fc_mass_and_oct_nersc_2018(), make_fc_mass_and_oct_nersc_2019(), make_fc_mh_nersc_2018(), make_fc_mh_nersc_2019(), make_fc_nus_surfs_nersc_2018(), make_fc_nus_surfs_nersc_2019(), make_fc_oct_nersc_2018(), make_fc_oct_nersc_2019(), make_fc_slices_nersc_2018(), make_fc_slices_nersc_2018_stats(), make_fc_slices_nersc_2019(), make_fc_surfaces_nersc_2018(), make_fc_surfaces_nersc_2018_stats(), make_fc_surfaces_nersc_2019(), make_nus17_fc_surfs(), MakeCAFSensitivities_for_FNEX(), makeMatrixElementSurface(), MakeSurface(), Nus18SystsNeutronLoad(), plot_fa_results(), plot_sa_results(), plot_sa_results_nodata(), Plotting_DataAndPrediction(), ana::PredictionCombinePeriods::PredictComponent(), ana::Profile(), reach_2018_dCPfractions(), ana::ReactorExperiment::ReactorExperiment(), sensitivity2018(), ana::T2KToyExperiment::SetPOTAnti(), ana::SingleSampleExperiment::SingleSampleExperiment(), ana::Ssth23Constraint::Ssth23Constraint(), ana::MinuitFitter::SupportsDerivatives(), test_numupred2017(), Tutorial2019Fit(), Tutorial2019FitContours(), Tutorial2019FitSlices(), ana::CovarianceMatrix::~CovarianceMatrix(), and ana::IChiSqExperiment::~IChiSqExperiment().

34 {return SystShifts();}
double ana::SystShifts::Penalty ( ) const

Penalty term for (frequentist) chi-squared fits.

Definition at line 142 of file SystShifts.cxx.

References fSystsDbl, it, and runNovaSAM::ret.

143  {
144  double ret = 0;
145  // Systematics are all expressed in terms of sigmas
146  for(auto it: fSystsDbl) ret += it.second * it.second;
147  return ret;
148  }
set< int >::iterator it
std::unordered_map< const ISyst *, double > fSystsDbl
Definition: SystShifts.h:85
virtual stan::math::var ana::SystShifts::Prior ( ) const
inlinevirtual

Prior used in Bayesian fitting. Override as needed. If it's more efficient to calculate log(prior) explicitly, override LogPrior() as well

Reimplemented in ana::GaussianPriorSystShifts.

Definition at line 65 of file SystShifts.h.

References dir, ana::LoadFrom(), ana::SaveTo(), Munits::sr, and plotSysts::weight.

Referenced by LogPrior().

65 { return 1.; }
void ana::SystShifts::ResetToNominal ( )
void ana::SystShifts::SaveTo ( TDirectory *  dir) const

Definition at line 207 of file SystShifts.cxx.

References fSystsDbl, demo3::h, getBrightness::ibin, it, and tmp.

Referenced by joint_fit_2019_bestfit().

208  {
209  TDirectory* tmp = gDirectory;
210 
211  dir->cd();
212  TObjString("SystShifts").Write("type");
213 
214  // Don't write any histogram for the nominal case
215  if(!fSystsDbl.empty()){
216  TH1D h("", "", fSystsDbl.size(), 0, fSystsDbl.size());
217  int ibin = 0;
218  for(auto it: fSystsDbl){
219  ++ibin;
220  h.GetXaxis()->SetBinLabel(ibin, it.first->ShortName().c_str());
221  h.SetBinContent(ibin, it.second);
222  }
223  h.Write("vals");
224  }
225 
226  tmp->cd();
227  }
set< int >::iterator it
Float_t tmp
Definition: plot.C:36
std::unordered_map< const ISyst *, double > fSystsDbl
Definition: SystShifts.h:85
TDirectory * dir
Definition: macro.C:5
h
Definition: demo3.py:41
void ana::SystShifts::SetShift ( const ISyst syst,
double  shift,
bool  force = false 
)

shift: 0 = nominal; +-1 = 1sigma shifts etc. Arbitrary shifts allowed set force=true to insert a syst even if the shift is 0

Definition at line 70 of file SystShifts.cxx.

References om::cerr, allTimeWatchdog::endl, fgNextID, fID, fSystsDbl, fSystsStan, plotSysts::shift, and ana::ISyst::ShortName().

Referenced by ana::CovarianceMatrix::BuildFullCovMx(), CalcRWithSysts(), CalcRWithSystsNus17(), ana::MultiExperiment::ChiSq(), ana::CovMxManager::CovMxManager(), demoPlotSystBands(), demoStarPlots(), ana::MultiExperiment::Derivative(), ana::PredictionAddRock::Derivative(), demo::DrawUpDownRatioCanvas(), fake_future_data(), FCContour(), fill_col(), ana::IFitter::FitHelper(), ana::MinuitFitter::FitHelperSeeded(), ana::StanFitter::FitHelperSeeded(), genie_contours(), get_sa_systs_mags_v2(), getPredictions(), getPredictions_updatedAna(), ana::GetShift(), GetShift(), ana::SystConcat::GetShifts(), ana::GetSystShiftsMultiverse(), goodness_of_fit(), joint_fit_2017_make_fc_slice(), joint_fit_2017_make_fc_surf(), make_fc_mass_and_oct_nersc_2018(), make_fc_mass_and_oct_nersc_2019(), make_fc_mh_nersc_2018(), make_fc_mh_nersc_2019(), make_fc_oct_nersc_2018(), make_fc_oct_nersc_2019(), make_fc_points(), make_fc_slices_nersc_2018(), make_fc_slices_nersc_2018_stats(), make_fc_slices_nersc_2019(), make_fc_surfaces_nersc_2018(), make_fc_surfaces_nersc_2018_stats(), make_fc_surfaces_nersc_2019(), make_starplots(), make_xsec_wgts_2018_hists(), MakeFakeData(), MakeSystRatios(), MatrixValidation(), mec_tuning(), mec_tuning_fitter_2020(), mec_tuning_preds_2020(), MichelDecompTest(), myPlotWithSystErrorBand(), nuebar_signif(), plot_datamcpred(), plot_datapredictions(), plot_fa_results(), plot_predictions(), plot_sa_results(), plot_sa_results_nodata(), plotDataPred(), plotDataPred_SplitCanvas(), PlotSysts(), Plotting_DataAndPrediction(), ana::PlotWithAreaSystErrorBand(), ana::PlotWithSystErrorBand(), ana::PlotWithSystErrorBand_Quant(), ana::SingleSampleExperiment::PredHistIncCosmics(), ana::PredictionAddRock::PredictComponentSyst(), ana::PredictionNumuFAHadE::PredictComponentSyst(), ana::PredictionInterp::PredictionInterp(), SetShift(), starPlot(), syst_test(), test_stanfit_dummy(), test_stanfit_systpulls(), and TestPred().

71  {
72  fID = fgNextID++;
73 
74  // if this slot already exists in the Stan systs, and we're not setting the same value,
75  // some shenanigans are going on that we need to figure out. abort.
76  auto itStan = fSystsStan.find(syst);
77  if(itStan != fSystsStan.end() && itStan->second != shift)
78  {
79  std::cerr << "Error Syst '" << syst->ShortName() << " already has a Stan pull set (" << itStan->second << ") "
80  << "and you're trying to set a different double one (" << shift << ")." << std::endl;
81  std::cerr << "You almost certainly didn't mean to do that." << std::endl;
82  std::cerr << "Abort." << std::endl;
83  abort();
84  }
85 
86  fSystsDbl.erase(syst);
87  if(force || shift != 0.) fSystsDbl.emplace(syst, shift);
88  }
static int fgNextID
The next unused ID.
Definition: SystShifts.h:91
std::unordered_map< const ISyst *, stan::math::var > fSystsStan
Definition: SystShifts.h:86
OStream cerr
Definition: OStream.cxx:7
std::unordered_map< const ISyst *, double > fSystsDbl
Definition: SystShifts.h:85
string syst
Definition: plotSysts.py:176
void ana::SystShifts::SetShift ( const ISyst syst,
stan::math::var  shift 
)

Definition at line 91 of file SystShifts.cxx.

References fSystsStan, and SetShift().

92  {
93  fSystsStan.erase(syst);
94  // note: _always_ put the syst in, even if the value is 0.
95  // autodiff relies on the calculation happening so as to get the gradient
96  fSystsStan.emplace(syst, shift);
97  SetShift(syst, util::GetValAs<double>(shift), true);
98  }
std::unordered_map< const ISyst *, stan::math::var > fSystsStan
Definition: SystShifts.h:86
string syst
Definition: plotSysts.py:176
void SetShift(const ISyst *syst, double shift, bool force=false)
Definition: SystShifts.cxx:70
void ana::SystShifts::Shift ( caf::SRProxy sr,
double &  weight 
) const

Definition at line 154 of file SystShifts.cxx.

References fSystsDbl, and it.

Referenced by ana::GetWeightFromShifts(), jw::GetWeightFromShifts(), and ana::SpectrumLoader::HandleRecord().

156  {
157  // always fSystsDbl here because this is only used in the event loop, not in fitting
158  // (so the autodiff'd version is not needed)
159  for(auto it: fSystsDbl) it.first->Shift(it.second, sr, weight);
160  }
set< int >::iterator it
const Var weight
std::unordered_map< const ISyst *, double > fSystsDbl
Definition: SystShifts.h:85
void ana::SystShifts::Shift ( caf::SRNeutrinoProxy sr,
double &  weight 
) const

Definition at line 163 of file SystShifts.cxx.

References fSystsDbl, and it.

164  {
165  // always fSystsDbl here because this is only used in the event loop, not in fitting
166  // (so the autodiff'd version is not needed)
167  for(auto it: fSystsDbl) it.first->TruthShift(it.second, nu, weight);
168  }
set< int >::iterator it
const Var weight
std::unordered_map< const ISyst *, double > fSystsDbl
Definition: SystShifts.h:85
std::string ana::SystShifts::ShortName ( ) const

Brief description of component shifts, for printing to screen.

Definition at line 171 of file SystShifts.cxx.

References genie::utils::style::Format(), fSystsDbl, IsNominal(), it, and runNovaSAM::ret.

172  {
173  if(IsNominal()) return "nominal";
174 
176  for(auto it: fSystsDbl){
177  if(!ret.empty()) ret += ",";
178  ret += it.first->ShortName() + TString::Format("=%+g", it.second).Data();
179  }
180 
181  return ret;
182  }
set< int >::iterator it
bool IsNominal() const
Definition: SystShifts.h:43
std::unordered_map< const ISyst *, double > fSystsDbl
Definition: SystShifts.h:85
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
void Format(TGraph *gr, int lcol, int lsty, int lwid, int mcol, int msty, double msiz)
Definition: Style.cxx:154

Member Data Documentation

int ana::SystShifts::fgNextID = 1
staticprivate

The next unused ID.

Definition at line 91 of file SystShifts.h.

Referenced by SetShift().

int ana::SystShifts::fID
private

Definition at line 89 of file SystShifts.h.

Referenced by ResetToNominal(), and SetShift().

std::unordered_map<const ISyst*, double> ana::SystShifts::fSystsDbl
protected
std::unordered_map<const ISyst*, stan::math::var> ana::SystShifts::fSystsStan
mutableprotected

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