From 1c14b05d6925fd858f18ad88d9ba6385198d8f5b Mon Sep 17 00:00:00 2001 From: Maximilian Reininghaus <maximilian.reininghaus@tu-dortmund.de> Date: Fri, 22 Jan 2021 14:26:06 +0100 Subject: [PATCH] added flag for overwriting; refactored InteractionHistogram --- .../framework/process/InteractionHistogram.inl | 8 -------- .../framework/utility/SaveBoostHistogram.inl | 14 +++++++++++++- corsika/framework/process/InteractionHistogram.hpp | 3 --- corsika/framework/utility/SaveBoostHistogram.hpp | 2 +- examples/em_shower.cpp | 5 +++-- examples/hybrid_MC.cpp | 8 +++----- examples/vertical_EAS.cpp | 5 +++-- tests/framework/testInteractionCounter.cpp | 8 ++++---- tests/framework/testSaveBoostHistogram.cpp | 3 ++- 9 files changed, 29 insertions(+), 27 deletions(-) diff --git a/corsika/detail/framework/process/InteractionHistogram.inl b/corsika/detail/framework/process/InteractionHistogram.inl index 7bf0d5e58..e6cfae869 100644 --- a/corsika/detail/framework/process/InteractionHistogram.inl +++ b/corsika/detail/framework/process/InteractionHistogram.inl @@ -42,14 +42,6 @@ namespace corsika { } } - void InteractionHistogram::saveLab(std::string const& filename) const { - corsika::save_hist(inthist_lab_, filename); - } - - void InteractionHistogram::saveCMS(std::string const& filename) const { - corsika::save_hist(inthist_cms_, filename); - } - InteractionHistogram& InteractionHistogram::operator+=( InteractionHistogram const& other) { inthist_lab_ += other.inthist_lab_; diff --git a/corsika/detail/framework/utility/SaveBoostHistogram.inl b/corsika/detail/framework/utility/SaveBoostHistogram.inl index c934bede3..bfb2d71fd 100644 --- a/corsika/detail/framework/utility/SaveBoostHistogram.inl +++ b/corsika/detail/framework/utility/SaveBoostHistogram.inl @@ -16,6 +16,7 @@ #include <memory> #include <numeric> #include <utility> +#include <filesystem> #include <vector> #include <string> @@ -23,7 +24,18 @@ namespace corsika { template <class Axes, class Storage> inline void save_hist(boost::histogram::histogram<Axes, Storage> const& h, - std::string const& filename) { + std::string const& filename, bool overwrite) { + if (std::filesystem::status(filename).type() != + std::filesystem::file_type::not_found) { + if (overwrite) { + std::filesystem::remove(filename); + } else { + using namespace std::literals; + throw std::runtime_error( + ("save_hist(): "s + filename + " already exists"s).c_str()); + } + } + unsigned const rank = h.rank(); std::vector<size_t> axes_dims; diff --git a/corsika/framework/process/InteractionHistogram.hpp b/corsika/framework/process/InteractionHistogram.hpp index 811fcd944..ab0fee8fd 100644 --- a/corsika/framework/process/InteractionHistogram.hpp +++ b/corsika/framework/process/InteractionHistogram.hpp @@ -43,9 +43,6 @@ namespace corsika { hist_type const& CMSHist() const { return inthist_cms_; } hist_type const& labHist() const { return inthist_lab_; } - void saveLab(std::string const& filename) const; - void saveCMS(std::string const& filename) const; - InteractionHistogram& operator+=(InteractionHistogram const& other); InteractionHistogram operator+(InteractionHistogram other) const; }; diff --git a/corsika/framework/utility/SaveBoostHistogram.hpp b/corsika/framework/utility/SaveBoostHistogram.hpp index ab418e1b2..c99ab54bb 100644 --- a/corsika/framework/utility/SaveBoostHistogram.hpp +++ b/corsika/framework/utility/SaveBoostHistogram.hpp @@ -22,7 +22,7 @@ namespace corsika { */ template <class Axes, class Storage> inline void save_hist(boost::histogram::histogram<Axes, Storage> const& h, - std::string const& filename); + std::string const& filename, bool overwrite = true); } // namespace corsika #include <corsika/detail/framework/utility/SaveBoostHistogram.inl> diff --git a/examples/em_shower.cpp b/examples/em_shower.cpp index 855fc5a38..edb377953 100644 --- a/examples/em_shower.cpp +++ b/examples/em_shower.cpp @@ -7,6 +7,7 @@ */ #include <corsika/framework/core/Cascade.hpp> +#include <corsika/framework/utility/SaveBoostHistogram.hpp> #include <corsika/framework/geometry/Plane.hpp> #include <corsika/framework/geometry/Sphere.hpp> #include <corsika/framework/geometry/PhysicalGeometry.hpp> @@ -180,7 +181,7 @@ int main(int argc, char** argv) { em_continuous.reset(); auto const hists = proposalCounted.getHistogram(); - hists.saveLab("inthist_lab_emShower.npz"); - hists.saveCMS("inthist_cms_emShower.npz"); + save_hist(hists.labHist(), "inthist_lab_emShower.npz", true); + save_hist(hists.CMSHist(), "inthist_cms_emShower.npz", true); longprof.save("longprof_emShower.txt"); } diff --git a/examples/hybrid_MC.cpp b/examples/hybrid_MC.cpp index b4eab698c..71d8cf828 100644 --- a/examples/hybrid_MC.cpp +++ b/examples/hybrid_MC.cpp @@ -13,6 +13,7 @@ #include <corsika/framework/process/InteractionCounter.hpp> /* clang-format on */ #include <corsika/framework/geometry/Plane.hpp> +#include <corsika/framework/utility/SaveBoostHistogram.hpp> #include <corsika/framework/geometry/Sphere.hpp> #include <corsika/framework/core/Logging.hpp> #include <corsika/framework/process/ProcessSequence.hpp> @@ -267,11 +268,8 @@ int main(int argc, char** argv) { auto const hists = sibyllCounted.getHistogram() + sibyllNucCounted.getHistogram() + urqmdCounted.getHistogram(); - hists.saveLab("inthist_lab.txt"); - hists.saveCMS("inthist_cms.txt"); - - hists.saveLab("inthist_lab.txt"); - hists.saveCMS("inthist_cms.txt"); + save_hist(hists.labHist(), "inthist_lab_hybrid.npz", true); + save_hist(hists.CMSHist(), "inthist_cms_hybrid.npz", true); longprof.save("longprof.txt"); diff --git a/examples/vertical_EAS.cpp b/examples/vertical_EAS.cpp index 7d0a97d06..28488a1bd 100644 --- a/examples/vertical_EAS.cpp +++ b/examples/vertical_EAS.cpp @@ -15,6 +15,7 @@ #include <corsika/framework/geometry/Plane.hpp> #include <corsika/framework/geometry/Sphere.hpp> #include <corsika/framework/core/Logging.hpp> +#include <corsika/framework/utility/SaveBoostHistogram.hpp> #include <corsika/framework/process/ProcessSequence.hpp> #include <corsika/framework/process/SwitchProcessSequence.hpp> #include <corsika/framework/process/InteractionCounter.hpp> @@ -281,7 +282,7 @@ int main(int argc, char** argv) { auto const hists = sibyllCounted.getHistogram() + sibyllNucCounted.getHistogram() + urqmdCounted.getHistogram() + proposalCounted.getHistogram(); - hists.saveLab("inthist_lab_verticalEAS.npz"); - hists.saveCMS("inthist_cms_verticalEAS.npz"); + save_hist(hists.labHist(), "inthist_lab_verticalEAS.npz", true); + save_hist(hists.CMSHist(), "inthist_cms_verticalEAS.npz", true); longprof.save("longprof_verticalEAS.txt"); } diff --git a/tests/framework/testInteractionCounter.cpp b/tests/framework/testInteractionCounter.cpp index d4ba7df8a..bf73fc578 100644 --- a/tests/framework/testInteractionCounter.cpp +++ b/tests/framework/testInteractionCounter.cpp @@ -74,10 +74,10 @@ TEST_CASE("InteractionCounter", "[process]") { CHECK(h2.at(h2.axis(0).index(1'000'070'140), h2.axis(1).index(1.6e12)) == 1); CHECK(std::accumulate(h2.cbegin(), h2.cend(), 0) == 1); - std::remove("testInteractionCounter_file1.npz"); - std::remove("testInteractionCounter_file2.npz"); - countedProcess.getHistogram().saveLab("testInteractionCounter_file1.npz"); - countedProcess.getHistogram().saveCMS("testInteractionCounter_file2.npz"); + save_hist(countedProcess.getHistogram().labHist(), "testInteractionCounter_file1.npz", + true); + save_hist(countedProcess.getHistogram().CMSHist(), "testInteractionCounter_file2.npz", + true); SECTION("output validation") { auto const file = GENERATE(as<std::string>{}, "testInteractionCounter_file1", diff --git a/tests/framework/testSaveBoostHistogram.cpp b/tests/framework/testSaveBoostHistogram.cpp index 91bac8b82..d5c16438a 100644 --- a/tests/framework/testSaveBoostHistogram.cpp +++ b/tests/framework/testSaveBoostHistogram.cpp @@ -40,5 +40,6 @@ TEST_CASE("SaveHistogram") { h(a, b, c, d); } - corsika::save_hist(h, "hist.npz"); + REQUIRE_NOTHROW(corsika::save_hist(h, "hist.npz", true)); + REQUIRE_THROWS(corsika::save_hist(h, "hist.npz", false)); } -- GitLab