All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
ana::SpectrumLoader Class Reference

Collaborates with Spectrum and OscillatableSpectrum to fill spectra from CAF files. More...

#include <SpectrumLoader.h>

Inheritance diagram for ana::SpectrumLoader:
ana::SpectrumLoaderBase

Public Member Functions

 SpectrumLoader (const std::string &wildcard, DataSource src=kBeam, int max=0)
 
 SpectrumLoader (const std::vector< std::string > &fnames, DataSource src=kBeam, int max=0)
 
virtual ~SpectrumLoader ()
 
virtual void Go () override
 Load all the registered spectra. More...
 
- Public Member Functions inherited from ana::SpectrumLoaderBase
virtual ~SpectrumLoaderBase ()
 
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. More...
 
virtual void AddSpectrum (Spectrum &spect, const MultiVar &var, const SpillCut &spillcut, const Cut &cut, const SystShifts &shift, const Var &wei=kUnweighted)
 For use by the Spectrum constructor. More...
 
virtual void AddSpectrum (Spectrum &spect, const SpillVar &var, const SpillCut &cut, const SpillVar &wei=kSpillUnweighted)
 For use by the Spectrum constructor. More...
 
virtual void AddSpectrum (Spectrum &spect, const SpillMultiVar &var, const SpillCut &cut, const SpillVar &wei=kSpillUnweighted)
 For use by the Spectrum constructor. More...
 
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. More...
 
virtual void AddReweightableSpectrum (ReweightableSpectrum &spect, const Var &var, const SpillCut &spillcut, const SliceCut &slicecut, const SystShifts &shift, const Var &wei)
 For use by the constructors of ReweightableSpectrum subclasses. More...
 
virtual bool Gone () const
 Indicate whether or not Go has been called. More...
 

Static Public Member Functions

static SpectrumLoader FromSAMProject (const std::string &proj, DataSource src=kBeam, int fileLimit=-1)
 Named constructor for SAM projects. More...
 

Protected Member Functions

 SpectrumLoader (DataSource src=kBeam)
 
 SpectrumLoader (SpectrumLoader &&)=default
 
SpectrumLoaderoperator= (SpectrumLoader &&)=default
 
 SpectrumLoader (const SpectrumLoader &)=delete
 
SpectrumLoaderoperator= (const SpectrumLoader &)=delete
 
virtual void HandleFile (TFile *f, Progress *prog=0)
 
virtual void HandleRecord (caf::SRSpillProxy *sr)
 
virtual void StoreExposures ()
 Save accumulated exposures into the individual spectra. More...
 
- Protected Member Functions inherited from ana::SpectrumLoaderBase
 SpectrumLoaderBase (DataSource src=kBeam)
 Component of other constructors. More...
 
 SpectrumLoaderBase (const std::string &wildcard, DataSource src=kBeam)
 Construct from a filename, wildcard, SAM definition, or SAM query. More...
 
 SpectrumLoaderBase (const std::vector< std::string > &fnames, DataSource src=kBeam)
 Construct from an explicit list of files. More...
 
 SpectrumLoaderBase (SpectrumLoaderBase &&)=default
 
SpectrumLoaderBaseoperator= (SpectrumLoaderBase &&)=default
 
 SpectrumLoaderBase (const SpectrumLoaderBase &)=delete
 
SpectrumLoaderBaseoperator= (const SpectrumLoaderBase &)=delete
 
IFileSourceWildcardOrSAMQuery (const std::string &str) const
 Figure out if str is a wildcard or SAM query and return a source. More...
 
virtual void RemoveSpectrum (Spectrum *)
 
virtual void RemoveReweightableSpectrum (ReweightableSpectrum *)
 
int NFiles () const
 Forwards to fFileSource. More...
 
TFile * GetNextFile ()
 

Protected Attributes

int max_entries
 All unique cuts contained in fHistDefs. More...
 
- Protected Attributes inherited from ana::SpectrumLoaderBase
std::string fWildcard
 
std::unique_ptr< IFileSourcefFileSource
 
DataSource fSource
 
bool fGone
 Has Go() been called? Can't add more histograms after that. More...
 
double fPOT
 
double fPOTFromHist
 Accumulated by calls to GetNextFile. More...
 
int fNReadouts
 
