Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Apps/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ foreach(app
LikelihoodScan
Variations
)

add_executable(${app} ${app}.cpp)
target_link_libraries(${app} SamplesDUNE MaCh3DUNE::All Minuit)
target_link_libraries(${app} SamplesDUNE MaCh3DUNE::All ROOT::Minuit)

add_dependencies(MaCh3DUNEApps ${app})

Expand Down
82 changes: 17 additions & 65 deletions Apps/SigmaVariation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,84 +12,36 @@
#include <TColor.h>
#include <TMath.h>

#include "Samples/MaCh3DUNEFactory.h"
#include "Samples/StructsDUNE.h"
#include "Fitters/MaCh3Factory.h"
#include "Samples/MaCh3DUNEFactory.h"

int main(int argc, char * argv[]) {
auto FitManager = MaCh3ManagerFactory(argc, argv);

//###############################################################################################################################

//DB Sigma variations in units of each parameters Sigma
std::vector<double> sigmaVariations = {-3, -1, 0, 1, 3};
auto FitManager = MaCh3ManagerFactory(argc, argv);
auto OutputFileName = FitManager->raw()["General"]["OutputFile"].as<std::string>();

//###############################################################################################################################
//####################################################################################
//Create sample handler + parameter_handler objects
auto [param_handler, samples] = MaCh3DuneFactory(FitManager);

//###############################################################################################################################
//Perform reweight and print total integral

MACH3LOG_INFO("=======================================================");
for(SampleHandlerBase* handler: samples){
handler->Reweight();
for (int iSample=0;iSample<handler->GetNSamples();iSample++) {
MACH3LOG_INFO("Event rate for {} : {:<5.2f}", handler->GetSampleTitle(iSample), handler->GetMCHist(iSample)->Integral());
}
}
MACH3LOG_INFO("=======================================================");

std::string OutputFileName = FitManager->raw()["General"]["OutputFile"].as<std::string>();
TFile* File = TFile::Open(OutputFileName.c_str(),"RECREATE");

MACH3LOG_INFO("Starting Variations for covarianceBase Object: {}",param_handler->GetName());
//###########################################################################################################
//MCMC

int nPars = param_handler->GetNumParams();
for (int iPar=0;iPar<nPars;iPar++) {
std::string ParName = param_handler->GetParFancyName(iPar);
double VarInit = param_handler->GetParPreFit(iPar);
double VarSigma = param_handler->GetDiagonalError(iPar);
auto MaCh3Fitter = std::make_unique<MR2T2>(FitManager.get());

MACH3LOG_INFO("\tParameter : {:<30} - Variations around value : {:<10.7f} , in units of 1 Sigma : {:<10.7f}",ParName,VarInit,VarSigma);
//Add systematic objects
MaCh3Fitter->AddSystObj(param_handler.get());

File->cd();
File->mkdir(ParName.c_str());
File->cd(ParName.c_str());

for (size_t iSigVar=0;iSigVar<sigmaVariations.size();iSigVar++) {
double VarVal = VarInit + sigmaVariations[iSigVar] * VarSigma;
if (VarVal < param_handler->GetLowerBound(iPar)) VarVal = param_handler->GetLowerBound(iPar);
if (VarVal > param_handler->GetUpperBound(iPar)) VarVal = param_handler->GetUpperBound(iPar);

MACH3LOG_INFO("\t\tVariation {:<5.3f} - Parameter Value : {:<10.7f}",
sigmaVariations[iSigVar], VarVal);
param_handler->SetParProp(iPar, VarVal);

for (auto handler : samples) {
for (int iSample = 0; iSample < handler->GetNSamples(); iSample++) {
std::string SampleName = handler->GetSampleTitle(iSample);

File->cd(ParName.c_str());
if (iSigVar == 0) {
File->mkdir((ParName + "/" + SampleName).c_str());
}
File->cd((ParName + "/" + SampleName).c_str());

handler->Reweight();
auto Hist = handler->GetMCHist(iSample);
MACH3LOG_INFO("\t\t\tSample : {:<30} - Integral : {:<10}", SampleName,
Hist->Integral());

Hist->Write(Form("Variation_%i", (int)iSigVar));
}
}
}
//Add samples
for(auto Sample : samples){
MaCh3Fitter->AddSampleHandler(Sample);
}

param_handler->SetParProp(iPar, VarInit);
MaCh3Fitter->RunSigmaVar();

MACH3LOG_INFO("=======================================================");
}
//Writing the memory usage at the end to eventually spot some nasty leak
MACH3LOG_WARN("\033[0;31mCurrent Total RAM usage is {:.2f} GB\033[0m", M3::Utils::getValue("VmRSS") / 1048576.0);
MACH3LOG_WARN("\033[0;31mOut of Total available RAM {:.2f} GB\033[0m", M3::Utils::getValue("MemTotal") / 1048576.0);

return 0;
}
73 changes: 40 additions & 33 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,8 @@ file(
)
include(${CMAKE_CURRENT_BINARY_DIR}/cmake/CPM.cmake)

