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)
19  {
20  return T::LoadFrom(dir);
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);
30  class IExtrap;
31  template<> std::unique_ptr<IExtrap> LoadFrom<IExtrap>(TDirectory* dir);
32  class IPrediction;
33  template<> std::unique_ptr<IPrediction> LoadFrom<IPrediction>(TDirectory* dir);
34  class IChiSqExperiment;
35  template<> std::unique_ptr<IChiSqExperiment> LoadFrom<IChiSqExperiment>(TDirectory* dir);
37  template<> std::unique_ptr<ModularExtrapComponent>
39  class IBkgdEstimator;
40  template<> std::unique_ptr<IBkgdEstimator> LoadFrom<IBkgdEstimator>(TDirectory* dir);
41 
42  // This one is actually implemented in LoadFromFile.cxx to avoid polluting
43  // OscLib with CAFAna conventions.
44  template<> std::unique_ptr<osc::IOscCalculator> LoadFrom<osc::IOscCalculator>(TDirectory* dir);
45  template<> std::unique_ptr<osc::IOscCalculatorAdjustable> LoadFrom<osc::IOscCalculatorAdjustable>(TDirectory* dir);
46 
47  //----------------------------------------------------------------------
48  // For symmetry
49  template<class T> void SaveTo(const T& x, TDirectory* dir)
50  {
51  x.SaveTo(dir);
52  }
53 
54  // Also in the cxx, to avoid having to put this logic into OscLib
55  template<> void SaveTo(const osc::IOscCalculator& x, TDirectory* dir);
56 
57  //----------------------------------------------------------------------
58  template<class T> std::unique_ptr<T> LoadFromFile(TFile* fin,
59  const std::string& label)
60  {
61  assert(!fin->IsZombie());
62  TDirectory* dir = fin->GetDirectory(label.c_str());
63  if(!dir){
64  std::cerr << "Didn't find '" << label << "' in " << fin->GetName() << std::endl;
65  abort();
66  }
67  return LoadFrom<T>(dir);
68  }
69 
70  //----------------------------------------------------------------------
71  template<class T> std::unique_ptr<T> LoadFromFile(const std::string& fname,
72  const std::string& label)
73  {
74  TFile fin(fname.c_str());
75  return LoadFromFile<T>(&fin, label);
76  }
77 
78  //----------------------------------------------------------------------
79  template<class T> void SaveToFile(const T& x,
80  const std::string& fname,
81  const std::string& label)
82  {
83  TFile fout(fname.c_str(), "RECREATE");
84  x.SaveTo(fout.mkdir(label.c_str()));
85  }
86 }
TString fin
Definition: Style.C:24
Oscillation analysis framework, runs over CAF files outside of ART.
std::unique_ptr< ModularExtrapComponent > LoadFrom< ModularExtrapComponent >(TDirectory *dir)
General interface to oscillation calculators.
Definition: FwdDeclare.h:15
Base class for component extrapolation.
void SaveToFile(const T &x, const std::string &fname, const std::string &label)
Definition: LoadFromFile.h:79
std::unique_ptr< IExtrap > LoadFrom< IExtrap >(TDirectory *dir)
Definition: IExtrap.cxx:20
std::unique_ptr< IChiSqExperiment > LoadFrom< IChiSqExperiment >(TDirectory *dir)
OStream cerr
Definition: OStream.cxx:7
const char * label
void SaveTo(const osc::IOscCalculator &x, TDirectory *dir)
Base class defining interface for experiments.
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
std::unique_ptr< IPrediction > LoadFrom< IPrediction >(TDirectory *dir)
Definition: IPrediction.cxx:38
Interface to extrapolation procedures.
Definition: IExtrap.h:8
TDirectory * dir
Definition: macro.C:5
std::unique_ptr< T > LoadFromFile(TFile *fin, const std::string &label)
Definition: LoadFromFile.h:58
Standard interface to all decomposition techniques.
Definition: IDecomp.h:13
Standard interface to all prediction techniques.
Definition: IPrediction.h:57
double T
Definition: Xdiff_gwt.C:5
std::unique_ptr< IBkgdEstimator > LoadFrom< IBkgdEstimator >(TDirectory *dir)
std::unique_ptr< T > LoadFrom(TDirectory *dir)
Definition: LoadFromFile.h:18
std::unique_ptr< IDecomp > LoadFrom< IDecomp >(TDirectory *dir)
Definition: IDecomp.cxx:32