MaCh3  2.4.2
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 RunLLHMap ()
 Perform a general multi-dimensional 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 1D/2D 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 GetScanRange (std::map< std::string, std::vector< double >> &scanRanges) const
 YSP: Set up a mapping to store parameters with user-specified ranges, suggested by D. Barrow. More...
 
void GetParameterScanRange (const ParameterHandlerBase *cov, const int i, double &CentralValue, double &lower, double &upper, const int n_points, const std::string &suffix="") const
 Helper function to get parameter scan range, central value. 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) const
 For comparison with other fitting frameworks (like 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 for configuration handling. 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, single SampleHandler can store more than one analysis sample! 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 }
#define MACH3LOG_INFO
Definition: MaCh3Logger.h:35
FitterBase(Manager *const fitMan)
Constructor.
Definition: FitterBase.cpp:16
Manager * fitMan
The manager for configuration handling.
Definition: FitterBase.h:108
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() const
Return config.
Definition: Manager.h:41

◆ ~LikelihoodFit()

LikelihoodFit::~LikelihoodFit ( )
virtual

Destructor.

Definition at line 16 of file LikelihoodFit.cpp.

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

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 39 of file LikelihoodFit.cpp.

39  {
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 }
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
unsigned int step
current state
Definition: FitterBase.h:111
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
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

◆ 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 21 of file LikelihoodFit.cpp.

21  {
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 }
void SaveSettings()
Save the settings that the MCMC was run with.
Definition: FitterBase.cpp:79
void PrepareOutput()
Prepare the output file.
Definition: FitterBase.cpp:153

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: