MCMCSample.cxx
Go to the documentation of this file.
2 
3 #include "TObjString.h"
4 #include "TParameter.h"
5 
6 namespace ana
7 {
8  //----------------------------------------------------------------------
9  MCMCSample::MCMCSample(double LL, const std::vector<double> &diagVals, const std::vector<double> &entryVals,
10  const std::vector<std::string> &diagBranchNames,
11  const std::vector<const IFitVar *> &vars, const std::vector<const ISyst *> &systs)
12  : fDiagBranches(std::move(diagBranchNames)), fVars(std::move(vars)), fSysts(std::move(systs))
13  {
14  if(diagVals.size() != fDiagBranches.size() || entryVals.size() != fVars.size() + fSysts.size())
15  {
16  std::cerr << "MCMCSample::MCMCSample(): branch sizes don't match." << std::endl;
17  abort();
18  }
19  fVals.reserve(1 + diagVals.size() + entryVals.size());
20  fVals.push_back(LL);
21  fVals.insert(fVals.end(), diagVals.begin(), diagVals.end());
22  fVals.insert(fVals.end(), entryVals.begin(), entryVals.end());
23  }
24 
25  //----------------------------------------------------------------------
26  MCMCSample::MCMCSample(const TMap &map, const std::vector<const ana::IFitVar *> &vars,
27  const std::vector<const ana::ISyst *> &systs)
28  {
29  fVals.resize(map.GetSize());
30  std::size_t diagBranchSize = map.GetSize() - vars.size() - systs.size() - 1;
31 
32  for (const auto &mapPair : map)
33  {
34  auto pair = dynamic_cast<TPair *>(mapPair);
35  std::string name(dynamic_cast<TObjString *>(pair->Key())->GetString());
36  double value = dynamic_cast<TParameter<double> *>(pair->Value())->GetVal();
37 
38  auto itVar = std::find_if(vars.begin(), vars.end(), [&name](const IFitVar* v){ return v->ShortName() == name; });
39  auto itSyst = (itVar == vars.end()) ? systs.end() : std::find_if(systs.begin(), systs.end(), [&name](const ISyst* s){ return s->ShortName() == name; });
40 
41  // find out whether this is a Var, a Syst, or diagnostic branch
42  if (itVar != vars.end())
43  fVals[1 + diagBranchSize + std::distance(vars.begin(), itVar)] = value;
44  else if (itSyst != systs.end())
45  fVals[1 + diagBranchSize + vars.size() + std::distance(systs.begin(), itSyst)] = value;
46  else if (name == "logprob")
47  fVals[0] = value;
48  else
49  {
50  fVals[1 + fDiagBranches.size()] = value;
51  fDiagBranches.push_back(name);
52  }
53  }
54  }
55 
56  //----------------------------------------------------------------------
57  double MCMCSample::DiagnosticVal(const std::string &brName) const
58  {
59  auto it = std::find(fDiagBranches.begin(), fDiagBranches.end(), brName);
60  assert(it != fDiagBranches.end() && ("This MCMCSample does not contain a value called '" + brName + "'").c_str());
61 
62  return fVals[1 + std::distance(fDiagBranches.begin(), it)];
63  }
64 
65  //----------------------------------------------------------------------
66  std::unique_ptr<TMap> MCMCSample::ToTMap() const
67  {
68  auto ret = std::make_unique<TMap>();
69  ret->Add(new TObjString("logprob"), new TParameter<double>("", fVals[0]));
70 
71  for (const auto & brName : fDiagBranches)
72  ret->Add(new TObjString(brName.c_str()), new TParameter<double>("", DiagnosticVal(brName)));
73  for (const auto & v : fVars)
74  ret->Add(new TObjString(v->ShortName().c_str()), new TParameter<double>("", Val(v)));
75  for (const auto & s : fSysts)
76  ret->Add(new TObjString(s->ShortName().c_str()), new TParameter<double>("", Val(s)));
77 
78  return std::move(ret);
79  }
80 
81  //----------------------------------------------------------------------
82  double MCMCSample::Val(const ana::IFitVar *var) const
83  {
84  auto it = std::find(fVars.begin(), fVars.end(), var);
85  assert(it != fVars.end() && ("This MCMCSample does not contain Var: '" + var->ShortName() + "'").c_str());
86 
87  return fVals[1 + fDiagBranches.size() + std::distance(fVars.begin(), it)];
88  }
89 
90  //----------------------------------------------------------------------
91  double MCMCSample::Val(const ana::ISyst *syst) const
92  {
93  auto it = std::find(fSysts.begin(), fSysts.end(), syst);
94  assert(it != fSysts.end() && ("This MCMCSample does not contain Syst: '" + syst->ShortName() + "'").c_str());
95 
96  return fVals[1 + fDiagBranches.size() + fVars.size() + std::distance(fSysts.begin(), it)];
97  }
98 
99 }
std::unique_ptr< TMap > ToTMap() const
Mostly for serialization.
Definition: MCMCSample.cxx:66
const XML_Char * name
Definition: expat.h:151
std::vector< std::string > fDiagBranches
Definition: MCMCSample.h:66
Cuts and Vars for the 2020 FD DiF Study.
Definition: vars.h:6
set< int >::iterator it
std::vector< SystGroupDef > systs
Definition: syst_header.h:385
virtual const std::string & ShortName() const final
The name printed out to the screen.
Definition: ISyst.h:27
std::vector< const ana::IFitVar * > fVars
Definition: MCMCSample.h:67
OStream cerr
Definition: OStream.cxx:7
double DiagnosticVal(const std::string &brName) const
Definition: MCMCSample.cxx:57
unsigned distance(const T &t1, const T &t2)
Encapsulate code to systematically shift a caf::SRProxy.
Definition: ISyst.h:14
const XML_Char * s
Definition: expat.h:262
const XML_Char int const XML_Char * value
Definition: expat.h:331
double Val(const ana::IFitVar *var) const
Definition: MCMCSample.cxx:82
MCMCSample(double LL, const std::vector< double > &diagVals, const std::vector< double > &entryVals, const std::vector< std::string > &diagBranchNames, const std::vector< const IFitVar * > &vars, const std::vector< const ISyst * > &systs)
Definition: MCMCSample.cxx:9
const std::map< std::pair< std::string, std::string >, Variable > vars
const std::string & ShortName() const
Definition: IFitVar.h:36
string GetString(xmlDocPtr xml_doc, string node_path)
assert(nhit_max >=nhit_nbins)
Interface definition for fittable variables.
Definition: IFitVar.h:16
std::vector< double > fVals
Definition: MCMCSample.h:70
std::vector< const ana::ISyst * > fSysts
Definition: MCMCSample.h:68
enum BeamMode string