TrivialBkgdEstimator.cxx
Go to the documentation of this file.
3 
4 #include "TObjString.h"
5 #include "TVectorD.h"
6 
7 namespace ana
8 {
9  int
11  {
12  return fMCBkgd.size();
13  }
14 
15  const Spectrum *
17  {
18  // assert(n < (int) fMCBkgd.size() && "Index out of range");
19  return fMCBkgd[n];
20  }
21 
23  {
24  assert(fMCBkgd.size() > 0 && "No background given to estimator");
25  Spectrum ret = *fMCBkgd[0];
26  for (int i = 1; i < int(fMCBkgd.size()); i++)
27  ret += *fMCBkgd[i];
28  return ret;
29  }
30 
31  void TrivialBkgdEstimator::SaveTo(TDirectory *dir, const std::string& name) const
32  {
33  TDirectory *tmp = gDirectory;
34 
35  dir = dir->mkdir(name.c_str()); // switch to subdir
36  dir->cd();
37 
38  TObjString("TrivialBkgdEstimator").Write("type");
39 
40  // Need a way to save a vector of Spectrum*
41  TVectorD nmcbkgd(1);
42  nmcbkgd[0] = fMCBkgd.size();
43  nmcbkgd.Write("nmcbkgd");
44  for (int i = 0; i < int(fMCBkgd.size()); i++){
45  fMCBkgd[i]->SaveTo(dir, "fMCBkgd_"+std::to_string(i));
46  }
47 
48  dir->Write();
49  delete dir;
50 
51  tmp->cd();
52  }
53 
54  std::unique_ptr<TrivialBkgdEstimator>
56  {
57  dir = dir->GetDirectory(name.c_str()); // switch to subdir
58  assert(dir);
59 
60  TVectorD* nmcbkgd = (TVectorD*)dir->Get("nmcbkgd");
61  assert(nmcbkgd->GetNoElements()==1 && "N MCBkgds is not 1.");
62  std::vector<Spectrum*> mcbkgd;
63  dir->cd();
64  for (int i = 0; i < (*nmcbkgd)[0]; i++){
65  mcbkgd.push_back(ana::LoadFrom<Spectrum>(dir, "fMCBkgd_"+std::to_string(i)).release());
66  }
67 
68  delete dir;
69 
70  return std::unique_ptr<TrivialBkgdEstimator>(
71  new TrivialBkgdEstimator(mcbkgd));
72  }
73 
74 
76  for(int i = 0; i < (int) fMCBkgd.size(); i++) {
77  delete fMCBkgd[i];
78  }
79  }
80 
82  fMCBkgd = copy.fMCBkgd;
83  }
84 
85  TrivialBkgdEstimator& TrivialBkgdEstimator::operator=
86  (const TrivialBkgdEstimator& rhs) {
87  if(this == & rhs) return *this;
88  else {
89  fMCBkgd = rhs.fMCBkgd;
90  return *this;
91  }
92  }
93 
94 }
const XML_Char * name
Definition: expat.h:151
Cuts and Vars for the 2020 FD DiF Study.
Definition: vars.h:6
static std::unique_ptr< TrivialBkgdEstimator > LoadFrom(TDirectory *dir, const std::string &name)
Float_t tmp
Definition: plot.C:36
Representation of a spectrum in any variable, with associated POT.
Definition: Spectrum.h:33
const Spectrum * GetComponent(int) const
Spectrum Background() const override
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
TDirectory * dir
Definition: macro.C:5
TrivialBkgdEstimator(SpectrumLoaderBase &lMC, const HistAxis &axis, const Cut &sel, std::vector< Cut > trueBkgds, const SystShifts &shift=kNoShift, const Var &wei=kUnweighted)
assert(nhit_max >=nhit_nbins)
std::string to_string(ModuleType mt)
Definition: ModuleType.h:32
Just return the MC prediction for the background.
std::vector< Spectrum * > fMCBkgd
void SaveTo(TDirectory *dir, const std::string &name) const override