MaCh3  2.6.0
Reference Guide
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
ParameterHandlerGeneric Class Reference

Class responsible for handling of systematic error parameters with different types defined in the config. Like spline, normalisation parameters etc. More...

#include <Parameters/ParameterHandlerGeneric.h>

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

Public Member Functions

 ParameterHandlerGeneric (const std::vector< std::string > &FileNames, std::string name="xsec_cov", double threshold=-1, int FirstPCAdpar=-999, int LastPCAdpar=-999)
 Constructor. More...
 
 ~ParameterHandlerGeneric ()
 Destructor. More...
 
std::vector< std::string > GetParSampleID (const int i) const
 ETA - just return the int of the SampleName, this can be removed to do a string comp at some point. More...
 
std::string GetParamTypeString (const int i) const
 ETA - just return a string of "spline", "norm" or "functional". More...
 
SystType GetParamType (const int i) const
 Returns enum describing our param type. More...
 
SplineInterpolation GetParSplineInterpolation (const int i) const
 Get interpolation type for a given parameter. More...
 
const std::vector< SplineInterpolationGetSplineInterpolationFromSampleName (const std::string &SampleName) const
 Get the interpolation types for splines affecting a particular SampleName. More...
 
std::string GetParSplineName (const int i) const
 Get the name of the spline associated with the spline at index i. More...
 
const std::vector< int > GetGlobalSystIndexFromSampleName (const std::string &SampleName, const SystType Type) const
 DB Get spline parameters depending on given SampleName. More...
 
double GetParSplineKnotUpperBound (const int i) const
 EM: value at which we cap spline knot weight. More...
 
double GetParSplineKnotLowerBound (const int i) const
 EM: value at which we cap spline knot weight. More...
 
int GetNumParamsFromSampleName (const std::string &SampleName, const SystType Type) const
 DB Grab the number of parameters for the relevant SampleName. More...
 
const std::vector< std::string > GetParsNamesFromSampleName (const std::string &SampleName, const SystType Type) const
 DB Grab the parameter names for the relevant SampleName. More...
 
const std::vector< int > GetParsIndexFromSampleName (const std::string &SampleName, const SystType Type) const
 DB Grab the parameter indices for the relevant SampleName. More...
 
const std::vector< std::string > GetSplineParsNamesFromSampleName (const std::string &SampleName) const
 DB Get spline parameters depending on given SampleName. More...
 
const std::vector< std::string > GetSplineFileParsNamesFromSampleName (const std::string &SampleName) const
 DB Get spline parameters depending on given SampleName. More...
 
const std::vector< std::vector< int > > GetSplineModeVecFromSampleName (const std::string &SampleName) const
 DB Grab the Spline Modes for the relevant SampleName. More...
 
const std::vector< int > GetSystIndexFromSampleName (const std::string &SampleName, const SystType Type) const
 Grab the index of the syst relative to global numbering. More...
 
const std::vector< NormParameterGetNormParsFromSampleName (const std::string &SampleName) const
 DB Get norm/func parameters depending on given SampleName. More...
 
const std::vector< FunctionalParameterGetFunctionalParametersFromSampleName (const std::string &SampleName) const
 HH Get functional parameters for the relevant SampleName. More...
 
const std::vector< SplineParameterGetSplineParsFromSampleName (const std::string &SampleName) const
 KS: Grab the Spline parameters for the relevant SampleName. More...
 
bool IsParFromGroup (const int i, const std::string &Group) const
 Checks if parameter belongs to a given group. More...
 
int GetNumParFromGroup (const std::string &Group) const
 KS: Check how many parameters are associated with given group. More...
 
std::vector< std::string > GetUniqueParameterGroups () const
 KS: Get names of all unique parameter groups. More...
 
void SetGroupOnlyParameters (const std::string &Group, const std::vector< double > &Pars={})
 KS Function to set to prior parameters of a given group or values from vector. More...
 
void SetGroupOnlyParameters (const std::vector< std::string > &Groups)
 KS Function to set to prior parameters of a given groups or values from vector. More...
 
void SetFixGroupOnlyParameters (const std::string &Group)
 TN Method to set parameters within a group to be fixed to their prior values. More...
 
void SetFixGroupOnlyParameters (const std::vector< std::string > &Groups)
 TN Method to set parameters of certain groups to be fixed to their prior values. More...
 
void SetFreeGroupOnlyParameters (const std::string &Group)
 TN Method to set parameters within a group to be treated as free. More...
 
void SetFreeGroupOnlyParameters (const std::vector< std::string > &Groups)
 TN Method to set parameters of certain groups to be treated as free. More...
 
void DumpMatrixToFile (const std::string &Name)
 Dump Matrix to ROOT file, useful when we need to pass matrix info to another fitting group. More...
 
std::vector< const M3::float_t * > GetOscParsFromSampleName (const std::string &SampleName) const
 Get pointers to Osc params from Sample name. More...
 
- Public Member Functions inherited from ParameterHandlerBase
 ParameterHandlerBase (std::string name, std::string file, double threshold=-1, int FirstPCAdpar=-999, int LastPCAdpar=-999)
 "Usual" constructors from root file More...
 
 ParameterHandlerBase ()=default
 
virtual ~ParameterHandlerBase ()
 Destructor. More...
 
void SetCovMatrix (TMatrixDSym *cov)
 Set covariance matrix. More...
 
void SetName (const std::string &name)
 Set matrix name. More...
 
void SetParName (const int i, const std::string &name)
 change parameter name More...
 
void SetSingleParameter (const int parNo, const double parVal)
 Set value of single param to a given value. More...
 
void SetPar (const int i, const double val)
 Set all the covariance matrix parameters to a user-defined value. More...
 
void SetParCurrProp (const int i, const double val)
 Set current parameter value. More...
 
void SetParProp (const int i, const double val)
 Set proposed parameter value. More...
 
void SetParameters (const std::vector< double > &pars={})
 Set parameter values using vector, it has to have same size as covariance class. More...
 
void SetFlatPrior (const int i, const bool eL)
 Set if parameter should have flat prior or not. More...
 
void SetRandomThrow (const int i, const double rand)
 Set random value useful for debugging/CI. More...
 
double GetRandomThrow (const int i) const
 Get random value useful for debugging/CI. More...
 
void SetBranches (TTree &tree, const bool SaveProposal=false)
 set branches for output file More...
 
void SetStepScale (const double scale, const bool verbose=true)
 Set global step scale for covariance object. More...
 
void SetIndivStepScale (const int ParameterIndex, const double StepScale)
 DB Function to set fIndivStepScale from a vector (Can be used from execs and inside covariance constructors) More...
 
void SetIndivStepScale (const std::vector< double > &stepscale)
 DB Function to set fIndivStepScale from a vector (Can be used from execs and inside covariance constructors) More...
 
void SetPrintLength (const unsigned int PriLen)
 KS: In case someone really want to change this. More...
 
void SaveUpdatedMatrixConfig ()
 KS: After step scale, prefit etc. value were modified save this modified config. More...
 
void ThrowParameters ()
 Throw the parameters according to the covariance matrix. This shouldn't be used in MCMC code ase it can break Detailed Balance;. More...
 
void RandomConfiguration ()
 Randomly throw the parameters in their 1 sigma range. More...
 
int CheckBounds () const _noexcept_
 Check if parameters were proposed outside physical boundary. More...
 
double CalcLikelihood () const _noexcept_
 Calc penalty term based on inverted covariance matrix. More...
 
virtual double GetLikelihood ()
 Return CalcLikelihood if some params were thrown out of boundary return LARGE_LOGL More...
 
TMatrixDSym * GetCovMatrix () const
 Return covariance matrix. More...
 
TMatrixDSym * GetInvCovMatrix () const
 Return inverted covariance matrix. More...
 
double GetInvCovMatrix (const int i, const int j) const
 Return inverted covariance matrix. More...
 
double GetCorrThrows (const int i) const
 Return correlated throws. More...
 
bool GetFlatPrior (const int i) const
 Get if param has flat prior or not. More...
 
std::string GetName () const
 Get name of covariance. More...
 
std::string GetParName (const int i) const
 Get name of parameter. More...
 
int GetParIndex (const std::string &name) const
 Get index based on name. More...
 
std::string GetParFancyName (const int i) const
 Get fancy name of the Parameter. More...
 
double GetDiagonalError (const int i) const
 Get diagonal error for ith parameter. More...
 
double GetError (const int i) const
 Get the error for the ith parameter. More...
 
void ResetIndivStepScale ()
 Adaptive Step Tuning Stuff. More...
 
void SetIndivStepScaleForSkippedAdaptParams ()
 Set individual step scale for parameters which are skipped during adaption to initial values. More...
 
void InitialiseAdaption (const YAML::Node &adapt_manager)
 Initialise adaptive MCMC. More...
 
void SaveAdaptiveToFile (const std::string &outFileName, const std::string &systematicName)
 Save adaptive throw matrix to file. More...
 
bool GetDoAdaption () const
 Do we adapt or not. More...
 
void SetThrowMatrix (const TMatrixDSym *cov)
 Use new throw matrix, used in adaptive MCMC. More...
 
void SetSubThrowMatrix (int first_index, int last_index, TMatrixDSym const &subcov)
 
void UpdateThrowMatrix (TMatrixDSym *cov)
 Replaces old throw matrix with new one. More...
 
void SetNumberOfSteps (const int nsteps)
 Set number of MCMC step, when running adaptive MCMC it is updated with given frequency. We need number of steps to determine frequency. More...
 
TMatrixDSym * GetThrowMatrix () const
 Get matrix used for step proposal. More...
 
double GetThrowMatrix (const int i, const int j) const
 Get matrix used for step proposal. More...
 
TH2D * GetCorrelationMatrix () const
 KS: Convert covariance matrix to correlation matrix and return TH2D which can be used for fancy plotting. More...
 
const M3::float_tRetPointer (const int iParam) const
 DB Pointer return to param position. More...
 
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 having to copy values using getProposed() More...
 
int GetNumParams () const
 Get total number of parameters. More...
 
std::vector< double > GetPreFitValues () const
 Get the pre-fit values of the parameters. More...
 
std::vector< double > GetProposed () const
 Get vector of all proposed parameter values. More...
 
M3::float_t GetParProp (const int i) const
 Get proposed parameter value. More...
 
double GetParCurr (const int i) const
 Get current parameter value. More...
 
const std::vector< double > & GetParCurrVec () const
 Get vector of current parameter values. More...
 
double GetParPreFit (const int i) const
 Get prior parameter value. More...
 
double GetUpperBound (const int i) const
 Get upper parameter bound in which it is physically valid. More...
 
double GetLowerBound (const int i) const
 Get lower parameter bound in which it is physically valid. More...
 
double GetIndivStepScale (const int ParameterIndex) const
 Get individual step scale for selected parameter. More...
 
double GetGlobalStepScale () const
 Get global step scale for covariance object. More...
 
int GetNParameters () const
 Get number of params which will be different depending if using Eigen decomposition or not. More...
 
void PrintPreFitValues () const
 Print prior value for every parameter. More...
 
void PrintPreFitCurrPropValues () const
 Print prior, current and proposed value for each parameter. More...
 
void PrintIndivStepScale () const
 Print step scale for each parameter. More...
 
virtual void ProposeStep ()
 Generate a new proposed state. More...
 
void Randomize () _noexcept_
 "Randomize" the parameters in the covariance class for the proposed step. Used the proposal kernel and the current parameter value to set proposed step More...
 
void CorrelateSteps () _noexcept_
 Use Cholesky throw matrix for better step proposal. More...
 
void UpdateAdaptiveCovariance ()
 Method to update adaptive MCMC [15]. More...
 
void AcceptStep () _noexcept_
 Accepted this step. More...
 
void SetFixAllParameters ()
 Set all parameters to be fixed at prior values. More...
 
void SetFixParameter (const int i)
 Set parameter to be fixed at prior value. More...
 
void SetFixParameter (const std::string &name)
 Set parameter to be fixed at prior value. More...
 
void SetFreeAllParameters ()
 Set all parameters to be treated as free. More...
 
