ModularExtrapComponent.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "CAFAna/Core/HistAxis.h"
5 #include "CAFAna/Core/Ratio.h"
8 
9 class TDirectory;
10 class TH1;
11 
12 namespace ana
13 {
14 
15  // class IDecomp;
16 
17  /// Simple way to remember what to ask the decomposition for.
19 
20  /// Base class for component extrapolation.
22  {
23  public:
24 
25  ModularExtrapComponent() : fEvaluated(false), fCache(OscillatableSpectrum::Uninitialized()) {}
27  virtual void SaveTo(TDirectory* dir, const std::string& name) const = 0;
28  virtual void SavePlots(TDirectory* dir, double potFD) const=0;
29  static void SetQuiet(bool quiet = true){ fQuiet = quiet; }
30 
31  /// Interface so that result of Eval() is called only once and cached.
32  const OscillatableSpectrum& Return() const;
33 
34  virtual const IDecomp* GetDecomp() const = 0;
35 
36  protected:
37 
38  /// Core extrapolation math.
39  virtual OscillatableSpectrum Eval() const = 0;
40 
41  /// Helper function to pick out single Spectrum from a decomposition.
42  static Spectrum GetDecompResult(const IDecomp&, DecompResult);
43 
44  /// Helper function to turn a DecompResult into a string (for storage).
45  static std::string DRToString(DecompResult);
46 
47  /// Helper function to turn a string into a DecompResult (for loading).
48  static DecompResult StringToDR(std::string);
49 
50  /// Form Ratio, but be aware of zero division.
51  /** Necessary because root thinks n/0==0, so we lose events when
52  low stats cause empty bins. If zero division occurs, ratio is set to
53  1 and warning is issued. If numerator is 0 and ratio will be
54  multiplied by 0 later, the warning is suppressed. */
55  static Ratio FormSmartRatio(
56  const Spectrum& num,
57  const Spectrum& denom,
60  const Spectrum& mult
61  );
62 
63  static void ComparisonPlot(
64  Spectrum mc,
65  Spectrum notMC,
66  double pot,
67  std::string notMCLabel,
68  int notMCColor,
71  std::string saveAs,
72  bool restrictRange = false
73  );
74 
75  private:
76  mutable bool fEvaluated;
78  static bool fQuiet;
79 
81  {
82  public:
83  DivByZeroCounter(bool quiet = false)
84  : fQuiet(quiet)
85  {}
87 
88  std::set<std::tuple<std::string, std::string, int>> fBins;
89 
90  private:
91  bool fQuiet;
92 
93  };
94 
95  };
96 
97  /// "Extrapolates" component by returning FD Monte Carlo.
99  {
100 
101  public:
102  NoReweight(
104  const HistAxis& axis,
105  const Cut& fdcut,
106  const SystShifts& shiftMC,
107  const Var& weight,
108  const Cut& flavors,
109  SpectrumLoaderBase& extraloaderswap = kNullLoader, // for NC
110  SpectrumLoaderBase& extraloadertau = kNullLoader // for NC
111  );
112  OscillatableSpectrum Eval() const override;
113  void SaveTo(TDirectory* dir, const std::string& name) const override;
114  void SavePlots(TDirectory* dir, double potFD) const override;
115  static std::unique_ptr<NoReweight> LoadFrom(TDirectory* dir, const std::string& name);
116 
117  const IDecomp* GetDecomp() const override {return 0;}
118  private:
119  NoReweight(const OscillatableSpectrum& recoFD) : fRecoFD(recoFD) {}
120 
122 
123  };
124 
125  /// Extrapolates component using truth-over-truth method.
127  {
128 
129  public:
131  SpectrumLoaderBase& ndloader,
132  const HistAxis& axisFD,
133  const HistAxis& axisND,
134  const Cut& fdcut,
135  const SystShifts& shiftMC,
136  const Var& weight,
139  const Cut& ndcut,
140  const IDecomp& decomposition,
141  const DecompResult dr,
142  const Cut& ndflavor,
143  SpectrumLoaderBase& fdloader,
144  const Cut& fdflavors
145  );
146  ~TruthReweight();
147  OscillatableSpectrum Eval() const override;
148  void SaveTo(TDirectory* dir, const std::string& name) const override;
149  void SavePlots(TDirectory* dir, double potFD) const override;
150  static std::unique_ptr<TruthReweight> LoadFrom(TDirectory* dir, const std::string& name);
151 
152  const IDecomp* GetDecomp() const override {return &fDecomp;}
153  private:
155  OscillatableSpectrum recoToTrueND,
156  OscillatableSpectrum trueToRecoFD,
157  const IDecomp& decomp,
158  const DecompResult decompRes,
159  std::string label,
160  std::string latex
161  ) : fRecoToTrueND(recoToTrueND), fTrueToRecoFD(trueToRecoFD),
162  fDecomp(decomp), fDecompRes(decompRes),
163  fLabel(label), fLatex(latex) {}
164 
167  const IDecomp& fDecomp;
172 
173  };
174 
175  /// Extrapolates using reco-over-reco method.
177  {
178 
179  public:
180  RecoReweight(
181  SpectrumLoaderBase& ndloader,
182  const HistAxis& axis,
183  const Cut& fdcut,
184  const SystShifts& shiftMC,
185  const Var& weight,
188  const Cut& ndcut,
189  const IDecomp& decomposition,
190  const DecompResult dr,
191  const Cut& ndflavor,
192  SpectrumLoaderBase& fdloader,
193  const Cut& fdflavors,
194  SpectrumLoaderBase& extrafdloaderswap = kNullLoader, // for NC
195  SpectrumLoaderBase& extrafdloadertau = kNullLoader // for NC
196  );
197  ~RecoReweight();
198  OscillatableSpectrum Eval() const override;
199  void SaveTo(TDirectory* dir, const std::string& name) const override;
200  void SavePlots(TDirectory* dir, double potFD) const override;
201  static std::unique_ptr<RecoReweight> LoadFrom(TDirectory* dir, const std::string& name);
202 
203  /// Uses MC spectra and target MC stats to estimate optimal binning.
204  TH1* OptimalBinning( double potMCFD, double potMCND ) const;
205  /// Fits gaussians to FD and ND MC to estimate optimal binning
206  TH1* OptimalBinningFit( double potMCFD, double potMCND ) const;
207  static TH1* OptimalBinningHelper( TH1*, TH1* );
208 
209  const IDecomp* GetDecomp() const override {return fDecomp;}
210 
211  friend class ModularExtrap;
212  private:
214  Spectrum recoND,
215  OscillatableSpectrum trueToRecoFD,
216  const IDecomp& decomp,
217  const DecompResult decompRes,
218  std::string label,
219  std::string latex
220  ) : fRecoND(recoND), fTrueToRecoFD(trueToRecoFD),
221  fDecomp(&decomp), fOwnDecomp(false), fDecompRes(decompRes),
222  fLabel(label), fLatex(latex) {}
223 
226  const IDecomp* fDecomp;
231 
232  };
233 
234 }
const XML_Char * name
Definition: expat.h:151
void latex(double x, double y, std::string txt, double ang=0, int align=22, double size=0.042)
Cuts and Vars for the 2020 FD DiF Study.
Definition: vars.h:6
const IDecomp * GetDecomp(IPrediction *prediction, EModExtrapComps modExtrapComp)
#define location
void SaveTo(const osc::IOscCalc &x, TDirectory *dir, const std::string &name)
static void SetQuiet(bool quiet=true)
OscillatableSpectrum fTrueToRecoFD
const Var weight
Base class for component extrapolation.
"Extrapolates" component by returning FD Monte Carlo.
Simple record of shifts applied to systematic parameters.
Definition: SystShifts.h:20
OscillatableSpectrum fRecoToTrueND
const DecompResult fDecompRes
std::unique_ptr< T > LoadFrom(TDirectory *dir, const std::string &label)
Definition: LoadFromFile.h:17
Representation of a spectrum in any variable, with associated POT.
Definition: Spectrum.h:33
const char * label
OscillatableSpectrum fTrueToRecoFD
TruthReweight(OscillatableSpectrum recoToTrueND, OscillatableSpectrum trueToRecoFD, const IDecomp &decomp, const DecompResult decompRes, std::string label, std::string latex)
NoReweight(const OscillatableSpectrum &recoFD)
std::vector< std::string > flavors
#define pot
Extrapolates component using truth-over-truth method.
DecompResult
Simple way to remember what to ask the decomposition for.
const IDecomp * GetDecomp() const override
loader
Definition: demo0.py:10
const IDecomp * GetDecomp() const override
Represent the ratio between two spectra.
Definition: Ratio.h:8
Base class for the various types of spectrum loader.
OscillatableSpectrum fRecoFD
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
TDirectory * dir
Definition: macro.C:5
int num
Definition: f2_nu.C:119
std::set< std::tuple< std::string, std::string, int > > fBins
Standard interface to all decomposition techniques.
Definition: IDecomp.h:13
const IDecomp * GetDecomp() const override
Spectrum with true energy information, allowing it to be oscillated
Extrapolate each component using a separate ModularExtrapComponent.
Definition: ModularExtrap.h:23
const DecompResult fDecompRes
RecoReweight(Spectrum recoND, OscillatableSpectrum trueToRecoFD, const IDecomp &decomp, const DecompResult decompRes, std::string label, std::string latex)
static NullLoader kNullLoader
Dummy loader that doesn&#39;t load any files.
Extrapolates using reco-over-reco method.