MaCh3  2.4.2
Reference Guide
PCAHandler.h
Go to the documentation of this file.
1 #pragma once
2 
3 // MaCh3 Includes
4 #include "Manager/Manager.h"
6 
7 #ifdef DEBUG
8  #define DEBUG_PCA 1
9 #endif
10 
11 #ifdef DEBUG_PCA
12 //KS: When debugging we produce some fancy plots, but we don't need it during normal work flow
13 #include "TCanvas.h"
14 #include "TROOT.h"
15 #include "TStyle.h"
16 #include "TColor.h"
17 #include "TLine.h"
18 #include "TText.h"
19 #include "TLegend.h"
20 
21 #if DEBUG_PCA == 2
22 #include "Eigen/Eigenvalues"
23 #endif
24 
25 #endif
26 
58 class PCAHandler{
59  public:
61  PCAHandler();
62 
64  virtual ~PCAHandler();
65 
67  void Print() const;
69  int GetNumberPCAedParameters() const { return NumParPCA; }
70 
74  void SetupPointers(std::vector<double>* fCurr_Val,
75  std::vector<double>* fProp_Val);
76 
83  void ConstructPCA(TMatrixDSym * CovMatrix, const int firstPCAd, const int lastPCAd,
84  const double eigen_thresh, const int _fNumPar);
85 
87  void TransferToPCA();
89  void TransferToParam();
90 
92  void ThrowParameters(const std::vector<std::unique_ptr<TRandom3>>& random_number,
93  double** throwMatrixCholDecomp,
94  double* randParams,
95  double* corr_throw,
96  const std::vector<double>& fPreFitValue,
97  const std::vector<double>& fLowBound,
98  const std::vector<double>& fUpBound,
99  int _fNumPar);
100 
102  void AcceptStep() _noexcept_;
104  void CorrelateSteps(const std::vector<double>& IndivStepScale,
105  const double GlobalStepScale,
106  const double* _restrict_ randParams,
107  const double* _restrict_ corr_throw) _noexcept_;
108 
111  void SetInitialParameters();
114  void SetBranches(TTree &tree, const bool SaveProposal, const std::vector<std::string>& Names);
117  void ToggleFixAllParameters(const std::vector<std::string>& Names);
120  void ToggleFixParameter(const int i, const std::vector<std::string>& Names);
121 
125  void SetParametersPCA(const std::vector<double> &pars) {
126  if (int(pars.size()) != NumParPCA) {
127  MACH3LOG_ERROR("Parameter arrays of incompatible size! Not changing parameters! has size {} but was expecting {}", pars.size(), NumParPCA);
128  throw MaCh3Exception(__FILE__ , __LINE__ );
129  }
130  int parsSize = int(pars.size());
131  for (int i = 0; i < parsSize; i++) {
132  _fParPropPCA(i) = pars[i];
133  }
134  //KS: Transfer to normal base
135  TransferToParam();
136  }
137 
141  bool IsParameterFixedPCA(const int i) const {
142  if (_fErrorPCA[i] < 0) { return true; }
143  else { return false; }
144  }
145 
148  const TMatrixD GetEigenVectors() const {
149  return eigen_vectors;
150  }
151 
156  void SetParPropPCA(const int i, const double value) {
157  _fParPropPCA(i) = value;
158  // And then transfer back to the parameter basis
159  TransferToParam();
160  }
165  void SetParCurrPCA(const int i, const double value) {
166  _fParCurrPCA(i) = value;
167  // And then transfer back to the parameter basis
168  TransferToParam();
169  }
170 
174  double GetParPropPCA(const int i) const {
175  return _fParPropPCA(i);
176  }
177 
181  double GetPreFitValuePCA(const int i) const {
182  return _fPreFitValuePCA[i];
183  }
184 
188  double GetParCurrPCA(const int i) const {
189  return _fParCurrPCA(i);
190  }
191 
194  const TMatrixD GetTransferMatrix() const {
195  return TransferMat;
196  }
197 
200  const TVectorD GetEigenValues() const {
201  return eigen_values;
202  }
205  const std::vector<double> GetEigenValuesMaster() const {
206  return eigen_values_master;
207  }
208 
212  bool IsParameterDecomposed(const int i) const {
213  if(isDecomposedPCA[i] >= 0) return false;
214  else return true;
215  }
216 
217  #ifdef DEBUG_PCA
219  void DebugPCA(const double sum, TMatrixD temp, TMatrixDSym submat, int NumPar);
220  #endif
221 
222  private:
224  void SanitisePCA(TMatrixDSym* CovMatrix);
225 
227  std::vector<double> _fPreFitValuePCA;
229  TVectorD _fParPropPCA;
231  TVectorD _fParCurrPCA;
233  std::vector<double> _fErrorPCA;
235  std::vector<int> isDecomposedPCA;
238 
240  TMatrixD TransferMat;
242  TMatrixD TransferMatT;
244  TVectorD eigen_values;
246  TMatrixD eigen_vectors;
248  std::vector<double> eigen_values_master;
249 
258 
260  std::vector<double>* _pCurrVal;
262  std::vector<double>* _pPropVal;
263 };
264 
#define _noexcept_
KS: noexcept can help with performance but is terrible for debugging, this is meant to help easy way ...
Definition: Core.h:96
#define _restrict_
KS: Using restrict limits the effects of pointer aliasing, aiding optimizations. While reading I foun...
Definition: Core.h:108
#define MACH3LOG_ERROR
Definition: MaCh3Logger.h:37
Custom exception class used throughout MaCh3.
Class responsible for handling Principal Component Analysis (PCA) of covariance matrix.
Definition: PCAHandler.h:58
std::vector< int > isDecomposedPCA
If param is decomposed this will return -1, if not this will return enumerator to param in normal bas...
Definition: PCAHandler.h:235
void ThrowParameters(const std::vector< std::unique_ptr< TRandom3 >> &random_number, double **throwMatrixCholDecomp, double *randParams, double *corr_throw, const std::vector< double > &fPreFitValue, const std::vector< double > &fLowBound, const std::vector< double > &fUpBound, int _fNumPar)
Throw the parameters according to the covariance matrix. This shouldn't be used in MCMC code ase it c...
Definition: PCAHandler.cpp:318
void ToggleFixAllParameters(const std::vector< std::string > &Names)
fix parameters at prior values
Definition: PCAHandler.cpp:294
TMatrixD TransferMat
Matrix used to converting from PCA base to normal base.
Definition: PCAHandler.h:240
const TMatrixD GetTransferMatrix() const
Get transfer matrix allowing to go from PCA base to normal base.
Definition: PCAHandler.h:194
const std::vector< double > GetEigenValuesMaster() const
Get eigen value of only decomposed parameters, if you want for all parameters use GetEigenValues.
Definition: PCAHandler.h:205
double GetParCurrPCA(const int i) const
Get current parameter value using PCA.
Definition: PCAHandler.h:188
std::vector< double > eigen_values_master
Eigen values which have dimension equal to _fNumParPCA, and can be used in CorrelateSteps.
Definition: PCAHandler.h:248
void SetBranches(TTree &tree, const bool SaveProposal, const std::vector< std::string > &Names)
set branches for output file
Definition: PCAHandler.cpp:279
TMatrixD eigen_vectors
Eigen vectors only of params which are being decomposed.
Definition: PCAHandler.h:246
TVectorD _fParPropPCA
CW: Current parameter value in PCA base.
Definition: PCAHandler.h:229
bool IsParameterFixedPCA(const int i) const
Is parameter fixed in PCA base or not.
Definition: PCAHandler.h:141
double GetParPropPCA(const int i) const
Get current parameter value using PCA.
Definition: PCAHandler.h:174
int NumParPCA
Number of parameters in PCA base.
Definition: PCAHandler.h:237
void SetInitialParameters()
KS: Transfer the starting parameters to the PCA basis, you don't want to start with zero....
Definition: PCAHandler.cpp:245
int GetNumberPCAedParameters() const
Retrieve number of parameters in PCA base.
Definition: PCAHandler.h:69
std::vector< double > * _pCurrVal
Pointer to current value of the parameter.
Definition: PCAHandler.h:260
TMatrixD TransferMatT
Matrix used to converting from normal base to PCA base.
Definition: PCAHandler.h:242
void AcceptStep() _noexcept_
Accepted this step.
Definition: PCAHandler.cpp:179
void CorrelateSteps(const std::vector< double > &IndivStepScale, const double GlobalStepScale, const double *_restrict_ randParams, const double *_restrict_ corr_throw) _noexcept_
Use Cholesky throw matrix for better step proposal.
Definition: PCAHandler.cpp:194
void SetParPropPCA(const int i, const double value)
Set proposed value for parameter in PCA base.
Definition: PCAHandler.h:156
void TransferToPCA()
Transfer param values from normal base to PCA base.
Definition: PCAHandler.cpp:230
virtual ~PCAHandler()
Destructor.
Definition: PCAHandler.cpp:12
void SetupPointers(std::vector< double > *fCurr_Val, std::vector< double > *fProp_Val)
KS: Setup pointers to current and proposed parameter value which we need to convert them to PCA base ...
Definition: PCAHandler.cpp:17
double GetPreFitValuePCA(const int i) const
Get current parameter value using PCA.
Definition: PCAHandler.h:181
std::vector< double > * _pPropVal
Pointer to proposed value of the parameter.
Definition: PCAHandler.h:262
bool IsParameterDecomposed(const int i) const
Check if parameter in PCA base is decomposed or not.
Definition: PCAHandler.h:212
TVectorD eigen_values
Eigen value only of particles which are being decomposed.
Definition: PCAHandler.h:244
void TransferToParam()
Transfer param values from PCA base to normal base.
Definition: PCAHandler.cpp:255
int LastPCAdpar
Index of the last param that is being decomposed.
Definition: PCAHandler.h:255
int nKeptPCApars
Total number that remained after applying PCA Threshold.
Definition: PCAHandler.h:251
const TVectorD GetEigenValues() const
Get eigen values for all parameters, if you want for decomposed only parameters use GetEigenValuesMas...
Definition: PCAHandler.h:200
std::vector< double > _fErrorPCA
Tells if parameter is fixed in PCA base or not.
Definition: PCAHandler.h:233
PCAHandler()
Constructor.
Definition: PCAHandler.cpp:5
std::vector< double > _fPreFitValuePCA
Prefit value for PCA params.
Definition: PCAHandler.h:227
void ToggleFixParameter(const int i, const std::vector< std::string > &Names)
fix parameters at prior values
Definition: PCAHandler.cpp:300
void SetParCurrPCA(const int i, const double value)
Set current value for parameter in PCA base.
Definition: PCAHandler.h:165
void Print() const
KS: Print info about PCA parameters.
Definition: PCAHandler.cpp:270
TVectorD _fParCurrPCA
CW: Proposed parameter value in PCA base.
Definition: PCAHandler.h:231
void SanitisePCA(TMatrixDSym *CovMatrix)
KS: Make sure decomposed matrix isn't correlated with undecomposed.
Definition: PCAHandler.cpp:150
void SetParametersPCA(const std::vector< double > &pars)
Set values for PCA parameters in PCA base.
Definition: PCAHandler.h:125
void ConstructPCA(TMatrixDSym *CovMatrix, const int firstPCAd, const int lastPCAd, const double eigen_thresh, const int _fNumPar)
CW: Calculate eigen values, prepare transition matrices and remove param based on defined threshold.
Definition: PCAHandler.cpp:25
int FirstPCAdpar
Index of the first param that is being decomposed.
Definition: PCAHandler.h:253
const TMatrixD GetEigenVectors() const
Get eigen vectors of covariance matrix, only works with PCA.
Definition: PCAHandler.h:148
double eigen_threshold
CW: Threshold based on which we remove parameters in eigen base.
Definition: PCAHandler.h:257