void SetFreeParameter (const int i)
 Set parameter to be treated as free. More...
 
void SetFreeParameter (const std::string &name)
 Set parameter to be treated as free. More...
 
bool IsParameterFixed (const int i) const
 Is parameter fixed or not. More...
 
bool IsParameterFixed (const std::string &name) const
 Is parameter fixed or not. More...
 
void ConstructPCA (const double eigen_threshold, int FirstPCAdpar, int LastPCAdpar)
 CW: Calculate eigen values, prepare transition matrices and remove param based on defined threshold. More...
 
bool IsPCA () const
 is PCA, can use to query e.g. LLH scans More...
 
YAML::Node GetConfig () const
 Getter to return a copy of the YAML node. More...
 
AdaptiveMCMCHandlerGetAdaptiveHandler () const
 Get pointer for AdaptiveHandler. More...
 
void SetTune (const std::string &TuneName)
 KS: Set proposed parameter values vector to be base on tune values, for example set proposed values to be of generated or maybe PostND. More...
 
PCAHandlerGetPCAHandler () const
 Get pointer for PCAHandler. More...
 
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. More...
 

Protected Member Functions

void InitialiseFromConfig (const std::vector< std::string > &YAMLFile)
 Initialisation of the class using config. More...
 
void Print () const
 Print information about the whole object once it is set. More...
 
void PrintGlobablInfo () const
 Prints general information about the ParameterHandler object. More...
 
void PrintNormParams () const
 Prints normalization parameters. More...
 
void PrintSplineParams () const
 Prints spline parameters. More...
 
void PrintFunctionalParams () const
 Prints functional parameters. More...
 
void PrintOscillationParams () const
 Prints oscillation parameters. More...
 
void PrintParameterGroups () const
 Prints groups of parameters. More...
 
void CheckCorrectInitialisation () const
 KS: Check if matrix is correctly initialised. More...
 
void LoadCorrelationFromConfig (std::vector< std::map< std::string, double >> &Correlations, std::map< std::string, int > &CorrNamesMap)
 Load correlation from yaml config. More...
 
void LoadAndMergeYAML (const std::vector< std::string > &YAMLFile, std::map< std::pair< int, int >, std::unique_ptr< TMatrixDSym >> &overrides)
 Initialise single yaml based on several yaml files. More...
 
template<typename FilterFunc , typename ActionFunc >
void IterateOverParams (const std::string &SampleName, FilterFunc filter, ActionFunc action) const
 Iterates over parameters and applies a filter and action function. More...
 
void InitParameters ()
 Initializes the systematic parameters from the configuration file. This function loads parameters like normalizations and splines from the provided YAML file. More...
 
void InitParametersTypeFromConfig ()
 Parses the YAML configuration to set up cross-section parameters. The YAML file defines the types of systematic errors, interpolation types, and bounds for splines. More...
 
NormParameter GetNormParameter (const YAML::Node &param, const int Index) const
 Get Norm params. More...
 
OscillationParameter GetOscillationParameters (const YAML::Node &param, const int Index) const
 Get Osc params. More...
 
FunctionalParameter GetFunctionalParameters (const YAML::Node &param, const int Index) const
 Get Func params. More...
 
SplineParameter GetSplineParameter (const YAML::Node &param, const int Index) const
 Get Spline params. More...
 
void GetBaseParameter (const YAML::Node &param, const int Index, TypeParameterBase &Parameter) const
 Fill base parameters. More...
 
bool AppliesToSample (const int SystIndex, const std::string &SampleName) const
 Check if parameter is affecting given sample name. More...
 
template<typename ParamT >
std::vector< ParamT > GetTypeParamsFromSampleName (const std::map< int, int > &indexMap, const std::vector< ParamT > &params, const std::string &SampleName) const
 Retrieve parameters that apply to a given sample name. More...
 
- Protected Member Functions inherited from ParameterHandlerBase
void InitFromFile (const std::string &name, const std::string &file)
 Initialisation of the class using matrix from root file. More...
 
void ReserveMemory (const int size)
 Initialise vectors with parameters information. More...
 
void MakePosDef (TMatrixDSym *cov=nullptr, bool verbose=true)
 Make matrix positive definite by adding small values to diagonal, necessary for inverting matrix. More...
 
void MakeClosestPosDef (TMatrixDSym *cov)
 HW: Finds closest possible positive definite matrix in Frobenius Norm ||.||_frob Where ||X||_frob=sqrt[sum_ij(x_ij^2)] (basically just turns an n,n matrix into vector in n^2 space then does Euclidean norm) More...
 
void SetThrowMatrixFromFile (const std::string &matrix_file_name, const std::string &matrix_name, const std::string &means_name)
 sets throw matrix from a file More...
 
void SanitizeAdaption () const
 Perform sanity check to ensure adaption isn't misbehaving before fit starts. More...
 
void FlipParameterValue (const int index, const double FlipPoint)
 KS: Flip parameter around given value, for example mass ordering around 0. More...
 
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. More...
 
void EnableSpecialProposal (const YAML::Node &param, const int Index)
 Enable special proposal. More...
 
void SpecialStepProposal ()
 Perform Special Step Proposal. More...
 

Protected Attributes

std::vector< std::vector< std::string > > _fSampleNames
 Tells to which samples object param should be applied. More...
 
std::vector< SystType_fParamType
 Type of parameter like norm, spline etc. More...
 
std::vector< std::string > _ParameterGroup
 KS: Allow to group parameters for example to affect only cross-section or only flux etc. More...
 
std::vector< std::map< int, int > > _fSystToGlobalSystIndexMap
 Map between number of given parameter type with global parameter numbering. For example 2nd norm param may be 10-th global param. More...
 
std::vector< SplineParameterSplineParams
 Vector containing info for normalisation systematics. More...
 
std::vector< NormParameterNormParams
 Vector containing info for normalisation systematics. More...
 
std::vector< FunctionalParameterFuncParams
 Vector containing info for functional systematics. More...
 
std::vector< OscillationParameterOscParams
 Vector containing info for functional systematics. More...
 
- Protected Attributes inherited from ParameterHandlerBase
bool doSpecialStepProposal
 Check if any of special step proposal were enabled. More...
 
std::string inputFile
 The input root file we read in. More...
 
std::string matrixName
 Name of cov matrix. More...
 
TMatrixDSym * covMatrix
 The covariance matrix. More...
 
TMatrixDSym * invCovMatrix
 The inverse covariance matrix. More...
 
std::vector< std::vector< double > > InvertCovMatrix
 KS: Same as above but much faster as TMatrixDSym cache miss. More...
 
std::vector< std::unique_ptr< TRandom3 > > random_number
 KS: Set Random numbers for each thread so each thread has different seed. More...
 
double * randParams
 Random number taken from gaussian around prior error used for corr_throw. More...
 
double * corr_throw
 Result of multiplication of Cholesky matrix and randParams. More...
 
double _fGlobalStepScale
 Global step scale applied to all params in this class. More...
 
int PrintLength
 KS: This is used when printing parameters, sometimes we have super long parameters name, we want to flexibly adjust couts. More...
 
std::vector< std::string > _fNames
 ETA _fNames is set automatically in the covariance class to be something like param_i, this is currently to make things compatible with the Diagnostic tools. More...
 
std::vector< std::string > _fFancyNames
 Fancy name for example rather than param_0 it is MAQE, useful for human reading. More...
 
YAML::Node _fYAMLDoc
 Stores config describing systematics. More...
 
int _fNumPar
 Number of systematic parameters. More...
 
std::vector< double > _fPreFitValue
 Parameter value dictated by the prior model. Based on it penalty term is calculated. More...
 
std::vector< double > _fCurrVal
 Current value of the parameter. More...
 
std::vector< M3::float_t_fPropVal
 Proposed value of the parameter. More...
 
std::vector< double > _fError
 Prior error on the parameter. More...
 
std::vector< double > _fLowBound
 Lowest physical bound, parameter will not be able to go beyond it. More...
 
std::vector< double > _fUpBound
 Upper physical bound, parameter will not be able to go beyond it. More...
 
std::vector< double > _fIndivStepScale
 Individual step scale used by MCMC algorithm. More...
 
std::vector< bool > _fFlatPrior
 Whether to apply flat prior or not. More...
 
std::vector< double > _fIndivStepScaleInitial
 Backup of _fIndivStepScale for parameters which are skipped during adaption. More...
 
double _fGlobalStepScaleInitial
 Backup of _fGlobalStepScale for parameters which are skipped during adaption. More...
 
std::vector< bool > param_skip_adapt_flags
 Flags telling if parameter should be skipped during adaption. More...
 
TMatrixDSym * throwMatrix
 Matrix which we use for step proposal before Cholesky decomposition (not actually used for step proposal) More...
 
double ** throwMatrixCholDecomp
 Throw matrix that is being used in the fit, much faster as TMatrixDSym cache miss. More...
 
bool pca
 perform PCA or not More...
 
bool use_adaptive
 Are we using AMCMC? More...
 
std::unique_ptr< PCAHandlerPCAObj
 Struct containing information about PCA. More...
 
std::unique_ptr< AdaptiveMCMCHandlerAdaptiveHandler
 Struct containing information about adaption. More...
 
std::unique_ptr< ParameterTunesTunes
 Struct containing information about adaption. More...
 
std::vector< int > FlipParameterIndex
 Indices of parameters with flip symmetry. More...
 
std::vector< double > FlipParameterPoint
 Central points around which parameters are flipped. More...
 
std::vector< int > CircularBoundsIndex
 Indices of parameters with circular bounds. More...
 
std::vector< std::pair< double, double > > CircularBoundsValues
 Circular bounds for each parameter (lower, upper) More...
 

Detailed Description

Class responsible for handling of systematic error parameters with different types defined in the config. Like spline, normalisation parameters etc.

Author
Dan Barrow
Ed Atkin
Kamil Skwarczynski

Definition at line 14 of file ParameterHandlerGeneric.h.

Constructor & Destructor Documentation

◆ ParameterHandlerGeneric()

ParameterHandlerGeneric::ParameterHandlerGeneric ( const std::vector< std::string > &  FileNames,
std::string  name = "xsec_cov",
double  threshold = -1,
int  FirstPCAdpar = -999,
int  LastPCAdpar = -999 
)

Constructor.

Parameters
FileNamesA vector of strings representing the YAML files used for initialisation of matrix
nameMatrix name
thresholdPCA threshold from 0 to 1. Default is -1 and means no PCA
FirstPCAdparFirst PCA parameter that will be decomposed.
LastPCAdparFirst PCA parameter that will be decomposed.

Definition at line 7 of file ParameterHandlerGeneric.cpp.

8  {
9 // ********************************************
10  MACH3LOG_INFO("Constructing instance of ParameterHandler using");
11  inputFile = YAMLFile[0];
12  matrixName = name;
13  pca = true;
14 
15  doSpecialStepProposal = false;
16  // Not using adaptive by default
17  use_adaptive = false;
18  for(unsigned int i = 0; i < YAMLFile.size(); i++)
19  {
20  MACH3LOG_INFO("{}", YAMLFile[i]);
21  }
22  MACH3LOG_INFO("as an input");
23 
24  if (threshold < 0 || threshold >= 1) {
25  MACH3LOG_INFO("Principal component analysis but given the threshold for the principal components to be less than 0, or greater than (or equal to) 1. This will not work");
26  MACH3LOG_INFO("Please specify a number between 0 and 1");
27  MACH3LOG_INFO("You specified: ");
28  MACH3LOG_INFO("Am instead calling the usual non-PCA constructor...");
29  pca = false;
30  }
31 
32  InitialiseFromConfig(YAMLFile);
33  // Call the innocent helper function
34  if (pca) ConstructPCA(threshold, FirstPCA, LastPCA);
35 
37 
38  //ETA - again this really doesn't need to be hear...
39  for (int i = 0; i < _fNumPar; i++)
40  {
41  // Sort out the print length
42  if(int(_fNames[i].length()) > PrintLength) PrintLength = int(_fNames[i].length());
43  } // end the for loop
44 
45  MACH3LOG_DEBUG("Constructing instance of ParameterHandler");
47  // Print
48  Print();
49 }
#define MACH3LOG_DEBUG
Definition: MaCh3Logger.h:34
#define MACH3LOG_INFO
Definition: MaCh3Logger.h:35
bool use_adaptive
Are we using AMCMC?
std::string inputFile
The input root file we read in.
bool doSpecialStepProposal
Check if any of special step proposal were enabled.
std::string matrixName
Name of cov matrix.
int _fNumPar
Number of systematic parameters.
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< std::string > _fNames
ETA _fNames is set automatically in the covariance class to be something like param_i,...
bool pca
perform PCA or not
int PrintLength
KS: This is used when printing parameters, sometimes we have super long parameters name,...
void Print() const
Print information about the whole object once it is set.
void InitParametersTypeFromConfig()
Parses the YAML configuration to set up cross-section parameters. The YAML file defines the types of ...
void InitParameters()
Initializes the systematic parameters from the configuration file. This function loads parameters lik...
void InitialiseFromConfig(const std::vector< std::string > &YAMLFile)
Initialisation of the class using config.

