MaCh3 2.2.1
Reference Guide
Loading...
Searching...
No Matches
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
31 public:
33 PCAHandler();
34
36 virtual ~PCAHandler();
37
39 void Print();
41 int GetNumberPCAedParameters() const { return NumParPCA; }
42
46 void SetupPointers(std::vector<double>* fCurr_Val,
47 std::vector<double>* fProp_Val);
48
55 void ConstructPCA(TMatrixDSym * CovMatrix, const int firstPCAd, const int lastPCAd,
56 const double eigen_thresh, const int _fNumPar);
57
59 void TransferToPCA();
61 void TransferToParam();
62
64 void ThrowParameters(const std::vector<std::unique_ptr<TRandom3>>& random_number,
65 double** throwMatrixCholDecomp,
66 double* randParams,
67 double* corr_throw,
68 const std::vector<double>& fPreFitValue,
69 const std::vector<double>& fLowBound,
70 const std::vector<double>& fUpBound,
71 int _fNumPar);
72
76 void CorrelateSteps(const std::vector<double>& IndivStepScale,
77 const double GlobalStepScale,
78 const double* _restrict_ randParams,
79 const double* _restrict_ corr_throw) _noexcept_;
80
83 void SetInitialParameters(std::vector<double>& IndStepScale);
85 void ThrowParProp(const double mag, const double* _restrict_ randParams);
87 void ThrowParCurr(const double mag, const double* _restrict_ randParams);
90 void SetBranches(TTree &tree, bool SaveProposal, const std::vector<std::string>& Names);
96 void ToggleFixParameter(const int i, const std::vector<std::string>& Names);
97
101 void SetParametersPCA(const std::vector<double> &pars) {
102 if (int(pars.size()) != NumParPCA) {
103 MACH3LOG_ERROR("Parameter arrays of incompatible size! Not changing parameters! has size {} but was expecting {}", pars.size(), NumParPCA);
104 throw MaCh3Exception(__FILE__ , __LINE__ );
105 }
106 int parsSize = int(pars.size());
107 for (int i = 0; i < parsSize; i++) {
108 _fParPropPCA(i) = pars[i];
109 }
110 //KS: Transfer to normal base
112 }
113
117 bool IsParameterFixedPCA(const int i) const {
118 if (_fErrorPCA[i] < 0) { return true; }
119 else { return false; }
120 }
121
124 const TMatrixD GetEigenVectors() const {
125 return eigen_vectors;
126 }
127
132 void SetParPropPCA(const int i, const double value) {
133 _fParPropPCA(i) = value;
134 // And then transfer back to the parameter basis
136 }
141 void SetParCurrPCA(const int i, const double value) {
142 _fParCurrPCA(i) = value;
143 // And then transfer back to the parameter basis
145 }
146
150 double GetParPropPCA(const int i) const {
151 return _fParPropPCA(i);
152 }
153
157 double GetPreFitValuePCA(const int i) const {
158 return _fPreFitValuePCA[i];
159 }
160
164 double GetParCurrPCA(const int i) const {
165 return _fParCurrPCA(i);
166 }
167
170 const TMatrixD GetTransferMatrix() const {
171 return TransferMat;
172 }
173
176 const TVectorD GetEigenValues() const {
177 return eigen_values;
178 }
181 const std::vector<double> GetEigenValuesMaster() const {
182 return eigen_values_master;
183 }
184
188 bool IsParameterDecomposed(const int i) const {
189 if(isDecomposedPCA[i] >= 0) return false;
190 else return true;
191 }
192
193 #ifdef DEBUG_PCA
195 void DebugPCA(const double sum, TMatrixD temp, TMatrixDSym submat, int NumPar);
196 #endif
197
198 private:
200 void SanitisePCA(TMatrixDSym* CovMatrix);
201
203 std::vector<double> _fPreFitValuePCA;
205 TVectorD _fParPropPCA;
207 TVectorD _fParCurrPCA;
209 std::vector<double> _fErrorPCA;
211 std::vector<int> isDecomposedPCA;
214
216 TMatrixD TransferMat;
218 TMatrixD TransferMatT;
220 TVectorD eigen_values;
224 std::vector<double> eigen_values_master;
225
234
236 std::vector<double>* _pCurrVal;
238 std::vector<double>* _pPropVal;
239};
240
#define _noexcept_
KS: noexcept can help with performance but is terrible for debugging, this is meant to help easy way ...
Definition: Core.h:83
#define _restrict_
KS: Using restrict limits the effects of pointer aliasing, aiding optimizations. While reading I foun...
Definition: Core.h:90
#define MACH3LOG_ERROR
Definition: MaCh3Logger.h:25
Custom exception class for MaCh3 errors.
Class responsible for handling Principal Component Analysis (PCA) of covariance matrix.
Definition: PCAHandler.h:30
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:211
TMatrixD TransferMat
Matrix used to converting from PCA base to normal base.
Definition: PCAHandler.h:216
std::vector< double > eigen_values_master
Eigen values which have dimension equal to _fNumParPCA, and can be used in CorrelateSteps.
Definition: PCAHandler.h:224
TMatrixD eigen_vectors
Eigen vectors only of params which are being decomposed.
Definition: PCAHandler.h:222
TVectorD _fParPropPCA
CW: Current parameter value in PCA base.
Definition: PCAHandler.h:205
void ThrowParProp(const double mag, const double *_restrict_ randParams)
Throw the proposed parameter by mag sigma.
Definition: PCAHandler.cpp:280
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:352
int NumParPCA
Number of parameters in PCA base.
Definition: PCAHandler.h:213
int GetNumberPCAedParameters() const
Retrieve number of parameters in PCA base.
Definition: PCAHandler.h:41
std::vector< double > * _pCurrVal
Pointer to current value of the parameter.
Definition: PCAHandler.h:236
TMatrixD TransferMatT
Matrix used to converting from normal base to PCA base.
Definition: PCAHandler.h:218
void AcceptStep() _noexcept_
Accepted this step.
Definition: PCAHandler.cpp:183
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:198
void SetInitialParameters(std::vector< double > &IndStepScale)
KS: Transfer the starting parameters to the PCA basis, you don't want to start with zero....
Definition: PCAHandler.cpp:249
void TransferToPCA()
Transfer param values from normal base to PCA base.
Definition: PCAHandler.cpp:234
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:18
std::vector< double > * _pPropVal
Pointer to proposed value of the parameter.
Definition: PCAHandler.h:238
TVectorD eigen_values
Eigen value only of particles which are being decomposed.
Definition: PCAHandler.h:220
void TransferToParam()
Transfer param values from PCA base to normal base.
Definition: PCAHandler.cpp:264
int LastPCAdpar
Index of the last param that is being decomposed.
Definition: PCAHandler.h:231
int nKeptPCApars
Total number that remained after applying PCA Threshold.
Definition: PCAHandler.h:227
std::vector< double > _fErrorPCA
Tells if parameter is fixed in PCA base or not.
Definition: PCAHandler.h:209
PCAHandler()
Constructor.
Definition: PCAHandler.cpp:5
void Print()
KS: Print info about PCA parameters.
Definition: PCAHandler.cpp:303
std::vector< double > _fPreFitValuePCA
Prefit value for PCA params.
Definition: PCAHandler.h:203
void ThrowParCurr(const double mag, const double *_restrict_ randParams)
Helper function to throw the current parameter by mag sigma.
Definition: PCAHandler.cpp:292
TVectorD _fParCurrPCA
CW: Proposed parameter value in PCA base.
Definition: PCAHandler.h:207
void SanitisePCA(TMatrixDSym *CovMatrix)
@biref KS: Make sure decomposed matrix isn't correlated with undecomposed
Definition: PCAHandler.cpp:154
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:26
int FirstPCAdpar
Index of the first param that is being decomposed.
Definition: PCAHandler.h:229
double eigen_threshold
CW: Threshold based on which we remove parameters in eigen base.
Definition: PCAHandler.h:233
const TMatrixD GetTransferMatrix() const
Get transfer matrix allowing to go from PCA base to normal base.
Definition: PCAHandler.h:170
double GetParCurrPCA(const int i) const
Get current parameter value using PCA.
Definition: PCAHandler.h:164
const std::vector< double > GetEigenValuesMaster() const
Get eigen value of only decomposed parameters, if you want for all parameters use GetEigenValues.
Definition: PCAHandler.h:181
bool IsParameterFixedPCA(const int i) const
Is parameter fixed in PCA base or not.
Definition: PCAHandler.h:117
double GetParPropPCA(const int i) const
Get current parameter value using PCA.
Definition: PCAHandler.h:150
double GetPreFitValuePCA(const int i) const
Get current parameter value using PCA.
Definition: PCAHandler.h:157
bool IsParameterDecomposed(const int i) const
Check if parameter in PCA base is decomposed or not.
Definition: PCAHandler.h:188
const TVectorD GetEigenValues() const
Get eigen values for all parameters, if you want for decomposed only parameters use GetEigenValuesMas...
Definition: PCAHandler.h:176
const TMatrixD GetEigenVectors() const
Get eigen vectors of covariance matrix, only works with PCA.
Definition: PCAHandler.h:124
void ToggleFixAllParameters()
fix parameters at prior values
Definition: PCAHandler.cpp:327
void SetBranches(TTree &tree, bool SaveProposal, const std::vector< std::string > &Names)
set branches for output file
Definition: PCAHandler.cpp:312
void SetParPropPCA(const int i, const double value)
Set proposed value for parameter in PCA base.
Definition: PCAHandler.h:132
void ToggleFixParameter(const int i, const std::vector< std::string > &Names)
fix parameters at prior values
Definition: PCAHandler.cpp:335
void SetParCurrPCA(const int i, const double value)
Set current value for parameter in PCA base.
Definition: PCAHandler.h:141
void SetParametersPCA(const std::vector< double > &pars)
Set values for PCA parameters in PCA base.
Definition: PCAHandler.h:101