LoadFromFile.cxx
Go to the documentation of this file.
2 
3 #include "OscLib/OscCalc.h"
4 #include "OscLib/OscCalcDumb.h"
6 #include "OscLib/OscCalcPMNS.h"
10 
11 #include "TObjString.h"
12 #include "TVectorD.h"
13 
14 #include <iostream>
15 #include <vector>
16 
17 namespace ana
18 {
19  //----------------------------------------------------------------------
20  template<> std::unique_ptr<osc::IOscCalc>
21  LoadFrom<osc::IOscCalc>(TDirectory* dir, const std::string& name)
22  {
23  dir = dir->GetDirectory(name.c_str()); // switch to subdir
24  assert(dir);
25 
26  // There are a lot of ways to exit this function. Make sure they all write
27  // and delete the directory.
28  struct DirCleaner
29  {
30  ~DirCleaner(){fDir->Write(); delete fDir;}
31  TDirectory* fDir;
32  } cleaner{dir};
33 
34  TObjString* ptag = (TObjString*)dir->Get("type");
35  assert(ptag);
36  const TString tag = ptag->GetString();
37 
38  if(tag == "NoOscillations") return std::unique_ptr<osc::IOscCalc>(new osc::NoOscillations);
39  if(tag == "OscCalculatorDumb") return std::unique_ptr<osc::IOscCalc>(new osc::OscCalcDumb);
40 
41  // Otherwise it's probably an adjustable calculator
42  return LoadFrom<osc::IOscCalcAdjustable>(dir, name);
43  }
44 
45  //----------------------------------------------------------------------
46  template<> std::unique_ptr<osc::IOscCalcAdjustable>
47  LoadFrom<osc::IOscCalcAdjustable>(TDirectory* dir, const std::string& name)
48  {
49  dir = dir->GetDirectory(name.c_str()); // switch to subdir
50  assert(dir);
51 
52  struct DirCleaner
53  {
54  ~DirCleaner(){fDir->Write(); delete fDir;}
55  TDirectory* fDir;
56  } cleaner{dir};
57 
58  TObjString* ptag = (TObjString*)dir->Get("type");
59  assert(ptag);
60  const TString tag = ptag->GetString();
61 
63 
64  if(tag == "OscCalculator") ret = new osc::OscCalc;
65  if(tag == "OscCalculatorGeneral") ret = new osc::OscCalcGeneral;
66  if(tag == "OscCalculatorPMNS") ret = new osc::OscCalcPMNS;
67  if(tag == "OscCalculatorPMNSOpt") ret = new osc::OscCalcPMNSOpt;
68  if(tag == "OscCalculatorSterile") ret = new osc::OscCalcSterile;
69  if(tag == "OscCalculatorPMNS_NSI") ret = new osc::OscCalcPMNS_NSI;
70 
71  if(!ret){
72  std::cout << "LoadFrom not implemented for " << tag << std::endl;
73  abort();
74  }
75 
76  TVectorD* params = (TVectorD*)dir->Get("params");
77  assert(params);
78  //special case how OscCalcSterile is initialized
79  if(tag == "OscCalculatorSterile") {
80  std::vector<double> state;
81  for (int i = 0; i < params->GetNrows(); ++i) {
82  state.push_back( (*params)[i] );
83  }
84  dynamic_cast<osc::OscCalcSterile*>(ret)->SetState(state);
85  return std::unique_ptr<osc::IOscCalcAdjustable>(ret);
86  }
87  //special case how OscCalcPMNS_NSI is initialized
88  if(tag == "OscCalculatorPMNS_NSI") {
89  std::vector<double> state;
90  for (int i = 0; i < params->GetNrows(); ++i) {
91  state.push_back( (*params)[i] );
92  }
93  dynamic_cast<osc::OscCalcPMNS_NSI*>(ret)->SetState(state);
94  return std::unique_ptr<osc::IOscCalcAdjustable>(ret);
95  }
96 
97  assert(params->GetNrows() == 8);
98 
99  ret->SetL ((*params)[0]);
100  ret->SetRho ((*params)[1]);
101  ret->SetDmsq21((*params)[2]);
102  ret->SetDmsq32((*params)[3]);
103  ret->SetTh12 ((*params)[4]);
104  ret->SetTh13 ((*params)[5]);
105  ret->SetTh23 ((*params)[6]);
106  ret->SetdCP ((*params)[7]);
107 
108  return std::unique_ptr<osc::IOscCalcAdjustable>(ret);
109  }
110 
111  //----------------------------------------------------------------------
112  template<> void SaveTo(const osc::IOscCalc& x, TDirectory* dir, const std::string& name)
113  {
114  TDirectory* tmp = gDirectory;
115 
116  dir = dir->mkdir(name.c_str()); // switch to subdir
117  dir->cd();
118 
119  // There are a lot of ways to exit this function. Make sure they all write
120  // and delete the directory.
121  struct DirCleaner
122  {
123  ~DirCleaner(){fDir->Write(); delete fDir;}
124  TDirectory* fDir;
125  } cleaner{dir};
126 
127 
128  if(dynamic_cast<const osc::NoOscillations*>(&x)){
129  TObjString("NoOscillations").Write("type");
130  tmp->cd();
131  return;
132  }
133 
134  if(dynamic_cast<const osc::OscCalcDumb*>(&x)){
135  TObjString("OscCalculatorDumb").Write("type");
136  tmp->cd();
137  return;
138  }
139 
140  const osc::IOscCalcAdjustable* y = dynamic_cast<const osc::IOscCalcAdjustable*>(&x);
141  if(!y){
142  std::cout << "Unknown calculator in SaveTo " << typeid(x).name() << std::endl;
143  abort();
144  }
145 
146  const osc::OscCalcSterile* tmpSterile = dynamic_cast<const osc::OscCalcSterile*>(&x);
147  if(tmpSterile) {
148  TObjString("OscCalculatorSterile").Write("type");
149  std::vector<double> state = tmpSterile->GetState();
150  TVectorD params(state.size());
151  for (unsigned int i = 0; i < state.size(); ++i) params[i] = state[i];
152  params.Write("params");
153  tmp->cd();
154  return;
155  }
156 
157  //for the implementation of OscCalcPMNS_NSI
158  const osc::OscCalcPMNS_NSI* tmpNSI = dynamic_cast<const osc::OscCalcPMNS_NSI*>(&x);
159  if(tmpNSI) {
160  TObjString("OscCalculatorPMNS_NSI").Write("type");
161  std::vector<double> state = tmpNSI->GetState();
162  TVectorD params(state.size());
163  for (unsigned int i = 0; i < state.size(); ++i) params[i] = state[i];
164  params.Write("params");
165  tmp->cd();
166  return;
167  }
168 
169  /* */if(dynamic_cast<const osc::OscCalc*>(&x)) TObjString("OscCalculatorPMNS").Write("type");
170  else if(dynamic_cast<const osc::OscCalcGeneral*>(&x)) TObjString("OscCalculatorGeneral").Write("type");
171  else if(dynamic_cast<const osc::OscCalcPMNS*>(&x)) TObjString("OscCalculatorPMNS").Write("type");
172  else if(dynamic_cast<const osc::OscCalcPMNSOpt*>(&x)) TObjString("OscCalculatorPMNSOpt").Write("type");
173  else{
174  std::cout << "Unimplemented calculator in SaveTo " << typeid(x).name() << std::endl;
175  abort();
176  }
177 
178  TVectorD params(8);
179 
180  params[0] = y->GetL();
181  params[1] = y->GetRho();
182  params[2] = y->GetDmsq21();
183  params[3] = y->GetDmsq32();
184  params[4] = y->GetTh12();
185  params[5] = y->GetTh13();
186  params[6] = y->GetTh23();
187  params[7] = y->GetdCP();
188 
189  params.Write("params");
190 
191  tmp->cd();
192  }
193 }
Optimized version of OscCalcPMNS.
const XML_Char * name
Definition: expat.h:151
virtual void SetL(double L)=0
Cuts and Vars for the 2020 FD DiF Study.
Definition: vars.h:6
More generic (but probably slower) oscillation calculations.
_OscCalcPMNSOpt< double > OscCalcPMNSOpt
void SaveTo(const osc::IOscCalc &x, TDirectory *dir, const std::string &name)
virtual void SetDmsq21(const T &dmsq21)=0
std::vector< double > GetState() const
General interface to oscillation calculators.
Definition: FwdDeclare.h:14
virtual double GetL() const
Definition: IOscCalc.h:83
virtual void SetTh13(const T &th13)=0
Adapt the PMNS_Sterile calculator to standard interface.
virtual T GetTh23() const
Definition: IOscCalc.h:89
Float_t tmp
Definition: plot.C:36
virtual void SetDmsq32(const T &dmsq32)=0
_NoOscillations< double > NoOscillations
Definition: IOscCalc.h:62
std::vector< double > GetState() const
Simple oscillation probability calculator that has no solar term or mass hierarchy or delta so it&#39;s s...
Definition: OscCalcDumb.h:16
virtual T GetDmsq32() const
Definition: IOscCalc.h:86
virtual T GetTh12() const
Definition: IOscCalc.h:87
virtual T GetdCP() const
Definition: IOscCalc.h:90
OStream cout
Definition: OStream.cxx:6
virtual T GetTh13() const
Definition: IOscCalc.h:88
virtual T GetDmsq21() const
Definition: IOscCalc.h:85
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
virtual void SetRho(double rho)=0
_OscCalcPMNS< double > OscCalcPMNS
Definition: OscCalcPMNS.h:57
TDirectory * dir
Definition: macro.C:5
virtual void SetTh23(const T &th23)=0
assert(nhit_max >=nhit_nbins)
virtual double GetRho() const
Definition: IOscCalc.h:84
virtual void SetTh12(const T &th12)=0
virtual void SetdCP(const T &dCP)=0