◆ ~ParameterHandlerGeneric()

ParameterHandlerGeneric::~ParameterHandlerGeneric ( )

Destructor.

Definition at line 356 of file ParameterHandlerGeneric.cpp.

356  {
357 // ********************************************
358  MACH3LOG_DEBUG("Deleting ParameterHandler");
359 }

Member Function Documentation

◆ AppliesToSample()

bool ParameterHandlerGeneric::AppliesToSample ( const int  SystIndex,
const std::string &  SampleName 
) const
protected

Check if parameter is affecting given sample name.

Parameters
SystIndexnumber of parameter
SampleNameThe Sample name used to filter parameters.

Definition at line 539 of file ParameterHandlerGeneric.cpp.

539  {
540 // ********************************************
541  // Empty means apply to all
542  if (_fSampleNames[SystIndex].size() == 0) return true;
543 
544  // Check for unsupported wildcards in SampleName
545  if (SampleName.find('*') != std::string::npos) {
546  MACH3LOG_ERROR("Wildcards ('*') are not supported in sample name: '{}'", SampleName);
547  throw MaCh3Exception(__FILE__ , __LINE__ );
548  }
549  return M3::CaseInsensitiveMatchAny(SampleName, _fSampleNames[SystIndex]);
550 }
#define MACH3LOG_ERROR
Definition: MaCh3Logger.h:37
Custom exception class used throughout MaCh3.
std::vector< std::vector< std::string > > _fSampleNames
Tells to which samples object param should be applied.
bool CaseInsensitiveMatchAny(std::string Text, const std::vector< std::string > &Patterns)
Matches a string against a simple wildcard Pattern using regex. Is not case sensitive.

◆ CheckCorrectInitialisation()

void ParameterHandlerGeneric::CheckCorrectInitialisation ( ) const
protected

KS: Check if matrix is correctly initialised.

Definition at line 896 of file ParameterHandlerGeneric.cpp.

896  {
897 // ********************************************
898  // KS: Lambda Function which simply checks if there are no duplicates in std::vector
899  auto CheckForDuplicates = [](const std::vector<std::string>& names, const std::string& nameType, bool Warning) {
900  std::unordered_map<std::string, size_t> seenStrings;
901  for (size_t i = 0; i < names.size(); ++i) {
902  const auto& name = names[i];
903  if (seenStrings.find(name) != seenStrings.end()) {
904  size_t firstIndex = seenStrings[name];
905  if(Warning){
906  MACH3LOG_WARN("There are two systematics with the same {} '{}', first at index {}, and again at index {}", nameType, name, firstIndex, i);
907  MACH3LOG_WARN("Is this desired?");
908  return;
909  } else {
910  MACH3LOG_CRITICAL("There are two systematics with the same {} '{}', first at index {}, and again at index {}", nameType, name, firstIndex, i);
911  throw MaCh3Exception(__FILE__, __LINE__);
912  }
913  }
914  seenStrings[name] = i;
915  }
916  };
917  std::vector<std::string> SplineNameTemp(SplineParams.size());
918  for(size_t it = 0; it < SplineParams.size(); it++){
919  SplineNameTemp[it] = SplineParams[it]._fSplineNames;
920  }
921  // KS: Checks if there are no duplicates in fancy names etc, this can happen if we merge configs etc
922  CheckForDuplicates(_fFancyNames, "_fFancyNames", false);
923  CheckForDuplicates(SplineNameTemp, "_fSplineNames", true);
924 }
#define MACH3LOG_CRITICAL
Definition: MaCh3Logger.h:38
#define MACH3LOG_WARN
Definition: MaCh3Logger.h:36
std::vector< std::string > _fFancyNames
Fancy name for example rather than param_0 it is MAQE, useful for human reading.
std::vector< SplineParameter > SplineParams
Vector containing info for normalisation systematics.

◆ DumpMatrixToFile()

void ParameterHandlerGeneric::DumpMatrixToFile ( const std::string &  Name)

Dump Matrix to ROOT file, useful when we need to pass matrix info to another fitting group.

Parameters
NameName of TFile to which we save stuff
Warning
This is mostly used for backward compatibility

Definition at line 1041 of file ParameterHandlerGeneric.cpp.

1041  {
1042 // ********************************************
1043  // KS: Ideally we remove it eventually...
1044  TH2D* CorrMatrix = GetCorrelationMatrix();
1046  _fPreFitValue,
1047  _fError,
1048  _fLowBound,
1049  _fUpBound,
1051  _fFancyNames,
1052  _fFlatPrior,
1053  SplineParams,
1054  covMatrix,
1055  CorrMatrix,
1056  Name);
1057  delete CorrMatrix;
1058  MACH3LOG_INFO("Finished dumping ParameterHandler object");
1059 }
TH2D * GetCorrelationMatrix() const
KS: Convert covariance matrix to correlation matrix and return TH2D which can be used for fancy plott...
std::vector< bool > _fFlatPrior
Whether to apply flat prior or not.
std::vector< double > _fError
Prior error on the parameter.
std::vector< double > _fLowBound
Lowest physical bound, parameter will not be able to go beyond it.
TMatrixDSym * covMatrix
The covariance matrix.
std::vector< double > _fPreFitValue
Parameter value dictated by the prior model. Based on it penalty term is calculated.
std::vector< double > _fUpBound
Upper physical bound, parameter will not be able to go beyond it.
std::vector< double > _fIndivStepScale
Individual step scale used by MCMC algorithm.
void DumpParamHandlerToFile(const int _fNumPar, const std::vector< double > &_fPreFitValue, const std::vector< double > &_fError, const std::vector< double > &_fLowBound, const std::vector< double > &_fUpBound, const std::vector< double > &_fIndivStepScale, const std::vector< std::string > &_fFancyNames, const std::vector< bool > &_fFlatPrior, const std::vector< SplineParameter > &SplineParams, TMatrixDSym *covMatrix, TH2D *CorrMatrix, const std::string &Name)
Dump Matrix to ROOT file, useful when we need to pass matrix info to another fitting group.

◆ GetBaseParameter()

void ParameterHandlerGeneric::GetBaseParameter ( const YAML::Node &  param,
const int  Index,
TypeParameterBase Parameter 
) const
protected

Fill base parameters.

Parameters
paramYaml node describing param
IndexGlobal parameter index
ParameterObject storing info

Definition at line 435 of file ParameterHandlerGeneric.cpp.

435  {
436 // ********************************************
437  Parameter.name = GetParFancyName(Index);
438 
439  // Set the global parameter index of the normalisation parameter
440  Parameter.index = Index;
441 
442  int NumKinematicCuts = 0;
443  if(param["KinematicCuts"]) {
444  NumKinematicCuts = int(param["KinematicCuts"].size());
445 
446  std::vector<std::string> TempKinematicStrings;
447  std::vector<std::vector<std::vector<double>>> TempKinematicBounds;
448  //First element of TempKinematicBounds is always -999, and size is then 3
449  for(int KinVar_i = 0 ; KinVar_i < NumKinematicCuts ; ++KinVar_i) {
450  //ETA: This is a bit messy, Kinematic cuts is a list of maps
451  for (YAML::const_iterator it = param["KinematicCuts"][KinVar_i].begin();it!=param["KinematicCuts"][KinVar_i].end();++it) {
452  TempKinematicStrings.push_back(it->first.as<std::string>());
453  TempKinematicBounds.push_back(Get2DBounds(it->second));
454  }
455  if(TempKinematicStrings.size() == 0) {
456  MACH3LOG_ERROR("Received a KinematicCuts node but couldn't read the contents (it's a list of single-element dictionaries (python) = map of pairs (C++))");
457  MACH3LOG_ERROR("For Param {}", Parameter.name);
458  throw MaCh3Exception(__FILE__, __LINE__);
459  }
460  }//KinVar_i
461  Parameter.KinematicVarStr = TempKinematicStrings;
462  Parameter.Selection = TempKinematicBounds;
463  }
464 
465  //Next ones are kinematic bounds on where normalisation parameter should apply
466  //We set a bool to see if any bounds exist so we can short-circuit checking all of them every step
467  bool HasKinBounds = false;
468 
469  if(Parameter.KinematicVarStr.size() > 0) HasKinBounds = true;
470 
471  Parameter.hasKinBounds = HasKinBounds;
472  //End of kinematic bound checking
473 }
#define Get2DBounds(filename)
Definition: YamlHelper.h:591
std::string GetParFancyName(const int i) const
Get fancy name of the Parameter.
int index
Parameter number of this normalisation in current systematic model.
bool hasKinBounds
Does this parameter have kinematic bounds.
std::string name
Name of parameters.
std::vector< std::string > KinematicVarStr
std::vector< std::vector< std::vector< double > > > Selection

◆ GetFunctionalParameters()

FunctionalParameter ParameterHandlerGeneric::GetFunctionalParameters ( const YAML::Node &  param,
const int  Index 
) const
protected

Get Func params.

Parameters
paramYaml node describing param
IndexGlobal parameter index

Definition at line 554 of file ParameterHandlerGeneric.cpp.

554  {
555 // ********************************************
556  FunctionalParameter func;
557  GetBaseParameter(param, Index, func);
558 
559  func.modes = GetFromManager<std::vector<int>>(param["Mode"], std::vector<int>(), __FILE__ , __LINE__);
560 
561  return func;
562 }
void GetBaseParameter(const YAML::Node &param, const int Index, TypeParameterBase &Parameter) const
Fill base parameters.
HH - Functional parameters Carrier for whether you want to apply a systematic to an event or not.
std::vector< int > modes
Mode which parameter applies to.

◆ GetFunctionalParametersFromSampleName()

const std::vector< FunctionalParameter > ParameterHandlerGeneric::GetFunctionalParametersFromSampleName ( const std::string &  SampleName) const

HH Get functional parameters for the relevant SampleName.

Definition at line 576 of file ParameterHandlerGeneric.cpp.

576  {
577 // ********************************************
579 }
@ kFunc
For functional parameters.
std::vector< std::map< int, int > > _fSystToGlobalSystIndexMap
Map between number of given parameter type with global parameter numbering. For example 2nd norm para...
std::vector< ParamT > GetTypeParamsFromSampleName(const std::map< int, int > &indexMap, const std::vector< ParamT > &params, const std::string &SampleName) const
Retrieve parameters that apply to a given sample name.
std::vector< FunctionalParameter > FuncParams
Vector containing info for functional systematics.

◆ GetGlobalSystIndexFromSampleName()

const std::vector< int > ParameterHandlerGeneric::GetGlobalSystIndexFromSampleName ( const std::string &  SampleName,
const SystType  Type 
) const

DB Get spline parameters depending on given SampleName.

Definition at line 478 of file ParameterHandlerGeneric.cpp.

