Skip to content

Commit

Permalink
Merge pull request #74 from mach3-software/feature_BagofTricks
Browse files Browse the repository at this point in the history
Bag of tricks I learned from Luke
  • Loading branch information
KSkwarczynski committed Jul 12, 2024
2 parents 9644521 + 2f86b67 commit a40f944
Show file tree
Hide file tree
Showing 12 changed files with 398 additions and 405 deletions.
113 changes: 9 additions & 104 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ else()
project(MaCh3 VERSION ${MaCh3_VERSION} LANGUAGES CXX CUDA)
endif()


# Changes default install path to be a subdirectory of the build dir.
# Can set build dir at configure time with -DCMAKE_INSTALL_PREFIX=/install/path
if(CMAKE_INSTALL_PREFIX STREQUAL "" OR CMAKE_INSTALL_PREFIX STREQUAL
Expand All @@ -43,106 +42,24 @@ find_program(CMAKE_CXX_COMPILER NAMES $ENV{CXX} g++ PATHS ENV PATH NO_DEFAULT_PA

LIST(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/Modules)
################################## Dependencies ################################

include(CPM)

#Luke's handing cmake modules which Neutrino hep experiments might want
CPMFindPackage(
NAME CMakeModules
GIT_TAG stable
GITHUB_REPOSITORY NuHepMC/CMakeModules
DOWNLOAD_ONLY
)
include(${CMakeModules_SOURCE_DIR}/NuHepMCModules.cmake)
include(NuHepMCUtils)

# Check if CUDA was found
if(MaCh3_GPU_ENABLED)
include(${CMAKE_CURRENT_LIST_DIR}/cmake/Modules/CUDASetup.cmake)
endif()

include(ROOT)
if(NOT TARGET ROOT::ROOT)
cmessage(FATAL_ERROR "MaCh3 Expected dependency target: ROOT::ROOT")
endif()

if(ROOT_VERSION VERSION_LESS 6.18.0)
cmessage(FATAL_ERROR "Using ROOT version smaller than 6.18.0, this may lead to unexpected results")
endif()

#YAML for reading in config files
set(YAML_CPP_VERSION 0.7.0) #KS: We need it for version.h file also define this number olny once
set(YAML_CPP_GIT_TAG "yaml-cpp-${YAML_CPP_VERSION}")
CPMAddPackage(
NAME yaml-cpp
VERSION ${YAML_CPP_VERSION}
GITHUB_REPOSITORY "jbeder/yaml-cpp"
GIT_TAG "${YAML_CPP_GIT_TAG}"
OPTIONS
"YAML_BUILD_SHARED_LIBS ON"
)

if(NOT TARGET yaml-cpp::yaml-cpp)
cmessage(FATAL_ERROR "MaCh3 Expected dependency target: yaml-cpp::yaml-cpp")
endif()

#KS: Not being used right now so commenting it out.
#CPMAddPackage(
# NAME Eigen
# VERSION 3.2.8
# URL https://gitlab.com/libeigen/eigen/-/archive/3.2.8/eigen-3.2.8.tar.gz
# # Eigen's CMakelists are not intended for library use
# DOWNLOAD_ONLY YES
#)

#if(Eigen_ADDED)
# add_library(Eigen INTERFACE IMPORTED)
# target_include_directories(Eigen INTERFACE ${Eigen_SOURCE_DIR})
#endif()


################################## Oscillation ################################
#KS: All of these should be moved to separate cmake and be handled by osc class, keep it for now
#If USE_PROB3 not defined turn it off by default
if(NOT DEFINED USE_PROB3)
SET(USE_PROB3 FALSE)
endif()

# Oscillation calculation
# In the future which osc calc we use might be set with a flag
SET(MaCh3_Oscillator_ENABLED "")
if (USE_PROB3)
CPMFindPackage(
NAME Prob3plusplus
VERSION 3.10.3
GITHUB_REPOSITORY "mach3-software/Prob3plusplus"
GIT_TAG v3.10.3
)
LIST(APPEND MaCh3_Oscillator_ENABLED "Prob3++")
else()
CPMFindPackage(
NAME CUDAProb3
GITHUB_REPOSITORY "mach3-software/CUDAProb3"
GIT_TAG "feature_cleanup"
DOWNLOAD_ONLY YES
)
LIST(APPEND MaCh3_Oscillator_ENABLED "CUDAProb3")
endif()

#dump_cmake_variables(Prob3plusplus)
#KS: Store some handy cmake functions
include(${CMAKE_CURRENT_LIST_DIR}/cmake/Modules/MaCh3Utils.cmake)
#Loads all dependencies
include(${CMAKE_CURRENT_LIST_DIR}/cmake/Modules/MaCh3Dependencies.cmake)

############################ C++ Compiler ####################################
if (NOT DEFINED CMAKE_CXX_STANDARD OR "${CMAKE_CXX_STANDARD} " STREQUAL " ")
SET(CMAKE_CXX_STANDARD 11)
endif()

# KS: If C++ standard is lower than C++ standar used for ROOT compilation things will go terribly wrong
# KS: If C++ standard is lower than C++ standard used for ROOT compilation things will go terribly wrong
if(DEFINED ROOT_CXX_STANDARD AND ROOT_CXX_STANDARD GREATER CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD ${ROOT_CXX_STANDARD})
endif()
cmessage(STATUS "CMAKE CXX Standard: ${CMAKE_CXX_STANDARD}")

if(${ROOT_CXX_STANDARD} LESS 14)
# KS: ROOT changed cmake in 6.32, we should move avay from using Luke's hack, keep it for due to compatibility
if(ROOT_CXX_STANDARD LESS 14 AND ROOT_VERSION VERSION_LESS 6.32.00)
cmessage(WARNING "ROOT CXX STANDARD: ${ROOT_CXX_STANDARD}")
endif()

Expand Down Expand Up @@ -171,9 +88,7 @@ target_compile_options(MaCh3CompilerOptions INTERFACE
-Woverloaded-virtual
)
#KS: If Debug is not defined disable it by default
if(NOT DEFINED MaCh3_DEBUG_ENABLED)
SET(MaCh3_DEBUG_ENABLED FALSE)
endif()
DefineEnabledRequiredSwitch(MaCh3_DEBUG_ENABLED FALSE)

#If DEBUG_LEVEL was defined but MaCh3_DEBUG_ENABLED not, enable debug flag
if(DEFINED DEBUG_LEVEL)
Expand Down Expand Up @@ -205,9 +120,7 @@ else()
endif()

#KS: If multithreading is not defined enable it by default
if(NOT DEFINED MaCh3_MULTITHREAD_ENABLED)
SET(MaCh3_MULTITHREAD_ENABLED TRUE)
endif()
DefineEnabledRequiredSwitch(MaCh3_MULTITHREAD_ENABLED TRUE)

#Add Multithread flags
if(MaCh3_MULTITHREAD_ENABLED)
Expand Down Expand Up @@ -242,14 +155,6 @@ install(TARGETS MaCh3CompilerOptions
EXPORT MaCh3-targets
LIBRARY DESTINATION lib/)

#KS: Consider moving it somewhere else
SET(MaCh3_Fitter_ENABLED "MR2T2")
SET(MaCh3_MINUIT2_ENABLED FALSE)
if(ROOT_CXX_FLAGS MATCHES "-DMINUIT2_ENABLED")
LIST(APPEND MaCh3_Fitter_ENABLED " Minuit2")
SET(MaCh3_MINUIT2_ENABLED TRUE)
endif()

#Include logger
include(${CMAKE_CURRENT_LIST_DIR}/cmake/Modules/Logger.cmake)
################################# Features ##################################
Expand Down
96 changes: 96 additions & 0 deletions cmake/Modules/MaCh3Dependencies.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
include(CPM)

#Luke's handing cmake modules which Neutrino hep experiments might want
CPMFindPackage(
NAME CMakeModules
GIT_TAG stable
GITHUB_REPOSITORY NuHepMC/CMakeModules
DOWNLOAD_ONLY
)
include(${CMakeModules_SOURCE_DIR}/NuHepMCModules.cmake)
include(NuHepMCUtils)

# Check if CUDA was found
if(MaCh3_GPU_ENABLED)
include(${CMAKE_CURRENT_LIST_DIR}/cmake/Modules/CUDASetup.cmake)
endif()

include(ROOT)
if(NOT TARGET ROOT::ROOT)
cmessage(FATAL_ERROR "MaCh3 Expected dependency target: ROOT::ROOT")
endif()

if(ROOT_VERSION VERSION_LESS 6.18.00)
cmessage(FATAL_ERROR "Using ROOT version smaller than 6.18.0, this may lead to unexpected results")
endif()

# KS: Since ROOT 6.32.0 Minuit is turned on by default
SET(MaCh3_MINUIT2_ENABLED FALSE)
if(ROOT_VERSION GREATER_EQUAL 6.32.00 OR ROOT_CXX_FLAGS MATCHES "-DMINUIT2_ENABLED")
SET(MaCh3_MINUIT2_ENABLED TRUE)
endif()

#YAML for reading in config files
set(YAML_CPP_VERSION 0.7.0) #KS: We need it for version.h file also define this number olny once
set(YAML_CPP_GIT_TAG "yaml-cpp-${YAML_CPP_VERSION}")
CPMAddPackage(
NAME yaml-cpp
VERSION ${YAML_CPP_VERSION}
GITHUB_REPOSITORY "jbeder/yaml-cpp"
GIT_TAG "${YAML_CPP_GIT_TAG}"
OPTIONS
"YAML_BUILD_SHARED_LIBS ON"
)

if(NOT TARGET yaml-cpp::yaml-cpp)
cmessage(FATAL_ERROR "MaCh3 Expected dependency target: yaml-cpp::yaml-cpp")
endif()

#KS: Not being used right now so commenting it out.
#CPMAddPackage(
# NAME Eigen
# VERSION 3.2.8
# URL https://gitlab.com/libeigen/eigen/-/archive/3.2.8/eigen-3.2.8.tar.gz
# # Eigen's CMakelists are not intended for library use
# DOWNLOAD_ONLY YES
#)

#if(Eigen_ADDED)
# add_library(Eigen INTERFACE IMPORTED)
# target_include_directories(Eigen INTERFACE ${Eigen_SOURCE_DIR})
#endif()


SET(MaCh3_Fitter_ENABLED "MR2T2")
LIST(APPEND MaCh3_Fitter_ENABLED " PSO")
if(MaCh3_MINUIT2_ENABLED)
LIST(APPEND MaCh3_Fitter_ENABLED " Minuit2")
endif()

################################## Oscillation ################################
#KS: All of these should be moved to separate cmake and be handled by osc class, keep it for now
#If USE_PROB3 not defined turn it off by default
DefineEnabledRequiredSwitch(USE_PROB3 FALSE)

# Oscillation calculation
# In the future which osc calc we use might be set with a flag
SET(MaCh3_Oscillator_ENABLED "")
if (USE_PROB3)
CPMFindPackage(
NAME Prob3plusplus
VERSION 3.10.3
GITHUB_REPOSITORY "mach3-software/Prob3plusplus"
GIT_TAG v3.10.3
)
LIST(APPEND MaCh3_Oscillator_ENABLED "Prob3++")
else()
CPMFindPackage(
NAME CUDAProb3
GITHUB_REPOSITORY "mach3-software/CUDAProb3"
GIT_TAG "feature_cleanup"
DOWNLOAD_ONLY YES
)
LIST(APPEND MaCh3_Oscillator_ENABLED "CUDAProb3")
endif()

#dump_cmake_variables(Prob3plusplus)
12 changes: 12 additions & 0 deletions cmake/Modules/MaCh3Utils.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#KS: Inspired by Nuisance code
if(NOT COMMAND DefineEnabledRequiredSwitch)
function(DefineEnabledRequiredSwitch VARNAME DEFAULTVALUE)
if(NOT DEFINED ${VARNAME} OR "${${VARNAME}}x" STREQUAL "x")
SET(${VARNAME} ${DEFAULTVALUE} PARENT_SCOPE)
SET(${VARNAME} ${DEFAULTVALUE})
else()
SET(${VARNAME}_REQUIRED ${${VARNAME}} PARENT_SCOPE)
SET(${VARNAME}_REQUIRED ${${VARNAME}})
endif()
endfunction()
endif()
31 changes: 19 additions & 12 deletions covariance/covarianceBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,10 @@ void covarianceBase::ConstructPCA() {
#endif
}

void covarianceBase::init(const char *name, const char *file)
{
// ********************************************
void covarianceBase::init(const char *name, const char *file) {
// ********************************************

// Set the covariance matrix from input ROOT file (e.g. flux, ND280, NIWG)
TFile *infile = new TFile(file, "READ");
if (infile->IsZombie()) {
Expand Down Expand Up @@ -321,12 +323,13 @@ void covarianceBase::init(const char *name, const char *file)
delete infile;
}

// ********************************************
// ETA
// An init function for the YAML constructor
// All you really need from the YAML file is the number of Systematics
// Then get all the info from the YAML file in the covarianceXsec::ParseYAML function
void covarianceBase::init(const std::vector<std::string>& YAMLFile)
{
void covarianceBase::init(const std::vector<std::string>& YAMLFile) {
// ********************************************
_fYAMLDoc["Systematics"] = YAML::Node(YAML::NodeType::Sequence);
for(unsigned int i = 0; i < YAMLFile.size(); i++)
{
Expand Down Expand Up @@ -427,7 +430,7 @@ void covarianceBase::init(const std::vector<std::string>& YAMLFile)
}
else {
MACH3LOG_ERROR("Parameter {} not in list! Check your spelling?", key);
exit(5);
throw MaCh3Exception(__FILE__ , __LINE__ );
}

//
Expand All @@ -439,11 +442,11 @@ void covarianceBase::init(const std::vector<std::string>& YAMLFile)
if(std::abs(Corr2 - Corr1) > FLT_EPSILON) {
MACH3LOG_ERROR("Correlations are not equal between {} and {}", _fFancyNames[j], key);
MACH3LOG_ERROR("Got : {} and {}", Corr2, Corr1);
exit(5);
throw MaCh3Exception(__FILE__ , __LINE__ );
}
} else {
MACH3LOG_ERROR("Correlation does not appear reciprocally between {} and {}", _fFancyNames[j], key);
exit(5);
throw MaCh3Exception(__FILE__ , __LINE__ );
}
(*_fCovMatrix)(j, index)= (*_fCovMatrix)(index, j) = Corr1*_fError[j]*_fError[index];
}
Expand All @@ -470,8 +473,9 @@ void covarianceBase::init(const std::vector<std::string>& YAMLFile)
return;
}

// ********************************************
void covarianceBase::init(TMatrixDSym* covMat) {

// ********************************************
size = covMat->GetNrows();
_fNumPar = size;
InvertCovMatrix = new double*[_fNumPar]();
Expand All @@ -495,8 +499,10 @@ void covarianceBase::init(TMatrixDSym* covMat) {
MACH3LOG_INFO("Created covariance matrix named: {}", getName());
}

// ********************************************
// Set the covariance matrix for this class
void covarianceBase::setCovMatrix(TMatrixDSym *cov) {
// ********************************************
if (cov == NULL) {
MACH3LOG_ERROR("Could not find covariance matrix you provided to setCovMatrix");
MACH3LOG_ERROR("{}:{}", __FILE__, __LINE__);
Expand Down Expand Up @@ -604,8 +610,9 @@ void covarianceBase::TransferToParam() {
_fCurrVal[i] = fParCurr_vec(i);
}
}

const std::vector<double> covarianceBase::getProposed() const {
// ********************************************
std::vector<double> covarianceBase::getProposed() const {
// ********************************************
std::vector<double> props(_fNumPar);
for (int i = 0; i < _fNumPar; ++i) props[i] = _fPropVal[i];
return props;
Expand Down Expand Up @@ -1248,7 +1255,7 @@ double covarianceBase::MatrixVectorMultiSingle(double** _restrict_ matrix, const
}

// ********************************************
void covarianceBase::setIndivStepScale(std::vector<double> stepscale) {
void covarianceBase::setIndivStepScale(const std::vector<double>& stepscale) {
// ********************************************

if ((int)stepscale.size() != _fNumPar)
Expand Down Expand Up @@ -1283,7 +1290,7 @@ void covarianceBase::printIndivStepScale() {
void covarianceBase::MakePosDef(TMatrixDSym *cov) {
//DB Save original warning state and then increase it in this function to suppress 'matrix not positive definite' messages
//Means we no longer need to overload
if(cov == NULL){
if(cov == nullptr){
cov = &*covMatrix;
}

Expand Down
Loading

0 comments on commit a40f944

Please sign in to comment.