MaCh3  2.4.2
Reference Guide
LikelihoodFit.cpp
Go to the documentation of this file.
1 #include "LikelihoodFit.h"
2 
3 // *******************
4 // Run the Markov chain with all the systematic objects added
6 // *******************
7  NPars = 0;
8  NParsPCA = 0;
9  fMirroring = GetFromManager<bool>(fitMan->raw()["General"]["Fitter"]["Mirroring"], false);
10  if(fMirroring) MACH3LOG_INFO("Mirroring enabled");
11 }
12 
13 
14 // *************************
15 // Destructor: close the logger and output file
17 // *************************
18 }
19 
20 // *******************
22 // *******************
23  // Save the settings into the output file
24  SaveSettings();
25 
26  // Prepare the output branches
27  PrepareOutput();
28 
29  for (size_t s = 0; s < systematics.size(); ++s) {
30  NPars += systematics[s]->GetNumParams();
31  NParsPCA += systematics[s]->GetNParameters();
32  }
33 
34  //KS: If PCA is note enabled NParsPCA == NPars
35  MACH3LOG_INFO("Total number of parameters {}", NParsPCA);
36 }
37 
38 // *******************
39 double LikelihoodFit::CalcChi2(const double* x) {
40 // *******************
41  if (step % 10000 == 0) {
42  MACH3LOG_INFO("Iteration {}", step);
43  }
44 
45  stepClock->Start();
46 
47  int ParCounter = 0;
48  double llh = 0;
49  for (std::vector<ParameterHandlerBase*>::iterator it = systematics.begin(); it != systematics.end(); ++it)
50  {
51  if(!(*it)->IsPCA())
52  {
53  std::vector<double> pars;
54  const int NumPar = (*it)->GetNumParams();
55  //KS: Avoid push back as they are slow
56  pars.resize(NumPar);
57  for(int i = 0; i < NumPar; ++i, ++ParCounter)
58  {
59  double ParVal = x[ParCounter];
60  //KS: Basically apply mirroring for parameters out of bounds
61  if(fMirroring)
62  {
63  if(ParVal < (*it)->GetLowerBound(i))
64  {
65  ParVal = (*it)->GetLowerBound(i) + ((*it)->GetLowerBound(i) - ParVal);
66  }
67  else if (ParVal > (*it)->GetUpperBound(i))
68  {
69  ParVal = (*it)->GetUpperBound(i) - ( ParVal - (*it)->GetUpperBound(i));
70  }
71  }
72  pars[i] = ParVal;
73  }
74  (*it)->SetParameters(pars);
75  }
76  else
77  {
78  std::vector<double> pars;
79  const int NumPar = (*it)->GetNParameters();
80  //KS: Avoid push back as they are slow
81  pars.resize(NumPar);
82  for(int i = 0; i < NumPar; ++i, ++ParCounter)
83  {
84  double ParVal = x[ParCounter];
85  //KS: Basically apply mirroring for parameters out of bounds
86  pars[i] = ParVal;
87  }
88  (*it)->GetPCAHandler()->SetParametersPCA(pars);
89  }
90  (*it)->AcceptStep();
91  }
92 
93  // Loop over the systematics and propose the initial step
94  int stdIt = 0;
95  for (std::vector<ParameterHandlerBase*>::iterator it = systematics.begin(); it != systematics.end(); ++it, ++stdIt)
96  {
97  //GetLikelihood will return LargeL if out of bounds, for minimizers this is not the problem, while calcLikelihood will return actual likelihood
98  syst_llh[stdIt] = (*it)->CalcLikelihood();
99  llh += syst_llh[stdIt];
100  #ifdef DEBUG
101  if (debug) debugFile << "LLH after " << systematics[stdIt]->GetName() << " " << llh << std::endl;
102  #endif
103  }
104  // Could multi-thread this
105  // But since sample reweight is multi-threaded it's probably better to do that
106  for (size_t i = 0; i < samples.size(); i++)
107  {
108  samples[i]->Reweight();
109  }
110 
111  //DB for atmospheric event by event sample migration, need to fully reweight all samples to allow event passing prior to likelihood evaluation
112  for (size_t i = 0; i < samples.size(); i++) {
113  // Get the sample likelihoods and add them
114  sample_llh[i] = samples[i]->GetLikelihood();
115  llh += sample_llh[i];
116  #ifdef DEBUG
117  if (debug) debugFile << "LLH after sample " << i << " " << llh << std::endl;
118  #endif
119  }
120 
121  // Save the proposed likelihood (class member)
122  logLProp = llh;
123  logLCurr = llh;
124  accProb = 1;
125 
126  stepClock->Stop();
127  stepTime = stepClock->RealTime();
128 
129  // Write step to output tree
130  outTree->Fill();
131 
132  // Auto save the output
133  if (step % auto_save == 0) outTree->AutoSave();
134  step++;
135  accCount++;
136 
137  llh = 2.0*llh;
138  return llh;
139 }
140 
#define MACH3LOG_INFO
Definition: MaCh3Logger.h:35
Base class for implementing fitting algorithms.
Definition: FitterBase.h:26
std::vector< SampleHandlerBase * > samples
Sample holder.
Definition: FitterBase.h:129
double logLProp
proposed likelihood
Definition: FitterBase.h:115
int accCount
counts accepted steps
Definition: FitterBase.h:119
void SaveSettings()
Save the settings that the MCMC was run with.
Definition: FitterBase.cpp:79
unsigned int step
current state
Definition: FitterBase.h:111
void PrepareOutput()
Prepare the output file.
Definition: FitterBase.cpp:153
double accProb
current acceptance prob
Definition: FitterBase.h:117
std::vector< double > sample_llh
store the llh breakdowns
Definition: FitterBase.h:124
double stepTime
Time of single step.
Definition: FitterBase.h:141
Manager * fitMan
The manager for configuration handling.
Definition: FitterBase.h:108
std::unique_ptr< TStopwatch > stepClock
tells how long single step/fit iteration took
Definition: FitterBase.h:139
double logLCurr
current likelihood
Definition: FitterBase.h:113
std::vector< double > syst_llh
systematic llh breakdowns
Definition: FitterBase.h:126
int auto_save
auto save every N steps
Definition: FitterBase.h:155
TTree * outTree
Output tree with posteriors.
Definition: FitterBase.h:153
std::vector< ParameterHandlerBase * > systematics
Systematic holder.
Definition: FitterBase.h:134
int NParsPCA
Number of all parameters from all covariances in PCA base.
Definition: LikelihoodFit.h:25
LikelihoodFit(Manager *const fitMan)
Constructor.
virtual ~LikelihoodFit()
Destructor.
int NPars
Number of all parameters from all covariances.
Definition: LikelihoodFit.h:23
void PrepareFit()
prepare output and perform sanity checks
bool fMirroring
Flag telling if mirroring is used or not.
Definition: LikelihoodFit.h:27
virtual double CalcChi2(const double *x)
Chi2 calculation over all included samples and syst objects.
The manager class is responsible for managing configurations and settings.
Definition: Manager.h:16
YAML::Node const & raw() const
Return config.
Definition: Manager.h:41