LoadFromFile.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cassert>
4 #include <memory>
5 #include <string>
6 
7 #include "TFile.h"
8 
10 
11 class TDirectory;
12 
13 namespace ana
14 {
15  //----------------------------------------------------------------------
16  // Most classes are happy to load themselves
17  template<class T> std::unique_ptr<T> LoadFrom(TDirectory* dir, const std::string& label)
18  {
19  return T::LoadFrom(dir, label);
20  }
21 
22  //----------------------------------------------------------------------
23  // But if you're trying to load a base class we need to figure out which
24  // derived class is actually in the file and hand off to that. The
25  // implementations of these are in the cxx files for the base classes in
26  // question.
27  class IDecomp;
28  template<> std::unique_ptr<IDecomp> LoadFrom<IDecomp>(TDirectory* dir, const std::string& label);
29  class IExtrap;
30  template<> std::unique_ptr<IExtrap> LoadFrom<IExtrap>(TDirectory* dir, const std::string& label);
31  class IPrediction;
32  template<> std::unique_ptr<IPrediction> LoadFrom<IPrediction>(TDirectory* dir, const std::string& label);
33  class IExperiment;
34  template<> std::unique_ptr<IExperiment> LoadFrom<IExperiment>(TDirectory* dir, const std::string& label);
36  template<> std::unique_ptr<ModularExtrapComponent>
38  class IBkgdEstimator;
39  template<> std::unique_ptr<IBkgdEstimator> LoadFrom<IBkgdEstimator>(TDirectory* dir, const std::string& label);
40 
41  // This one is actually implemented in LoadFromFile.cxx to avoid polluting
42  // OscLib with CAFAna conventions.
43  template<> std::unique_ptr<osc::IOscCalc> LoadFrom<osc::IOscCalc>(TDirectory* dir, const std::string& label);
44  template<> std::unique_ptr<osc::IOscCalcAdjustable> LoadFrom<osc::IOscCalcAdjustable>(TDirectory* dir, const std::string& label);
45 
46  //----------------------------------------------------------------------
47  // For symmetry
48  template<class T> void SaveTo(const T& x, TDirectory* dir, const std::string& label)
49  {
50  x.SaveTo(dir, label);
51  }
52 
53  // Also in the cxx, to avoid having to put this logic into OscLib
54  template<> void SaveTo(const osc::IOscCalc& x, TDirectory* dir, const std::string& label);
55 
56  //----------------------------------------------------------------------
57  template<class T> std::unique_ptr<T> LoadFromFile(const std::string& fname,
58  const std::string& label)
59  {
60  TFile fin(fname.c_str());
61  assert(!fin.IsZombie());
62  return LoadFrom<T>(&fin, label);
63  }
64 
65  //----------------------------------------------------------------------
66  template<class T> void SaveToFile(const T& x,
67  const std::string& fname,
68  const std::string& label)
69  {
70  TFile fout(fname.c_str(), "RECREATE");
71  x.SaveTo(&fout, label);
72  }
73 }
TString fin
Definition: Style.C:24
std::unique_ptr< ModularExtrapComponent > LoadFrom< ModularExtrapComponent >(TDirectory *dir, const std::string &label)
Cuts and Vars for the 2020 FD DiF Study.
Definition: vars.h:6
void SaveTo(const osc::IOscCalc &x, TDirectory *dir, const std::string &name)
Base class for component extrapolation.
void SaveToFile(const T &x, const std::string &fname, const std::string &label)
Definition: LoadFromFile.h:66
General interface to oscillation calculators.
Definition: StanTypedefs.h:22
std::unique_ptr< T > LoadFrom(TDirectory *dir, const std::string &label)
Definition: LoadFromFile.h:17
const char * label
std::unique_ptr< IExtrap > LoadFrom< IExtrap >(TDirectory *dir, const std::string &label)
Definition: IExtrap.cxx:15
std::unique_ptr< IPrediction > LoadFrom< IPrediction >(TDirectory *dir, const std::string &label)
Definition: IPrediction.cxx:18
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
Interface to extrapolation procedures.
Definition: IExtrap.h:8
TDirectory * dir
Definition: macro.C:5
Standard interface to all decomposition techniques.
Definition: IDecomp.h:13
Base class defining interface for experiments.
Definition: IExperiment.h:14
assert(nhit_max >=nhit_nbins)
std::unique_ptr< IBkgdEstimator > LoadFrom< IBkgdEstimator >(TDirectory *dir, const std::string &label)
Standard interface to all prediction techniques.
Definition: IPrediction.h:57
double T
Definition: Xdiff_gwt.C:5
std::unique_ptr< IExperiment > LoadFrom< IExperiment >(TDirectory *dir, const std::string &label)
Definition: IExperiment.cxx:16
std::unique_ptr< IDecomp > LoadFrom< IDecomp >(TDirectory *dir, const std::string &label)
Definition: IDecomp.cxx:53
std::unique_ptr< T > LoadFromFile(const std::string &fname, const std::string &label)
Definition: LoadFromFile.h:57