478  {
479 // ********************************************
480  std::vector<int> returnVec;
481  for (auto &pair : _fSystToGlobalSystIndexMap[Type]) {
482  auto &SystIndex = pair.second;
483  if (AppliesToSample(SystIndex, SampleName)) { //If parameter applies to required SampleID
484  returnVec.push_back(SystIndex);
485  }
486  }
487  return returnVec;
488 }
bool AppliesToSample(const int SystIndex, const std::string &SampleName) const
Check if parameter is affecting given sample name.

◆ GetNormParameter()

NormParameter ParameterHandlerGeneric::GetNormParameter ( const YAML::Node &  param,
const int  Index 
) const
protected

Get Norm params.

Parameters
paramYaml node describing param
IndexGlobal parameter index

Definition at line 410 of file ParameterHandlerGeneric.cpp.

410  {
411 // ********************************************
412  NormParameter norm;
413 
414  GetBaseParameter(param, Index, norm);
415 
416  // ETA size 0 to mean apply to all
417  // Ultimately all this information ends up in the NormParams vector
418  norm.modes = GetFromManager<std::vector<int>>(param["Mode"], {}, __FILE__ , __LINE__);
419  norm.pdgs = GetFromManager<std::vector<int>>(param["NeutrinoFlavour"], {}, __FILE__ , __LINE__);
420  norm.preoscpdgs = GetFromManager<std::vector<int>>(param["NeutrinoFlavourUnosc"], {}, __FILE__ , __LINE__);
421  norm.targets = GetFromManager<std::vector<int>>(param["TargetNuclei"], {}, __FILE__ , __LINE__);
422 
423  if(_fLowBound[Index] < 0.) {
424  MACH3LOG_ERROR("Normalisation Parameter {} ({}), has lower parameters bound which can go below 0 and is equal {}",
425  GetParFancyName(Index), Index, _fLowBound[Index]);
426  MACH3LOG_ERROR("Normalisation parameters can't go bellow 0 as this is unphysical");
427  throw MaCh3Exception(__FILE__, __LINE__);
428  }
429 
430  return norm;
431 }
ETA - Normalisations for cross-section parameters Carrier for whether you want to apply a systematic ...
std::vector< int > preoscpdgs
Preosc PDG which parameter applies to.
std::vector< int > modes
Mode which parameter applies to.
std::vector< int > pdgs
PDG which parameter applies to.
std::vector< int > targets
Targets which parameter applies to.

◆ GetNormParsFromSampleName()

const std::vector< NormParameter > ParameterHandlerGeneric::GetNormParsFromSampleName ( const std::string &  SampleName) const

DB Get norm/func parameters depending on given SampleName.

Definition at line 583 of file ParameterHandlerGeneric.cpp.

583  {
584 // ********************************************
586 }
@ kNorm
For normalisation parameters.
std::vector< NormParameter > NormParams
Vector containing info for normalisation systematics.

◆ GetNumParamsFromSampleName()

int ParameterHandlerGeneric::GetNumParamsFromSampleName ( const std::string &  SampleName,
const SystType  Type 
) const

DB Grab the number of parameters for the relevant SampleName.

Parameters
SampleNameproperty of SampleHandler class based on which we select whether to apply uncertainties or not
TypeType of syst, for example kNorm, kSpline etc

Definition at line 613 of file ParameterHandlerGeneric.cpp.

613  {
614 // ********************************************
615  int returnVal = 0;
616  IterateOverParams(SampleName,
617  [&](int i) { return GetParamType(i) == Type; }, // Filter condition
618  [&](int) { returnVal += 1; } // Action to perform if filter passes
619  );
620  return returnVal;
621 }
void IterateOverParams(const std::string &SampleName, FilterFunc filter, ActionFunc action) const
Iterates over parameters and applies a filter and action function.
SystType GetParamType(const int i) const
Returns enum describing our param type.

◆ GetNumParFromGroup()

int ParameterHandlerGeneric::GetNumParFromGroup ( const std::string &  Group) const

KS: Check how many parameters are associated with given group.

Definition at line 1016 of file ParameterHandlerGeneric.cpp.

1016  {
1017 // ********************************************
1018  int Counter = 0;
1019  for (int i = 0; i < _fNumPar; i++) {
1020  if(IsParFromGroup(i, Group)) Counter++;
1021  }
1022  return Counter;
1023 }
bool IsParFromGroup(const int i, const std::string &Group) const
Checks if parameter belongs to a given group.

◆ GetOscillationParameters()

OscillationParameter ParameterHandlerGeneric::GetOscillationParameters ( const YAML::Node &  param,
const int  Index 
) const
protected

Get Osc params.

Parameters
paramYaml node describing param
IndexGlobal parameter index

Definition at line 566 of file ParameterHandlerGeneric.cpp.

566  {
567 // ********************************************
568  OscillationParameter OscParamInfo;
569  GetBaseParameter(param, Index, OscParamInfo);
570 
571  return OscParamInfo;
572 }
KS: Struct holding info about oscillation Systematics.

◆ GetOscParsFromSampleName()

std::vector< const M3::float_t * > ParameterHandlerGeneric::GetOscParsFromSampleName ( const std::string &  SampleName) const

Get pointers to Osc params from Sample name.

Definition at line 1027 of file ParameterHandlerGeneric.cpp.

1027  {
1028 // ********************************************
1029  std::vector<const M3::float_t*> returnVec;
1030  for (const auto& pair : _fSystToGlobalSystIndexMap[SystType::kOsc]) {
1031  const auto& globalIndex = pair.second;
1032  if (AppliesToSample(globalIndex, SampleName)) {
1033  returnVec.push_back(RetPointer(globalIndex));
1034  }
1035  }
1036  return returnVec;
1037 }
@ kOsc
For oscillation parameters.
const M3::float_t * RetPointer(const int iParam) const
DB Pointer return to param position.

◆ GetParamType()

SystType ParameterHandlerGeneric::GetParamType ( const int  i) const
inline

Returns enum describing our param type.

Parameters
iparameter index

Definition at line 35 of file ParameterHandlerGeneric.h.

35 {return _fParamType[i];}
std::vector< SystType > _fParamType
Type of parameter like norm, spline etc.

◆ GetParamTypeString()

std::string ParameterHandlerGeneric::GetParamTypeString ( const int  i) const
inline

ETA - just return a string of "spline", "norm" or "functional".

Parameters
iparameter index

Definition at line 32 of file ParameterHandlerGeneric.h.

32 { return SystType_ToString(_fParamType[i]); }
std::string SystType_ToString(const SystType i)
Convert a Syst type type to a string.

◆ GetParSampleID()

std::vector<std::string> ParameterHandlerGeneric::GetParSampleID ( const int  i) const
inline

ETA - just return the int of the SampleName, this can be removed to do a string comp at some point.

Parameters
iparameter index

Definition at line 29 of file ParameterHandlerGeneric.h.

29 { return _fSampleNames[i];};

◆ GetParsIndexFromSampleName()

const std::vector< int > ParameterHandlerGeneric::GetParsIndexFromSampleName ( const std::string &  SampleName,
const SystType  Type 
) const

DB Grab the parameter indices for the relevant SampleName.

Parameters
SampleNameproperty of SampleHandler class based on which we select whether to apply uncertainties or not
TypeType of syst, for example kNorm, kSpline etc

Definition at line 637 of file ParameterHandlerGeneric.cpp.

637  {
638 // ********************************************
639  std::vector<int> returnVec;
640  IterateOverParams(SampleName,
641  [&](int i) { return GetParamType(i) == Type; }, // Filter condition
642  [&](int i) { returnVec.push_back(i); } // Action to perform if filter passes
643  );
644  return returnVec;
645 }

◆ GetParsNamesFromSampleName()

const std::vector< std::string > ParameterHandlerGeneric::GetParsNamesFromSampleName ( const std::string &  SampleName,
const SystType  Type 
) const

DB Grab the parameter names for the relevant SampleName.

Parameters
SampleNameproperty of SampleHandler class based on which we select whether to apply uncertainties or not
TypeType of syst, for example kNorm, kSpline etc

Definition at line 625 of file ParameterHandlerGeneric.cpp.

625  {
626 // ********************************************
627  std::vector<std::string> returnVec;
628  IterateOverParams(SampleName,
629  [&](int i) { return GetParamType(i) == Type; }, // Filter condition
630  [&](int i) { returnVec.push_back(GetParFancyName(i)); } // Action to perform if filter passes
631  );
632  return returnVec;
633 }

◆ GetParSplineInterpolation()

SplineInterpolation ParameterHandlerGeneric::GetParSplineInterpolation ( const int  i) const
inline

Get interpolation type for a given parameter.

Parameters
ispline parameter index, not confuse with global index

Definition at line 39 of file ParameterHandlerGeneric.h.

39 {return SplineParams.at(i)._SplineInterpolationType;}

◆ GetParSplineKnotLowerBound()

double ParameterHandlerGeneric::GetParSplineKnotLowerBound ( const int  i) const
inline

EM: value at which we cap spline knot weight.

Parameters
ispline parameter index, not confuse with global index

Definition at line 53 of file ParameterHandlerGeneric.h.

53 {return SplineParams.at(i)._SplineKnotLowBound;}

◆ GetParSplineKnotUpperBound()

double ParameterHandlerGeneric::GetParSplineKnotUpperBound ( const int  i) const
inline

EM: value at which we cap spline knot weight.

Parameters
ispline parameter index, not confuse with global index

Definition at line 50 of file ParameterHandlerGeneric.h.

50 {return SplineParams.at(i)._SplineKnotUpBound;}

◆ GetParSplineName()

std::string ParameterHandlerGeneric::GetParSplineName ( const int  i) const
inline

Get the name of the spline associated with the spline at index i.

Parameters
ispline parameter index, not to be confused with global index

Definition at line 44 of file ParameterHandlerGeneric.h.

44 {return SplineParams.at(i)._fSplineNames;}

◆ GetSplineFileParsNamesFromSampleName()

const std::vector<std::string> ParameterHandlerGeneric::GetSplineFileParsNamesFromSampleName ( const std::string &  SampleName) const

DB Get spline parameters depending on given SampleName.

◆ GetSplineInterpolationFromSampleName()

const std::vector< SplineInterpolation > ParameterHandlerGeneric::GetSplineInterpolationFromSampleName ( const std::string &  SampleName) const

Get the interpolation types for splines affecting a particular SampleName.

Definition at line 377 of file ParameterHandlerGeneric.cpp.

377  {
378 // ********************************************
379  std::vector<SplineInterpolation> returnVec;
380  for (auto &pair : _fSystToGlobalSystIndexMap[SystType::kSpline]) {
381  auto &SplineIndex = pair.first;
382  auto &SystIndex = pair.second;
383 
384  if (AppliesToSample(SystIndex, SampleName)) { //If parameter applies to required SampleID
385  returnVec.push_back(SplineParams.at(SplineIndex)._SplineInterpolationType);
386  }
387  }
388  return returnVec;
389 }
@ kSpline
For splined parameters (1D)
Flat representation of a spline index entry.
Definition: SplineCommon.h:33

◆ GetSplineModeVecFromSampleName()

const std::vector< std::vector< int > > ParameterHandlerGeneric::GetSplineModeVecFromSampleName ( const std::string &  SampleName) const

DB Grab the Spline Modes for the relevant SampleName.

Definition at line 393 of file ParameterHandlerGeneric.cpp.

393  {
394 // ********************************************
395  std::vector< std::vector<int> > returnVec;
396  //Need a counter or something to correctly get the index in _fSplineModes since it's not of length nPars
397  //Should probably just make a std::map<std::string, int> for param name to FD spline index
398  for (auto &pair : _fSystToGlobalSystIndexMap[SystType::kSpline]) {
399  auto &SplineIndex = pair.first;
400  auto &SystIndex = pair.second;
401  if (AppliesToSample(SystIndex, SampleName)) { //If parameter applies to required SampleID
402  returnVec.push_back(SplineParams.at(SplineIndex)._fSplineModes);
403  }
404  }
405  return returnVec;
406 }

◆ GetSplineParameter()

SplineParameter ParameterHandlerGeneric::GetSplineParameter ( const YAML::Node &  param,
const int  Index 
) const
protected

Get Spline params.

