MaCh3  2.2.3
Reference Guide
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
LikelihoodFit Class Reference

Implementation of base Likelihood Fit class, it is mostly responsible for likelihood calculation while actual use depend on classes which inherits. More...

#include <Fitters/LikelihoodFit.h>

Inheritance diagram for LikelihoodFit:
[legend]
Collaboration diagram for LikelihoodFit:
[legend]

Public Member Functions

 LikelihoodFit (manager *const fitMan)
 Constructor. More...
 
virtual ~LikelihoodFit ()
 Destructor. More...
 
virtual double CalcChi2 (const double *x)
 Chi2 calculation over all included samples and syst objects. More...
 
int GetNPars ()
 Get total number of params, this sums over all covariance objects. More...
 
- Public Member Functions inherited from FitterBase
 FitterBase (manager *const fitMan)
 Constructor. More...
 
virtual ~FitterBase ()
 Destructor for the FitterBase class. More...
 
void AddSampleHandler (SampleHandlerBase *sample)
 This function adds a sample PDF object to the analysis framework. The sample PDF object will be utilized in fitting procedures or likelihood scans. More...
 
void AddSystObj (ParameterHandlerBase *cov)
 This function adds a Covariance object to the analysis framework. The Covariance object will be utilized in fitting procedures or likelihood scans. More...
 
virtual void RunMCMC ()=0
 The specific fitting algorithm implemented in this function depends on the derived class. It could be Markov Chain Monte Carlo (MCMC), MinuitFit, or another algorithm. More...
 
void DragRace (const int NLaps=100)
 Calculates the required time for each sample or covariance object in a drag race simulation. Inspired by Dan's feature. More...
 
void RunLLHScan ()
 Perform a 1D likelihood scan. More...
 
void GetStepScaleBasedOnLLHScan ()
 LLH scan is good first estimate of step scale. More...
 
void Run2DLLHScan ()
 Perform a 2D likelihood scan. More...
 
void RunSigmaVar ()
 Perform a 2D and 1D sigma var for all samples. More...
 
void RunSigmaVarFD ()
 Perform a 1D sigma var for all samples. More...
 
virtual void StartFromPreviousFit (const std::string &FitName)
 Allow to start from previous fit/chain. More...
 
std::string GetName () const
 Get name of class. More...
 

Protected Member Functions

void PrepareFit ()
 prepare output and perform sanity checks More...
 
- Protected Member Functions inherited from FitterBase
void ProcessMCMC ()
 Process MCMC output. More...
 
void PrepareOutput ()
 Prepare the output file. More...
 
void SaveOutput ()
 Save output and close files. More...
 
void SanitiseInputs ()
 Remove obsolete memory and make other checks before fit starts. More...
 
void SaveSettings ()
 Save the settings that the MCMC was run with. More...
 
bool GetScaneRange (std::map< std::string, std::vector< double >> &scanRanges)
 YSP: Set up a mapping to store parameters with user-specified ranges, suggested by D. Barrow. More...
 
bool CheckSkipParameter (const std::vector< std::string > &SkipVector, const std::string &ParamName) const
 KS: Check whether we want to skip parameter using skip vector. More...
 
void CustomRange (const std::string &ParName, const double sigma, double &ParamShiftValue)
 For comparison with P-Theta we usually have to apply different parameter values then usual 1, 3 sigma. More...
 

Protected Attributes

int NPars
 Number of all parameters from all covariances. More...
 
int NParsPCA
 Number of all parameters from all covariances in PCA base. More...
 
bool fMirroring
 Flag telling if mirroring is used or not. More...
 
- Protected Attributes inherited from FitterBase
managerfitMan
 The manager. More...
 
unsigned int step
 current state More...
 
double logLCurr
 current likelihood More...
 
double logLProp
 proposed likelihood More...
 
double accProb
 current acceptance prob More...
 
int accCount
 counts accepted steps More...
 
unsigned int stepStart
 step start, by default 0 if we start from previous chain then it will be different More...
 
std::vector< double > sample_llh
 store the llh breakdowns More...
 
std::vector< double > syst_llh
 systematic llh breakdowns More...
 
std::vector< SampleHandlerBase * > samples
 Sample holder. More...
 
unsigned int TotalNSamples
 Total number of samples used. More...
 
std::vector< ParameterHandlerBase * > systematics
 Systematic holder. More...
 
std::unique_ptr< TStopwatch > clock
 tells global time how long fit took More...
 