CPMFindPackage(
NAME CMakeModules
GIT_TAG stable
GITHUB_REPOSITORY NuHepMC/CMakeModules
DOWNLOAD_ONLY
)
include(${CMakeModules_SOURCE_DIR}/NuHepMCModules.cmake)

#=================== ROOT
include(ROOT)

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

if(DEFINED ROOT_CXX_STANDARD AND ROOT_CXX_STANDARD GREATER CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD ${ROOT_CXX_STANDARD})
endif()
find_package(ROOT 6 REQUIRED)

#=================== DUNEAnaObj
if(NOT DEFINED DUNE_ANAOBJ_BRANCH)
Expand All @@ -79,22 +63,44 @@ if(MaCh3_DUNE_USE_SRProxy)
endif()

if(NOT TARGET duneanaobj::all)
cmessage(FATAL_ERROR "MaCh3 DUNE Expected dependency target: duneanaobj::all")
message(FATAL_ERROR "MaCh3 DUNE Expected dependency target: duneanaobj::all")
endif()
else()
include(fetchduneanaobj)
fetchduneanaobj(${DUNE_ANAOBJ_BRANCH})
endif()

include(FetchContent)
FetchContent_Declare(
duneanafluxtools
GIT_REPOSITORY https://github.com/DUNE/duneanafluxtools.git
GIT_TAG v0.9.0
)
# we don't need to build the caf interface
SET(duneanafluxtools_USE_DUNEANAOBJ OFF)
FetchContent_MakeAvailable(duneanafluxtools)

CPMAddPackage(
NAME duneanasel
GITHUB_REPOSITORY DUNE/duneanasel
GIT_TAG main
DOWNLOAD_ONLY YES
)
add_library(duneanasel_all INTERFACE)
target_include_directories(duneanasel_all INTERFACE ${duneanasel_SOURCE_DIR})
add_library(duneanasel::all ALIAS duneanasel_all)

################################## MaCh3 ######################################
#If MaCh3 was sourced find it, otherwise use CPM
SET(MaCh3_FOUND FALSE)
find_package(MaCh3 2.6.0 EXACT QUIET)
# don't find mach3 core from a previous build in this directory or sad times occur
LIST(APPEND CMAKE_IGNORE_PATH ${CMAKE_CURRENT_BINARY_DIR})
find_package(MaCh3 2.6.0 CONFIG EXACT QUIET)
set(MaCh3_CORE_BRANCH v2.6.0 CACHE STRING "Specify the MaCh3 core branch to use")
message(STATUS "Using MaCh3_CORE_BRANCH: ${MaCh3_CORE_BRANCH}")

if(NOT MaCh3_FOUND)
cmessage(STATUS "Didn't find MaCh3, attempting to use built in MaCh3")
message(STATUS "Didn't find MaCh3, attempting to use built in MaCh3")

if(NOT DEFINED MaCh3_GPU_ENABLED)
set(MaCh3_GPU_ENABLED ON)
Expand Down Expand Up @@ -143,7 +149,7 @@ else()
endif()

if(NOT TARGET MaCh3::All)
cmessage(FATAL_ERROR "MaCh3 DUNE Expected dependency target: MaCh3::All, did you source setup.MaCh3.sh")
message(FATAL_ERROR "MaCh3 DUNE Expected dependency target: MaCh3::All, did you source setup.MaCh3.sh")
endif()

############################ C++ Compiler ####################################
Expand All @@ -153,7 +159,7 @@ if(NOT DEFINED MACH3_CXX_STANDARD)
else()
SET(CMAKE_CXX_STANDARD ${MACH3_CXX_STANDARD})
endif()
cmessage(STATUS "CMAKE CXX Standard: ${CMAKE_CXX_STANDARD}")
message(STATUS "CMAKE CXX Standard: ${CMAKE_CXX_STANDARD}")

