SpectrumLoaderBase.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cassert>
4 #include <functional>
5 #include <list>
6 #include <memory>
7 #include <set>
8 #include <string>
9 #include <vector>
10 
11 #include "CAFAna/Core/Cut.h"
12 #include "CAFAna/Core/FwdDeclare.h"
14 #include "CAFAna/Core/MultiVar.h"
15 #include "CAFAna/Core/SystShifts.h"
16 #include "CAFAna/Core/Var.h"
17 
20 
22 
23 class TFile;
24 class TH1;
25 
26 namespace std
27 {
28  template<> struct hash<std::pair<int, int>>
29  {
30  size_t operator()(const std::pair<int, int>& x) const
31  {
32  return std::hash<int>()(x.first*65536 + x.second);
33  }
34  };
35 
36  template<> struct hash<std::tuple<int, int, int>>
37  {
38  size_t operator()(const std::tuple<int, int, int>& x) const
39  {
40  return std::hash<int>()(std::get<0>(x)*1048576 + std::get<1>(x)*1024 + std::get<2>(x));
41  }
42  };
43 
44  template<> struct hash<std::tuple<int, int, int, int>>
45  {
46  size_t operator()(const std::tuple<int, int, int, int>& x) const
47  {
48  return std::hash<int>()(std::get<0>(x)*16777216 + std::get<1>(x)*65536 + std::get<2>(x) * 256 + std::get<3>(x));
49  }
50  };
51 }
52 
53 namespace ana
54 {
55  class IFileSource;
56  class ReweightableSpectrum;
57 
58  class CutApplier;
59  class SystApplier;
61 
62  // TODO this belongs in cafanacore's MultiVar.h
64 
65  /// Base class for the various types of spectrum loader
67  {
68  public:
69 
70  friend class ReweightableSpectrum;
71  friend class NDOscillatableSpectrum;
72  friend class OscillatableSpectrum;
73  friend class Spectrum;
74 
75  virtual ~SpectrumLoaderBase();
76 
77  void SetSpillCut(const SpillCut& cut);
78 
79  /// For use by the \ref Spectrum constructor
80  virtual void AddSpectrum(Spectrum& spect,
81  const Var& var,
82  const Cut& cut,
83  const SystShifts& shift,
84  const Var& wei = kUnweighted);
85 
86  /// For use by the \ref Spectrum constructor
87  virtual void AddSpectrum(Spectrum& spect,
88  const MultiVar& var,
89  const Cut& cut,
90  const SystShifts& shift,
91  const Var& wei = kUnweighted);
92 
93  /// For use by the \ref Spectrum constructor
94  virtual void AddSpectrum(Spectrum& spect,
95  const NuTruthVar& var,
96  const NuTruthCut& cut,
97  const SystShifts& shift,
98  const NuTruthVar& wei = kNuTruthUnweighted);
99 
100  virtual void AddSpectrum(Spectrum& spect,
101  const NuTruthMultiVar& var,
102  const NuTruthCut& cut,
103  const SystShifts& shift,
104  const NuTruthVar& wei = kNuTruthUnweighted);
105 
106  /// For use by the constructors of \ref ReweightableSpectrum subclasses
107  virtual void AddReweightableSpectrum(ReweightableSpectrum& spect,
108  const Var& xvar,
109  const Var& yvar,
110  const Cut& cut,
111  const SystShifts& shift,
112  const Var& wei);
113 
114  /// Uses include counting the total POT or spills in a run
115  virtual void AddSpillHistogram(TH1* h,
116  const SpillVar& var,
117  const SpillCut& cut,
118  const SpillVar& wei = kSpillUnweighted);
119 
120  /// Load all the registered spectra
121  virtual void Go() = 0;
122 
123  /// Indicate whether or not \ref Go has been called
124  virtual bool Gone() const {return fGone;}
125 
126  double GetWeightedPOT();
127 
128  protected:
129  /// Component of other constructors
131  /// Construct from a filename, wildcard, SAM definition, or SAM query
133  /// Construct from an explicit list of files
134  SpectrumLoaderBase(const std::vector<std::string>& fnames);
135 
136  // Move operations
138  SpectrumLoaderBase& operator=(SpectrumLoaderBase&&) = default;
139 
140  // No copy operations because I don't want to deal with pointers
141  SpectrumLoaderBase(const SpectrumLoaderBase&) = delete;
142  SpectrumLoaderBase& operator=(const SpectrumLoaderBase&) = delete;
143 
144  /// Figure out if \a str is a wildcard or SAM query and return a source
145  IFileSource* WildcardOrSAMQuery(const std::string& str) const;
146 
147  virtual void AccumulateExposures(const caf::SRSpillProxy* spill) = 0;
148 
149  /// Forwards to \ref fFileSource
150  int NFiles() const;
151 
152  /// Forwards to \ref fFileSource but also accumulates POT and livetime
153  TFile* GetNextFile();
154 
156  std::unique_ptr<IFileSource> fFileSource;
157 
158  bool fGone; ///< Has Go() been called? Can't add more histograms after that
159 
160  double fPOT; ///< Accumulated by calls to \ref GetNextFile
161  double fRunPOT; ///< Crude measure, not including spill cuts
162 
163  double fSinglePOT;
164  double fBeamNue;
165  double fSingleNue;
166  double fWeightedPOT;
167 
168  WeightApplier* GetSinkSCW(const SystShifts& shift,
169  const Cut& cut,
170  const Var& wei);
171 
172  std::set<IRecordSink*> fSpectrumSinks;
173 
174  std::unordered_map<std::tuple<int, int, int>, WeightApplier*> fSinksSCW;
175  std::unordered_map<std::pair<int, int>, CutApplier*> fSinksSC;
176  std::unordered_map<int, SystApplier*> fSinksS;
177 
179 
181 
182  /// Helper for \ref SpectrumLoaderBase. Details of SpillVar plots
184  {
185  SpillHistDef(TH1* _h,
186  const SpillVar& _var,
187  const SpillCut& _cut,
188  const SpillVar& _wei = kSpillUnweighted)
189  : h(_h), var(_var), cut(_cut), wei(_wei)
190  {
191  }
192 
193  TH1* h;
194  const SpillVar var;
195  const SpillCut cut;
196  const SpillVar wei;
197  };
198 
199  /// Histograms that are filled from the nuTree
200  struct NuHistDef
201  {
203  const NuTruthVar& _var,
204  const NuTruthCut& _cut,
205  const SystShifts& _shift,
206  const NuTruthVar& _wei = kNuTruthUnweighted)
207  : spect(_spect), var(_var), cut(_cut), shift(_shift), wei(_wei)
208  {
209  }
210 
212  const NuTruthMultiVar& _var,
213  const NuTruthCut& _cut,
214  const SystShifts& _shift,
215  const NuTruthVar& _wei = kNuTruthUnweighted)
216  : spect(_spect), multivar(_var), cut(_cut), shift(_shift), wei(_wei)
217  {
218  }
219 
220 
222  const std::optional<NuTruthVar> var;
223  const std::optional<NuTruthMultiVar> multivar;
227  };
228 
229  std::list<SpillHistDef> fSpillHistDefs;
230  std::list<NuHistDef> fNuHistDefs;
231 
232  /// Cut applied to the spill branch for every event
233  std::unique_ptr<SpillCut> fSpillCut;
234  };
235 
236  /// \brief Dummy loader that doesn't load any files
237  ///
238  /// Useful when a loader is required for a component you want to ignore
240  {
241  public:
243  ~NullLoader();
244 
245  virtual void Go() override;
246  void AddSpectrum(Spectrum& spect,
247  const Var& var,
248  const Cut& cut,
249  const SystShifts& shift,
250  const Var& wei = kUnweighted) override {}
251  void AddSpectrum(Spectrum& spect,
252  const MultiVar& var,
253  const Cut& cut,
254  const SystShifts& shift,
255  const Var& wei = kUnweighted) override {}
256  void AddSpectrum(Spectrum& spect,
257  const NuTruthVar& var,
258  const NuTruthCut& cut,
259  const SystShifts& shift,
260  const NuTruthVar& wei = kNuTruthUnweighted) override {}
261  void AddSpectrum(Spectrum& spect,
262  const NuTruthMultiVar& var,
263  const NuTruthCut& cut,
264  const SystShifts& shift,
265  const NuTruthVar& wei = kNuTruthUnweighted) override {}
267  const Var& xvar,
268  const Var& yvar,
269  const Cut& cut,
270  const SystShifts& shift,
271  const Var& wei) override {}
272 
273  void AccumulateExposures(const caf::SRSpillProxy* spill) override {};
274  };
275  /// \brief Dummy loader that doesn't load any files
276  ///
277  /// Useful when a loader is required for a component you want to ignore
279 }
size_t operator()(const std::pair< int, int > &x) const
Histograms that are filled from the nuTree.
Cuts and Vars for the 2020 FD DiF Study.
Definition: vars.h:6
void AddSpectrum(Spectrum &spect, const NuTruthVar &var, const NuTruthCut &cut, const SystShifts &shift, const NuTruthVar &wei=kNuTruthUnweighted) override
For use by the Spectrum constructor.
bool fGone
Has Go() been called? Can&#39;t add more histograms after that.
Simple record of shifts applied to systematic parameters.
Definition: SystShifts.h:20
Spectrum with the value of a second variable, allowing for reweighting
double fRunPOT
Crude measure, not including spill cuts.
const std::optional< NuTruthVar > var
void AddSpectrum(Spectrum &spect, const MultiVar &var, const Cut &cut, const SystShifts &shift, const Var &wei=kUnweighted) override
For use by the Spectrum constructor.
Representation of a spectrum in any variable, with associated POT.
Definition: Spectrum.h:40
std::unique_ptr< SpillCut > fSpillCut
Cut applied to the spill branch for every event.
std::set< IRecordSink * > fSpectrumSinks
const SpillVar kSpillUnweighted
Definition: Var.h:98
void AddSpectrum(Spectrum &spect, const NuTruthMultiVar &var, const NuTruthCut &cut, const SystShifts &shift, const NuTruthVar &wei=kNuTruthUnweighted) override
SpillHistDef(TH1 *_h, const SpillVar &_var, const SpillCut &_cut, const SpillVar &_wei=kSpillUnweighted)
void AddSpectrum(Spectrum &spect, const Var &var, const Cut &cut, const SystShifts &shift, const Var &wei=kUnweighted) override
For use by the Spectrum constructor.
std::vector< std::string > wildcard(const std::string &wildcardString)
Definition: convert.C:9
void AddReweightableSpectrum(ReweightableSpectrum &spect, const Var &xvar, const Var &yvar, const Cut &cut, const SystShifts &shift, const Var &wei) override
For use by the constructors of ReweightableSpectrum subclasses.
std::unordered_map< int, SystApplier * > fSinksS
std::list< SpillHistDef > fSpillHistDefs
std::unordered_map< std::pair< int, int >, CutApplier * > fSinksSC
Proxy for caf::SRSpill.
Definition: SRProxy.h:1346
Base class for the various types of spectrum loader.
const Cut cut
Definition: exporter_fd.C:30
size_t operator()(const std::tuple< int, int, int > &x) const
Interface class for accessing ROOT files in sequence.
Definition: IFileSource.h:10
std::unique_ptr< IFileSource > fFileSource
NuHistDef(Spectrum &_spect, const NuTruthMultiVar &_var, const NuTruthCut &_cut, const SystShifts &_shift, const NuTruthVar &_wei=kNuTruthUnweighted)
const std::optional< NuTruthMultiVar > multivar
const NuTruthVar kNuTruthUnweighted
Definition: Var.h:100
Helper for SpectrumLoaderBase. Details of SpillVar plots.
Dummy loader that doesn&#39;t load any files.
std::unordered_map< std::tuple< int, int, int >, WeightApplier * > fSinksSCW
_MultiVar< caf::SRNeutrinoProxy > NuTruthMultiVar
void AccumulateExposures(const caf::SRSpillProxy *spill) override
Template for Cut and SpillCut.
Definition: Cut.h:15
double fPOT
Accumulated by calls to GetNextFile.
Spectrum with true energy information, allowing it to be oscillated
virtual bool Gone() const
Indicate whether or not Go has been called.
std::list< NuHistDef > fNuHistDefs
const Var kUnweighted
The simplest possible Var, always 1. Used as a default weight.
Definition: Var.h:96
Spectrum with true L/E information, allowing it to be oscillated
static NullLoader kNullLoader
Dummy loader that doesn&#39;t load any files.
size_t operator()(const std::tuple< int, int, int, int > &x) const
NuHistDef(Spectrum &_spect, const NuTruthVar &_var, const NuTruthCut &_cut, const SystShifts &_shift, const NuTruthVar &_wei=kNuTruthUnweighted)
enum BeamMode string