44 for (
auto const ¶m :
_fYAMLDoc[
"Systematics"])
46 _ParameterGroup[i] = Get<std::string>(param[
"Systematic"][
"ParameterGroup"], __FILE__ , __LINE__);
49 auto ParamType = Get<std::string>(param[
"Systematic"][
"Type"], __FILE__ , __LINE__);
78 MACH3LOG_ERROR(
"Given unrecognised systematic type: {}", ParamType);
79 std::string expectedTypes =
"Expecting ";
81 if (s > 0) expectedTypes +=
", ";
108 std::vector<std::string> returnVec;
110 auto &SplineIndex = pair.first;
111 auto &SystIndex = pair.second;
113 returnVec.push_back(
SplineParams[SplineIndex]._fSplineNames);
122 std::vector<SplineInterpolation> returnVec;
124 auto &SplineIndex = pair.first;
125 auto &SystIndex = pair.second;
128 returnVec.push_back(
SplineParams.at(SplineIndex)._SplineInterpolationType);
138 std::vector< std::vector<int> > returnVec;
142 auto &SplineIndex = pair.first;
143 auto &SystIndex = pair.second;
145 returnVec.push_back(
SplineParams.at(SplineIndex)._fSplineModes);
161 norm.
modes = GetFromManager<std::vector<int>>(param[
"Mode"], {}, __FILE__ , __LINE__);
162 norm.
pdgs = GetFromManager<std::vector<int>>(param[
"NeutrinoFlavour"], {}, __FILE__ , __LINE__);
163 norm.
preoscpdgs = GetFromManager<std::vector<int>>(param[
"NeutrinoFlavourUnosc"], {}, __FILE__ , __LINE__);
164 norm.
targets = GetFromManager<std::vector<int>>(param[
"TargetNuclei"], {}, __FILE__ , __LINE__);
167 MACH3LOG_ERROR(
"Normalisation Parameter {} ({}), has lower parameters bound which can go below 0 and is equal {}",
169 MACH3LOG_ERROR(
"Normalisation parameters can't go bellow 0 as this is unphysical");
183 Parameter.
index = Index;
185 int NumKinematicCuts = 0;
186 if(param[
"KinematicCuts"]) {
187 NumKinematicCuts = int(param[
"KinematicCuts"].size());
189 std::vector<std::string> TempKinematicStrings;
190 std::vector<std::vector<std::vector<double>>> TempKinematicBounds;
192 for(
int KinVar_i = 0 ; KinVar_i < NumKinematicCuts ; ++KinVar_i) {
194 for (YAML::const_iterator it = param[
"KinematicCuts"][KinVar_i].begin();it!=param[
"KinematicCuts"][KinVar_i].end();++it) {
195 TempKinematicStrings.push_back(it->first.as<std::string>());
196 TempKinematicBounds.push_back(
Get2DBounds(it->second));
198 if(TempKinematicStrings.size() == 0) {
199 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++))");
205 Parameter.
Selection = TempKinematicBounds;
210 bool HasKinBounds =
false;
223 std::vector<int> returnVec;
225 auto &SystIndex = pair.second;
227 returnVec.push_back(SystIndex);
238 std::vector<int> returnVec;
240 auto &SplineIndex = pair.first;
241 auto &systIndex = pair.second;
243 returnVec.push_back(SplineIndex);
256 auto& SplinePar = param[
"SplineInformation"];
258 if (SplinePar[
"InterpolationType"]) {
267 Spline.
_fSplineNames = SplinePar[
"SplineName"].as<std::string>();
272 MACH3LOG_WARN(
"Spline knot capping enabled with bounds [{}, {}]. For reliable fits, consider modifying the input generation instead.",
288 func.
modes = GetFromManager<std::vector<int>>(param[
"Mode"], std::vector<int>(), __FILE__ , __LINE__);
326 template<
typename ParamT>
329 std::vector<ParamT> returnVec;
330 for (
const auto& pair : indexMap) {
331 const auto& localIndex = pair.first;
332 const auto& globalIndex = pair.second;
334 returnVec.push_back(params[localIndex]);
347 [&](int) { returnVal += 1; }
356 std::vector<std::string> returnVec;
368 std::vector<int> returnVec;
371 [&](
int i) { returnVec.push_back(i); }
377 template <
typename FilterFunc,
typename ActionFunc>
380 for (
int i = 0; i <
_fNumPar; ++i) {
390 for (
int i = 0; i <
_fNumPar; ++i) {
392 _fNames[i] =
"param_"+std::to_string(i);
404 #pragma GCC diagnostic push
405 #pragma GCC diagnostic ignored "-Wuseless-cast"
409 #pragma GCC diagnostic pop
414 PCAObj->SetInitialParameters();
422 MACH3LOG_INFO(
"#################################################");
438 MACH3LOG_INFO(
"#################################################");
446 MACH3LOG_INFO(
"============================================================================================================================================================");
447 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");
448 MACH3LOG_INFO(
"------------------------------------------------------------------------------------------------------------------------------------------------------------");
450 std::string ErrString = fmt::format(
"{:.2f}",
_fError[i]);
451 std::string SampleNameString =
"";
453 if (!SampleNameString.empty()) {
454 SampleNameString +=
", ";
456 SampleNameString += SampleName;
458 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]));
460 MACH3LOG_INFO(
"============================================================================================================================================================");
470 bool have_parameter_with_kin_bounds =
false;
473 MACH3LOG_INFO(
"┌────┬──────────┬────────────────────────────────────────┬────────────────────┬────────────────────┬────────────────────┐");
474 MACH3LOG_INFO(
"│{0:4}│{1:10}│{2:40}│{3:20}│{4:20}│{5:20}│",
"#",
"Global #",
"Name",
"Int. mode",
"Target",
"pdg");
475 MACH3LOG_INFO(
"├────┼──────────┼────────────────────────────────────────┼────────────────────┼────────────────────┼────────────────────┤");
477 for (
unsigned int i = 0; i <
NormParams.size(); ++i)
479 std::string intModeString;
480 for (
unsigned int j = 0; j <
NormParams[i].modes.size(); j++) {
481 intModeString += std::to_string(
NormParams[i].modes[j]);
482 intModeString +=
" ";
484 if (
NormParams[i].modes.empty()) intModeString +=
"all";
486 std::string targetString;
487 for (
unsigned int j = 0; j <
NormParams[i].targets.size(); j++) {
488 targetString += std::to_string(
NormParams[i].targets[j]);
491 if (
NormParams[i].targets.empty()) targetString +=
"all";
493 std::string pdgString;
494 for (
unsigned int j = 0; j <
NormParams[i].pdgs.size(); j++) {
495 pdgString += std::to_string(
NormParams[i].pdgs[j]);
498 if (
NormParams[i].pdgs.empty()) pdgString +=
"all";
502 if(
NormParams[i].hasKinBounds) have_parameter_with_kin_bounds =
true;
504 MACH3LOG_INFO(
"└────┴──────────┴────────────────────────────────────────┴────────────────────┴────────────────────┴────────────────────┘");
506 if(have_parameter_with_kin_bounds) {
507 MACH3LOG_INFO(
"Normalisation parameters KinematicCuts information");
508 MACH3LOG_INFO(
"┌────┬──────────┬────────────────────────────────────────┬────────────────────┬────────────────────────────────────────┐");
509 MACH3LOG_INFO(
"│{0:4}│{1:10}│{2:40}│{3:20}│{4:40}│",
"#",
"Global #",
"Name",
"KinematicCut",
"Value");
510 MACH3LOG_INFO(
"├────┼──────────┼────────────────────────────────────────┼────────────────────┼────────────────────────────────────────┤");
511 for (
unsigned int i = 0; i <
NormParams.size(); ++i)
516 const long unsigned int ncuts =
NormParams[i].KinematicVarStr.size();
517 for(
long unsigned int icut = 0; icut < ncuts; icut++) {
518 std::string kinematicCutValueString;
519 for(
const auto & value :
NormParams[i].Selection[icut]) {
520 for (
const auto& v : value) {
521 kinematicCutValueString += fmt::format(
"{:.2f} ", v);
527 MACH3LOG_INFO(
"│{: <4}│{: <10}│{: <40}│{: <20}│{: <40}│",
"",
"",
"",
NormParams[i].KinematicVarStr[icut], kinematicCutValueString);
530 MACH3LOG_INFO(
"└────┴──────────┴────────────────────────────────────────┴────────────────────┴────────────────────────────────────────┘");
533 MACH3LOG_INFO(
"No normalisation parameters have KinematicCuts defined");
541 MACH3LOG_INFO(
"=====================================================================================================================================================================");
542 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",
"|");
543 MACH3LOG_INFO(
"---------------------------------------------------------------------------------------------------------------------------------------------------------------------");
545 auto &SplineIndex = pair.first;
546 auto &GlobalIndex = pair.second;
548 MACH3LOG_INFO(
"{:<4} {:<2} {:<40} {:<2} {:<40} {:<2} {:<20} {:<2} {:<20} {:<2} {:<20} {:<2}",
555 MACH3LOG_INFO(
"=====================================================================================================================================================================");
563 MACH3LOG_INFO(
"┌────┬──────────┬────────────────────────────────────────┐");
564 MACH3LOG_INFO(
"│{0:4}│{1:10}│{2:40}│",
"#",
"Global #",
"Name");
565 MACH3LOG_INFO(
"├────┼──────────┼────────────────────────────────────────┤");
567 auto &FuncIndex = pair.first;
568 auto &GlobalIndex = pair.second;
571 MACH3LOG_INFO(
"└────┴──────────┴────────────────────────────────────────┘");
579 MACH3LOG_INFO(
"┌────┬──────────┬────────────────────────────────────────┐");
580 MACH3LOG_INFO(
"│{0:4}│{1:10}│{2:40}│",
"#",
"Global #",
"Name");
581 MACH3LOG_INFO(
"├────┼──────────┼────────────────────────────────────────┤");
583 auto &OscIndex = pair.first;
584 auto &GlobalIndex = pair.second;
587 MACH3LOG_INFO(
"└────┴──────────┴────────────────────────────────────────┘");
594 std::unordered_map<std::string, int> paramCounts;
597 [¶mCounts](
const std::string& param) {
598 paramCounts[param]++;
603 for (
const auto& pair : paramCounts) {
604 MACH3LOG_INFO(
"Found {}: {} params", pair.second, pair.first);
611 std::unordered_set<std::string> uniqueGroups;
615 uniqueGroups.insert(param);
619 std::vector<std::string> result(uniqueGroups.begin(), uniqueGroups.end());
628 auto CheckForDuplicates = [](
const std::vector<std::string>& names,
const std::string& nameType,
bool Warning) {
629 std::unordered_map<std::string, size_t> seenStrings;
630 for (
size_t i = 0; i < names.size(); ++i) {
631 const auto& name = names[i];
632 if (seenStrings.find(name) != seenStrings.end()) {
633 size_t firstIndex = seenStrings[name];
635 MACH3LOG_WARN(
"There are two systematics with the same {} '{}', first at index {}, and again at index {}", nameType, name, firstIndex, i);
639 MACH3LOG_CRITICAL(
"There are two systematics with the same {} '{}', first at index {}, and again at index {}", nameType, name, firstIndex, i);
643 seenStrings[name] = i;
646 std::vector<std::string> SplineNameTemp(
SplineParams.size());
651 CheckForDuplicates(
_fFancyNames,
"_fFancyNames",
false);
652 CheckForDuplicates(SplineNameTemp,
"_fSplineNames",
true);
659 for(
size_t i = 0; i < Groups.size(); i++){
668 #pragma GCC diagnostic push
669 #pragma GCC diagnostic ignored "-Wuseless-cast"
672 for (
int i = 0; i <
_fNumPar; i++) {
677 if (Pars.size() != ExpectedSize) {
678 MACH3LOG_ERROR(
"Number of param in group {} is {}, while you passed {}", Group, ExpectedSize, Pars.size());
682 for (
int i = 0; i <
_fNumPar; i++) {
693 PCAObj->TransferToParam();
695 #pragma GCC diagnostic pop
710 for(
size_t i = 0; i < Groups.size(); i++)
726 for(
size_t i = 0; i < Groups.size(); i++)
742 for(
size_t i = 0; i < Groups.size(); i++)
750 std::string groupLower = Group;
754 std::transform(groupLower.begin(), groupLower.end(), groupLower.begin(), ::tolower);
755 std::transform(paramGroupLower.begin(), paramGroupLower.end(), paramGroupLower.begin(), ::tolower);
757 return groupLower == paramGroupLower;
764 for (
int i = 0; i <
_fNumPar; i++) {
774 std::vector<const M3::float_t*> returnVec;
776 const auto& globalIndex = pair.second;
#define MACH3LOG_CRITICAL
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.
std::string SystType_ToString(const SystType i)
Convert a Syst type type to a string.
@ kNorm
For normalisation parameters.
@ kSpline
For splined parameters (1D)
@ kSystTypes
This only enumerates.
@ kOsc
For oscillation parameters.
@ kFunc
For functional parameters.
#define Get2DBounds(filename)
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.
Custom exception class used throughout MaCh3.
Base class responsible for handling of systematic error parameters. Capable of using PCA or using ada...
int GetNumParams() const
Get total number of parameters.
void ToggleFixParameter(const int i)
Toggle fixing parameter at prior values.
bool AppliesToSample(const int SystIndex, const std::string &SampleName) const
Check if parameter is affecting given sample name.
std::vector< M3::float_t > _fPropVal
Proposed value of the parameter.
const M3::float_t * RetPointer(const int iParam)
DB Pointer return to param position.
std::vector< std::string > _fFancyNames
Fancy name for example rather than param_0 it is MAQE, useful for human reading.
std::vector< bool > _fFlatPrior
Whether to apply flat prior or not.
std::string GetParFancyName(const int i) const
Get fancy name of the Parameter.
std::vector< double > _fError
Prior error on the parameter.
TH2D * GetCorrelationMatrix()
KS: Convert covariance matrix to correlation matrix and return TH2D which can be used for fancy plott...
void SetFixParameter(const int i)
Set parameter to be fixed at prior value.
YAML::Node _fYAMLDoc
Stores config describing systematics.
std::unique_ptr< PCAHandler > PCAObj
Struct containing information about PCA.
int _fNumPar
Number of systematic parameters.
std::vector< double > _fLowBound
Lowest physical bound, parameter will not be able to go beyond it.
std::vector< double > _fCurrVal
Current value of the parameter.
TMatrixDSym * covMatrix
The covariance matrix.
std::vector< double > _fPreFitValue
Parameter value dictated by the prior model. Based on it penalty term is calculated.
void Randomize() _noexcept_
"Randomize" the parameters in the covariance class for the proposed step. Used the proposal kernel an...
std::vector< std::string > _fNames
ETA _fNames is set automatically in the covariance class to be something like param_i,...
void SetFreeParameter(const int i)
Set parameter to be treated as free.
std::vector< double > _fUpBound
Upper physical bound, parameter will not be able to go beyond it.
bool pca
perform PCA or not
std::vector< double > _fIndivStepScale
Individual step scale used by MCMC algorithm.
int PrintLength
KS: This is used when printing parameters, sometimes we have super long parameters name,...
std::vector< std::vector< std::string > > _fSampleNames
Tells to which samples object param should be applied.
void PrintGlobablInfo() const
Prints general information about the ParameterHandler object.
void Print() const
Print information about the whole object once it is set.
FunctionalParameter GetFunctionalParameters(const YAML::Node ¶m, const int Index)
Get Func params.
std::vector< SplineParameter > SplineParams
Vector containing info for normalisation systematics.
SplineParameter GetSplineParameter(const YAML::Node ¶m, const int Index)
Get Spline params.
const std::vector< NormParameter > GetNormParsFromSampleName(const std::string &SampleName) const
DB Get norm/func parameters depending on given SampleName.
std::vector< std::string > GetUniqueParameterGroups() const
KS: Get names of all unique parameter groups.
ParameterHandlerGeneric(const std::vector< std::string > &FileNames, std::string name="xsec_cov", double threshold=-1, int FirstPCAdpar=-999, int LastPCAdpar=-999)
Constructor.
void PrintSplineParams() const
Prints spline parameters.
SystType GetParamType(const int i) const
Returns enum describing our param type.
void ToggleFixGroupOnlyParameters(const std::string &Group)
TN Method to toggle fix/free parameters within a group.
const std::vector< int > GetSystIndexFromSampleName(const std::string &SampleName, const SystType Type) const
Grab the index of the syst relative to global numbering.
std::vector< const M3::float_t * > GetOscParsFromSampleName(const std::string &SampleName)
Get pointers to Osc params from Sample name.
void InitParams()
Initializes the systematic parameters from the configuration file. This function loads parameters lik...
const std::vector< int > GetGlobalSystIndexFromSampleName(const std::string &SampleName, const SystType Type)
DB Get spline parameters depending on given SampleName.
std::vector< NormParameter > NormParams
Vector containing info for normalisation systematics.
double GetParSplineKnotUpperBound(const int i) const
EM: value at which we cap spline knot weight.
std::vector< std::map< int, int > > _fSystToGlobalSystIndexMap
Map between number of given parameter type with global parameter numbering. For example 2nd norm para...
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.
std::vector< SystType > _fParamType
Type of parameter like norm, spline etc.
const std::vector< int > GetParsIndexFromSampleName(const std::string &SampleName, const SystType Type)
DB Grab the parameter indices for the relevant SampleName.
const std::vector< SplineParameter > GetSplineParsFromSampleName(const std::string &SampleName) const
KS: Grab the Spline parameters for the relevant SampleName.
void PrintOscillationParams() const
Prints oscillation parameters.
const std::vector< FunctionalParameter > GetFunctionalParametersFromSampleName(const std::string &SampleName) const
HH Get functional parameters for the relevant SampleName.
void SetFreeGroupOnlyParameters(const std::string &Group)
TN Method to set parameters within a group to be treated as free.
NormParameter GetNormParameter(const YAML::Node ¶m, const int Index)
Get Norm params.
void InitParametersTypeFromConfig()
Parses the YAML configuration to set up cross-section parameters. The YAML file defines the types of ...
std::vector< ParamT > GetTypeParamsFromSampleName(const std::map< int, int > &indexMap, const std::vector< ParamT > ¶ms, const std::string &SampleName) const
Retrieve parameters that apply to a given sample name.
int GetNumParamsFromSampleName(const std::string &SampleName, const SystType Type)
DB Grab the number of parameters for the relevant SampleName.
const std::vector< std::string > GetParsNamesFromSampleName(const std::string &SampleName, const SystType Type)
DB Grab the parameter names for the relevant SampleName.
std::vector< FunctionalParameter > FuncParams
Vector containing info for functional systematics.
void PrintFunctionalParams() const
Prints functional parameters.
std::vector< OscillationParameter > OscParams
Vector containing info for functional systematics.
void DumpMatrixToFile(const std::string &Name)
Dump Matrix to ROOT file, useful when we need to pass matrix info to another fitting group.
SplineInterpolation GetParSplineInterpolation(const int i) const
Get interpolation type for a given parameter.
const std::vector< std::string > GetSplineParsNamesFromSampleName(const std::string &SampleName)
DB Get spline parameters depending on given SampleName.
OscillationParameter GetOscillationParameters(const YAML::Node ¶m, const int Index)
Get Osc params.
bool IsParFromGroup(const int i, const std::string &Group) const
Checks if parameter belongs to a given group.
void CheckCorrectInitialisation() const
KS: Check if matrix is correctly initialised.
void SetFixGroupOnlyParameters(const std::string &Group)
TN Method to set parameters within a group to be fixed to their prior values.
void IterateOverParams(const std::string &SampleName, FilterFunc filter, ActionFunc action)
Iterates over parameters and applies a filter and action function.
void GetBaseParameter(const YAML::Node ¶m, const int Index, TypeParameterBase &Parameter)
Fill base parameters.
std::vector< std::string > _ParameterGroup
KS: Allow to group parameters for example to affect only cross-section or only flux etc.
const std::vector< SplineInterpolation > GetSplineInterpolationFromSampleName(const std::string &SampleName)
Get the interpolation types for splines affecting a particular SampleName.
double GetParSplineKnotLowerBound(const int i) const
EM: value at which we cap spline knot weight.
void PrintNormParams() const
Prints normalization parameters.
const std::vector< std::vector< int > > GetSplineModeVecFromSampleName(const std::string &SampleName)
DB Grab the Spline Modes for the relevant SampleName.
int GetNumParFromGroup(const std::string &Group) const
KS: Check how many parameters are associated with given group.
void PrintParameterGroups() const
Prints groups of parameters.
~ParameterHandlerGeneric()
Destructor.
constexpr static const double DefSplineKnotUpBound
Default value for spline knot capping, default mean not capping is being applied.
constexpr static const double DefSplineKnotLowBound
Default value for spline knot capping, default mean not capping is being applied.
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.
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.
const M3::float_t * valuePtr
Parameter value pointer.
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.
KS: Struct holding info about oscillation Systematics.
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),.
Base class storing info for parameters types, helping unify codebase.
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