IDMap< SpillCut, IDMap
< SystShifts, IDMap< Cut,
IDMap< Var, IDMap
< VarOrMultiVar, SpectList > > > > > 
fHistDefs
 All the spectra that need to be filled. More...
 
IDMap< SpillCut, IDMap
< SpillVar, IDMap
< SpillVarOrMultiVar,
SpectList > > > 
fSpillHistDefs
 [spillcut][spillwei][spillvar] More...
 

Additional Inherited Members

- Protected Types inherited from ana::SpectrumLoaderBase
typedef _VarOrMultiVar
< caf::SRSliceProxy
VarOrMultiVar
 
typedef _VarOrMultiVar
< caf::SRSpillProxy
SpillVarOrMultiVar
 

Detailed Description

Collaborates with Spectrum and OscillatableSpectrum to fill spectra from CAF files.

The outside user should just pass a filename, wildcard, SAM dataset name, or query to the constructor. Then construct all the spectra you need. They will register with this loader. Finally, calling Go will cause all the spectra to be filled at once. After this the loader may not be used again.

Definition at line 19 of file SpectrumLoader.h.

Constructor & Destructor Documentation

ana::SpectrumLoader::SpectrumLoader ( const std::string &  wildcard,
DataSource  src = kBeam,
int  max = 0 
)

Definition at line 24 of file SpectrumLoader.cxx.

25  : SpectrumLoaderBase(wildcard, src), max_entries(max)
26  {
27  }
int max_entries
All unique cuts contained in fHistDefs.
SpectrumLoaderBase(DataSource src=kBeam)
Component of other constructors.
ana::SpectrumLoader::SpectrumLoader ( const std::vector< std::string > &  fnames,
DataSource  src = kBeam,
int  max = 0 
)

Definition at line 30 of file SpectrumLoader.cxx.

32  : SpectrumLoaderBase(fnames, src), max_entries(max)
33  {
34  }
int max_entries
All unique cuts contained in fHistDefs.
SpectrumLoaderBase(DataSource src=kBeam)
Component of other constructors.
ana::SpectrumLoader::~SpectrumLoader ( )
virtual

Definition at line 55 of file SpectrumLoader.cxx.

56  {
57  }
ana::SpectrumLoader::SpectrumLoader ( DataSource  src = kBeam)
protected

Definition at line 37 of file SpectrumLoader.cxx.

38  : SpectrumLoaderBase(src)
39  {
40  }
SpectrumLoaderBase(DataSource src=kBeam)
Component of other constructors.
ana::SpectrumLoader::SpectrumLoader ( SpectrumLoader &&  )
protecteddefault
ana::SpectrumLoader::SpectrumLoader ( const SpectrumLoader )
protecteddelete

Member Function Documentation

SpectrumLoader ana::SpectrumLoader::FromSAMProject ( const std::string &  proj,
DataSource  src = kBeam,
int  fileLimit = -1 
)
static

Named constructor for SAM projects.

Definition at line 43 of file SpectrumLoader.cxx.

46  {
47  SpectrumLoader ret;
48  ret.fSource = src;
49  ret.fWildcard = "project "+proj;
50  ret.fFileSource = std::unique_ptr<IFileSource>(new SAMProjectSource(proj, fileLimit));
51  return ret;
52  }
SpectrumLoader(const std::string &wildcard, DataSource src=kBeam, int max=0)
void ana::SpectrumLoader::Go ( )
overridevirtual

Load all the registered spectra.

Implements ana::SpectrumLoaderBase.

Definition at line 68 of file SpectrumLoader.cxx.