Parameters
paramYaml node describing param
IndexGlobal parameter index

Definition at line 508 of file ParameterHandlerGeneric.cpp.

508  {
509 // ********************************************
510  SplineParameter Spline;
511 
512  GetBaseParameter(param, Index, Spline);
513  auto& SplinePar = param["SplineInformation"];
514  //Now get the Spline interpolation type
515  if (SplinePar["InterpolationType"]) {
516  for(int InterpType = 0; InterpType < kSplineInterpolations ; ++InterpType){
517  if(SplinePar["InterpolationType"].as<std::string>() == SplineInterpolation_ToString(SplineInterpolation(InterpType)))
518  Spline._SplineInterpolationType = SplineInterpolation(InterpType);
519  }
520  } else { //KS: By default use TSpline3
522  }
523 
524  Spline._fSplineNames = Get<std::string>(SplinePar["SplineName"], __FILE__ , __LINE__);
525  Spline._SplineKnotUpBound = GetFromManager<double>(SplinePar["SplineKnotUpBound"], M3::DefSplineKnotUpBound, __FILE__ , __LINE__);
526  Spline._SplineKnotLowBound = GetFromManager<double>(SplinePar["SplineKnotLowBound"], M3::DefSplineKnotLowBound, __FILE__ , __LINE__);
527 
529  MACH3LOG_WARN("Spline knot capping enabled with bounds [{}, {}]. For reliable fits, consider modifying the input generation instead.",
530  Spline._SplineKnotLowBound, Spline._SplineKnotUpBound);
531  }
532  //If there is no mode information given then this will be an empty vector
533  Spline._fSplineModes = GetFromManager(SplinePar["Mode"], std::vector<int>(), __FILE__ , __LINE__);
534 
535  return Spline;
536 }
std::string SplineInterpolation_ToString(const SplineInterpolation i)
Convert a LLH type to a string.
SplineInterpolation
Make an enum of the spline interpolation type.
@ kTSpline3
Default TSpline3 interpolation.
@ kSplineInterpolations
This only enumerates.
Type GetFromManager(const YAML::Node &node, const Type defval, const std::string &File="", const int Line=1)
Get content of config file if node is not found take default value specified.
Definition: YamlHelper.h:329
constexpr static const double DefSplineKnotUpBound
Default value for spline knot capping, default mean not capping is being applied.
Definition: Core.h:86
constexpr static const double DefSplineKnotLowBound
Default value for spline knot capping, default mean not capping is being applied.
Definition: Core.h:88
KS: Struct holding info about Spline Systematics.
SplineInterpolation _SplineInterpolationType
Spline interpolation vector.
double _SplineKnotUpBound
EM: Cap spline knot higher value.
double _SplineKnotLowBound
EM: Cap spline knot lower value.
std::vector< int > _fSplineModes
Modes to which spline applies (valid only for binned splines)
std::string _fSplineNames
Name of spline in TTree (TBranch),.

◆ GetSplineParsFromSampleName()

const std::vector< SplineParameter > ParameterHandlerGeneric::GetSplineParsFromSampleName ( const std::string &  SampleName) const

KS: Grab the Spline parameters for the relevant SampleName.

Definition at line 590 of file ParameterHandlerGeneric.cpp.

590  {
591 // ********************************************
593 }

◆ GetSplineParsNamesFromSampleName()

const std::vector< std::string > ParameterHandlerGeneric::GetSplineParsNamesFromSampleName ( const std::string &  SampleName) const

DB Get spline parameters depending on given SampleName.

Definition at line 363 of file ParameterHandlerGeneric.cpp.

363  {
364 // ********************************************
365  std::vector<std::string> returnVec;
366  for (auto &pair : _fSystToGlobalSystIndexMap[SystType::kSpline]) {
367  auto &SplineIndex = pair.first;
368  auto &SystIndex = pair.second;
369  if (AppliesToSample(SystIndex, SampleName)) { //If parameter applies to required Sample
370  returnVec.push_back(SplineParams[SplineIndex]._fSplineNames);
371  }
372  }
373  return returnVec;
374 }

◆ GetSystIndexFromSampleName()

const std::vector< int > ParameterHandlerGeneric::GetSystIndexFromSampleName ( const std::string &  SampleName,
const SystType  Type 
) const

Grab the index of the syst relative to global numbering.

Parameters
SampleNameproperty of SampleHandler class based on which we select whether to apply uncertainties or not
TypeType of syst, for example kNorm, kSpline etc

Definition at line 493 of file ParameterHandlerGeneric.cpp.

493  {
494 // ********************************************
495  std::vector<int> returnVec;
496  for (auto &pair : _fSystToGlobalSystIndexMap[Type]) {
497  auto &SplineIndex = pair.first;
498  auto &systIndex = pair.second;
499  if (AppliesToSample(systIndex, SampleName)) { //If parameter applies to required SampleID
500  returnVec.push_back(SplineIndex);
501  }
502  }
503  return returnVec;
504 }

◆ GetTypeParamsFromSampleName()

template<typename ParamT >
std::vector< ParamT > ParameterHandlerGeneric::GetTypeParamsFromSampleName ( const std::map< int, int > &  indexMap,
const std::vector< ParamT > &  params,
const std::string &  SampleName 
) const
protected

Retrieve parameters that apply to a given sample name.

Template Parameters
ParamTType of parameter (e.g., FunctionalParameter, NormParameter).
Parameters
indexMapMap from local to global parameter indices.
paramsVector of all parameters of the given type.
SampleNameThe name of the sample to filter applicable parameters for.
Returns
Vector of parameters of type ParamT that apply to the specified sample.

Definition at line 597 of file ParameterHandlerGeneric.cpp.

598  {
599 // ********************************************
600  std::vector<ParamT> returnVec;
601  for (const auto& pair : indexMap) {
602  const auto& localIndex = pair.first;
603  const auto& globalIndex = pair.second;
604  if (AppliesToSample(globalIndex, SampleName)) {
605  returnVec.push_back(params[localIndex]);
606  }
607  }
608  return returnVec;
609 }

◆ GetUniqueParameterGroups()

std::vector< std::string > ParameterHandlerGeneric::GetUniqueParameterGroups ( ) const

KS: Get names of all unique parameter groups.

Definition at line 880 of file ParameterHandlerGeneric.cpp.

880  {
881 // ********************************************
882  std::unordered_set<std::string> uniqueGroups;
883 
884  // Fill the set with unique values
885  for (const auto& param : _ParameterGroup) {
886  uniqueGroups.insert(param);
887  }
888 
889  // Convert to vector and return
890  std::vector<std::string> result(uniqueGroups.begin(), uniqueGroups.end());
891  return result;
892 }
std::vector< std::string > _ParameterGroup
KS: Allow to group parameters for example to affect only cross-section or only flux etc.

◆ InitialiseFromConfig()

void ParameterHandlerGeneric::InitialiseFromConfig ( const std::vector< std::string > &  YAMLFile)
protected

Initialisation of the class using config.

Parameters
YAMLFileA vector of strings representing the YAML files used for initialisation of matrix

Definition at line 187 of file ParameterHandlerGeneric.cpp.

187  {
188 // ********************************************
189  std::map<std::pair<int, int>, std::unique_ptr<TMatrixDSym>> ThrowSubMatrixOverrides;
190  LoadAndMergeYAML(YAMLFile, ThrowSubMatrixOverrides);
191 
192  const int nThreads = M3::GetNThreads();
193  //KS: set Random numbers for each thread so each thread has different seed
194  //or for one thread if without MULTITHREAD
195  random_number.reserve(nThreads);
196  for (int iThread = 0; iThread < nThreads; iThread++) {
197  random_number.emplace_back(std::make_unique<TRandom3>(0));
198  }
199  PrintLength = 35;
200 
201  // Set the covariance matrix
202  _fNumPar = int(_fYAMLDoc["Systematics"].size());
203 
205 
206  int i = 0;
207  std::vector<std::map<std::string,double>> Correlations(_fNumPar);
208  std::map<std::string, int> CorrNamesMap;
209 
210  //ETA - read in the systematics. Would be good to add in some checks to make sure
211  //that there are the correct number of entries i.e. are the _fNumPar for Names,
212  //PreFitValues etc etc.
213  for (auto const &param : _fYAMLDoc["Systematics"])
214  {
215  _fFancyNames[i] = Get<std::string>(param["Systematic"]["Names"]["FancyName"], __FILE__ , __LINE__);
216  _fPreFitValue[i] = Get<double>(param["Systematic"]["ParameterValues"]["PreFitValue"], __FILE__ , __LINE__);
217  _fIndivStepScale[i] = Get<double>(param["Systematic"]["StepScale"]["MCMC"], __FILE__ , __LINE__);
218  _fError[i] = Get<double>(param["Systematic"]["Error"], __FILE__ , __LINE__);
219  if(_fError[i] <= 0) {
220  MACH3LOG_ERROR("Error for param {}({}) is negative and equal to {}", _fFancyNames[i], i, _fError[i]);
221  throw MaCh3Exception(__FILE__ , __LINE__ );
222  }
223  //ETA - a bit of a fudge but works
224  auto TempBoundsVec = GetBounds(param["Systematic"]["ParameterBounds"]);
225  _fLowBound[i] = TempBoundsVec[0];
226  _fUpBound[i] = TempBoundsVec[1];
227 
228  //ETA - now for parameters which are optional and have default values
229  _fFlatPrior[i] = GetFromManager<bool>(param["Systematic"]["FlatPrior"], false, __FILE__ , __LINE__);
230 
231  // Allow to fix param, this setting should be used only for params which are permanently fixed like baseline, please use global config for fixing param more flexibly
232  if(GetFromManager<bool>(param["Systematic"]["FixParam"], false, __FILE__ , __LINE__)) {
234  }
235 
236  if(param["Systematic"]["SpecialProposal"]) {
237  EnableSpecialProposal(param["Systematic"]["SpecialProposal"], i);
238  }
239 
240  //Fill the map to get the correlations later as well
241  CorrNamesMap[param["Systematic"]["Names"]["FancyName"].as<std::string>()]=i;
242 
243  //Also loop through the correlations
244  if(param["Systematic"]["Correlations"]) {
245  for(unsigned int Corr_i = 0; Corr_i < param["Systematic"]["Correlations"].size(); ++Corr_i){
246  for (YAML::const_iterator it = param["Systematic"]["Correlations"][Corr_i].begin(); it!=param["Systematic"]["Correlations"][Corr_i].end();++it) {
247  Correlations[i][it->first.as<std::string>()] = it->second.as<double>();
248  }
249  }
250  }
251  i++;
252  } // end loop over para
253  if(i != _fNumPar) {
254  MACH3LOG_CRITICAL("Inconsistent number of params in Yaml {} vs {}, this indicate wrong syntax", i, i, _fNumPar);
255  throw MaCh3Exception(__FILE__ , __LINE__ );
256  }
257 
258  // load correlation
259  LoadCorrelationFromConfig(Correlations, CorrNamesMap);
260  if (_fNumPar <= 0) {
261  MACH3LOG_ERROR("ParameterHandler object has {} systematics!", _fNumPar);
262  throw MaCh3Exception(__FILE__ , __LINE__ );
263  }
264 
265  for(auto const & matovr : ThrowSubMatrixOverrides){
266  SetSubThrowMatrix(matovr.first.first, matovr.first.second, *matovr.second);
267  }
268 
269  Tunes = std::make_unique<ParameterTunes>(_fYAMLDoc["Systematics"]);
270 
271  MACH3LOG_INFO("Created covariance matrix from files: ");
272  for(const auto &file : YAMLFile){
273  MACH3LOG_INFO("{} ", file);
274  }
275  MACH3LOG_INFO("----------------");
276  MACH3LOG_INFO("Found {} systematics parameters in total", _fNumPar);
277  MACH3LOG_INFO("----------------");
278 }
#define GetBounds(filename)
Definition: YamlHelper.h:590
std::unique_ptr< ParameterTunes > Tunes
Struct containing information about adaption.
void ReserveMemory(const int size)
Initialise vectors with parameters information.
void SetFixParameter(const int i)
Set parameter to be fixed at prior value.
YAML::Node _fYAMLDoc
Stores config describing systematics.
void EnableSpecialProposal(const YAML::Node &param, const int Index)
Enable special proposal.
void SetSubThrowMatrix(int first_index, int last_index, TMatrixDSym const &subcov)
std::vector< std::unique_ptr< TRandom3 > > random_number
KS: Set Random numbers for each thread so each thread has different seed.
void LoadCorrelationFromConfig(std::vector< std::map< std::string, double >> &Correlations, std::map< std::string, int > &CorrNamesMap)
Load correlation from yaml config.
void LoadAndMergeYAML(const std::vector< std::string > &YAMLFile, std::map< std::pair< int, int >, std::unique_ptr< TMatrixDSym >> &overrides)
Initialise single yaml based on several yaml files.
int GetNThreads()
number of threads which we need for example for TRandom3
Definition: Monitor.cpp:372

