diff --git a/Detectors/Base/CMakeLists.txt b/Detectors/Base/CMakeLists.txt index 51047840e171e..40e89cb0228e5 100644 --- a/Detectors/Base/CMakeLists.txt +++ b/Detectors/Base/CMakeLists.txt @@ -98,6 +98,13 @@ o2_add_test( PUBLIC_LINK_LIBRARIES O2::DetectorsBase LABELS detectorsbase) +o2_add_test( + Aligner + SOURCES test/testAligner.cxx + COMPONENT_NAME DetectorsBase + PUBLIC_LINK_LIBRARIES O2::DetectorsBase + LABELS detectorsbase) + o2_add_test( DetImplClone SOURCES test/testDetImplClone.cxx diff --git a/Detectors/Base/README.md b/Detectors/Base/README.md index 723d28d57088d..f9614b997a8b5 100644 --- a/Detectors/Base/README.md +++ b/Detectors/Base/README.md @@ -96,5 +96,7 @@ It allows to set the following parameters: * `align-geom.mTimeStamp : the time-stamp for CCDB query. If it is 0 (default) then the time-stamp = `now()` in milliseconds will be used. +* `align-geom.mAlignPaths : Comma separated DET:path list of alternative alignment objects, e.g. "MCH:MCH/MisCalib/Align". A detector not listed uses /Calib/Align, as does a listed one whose alternative object cannot be fetched.` + `TODO`: once the framework will provide CCDB service, the helper will be adapted to retrieve the alignment objects via query to DPL. diff --git a/Detectors/Base/include/DetectorsBase/Aligner.h b/Detectors/Base/include/DetectorsBase/Aligner.h index 12eb7dfee003c..c3d550c5b249b 100644 --- a/Detectors/Base/include/DetectorsBase/Aligner.h +++ b/Detectors/Base/include/DetectorsBase/Aligner.h @@ -29,6 +29,9 @@ class Aligner : public o2::conf::ConfigurableParamHelper { public: const std::string& getDetectors() const { return mDetectors; } + const std::string& getAlignPaths() const { return mAlignPaths; } + std::string getAlignmentPath(o2::detectors::DetID id) const; + static std::string getAlignmentPath(const std::string& alignPaths, o2::detectors::DetID id); long getTimeStamp() const; o2::detectors::DetID::mask_t getDetectorsMask() const; @@ -38,6 +41,7 @@ class Aligner : public o2::conf::ConfigurableParamHelper private: std::string mDetectors = "all"; // comma-separated list of modules to align, "all" or "none" long mTimeStamp = 0; // assigned TimeStamp or now() if 0 + std::string mAlignPaths = ""; // comma-separated DET:path alternative alignment objects, e.g. "MCH:MCH/MisCalib/Align" O2ParamDef(Aligner, "align-geom"); }; diff --git a/Detectors/Base/src/Aligner.cxx b/Detectors/Base/src/Aligner.cxx index 70f5f9f96a453..ca89a443d1107 100644 --- a/Detectors/Base/src/Aligner.cxx +++ b/Detectors/Base/src/Aligner.cxx @@ -14,6 +14,7 @@ #include "DetectorsBase/GeometryManager.h" #include "DetectorsCommonDataFormats/AlignParam.h" #include "DetectorsCommonDataFormats/DetectorNameConf.h" +#include "CommonUtils/StringUtils.h" #include O2ParamImpl(o2::base::Aligner); @@ -53,7 +54,24 @@ void Aligner::applyAlignment(long timestamp, DetID::mask_t addMask, bool keepPhy continue; } std::string path = o2::base::DetectorNameConf::getAlignmentPath({id}); - auto algV = ccdbmgr.get>(path); + std::vector* algV = nullptr; + auto altPath = getAlignmentPath({id}); + if (altPath != path) { + // an alternative object may not exist for every timestamp: fall back to the standard one + bool fatalWhenNull = ccdbmgr.getFatalWhenNull(); + ccdbmgr.setFatalWhenNull(false); + algV = ccdbmgr.get>(altPath); + ccdbmgr.setFatalWhenNull(fatalWhenNull); + if (algV) { + LOGP(info, "Using alignment {} for {}", altPath, DetID::getName(id)); + path = altPath; + } else { + LOGP(warning, "Alignment {} could not be fetched for timestamp {}, using {} for {}", altPath, timestamp, path, DetID::getName(id)); + } + } + if (!algV) { + algV = ccdbmgr.get>(path); + } if (!algV) { throw std::runtime_error(fmt::format("Failed to fetch alignment from {}:{}", o2::base::NameConf::getCCDBServer(), path)); } @@ -75,6 +93,39 @@ void Aligner::applyAlignment(long timestamp, DetID::mask_t addMask, bool keepPhy gGeoManager->RefreshPhysicalNodes(false); } +std::string Aligner::getAlignmentPath(DetID id) const +{ + return getAlignmentPath(mAlignPaths, id); +} + +std::string Aligner::getAlignmentPath(const std::string& alignPaths, DetID id) +{ + std::string path = o2::base::DetectorNameConf::getAlignmentPath(id); + DetID::mask_t seen; + for (const auto& entry : o2::utils::Str::tokenize(alignPaths, ',')) { + auto sep = entry.find(':'); + auto det = entry.substr(0, sep); + auto altPath = sep == std::string::npos ? std::string{} : entry.substr(sep + 1); + o2::utils::Str::trim(det); + o2::utils::Str::trim(altPath); + if (det.empty() || altPath.empty()) { + throw std::runtime_error(fmt::format("Malformed align-geom.mAlignPaths entry \"{}\", expected DET:path", entry)); + } + int detID = DetID::nameToID(det.c_str()); + if (detID < DetID::First) { + throw std::runtime_error(fmt::format("Unknown detector {} in align-geom.mAlignPaths", det)); + } + if (seen[detID]) { + throw std::runtime_error(fmt::format("Detector {} given more than once in align-geom.mAlignPaths", det)); + } + seen.set(detID); + if (detID == id) { + path = altPath; + } + } + return path; +} + long Aligner::getTimeStamp() const { return mTimeStamp > 0 ? mTimeStamp : std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count(); diff --git a/Detectors/Base/test/testAligner.cxx b/Detectors/Base/test/testAligner.cxx new file mode 100644 index 0000000000000..0be6d21e73a97 --- /dev/null +++ b/Detectors/Base/test/testAligner.cxx @@ -0,0 +1,57 @@ +// Copyright 2019-2020 CERN and copyright holders of ALICE O2. +// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders. +// All rights not expressly granted are reserved. +// +// This software is distributed under the terms of the GNU General Public +// License v3 (GPL Version 3), copied verbatim in the file "COPYING". +// +// In applying this license CERN does not waive the privileges and immunities +// granted to it by virtue of its status as an Intergovernmental Organization +// or submit itself to any jurisdiction. + +#define BOOST_TEST_MODULE Test Aligner +#define BOOST_TEST_MAIN +#define BOOST_TEST_DYN_LINK + +#include +#include +#include "DetectorsBase/Aligner.h" +#include "CommonUtils/ConfigurableParam.h" + +using o2::base::Aligner; +using DetID = o2::detectors::DetID; + +// Choice of the alignment object path per detector from align-geom.mAlignPaths +BOOST_AUTO_TEST_CASE(AlignmentPathDefault) +{ + BOOST_CHECK_EQUAL(Aligner::getAlignmentPath("", DetID::MCH), "MCH/Calib/Align"); + BOOST_CHECK_EQUAL(Aligner::getAlignmentPath("MID:MID/MisCalib/Align", DetID::MCH), "MCH/Calib/Align"); +} + +BOOST_AUTO_TEST_CASE(AlignmentPathOverride) +{ + const std::string paths = "MCH:MCH/MisCalib/Align,MID:MID/MisCalib/Align"; + BOOST_CHECK_EQUAL(Aligner::getAlignmentPath(paths, DetID::MCH), "MCH/MisCalib/Align"); + BOOST_CHECK_EQUAL(Aligner::getAlignmentPath(paths, DetID::MID), "MID/MisCalib/Align"); + BOOST_CHECK_EQUAL(Aligner::getAlignmentPath(paths, DetID::ITS), "ITS/Calib/Align"); + BOOST_CHECK_EQUAL(Aligner::getAlignmentPath(" MCH : MCH/MisCalib/Align ", DetID::MCH), "MCH/MisCalib/Align"); +} + +BOOST_AUTO_TEST_CASE(AlignmentPathInvalid) +{ + BOOST_CHECK_THROW(Aligner::getAlignmentPath("MCH", DetID::MCH), std::runtime_error); + BOOST_CHECK_THROW(Aligner::getAlignmentPath("MCH:", DetID::MCH), std::runtime_error); + BOOST_CHECK_THROW(Aligner::getAlignmentPath(":MCH/MisCalib/Align", DetID::MCH), std::runtime_error); + BOOST_CHECK_THROW(Aligner::getAlignmentPath("XYZ:XYZ/MisCalib/Align", DetID::MCH), std::runtime_error); + BOOST_CHECK_THROW(Aligner::getAlignmentPath("MCH:a/b/c,MCH:d/e/f", DetID::MCH), std::runtime_error); + // an invalid entry is reported also when it is not the one asked for + BOOST_CHECK_THROW(Aligner::getAlignmentPath("MCH:MCH/MisCalib/Align,XYZ:a/b/c", DetID::MCH), std::runtime_error); +} + +BOOST_AUTO_TEST_CASE(AlignmentPathConfigKey) +{ + o2::conf::ConfigurableParam::updateFromString("align-geom.mAlignPaths=MCH:MCH/MisCalib/Align"); + BOOST_CHECK_EQUAL(Aligner::Instance().getAlignPaths(), "MCH:MCH/MisCalib/Align"); + BOOST_CHECK_EQUAL(Aligner::Instance().getAlignmentPath(DetID::MCH), "MCH/MisCalib/Align"); + BOOST_CHECK_EQUAL(Aligner::Instance().getAlignmentPath(DetID::TPC), "TPC/Calib/Align"); +}