MaCh3 2.2.1
Reference Guide
Loading...
Searching...
No Matches
Public Member Functions | Private Attributes | List of all members
MinuitFit Class Reference

Implementation of Minuit fitting algorithm [15]. More...

#include <Fitters/MinuitFit.h>

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

Public Member Functions

 MinuitFit (manager *const fitMan)
 Constructor.
 
virtual ~MinuitFit ()
 Destructor.
 
void RunMCMC () override
 Actual implementation of Minuit Fit algorithm.
 
std::string GetName () const
 Get name of class.
 
- Public Member Functions inherited from LikelihoodFit
 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.
 

Private Attributes

std::unique_ptr< ROOT::Math::Minimizer > minuit
 Pointer to minimizer, which most often is Minuit.
 

Additional Inherited Members

- Protected Member Functions inherited from LikelihoodFit
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 inherited from LikelihoodFit
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 Minuit fitting algorithm [15].

Author
Kamil Skwarczynski

Definition at line 14 of file MinuitFit.h.

Constructor & Destructor Documentation

◆ MinuitFit()

MinuitFit::MinuitFit ( manager *const  fitMan)

Constructor.

Todo:
KS: Make this in future configurable, for more see: https://root.cern.ch/doc/master/classROOT_1_1Math_1_1Minimizer.html

Definition at line 5 of file MinuitFit.cpp.

6// *******************
8 // Minimizer type: determines the underlying implementation.
9 // Available types include:
10 // - "Minuit2" (recommended modern option)
11 // - "Minuit" (legacy)
12 // - "Fumili"
13 // - "GSLMultiMin" (for gradient-free minimization)
14 // - "GSLMultiFit"
15 // - "GSLSimAn" (Simulated Annealing)
16 const std::string MinimizerType = "Minuit2";
17 // Minimizer algorithm (specific to the selected type).
18 // For Minuit2, the following algorithms are available:
19 // - "Migrad" : gradient-based minimization (default)
20 // - "Simplex" : Nelder-Mead simplex method (derivative-free)
21 // - "Combined" : combination of Simplex and Migrad
22 // - "Scan" : parameter grid scan
23 const std::string MinimizerAlgo = "Migrad";
24
25 MACH3LOG_INFO("Creating instance of Minimizer with {} and {}", MinimizerType, MinimizerAlgo);
26
27 minuit = std::unique_ptr<ROOT::Math::Minimizer>(
28 ROOT::Math::Factory::CreateMinimizer(MinimizerType.c_str(), MinimizerAlgo.c_str()));
29}
MaCh3Plotting::PlottingManager * man
#define MACH3LOG_INFO
Definition: MaCh3Logger.h:23
Implementation of base Likelihood Fit class, it is mostly responsible for likelihood calculation whil...
Definition: LikelihoodFit.h:6
std::unique_ptr< ROOT::Math::Minimizer > minuit
Pointer to minimizer, which most often is Minuit.
Definition: MinuitFit.h:29

◆ ~MinuitFit()

MinuitFit::~MinuitFit ( )
virtual

Destructor.

Definition at line 33 of file MinuitFit.cpp.

33 {
34// *************************
35}

Member Function Documentation

◆ GetName()

std::string MinuitFit::GetName ( ) const
inlinevirtual

Get name of class.

Reimplemented from LikelihoodFit.

Definition at line 25 of file MinuitFit.h.

25{return "MinuitFit";};

◆ RunMCMC()

void MinuitFit::RunMCMC ( )
overridevirtual

Actual implementation of Minuit Fit algorithm.

Implements LikelihoodFit.

Definition at line 40 of file MinuitFit.cpp.