◆ InitParameters()

void ParameterHandlerGeneric::InitParameters ( )
protected

Initializes the systematic parameters from the configuration file. This function loads parameters like normalizations and splines from the provided YAML file.

Note
This is used internally during the object's initialization process.

Definition at line 659 of file ParameterHandlerGeneric.cpp.

659  {
660 // ********************************************
661  for (int i = 0; i < _fNumPar; ++i) {
662  //ETA - set the name to be param_% as this is what ProcessorMCMC expects
663  _fNames[i] = "param_"+std::to_string(i);
664 
665  // KS: Plenty of MaCh3 processing script rely on osc params having "fancy name" this is to maintain backward compatibility with them
666  if(_fParamType[i] == kOsc) {
667  _fNames[i] = _fFancyNames[i];
668 
669  if(_ParameterGroup[i] != "Osc"){
670  MACH3LOG_ERROR("Parameter {}, is of type Oscillation but doesn't belong to Osc group", _fFancyNames[i]);
671  MACH3LOG_ERROR("It belongs to {} group", _ParameterGroup[i]);
672  throw MaCh3Exception(__FILE__ , __LINE__ );
673  }
674  }
675  #pragma GCC diagnostic push
676  #pragma GCC diagnostic ignored "-Wuseless-cast"
677  // Set ParameterHandler parameters (Curr = current, Prop = proposed, Sigma = step)
678  _fCurrVal[i] = _fPreFitValue[i];
679  _fPropVal[i] = static_cast<M3::float_t>(_fCurrVal[i]);
680  #pragma GCC diagnostic pop
681  }
682  Randomize();
683  //KS: Transfer the starting parameters to the PCA basis, you don't want to start with zero..
684  if (pca) {
685  PCAObj->SetInitialParameters();
686  }
687 }
std::vector< M3::float_t > _fPropVal
Proposed value of the parameter.
std::unique_ptr< PCAHandler > PCAObj
Struct containing information about PCA.
std::vector< double > _fCurrVal
Current value of the parameter.
void Randomize() _noexcept_
"Randomize" the parameters in the covariance class for the proposed step. Used the proposal kernel an...
double float_t
Definition: Core.h:37

◆ InitParametersTypeFromConfig()

void ParameterHandlerGeneric::InitParametersTypeFromConfig ( )
protected

Parses the YAML configuration to set up cross-section parameters. The YAML file defines the types of systematic errors, interpolation types, and bounds for splines.

Definition at line 281 of file ParameterHandlerGeneric.cpp.

281  {
282 // ********************************************
284 
285  _fParamType = std::vector<SystType>(_fNumPar);
286  _ParameterGroup = std::vector<std::string>(_fNumPar);
287  _fSampleNames = std::vector<std::vector<std::string>>(_fNumPar);
288 
289  //KS: We know at most how params we expect so reserve memory for max possible params. Later we will shrink to size to not waste memory. Reserving means slightly faster loading and possible less memory fragmentation.
290  NormParams.reserve(_fNumPar);
291  SplineParams.reserve(_fNumPar);
292  FuncParams.reserve(_fNumPar);
293  OscParams.reserve(_fNumPar);
294 
295  int i = 0;
296  unsigned int ParamCounter[SystType::kSystTypes] = {0};
297  //ETA - read in the systematics. Would be good to add in some checks to make sure
298  //that there are the correct number of entries i.e. are the _fNumPars for Names,
299  //PreFitValues etc etc.
300  for (auto const &param : _fYAMLDoc["Systematics"])
301  {
302  _ParameterGroup[i] = Get<std::string>(param["Systematic"]["ParameterGroup"], __FILE__ , __LINE__);
303  _fSampleNames[i] = GetFromManager<std::vector<std::string>>(param["Systematic"]["SampleNames"], {}, __FILE__, __LINE__);
304 
305  //Fill the map to get the correlations later as well
306  auto ParamType = Get<std::string>(param["Systematic"]["Type"], __FILE__ , __LINE__);
307  //Now load in variables for spline systematics only
308  if (ParamType == SystType_ToString(SystType::kSpline))
309  {
310  //Set param type
312  // Fill Spline info
313  SplineParams.push_back(GetSplineParameter(param["Systematic"], i));
314 
315  //Insert the mapping from the spline index i.e. the length of _fSplineNames etc
316  //to the Systematic index i.e. the counter for things like _fSampleID
317  _fSystToGlobalSystIndexMap[SystType::kSpline].insert(std::make_pair(ParamCounter[SystType::kSpline], i));
318  ParamCounter[SystType::kSpline]++;
319  } else if(ParamType == SystType_ToString(SystType::kNorm)) {
321  NormParams.push_back(GetNormParameter(param["Systematic"], i));
322  _fSystToGlobalSystIndexMap[SystType::kNorm].insert(std::make_pair(ParamCounter[SystType::kNorm], i));
323  ParamCounter[SystType::kNorm]++;
324  } else if(ParamType == SystType_ToString(SystType::kFunc)){
326  FuncParams.push_back(GetFunctionalParameters(param["Systematic"], i));
327  _fSystToGlobalSystIndexMap[SystType::kFunc].insert(std::make_pair(ParamCounter[SystType::kFunc], i));
328  ParamCounter[SystType::kFunc]++;
329  } else if(ParamType == SystType_ToString(SystType::kOsc)){
331  OscParams.push_back(GetOscillationParameters(param["Systematic"], i));
332  _fSystToGlobalSystIndexMap[SystType::kOsc].insert(std::make_pair(ParamCounter[SystType::kOsc], i));
333  ParamCounter[SystType::kOsc]++;
334  } else{
335  MACH3LOG_ERROR("Given unrecognised systematic type: {}", ParamType);
336  std::string expectedTypes = "Expecting ";
337  for (int s = 0; s < SystType::kSystTypes; ++s) {
338  if (s > 0) expectedTypes += ", ";
339  expectedTypes += SystType_ToString(static_cast<SystType>(s)) + "\"";
340  }
341  expectedTypes += ".";
342  MACH3LOG_ERROR(expectedTypes);
343  throw MaCh3Exception(__FILE__, __LINE__);
344  }
345  i++;
346  } //end loop over params
347 
348  //KS We resized them above to all params to fight memory fragmentation, now let's resize to fit only allocated memory to save RAM
349  NormParams.shrink_to_fit();
350  SplineParams.shrink_to_fit();
351  FuncParams.shrink_to_fit();
352  OscParams.shrink_to_fit();
353 }
SystType
@ kSystTypes
This only enumerates.
FunctionalParameter GetFunctionalParameters(const YAML::Node &param, const int Index) const
Get Func params.
SplineParameter GetSplineParameter(const YAML::Node &param, const int Index) const
Get Spline params.
std::vector< OscillationParameter > OscParams
Vector containing info for functional systematics.
OscillationParameter GetOscillationParameters(const YAML::Node &param, const int Index) const
Get Osc params.
NormParameter GetNormParameter(const YAML::Node &param, const int Index) const
Get Norm params.

◆ IsParFromGroup()

bool ParameterHandlerGeneric::IsParFromGroup ( const int  i,
const std::string &  Group 
) const

Checks if parameter belongs to a given group.

Parameters
iparameter index
Groupname of group, like Xsec or Flux
Returns
bool telling whether param is part of group

Definition at line 1003 of file ParameterHandlerGeneric.cpp.

1003  {
1004 // ********************************************
1005  std::string groupLower = Group;
1006  std::string paramGroupLower = _ParameterGroup[i];
1007 
1008  // KS: Convert both strings to lowercase, this way comparison will be case insensitive
1009  std::transform(groupLower.begin(), groupLower.end(), groupLower.begin(), ::tolower);
1010  std::transform(paramGroupLower.begin(), paramGroupLower.end(), paramGroupLower.begin(), ::tolower);
1011 
1012  return groupLower == paramGroupLower;
1013 }

◆ IterateOverParams()

template<typename FilterFunc , typename ActionFunc >
void ParameterHandlerGeneric::IterateOverParams ( const std::string &  SampleName,
FilterFunc  filter,
ActionFunc  action 
) const
protected

Iterates over parameters and applies a filter and action function.

This template function provides a way to iterate over parameters associated with a specific Sample ID (SampleName). It applies a filter function to determine which parameters to process and an action function to define what to do with the selected parameters.

Template Parameters
FilterFuncThe type of the filter function used to determine which parameters to include.
ActionFuncThe type of the action function applied to each selected parameter.
Parameters
SampleNameThe Sample ID used to filter parameters.

Definition at line 649 of file ParameterHandlerGeneric.cpp.

649  {
650 // ********************************************
651  for (int i = 0; i < _fNumPar; ++i) {
652  if ((AppliesToSample(i, SampleName)) && filter(i)) { // Common filter logic
653  action(i); // Specific action for each function
654  }
655  }
656 }

◆ LoadAndMergeYAML()

void ParameterHandlerGeneric::LoadAndMergeYAML ( const std::vector< std::string > &  YAMLFile,
std::map< std::pair< int, int >, std::unique_ptr< TMatrixDSym >> &  overrides 
)
protected

Initialise single yaml based on several yaml files.

Definition at line 52 of file ParameterHandlerGeneric.cpp.

