All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PredictionInterp.h
Go to the documentation of this file.
1 #pragma once
2 
5 
8 
9 #include <map>
10 #include <memory>
11 #include <unordered_map>
12 
13 #include "TMD5.h"
14 
15 class TH1;
16 
17 namespace ana
18 {
19  class Loaders;
20 
21  /// Implements systematic errors by interpolation between shifted templates
23  {
24  public:
25  enum EMode_t{
27  };
28 
29  /// \param systs What systematics we should be capable of interpolating
30  /// \param osc The oscillation point to expand around
31  /// \param predGen Construct an IPrediction from the following
32  /// information.
33  /// \param loaders The loaders to be passed on to the underlying prediction
34  /// \param shiftMC Underlying shift. Use with care. Mostly for
35  /// PredictionNumuFAHadE. Should not contain any of of
36  /// \a systs
37  /// \param mode In FHC the wrong-sign has bad stats and probably the
38  /// fits can't be split out reasonably. For RHC it's
39  /// important not to conflate them.
40  PredictionInterp(std::vector<const ISyst*> systs,
41  osc::IOscCalc* osc,
42  const IPredictionGenerator& predGen,
43  Loaders& loaders,
44  const SystShifts& shiftMC = kNoShift,
46 
47  virtual ~PredictionInterp();
48 
49 
50 
51  virtual Spectrum Predict(osc::IOscCalc* calc) const override;
52  virtual Spectrum PredictSyst(osc::IOscCalc* calc,
53  const SystShifts& shift) const override;
54 
56  Flavors::Flavors_t flav,
57  Current::Current_t curr,
58  Sign::Sign_t sign) const override;
60  const SystShifts& shift,
61  Flavors::Flavors_t flav,
62  Current::Current_t curr,
63  Sign::Sign_t sign) const override;
64 
65  virtual void SaveTo(TDirectory* dir) const override;
66  static std::unique_ptr<PredictionInterp> LoadFrom(TDirectory* dir);
67 
68  /// After calling this DebugPlots won't work fully and SaveTo won't work at
69  /// all.
70  void MinimizeMemory();
71 
72  void DebugPlot(const ISyst* syst,
73  osc::IOscCalc* calc,
76  Sign::Sign_t sign = Sign::kBoth) const;
77 
78  // If \a savePattern is not empty, print each pad. If it contains "%s" then
79  // multiple files will be written, one per systematic.
80  void DebugPlots(osc::IOscCalc* calc,
81  const std::string& savePattern = "",
84  Sign::Sign_t sign = Sign::kBoth) const;
85 
86  void SetOscSeed(osc::IOscCalc* oscSeed);
87 
88  void DebugPlotColz(const ISyst* syst,
89  osc::IOscCalc* calc,
92  Sign::Sign_t sign = Sign::kBoth) const;
93 
94  void DebugPlotsColz(osc::IOscCalc* calc,
95  const std::string& savePattern = "",
98  Sign::Sign_t sign = Sign::kBoth) const;
99 
100  bool SplitBySign() const {return fSplitBySign;}
103  kOther, ///< Taus, numu appearance
105  };
106 
107  PredictionInterp() : fBinning(0, {}, {}, 0, 0) {}
108 
109  static void LoadFromBody(TDirectory* dir, PredictionInterp* ret,
110  std::vector<const ISyst*> veto = {});
111 
112  struct Coeffs{
113  Coeffs(double _a, double _b, double _c, double _d)
114  : a(_a), b(_b), c(_c), d(_d) {}
115  double a, b, c, d;
116  };
117 
118  /// Find coefficients describing this set of shifts
119  std::vector<std::vector<Coeffs>>
120  FitRatios(const std::vector<double>& shifts,
121  const std::vector<std::unique_ptr<TH1>>& ratios) const;
122 
123  /// Find coefficients describing the ratios from this component
124  std::vector<std::vector<Coeffs>>
125  FitComponent(const std::vector<double>& shifts,
126  const std::vector<IPrediction*>& preds,
127  Flavors::Flavors_t flav,
128  Current::Current_t curr,
130  const std::string& shortName) const;
131 
134  bool nubar, // try to use fitsNubar if it exists
135  const SystShifts& shift) const;
136 
137  /// Helper for PredictComponentSyst
139  const TMD5* hash,
140  const SystShifts& shift,
141  Flavors::Flavors_t flav,
142  Current::Current_t curr,
144  CoeffsType type) const;
145 
146  std::unique_ptr<IPrediction> fPredNom; ///< The nominal prediction
147 
149  {
150  double Stride() const {return shifts.size() > 1 ? shifts[1]-shifts[0] : 1;}
151 
152  std::string systName; ///< What systematic we're interpolating
153  std::vector<double> shifts; ///< Shift values sampled
154  std::vector<IPrediction*> preds;
155 
156  int nCoeffs; // Faster than calling size()
157 
158  /// Indices: [type][histogram bin][shift bin]
159  std::vector<std::vector<std::vector<Coeffs>>> fits;
160  /// Will be filled if signs are separated, otherwise not
161  std::vector<std::vector<std::vector<Coeffs>>> fitsNubar;
162  };
163 
164  protected:
165  mutable std::unordered_map<const ISyst*, ShiftedPreds> fPreds;
166 
167  /// The oscillation values we assume when evaluating the coefficients
169 
170  mutable Spectrum fBinning; ///< Dummy spectrum to provide binning
171 
172  struct Key_t
173  {
177  bool operator<(const Key_t& rhs) const
178  {
179  return (std::make_tuple(flav, curr, sign) <
180  std::make_tuple(rhs.flav, rhs.curr, rhs.sign));
181  }
182  };
183  struct Val_t
184  {
185  TMD5 hash;
187  };
188  mutable std::map<Key_t, Val_t> fNomCache;
189 
191 
192  void InitFits() const;
193 
194  void InitFitsHelper(ShiftedPreds& sp,
195  std::vector<std::vector<std::vector<Coeffs>>>& fits,
196  Sign::Sign_t sign) const;
197  };
198 
199 }
virtual Spectrum PredictComponent(osc::IOscCalc *calc, Flavors::Flavors_t flav, Current::Current_t curr, Sign::Sign_t sign) const override
Implements systematic errors by interpolation between shifted templates.
virtual Spectrum PredictSyst(osc::IOscCalc *calc, const SystShifts &shift) const override
std::string systName
What systematic we&#39;re interpolating.
Simple record of shifts applied to systematic parameters.
Definition: SystShifts.h:16
Collection of SpectrumLoaders for many configurations.
Definition: Loaders.h:12
std::vector< std::vector< std::vector< Coeffs > > > fitsNubar
Will be filled if signs are separated, otherwise not.
virtual void SaveTo(TDirectory *dir) const override
void SetOscSeed(osc::IOscCalc *oscSeed)
std::vector< std::vector< Coeffs > > FitRatios(const std::vector< double > &shifts, const std::vector< std::unique_ptr< TH1 >> &ratios) const
Find coefficients describing this set of shifts.
process_name opflashCryoW ana
void DebugPlotsColz(osc::IOscCalc *calc, const std::string &savePattern="", Flavors::Flavors_t flav=Flavors::kAll, Current::Current_t curr=Current::kBoth, Sign::Sign_t sign=Sign::kBoth) const
shift
Definition: fcl_checks.sh:26
Encapsulate code to systematically shift a caf::StandardRecord.
Definition: ISyst.h:14
Representation of a spectrum in any variable, with associated POT.
Definition: Spectrum.h:30
std::unique_ptr< IPrediction > fPredNom
The nominal prediction.
std::map< Key_t, Val_t > fNomCache
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:265
Interactions of both types.
Definition: IPrediction.h:41
std::vector< double > shifts
Shift values sampled.
const char mode
Definition: noise_ana.cxx:20
std::vector< IPrediction * > preds
Spectrum fBinning
Dummy spectrum to provide binning.
void DebugPlots(osc::IOscCalc *calc, const std::string &savePattern="", Flavors::Flavors_t flav=Flavors::kAll, Current::Current_t curr=Current::kBoth, Sign::Sign_t sign=Sign::kBoth) const
static std::unique_ptr< PredictionInterp > LoadFrom(TDirectory *dir)
const SystShifts kNoShift
Definition: SystShifts.h:61
tuple dir
Definition: dropbox.py:28
Taus, numu appearance.
bool operator<(const Key_t &rhs) const
int sign(double val)
Definition: UtilFunc.cxx:104
void DebugPlotColz(const ISyst *syst, osc::IOscCalc *calc, Flavors::Flavors_t flav=Flavors::kAll, Current::Current_t curr=Current::kBoth, Sign::Sign_t sign=Sign::kBoth) const
then echo File list $list not found else cat $list while read file do echo $file sed s
Definition: file_to_url.sh:60
Coeffs(double _a, double _b, double _c, double _d)
Both neutrinos and antineutrinos.
Definition: IPrediction.h:51
Standard interface to all prediction techniques.
Definition: IPrediction.h:58
void DebugPlot(const ISyst *syst, osc::IOscCalc *calc, Flavors::Flavors_t flav=Flavors::kAll, Current::Current_t curr=Current::kBoth, Sign::Sign_t sign=Sign::kBoth) const
void InitFitsHelper(ShiftedPreds &sp, std::vector< std::vector< std::vector< Coeffs >>> &fits, Sign::Sign_t sign) const
Given loaders and an MC shift, Generate() generates an IPrediction.
std::unordered_map< const ISyst *, ShiftedPreds > fPreds
osc::IOscCalc * fOscOrigin
The oscillation values we assume when evaluating the coefficients.
All neutrinos, any flavor.
Definition: IPrediction.h:25
std::vector< std::vector< Coeffs > > FitComponent(const std::vector< double > &shifts, const std::vector< IPrediction * > &preds, Flavors::Flavors_t flav, Current::Current_t curr, Sign::Sign_t sign, const std::string &shortName) const
Find coefficients describing the ratios from this component.
virtual Spectrum PredictComponentSyst(osc::IOscCalc *calc, const SystShifts &shift, Flavors::Flavors_t flav, Current::Current_t curr, Sign::Sign_t sign) const override
virtual Spectrum Predict(osc::IOscCalc *calc) const override
Spectrum ShiftedComponent(osc::IOscCalc *calc, const TMD5 *hash, const SystShifts &shift, Flavors::Flavors_t flav, Current::Current_t curr, Sign::Sign_t sign, CoeffsType type) const
Helper for PredictComponentSyst.
Spectrum ShiftSpectrum(const Spectrum &s, CoeffsType type, bool nubar, const SystShifts &shift) const
std::vector< std::vector< std::vector< Coeffs > > > fits
Indices: [type][histogram bin][shift bin].