69  {
70  if(fGone){
71  std::cerr << "Error: can only call Go() once on a SpectrumLoader" << std::endl;
72  abort();
73  }
74  fGone = true;
75 
76  // Find all the unique cuts
77  // std::set<Cut, CompareByID> cuts;
78  // for(auto& shiftdef: fHistDefs)
79  // for(auto& cutdef: shiftdef.second)
80  // cuts.insert(cutdef.first);
81  // for(const Cut& cut: cuts) fAllCuts.push_back(cut);
82 
83  // fLivetimeByCut.resize(fAllCuts.size());
84  // fPOTByCut.resize(fAllCuts.size());
85 
86 
87  const int Nfiles = NFiles();
88 
89  Progress* prog = 0;
90 
91  caf::SRBranchRegistry::clear();
92 
93  int fileIdx = -1;
94  while(TFile* f = GetNextFile()){
95  ++fileIdx;
96 
97  if(Nfiles >= 0 && !prog) prog = new Progress(TString::Format("Filling %lu spectra from %d files matching '%s'", fHistDefs.TotalSize(), Nfiles, fWildcard.c_str()).Data());
98 
99  HandleFile(f, Nfiles == 1 ? prog : 0);
100 
101  if(Nfiles > 1 && prog) prog->SetProgress((fileIdx+1.)/Nfiles);
102  } // end for fileIdx
103 
104  if(prog){
105  prog->Done();
106  delete prog;
107  }
108 
109  StoreExposures(); // also triggers the POT printout
110 
111  fHistDefs.RemoveLoader(this);
112  fHistDefs.Clear();
113  }
virtual void StoreExposures()
Save accumulated exposures into the individual spectra.
BEGIN_PROLOG could also be cerr
bool fGone
Has Go() been called? Can&#39;t add more histograms after that.
IDMap< SpillCut, IDMap< SystShifts, IDMap< Cut, IDMap< Var, IDMap< VarOrMultiVar, SpectList > > > > > fHistDefs
All the spectra that need to be filled.
virtual void HandleFile(TFile *f, Progress *prog=0)
int NFiles() const
Forwards to fFileSource.
prog
Definition: just_cmake.sh:3
void ana::SpectrumLoader::HandleFile ( TFile *  f,
Progress prog = 0 
)
protectedvirtual

Definition at line 116 of file SpectrumLoader.cxx.

117  {
118  assert(!f->IsZombie());
119 
120  TTree* tr = (TTree*)f->Get("recTree");
121  assert(tr);
122 
123  // We try to access this field for every record. It was only added to the
124  // files in late 2021, and we don't want to render all earlier files
125  // unusable at a stroke. This logic can safely be removed once all extant
126  // files have such a field (estimate mid-2022?)
127  const bool has_husk = tr->GetLeaf("rec.hdr.husk");
128 
129  caf::SRSpillProxy sr(tr, "rec");
130 
131  // FloatingExceptionOnNaN fpnan;
132 
133  long Nentries = tr->GetEntries();
134  if (max_entries != 0 && max_entries < Nentries) Nentries = max_entries;
135 
136  for(long n = 0; n < Nentries; ++n){
137  tr->LoadTree(n);
138 
139  // If there is no husk field there is no concept of husk events
140  if(!has_husk) sr.hdr.husk = false;
141 
142  HandleRecord(&sr);
143 
144  if(prog) prog->SetProgress(double(n)/Nentries);
145  } // end for n
146  }
int max_entries
All unique cuts contained in fHistDefs.
caf::Proxy< caf::StandardRecord > SRSpillProxy
Definition: EpilogFwd.h:3
virtual void HandleRecord(caf::SRSpillProxy *sr)
prog
Definition: just_cmake.sh:3
void ana::SpectrumLoader::HandleRecord ( caf::SRSpillProxy sr)
protectedvirtual

Definition at line 177 of file SpectrumLoader.cxx.

