MaCh3 2.2.1
Reference Guide
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
LikelihoodFit Class Referenceabstract

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.
 
virtual ~LikelihoodFit ()
 Destructor.
 
virtual double CalcChi2 (const double *x)
 Chi2 calculation over all included samples and syst objects.
 
int GetNPars ()
 Get total number of params, this sums over all covariance objects.
 
virtual void RunMCMC ()=0
 Implementation of fitting algorithm.
 
virtual std::string GetName () const
 Get name of class.
 
- Public Member Functions inherited from FitterBase
 FitterBase (manager *const fitMan)
 Constructor.
 
virtual ~FitterBase ()
 Destructor for the FitterBase class.
 
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.
 
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.
 
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.
 
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.
 
void RunLLHScan ()
 Perform a 1D likelihood scan.
 
void GetStepScaleBasedOnLLHScan ()
 LLH scan is good first estimate of step scale.
 
void Run2DLLHScan ()
 Perform a 2D likelihood scan.
 
void RunSigmaVar ()
 Perform a 2D and 1D sigma var for all samples.
 
virtual void StartFromPreviousFit (const std::string &FitName)
 Allow to start from previous fit/chain.
 
virtual std::string GetName () const
 Get name of class.
 

Protected Member Functions

void PrepareFit ()
 prepare output and perform sanity checks
 
- Protected Member Functions inherited from FitterBase
void ProcessMCMC ()
 Process MCMC output.
 
void PrepareOutput ()
 Prepare the output file.
 
void SaveOutput ()
 Save output and close files.
 
void SanitiseInputs ()
 Remove obsolete memory and make other checks before fit starts.
 
void SaveSettings ()
 Save the settings that the MCMC was run with.
 
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.
 
bool CheckSkipParameter (const std::vector< std::string > &SkipVector, const std::string &ParamName) const
 KS: Check whether we want to skip parameter using skip vector.
 

Protected Attributes

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

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:23
Base class for implementing fitting algorithms.
Definition: FitterBase.h:23
manager * fitMan
The manager.
Definition: FitterBase.h:92
int NParsPCA
Number of all parameters from all covariances in PCA base.
Definition: LikelihoodFit.h:30
int NPars
Number of all parameters from all covariances.
Definition: LikelihoodFit.h:28
bool fMirroring
Flag telling if mirroring is used or not.
Definition: LikelihoodFit.h:32
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:113
double logLProp
proposed likelihood
Definition: FitterBase.h:99
int accCount
counts accepted steps
Definition: FitterBase.h:103
unsigned int step
current state
Definition: FitterBase.h:95
double accProb
current acceptance prob
Definition: FitterBase.h:101
std::vector< double > sample_llh
store the llh breakdowns
Definition: FitterBase.h:108
double stepTime
Time of single step.
Definition: FitterBase.h:125
std::unique_ptr< TStopwatch > stepClock
tells how long single step/fit iteration took
Definition: FitterBase.h:123
double logLCurr
current likelihood
Definition: FitterBase.h:97
std::vector< double > syst_llh
systematic llh breakdowns
Definition: FitterBase.h:110
int auto_save
auto save every N steps
Definition: FitterBase.h:139
TTree * outTree
Output tree with posteriors.
Definition: FitterBase.h:137
std::vector< ParameterHandlerBase * > systematics
Systematic holder.
Definition: FitterBase.h:118

◆ GetName()

virtual std::string LikelihoodFit::GetName ( ) const
inlinevirtual

Get name of class.

Reimplemented from FitterBase.

Reimplemented in MinuitFit, and PSO.

Definition at line 22 of file LikelihoodFit.h.

22{return "LikelihoodFit";};

◆ 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
26
27 // Prepare the output branches
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:82
void PrepareOutput()
Prepare the output file.
Definition: FitterBase.cpp:148

◆ RunMCMC()

virtual void LikelihoodFit::RunMCMC ( )
pure virtual

Implementation of fitting algorithm.

Implements FitterBase.

Implemented in MinuitFit, PSO, and PyLikelihoodFit.

Member Data Documentation

◆ fMirroring

bool LikelihoodFit::fMirroring
protected

Flag telling if mirroring is used or not.

Definition at line 32 of file LikelihoodFit.h.

◆ NPars

int LikelihoodFit::NPars
protected

Number of all parameters from all covariances.

Definition at line 28 of file LikelihoodFit.h.

◆ NParsPCA

int LikelihoodFit::NParsPCA
protected

Number of all parameters from all covariances in PCA base.

Definition at line 30 of file LikelihoodFit.h.


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