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 #include "CAFAna/Core/FwdDeclare.h"
11 
12 class TDirectory;
13 
14 namespace ana
15 {
16  //----------------------------------------------------------------------
17  // Most classes are happy to load themselves
18  template<class T> std::unique_ptr<T> LoadFrom(TDirectory* dir, const std::string& label)
19  {
20  return T::LoadFrom(dir, label);
21  }
22 
23  //----------------------------------------------------------------------
24  // But if you're trying to load a base class we need to figure out which
25  // derived class is actually in the file and hand off to that. The
26  // implementations of these are in the cxx files for the base classes in
27  // question.
28  class IDecomp;
29  template<> std::unique_ptr<IDecomp> LoadFrom<IDecomp>(TDirectory* dir, const std::string& label);
30  class IExtrap;
31  template<> std::unique_ptr<IExtrap> LoadFrom<IExtrap>(TDirectory* dir, const std::string& label);
32  class IPrediction;
33  template<> std::unique_ptr<IPrediction> LoadFrom<IPrediction>(TDirectory* dir, const std::string& label);
34  class IExperiment;
35  template<> std::unique_ptr<IExperiment> LoadFrom<IExperiment>(TDirectory* dir, const std::string& label);
37  template<> std::unique_ptr<ModularExtrapComponent>
39  class IBkgdEstimator;
40  template<> std::unique_ptr<IBkgdEstimator> LoadFrom<IBkgdEstimator>(TDirectory* dir, const std::string& label);
41 
42  // This one is actually implemented in LoadFromFile.cxx to avoid polluting
43  // OscLib with CAFAna conventions.
44  template<> std::unique_ptr<osc::IOscCalc> LoadFrom<osc::IOscCalc>(TDirectory* dir, const std::string& label);
45  template<> std::unique_ptr<osc::IOscCalcAdjustable> LoadFrom<osc::IOscCalcAdjustable>(TDirectory* dir, const std::string& label);
46 
47  //----------------------------------------------------------------------
48  // For symmetry
49  template<class T> void SaveTo(const T& x, TDirectory* dir, const std::string& label)
50  {
51  x.SaveTo(dir, label);
52  }
53 
54  // Also in the cxx, to avoid having to put this logic into OscLib
55  template<> void SaveTo(const osc::IOscCalc& x, TDirectory* dir, const std::string& label);
56 
57  //----------------------------------------------------------------------
58  template<class T> std::unique_ptr<T> LoadFromFile(const std::string& fname,
59  const std::string& label)
60  {
61  TFile fin(fname.c_str());
62  assert(!fin.IsZombie());
63  return LoadFrom<T>(&fin, label);
64  }
65 
66  //----------------------------------------------------------------------
67  template<class T> void SaveToFile(const T& x,
68  const std::string& fname,
69  const std::string& label)
70  {
71  TFile fout(fname.c_str(), "RECREATE");
72  x.SaveTo(fout.mkdir(label.c_str()));
73  }
74 }
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: CutFlow_header.h:5
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:67
std::unique_ptr< T > LoadFrom(TDirectory *dir, const std::string &label)
Definition: LoadFromFile.h:18
const char * label
std::unique_ptr< IExtrap > LoadFrom< IExtrap >(TDirectory *dir, const std::string &label)
Definition: IExtrap.cxx:20
std::unique_ptr< IPrediction > LoadFrom< IPrediction >(TDirectory *dir, const std::string &label)
Definition: IPrediction.cxx:33
::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:22
std::unique_ptr< IDecomp > LoadFrom< IDecomp >(TDirectory *dir, const std::string &label)
Definition: IDecomp.cxx:32
std::unique_ptr< T > LoadFromFile(const std::string &fname, const std::string &label)
Definition: LoadFromFile.h:58