All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SpectrumLoaderBase.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <functional>
4 #include <list>
5 #include <memory>
6 #include <set>
7 #include <string>
8 #include <vector>
9 
10 #include "sbnana/CAFAna/Core/Cut.h"
14 #include "sbnana/CAFAna/Core/Var.h"
15 
16 namespace caf{class StandardRecord;}
17 
18 class TFile;
19 class TH1;
20 class TTree;
21 
22 namespace ana
23 {
24  class Spectrum;
25  class ReweightableSpectrum;
26 
27  /// Is this data-file representing beam spills or cosmic spills?
28  enum DataSource{
29  // TODO there are no longer any actions taken as a result of this
30  // distinction. Remove after SA.
33  };
34 
35  /// Base class for the various types of spectrum loader
37  {
38  public:
39 
40  friend class ReweightableSpectrum;
41  friend class NDOscillatableSpectrum;
42  friend class OscillatableSpectrum;
43  friend class Spectrum;
44 
45  virtual ~SpectrumLoaderBase();
46 
47  /// For use by the \ref Spectrum constructor
48  virtual void AddSpectrum(Spectrum& spect,
49  const Var& var,
50  const SpillCut& spillcut,
51  const Cut& cut,
52  const SystShifts& shift,
53  const Var& wei = kUnweighted);
54 
55  /// For use by the \ref Spectrum constructor
56  virtual void AddSpectrum(Spectrum& spect,
57  const MultiVar& var,
58  const SpillCut& spillcut,
59  const Cut& cut,
60  const SystShifts& shift,
61  const Var& wei = kUnweighted);
62 
63  /// For use by the \ref Spectrum constructor
64  virtual void AddSpectrum(Spectrum& spect,
65  const SpillVar& var,
66  const SpillCut& cut,
67  const SpillVar& wei = kSpillUnweighted);
68 
69  /// For use by the \ref Spectrum constructor
70  virtual void AddSpectrum(Spectrum& spect,
71  const SpillMultiVar& var,
72  const SpillCut& cut,
73  const SpillVar& wei = kSpillUnweighted);
74 
75  /// For use by the constructors of \ref ReweightableSpectrum subclasses
77  const Var& var,
78  const Cut& cut,
79  const SystShifts& shift,
80  const Var& wei);
81 
82  /// For use by the constructors of \ref ReweightableSpectrum subclasses
84  const Var& var,
85  const SpillCut& spillcut,
86  const SliceCut& slicecut,
87  const SystShifts& shift,
88  const Var& wei);
89 
90  /// Load all the registered spectra
91  virtual void Go() = 0;
92 
93  /// Indicate whether or not \ref Go has been called
94  virtual bool Gone() const {return fGone;}
95 
96  protected:
97  /// Component of other constructors
99  /// Construct from a filename, wildcard, SAM definition, or SAM query
100  SpectrumLoaderBase(const std::string& wildcard, DataSource src = kBeam);
101  /// Construct from an explicit list of files
102  SpectrumLoaderBase(const std::vector<std::string>& fnames, DataSource src = kBeam);
103 
104  // Move operations
107 
108  // No copy operations because I don't want to deal with pointers
109  SpectrumLoaderBase(const SpectrumLoaderBase&) = delete;
111 
112  /// Figure out if \a str is a wildcard or SAM query and return a source
113  IFileSource* WildcardOrSAMQuery(const std::string& str) const;
114 
116  virtual void RemoveSpectrum(Spectrum*);
118 
119  /// Forwards to \ref fFileSource
120  int NFiles() const;
121 
122  TFile* GetNextFile();
123 
124  std::string fWildcard;
125  std::unique_ptr<IFileSource> fFileSource;
126 
128 
129  bool fGone; ///< Has Go() been called? Can't add more histograms after that
130 
131  double fPOT;
132  double fPOTFromHist; ///< Accumulated by calls to \ref GetNextFile
134 
135  /// \brief Helper class for \ref SpectrumLoaderBase
136  ///
137  /// List of Spectrum and OscillatableSpectrum, some utility functions
138  struct SpectList
139  {
140  void Erase(Spectrum* s);
141  void Erase(ReweightableSpectrum* os);
143  size_t TotalSize() const;
144  void GetSpectra(std::vector<Spectrum*>& ss);
145  void GetReweightableSpectra(std::vector<ReweightableSpectrum*>& ss);
146 
147  std::vector<Spectrum*> spects;
148  std::vector<ReweightableSpectrum*> rwSpects;
149  };
150 
151  /// \brief Helper class for \ref SpectrumLoaderBase
152  ///
153  /// Functions like std::map<T, U> except it should be faster to iterate
154  /// through the elements (while slower to fill) and it knows to compare Ts
155  /// via their ID() function. Various methods that forward through to the
156  /// \ref SpectList at the end of the chain.
157  template<class T, class U> struct IDMap
158  {
159  U& operator[](const T& key);
160 
161  // Make class iterable. Keep inline for speed
162  typedef typename std::vector<std::pair<T, U>>::iterator it_t;
163  inline it_t begin(){return fElems.begin();}
164  inline it_t end(){return fElems.end();}
165 
166  template<class V> void Erase(const V& v);
168  void Clear();
169  size_t TotalSize();
170  void GetSpectra(std::vector<Spectrum*>& ss);
171  void GetReweightableSpectra(std::vector<ReweightableSpectrum*>& ss);
172  protected:
173  std::vector<std::pair<T, U>> fElems;
174  };
175 
176  template<class T> class _VarOrMultiVar
177  {
178  public:
179  // v could easily be a temporary, have to make a copy
180  _VarOrMultiVar(const _Var<T>& v) : fVar(new _Var<T>(v)), fMultiVar(0) {}
181  _VarOrMultiVar(const _MultiVar<T>& v) : fVar(0), fMultiVar(new _MultiVar<T>(v)) {}
182  ~_VarOrMultiVar() {delete fVar; delete fMultiVar;}
183 
185  : fVar(v.fVar ? new _Var<T>(*v.fVar) : 0),
186  fMultiVar(v.fMultiVar ? new _MultiVar<T>(*v.fMultiVar) : 0)
187  {
188  }
189 
191  {
192  fVar = v.fVar;
193  fMultiVar = v.fMultiVar;
194  v.fVar = 0;
195  v.fMultiVar = 0;
196  }
197 
198  bool IsMulti() const {return fMultiVar;}
199  const _Var<T>& GetVar() const {assert(fVar); return *fVar;}
200  const _MultiVar<T>& GetMultiVar() const {assert(fMultiVar); return *fMultiVar;}
201 
202  int ID() const {return fVar ? fVar->ID() : fMultiVar->ID();}
203 
204  protected:
205  const _Var<T>* fVar;
207  };
208 
211 
212  /// \brief All the spectra that need to be filled
213  ///
214  /// [spillcut][shift][cut][wei][var]
216  /// [spillcut][spillwei][spillvar]
218  };
219 
220  /// \brief Dummy loader that doesn't load any files
221  ///
222  /// Useful when a loader is required for a component you want to ignore
224  {
225  public:
227  ~NullLoader();
228 
229  virtual void Go() override;
230  void AddSpectrum(Spectrum& spect,
231  const Var& var,
232  const SpillCut& spillcut,
233  const Cut& cut,
234  const SystShifts& shift,
235  const Var& wei = kUnweighted) override {}
236  void AddSpectrum(Spectrum& spect,
237  const MultiVar& var,
238  const SpillCut& spillcut,
239  const Cut& cut,
240  const SystShifts& shift,
241  const Var& wei = kUnweighted) override {}
242 
243  void AddSpectrum(Spectrum& spect,
244  const SpillVar& var,
245  const SpillCut& cut,
246  const SpillVar& wei = kSpillUnweighted) override {}
247 
248  void AddSpectrum(Spectrum& spect,
249  const SpillMultiVar& var,
250  const SpillCut& cut,
251  const SpillVar& wei = kSpillUnweighted) override {}
252 
254  const Var& var,
255  const Cut& cut,
256  const SystShifts& shift,
257  const Var& wei) override {}
258 
260  const Var& var,
261  const SpillCut& spillcut,
262  const Cut& cut,
263  const SystShifts& shift,
264  const Var& wei) override {}
265  };
266  /// \brief Dummy loader that doesn't load any files
267  ///
268  /// Useful when a loader is required for a component you want to ignore
270 }
std::vector< ReweightableSpectrum * > rwSpects
const SpillVar kSpillUnweighted([](const caf::SRSpillProxy *){return 1;})
void AddReweightableSpectrum(ReweightableSpectrum &spect, const Var &var, const Cut &cut, const SystShifts &shift, const Var &wei) override
Helper class for SpectrumLoaderBase.
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:16
Spectrum with the value of a second variable, allowing for reweighting
process_name opflashCryoW ana
_VarOrMultiVar< caf::SRSliceProxy > VarOrMultiVar
friend class NDOscillatableSpectrum
shift
Definition: fcl_checks.sh:26
virtual void Go() override
Load all the registered spectra.
void AddSpectrum(Spectrum &spect, const Var &var, const SpillCut &spillcut, const Cut &cut, const SystShifts &shift, const Var &wei=kUnweighted) override
void AddSpectrum(Spectrum &spect, const SpillVar &var, const SpillCut &cut, const SpillVar &wei=kSpillUnweighted) override
Representation of a spectrum in any variable, with associated POT.
Definition: Spectrum.h:30
void AddSpectrum(Spectrum &spect, const SpillMultiVar &var, const SpillCut &cut, const SpillVar &wei=kSpillUnweighted) override
std::vector< std::pair< T, U > > fElems
virtual void Go()=0
Load all the registered spectra.
std::vector< Spectrum * > spects
BEGIN_PROLOG V
double fPOTFromHist
Accumulated by calls to GetNextFile.
virtual void AddReweightableSpectrum(ReweightableSpectrum &spect, const Var &var, const Cut &cut, const SystShifts &shift, const Var &wei)
For use by the constructors of ReweightableSpectrum subclasses.
IDMap< SpillCut, IDMap< SpillVar, IDMap< SpillVarOrMultiVar, SpectList > > > fSpillHistDefs
[spillcut][spillwei][spillvar]
void AddReweightableSpectrum(ReweightableSpectrum &spect, const Var &var, const SpillCut &spillcut, const Cut &cut, const SystShifts &shift, const Var &wei) override
IDMap< SpillCut, IDMap< SystShifts, IDMap< Cut, IDMap< Var, IDMap< VarOrMultiVar, SpectList > > > > > fHistDefs
All the spectra that need to be filled.
void GetSpectra(std::vector< Spectrum * > &ss)
DataSource
Is this data-file representing beam spills or cosmic spills?
void GetReweightableSpectra(std::vector< ReweightableSpectrum * > &ss)
virtual void RemoveReweightableSpectrum(ReweightableSpectrum *)
SpectrumLoaderBase(DataSource src=kBeam)
Component of other constructors.
void RemoveLoader(SpectrumLoaderBase *l)
virtual void AddSpectrum(Spectrum &spect, const Var &var, const SpillCut &spillcut, const Cut &cut, const SystShifts &shift, const Var &wei=kUnweighted)
For use by the Spectrum constructor.
SpectrumLoaderBase & operator=(SpectrumLoaderBase &&)=default
Base class for the various types of spectrum loader.
Interface class for accessing ROOT files in sequence.
Definition: IFileSource.h:10
std::unique_ptr< IFileSource > fFileSource
_VarOrMultiVar< caf::SRSpillProxy > SpillVarOrMultiVar
const Var kUnweighted([](const caf::SRSliceProxy *){return 1;})
The simplest possible Var, always 1. Used as a default weight.
void GetSpectra(std::vector< Spectrum * > &ss)
then echo File list $list not found else cat $list while read file do echo $file sed s
Definition: file_to_url.sh:60
IFileSource * WildcardOrSAMQuery(const std::string &str) const
Figure out if str is a wildcard or SAM query and return a source.
virtual void RemoveSpectrum(Spectrum *)
Dummy loader that doesn&#39;t load any files.
void AddSpectrum(Spectrum &spect, const MultiVar &var, const SpillCut &spillcut, const Cut &cut, const SystShifts &shift, const Var &wei=kUnweighted) override
void RemoveLoader(SpectrumLoaderBase *l)
Helper class for SpectrumLoaderBase.
int NFiles() const
Forwards to fFileSource.
Template for Cut and SpillCut.
Definition: Cut.h:16
Most useful for combining weights.
Definition: Var.h:23
Spectrum with true energy information, allowing it to be oscillated
virtual bool Gone() const
Indicate whether or not Go has been called.
void GetReweightableSpectra(std::vector< ReweightableSpectrum * > &ss)
std::vector< std::pair< T, U > >::iterator it_t
static NullLoader kNullLoader
Dummy loader that doesn&#39;t load any files.
const _MultiVar< T > & GetMultiVar() const