NDOscillatableSpectrum.cxx
Go to the documentation of this file.
2 
3 #include "CAFAna/Core/Binning.h"
5 #include "CAFAna/Core/Ratio.h"
7 
9 
10 #include "OscLib/IOscCalc.h"
11 
12 #include "TDirectory.h"
13 #include "TH2.h"
14 #include "TObjString.h"
15 
16 #include <cassert>
17 #include <memory>
18 
19 namespace ana
20 {
21  const Var kTrueLOverTrueE([](const caf::SRProxy* sr)
22  {
23  // Returning zero causes this event to be skipped
24  if(sr->mc.nnu == 0) return 0.;
25 
26  const double trueE = sr->mc.nu[0].E;
27 
28  const double trueL = sr->mc.nu[0].L;
29 
30  return trueL/trueE;
31  });
32 
33  //----------------------------------------------------------------------
37  const Var& var,
38  const Cut& cut,
39  const SystShifts& shift,
40  const Var& wei)
41  : ReweightableSpectrum(loader,
42  HistAxis(label, bins, var),
43  HistAxis("True Energy (GeV)", kTrueLOverTrueEBins, kTrueLOverTrueE),
44  cut, shift, wei)
45  {
46  }
47 
48  //----------------------------------------------------------------------
50  const HistAxis& axis,
51  const Cut& cut,
52  const SystShifts& shift,
53  const Var& wei)
54  : ReweightableSpectrum(loader, axis,
55  HistAxis("True Energy (GeV)", kTrueLOverTrueEBins, kTrueLOverTrueE),
56  cut, shift, wei)
57  {
58  }
59 
60  //----------------------------------------------------------------------
62  {
63  }
64 
65  //----------------------------------------------------------------------
68  {
69  assert( rhs.fReferences.empty() ); // Copying with pending loads is unexpected
70  }
71 
72 
73  //----------------------------------------------------------------------
76  {
77  assert( rhs.fReferences.empty() ); // Copying with pending loads is unexpected
78  }
79 
80  //----------------------------------------------------------------------
82  {
83  if(this == &rhs) return *this;
84 
86 
87  assert( rhs.fReferences.empty() ); // Copying with pending loads is unexpected
88  assert( fReferences.empty() ); // Copying with pending loads is unexpected
89 
90  return *this;
91  }
92 
93  //----------------------------------------------------------------------
95  {
96  if(this == &rhs) return *this;
97 
99 
100  assert( rhs.fReferences.empty() ); // Copying with pending loads is unexpected
101  assert( fReferences.empty() ); // Copying with pending loads is unexpected
102 
103  return *this;
104  }
105 
106  //----------------------------------------------------------------------
108  int from, int to) const
109  {
110  const NDOscCurve curve(calc, from, to);
111  return WeightedBy(curve);
112  }
113 
114  //----------------------------------------------------------------------
115  void NDOscillatableSpectrum::SaveTo(TDirectory* dir, const std::string& name) const
116  {
117  _SaveTo(dir, name, "NDOscillatableSpectrum");
118  }
119 
120  //----------------------------------------------------------------------
121  std::unique_ptr<NDOscillatableSpectrum> NDOscillatableSpectrum::LoadFrom(TDirectory* dir, const std::string& name)
122  {
123  dir = dir->GetDirectory(name.c_str()); // switch to subdir
124  assert(dir);
125 
126  DontAddDirectory guard;
127 
128  TObjString* tag = (TObjString*)dir->Get("type");
129  assert(tag);
130  assert(tag->GetString() == "NDOscillatableSpectrum");
131  delete tag;
132 
133  TH2D* spect = (TH2D*)dir->Get("hist");
134  assert(spect);
135  TH1* hPot = (TH1*)dir->Get("pot");
136  assert(hPot);
137  TH1* hLivetime = (TH1*)dir->Get("livetime");
138  assert(hLivetime);
139 
140  std::vector<std::string> labels;
141  std::vector<Binning> bins;
142 
143  for(int i = 0; ; ++i){
144  const std::string subname = TString::Format("bins%d", i).Data();
145  TDirectory* subdir = dir->GetDirectory(subname.c_str());
146  if(!subdir) break;
147  delete subdir;
148  bins.push_back(*Binning::LoadFrom(dir, subname));
149  TObjString* label = (TObjString*)dir->Get(TString::Format("label%d", i));
150  labels.push_back(label ? label->GetString().Data() : "");
151  delete label;
152  }
153 
154  delete dir;
155 
156  auto ret = std::make_unique<NDOscillatableSpectrum>(kNullLoader,
157  HistAxis(labels, bins),
158  kNoCut);
159 
160  // ROOT histogram storage is row-major, but Eigen is column-major by
161  // default
162  typedef Eigen::Matrix<double, Eigen::Dynamic, Eigen:: Dynamic, Eigen::RowMajor> MatRowMajor;
163  ret->fMat = Eigen::Map<MatRowMajor>(spect->GetArray(),
164  ret->fMat.rows(),
165  ret->fMat.cols());
166 
167  delete spect;
168 
169  ret->fPOT = hPot->Integral(0, -1);
170  ret->fLivetime = hLivetime->Integral(0, -1);
171 
172  delete hPot;
173  delete hLivetime;
174 
175  return ret;
176  }
177 }
static std::unique_ptr< Binning > LoadFrom(TDirectory *dir, const std::string &name)
Definition: Binning.cxx:230
NDOscillatableSpectrum & operator=(const NDOscillatableSpectrum &rhs)
Assignment operator.
const XML_Char * name
Definition: expat.h:151
_HistAxis< Var > HistAxis
Definition: HistAxis.h:103
Represent the binning of a Spectrum&#39;s x-axis.
Definition: Binning.h:16
Cuts and Vars for the 2020 FD DiF Study.
Definition: vars.h:6
const Binning kTrueLOverTrueEBins
Default true-energy bin edges.
Definition: Binning.cxx:62
subdir
Definition: cvnie.py:7
Simple record of shifts applied to systematic parameters.
Definition: SystShifts.h:20
Proxy for caf::StandardRecord.
Definition: SRProxy.h:2126
General interface to oscillation calculators.
Definition: StanTypedefs.h:22
caf::Proxy< std::vector< caf::SRNeutrino > > nu
Definition: SRProxy.h:618
Spectrum with the value of a second variable, allowing for reweighting
static std::unique_ptr< NDOscillatableSpectrum > LoadFrom(TDirectory *dir, const std::string &name)
caf::Proxy< short int > nnu
Definition: SRProxy.h:617
osc::OscCalcDumb calc
Transition probability for any one channel as a function of energy.
Definition: NDOscCurve.h:10
_Var< caf::SRProxy > Var
Representation of a variable to be retrieved from a caf::StandardRecord object.
Definition: Var.h:74
Representation of a spectrum in any variable, with associated POT.
Definition: Spectrum.h:40
const char * label
Spectrum Oscillated(osc::IOscCalc *calc, int from, int to) const
const Var kTrueLOverTrueE([](const caf::SRProxy *sr){if(sr->mc.nnu==0) return 0.;const double trueE=sr->mc.nu[0].E;const double trueL=sr->mc.nu[0].L;return trueL/trueE;})
void SaveTo(TDirectory *dir, const std::string &name) const
Spectrum WeightedBy(const Ratio &weights) const
Reco spectrum with truth weights applied.
caf::StandardRecord * sr
loader
Definition: demo0.py:10
Base class for the various types of spectrum loader.
caf::Proxy< caf::SRTruthBranch > mc
Definition: SRProxy.h:2138
const Binning bins
Definition: NumuCC_CPiBin.h:8
std::set< ReweightableSpectrum ** > fReferences
const Cut cut
Definition: exporter_fd.C:30
void _SaveTo(TDirectory *dir, const std::string &name, const std::string &type) const
ReweightableSpectrum & operator=(const ReweightableSpectrum &rhs)
TDirectory * dir
Definition: macro.C:5
assert(nhit_max >=nhit_nbins)
NDOscillatableSpectrum()
Constructor for Uninitialized()
void Format(TGraph *gr, int lcol, int lsty, int lwid, int mcol, int msty, double msiz)
Definition: Style.cxx:154
Prevent histograms being added to the current directory.
Definition: UtilsExt.h:46
Spectrum with true L/E information, allowing it to be oscillated
const Cut kNoCut
The simplest possible cut: pass everything, used as a default.
Definition: Cut.h:109
static NullLoader kNullLoader
Dummy loader that doesn&#39;t load any files.
enum BeamMode string