40 {
41// *******************
42 PrepareFit();
43
44 // Remove obsolete memory and make other checks before fit starts
46
47 //KS: For none PCA this will be equal to normal parameters
48 const int NparsMinuitFull = NPars;
49 const int NparsMinuit = NParsPCA;
50
51 //KS: Set SetFunction we will Minimize
52 ROOT::Math::Functor fChi2(this, &MinuitFit::CalcChi2, NparsMinuit);
53 minuit->SetFunction(fChi2);
54
55 //KS: add config or something
56 minuit->SetPrintLevel(2);
57 minuit->SetTolerance(0.01);
58 minuit->SetMaxFunctionCalls(fitMan->raw()["General"]["Minuit2"]["NSteps"].as<unsigned>());
59 minuit->SetMaxIterations(10000);
60
61 MACH3LOG_INFO("Preparing Minuit");
62 int ParCounter = 0;
63
64 for (std::vector<ParameterHandlerBase*>::iterator it = systematics.begin(); it != systematics.end(); ++it)
65 {
66 if(!(*it)->IsPCA())
67 {
68 for(int i = 0; i < (*it)->GetNumParams(); ++i, ++ParCounter)
69 {
70 //KS: Index, name, prior, step scale [different to MCMC],
71 minuit->SetVariable(ParCounter, ((*it)->GetParName(i)), (*it)->GetParInit(i), (*it)->GetDiagonalError(i)/10);
72 minuit->SetVariableValue(ParCounter, (*it)->GetParInit(i));
73 //KS: lower bound, upper bound, if Mirroring enabled then ignore
74 if(!fMirroring) minuit->SetVariableLimits(ParCounter, (*it)->GetLowerBound(i), (*it)->GetUpperBound(i));
75 if((*it)->IsParameterFixed(i))
76 {
77 minuit->FixVariable(ParCounter);
78 }
79 }
80 }
81 else
82 {
83 for(int i = 0; i < (*it)->GetNParameters(); ++i, ++ParCounter)
84 {
85 minuit->SetVariable(ParCounter, Form("%i_PCA", i), (*it)->GetPCAHandler()->GetParPropPCA(i), (*it)->GetPCAHandler()->GetEigenValuesMaster()[i]/10);
86 if((*it)->GetPCAHandler()->IsParameterFixedPCA(i))
87 {
88 minuit->FixVariable(ParCounter);
89 }
90 }
91 }
92 }
93
94 minuit->SetPrintLevel(2);
95
96 MACH3LOG_INFO("Starting MIGRAD");
97 minuit->Minimize();
98
99 MACH3LOG_INFO("Starting HESSE");
100 minuit->Hesse();
101 outputFile->cd();
102
103 TVectorD* MinuitParValue = new TVectorD(NparsMinuitFull);
104 TVectorD* MinuitParError = new TVectorD(NparsMinuitFull);
105 TMatrixDSym* Postmatrix = new TMatrixDSym(NparsMinuitFull);
106
107 for(int i = 0; i < NparsMinuitFull; ++i)
108 {
109 (*MinuitParValue)(i) = 0;
110 (*MinuitParError)(i) = 0;
111 for(int j = 0; j < NparsMinuitFull; ++j)
112 {
113 (*Postmatrix)(i,j) = 0;
114 (*Postmatrix)(i,j) = minuit->CovMatrix(i,j);
115 }
116 }
117
118 ParCounter = 0;
119 const double *X = minuit->X();
120 const double *err = minuit->Errors();
121 for (std::vector<ParameterHandlerBase*>::iterator it = systematics.begin(); it != systematics.end(); ++it)
122 {
123 if(!(*it)->IsPCA())
124 {
125 for(int i = 0; i < (*it)->GetNumParams(); ++i, ++ParCounter)
126 {
127 double ParVal = X[ParCounter];
128 //KS: Basically apply mirroring for parameters out of bounds
129 if(fMirroring)
130 {
131 if(ParVal < (*it)->GetLowerBound(i))
132 {
133 ParVal = (*it)->GetLowerBound(i) + ((*it)->GetLowerBound(i) - ParVal);
134 }
135 else if (ParVal > (*it)->GetUpperBound(i))
136 {
137 ParVal = (*it)->GetUpperBound(i) - ( ParVal - (*it)->GetUpperBound(i));
138 }
139 }
140 (*MinuitParValue)(ParCounter) = ParVal;
141 (*MinuitParError)(ParCounter) = err[ParCounter];
142 //KS: For fixed params HESS will not calculate error so we need to pass prior error
143 if((*it)->IsParameterFixed(i))
144 {
145 (*MinuitParError)(ParCounter) = (*it)->GetDiagonalError(i);
146 (*Postmatrix)(ParCounter,ParCounter) = (*MinuitParError)(ParCounter) * (*MinuitParError)(ParCounter);
147 }
148 }
149 }
150 else
151 {
152 //KS: We need to convert parameters from PCA to normal base
153 TVectorD ParVals((*it)->GetNumParams());
154 TVectorD ParVals_PCA((*it)->GetNParameters());
155
156 TVectorD ErrorVals((*it)->GetNumParams());
157 TVectorD ErrorVals_PCA((*it)->GetNParameters());
158
159 TMatrixD MatrixVals((*it)->GetNumParams(), (*it)->GetNumParams());
160 TMatrixD MatrixVals_PCA((*it)->GetNParameters(), (*it)->GetNParameters());
161
162 //First save them
163 //KS: This code is super convoluted as MaCh3 can store separate matrices while Minuit has one matrix. In future this will be simplified, keep it like this for now.
164 const int StartVal = ParCounter;
165 for(int i = 0; i < (*it)->GetNParameters(); ++i, ++ParCounter)
166 {
167 ParVals_PCA(i) = X[ParCounter];
168 ErrorVals_PCA(i) = err[ParCounter];
169 int ParCounterMatrix = StartVal;
170 for(int j = 0; j < (*it)->GetNParameters(); ++j, ++ParCounterMatrix)
171 {
172 MatrixVals_PCA(i,j) = minuit->CovMatrix(ParCounter,ParCounterMatrix);
173 }
174 }
175 ParVals = ((*it)->GetPCAHandler()->GetTransferMatrix())*ParVals_PCA;
176 ErrorVals = ((*it)->GetPCAHandler()->GetTransferMatrix())*ErrorVals_PCA;
177 MatrixVals.Mult(((*it)->GetPCAHandler()->GetTransferMatrix()),MatrixVals_PCA);
178
179 ParCounter = StartVal;
180 //KS: Now after going from PCA to normal let';s save it
181 for(int i = 0; i < (*it)->GetNumParams(); ++i, ++ParCounter)
182 {
183 (*MinuitParValue)(ParCounter) = ParVals(i);
184 (*MinuitParError)(ParCounter) = std::fabs(ErrorVals(i));
185 int ParCounterMatrix = StartVal;
186 for(int j = 0; j < (*it)->GetNumParams(); ++j, ++ParCounterMatrix)
187 {
188 (*Postmatrix)(ParCounter,ParCounterMatrix) = MatrixVals(i,j);
189 }
190 //If fixed take prior
191 if((*it)->GetPCAHandler()->IsParameterFixedPCA(i))
192 {
193 (*MinuitParError)(ParCounter) = (*it)->GetDiagonalError(i);
194 (*Postmatrix)(ParCounter,ParCounter) = (*MinuitParError)(ParCounter) * (*MinuitParError)(ParCounter);
195 }
196 }
197 }
198 }
199
200 MinuitParValue->Write("MinuitParValue");
201 MinuitParError->Write("MinuitParError");
202 Postmatrix->Write("Postmatrix");
203 delete MinuitParValue;
204 delete MinuitParError;
205 delete Postmatrix;
206 // Save all the output
207 SaveOutput();
208}
void SaveOutput()
Save output and close files.
Definition: FitterBase.cpp:221
TFile * outputFile
Output.
Definition: FitterBase.h:131
manager * fitMan
The manager.
Definition: FitterBase.h:92
void SanitiseInputs()
Remove obsolete memory and make other checks before fit starts.
Definition: FitterBase.cpp:213
std::vector< ParameterHandlerBase * > systematics
Systematic holder.
Definition: FitterBase.h:118
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
void PrepareFit()
prepare output and perform sanity checks
bool fMirroring
Flag telling if mirroring is used or not.
Definition: LikelihoodFit.h:32
virtual double CalcChi2(const double *x)
Chi2 calculation over all included samples and syst objects.
YAML::Node const & raw()
Return config.
Definition: Manager.h:41

Member Data Documentation

◆ minuit

std::unique_ptr<ROOT::Math::Minimizer> MinuitFit::minuit
private

Pointer to minimizer, which most often is Minuit.

Definition at line 29 of file MinuitFit.h.


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