53  {
54 // ********************************************
55  int running_num_file_pars = 0;
56 
57  _fYAMLDoc["Systematics"] = YAML::Node(YAML::NodeType::Sequence);
58  for(unsigned int i = 0; i < YAMLFile.size(); i++)
59  {
60  YAML::Node YAMLDocTemp = M3OpenConfig(YAMLFile[i]);
61 
62  if (YAMLDocTemp["ThrowMatrixOverride"]) { // LP: this allows us to put in
63  // proposal matrix overrides per
64  // parameter-containing file, add
65  // the block diagonal proposal
66  // matrix to a list and overwrite
67  // the throw matrix after set up.
68  auto filename =
69  YAMLDocTemp["ThrowMatrixOverride"]["file"].as<std::string>();
70  TFile *submatrix_file = TFile::Open(filename.c_str());
71 
72  auto matrixname =
73  YAMLDocTemp["ThrowMatrixOverride"]["matrix"].as<std::string>();
74  std::unique_ptr<TMatrixDSym> submatrix{
75  submatrix_file->Get<TMatrixDSym>(matrixname.c_str())};
76  if (!submatrix) {
77  MACH3LOG_CRITICAL("Covariance matrix {} doesn't exist in file: {}",
78  matrixname, filename);
79  throw MaCh3Exception(__FILE__, __LINE__);
80  }
81  auto numrows = submatrix->GetNrows();
82  // LP: the -1 here is because we specify the last index for consistency
83  // with PCAHandler, not the first index after the end as is more common
84  // throughout computer science...
85  ThrowSubMatrixOverrides[{running_num_file_pars,
86  running_num_file_pars + (numrows - 1)}] = std::move(submatrix);
87 
88  // LP: check names by default, but have option to disable check if you
89  // know what you're doing
90  if (!bool(YAMLDocTemp["ThrowMatrixOverride"]["check_names"]) ||
91  YAMLDocTemp["ThrowMatrixOverride"]["check_names"].as<bool>()) {
92  auto nametree = submatrix_file->Get<TTree>("param_names");
93  if (!nametree) {
94  MACH3LOG_CRITICAL("TTree param_names doesn't exist in file: {}. Set "
95  "ThrowMatrixOverride: {{ check_names: False }} to "
96  "disable this check.",
97  filename);
98  throw MaCh3Exception(__FILE__, __LINE__);
99  }
100  std::string *param_name = nullptr;
101  nametree->SetBranchAddress("name", &param_name);
102 
103  if (nametree->GetEntries() != int(YAMLDocTemp["Systematics"].size())) {
104  MACH3LOG_CRITICAL("TTree param_names in file: {} has {} entries, but "
105  "the corresponding yaml file only declares {} "
106  "parameters. Set ThrowMatrixOverride: {{ "
107  "check_names: False }} to disable this check.",
108  filename, nametree->GetEntries(),
109  YAMLDocTemp["Systematics"].size());
110  throw MaCh3Exception(__FILE__, __LINE__);
111  }
112 
113  int pit = 0;
114  for (const auto &param : YAMLDocTemp["Systematics"]) {
115  nametree->GetEntry(pit++);
116  auto yaml_pname = Get<std::string>(
117  param["Systematic"]["Names"]["FancyName"], __FILE__, __LINE__);
118  if ((*param_name) != yaml_pname) {
120  "TTree param_names in file: {} at entry {} has parameter {}, "
121  "but "
122  "the corresponding yaml parameter is named {}. Set "
123  "ThrowMatrixOverride: {{ "
124  "check_names: False }} to disable this check.",
125  filename, pit, (*param_name), yaml_pname);
126  throw MaCh3Exception(__FILE__, __LINE__);
127  }
128  }
129  }
130  submatrix_file->Close();
131  }
132 
133  for (const auto& item : YAMLDocTemp["Systematics"]) {
134  _fYAMLDoc["Systematics"].push_back(item);
135  running_num_file_pars++;
136  }
137  }
138 }
#define M3OpenConfig(filename)
Macro to simplify calling LoadYaml with file and line info.
Definition: YamlHelper.h:589
TFile * Open(const std::string &Name, const std::string &Type, const std::string &File, const int Line)
Opens a ROOT file with the given name and mode.

◆ LoadCorrelationFromConfig()

void ParameterHandlerGeneric::LoadCorrelationFromConfig ( std::vector< std::map< std::string, double >> &  Correlations,
std::map< std::string, int > &  CorrNamesMap 
)
protected

Load correlation from yaml config.

Definition at line 141 of file ParameterHandlerGeneric.cpp.

142  {
143 // ********************************************
144  // ETA Now that we've been through all systematic let's fill the covmatrix
145  //This makes the root TCov from YAML
146  TMatrixDSym* _fCovMatrix = new TMatrixDSym(_fNumPar);
147  for(int j = 0; j < _fNumPar; j++) {
148  (*_fCovMatrix)(j, j) = _fError[j]*_fError[j];
149  //Get the map of parameter name to correlation from the Correlations object
150  for (auto const& pair : Correlations[j]) {
151  auto const& key = pair.first;
152  auto const& val = pair.second;
153  int index = -1;
154  //If you found the parameter name then get the index
155  if (CorrNamesMap.find(key) != CorrNamesMap.end()) {
156  index = CorrNamesMap[key];
157  } else {
158  MACH3LOG_ERROR("Parameter {} not in list! Check your spelling?", key);
159  throw MaCh3Exception(__FILE__ , __LINE__ );
160  }
161  double Corr1 = val;
162  double Corr2 = 0;
163  if(Correlations[index].find(_fFancyNames[j]) != Correlations[index].end()) {
164  Corr2 = Correlations[index][_fFancyNames[j]];
165  //Do they agree to better than float precision?
166  if(std::abs(Corr2 - Corr1) > FLT_EPSILON) {
167  MACH3LOG_ERROR("Correlations are not equal between {} and {}", _fFancyNames[j], key);
168  MACH3LOG_ERROR("Got : {} and {}", Corr2, Corr1);
169  throw MaCh3Exception(__FILE__ , __LINE__ );
170  }
171  } else {
172  MACH3LOG_ERROR("Correlation does not appear reciprocally between {} and {}", _fFancyNames[j], key);
173  throw MaCh3Exception(__FILE__ , __LINE__ );
174  }
175  (*_fCovMatrix)(j, index)= (*_fCovMatrix)(index, j) = Corr1*_fError[j]*_fError[index];
176  }
177  }
178 
179  //Now make positive definite
180  MakePosDef(_fCovMatrix);
181  SetCovMatrix(_fCovMatrix);
182 }
void SetCovMatrix(TMatrixDSym *cov)
Set covariance matrix.
void MakePosDef(TMatrixDSym *cov=nullptr, bool verbose=true)
Make matrix positive definite by adding small values to diagonal, necessary for inverting matrix.

◆ Print()

void ParameterHandlerGeneric::Print ( ) const
protected

Print information about the whole object once it is set.

Definition at line 691 of file ParameterHandlerGeneric.cpp.

691  {
692 // ********************************************
693  MACH3LOG_INFO("#################################################");
694  MACH3LOG_INFO("Printing ParameterHandlerGeneric:");
695 
697 
698  PrintNormParams();
699 
701 
703 
705 
707 
708  MACH3LOG_INFO("Finished");
709  MACH3LOG_INFO("#################################################");
710 
712 } // End
void PrintGlobablInfo() const
Prints general information about the ParameterHandler object.
void PrintSplineParams() const
Prints spline parameters.
void PrintOscillationParams() const
Prints oscillation parameters.
void PrintFunctionalParams() const
Prints functional parameters.
void CheckCorrectInitialisation() const
KS: Check if matrix is correctly initialised.
void PrintNormParams() const
Prints normalization parameters.
void PrintParameterGroups() const
Prints groups of parameters.

◆ PrintFunctionalParams()

void ParameterHandlerGeneric::PrintFunctionalParams ( ) const
protected

Prints functional parameters.

Definition at line 830 of file ParameterHandlerGeneric.cpp.

830  {
831 // ********************************************
832  MACH3LOG_INFO("Functional parameters: {}", _fSystToGlobalSystIndexMap[SystType::kFunc].size());
833  if(_fSystToGlobalSystIndexMap[SystType::kFunc].size() == 0) return;
834  MACH3LOG_INFO("┌────┬──────────┬────────────────────────────────────────┐");
835  MACH3LOG_INFO("│{0:4}│{1:10}│{2:40}│", "#", "Global #", "Name");
836  MACH3LOG_INFO("├────┼──────────┼────────────────────────────────────────┤");
837  for (auto &pair : _fSystToGlobalSystIndexMap[SystType::kFunc]) {
838  auto &FuncIndex = pair.first;
839  auto &GlobalIndex = pair.second;
840  MACH3LOG_INFO("│{0:4}│{1:<10}│{2:40}│", std::to_string(FuncIndex), GlobalIndex, GetParFancyName(GlobalIndex));
841  }
842  MACH3LOG_INFO("└────┴──────────┴────────────────────────────────────────┘");
843 }

◆ PrintGlobablInfo()

void ParameterHandlerGeneric::PrintGlobablInfo ( ) const
protected

Prints general information about the ParameterHandler object.

Definition at line 715 of file ParameterHandlerGeneric.cpp.

715  {
716 // ********************************************
717  MACH3LOG_INFO("============================================================================================================================================================");
718  MACH3LOG_INFO("{:<5} {:2} {:<40} {:2} {:<10} {:2} {:<10} {:2} {:<10} {:2} {:<10} {:2} {:<10} {:2} {:<20} {:2} {:<10}", "#", "|", "Name", "|", "Prior", "|", "Error", "|", "Lower", "|", "Upper", "|", "StepScale", "|", "SampleNames", "|", "Type");
719  MACH3LOG_INFO("------------------------------------------------------------------------------------------------------------------------------------------------------------");
720  for (int i = 0; i < GetNumParams(); i++) {
721  std::string ErrString = fmt::format("{:.2f}", _fError[i]);
722  std::string SampleNameString = "";
723  for (const auto& SampleName : _fSampleNames[i]) {
724  if (!SampleNameString.empty()) {
725  SampleNameString += ", ";
726  }
727  SampleNameString += SampleName;
728  }
729  MACH3LOG_INFO("{:<5} {:2} {:<40} {:2} {:<10} {:2} {:<10} {:2} {:<10} {:2} {:<10} {:2} {:<10} {:2} {:<20} {:2} {:<10}", i, "|", GetParFancyName(i), "|", _fPreFitValue[i], "|", "+/- " + ErrString, "|", _fLowBound[i], "|", _fUpBound[i], "|", _fIndivStepScale[i], "|", SampleNameString, "|", SystType_ToString(_fParamType[i]));
730  }
731  MACH3LOG_INFO("============================================================================================================================================================");
732 }
int GetNumParams() const
Get total number of parameters.

◆ PrintNormParams()

void ParameterHandlerGeneric::PrintNormParams ( ) const
protected

Prints normalization parameters.

Definition at line 735 of file ParameterHandlerGeneric.cpp.

735  {
736 // ********************************************
737  // Output the normalisation parameters as a sanity check!
738  MACH3LOG_INFO("Normalisation parameters: {}", NormParams.size());
739  if(_fSystToGlobalSystIndexMap[SystType::kNorm].size() == 0) return;
740 
741  bool have_parameter_with_kin_bounds = false;
742 
743  //KS: Consider making some class producing table..
744  MACH3LOG_INFO("┌────┬──────────┬────────────────────────────────────────┬────────────────────┬────────────────────┬────────────────────┐");
745  MACH3LOG_INFO("│{0:4}│{1:10}│{2:40}│{3:20}│{4:20}│{5:20}│", "#", "Global #", "Name", "Int. mode", "Target", "pdg");
746  MACH3LOG_INFO("├────┼──────────┼────────────────────────────────────────┼────────────────────┼────────────────────┼────────────────────┤");
747 
748  for (unsigned int i = 0; i < NormParams.size(); ++i)
749  {
750  std::string intModeString;
751  for (unsigned int j = 0; j < NormParams[i].modes.size(); j++) {
752  intModeString += std::to_string(NormParams[i].modes[j]);
753  intModeString += " ";
754  }
755  if (NormParams[i].modes.empty()) intModeString += "all";
756 
757  std::string targetString;
758  for (unsigned int j = 0; j < NormParams[i].targets.size(); j++) {
759  targetString += std::to_string(NormParams[i].targets[j]);
760  targetString += " ";
761  }
762  if (NormParams[i].targets.empty()) targetString += "all";
763 
764  std::string pdgString;
765  for (unsigned int j = 0; j < NormParams[i].pdgs.size(); j++) {
766  pdgString += std::to_string(NormParams[i].pdgs[j]);
767  pdgString += " ";
768  }
769  if (NormParams[i].pdgs.empty()) pdgString += "all";
770 
771  MACH3LOG_INFO("│{: <4}│{: <10}│{: <40}│{: <20}│{: <20}│{: <20}│", i, NormParams[i].index, NormParams[i].name, intModeString, targetString, pdgString);
772 
773  if(NormParams[i].hasKinBounds) have_parameter_with_kin_bounds = true;
774  }
775  MACH3LOG_INFO("└────┴──────────┴────────────────────────────────────────┴────────────────────┴────────────────────┴────────────────────┘");
776 
777  if(have_parameter_with_kin_bounds) {
778  MACH3LOG_INFO("Normalisation parameters KinematicCuts information");
779  MACH3LOG_INFO("┌────┬──────────┬────────────────────────────────────────┬────────────────────┬────────────────────────────────────────┐");
780  MACH3LOG_INFO("│{0:4}│{1:10}│{2:40}│{3:20}│{4:40}│", "#", "Global #", "Name", "KinematicCut", "Value");
781  MACH3LOG_INFO("├────┼──────────┼────────────────────────────────────────┼────────────────────┼────────────────────────────────────────┤");
782  for (unsigned int i = 0; i < NormParams.size(); ++i)
783  {
784  //skip parameters with no KinematicCuts
785  if(!NormParams[i].hasKinBounds) continue;
786 
787  const long unsigned int ncuts = NormParams[i].KinematicVarStr.size();
788  for(long unsigned int icut = 0; icut < ncuts; icut++) {
789  std::string kinematicCutValueString;
790  for(const auto & value : NormParams[i].Selection[icut]) {
791  for (const auto& v : value) {
792  kinematicCutValueString += fmt::format("{:.2f} ", v);
793  }
794  }
795  if(icut == 0)
796  MACH3LOG_INFO("│{: <4}│{: <10}│{: <40}│{: <20}│{: <40}│", i, NormParams[i].index, NormParams[i].name, NormParams[i].KinematicVarStr[icut], kinematicCutValueString);
797  else
798  MACH3LOG_INFO("│{: <4}│{: <10}│{: <40}│{: <20}│{: <40}│", "", "", "", NormParams[i].KinematicVarStr[icut], kinematicCutValueString);
799  }//icut
800  }//i
801  MACH3LOG_INFO("└────┴──────────┴────────────────────────────────────────┴────────────────────┴────────────────────────────────────────┘");
802  }
803  else
804  MACH3LOG_INFO("No normalisation parameters have KinematicCuts defined");
805 }

