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;
60  class WeightApplier;
61 
62  /// Base class for the various types of spectrum loader
64  {
65  public:
66 
67  friend class ReweightableSpectrum;
68  friend class NDOscillatableSpectrum;
69  friend class OscillatableSpectrum;
70  friend class Spectrum;
71 
72  virtual ~SpectrumLoaderBase();
73 
74  void SetSpillCut(const SpillCut& cut);
75 
76  /// For use by the \ref Spectrum constructor
77  virtual void AddSpectrum(Spectrum& spect,
78  const Var& var,
79  const Cut& cut,
80  const SystShifts& shift,
81  const Var& wei = kUnweighted);
82 
83  /// For use by the \ref Spectrum constructor
84  virtual void AddSpectrum(Spectrum& spect,
85  const MultiVar& var,
86  const Cut& cut,
87  const SystShifts& shift,
88  const Var& wei = kUnweighted);
89 
90  /// For use by the \ref Spectrum constructor
91  virtual void AddSpectrum(Spectrum& spect,
92  const NuTruthVar& var,
93  const NuTruthCut& cut,
94  const SystShifts& shift,
95  const NuTruthVar& wei = kNuTruthUnweighted);
96 
97  /// For use by the constructors of \ref ReweightableSpectrum subclasses
98  virtual void AddReweightableSpectrum(ReweightableSpectrum& spect,
99  const Var& var,
100  const Cut& cut,
101  const SystShifts& shift,
102  const Var& wei);
103 
104  /// Uses include counting the total POT or spills in a run
105  virtual void AddSpillHistogram(TH1* h,
106  const SpillVar& var,
107  const SpillCut& cut,
108  const SpillVar& wei = kSpillUnweighted);
109 
110  /// Load all the registered spectra
111  virtual void Go() = 0;
112 
113  /// Indicate whether or not \ref Go has been called
114  virtual bool Gone() const {return fGone;}
115 
116  double GetWeightedPOT();
117 
118  protected:
119  /// Component of other constructors
121  /// Construct from a filename, wildcard, SAM definition, or SAM query
123  /// Construct from an explicit list of files
124  SpectrumLoaderBase(const std::vector<std::string>& fnames);
125 
126  // Move operations
128  SpectrumLoaderBase& operator=(SpectrumLoaderBase&&) = default;
129 
130  // No copy operations because I don't want to deal with pointers
131  SpectrumLoaderBase(const SpectrumLoaderBase&) = delete;
132  SpectrumLoaderBase& operator=(const SpectrumLoaderBase&) = delete;
133 
134  /// Figure out if \a str is a wildcard or SAM query and return a source
135  IFileSource* WildcardOrSAMQuery(const std::string& str) const;
136 
137  virtual void AccumulateExposures(const caf::SRSpillProxy* spill) = 0;
138 
139  /// Forwards to \ref fFileSource
140  int NFiles() const;
141 
142  /// Forwards to \ref fFileSource but also accumulates POT and livetime
143  TFile* GetNextFile();
144 
146  std::unique_ptr<IFileSource> fFileSource;
147 
148  bool fGone; ///< Has Go() been called? Can't add more histograms after that
149 
150  double fPOT; ///< Accumulated by calls to \ref GetNextFile
151  double fRunPOT; ///< Crude measure, not including spill cuts
152 
153  double fSinglePOT;
154  double fBeamNue;
155  double fSingleNue;
156  double fWeightedPOT;
157 
158  WeightApplier* GetSinkSCW(const SystShifts& shift,
159  const Cut& cut,
160  const Var& wei);
161 
162  std::set<IRecordSink*> fSpectrumSinks;
163 
164  std::unordered_map<std::tuple<int, int, int>, WeightApplier*> fSinksSCW;
165  std::unordered_map<std::pair<int, int>, CutApplier*> fSinksSC;
166  std::unordered_map<int, SystApplier*> fSinksS;
167 
169 
171 
172  /// Helper for \ref SpectrumLoaderBase. Details of SpillVar plots
174  {
175  SpillHistDef(TH1* _h,
176  const SpillVar& _var,
177  const SpillCut& _cut,
178  const SpillVar& _wei = kSpillUnweighted)
179  : h(_h), var(_var), cut(_cut), wei(_wei)
180  {
181  }
182 
183  TH1* h;
184  const SpillVar var;
185  const SpillCut cut;
186  const SpillVar wei;
187  };
188 
189  /// Histograms that are filled from the nuTree
190  struct NuHistDef
191  {
193  const NuTruthVar& _var,
194  const NuTruthCut& _cut,
195  const SystShifts& _shift,
196  const NuTruthVar& _wei = kNuTruthUnweighted)
197  : spect(_spect), var(_var), cut(_cut), shift(_shift), wei(_wei)
198  {
199  }
200 
206  };
207 
208  std::list<SpillHistDef> fSpillHistDefs;
209  std::list<NuHistDef> fNuHistDefs;
210 
211  /// Cut applied to the spill branch for every event
212  std::unique_ptr<SpillCut> fSpillCut;
213  };
214 
215  /// \brief Dummy loader that doesn't load any files
216  ///
217  /// Useful when a loader is required for a component you want to ignore
219  {
220  public:
222  ~NullLoader();
223 
224  virtual void Go() override;
225  void AddSpectrum(Spectrum& spect,
226  const Var& var,
227  const Cut& cut,
228  const SystShifts& shift,
229  const Var& wei = kUnweighted) override {}
230  void AddSpectrum(Spectrum& spect,
231  const MultiVar& var,
232  const Cut& cut,
233  const SystShifts& shift,
234  const Var& wei = kUnweighted) override {}
235  void AddSpectrum(Spectrum& spect,
236  const NuTruthVar& var,
237  const NuTruthCut& cut,
238  const SystShifts& shift,
239  const NuTruthVar& wei = kNuTruthUnweighted) override {}
241  const Var& var,
242  const Cut& cut,
243  const SystShifts& shift,
244  const Var& wei) override {}
245 
246  void AccumulateExposures(const caf::SRSpillProxy* spill) override {};
247  };
248  /// \brief Dummy loader that doesn't load any files
249  ///
250  /// Useful when a loader is required for a component you want to ignore
252 }
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.
void AddReweightableSpectrum(ReweightableSpectrum &spect, const Var &var, const Cut &cut, const SystShifts &shift, const Var &wei) override
For use by the constructors of ReweightableSpectrum subclasses.
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.
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:33
std::unique_ptr< SpillCut > fSpillCut
Cut applied to the spill branch for every event.
std::set< IRecordSink * > fSpectrumSinks
const SpillVar kSpillUnweighted
Definition: Var.h:102
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
std::unordered_map< int, SystApplier * > fSinksS
std::list< SpillHistDef > fSpillHistDefs
std::unordered_map< std::pair< int, int >, CutApplier * > fSinksSC
Base class for the various types of spectrum loader.
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
const Cut cut
Definition: exporter_fd.C:30
Template for Cut and SpillCut.
Definition: Cut.h:15
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
const NuTruthVar kNuTruthUnweighted
Definition: Var.h:104
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
void AccumulateExposures(const caf::SRSpillProxy *spill) override
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:100
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)