MultiExperiment.cxx
Go to the documentation of this file.
1 #include "Utilities/func/Stan.h"
2 
4 
5 #include "CAFAna/Core/ISyst.h"
8 
9 #include "TDirectory.h"
10 #include "TObjString.h"
11 
12 #include <cassert>
13 #include <iostream>
14 
15 namespace ana
16 {
17  REGISTER_LOADFROM("MultiExperiment", IExperiment, MultiExperiment);
18 
19  //----------------------------------------------------------------------
21  const std::vector<std::pair<const ISyst *, const ISyst *>> &corrs) const
22  {
23  // Make a local copy we're going to rewrite into the terms this
24  // sub-experiment will accept.
25  SystShifts localShifts = shiftsIn;
26  for(auto it: corrs)
27  {
28  // We're mapping prim -> sec
29  const ISyst* prim = it.first;
30  const ISyst* sec = it.second;
31  if(shiftsIn.HasStan(prim) || shiftsIn.GetShift<double>(prim) != 0)
32  {
33  // sec can be unset, which means there's no representation needed
34  // of prim in the sub-experiment.
35  if(sec)
36  {
37  if (shiftsIn.HasStan(prim))
38  localShifts.SetShift(sec, shiftsIn.GetShift<stan::math::var>(prim));
39  else
40  localShifts.SetShift(sec, shiftsIn.GetShift<double>(prim));
41  }
42 
43  // We've either translated or discarded prim, so drop it here.
44  localShifts.RemoveShift(prim);
45  }
46  }
47 
48  return localShifts;
49  }
50 
51  //----------------------------------------------------------------------
53  const SystShifts& syst) const
54  {
55  double ret = 0;
56  for(unsigned int n = 0; n < fExpts.size(); ++n){
57  // Don't waste time fiddling with the systematics if for sure there's
58  // nothing to do.
59  if(fSystCorrelations[n].empty())
60  ret += fExpts[n]->ChiSq(osc, syst);
61  else
62  ret += fExpts[n]->ChiSq(osc, ApplySystCorrs(syst, fSystCorrelations[n]));
63  }
64  return ret;
65  }
66 
67  //----------------------------------------------------------------------
70  const std::vector<std::pair<const ISyst*, const ISyst*>>& corrs)
71  {
72  // Sanity-check the mapping
73  std::map<const ISyst*, const ISyst*> already;
74  for(auto it: corrs){
75  if(it.first == it.second)
76  {
77  std::cout << "MultiExperiment::SetSystCorrelations(): You're trying to map a syst to itself: " << std::endl;
78  auto name = it.first ? it.first->ShortName() : "nullptr";
79  std::cout << name << " --> " << name << std::endl;
80  abort();
81  }
82 
83  // Don't worry if second element is null pointer
84  if (!it.second) continue;
85  if(already.find(it.second) == already.end()){
86  already[it.second] = it.first;
87  }
88  else{
89  std::cout << "MultiExperiment::SetSystCorrelations(): Warning!\n"
90  << "In experiment " << idx << " both "
91  << already[it.second]->ShortName() << " and "
92  << it.first->ShortName()
93  << " are configured to map to " << it.second->ShortName()
94  << ". That's probably not what you want." << std::endl;
95  }
96  }
97 
98  // Apply it
99  fSystCorrelations[idx] = corrs;
100  }
101 
102  //----------------------------------------------------------------------
103  void MultiExperiment::Reset() const {
104  for (auto expt : fExpts) expt->Reset();
105  }
106 
107  //----------------------------------------------------------------------
110  {
111  stan::math::var ret = 0.;
112  for (unsigned int idx = 0; idx < fExpts.size(); ++idx)
113  {
114  if(fSystCorrelations[idx].empty())
115  ret += fExpts[idx]->LogLikelihood(osc, syst);
116  else
117  ret += fExpts[idx]->LogLikelihood(osc, ApplySystCorrs(syst, fSystCorrelations[idx]));
118  }
119  return ret;
120  }
121 
122  //----------------------------------------------------------------------
123  void MultiExperiment::SaveTo(TDirectory* dir, const std::string& name) const
124  {
125  bool hasCorr = false;
126  for(auto it: fSystCorrelations) if(!it.empty()) hasCorr = true;
127 
128  if(hasCorr){
129  std::cerr << "Warning in MultiExperiment: systematic correlations are set and will not be serialized by this call to SaveTo(). You will have to re-set them once you load the experiment back in." << std::endl;
130  }
131 
132  TDirectory* tmp = dir;
133 
134  dir = dir->mkdir(name.c_str()); // switch to subdir
135  dir->cd();
136 
137  TObjString("MultiExperiment").Write("type");
138 
139  for(unsigned int i = 0; i < fExpts.size(); ++i){
140  fExpts[i]->SaveTo(dir, TString::Format("expt%d", i).Data());
141  }
142 
143  dir->Write();
144  delete dir;
145 
146  tmp->cd();
147  }
148 
149  //----------------------------------------------------------------------
150  std::unique_ptr<MultiExperiment> MultiExperiment::LoadFrom(TDirectory* dir, const std::string& name)
151  {
152  dir = dir->GetDirectory(name.c_str()); // switch to subdir
153  assert(dir);
154 
155  TObjString* ptag = (TObjString*)dir->Get("type");
156  assert(ptag);
157  assert(ptag->GetString() == "MultiExperiment");
158  delete ptag;
159 
160  std::vector<const IExperiment*> expts;
161 
162  for(int i = 0; ; ++i){
163  const std::string subname = TString::Format("expt%d", i).Data();
164  TDirectory* subdir = dir->GetDirectory(subname.c_str());
165  if(!subdir) break;
166  delete subdir;
167 
168  expts.push_back(ana::LoadFrom<IExperiment>(dir, name).release());
169  }
170 
171  assert(!expts.empty());
172 
173  delete dir;
174 
175  return std::unique_ptr<MultiExperiment>(new MultiExperiment(expts));
176  }
177 
178 }
const XML_Char * name
Definition: expat.h:151
std::vector< const IExperiment * > fExpts
void Reset() const override
Cuts and Vars for the 2020 FD DiF Study.
Definition: vars.h:6
set< int >::iterator it
subdir
Definition: cvnie.py:7
Simple record of shifts applied to systematic parameters.
Definition: SystShifts.h:20
var_value< double > var
Definition: StanTypedefs.h:14
OStream cerr
Definition: OStream.cxx:7
Float_t tmp
Definition: plot.C:36
Encapsulate code to systematically shift a caf::SRProxy.
Definition: ISyst.h:14
std::vector< std::vector< std::pair< const ISyst *, const ISyst * > > > fSystCorrelations
expt
Definition: demo5.py:34
std::void_t< T > n
stan::math::var LogLikelihood(osc::IOscCalcAdjustableStan *osc, const SystShifts &syst) const override
Sum up log-likelihoods of sub-expts.
T GetShift(const ISyst *syst) const
virtual double ChiSq(osc::IOscCalcAdjustable *osc, const SystShifts &syst=SystShifts::Nominal()) const override
bool HasStan(const ISyst *s) const
Definition: SystShifts.h:63
Oscillation probability calculators.
Definition: Calcs.h:5
OStream cout
Definition: OStream.cxx:6
SystShifts ApplySystCorrs(const SystShifts &shiftsIn, const std::vector< std::pair< const ISyst *, const ISyst * >> &corrs) const
Get a new SystShifts, applying the specified correlations to it.
TDirectory * dir
Definition: macro.C:5
REGISTER_LOADFROM("BENDecomp", IDecomp, BENDecomp)
assert(nhit_max >=nhit_nbins)
void Format(TGraph *gr, int lcol, int lsty, int lwid, int mcol, int msty, double msiz)
Definition: Style.cxx:154
virtual void SaveTo(TDirectory *dir, const std::string &name) const override
std::unique_ptr< IExperiment > LoadFrom< IExperiment >(TDirectory *dir, const std::string &label)
Definition: IExperiment.cxx:16
static std::unique_ptr< MultiExperiment > LoadFrom(TDirectory *dir, const std::string &name)
void RemoveShift(const ISyst *syst)
Definition: SystShifts.cxx:74
void SetShift(const ISyst *syst, double shift, bool force=false)
Definition: SystShifts.cxx:81
MultiExperiment(std::vector< const IExperiment * > expts={})
void SetSystCorrelations(int idx, const std::vector< std::pair< const ISyst *, const ISyst * >> &corrs)
enum BeamMode string