MaCh3  2.2.3
Reference Guide
Public Member Functions | Private Attributes | List of all members
MinuitFit Class Reference

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

#include <Fitters/MinuitFit.h>

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

Public Member Functions

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

Private Attributes

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

Additional Inherited Members

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

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.

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

◆ ~MinuitFit()

MinuitFit::~MinuitFit ( )
virtual

Destructor.

Definition at line 34 of file MinuitFit.cpp.

34  {
35 // *************************
36 }

Member Function Documentation

◆ RunMCMC()

void MinuitFit::RunMCMC ( )
overridevirtual

Actual implementation of Minuit Fit algorithm.

Implements FitterBase.

Definition at line 41 of file MinuitFit.cpp.

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


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