All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SingleSampleExperiment.cxx
Go to the documentation of this file.
2 
6 
7 #include "OscLib/IOscCalc.h"
8 
9 #include "TDirectory.h"
10 #include "TObjString.h"
11 #include "TH1.h"
12 
13 namespace ana
14 {
15  //----------------------------------------------------------------------
17  const Spectrum& data,
18  const Spectrum& cosmicInTime,
19  const Spectrum& cosmicOutOfTime)
20  : fMC(pred), fData(data), fCosmicInTime(cosmicInTime), fCosmicOutOfTime(cosmicOutOfTime), fMask(0)
21  {
22  if(cosmicInTime.POT() > 0){
23  std::cout << "SingleSampleExperiment: in-time cosmics have nonzero POT. "
24  << "Did you confuse the two cosmics arguments?"
25  << std::endl;
26  abort();
27  }
28  }
29 
30  //----------------------------------------------------------------------
32  {
33  delete fMask;
34  }
35 
36  //----------------------------------------------------------------------
38  const SystShifts& syst) const
39  {
40  Spectrum p = fMC->PredictSyst(calc, syst);
41 
42  if(fCosmicOutOfTime.POT() > 0){ // if out-of-time cosmics supplied
43  p += fCosmicOutOfTime;
44  }
45 
46  TH1D* hpred = p.ToTH1(fData.POT());
47 
48  // "Livetime" here means number of readout windows.
49  if(fCosmicInTime.Livetime() > 0){ // if in-cosmics supplied
50  // This many are already included in the beam MC
51  const double beamLivetime = p.Livetime() * fData.POT()/p.POT();
52  // So we need to take this many from the cosmics-only to match the data
53  const double intimeLivetime = fData.Livetime() - beamLivetime;
54 
55  TH1D* hcosmic = fCosmicInTime.ToTH1(intimeLivetime, kLivetime);
56  hpred->Add(hcosmic);
57  HistCache::Delete(hcosmic);
58  }
59 
60  TH1D* hdata = fData.ToTH1(fData.POT());
61 
62  // If a valid mask has been set, zero out the offending bins
63  if (fMask){
64  assert(hpred->GetNbinsX() == fMask->GetNbinsX());
65  assert(hdata->GetNbinsX() == fMask->GetNbinsX());
66 
67  for(int i = 0; i < fMask->GetNbinsX()+2; ++i){
68  if (fMask->GetBinContent(i+1) == 1) continue;
69  hpred->SetBinContent(i+1, 0);
70  hdata->SetBinContent(i+1, 0);
71  }
72  }
73 
74  const double ll = LogLikelihood(hpred, hdata);
75 
76  HistCache::Delete(hpred);
77  HistCache::Delete(hdata);
78 
79  return ll;
80  }
81 
82  //----------------------------------------------------------------------
83  void SingleSampleExperiment::SaveTo(TDirectory* dir) const
84  {
85  TDirectory* tmp = dir;
86 
87  dir->cd();
88  TObjString("SingleSampleExperiment").Write("type");
89 
90  fMC->SaveTo(dir->mkdir("mc"));
91  fData.SaveTo(dir->mkdir("data"));
92  fCosmicInTime.SaveTo(dir->mkdir("cosmicInTime"));
93  fCosmicOutOfTime.SaveTo(dir->mkdir("cosmicOutOfTime"));
94 
95  tmp->cd();
96  }
97 
98  //----------------------------------------------------------------------
99  std::unique_ptr<SingleSampleExperiment> SingleSampleExperiment::LoadFrom(TDirectory* dir)
100  {
101  TObjString* ptag = (TObjString*)dir->Get("type");
102  assert(ptag);
103  assert(ptag->GetString() == "SingleSampleExperiment");
104 
105  assert(dir->GetDirectory("mc"));
106  assert(dir->GetDirectory("data"));
107 
108 
109  const IPrediction* mc = ana::LoadFrom<IPrediction>(dir->GetDirectory("mc")).release();
110  const std::unique_ptr<Spectrum> data = Spectrum::LoadFrom(dir->GetDirectory("data"));
111 
112  // Legacy format. This is the in-time cosmics
113  if(dir->GetDirectory("cosmic")){
114  const std::unique_ptr<Spectrum> cosmicInTime = Spectrum::LoadFrom(dir->GetDirectory("cosmic"));
115  return std::make_unique<SingleSampleExperiment>(mc, *data, *cosmicInTime, Spectrum(0, {}, {}, 0, 0));
116  }
117 
118  const std::unique_ptr<Spectrum> cosmicInTime = Spectrum::LoadFrom(dir->GetDirectory("cosmicInTime"));
119  const std::unique_ptr<Spectrum> cosmicOutOfTime = Spectrum::LoadFrom(dir->GetDirectory("cosmicOutOfTime"));
120 
121  return std::make_unique<SingleSampleExperiment>(mc, *data, *cosmicInTime, *cosmicOutOfTime);
122  }
123 
124  //----------------------------------------------------------------------
125  void SingleSampleExperiment::SetMaskHist(double xmin, double xmax, double ymin, double ymax)
126  {
127  fMask = GetMaskHist(fData, xmin, xmax, ymin, ymax);
128  }
129 }
TH1D * ToTH1(double exposure, Color_t col=kBlack, Style_t style=kSolid, EExposureType expotype=kPOT, EBinType bintype=kBinContent) const
Histogram made from this Spectrum, scaled to some exposure.
virtual double ChiSq(osc::IOscCalcAdjustable *osc, const SystShifts &syst=SystShifts::Nominal()) const override
static std::unique_ptr< Spectrum > LoadFrom(TDirectory *dir)
virtual void SaveTo(TDirectory *dir) const
Definition: IPrediction.cxx:85
double LogLikelihood(double e, double o)
The log-likelihood formula for a single bin.
Simple record of shifts applied to systematic parameters.
Definition: SystShifts.h:16
pdgs p
Definition: selectors.fcl:22
process_name opflashCryoW ana
Representation of a spectrum in any variable, with associated POT.
Definition: Spectrum.h:30
process_name pandoraGausCryo1 vertexChargeCryo1 vertexStubCryo1 xmin
static void Delete(TH1D *&h)
Definition: HistCache.cxx:92
virtual Spectrum PredictSyst(osc::IOscCalc *calc, const SystShifts &syst) const
Definition: IPrediction.cxx:62
double POT() const
Definition: Spectrum.h:289
tuple dir
Definition: dropbox.py:28
std::unique_ptr< IPrediction > LoadFrom< IPrediction >(TDirectory *dir)
Definition: IPrediction.cxx:26
SingleSampleExperiment(const IPrediction *pred, const Spectrum &data, const Spectrum &cosmicInTime, const Spectrum &cosmicOutOfTime)
Standard interface to all prediction techniques.
Definition: IPrediction.h:58
TH1 * GetMaskHist(const Spectrum &s, double xmin, double xmax, double ymin, double ymax)
double Livetime() const
Seconds. For informational purposes only. No calculations use this.
Definition: Spectrum.h:292
void SetMaskHist(double xmin=0, double xmax=-1, double ymin=0, double ymax=-1)
virtual void SaveTo(TDirectory *dir) const override
BEGIN_PROLOG could also be cout
static std::unique_ptr< SingleSampleExperiment > LoadFrom(TDirectory *dir)
void SaveTo(TDirectory *dir) const