All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
LoadFromFile.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cassert>
4 #include <iostream>
5 #include <memory>
6 #include <string>
7 
8 #include "TFile.h"
9 
10 class TDirectory;
11 
12 namespace osc
13 {
14  template<class T> class _IOscCalc;
15  typedef _IOscCalc<double> IOscCalc;
16 }
17 
18 namespace ana
19 {
20  //----------------------------------------------------------------------
21  // Most classes are happy to load themselves
22  template<class T> std::unique_ptr<T> LoadFrom(TDirectory* dir)
23  {
24  return T::LoadFrom(dir);
25  }
26 
27  //----------------------------------------------------------------------
28  // But if you're trying to load a base class we need to figure out which
29  // derived class is actually in the file and hand off to that. The
30  // implementations of these are in the cxx files for the base classes in
31  // question.
32  class IDecomp;
33  template<> std::unique_ptr<IDecomp> LoadFrom<IDecomp>(TDirectory* dir);
34  class IExtrap;
35  template<> std::unique_ptr<IExtrap> LoadFrom<IExtrap>(TDirectory* dir);
36  class IPrediction;
37  template<> std::unique_ptr<IPrediction> LoadFrom<IPrediction>(TDirectory* dir);
38  class IExperiment;
39  template<> std::unique_ptr<IExperiment> LoadFrom<IExperiment>(TDirectory* dir);
40  class ModularExtrapComponent;
41  template<> std::unique_ptr<ModularExtrapComponent>
43  class IBkgdEstimator;
44  template<> std::unique_ptr<IBkgdEstimator> LoadFrom<IBkgdEstimator>(TDirectory* dir);
45 
46  // This one is actually implemented in LoadFromFile.cxx to avoid polluting
47  // OscLib with CAFAna conventions.
48  template<> std::unique_ptr<osc::IOscCalc> LoadFrom<osc::IOscCalc>(TDirectory* dir);
49 
50  //----------------------------------------------------------------------
51  // For symmetry
52  template<class T> void SaveTo(const T& x, TDirectory* dir)
53  {
54  x.SaveTo(dir);
55  }
56 
57  // Also in the cxx, to avoid having to put this logic into OscLib
58  template<> void SaveTo(const osc::IOscCalc& x, TDirectory* dir);
59 
60  //----------------------------------------------------------------------
61  template<class T> std::unique_ptr<T> LoadFromFile(const std::string& fname,
62  const std::string& label)
63  {
64  TFile fin(fname.c_str());
65  assert(!fin.IsZombie());
66  TDirectory* dir = fin.GetDirectory(label.c_str());
67  if(!dir){
68  std::cerr << "Didn't find '" << label << "' in " << fname << std::endl;
69  abort();
70  }
71  return LoadFrom<T>(dir);
72  }
73 
74  //----------------------------------------------------------------------
75  template<class T> void SaveToFile(const T& x,
76  const std::string& fname,
77  const std::string& label)
78  {
79  TFile fout(fname.c_str(), "RECREATE");
80  x.SaveTo(fout.mkdir(label.c_str()));
81  }
82 }
string fname
Definition: demo.py:5
process_name opflash particleana ie x
BEGIN_PROLOG could also be cerr
void SaveToFile(const T &x, const std::string &fname, const std::string &label)
Definition: LoadFromFile.h:75
std::unique_ptr< IExtrap > LoadFrom< IExtrap >(TDirectory *dir)
Definition: IExtrap.cxx:17
std::unique_ptr< IDecomp > LoadFrom< IDecomp >(TDirectory *dir)
process_name opflashCryoW ana
_IOscCalc< double > IOscCalc
Definition: Plots.h:17
std::unique_ptr< IBkgdEstimator > LoadFrom< IBkgdEstimator >(TDirectory *dir)
std::unique_ptr< ModularExtrapComponent > LoadFrom< ModularExtrapComponent >(TDirectory *dir)
tuple dir
Definition: dropbox.py:28
std::unique_ptr< IPrediction > LoadFrom< IPrediction >(TDirectory *dir)
Definition: IPrediction.cxx:26
std::unique_ptr< T > LoadFrom(TDirectory *dir)
Definition: LoadFromFile.h:22
std::unique_ptr< IExperiment > LoadFrom< IExperiment >(TDirectory *dir)
Definition: IExperiment.cxx:22
std::unique_ptr< T > LoadFromFile(const std::string &fname, const std::string &label)
Definition: LoadFromFile.h:61
void SaveTo(const osc::IOscCalc &x, TDirectory *dir)