178  {
179  if(sr->hdr.first_in_subrun){
180  fPOT += sr->hdr.pot;
181  // TODO think about if this should be gated behind first_in_file. At the
182  // moment I think these will be synonymous. And despite the comment on
183  // hdr.pot, I think it may be file-based in practice too.
184  if(sr->hdr.ismc) fNReadouts += sr->hdr.ngenevt;
185  }
186 
187  if(!sr->hdr.ismc){
188  const int nbnb = sr->hdr.bnbinfo.size();
189  const int nnumi = sr->hdr.numiinfo.size();
190  if(nbnb > 0 && nnumi > 0){
191  std::cout << "SpectrumLoader: nonzero number of both BNB (" << nbnb
192  << ") and NuMI (" << nnumi << ") triggers. I'm confused"
193  << std::endl;
194  abort();
195  }
196 
197  fNReadouts += nbnb + nnumi;
198  }
199 
200  // This record was only kept as a receptacle for exposure information. It
201  // shouldn't be included in any selected spectra.
202  if(sr->hdr.husk) return;
203 
204  // Do the spill-level spectra first. Keep this very simple because we
205  // intend to change it.
206  for(auto& spillcutdef: fSpillHistDefs){
207  if(!spillcutdef.first(sr)) continue;
208  for(auto& spillweidef: spillcutdef.second){
209  const double wei = spillweidef.first(sr);
210  if(wei == 0) continue;
211  for(auto spillvardef: spillweidef.second){
212  if(spillvardef.first.IsMulti()){
213  for(double val: spillvardef.first.GetMultiVar()(sr)){
214  for(Spectrum* s: spillvardef.second.spects) s->Fill(val, wei);
215  }
216  }
217  else{
218  const double val = spillvardef.first.GetVar()(sr);
219  for(Spectrum* s: spillvardef.second.spects) s->Fill(val, wei);
220  }
221  }
222  }
223  }
224 
225 
226  for(auto& spillcutdef: fHistDefs){
227  const SpillCut& spillcut = spillcutdef.first;
228 
229  const bool spillpass = spillcut(sr); // nomSpillCutCache.Get(spillcut, sr);
230  // Cut failed, skip all the histograms that depended on it
231  if(!spillpass) continue;
232 
233  for(caf::SRSliceProxy& slc: sr->slc){
234 
235  // Some shifts only adjust the weight, so they're effectively nominal,
236  // but aren't grouped with the other nominal histograms. Keep track of
237  // the results for nominals in these caches to speed those systs up.
238  CutVarCache<bool, Cut, caf::SRSliceProxy> nomCutCache;
239  CutVarCache<double, Var, caf::SRSliceProxy> nomWeiCache;
240  CutVarCache<double, Var, caf::SRSliceProxy> nomVarCache;
241 
242  for(auto& shiftdef: spillcutdef.second){
243  const SystShifts& shift = shiftdef.first;
244 
245  // Need to provide a clean slate for each new set of systematic
246  // shifts to work from. Copying the whole StandardRecord is pretty
247  // expensive, so modify it in place and revert it afterwards.
248 
249  caf::SRProxySystController::BeginTransaction();
250 
251  bool shifted = false;
252 
253  double systWeight = 1;
254  // Can special-case nominal to not pay cost of Shift()
255  if(!shift.IsNominal()){
256  shift.Shift(&slc, systWeight);
257  // If there were only weighting systs applied then the cached
258  // nominal values are still valid.
259  shifted = caf::SRProxySystController::AnyShifted();
260  }
261 
262  for(auto& cutdef: shiftdef.second){
263  const Cut& cut = cutdef.first;
264 
265  const bool pass = shifted ? cut(&slc) : nomCutCache.Get(cut, &slc);
266  // Cut failed, skip all the histograms that depended on it
267  if(!pass) continue;
268 
269  for(auto& weidef: cutdef.second){
270  const Var& weivar = weidef.first;
271 
272  double wei = shifted ? weivar(&slc) : nomWeiCache.Get(weivar, &slc);
273 
274  wei *= systWeight;
275  if(wei == 0) continue;
276 
277  for(auto& vardef: weidef.second){
278  if(vardef.first.IsMulti()){
279  for(double val: vardef.first.GetMultiVar()(&slc)){
280  for(Spectrum* s: vardef.second.spects)
281  s->Fill(val, wei);
282  }
283  continue;
284  }
285 
286  const Var& var = vardef.first.GetVar();
287 
288  const double val = shifted ? var(&slc) : nomVarCache.Get(var, &slc);
289 
290  if(std::isnan(val) || std::isinf(val)){
291  std::cerr << "Warning: Bad value: " << val
292  << " returned from a Var. The input variable(s) could "
293  << "be NaN in the CAF, or perhaps your "
294  << "Var code computed 0/0?";
295  std::cout << " Not filling into this histogram for this slice." << std::endl;
296  continue;
297  }
298 
299  for(Spectrum* s: vardef.second.spects) s->Fill(val, wei);
300 
301  for(ReweightableSpectrum* rw: vardef.second.rwSpects){
302  const double yval = rw->ReweightVar()(&slc);
303 
304  if(std::isnan(yval) || std::isinf(yval)){
305  std::cerr << "Warning: Bad value: " << yval
306  << " for reweighting Var";
307  std::cout << ". Not filling into histogram." << std::endl;
308  continue;
309  }
310 
311  rw->fHist->Fill(val, yval, wei);
312  } // end for rw
313  } // end for vardef
314  } // end for weidef
315  } // end for cutdef
316 
317  // Return StandardRecord to its unshifted form ready for the next
318  // histogram.
319  caf::SRProxySystController::Rollback();
320  } // end for shiftdef
321  } // end for slc
322  } // end for spillcutdef
323  }
BEGIN_PROLOG could also be cerr
shift
Definition: fcl_checks.sh:26
caf::Proxy< caf::SRSlice > SRSliceProxy
Definition: EpilogFwd.h:2
_Var< caf::SRSliceProxy > Var
Representation of a variable to be retrieved from a caf::StandardRecord object.
Definition: Var.h:73
_Cut< caf::SRSliceProxy > Cut
Definition: Cut.h:95
IDMap< SpillCut, IDMap< SpillVar, IDMap< SpillVarOrMultiVar, SpectList > > > fSpillHistDefs
[spillcut][spillwei][spillvar]
IDMap< SpillCut, IDMap< SystShifts, IDMap< Cut, IDMap< Var, IDMap< VarOrMultiVar, SpectList > > > > > fHistDefs
All the spectra that need to be filled.
_Cut< caf::SRSpillProxy > SpillCut
Equivalent of Cut acting on caf::SRSpill. For use in spill-by-spill data quality cuts.
Definition: Cut.h:99
then echo File list $list not found else cat $list while read file do echo $file sed s
Definition: file_to_url.sh:60
BEGIN_PROLOG could also be cout
SpectrumLoader& ana::SpectrumLoader::operator= ( SpectrumLoader &&  )
protecteddefault
SpectrumLoader& ana::SpectrumLoader::operator= ( const SpectrumLoader )
protecteddelete
void ana::SpectrumLoader::StoreExposures ( )
protectedvirtual

Save accumulated exposures into the individual spectra.

Definition at line 326 of file SpectrumLoader.cxx.

327  {
328  if(fabs(fPOT - fPOTFromHist)/std::min(fPOT, fPOTFromHist) > 0.001){
329  std::cout << fPOT << " POT from hdr differs from " << fPOTFromHist << " POT from the TotalPOT histogram!" << std::endl;
330  abort();
331  }
332 
333  std::cout << fPOT << " POT over " << fNReadouts << " readouts" << std::endl;
334 
335  for(auto& shiftdef: fHistDefs){
336  for(auto& spillcutdef: shiftdef.second){
337  for(auto& cutdef: spillcutdef.second){
338  for(auto& weidef: cutdef.second){
339  for(auto& vardef: weidef.second){
340  for(Spectrum* s: vardef.second.spects){
341  s->fPOT += fPOT;
342  s->fLivetime += fNReadouts;
343  }
344  for(ReweightableSpectrum* rw: vardef.second.rwSpects){
345  rw->fPOT += fPOT;
346  rw->fLivetime += fNReadouts;
347  }
348  }
349  }
350  }
351  }
352  }
353 
354 
355  for(auto& spillcutdef: fSpillHistDefs){
356  for(auto& spillweidef: spillcutdef.second){
357  for(auto spillvardef: spillweidef.second){
358  for(Spectrum* s: spillvardef.second.spects){
359  s->fPOT += fPOT;
360  s->fLivetime += fNReadouts;
361  }
362  }
363  }
364  }
365 
366  }
double fPOTFromHist
Accumulated by calls to GetNextFile.
IDMap< SpillCut, IDMap< SpillVar, IDMap< SpillVarOrMultiVar, SpectList > > > fSpillHistDefs
[spillcut][spillwei][spillvar]
IDMap< SpillCut, IDMap< SystShifts, IDMap< Cut, IDMap< Var, IDMap< VarOrMultiVar, SpectList > > > > > fHistDefs
All the spectra that need to be filled.
then echo File list $list not found else cat $list while read file do echo $file sed s
Definition: file_to_url.sh:60
BEGIN_PROLOG could also be cout

Member Data Documentation

int ana::SpectrumLoader::max_entries
protected

All unique cuts contained in fHistDefs.

Definition at line 55 of file SpectrumLoader.h.


The documentation for this class was generated from the following files: