MaCh3  2.8.0
Reference Guide
ParameterHandlerBase.h
Go to the documentation of this file.
1 #pragma once
2 
3 // MaCh3 includes
4 #include "Manager/Manager.h"
9 #include "Manager/TablePrinter.h"
11 
28  public:
32  ParameterHandlerBase(std::string name, std::string file, double threshold = -1, int FirstPCAdpar = -999, int LastPCAdpar = -999);
33  ParameterHandlerBase() = default;
34 
36  virtual ~ParameterHandlerBase();
37 
38  // ETA - maybe need to add checks to index on the setters? i.e. if( i > _fPropVal.size()){throw;}
41  void SetCovMatrix(TMatrixDSym *cov);
43  void SetName(const std::string& name) { matrixName = name; }
47  void SetParName(const int i, const std::string& name) { _fNames.at(i) = name; }
49  void SetSingleParameter(const int parNo, const double parVal);
53  void SetPar(const int i, const double val);
57  void SetParCurrProp(const int i, const double val);
61  void SetParProp(const int i, const double val) {
62  _fPropVal[i] = static_cast<M3::float_t>(val);
63  if (pca) PCAObj->TransferToPCA();
64  }
67  void SetParameters(const std::vector<double>& pars = {});
71  void SetFlatPrior(const int i, const bool eL);
72 
76  void SetRandomThrow(const int i, const double rand) { randParams[i] = rand;}
79  double GetRandomThrow(const int i) const { return randParams[i];}
80 
84  void SetBranches(TTree &tree, const bool SaveProposal = false);
89  void SetStepScale(const double scale, const bool verbose=true);
93  void SetIndivStepScale(const int ParameterIndex, const double StepScale){ _fIndivStepScale.at(ParameterIndex) = StepScale; }
96  void SetIndivStepScale(const std::vector<double>& stepscale);
97 
100 
102  void ThrowParameters();
104  void RandomConfiguration();
105 
107  int CheckBounds() const _noexcept_;
121  double CalcLikelihood() const _noexcept_;
123  virtual double GetLikelihood();
124 
126  TMatrixDSym *GetCovMatrix() const { return covMatrix; }
128  TMatrixDSym *GetInvCovMatrix() const { return invCovMatrix; }
130  double GetInvCovMatrix(const int i, const int j) const { return InvertCovMatrix[i][j]; }
131 
134  double GetCorrThrows(const int i) const { return corr_throw[i]; }
135 
138  bool GetFlatPrior(const int i) const { return _fFlatPrior[i]; }
139 
141  std::string GetName() const { return matrixName; }
144  std::string GetParName(const int i) const {return _fNames[i];}
145 
147  int GetParIndex(const std::string& name) const;
148 
151  std::string GetParFancyName(const int i) const {return _fFancyNames[i];}
152 
155  double GetDiagonalError(const int i) const { return std::sqrt((*covMatrix)(i,i)); }
158  double GetError(const int i) const {return _fError[i];}
159 
161  void ResetIndivStepScale();
162 
165 
168  void InitialiseAdaption(const YAML::Node& adapt_manager);
170  void SaveAdaptiveToFile(const std::string& outFileName, const std::string& systematicName) {
171  AdaptiveHandler->SaveAdaptiveToFile(outFileName, systematicName); }
172 
174  bool GetDoAdaption() const {return use_adaptive;}
176  void SetThrowMatrix(const TMatrixDSym *cov);
177  void SetSubThrowMatrix(int first_index, int last_index, TMatrixDSym const &subcov);
179  void UpdateThrowMatrix(TMatrixDSym *cov);
181  void SetNumberOfSteps(const int nsteps) {
182  AdaptiveHandler->SetTotalSteps(nsteps);
183  if(AdaptiveHandler->AdaptionUpdate()) ResetIndivStepScale();
184  }
185 
187  TMatrixDSym *GetThrowMatrix() const {return throwMatrix;}
189  double GetThrowMatrix(const int i, const int j) const { return throwMatrixCholDecomp[i][j];}
190 
195  TH2D* GetCorrelationMatrix() const;
196 
206  const M3::float_t* RetPointer(const int iParam) const {return &(_fPropVal.data()[iParam]);}
207 
210  const std::vector<M3::float_t> &GetParPropVec() {return _fPropVal;}
211 
213  int GetNumParams() const {return _fNumPar;}
215  std::vector<double> GetPreFitValues() const {return _fPreFitValue;}
217  std::vector<double> GetProposed() const;
220  M3::float_t GetParProp(const int i) const { return _fPropVal[i]; }
223  double GetParCurr(const int i) const { return _fCurrVal[i]; }
225  const std::vector<double> &GetParCurrVec() const { return _fCurrVal; }
226 
229  double GetParPreFit(const int i) const { return _fPreFitValue[i]; }
232  double GetUpperBound(const int i) const { return _fUpBound[i];}
235  double GetLowerBound(const int i) const { return _fLowBound[i]; }
238  double GetIndivStepScale(const int ParameterIndex) const {return _fIndivStepScale.at(ParameterIndex); }
240  double GetGlobalStepScale() const {return _fGlobalStepScale; }
241 
243  int GetNParameters() const {
244  if (pca) return PCAObj->GetNumberPCAedParameters();
245  else return _fNumPar;
246  }
247 
249  void PrintPreFitValues() const;
251  void PrintPreFitCurrPropValues() const;
253  void PrintIndivStepScale() const;
254 
256  virtual void ProposeStep();
258  void Randomize() _noexcept_;
260  void CorrelateSteps() _noexcept_;
264 
266  void AcceptStep() _noexcept_;
267 
269  void SetFixAllParameters();
272  void SetFixParameter(const int i);
275  void SetFixParameter(const std::string& name);
276 
278  void SetFreeAllParameters();
281  void SetFreeParameter(const int i);
284  void SetFreeParameter(const std::string& name);
285 
288  bool IsParameterFixed(const int i) const {
289  if (_fError[i] < 0) { return true; }
290  else { return false; }
291  }
294  bool IsParameterFixed(const std::string& name) const;
295 
301  void ConstructPCA(const double eigen_threshold, int FirstPCAdpar, int LastPCAdpar);
302 
304  bool IsPCA() const { return pca; }
305 
307  YAML::Node GetConfig() const { return _fYAMLDoc; }
308 
311  if (!use_adaptive) {
312  MACH3LOG_ERROR("Am not running in Adaptive mode");
313  throw MaCh3Exception(__FILE__ , __LINE__ );
314  }
315  return AdaptiveHandler.get();
316  }
317 
319  void SetTune(const std::string& TuneName);
320 
323  if (!pca) {
324  MACH3LOG_ERROR("Am not running in PCA mode");
325  throw MaCh3Exception(__FILE__ , __LINE__ );
326  }
327  return PCAObj.get();
328  }
329 
341  void MatchMaCh3OutputBranches(TTree *PosteriorFile,
342  std::vector<double>& BranchValues,
343  std::vector<std::string>& BranchNames,
344  const std::vector<std::string>& FancyNames = {});
345  protected:
347  void InitFromFile(const std::string& name, const std::string& file);
350  void ReserveMemory(const int size);
351 
355  void MakePosDef(TMatrixDSym *cov = nullptr, bool verbose = true);
356 
358  void MakeClosestPosDef(TMatrixDSym *cov);
359 
364  void SetThrowMatrixFromFile(const std::string& matrix_file_name, const std::string& matrix_name, const std::string& means_name);
366  void SanitizeAdaption() const;
367 
370  void FlipParameterGroup(const std::string& group);
371 
374  M3::float_t EvaluateFunctionalFlip(const FunctionalFlipProposal& flip, const std::vector<double>& proposed_values) const;
375 
378  void CircularParBounds(const int i, const double LowBound, const double UpBound);
379 
381  void EnableSpecialProposal(const YAML::Node& param, const int Index);
382 
384  void AddFunctionalFlip(const YAML::Node& param, const int index, const std::string& group_name);
385 
387  void QueueFunctionalFlip(const YAML::Node& param, const int index, const std::string& group_name);
388 
390  void ResolveFunctionalFlips();
391 
394  void SpecialStepProposal();
395 
398 
400  std::string inputFile;
401 
403  std::string matrixName;
405  TMatrixDSym *covMatrix;
407  TMatrixDSym *invCovMatrix;
409  std::vector<std::vector<double>> InvertCovMatrix;
410 
412  double* randParams;
414  double* corr_throw;
417 
419  std::vector<std::string> _fNames;
421  std::vector<std::string> _fFancyNames;
423  YAML::Node _fYAMLDoc;
425  int _fNumPar;
427  std::vector<double> _fPreFitValue;
429  std::vector<double> _fCurrVal;
431  std::vector<M3::float_t> _fPropVal;
433  std::vector<double> _fError;
435  std::vector<double> _fLowBound;
437  std::vector<double> _fUpBound;
439  std::vector<double> _fIndivStepScale;
441  std::vector<bool> _fFlatPrior;
442 
444  std::vector<double> _fIndivStepScaleInitial;
445 
448 
450  std::vector<bool> param_skip_adapt_flags;
451 
453  TMatrixDSym* throwMatrix;
456 
458  bool pca;
461 
463  std::unique_ptr<PCAHandler> PCAObj;
465  std::unique_ptr<AdaptiveMCMCHandler> AdaptiveHandler;
467  std::unique_ptr<ParameterTunes> Tunes;
468 
470  std::map<std::string, FlipGroup> FlipGroups;
472  std::vector<PendingFunctionalFlipProposal> PendingFunctionalFlipParameters;
474  std::vector<int> CircularBoundsIndex;
476  std::vector<std::pair<double,double>> CircularBoundsValues;
477 
478  private:
481  void ToggleFixParameter(const int i);
484  void ToggleFixParameter(const std::string& name);
485 };
#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 MACH3LOG_ERROR
Definition: MaCh3Logger.h:37
Contains information about adaptive covariance matrix .
Custom exception class used throughout MaCh3.
Class responsible for handling Principal Component Analysis (PCA) of covariance matrix.
Definition: PCAHandler.h:38
Base class for handling systematic uncertainty parameters.
std::vector< int > CircularBoundsIndex
Indices of parameters with circular bounds.
int GetNumParams() const
Get total number of parameters.
std::vector< bool > param_skip_adapt_flags
Flags telling if parameter should be skipped during adaption.
void ToggleFixParameter(const int i)
Toggle fixing parameter at prior values.
bool use_adaptive
Are we using AMCMC?
void SetFixAllParameters()
Set all parameters to be fixed at prior values.
virtual void ProposeStep()
Generate a new proposed state.
std::string inputFile
The input root file we read in.
TH2D * GetCorrelationMatrix() const
KS: Convert covariance matrix to correlation matrix and return TH2D which can be used for fancy plott...
void SanitizeAdaption() const
Perform sanity check to ensure adaption isn't misbehaving before fit starts.
double * randParams
Random number taken from gaussian around prior error used for corr_throw.
void SetName(const std::string &name)
Set matrix name.
std::map< std::string, FlipGroup > FlipGroups
Map of flip groups, where the key is the group name and the value is a FlipGroup struct.
void AddFunctionalFlip(const YAML::Node &param, const int index, const std::string &group_name)
Parse and register a functional flip defined in YAML.
double ** throwMatrixCholDecomp
Throw matrix that is being used in the fit, much faster as TMatrixDSym cache miss.
void SetStepScale(const double scale, const bool verbose=true)
Set global step scale for covariance object.
virtual ~ParameterHandlerBase()
Destructor.
void SetRandomThrow(const int i, const double rand)
Set random value useful for debugging/CI.
TMatrixDSym * invCovMatrix
The inverse covariance matrix.
void SetCovMatrix(TMatrixDSym *cov)
Set covariance matrix.
std::string GetParName(const int i) const
Get name of parameter.
double GetInvCovMatrix(const int i, const int j) const
Return inverted covariance matrix.
bool GetDoAdaption() const
Do we adapt or not.
std::vector< double > GetProposed() const
Get vector of all proposed parameter values.
void SetFlatPrior(const int i, const bool eL)
Set if parameter should have flat prior or not.
void SetThrowMatrixFromFile(const std::string &matrix_file_name, const std::string &matrix_name, const std::string &means_name)
sets throw matrix from a file
void ResolveFunctionalFlips()
Resolve queued functional flips after parameter names are known.
void SetParProp(const int i, const double val)
Set proposed parameter value.
void AcceptStep() _noexcept_
Accepted this step.
double GetUpperBound(const int i) const
Get upper parameter bound in which it is physically valid.
virtual double GetLikelihood()
Return CalcLikelihood if some params were thrown out of boundary return LARGE_LOGL
std::unique_ptr< AdaptiveMCMCHandler > AdaptiveHandler
Struct containing information about adaption.
void QueueFunctionalFlip(const YAML::Node &param, const int index, const std::string &group_name)
Queue a functional flip until all parameters have been loaded.
std::vector< M3::float_t > _fPropVal
Proposed value of the parameter.
std::unique_ptr< ParameterTunes > Tunes
Struct containing information about adaption.
void InitialiseAdaption(const YAML::Node &adapt_manager)
Initialise adaptive MCMC.
int GetNParameters() const
Get number of params which will be different depending if using Eigen decomposition or not.
TMatrixDSym * throwMatrix
Matrix which we use for step proposal before Cholesky decomposition (not actually used for step propo...
double _fGlobalStepScale
Global step scale applied to all params in this class.
void FlipParameterGroup(const std::string &group)
With a 50% chance, flip all parameters in a group around their respective flip points.
int GetParIndex(const std::string &name) const
Get index based on name.
void ReserveMemory(const int size)
Initialise vectors with parameters information.
M3::float_t EvaluateFunctionalFlip(const FunctionalFlipProposal &flip, const std::vector< double > &proposed_values) const
Evaluate a formula-driven flip for a target parameter.
std::vector< std::string > _fFancyNames
Fancy name for example rather than param_0 it is MAQE, useful for human reading.
bool doSpecialStepProposal
Check if any of special step proposal were enabled.
TMatrixDSym * GetInvCovMatrix() const
Return inverted covariance matrix.
std::vector< bool > _fFlatPrior
Whether to apply flat prior or not.
void ThrowParameters()
Throw the parameters according to the covariance matrix. This shouldn't be used in MCMC code ase it c...
double _fGlobalStepScaleInitial
Backup of _fGlobalStepScale for parameters which are skipped during adaption.
std::string matrixName
Name of cov matrix.
void MakeClosestPosDef(TMatrixDSym *cov)
HW: Finds closest possible positive definite matrix in Frobenius Norm ||.||_frob Where ||X||_frob=sqr...
void RandomConfiguration()
Randomly throw the parameters in their 1 sigma range.
void SaveAdaptiveToFile(const std::string &outFileName, const std::string &systematicName)
Save adaptive throw matrix to file.
TMatrixDSym * GetCovMatrix() const
Return covariance matrix.
double GetThrowMatrix(const int i, const int j) const
Get matrix used for step proposal.
const std::vector< M3::float_t > & GetParPropVec()
Get a reference to the proposed parameter values Can be useful if you want to track these without hav...
void UpdateAdaptiveCovariance()
Method to update adaptive MCMC .
std::string GetParFancyName(const int i) const
Get fancy name of the Parameter.
std::vector< double > _fError
Prior error on the parameter.
PCAHandler * GetPCAHandler() const
Get pointer for PCAHandler.
void SetFreeAllParameters()
Set all parameters to be treated as free.
void SetFixParameter(const int i)
Set parameter to be fixed at prior value.
void SetParameters(const std::vector< double > &pars={})
Set parameter values using vector, it has to have same size as covariance class.
const std::vector< double > & GetParCurrVec() const
Get vector of current parameter values.
YAML::Node _fYAMLDoc
Stores config describing systematics.
void SetThrowMatrix(const TMatrixDSym *cov)
Use new throw matrix, used in adaptive MCMC.
bool IsParameterFixed(const int i) const
Is parameter fixed or not.
double GetRandomThrow(const int i) const
Get random value useful for debugging/CI.
void MatchMaCh3OutputBranches(TTree *PosteriorFile, std::vector< double > &BranchValues, std::vector< std::string > &BranchNames, const std::vector< std::string > &FancyNames={})
Matches branches in a TTree to parameters in a systematic handler.
void UpdateThrowMatrix(TMatrixDSym *cov)
Replaces old throw matrix with new one.
void CircularParBounds(const int i, const double LowBound, const double UpBound)
HW :: This method is a tad hacky but modular arithmetic gives me a headache.
AdaptiveMCMCHandler * GetAdaptiveHandler() const
Get pointer for AdaptiveHandler.
void SetBranches(TTree &tree, const bool SaveProposal=false)
set branches for output file
void SaveUpdatedMatrixConfig()
KS: After step scale, prefit etc. value were modified save this modified config.
double GetLowerBound(const int i) const
Get lower parameter bound in which it is physically valid.
void InitFromFile(const std::string &name, const std::string &file)
Initialisation of the class using matrix from root file.
void ResetIndivStepScale()
Adaptive Step Tuning Stuff.
std::unique_ptr< PCAHandler > PCAObj
Struct containing information about PCA.
int _fNumPar
Number of systematic parameters.
double CalcLikelihood() const _noexcept_
Calc penalty term based on inverted covariance matrix.
void ConstructPCA(const double eigen_threshold, int FirstPCAdpar, int LastPCAdpar)
CW: Calculate eigen values, prepare transition matrices and remove param based on defined threshold.
std::vector< double > _fIndivStepScaleInitial
Backup of _fIndivStepScale for parameters which are skipped during adaption.
std::vector< double > _fLowBound
Lowest physical bound, parameter will not be able to go beyond it.
std::vector< PendingFunctionalFlipProposal > PendingFunctionalFlipParameters
Functional flips waiting for full parameter-name registration.
std::vector< double > _fCurrVal
Current value of the parameter.
void SetParName(const int i, const std::string &name)
change parameter name
const M3::float_t * RetPointer(const int iParam) const
DB Pointer return to param position.
std::vector< std::vector< double > > InvertCovMatrix
KS: Same as above but much faster as TMatrixDSym cache miss.
double GetGlobalStepScale() const
Get global step scale for covariance object.
TMatrixDSym * covMatrix
The covariance matrix.
std::vector< double > _fPreFitValue
Parameter value dictated by the prior model. Based on it penalty term is calculated.
void Randomize() _noexcept_
"Randomize" the parameters in the covariance class for the proposed step. Used the proposal kernel an...
void PrintIndivStepScale() const
Print step scale for each parameter.
std::vector< std::string > _fNames
ETA _fNames is set automatically in the covariance class to be something like param_i,...
std::vector< std::pair< double, double > > CircularBoundsValues
Circular bounds for each parameter (lower, upper)
void EnableSpecialProposal(const YAML::Node &param, const int Index)
Enable special proposal.
double * corr_throw
Result of multiplication of Cholesky matrix and randParams.
bool GetFlatPrior(const int i) const
Get if param has flat prior or not.
bool IsPCA() const
is PCA, can use to query e.g. LLH scans
std::vector< double > GetPreFitValues() const
Get the pre-fit values of the parameters.
double GetIndivStepScale(const int ParameterIndex) const
Get individual step scale for selected parameter.
double GetError(const int i) const
Get the error for the ith parameter.
std::string GetName() const
Get name of covariance.
double GetParPreFit(const int i) const
Get prior parameter value.
void SetNumberOfSteps(const int nsteps)
Set number of MCMC step, when running adaptive MCMC it is updated with given frequency....
void SetParCurrProp(const int i, const double val)
Set current parameter value.
M3::float_t GetParProp(const int i) const
Get proposed parameter value.
void SetFreeParameter(const int i)
Set parameter to be treated as free.
std::vector< double > _fUpBound
Upper physical bound, parameter will not be able to go beyond it.
void SetSingleParameter(const int parNo, const double parVal)
Set value of single param to a given value.
void SetPar(const int i, const double val)
Set all the covariance matrix parameters to a user-defined value.
void SetIndivStepScale(const int ParameterIndex, const double StepScale)
DB Function to set fIndivStepScale from a vector (Can be used from execs and inside covariance constr...
void SetIndivStepScaleForSkippedAdaptParams()
Set individual step scale for parameters which are skipped during adaption to initial values.
double GetDiagonalError(const int i) const
Get diagonal error for ith parameter.
ParameterHandlerBase()=default
void SetSubThrowMatrix(int first_index, int last_index, TMatrixDSym const &subcov)
void SetTune(const std::string &TuneName)
KS: Set proposed parameter values vector to be base on tune values, for example set proposed values t...
YAML::Node GetConfig() const
Getter to return a copy of the YAML node.
bool pca
perform PCA or not
std::vector< double > _fIndivStepScale
Individual step scale used by MCMC algorithm.
void PrintPreFitValues() const
Print prior value for every parameter.
void SpecialStepProposal()
Perform Special Step Proposal.
void PrintPreFitCurrPropValues() const
Print prior, current and proposed value for each parameter.
void CorrelateSteps() _noexcept_
Use Cholesky throw matrix for better step proposal.
double GetCorrThrows(const int i) const
Return correlated throws.
void MakePosDef(TMatrixDSym *cov=nullptr, bool verbose=true)
Make matrix positive definite by adding small values to diagonal, necessary for inverting matrix.
double GetParCurr(const int i) const
Get current parameter value.
int CheckBounds() const _noexcept_
Check if parameters were proposed outside physical boundary.
TMatrixDSym * GetThrowMatrix() const
Get matrix used for step proposal.
double float_t
Definition: Core.h:37
Configuration for a formula-based functional flip.