std::unique_ptr< TStopwatch > stepClock
 tells how long single step/fit iteration took More...
 
double stepTime
 Time of single step. More...
 
std::unique_ptr< TRandom3 > random
 Random number. More...
 
TFile * outputFile
 Output. More...
 
TDirectory * CovFolder
 Output cov folder. More...
 
TDirectory * SampleFolder
 Output sample folder. More...
 
TTree * outTree
 Output tree with posteriors. More...
 
int auto_save
 auto save every N steps More...
 
bool fTestLikelihood
 Necessary for some fitting algorithms like PSO. More...
 
bool FileSaved
 Checks if file saved not repeat some operations. More...
 
bool SettingsSaved
 Checks if setting saved not repeat some operations. More...
 
bool OutputPrepared
 Checks if output prepared not repeat some operations. More...
 
std::string AlgorithmName
 Name of fitting algorithm that is being used. More...
 

Detailed Description

Implementation of base Likelihood Fit class, it is mostly responsible for likelihood calculation while actual use depend on classes which inherits.

Definition at line 6 of file LikelihoodFit.h.

Constructor & Destructor Documentation

◆ LikelihoodFit()

LikelihoodFit::LikelihoodFit ( manager *const  fitMan)

Constructor.

Definition at line 5 of file LikelihoodFit.cpp.

5  : FitterBase(man) {
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 }
MaCh3Plotting::PlottingManager * man
#define MACH3LOG_INFO
Definition: MaCh3Logger.h:25
manager * fitMan
The manager.
Definition: FitterBase.h:110
FitterBase(manager *const fitMan)
Constructor.
Definition: FitterBase.cpp:16
int NParsPCA
Number of all parameters from all covariances in PCA base.
Definition: LikelihoodFit.h:25
int NPars
Number of all parameters from all covariances.
Definition: LikelihoodFit.h:23
bool fMirroring
Flag telling if mirroring is used or not.
Definition: LikelihoodFit.h:27
YAML::Node const & raw()
Return config.
Definition: Manager.h:41

◆ ~LikelihoodFit()

LikelihoodFit::~LikelihoodFit ( )
virtual

Destructor.

Definition at line 16 of file LikelihoodFit.cpp.

16  {
17 // *************************
18 
19 }

Member Function Documentation

◆ CalcChi2()

double LikelihoodFit::CalcChi2 ( const double *  x)
virtual

Chi2 calculation over all included samples and syst objects.

Reimplemented in PSO.

Definition at line 40 of file LikelihoodFit.cpp.

