From 932d9bc7fcf212f4fd1b5c3fa8ab2492b11e5e48 Mon Sep 17 00:00:00 2001 From: Felix Weiglhofer <weiglhofer@fias.uni-frankfurt.de> Date: Mon, 12 Jun 2023 11:59:10 +0000 Subject: [PATCH] algo: Add archive class for reconstruction results. --- algo/CMakeLists.txt | 2 ++ algo/global/Archive.cxx | 34 ++++++++++++++++++++++ algo/global/Archive.h | 59 +++++++++++++++++++++++++++++++++++++++ algo/global/Reco.cxx | 8 +++++- algo/global/Reco.h | 7 +++-- algo/global/RecoIO.cxx | 11 ++++++++ algo/global/RecoIO.h | 41 +++++++++++++++++++++++++++ reco/app/cbmreco/main.cxx | 6 +++- 8 files changed, 163 insertions(+), 5 deletions(-) create mode 100644 algo/global/Archive.cxx create mode 100644 algo/global/Archive.h create mode 100644 algo/global/RecoIO.cxx create mode 100644 algo/global/RecoIO.h diff --git a/algo/CMakeLists.txt b/algo/CMakeLists.txt index ab468ea286..e816a0cdd1 100644 --- a/algo/CMakeLists.txt +++ b/algo/CMakeLists.txt @@ -37,6 +37,8 @@ set(SRCS detectors/trd2d/UnpackTrd2d.cxx detectors/rich/RichReadoutConfig.cxx detectors/rich/UnpackRich.cxx + global/Archive.cxx + global/RecoIO.cxx global/Reco.cxx qa/DigiEventQa.cxx qa/Histo1D.cxx diff --git a/algo/global/Archive.cxx b/algo/global/Archive.cxx new file mode 100644 index 0000000000..6084543697 --- /dev/null +++ b/algo/global/Archive.cxx @@ -0,0 +1,34 @@ +/* Copyright (C) 2023 FIAS Frankfurt Institute for Advanced Studies, Frankfurt / Main + SPDX-License-Identifier: GPL-3.0-only + Authors: Felix Weiglhofer [committer] */ +#include "Archive.h" + +#include "CbmStsDigi.h" + +#include <boost/archive/binary_iarchive.hpp> +#include <boost/archive/binary_oarchive.hpp> +#include <boost/serialization/vector.hpp> + +#include <fstream> + +#include "log.hpp" + +using namespace cbm::algo; + +namespace ba = boost::archive; + +Archive::Archive(fs::path file) +{ + L_(info) << "Reading archive from " << file; + std::ifstream ifs(file.string(), std::ios::binary); + ba::binary_iarchive ia(ifs); + ia >> *this; +} + +void Archive::Store(fs::path file) const +{ + L_(info) << "Writing archive to " << file; + std::ofstream ofs(file.string(), std::ios::binary); + ba::binary_oarchive oa(ofs); + oa << *this; +} diff --git a/algo/global/Archive.h b/algo/global/Archive.h new file mode 100644 index 0000000000..4f97dc3ac9 --- /dev/null +++ b/algo/global/Archive.h @@ -0,0 +1,59 @@ +/* Copyright (C) 2023 FIAS Frankfurt Institute for Advanced Studies, Frankfurt / Main + SPDX-License-Identifier: GPL-3.0-only + Authors: Felix Weiglhofer [committer] */ +#ifndef CBM_ALGO_GLOBAL_ARCHIVE_H +#define CBM_ALGO_GLOBAL_ARCHIVE_H + +#include "MicrosliceDescriptor.hpp" + +#include <boost/serialization/access.hpp> +#include <boost/serialization/version.hpp> + +#include <vector> + +#include "RecoIO.h" +#include "compat/Filesystem.h" + +namespace cbm::algo +{ + class Archive { + public: + /** + * @brief Read Archive object from file. + */ + Archive(fs::path file); + + /** + * @brief Construct empty archive. + */ + Archive(const std::vector<fles::SubsystemIdentifier>& detectors) : fDetectors(detectors) {} + ~Archive() = default; + + /** + * @brief Store Archive object to file. + */ + void Store(fs::path file) const; + + const std::vector<fles::SubsystemIdentifier>& Detectors() const { return fDetectors; } + + std::vector<RecoIO>& TimesliceResults() { return fTimesliceResults; } + const std::vector<RecoIO>& TimesliceResults() const { return fTimesliceResults; } + + private: + std::vector<fles::SubsystemIdentifier> fDetectors; + + std::vector<RecoIO> fTimesliceResults; + + friend class boost::serialization::access; + template<class Archive> + void serialize(Archive& ar, const unsigned int /*version*/) + { + ar& fDetectors; + ar& fTimesliceResults; + } + }; +} // namespace cbm::algo + +BOOST_CLASS_VERSION(cbm::algo::Archive, 1) + +#endif diff --git a/algo/global/Reco.cxx b/algo/global/Reco.cxx index aee8e12cef..637ed89fa3 100644 --- a/algo/global/Reco.cxx +++ b/algo/global/Reco.cxx @@ -72,7 +72,8 @@ void Reco::Init(const Options& opts) xpu::push_timer("Reco"); } -void Reco::Run(const fles::Timeslice& ts) + +RecoIO Reco::Run(const fles::Timeslice& ts) { if (!fInitialized) { throw std::runtime_error("Chain not initialized"); } @@ -108,6 +109,11 @@ void Reco::Run(const fles::Timeslice& ts) PrintTimings(ts_times); + + RecoIO results; + results.StsDigis() = std::move(digis); + + return results; } void Reco::Finalize() diff --git a/algo/global/Reco.h b/algo/global/Reco.h index 662e3e762f..276bc591fb 100644 --- a/algo/global/Reco.h +++ b/algo/global/Reco.h @@ -6,6 +6,7 @@ #include <xpu/host.h> +#include "RecoIO.h" #include "SubChain.h" #include "UnpackChain.h" #include "sts/StsHitfinderChain.h" @@ -24,13 +25,13 @@ namespace cbm::algo Reco(); ~Reco(); - Reco(const Reco&) = delete; + Reco(const Reco&) = delete; Reco& operator=(const Reco&) = delete; Reco(Reco&&) = delete; - Reco& operator=(Reco&&) = delete; + Reco& operator=(Reco&&) = delete; void Init(const Options&); - void Run(const fles::Timeslice&); + RecoIO Run(const fles::Timeslice&); void Finalize(); void PrintTimings(xpu::timings&); diff --git a/algo/global/RecoIO.cxx b/algo/global/RecoIO.cxx new file mode 100644 index 0000000000..aae3469301 --- /dev/null +++ b/algo/global/RecoIO.cxx @@ -0,0 +1,11 @@ +/* Copyright (C) 2023 FIAS Frankfurt Institute for Advanced Studies, Frankfurt / Main + SPDX-License-Identifier: GPL-3.0-only + Authors: Felix Weiglhofer [committer] */ +#include "RecoIO.h" + +#include "CbmStsDigi.h" + +using namespace cbm::algo; + +RecoIO::RecoIO() {} +RecoIO::~RecoIO() {} diff --git a/algo/global/RecoIO.h b/algo/global/RecoIO.h new file mode 100644 index 0000000000..fab2837b30 --- /dev/null +++ b/algo/global/RecoIO.h @@ -0,0 +1,41 @@ +/* Copyright (C) 2023 FIAS Frankfurt Institute for Advanced Studies, Frankfurt / Main + SPDX-License-Identifier: GPL-3.0-only + Authors: Felix Weiglhofer [committer] */ +#ifndef CBM_ALGO_GLOBAL_RECOIO_H +#define CBM_ALGO_GLOBAL_RECOIO_H + +#include <boost/serialization/access.hpp> + +#include <vector> + +class CbmStsDigi; +namespace cbm::algo::sts +{ + using Digi = CbmStsDigi; +} + +namespace cbm::algo +{ + class RecoIO { + + public: + // Place ctor / dtor in cxx, so that we can forward-declare the data classes and keep this header small. + RecoIO(); + ~RecoIO(); + + std::vector<sts::Digi>& StsDigis() { return fStsDigis; } + const std::vector<sts::Digi>& StsDigis() const { return fStsDigis; } + + private: + std::vector<sts::Digi> fStsDigis; + + friend class boost::serialization::access; + template<class Archive> + void serialize(Archive& ar, const unsigned int /*version*/) + { + ar& fStsDigis; + } + }; +} // namespace cbm::algo + +#endif diff --git a/reco/app/cbmreco/main.cxx b/reco/app/cbmreco/main.cxx index 32f2b08078..63aea0df5e 100644 --- a/reco/app/cbmreco/main.cxx +++ b/reco/app/cbmreco/main.cxx @@ -8,6 +8,7 @@ #include <xpu/host.h> +#include "Archive.h" #include "BuildInfo.h" #include "Options.h" #include "Reco.h" @@ -44,6 +45,8 @@ int main(int argc, char** argv) Reco reco; reco.Init(opts); + + Archive archive(opts.Detectors()); fles::TimesliceAutoSource source {opts.InputLocator()}; int tsIdx = 0; int num_ts = opts.NumTimeslices(); @@ -53,7 +56,8 @@ int main(int argc, char** argv) tsIdx++; continue; } - reco.Run(*ts); + auto result = reco.Run(*ts); + archive.TimesliceResults().push_back(result); tsIdx++; if (num_ts > 0 && tsIdx >= num_ts) { break; } -- GitLab