◆ PrintOscillationParams()

void ParameterHandlerGeneric::PrintOscillationParams ( ) const
protected

Prints oscillation parameters.

Definition at line 846 of file ParameterHandlerGeneric.cpp.

846  {
847 // ********************************************
848  MACH3LOG_INFO("Oscillation parameters: {}", _fSystToGlobalSystIndexMap[SystType::kOsc].size());
849  if(_fSystToGlobalSystIndexMap[SystType::kOsc].size() == 0) return;
850  MACH3LOG_INFO("┌────┬──────────┬────────────────────────────────────────┐");
851  MACH3LOG_INFO("│{0:4}│{1:10}│{2:40}│", "#", "Global #", "Name");
852  MACH3LOG_INFO("├────┼──────────┼────────────────────────────────────────┤");
853  for (auto &pair : _fSystToGlobalSystIndexMap[SystType::kOsc]) {
854  auto &OscIndex = pair.first;
855  auto &GlobalIndex = pair.second;
856  MACH3LOG_INFO("│{0:4}│{1:<10}│{2:40}│", std::to_string(OscIndex), GlobalIndex, GetParFancyName(GlobalIndex));
857  }
858  MACH3LOG_INFO("└────┴──────────┴────────────────────────────────────────┘");
859 }

◆ PrintParameterGroups()

void ParameterHandlerGeneric::PrintParameterGroups ( ) const
protected

Prints groups of parameters.

Definition at line 862 of file ParameterHandlerGeneric.cpp.

862  {
863 // ********************************************
864  // KS: Create a map to store the counts of unique strings, in principle this could be in header file
865  std::unordered_map<std::string, int> paramCounts;
866 
867  std::for_each(_ParameterGroup.begin(), _ParameterGroup.end(),
868  [&paramCounts](const std::string& param) {
869  paramCounts[param]++;
870  });
871 
872  MACH3LOG_INFO("Printing parameter groups");
873  // Output the counts
874  for (const auto& pair : paramCounts) {
875  MACH3LOG_INFO("Found {}: {} params", pair.second, pair.first);
876  }
877 }

◆ PrintSplineParams()

void ParameterHandlerGeneric::PrintSplineParams ( ) const
protected

Prints spline parameters.

Definition at line 808 of file ParameterHandlerGeneric.cpp.

808  {
809 // ********************************************
810  MACH3LOG_INFO("Spline parameters: {}", _fSystToGlobalSystIndexMap[SystType::kSpline].size());
811  if(_fSystToGlobalSystIndexMap[SystType::kSpline].size() == 0) return;
812  MACH3LOG_INFO("=====================================================================================================================================================================");
813  MACH3LOG_INFO("{:<4} {:<2} {:<40} {:<2} {:<40} {:<2} {:<20} {:<2} {:<20} {:<2} {:<20} {:<2}", "#", "|", "Name", "|", "Spline Name", "|", "Spline Interpolation", "|", "Low Knot Bound", "|", "Up Knot Bound", "|");
814  MACH3LOG_INFO("---------------------------------------------------------------------------------------------------------------------------------------------------------------------");
815  for (auto &pair : _fSystToGlobalSystIndexMap[SystType::kSpline]) {
816  auto &SplineIndex = pair.first;
817  auto &GlobalIndex = pair.second;
818 
819  MACH3LOG_INFO("{:<4} {:<2} {:<40} {:<2} {:<40} {:<2} {:<20} {:<2} {:<20} {:<2} {:<20} {:<2}",
820  SplineIndex, "|", GetParFancyName(GlobalIndex), "|",
821  SplineParams[SplineIndex]._fSplineNames, "|",
825  }
826  MACH3LOG_INFO("=====================================================================================================================================================================");
827 }
double GetParSplineKnotUpperBound(const int i) const
EM: value at which we cap spline knot weight.
SplineInterpolation GetParSplineInterpolation(const int i) const
Get interpolation type for a given parameter.
double GetParSplineKnotLowerBound(const int i) const
EM: value at which we cap spline knot weight.

◆ SetFixGroupOnlyParameters() [1/2]

void ParameterHandlerGeneric::SetFixGroupOnlyParameters ( const std::string &  Group)

TN Method to set parameters within a group to be fixed to their prior values.

Parameters
Groupname of the parameter group (Xsec, Flux, Osc, etc.)

Definition at line 971 of file ParameterHandlerGeneric.cpp.

971  {
972 // ********************************************
973  for (int i = 0; i < _fNumPar; ++i)
974  if(IsParFromGroup(i, Group)) SetFixParameter(i);
975 }

◆ SetFixGroupOnlyParameters() [2/2]

void ParameterHandlerGeneric::SetFixGroupOnlyParameters ( const std::vector< std::string > &  Groups)

TN Method to set parameters of certain groups to be fixed to their prior values.

Parameters
Groupsvector of group names (e.g. {"Xsec", "Flux"})

Definition at line 979 of file ParameterHandlerGeneric.cpp.

979  {
980 // ********************************************
981  for(size_t i = 0; i < Groups.size(); i++)
982  SetFixGroupOnlyParameters(Groups[i]);
983 }
void SetFixGroupOnlyParameters(const std::string &Group)
TN Method to set parameters within a group to be fixed to their prior values.

◆ SetFreeGroupOnlyParameters() [1/2]

void ParameterHandlerGeneric::SetFreeGroupOnlyParameters ( const std::string &  Group)

TN Method to set parameters within a group to be treated as free.

Parameters
Groupname of the parameter group (Xsec, Flux, Osc, etc.)

Definition at line 987 of file ParameterHandlerGeneric.cpp.

987  {
988 // ********************************************
989  for (int i = 0; i < _fNumPar; ++i)
990  if(IsParFromGroup(i, Group)) SetFreeParameter(i);
991 }
void SetFreeParameter(const int i)
Set parameter to be treated as free.

◆ SetFreeGroupOnlyParameters() [2/2]

void ParameterHandlerGeneric::SetFreeGroupOnlyParameters ( const std::vector< std::string > &  Groups)

TN Method to set parameters of certain groups to be treated as free.

Parameters
Groupsvector of group names (e.g. {"Xsec", "Flux"})

Definition at line 995 of file ParameterHandlerGeneric.cpp.

995  {
996 // ********************************************
997  for(size_t i = 0; i < Groups.size(); i++)
998  SetFreeGroupOnlyParameters(Groups[i]);
999 }
void SetFreeGroupOnlyParameters(const std::string &Group)
TN Method to set parameters within a group to be treated as free.

◆ SetGroupOnlyParameters() [1/2]

void ParameterHandlerGeneric::SetGroupOnlyParameters ( const std::string &  Group,
const std::vector< double > &  Pars = {} 
)

KS Function to set to prior parameters of a given group or values from vector.

Parameters
Groupname of group, like Xsec or Flux
ParsValues which will overwrite proposed step
Note
This mimics functionality of SetParameters

Definition at line 937 of file ParameterHandlerGeneric.cpp.

937  {
938 // ********************************************
939  #pragma GCC diagnostic push
940  #pragma GCC diagnostic ignored "-Wuseless-cast"
941  // If empty, set the proposed to prior
942  if (Pars.empty()) {
943  for (int i = 0; i < _fNumPar; i++) {
944  if(IsParFromGroup(i, Group)) _fPropVal[i] = static_cast<M3::float_t>(_fPreFitValue[i]);
945  }
946  } else{
947  const size_t ExpectedSize = static_cast<size_t>(GetNumParFromGroup(Group));
948  if (Pars.size() != ExpectedSize) {
949  MACH3LOG_ERROR("Number of param in group {} is {}, while you passed {}", Group, ExpectedSize, Pars.size());
950  throw MaCh3Exception(__FILE__, __LINE__);
951  }
952  int Counter = 0;
953  for (int i = 0; i < _fNumPar; i++) {
954  // If belongs to group set value from parsed vector, otherwise use propose value
955  if(IsParFromGroup(i, Group)){
956  _fPropVal[i] = static_cast<M3::float_t>(Pars[Counter]);
957  Counter++;
958  }
959  }
960  }
961  // And if pca make the transfer
962  if (pca) {
963  PCAObj->TransferToPCA();
964  PCAObj->TransferToParam();
965  }
966  #pragma GCC diagnostic pop
967 }
int GetNumParFromGroup(const std::string &Group) const
KS: Check how many parameters are associated with given group.

◆ SetGroupOnlyParameters() [2/2]

void ParameterHandlerGeneric::SetGroupOnlyParameters ( const std::vector< std::string > &  Groups)

KS Function to set to prior parameters of a given groups or values from vector.

Parameters
Groupsvector of group names, like Xsec or Flux

Definition at line 928 of file ParameterHandlerGeneric.cpp.

928  {
929 // ********************************************
930  for(size_t i = 0; i < Groups.size(); i++){
931  SetGroupOnlyParameters(Groups[i]);
932  }
933 }
void SetGroupOnlyParameters(const std::string &Group, const std::vector< double > &Pars={})
KS Function to set to prior parameters of a given group or values from vector.

Member Data Documentation

◆ _fParamType

std::vector<SystType> ParameterHandlerGeneric::_fParamType
protected

Type of parameter like norm, spline etc.

Definition at line 221 of file ParameterHandlerGeneric.h.

◆ _fSampleNames

std::vector<std::vector<std::string> > ParameterHandlerGeneric::_fSampleNames
protected

Tells to which samples object param should be applied.

Definition at line 218 of file ParameterHandlerGeneric.h.

◆ _fSystToGlobalSystIndexMap

std::vector<std::map<int, int> > ParameterHandlerGeneric::_fSystToGlobalSystIndexMap
protected

Map between number of given parameter type with global parameter numbering. For example 2nd norm param may be 10-th global param.

Definition at line 227 of file ParameterHandlerGeneric.h.

◆ _ParameterGroup

std::vector<std::string> ParameterHandlerGeneric::_ParameterGroup
protected

KS: Allow to group parameters for example to affect only cross-section or only flux etc.

Definition at line 224 of file ParameterHandlerGeneric.h.

◆ FuncParams

std::vector<FunctionalParameter> ParameterHandlerGeneric::FuncParams
protected

Vector containing info for functional systematics.

Definition at line 236 of file ParameterHandlerGeneric.h.

◆ NormParams

std::vector<NormParameter> ParameterHandlerGeneric::NormParams
protected

Vector containing info for normalisation systematics.

Definition at line 233 of file ParameterHandlerGeneric.h.

◆ OscParams

std::vector<OscillationParameter> ParameterHandlerGeneric::OscParams
protected

Vector containing info for functional systematics.

Definition at line 239 of file ParameterHandlerGeneric.h.

◆ SplineParams

std::vector<SplineParameter> ParameterHandlerGeneric::SplineParams
protected

Vector containing info for normalisation systematics.

Definition at line 230 of file ParameterHandlerGeneric.h.


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