############################ FLAGS ####################################
add_library(DUNEMaCh3Warnings INTERFACE)
Expand Down Expand Up @@ -185,9 +191,9 @@ target_compile_options(DUNEMaCh3Warnings INTERFACE

LIST(APPEND ALL_FEATURES
)
cmessage(STATUS "MaCh3DUNE Features: ")
message(STATUS "MaCh3DUNE Features: ")
foreach(f ${ALL_FEATURES})
cmessage(STATUS " ${f}: ${MaCh3DUNE_${f}_ENABLED}")
message(STATUS " ${f}: ${MaCh3DUNE_${f}_ENABLED}")
endforeach()

#KS: Options to print dependency graph
Expand Down Expand Up @@ -217,13 +223,13 @@ set_target_properties(MaCh3DUNECompilerOptions PROPERTIES EXPORT_NAME CompilerOp
target_include_directories(MaCh3DUNECompilerOptions
INTERFACE
$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/Apps>
$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}>
$<INSTALL_INTERFACE:include>)

install(TARGETS MaCh3DUNECompilerOptions
EXPORT mach3dune-targets
LIBRARY DESTINATION lib/)

add_subdirectory(Systematics)
add_subdirectory(Splines)
add_subdirectory(Samples)
add_subdirectory(Apps)
Expand All @@ -237,41 +243,41 @@ if(NOT DEFINED MaCh3_PREFIX OR MaCh3_PREFIX STREQUAL "")
# Try to detect if MaCh3 was fetched by CPM
if(EXISTS "${CMAKE_BINARY_DIR}/_deps/mach3-src")
set(MaCh3_PREFIX "${CMAKE_BINARY_DIR}/_deps/mach3-src")
cmessage(STATUS "Detected CPM-fetched MaCh3: ${MaCh3_PREFIX}")
message(STATUS "Detected CPM-fetched MaCh3: ${MaCh3_PREFIX}")
# Try to get from MaCh3 package config
elseif(DEFINED MaCh3_DIR)
get_filename_component(MaCh3_PREFIX "${MaCh3_DIR}" DIRECTORY)
get_filename_component(MaCh3_PREFIX "${MaCh3_PREFIX}" DIRECTORY)
cmessage(STATUS "Detected installed MaCh3 from MaCh3_DIR: ${MaCh3_PREFIX}")
message(STATUS "Detected installed MaCh3 from MaCh3_DIR: ${MaCh3_PREFIX}")
# Try to get from target properties
elseif(TARGET MaCh3::All)
get_target_property(MACH3_INCLUDE_DIRS MaCh3::All INTERFACE_INCLUDE_DIRECTORIES)
if(MACH3_INCLUDE_DIRS)
list(GET MACH3_INCLUDE_DIRS 0 FIRST_INCLUDE_DIR)
get_filename_component(MaCh3_PREFIX "${FIRST_INCLUDE_DIR}" DIRECTORY)
cmessage(STATUS "Detected MaCh3 from target include dirs: ${MaCh3_PREFIX}")
message(STATUS "Detected MaCh3 from target include dirs: ${MaCh3_PREFIX}")
endif()
endif()

# Final validation
if(NOT DEFINED MaCh3_PREFIX OR MaCh3_PREFIX STREQUAL "")
cmessage(FATAL_ERROR "Could not determine MaCh3_PREFIX. Please set it manually with -DMaCh3_PREFIX=<path>")
message(FATAL_ERROR "Could not determine MaCh3_PREFIX. Please set it manually with -DMaCh3_PREFIX=<path>")
endif()

if(NOT EXISTS "${MaCh3_PREFIX}")
cmessage(FATAL_ERROR "MaCh3_PREFIX does not exist: ${MaCh3_PREFIX}")
message(FATAL_ERROR "MaCh3_PREFIX does not exist: ${MaCh3_PREFIX}")
endif()

# Cache it for use in subdirectories
set(MaCh3_PREFIX "${MaCh3_PREFIX}" CACHE PATH "MaCh3 installation or source directory" FORCE)
cmessage(STATUS "Set MaCh3_PREFIX: ${MaCh3_PREFIX}")
message(STATUS "Set MaCh3_PREFIX: ${MaCh3_PREFIX}")
endif()


if(PYTHON_ENABLED)
cmessage(STATUS "PYTHON IS ENABLED!!")
message(STATUS "PYTHON IS ENABLED!!")
set(CMAKE_INSTALL_RPATH "$ORIGIN/../lib")
cmessage(STATUS "CMAKE_INSTALL_RPATH is ${CMAKE_INSTALL_RPATH}")
message(STATUS "CMAKE_INSTALL_RPATH is ${CMAKE_INSTALL_RPATH}")
set(PYMACH3_PATH ${CMAKE_BINARY_DIR}/python/pyMaCh3)
add_subdirectory(python)
endif()
Expand All @@ -291,6 +297,7 @@ file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/output)
install(DIRECTORY Inputs DESTINATION ${CMAKE_BINARY_DIR})
install(DIRECTORY Configs DESTINATION ${CMAKE_BINARY_DIR})
install(DIRECTORY Utils DESTINATION ${CMAKE_BINARY_DIR})
install(DIRECTORY plotting DESTINATION ${CMAKE_BINARY_DIR})

install(EXPORT mach3dune-targets
FILE MaCh3DUNETargets.cmake
Expand Down
Loading
Loading