40  {
41 // *******************
42  if (step % 10000 == 0) {
43  MACH3LOG_INFO("Iteration {}", step);
44  }
45 
46  stepClock->Start();
47 
48  int ParCounter = 0;
49  double llh = 0;
50  for (std::vector<ParameterHandlerBase*>::iterator it = systematics.begin(); it != systematics.end(); ++it)
51  {
52  if(!(*it)->IsPCA())
53  {
54  std::vector<double> pars;
55  const int NumPar = (*it)->GetNumParams();
56  //KS: Avoid push back as they are slow
57  pars.resize(NumPar);
58  for(int i = 0; i < NumPar; ++i, ++ParCounter)
59  {
60  double ParVal = x[ParCounter];
61  //KS: Basically apply mirroring for parameters out of bounds
62  if(fMirroring)
63  {
64  if(ParVal < (*it)->GetLowerBound(i))
65  {
66  ParVal = (*it)->GetLowerBound(i) + ((*it)->GetLowerBound(i) - ParVal);
67  }
68  else if (ParVal > (*it)->GetUpperBound(i))
69  {
70  ParVal = (*it)->GetUpperBound(i) - ( ParVal - (*it)->GetUpperBound(i));
71  }
72  }
73  pars[i] = ParVal;
74  }
75  (*it)->SetParameters(pars);
76  }
77  else
78  {
79  std::vector<double> pars;
80  const int NumPar = (*it)->GetNParameters();
81  //KS: Avoid push back as they are slow
82  pars.resize(NumPar);
83  for(int i = 0; i < NumPar; ++i, ++ParCounter)
84  {
85  double ParVal = x[ParCounter];
86  //KS: Basically apply mirroring for parameters out of bounds
87  pars[i] = ParVal;
88  }
89  (*it)->GetPCAHandler()->SetParametersPCA(pars);
90  }
91  (*it)->AcceptStep();
92  }
93 
94  // Loop over the systematics and propose the initial step
95  int stdIt = 0;
96  for (std::vector<ParameterHandlerBase*>::iterator it = systematics.begin(); it != systematics.end(); ++it, ++stdIt)
97  {
98  //GetLikelihood will return LargeL if out of bounds, for minimizers this is not the problem, while calcLikelihood will return actual likelihood
99  syst_llh[stdIt] = (*it)->CalcLikelihood();
100  llh += syst_llh[stdIt];
101  #ifdef DEBUG
102  if (debug) debugFile << "LLH after " << systematics[stdIt]->GetName() << " " << llh << std::endl;
103  #endif
104  }
105  // Could multi-thread this
106  // But since sample reweight is multi-threaded it's probably better to do that
107  for (size_t i = 0; i < samples.size(); i++)
108  {
109  samples[i]->Reweight();
110  }
111 
112  //DB for atmospheric event by event sample migration, need to fully reweight all samples to allow event passing prior to likelihood evaluation
113  for (size_t i = 0; i < samples.size(); i++) {
114  // Get the sample likelihoods and add them
115  sample_llh[i] = samples[i]->GetLikelihood();
116  llh += sample_llh[i];
117  #ifdef DEBUG
118  if (debug) debugFile << "LLH after sample " << i << " " << llh << std::endl;
119  #endif
120  }
121 
122  // Save the proposed likelihood (class member)
123  logLProp = llh;
124  logLCurr = llh;
125  accProb = 1;
126 
127  stepClock->Stop();
128  stepTime = stepClock->RealTime();
129 
130  // Write step to output tree
131  outTree->Fill();
132 
133  // Auto save the output
134  if (step % auto_save == 0) outTree->AutoSave();
135  step++;
136  accCount++;
137 
138  llh = 2.0*llh;
139  return llh;
140 }
std::vector< SampleHandlerBase * > samples
Sample holder.
Definition: FitterBase.h:131
double logLProp
proposed likelihood
Definition: FitterBase.h:117
int accCount
counts accepted steps
Definition: FitterBase.h:121
unsigned int step
current state
Definition: FitterBase.h:113
double accProb
current acceptance prob
Definition: FitterBase.h:119
std::vector< double > sample_llh
store the llh breakdowns
Definition: FitterBase.h:126
double stepTime
Time of single step.
Definition: FitterBase.h:143
std::unique_ptr< TStopwatch > stepClock
tells how long single step/fit iteration took
Definition: FitterBase.h:141
double logLCurr
current likelihood
Definition: FitterBase.h:115
std::vector< double > syst_llh
systematic llh breakdowns
Definition: FitterBase.h:128
int auto_save
auto save every N steps
Definition: FitterBase.h:157
TTree * outTree
Output tree with posteriors.
Definition: FitterBase.h:155
std::vector< ParameterHandlerBase * > systematics
Systematic holder.
Definition: FitterBase.h:136

◆ GetNPars()

int LikelihoodFit::GetNPars ( )
inline

Get total number of params, this sums over all covariance objects.

Definition at line 16 of file LikelihoodFit.h.

16 {return NPars;};

◆ PrepareFit()

void LikelihoodFit::PrepareFit ( )
protected

prepare output and perform sanity checks

Definition at line 22 of file LikelihoodFit.cpp.

22  {
23 // *******************
24  // Save the settings into the output file
25  SaveSettings();
26 
27  // Prepare the output branches
28  PrepareOutput();
29 
30  for (size_t s = 0; s < systematics.size(); ++s) {
31  NPars += systematics[s]->GetNumParams();
32  NParsPCA += systematics[s]->GetNParameters();
33  }
34 
35  //KS: If PCA is note enabled NParsPCA == NPars
36  MACH3LOG_INFO("Total number of parameters {}", NParsPCA);
37 }
void SaveSettings()
Save the settings that the MCMC was run with.
Definition: FitterBase.cpp:84
void PrepareOutput()
Prepare the output file.
Definition: FitterBase.cpp:155

Member Data Documentation

◆ fMirroring

bool LikelihoodFit::fMirroring
protected

Flag telling if mirroring is used or not.

Definition at line 27 of file LikelihoodFit.h.

◆ NPars

int LikelihoodFit::NPars
protected

Number of all parameters from all covariances.

Definition at line 23 of file LikelihoodFit.h.

◆ NParsPCA

int LikelihoodFit::NParsPCA
protected

Number of all parameters from all covariances in PCA base.

Definition at line 25 of file LikelihoodFit.h.


The documentation for this class was generated from the following files: