All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SpectrumLoaderBase.cxx
Go to the documentation of this file.
2 
10 
11 #include "sbnanaobj/StandardRecord/Proxy/SRProxy.h"
12 
13 #include "ifdh.h"
14 
15 #include "TFile.h"
16 #include "TH1.h"
17 #include "TTree.h"
18 
19 #include <algorithm>
20 #include <cassert>
21 #include <iostream>
22 #include "boost/algorithm/string.hpp"
23 
24 namespace ana
25 {
26  //----------------------------------------------------------------------
28  {
29  auto it = std::find(spects.begin(), spects.end(), s);
30  if(it != spects.end()) spects.erase(it);
31  }
32 
33  //----------------------------------------------------------------------
35  {
36  auto it = std::find(rwSpects.begin(), rwSpects.end(), rs);
37  if(it != rwSpects.end()) rwSpects.erase(it);
38  }
39 
40  //----------------------------------------------------------------------
42  {
43  for(Spectrum* s: spects) s->RemoveLoader(l);
44  for(ReweightableSpectrum* rs: rwSpects) rs->RemoveLoader(l);
45  }
46 
47  //----------------------------------------------------------------------
49  {
50  return spects.size() + rwSpects.size();
51  }
52 
53  //----------------------------------------------------------------------
54  void SpectrumLoaderBase::SpectList::GetSpectra(std::vector<Spectrum*>& ss)
55  {
56  ss.insert(ss.end(), spects.begin(), spects.end());
57  }
58 
59  //----------------------------------------------------------------------
61  GetReweightableSpectra(std::vector<ReweightableSpectrum*>& ss)
62  {
63  ss.insert(ss.end(), rwSpects.begin(), rwSpects.end());
64  }
65 
66  //----------------------------------------------------------------------
67  template<class T, class U> U& SpectrumLoaderBase::IDMap<T, U>::
68  operator[](const T& key)
69  {
70  for(auto& it: fElems){
71  if(it.first.ID() == key.ID()) return it.second;
72  }
73  fElems.push_back(std::make_pair(key, U()));
74  return fElems.back().second;
75  }
76 
77  //----------------------------------------------------------------------
78  template<class T, class U> template<class V> void SpectrumLoaderBase::IDMap<T, U>::Erase(const V& v)
79  {
80  for(auto& it: fElems) it.second.Erase(v);
81  }
82 
83  //----------------------------------------------------------------------
84  template<class T, class U> void SpectrumLoaderBase::IDMap<T, U>::
86  {
87  for(auto& it: fElems) it.second.RemoveLoader(l);
88  }
89 
90  //----------------------------------------------------------------------
91  template<class T, class U> void SpectrumLoaderBase::IDMap<T, U>::Clear()
92  {
93  fElems.clear();
94  }
95 
96  //----------------------------------------------------------------------
97  template<class T, class U> size_t SpectrumLoaderBase::IDMap<T, U>::
99  {
100  size_t ret = 0;
101  for(auto& it: fElems) ret += it.second.TotalSize();
102  return ret;
103  }
104 
105  //----------------------------------------------------------------------
106  template<class T, class U> void SpectrumLoaderBase::IDMap<T, U>::
107  GetSpectra(std::vector<Spectrum*>& ss)
108  {
109  for(auto& it: fElems) it.second.GetSpectra(ss);
110  }
111 
112  //----------------------------------------------------------------------
113  template<class T, class U> void SpectrumLoaderBase::IDMap<T, U>::
114  GetReweightableSpectra(std::vector<ReweightableSpectrum*>& ss)
115  {
116  for(auto& it: fElems) it.second.GetReweightableSpectra(ss);
117  }
118 
119  // Start of SpectrumLoaderBase proper
120 
121  //----------------------------------------------------------------------
123  : fSource(src), fGone(false), fPOT(0), fPOTFromHist(0), fNReadouts(0)
124  {
125  }
126 
127  //----------------------------------------------------------------------
128  SpectrumLoaderBase::SpectrumLoaderBase(const std::string& wildcard,
129  DataSource src)
130  : SpectrumLoaderBase(src)
131  {
132  fWildcard = wildcard;
133  fFileSource = std::unique_ptr<IFileSource>(WildcardOrSAMQuery(wildcard));
134  }
135 
136  //----------------------------------------------------------------------
137  SpectrumLoaderBase::SpectrumLoaderBase(const std::vector<std::string>& fnames,
138  DataSource src)
139  : SpectrumLoaderBase(src)
140  {
141  fWildcard = "file list";
142  fFileSource = std::unique_ptr<IFileSource>(new FileListSource(fnames));
143 
144  assert(!fnames.empty());
145  std::cout << "Loading from " << fnames.size() << " files" << std::endl;
146  }
147 
148  //----------------------------------------------------------------------
150  {
151  fHistDefs.RemoveLoader(this);
152  }
153 
154  //----------------------------------------------------------------------
156  WildcardOrSAMQuery(const std::string& str) const
157  {
158  int stride = -1;
159  int offset = -1;
160  if(getenv("CAFANA_STRIDE")){
161  stride = atoi(getenv("CAFANA_STRIDE"));
162  if(stride > 1 && getenv("CAFANA_OFFSET")){
163  offset = atoi(getenv("CAFANA_OFFSET"));
164  }
165  }
166 
167  // stat() blows up on strings with spaces
168  if(str.find(' ') == std::string::npos){
169  WildcardSource* ret = new WildcardSource(str, stride, offset);
170  if(ret->NFiles() > 0) return ret;
171  delete ret;
172  }
173 
174  // Maybe this the name of a SAM project?
175  {
176  IFDHSilent silent; // the usual case is for this to fail
177  ifdh_ns::ifdh i;
178  // findProject always gives back an address just by gluing bits together.
179  // (a tad annoying, because it _does_ go and look for the project, and
180  // even would print its 'didn't-find-this-project' error out to stderr if
181  // not for the IFDHSilent, but without scraping its stderr there's no way
182  // to know whether the project is there or not -- you still get the URL.)
183  // however, the WebAPI call looking for the /status will return a 404 if
184  // the project doesn't exist. (suggested by Robert I. in
185  // INC000000925362)
186  try{
187  ifdh_util_ns::WebAPI webapi(i.findProject(str, SAMExperiment()) + "/status");
188  return new SAMProjectSource(str);
189  }
190  catch(ifdh_util_ns::WebAPIException &e){
191  ;
192  }
193  }
194 
195  // Maybe this is a SAM dataset or query?
196  return new SAMQuerySource(str, stride, offset);
197  }
198 
199  //----------------------------------------------------------------------
201  const Var& var,
202  const SpillCut& spillcut,
203  const Cut& cut,
204  const SystShifts& shift,
205  const Var& wei)
206  {
207  if(fGone){
208  std::cerr << "Error: can't add Spectra after the call to Go()" << std::endl;
209  abort();
210  }
211 
212  fHistDefs[spillcut][shift][cut][wei][var].spects.push_back(&spect);
213 
214  spect.AddLoader(this); // Remember we have a Go() pending
215  }
216 
217  //----------------------------------------------------------------------
219  const MultiVar& var,
220  const SpillCut& spillcut,
221  const Cut& cut,
222  const SystShifts& shift,
223  const Var& wei)
224  {
225  if(fGone){
226  std::cerr << "Error: can't add Spectra after the call to Go()" << std::endl;
227  abort();
228  }
229 
230  fHistDefs[spillcut][shift][cut][wei][var].spects.push_back(&spect);
231 
232  spect.AddLoader(this); // Remember we have a Go() pending
233  }
234 
235  //----------------------------------------------------------------------
237  const SpillVar& var,
238  const SpillCut& cut,
239  const SpillVar& wei)
240  {
241  if(fGone){
242  std::cerr << "Error: can't add Spectra after the call to Go()" << std::endl;
243  abort();
244  }
245 
246  fSpillHistDefs[cut][wei][var].spects.push_back(&spect);
247 
248  spect.AddLoader(this); // Remember we have a Go() pending
249  }
250 
251  //----------------------------------------------------------------------
253  const SpillMultiVar& var,
254  const SpillCut& cut,
255  const SpillVar& wei)
256  {
257  if(fGone){
258  std::cerr << "Error: can't add Spectra after the call to Go()" << std::endl;
259  abort();
260  }
261 
262  fSpillHistDefs[cut][wei][var].spects.push_back(&spect);
263 
264  spect.AddLoader(this); // Remember we have a Go() pending
265  }
266 
267  //----------------------------------------------------------------------
269  {
270  fHistDefs.Erase(spect);
271  }
272 
273  //----------------------------------------------------------------------
275  const Var& var,
276  const Cut& cut,
277  const SystShifts& shift,
278  const Var& wei)
279  {
280  if(fGone){
281  std::cerr << "Error: can't add Spectra after the call to Go()" << std::endl;
282  abort();
283  }
284 
285  fHistDefs[kNoSpillCut][shift][cut][wei][var].rwSpects.push_back(&spect);
286 
287  spect.AddLoader(this); // Remember we have a Go() pending
288  }
289 
290  //----------------------------------------------------------------------
292  const Var& var,
293  const SpillCut& spillcut,
294  const SliceCut& slicecut,
295  const SystShifts& shift,
296  const Var& wei)
297  {
298  if(fGone){
299  std::cerr << "Error: can't add Spectra after the call to Go()" << std::endl;
300  abort();
301  }
302 
303  fHistDefs[spillcut][shift][slicecut][wei][var].rwSpects.push_back(&spect);
304 
305  spect.AddLoader(this); // Remember we have a Go() pending
306  }
307 
308  //----------------------------------------------------------------------
311  {
312  fHistDefs.Erase(spect);
313  }
314 
315  //----------------------------------------------------------------------
317  {
318  return fFileSource->NFiles();
319  }
320 
321  //----------------------------------------------------------------------
323  {
324  TFile* f = fFileSource->GetNextFile();
325  if(!f) return 0; // out of files
326 
327  TH1* hPOT = (TH1*)f->Get("TotalPOT");
328  assert(hPOT);
329  fPOTFromHist += hPOT->Integral(0, -1);
330 
331  return f;
332  }
333 
334  //----------------------------------------------------------------------
336  {
337  }
338 
339  //----------------------------------------------------------------------
341  {
342  }
343 
344  // Apparently the existence of fSpillDefs isn't enough and I need to spell
345  // this out to make sure the function bodies are generated.
347 
349 } // namespace
BEGIN_PROLOG TPC Trig offset(g4 rise time) ProjectToHeight
Definition: CORSIKAGen.fcl:7
BEGIN_PROLOG could also be cerr
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
void AddLoader(SpectrumLoaderBase *)
process_name opflashCryoW ana
ifdh calls between construction and destruction produce no output
shift
Definition: fcl_checks.sh:26
virtual void Go() override
Load all the registered spectra.
Representation of a spectrum in any variable, with associated POT.
Definition: Spectrum.h:30
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]
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.
const SpillCut kNoSpillCut([](const caf::SRSpillProxy *){return true;})
The simplest possible cut: pass everything, used as a default.
File source based on a SAM query or dataset (definition)
Definition: SAMQuerySource.h:8
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.
Base class for the various types of spectrum loader.
Interface class for accessing ROOT files in sequence.
Definition: IFileSource.h:10
Simple file source based on an explicit list provided by the user.
std::unique_ptr< IFileSource > fFileSource
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.
std::string SAMExperiment()
$SAM_EXPERIMENT or a nice error message and abort
int NFiles() const override
May return -1 indicating the number of files is not known.
do i e
virtual void RemoveSpectrum(Spectrum *)
Fetch files from a pre-existing SAM project.
void RemoveLoader(SpectrumLoaderBase *l)
int NFiles() const
Forwards to fFileSource.
File source based on a wildcard (glob)
Definition: WildcardSource.h:8
Template for Cut and SpillCut.
Definition: Cut.h:16
Most useful for combining weights.
Definition: Var.h:23
void AddLoader(SpectrumLoaderBase *)
void GetReweightableSpectra(std::vector< ReweightableSpectrum * > &ss)
BEGIN_PROLOG could also be cout