Skip to content

Commit

Permalink
My filght is delyed which can mean one thing, random commits. Impleme…
Browse files Browse the repository at this point in the history
…nt enum for syst types, this helps avoid lot's of copy pasting in cov xsec. In addition furhter cleaning
  • Loading branch information
KSkwarczynski committed Jul 12, 2024
1 parent 12559a4 commit 2f86b67
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 66 deletions.
88 changes: 31 additions & 57 deletions covariance/covarianceXsec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void covarianceXsec::InitXsecFromConfig() {
std::string ParamType = param["Systematic"]["Type"].as<std::string>();
int nFDSplines;
//Now load in variables for spline systematics only
if (ParamType.find("Spline") != std::string::npos)
if (ParamType.find(SystType_ToString(SystType::kSpline)) != std::string::npos)
{
if (param["Systematic"]["SplineInformation"]["FDSplineName"]) {
_fFDSplineNames.push_back(param["Systematic"]["SplineInformation"]["FDSplineName"].as<std::string>());
Expand Down Expand Up @@ -90,7 +90,7 @@ void covarianceXsec::InitXsecFromConfig() {
_fSplineKnotUpBound.push_back(GetFromManager<double>(param["Systematic"]["SplineInformation"]["SplineKnotUpBound"], 9999));
_fSplineKnotLowBound.push_back(GetFromManager<double>(param["Systematic"]["SplineInformation"]["SplineKnotLowBound"], -9999));

} else if(param["Systematic"]["Type"].as<std::string>() == "Norm") {
} else if(param["Systematic"]["Type"].as<std::string>() == SystType_ToString(SystType::kNorm)) {

// ETA Empty DummyVector can be used to specify no cut for mode, target and neutrino flavour
// ETA Has to be of size 0 to mean apply to all
Expand All @@ -103,15 +103,21 @@ void covarianceXsec::InitXsecFromConfig() {
_fNeutrinoFlavourUnosc.push_back(GetFromManager<std::vector<int>>(param["Systematic"]["NeutrinoFlavourUnosc"], DummyModeVec));
_fNormModes.push_back(GetFromManager<std::vector<int>>(param["Systematic"]["Mode"], DummyModeVec));
}
else if(param["Systematic"]["Type"].as<std::string>() == "Functional"){
else if(param["Systematic"]["Type"].as<std::string>() == SystType_ToString(SystType::kFunc)){
//std::cout << "Found a functional parameter!!" << std::endl;

}
else{
MACH3LOG_ERROR("Given unrecognised systematic type: {}", param["Systematic"]["Type"].as<std::string>());
MACH3LOG_ERROR("Expecting \"Norm\", \"Spline\" or \"Functional\"");
std::string expectedTypes = "Expecting ";
for (int s = 0; s < kSystTypes; ++s) {
if (s > 0) expectedTypes += ", ";
expectedTypes += SystType_ToString(static_cast<SystType>(s)) + "\"";
}
expectedTypes += ".";
MACH3LOG_ERROR(expectedTypes);
throw;
}
}

//ETA - I think this can go in the norm parameters only if statement above
int NumKinematicCuts = 0;
Expand Down Expand Up @@ -156,27 +162,10 @@ covarianceXsec::~covarianceXsec() {

}

// ********************************************
// DB Grab the Number of splines for the relevant DetID
int covarianceXsec::GetNumSplineParamsFromDetID(const int DetID) {
// ********************************************
int returnVal = 0;
for (int i = 0; i < _fNumPar; ++i) {
if ((GetParDetID(i) & DetID)) { //If parameter applies to required DetID
if (strcmp(GetParamType(i), "Spline") == 0) { //If parameter is implemented as a spline
returnVal += 1;
}
}
}

return returnVal;
}

// ********************************************
// DB Grab the Spline Names for the relevant DetID
const std::vector<std::string> covarianceXsec::GetSplineParsNamesFromDetID(const int DetID) {
// ********************************************

std::vector<std::string> returnVec;
returnVec.reserve(_fNumPar);
int nd_counter = 0;
Expand All @@ -185,22 +174,23 @@ const std::vector<std::string> covarianceXsec::GetSplineParsNamesFromDetID(const
if ((GetParDetID(i) & DetID)) { //If parameter applies to required DetID
if (strcmp(GetParamType(i), "Spline") == 0) { //If parameter is implemented as a spline

//ETA - Horrible hard-code becuase ND and FD spline names are different...
//ETA - Horrible hard-code becuase ND and FD spline names are different...
// this is only true at T2K and needs to change!
if(DetID == 1){
returnVec.push_back(_fNDSplineNames[nd_counter]);
nd_counter++;
}
else{
returnVec.push_back(GetParName(i));
}
if(DetID == 1){
returnVec.push_back(_fNDSplineNames[nd_counter]);
nd_counter++;
}
else{
returnVec.push_back(GetParName(i));
}
}
}
}
returnVec.shrink_to_fit();

return returnVec;
}

// ********************************************
// ETA - this is a complete fudge for now and is only here because on
// T2K the splines at ND280 and SK have different names...
Expand Down Expand Up @@ -292,22 +282,6 @@ const std::vector< std::vector<int> > covarianceXsec::GetSplineModeVecFromDetID(
return returnVec;
}

// ********************************************
// DB Grab the Spline Indices for the relevant DetID
const std::vector<int> covarianceXsec::GetSplineParsIndexFromDetID(const int DetID) {
// ********************************************
std::vector<int> returnVec;

for (int i = 0; i < _fNumPar; ++i) {
if ((GetParDetID(i) & DetID)) { //If parameter applies to required DetID
if (strcmp(GetParamType(i), "Spline") == 0) { //If parameter is implemented as a spline
returnVec.push_back(i);
}
}
}

return returnVec;
}

// ********************************************
// Get Norm params
Expand Down Expand Up @@ -384,14 +358,14 @@ const std::vector<XsecNorms4> covarianceXsec::GetNormParsFromDetID(const int Det


// ********************************************
// DB Grab the number of Normalisation parameters for the relevant DetID
int covarianceXsec::GetNumFuncParamsFromDetID(const int DetID) {
// DB Grab the number of parameters for the relevant DetID
int covarianceXsec::GetNumParamsFromDetID(const int DetID, const SystType Type) {
// ********************************************
int returnVal = 0;

for (int i = 0; i < _fNumPar; ++i) {
if ((GetParDetID(i) & DetID)) { //If parameter applies to required DetID
if (strcmp(GetParamType(i), "Functional") == 0) { //If parameter is implemented as a functional parameter
if (strcmp(GetParamType(i), SystType_ToString(Type).c_str()) == 0) { //If parameter is implemented as a functional parameter
returnVal += 1;
}
}
Expand All @@ -400,31 +374,31 @@ int covarianceXsec::GetNumFuncParamsFromDetID(const int DetID) {
}

// ********************************************
// DB Grab the Functional parameter names for the relevant DetID
const std::vector<std::string> covarianceXsec::GetFuncParsNamesFromDetID(const int DetID) {
// DB Grab the parameter names for the relevant DetID
const std::vector<std::string> covarianceXsec::GetParsNamesFromDetID(const int DetID, const SystType Type) {
// ********************************************
std::vector<std::string> returnVec;

for (int i = 0; i < _fNumPar; ++i) {
if ((GetParDetID(i) & DetID)) { //If parameter applies to required DetID
if (strcmp(GetParamType(i), "Functional") == 0) { //If parameter is implemented as a functional param
returnVec.push_back(GetParFancyName(i));
if (strcmp(GetParamType(i), SystType_ToString(Type).c_str()) == 0) { //If parameter is implemented as a functional param
returnVec.push_back(GetParFancyName(i));
}
}
}
return returnVec;
}

// ********************************************
// DB Grab the Functional parameter indices for the relevant DetID
const std::vector<int> covarianceXsec::GetFuncParsIndexFromDetID(const int DetID) {
// DB DB Grab the parameter indices for the relevant DetID
const std::vector<int> covarianceXsec::GetParsIndexFromDetID(const int DetID, const SystType Type) {
// ********************************************
std::vector<int> returnVec;

for (int i = 0; i < _fNumPar; ++i) {
if ((GetParDetID(i) & DetID)) { //If parameter applies to required DetID
if (strcmp(GetParamType(i), "Functional") == 0) { //If parameter is implemented as a functional param
returnVec.push_back(i);
if (strcmp(GetParamType(i), SystType_ToString(Type).c_str()) == 0) { //If parameter is implemented as a functional param
returnVec.push_back(i);
}
}
}
Expand Down
35 changes: 26 additions & 9 deletions covariance/covarianceXsec.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
class covarianceXsec : public covarianceBase {
public:
/// @brief Constructor
/// @param FileNames A vector of strings representing the YAML files used for initialisation of matrix
/// @param name Matrix name
/// @param threshold PCA threshold from 0 to 1. Default is -1 and means no PCA
/// @param FirstPCAdpar First PCA parameter that will be decomposed.
/// @param LastPCAdpar First PCA parameter that will be decomposed.
covarianceXsec(const std::vector<std::string>& FileNames, const char *name = "xsec_cov", double threshold = -1, int FirstPCAdpar = -999, int LastPCAdpar = -999);
/// @brief Destructor
~covarianceXsec();
Expand All @@ -39,20 +44,31 @@ class covarianceXsec : public covarianceBase {
inline int GetParDetID(const int i) const { return _fDetID[i];};
/// @brief ETA - just return a string of "spline", "norm" or "functional"
/// @param i parameter index
inline const char* GetParamType(const int i) const {return _fParamType[i].c_str();}
inline const char* GetParamType(const int i) const {return _fParamType[i].c_str();}

/// @brief Get interpolation type vector
inline const std::vector<SplineInterpolation>& GetSplineInterpolation() const{return _fSplineInterpolationType;}
/// @brief Get interpolation type for a given parameter
/// @param i spline parameter index, not confuse with global index
inline SplineInterpolation GetParSplineInterpolation(const int i) {return _fSplineInterpolationType.at(i);}

/// @brief EM: value at which we cap spline knot weight
/// @param i parameter index
/// @param i spline parameter index, not confuse with global index
inline double GetParSplineKnotUpperBound(const int i) {return _fSplineKnotUpBound[i];}
/// @brief EM: value at which we cap spline knot weight
/// @param i parameter index
/// @param i spline parameter index, not confuse with global index
inline double GetParSplineKnotLowerBound(const int i) {return _fSplineKnotLowBound[i];}

/// @brief DB Grab the number of parameters for the relevant DetID
/// @param Type Type of syst, for example kNorm, kSpline etc
int GetNumParamsFromDetID(const int DetID, const SystType Type);
/// @brief DB Grab the parameter names for the relevant DetID
/// @param Type Type of syst, for example kNorm, kSpline etc
const std::vector<std::string> GetParsNamesFromDetID(const int DetID, const SystType Type);
/// @brief DB Grab the parameter indices for the relevant DetID
/// @param Type Type of syst, for example kNorm, kSpline etc
const std::vector<int> GetParsIndexFromDetID(const int DetID, const SystType Type);

/// @brief DB Get spline parameters depending on given DetID
const std::vector<std::string> GetSplineParsNamesFromDetID(const int DetID);
/// @brief DB Get spline parameters depending on given DetID
Expand All @@ -64,20 +80,20 @@ class covarianceXsec : public covarianceBase {
/// @brief DB Grab the Spline Modes for the relevant DetID
const std::vector< std::vector<int> > GetSplineModeVecFromDetID(const int DetID);
/// @brief DB Grab the Spline Indices for the relevant DetID
const std::vector<int> GetSplineParsIndexFromDetID(const int DetID);
const std::vector<int> GetSplineParsIndexFromDetID(const int DetID){return GetParsIndexFromDetID(DetID, kSpline);}

/// @brief DB Grab the Number of splines for the relevant DetID
int GetNumSplineParamsFromDetID(const int DetID);
int GetNumSplineParamsFromDetID(const int DetID){return GetNumParamsFromDetID(DetID, kSpline);}

/// @brief DB Get norm/func parameters depending on given DetID
const std::vector<XsecNorms4> GetNormParsFromDetID(const int DetID);

/// @brief DB Grab the number of Normalisation parameters for the relevant DetID
int GetNumFuncParamsFromDetID(const int DetID);
int GetNumFuncParamsFromDetID(const int DetID){return GetNumParamsFromDetID(DetID, kFunc);}
/// @brief DB Grab the Functional parameter names for the relevant DetID
const std::vector<std::string> GetFuncParsNamesFromDetID(const int DetID);
const std::vector<std::string> GetFuncParsNamesFromDetID(const int DetID){return GetParsNamesFromDetID(DetID, kFunc);}
/// @brief DB Grab the Functional parameter indices for the relevant DetID
const std::vector<int> GetFuncParsIndexFromDetID(const int DetID);
const std::vector<int> GetFuncParsIndexFromDetID(const int DetID){return GetParsIndexFromDetID(DetID, kFunc);}

/// @brief KS: For most covariances nominal and fparInit (prior) are the same, however for Xsec those can be different
/// For example Sigma Var are done around nominal in ND280, no idea why though...
Expand All @@ -93,6 +109,7 @@ class covarianceXsec : public covarianceBase {
/// @param i parameter index
inline double getNominal(const int i) override { return _fPreFitValue.at(i); };
/// @brief Is parameter a flux param or not. This might become deprecated in future
/// @param i parameter index
/// @warning Will become deprecated
inline bool IsParFlux(const int i){ return isFlux[i]; }
/// @brief KS Function to set to nominal flux parameters
Expand All @@ -106,11 +123,11 @@ class covarianceXsec : public covarianceBase {
/// @brief Initialise CovarianceXsec
void initParams(const double fScale);
/// Is parameter flux or not, This might become deprecated in future
/// @warning Will become deprecated
std::vector<bool> isFlux;

/// Tells to which samples object param should be applied
std::vector<int> _fDetID;
//std::vector<std::string> _fDetString;
/// Type of parameter like norm, spline etc.
std::vector<std::string> _fParamType;

Expand Down
33 changes: 33 additions & 0 deletions samplePDF/Structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,39 @@ inline std::string SplineInterpolation_ToString(const SplineInterpolation i) {
return name;
}

/// Make an enum of systematic type recognised by covariance class
enum SystType {
kNorm,
kSpline,
kFunc,
kSystTypes //This only enumerates
};

// **************************************************
/// Convert a Syst type type to a string
inline std::string SystType_ToString(const SystType i) {
// **************************************************
std::string name = "";
switch(i) {
case kNorm:
name = "Norm";
break;
case kSpline:
name = "Spline";
break;
case kFunc:
name = "Functional";
break;
default:
std::cerr << "UNKNOWN SYST TYPE SPECIFIED!" << std::endl;
std::cerr << "You gave " << i << std::endl;
std::cerr << __FILE__ << ":" << __LINE__ << std::endl;
throw;
}
return name;
}


// ***************************
// A handy namespace for variables extraction
namespace MaCh3Utils {
Expand Down

0 comments on commit 2f86b67

Please sign in to comment.