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
7 changes: 7 additions & 0 deletions Detectors/Base/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions Detectors/Base/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <DET>/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.

4 changes: 4 additions & 0 deletions Detectors/Base/include/DetectorsBase/Aligner.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ class Aligner : public o2::conf::ConfigurableParamHelper<Aligner>
{
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;

Expand All @@ -38,6 +41,7 @@ class Aligner : public o2::conf::ConfigurableParamHelper<Aligner>
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");
};
Expand Down
53 changes: 52 additions & 1 deletion Detectors/Base/src/Aligner.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "DetectorsBase/GeometryManager.h"
#include "DetectorsCommonDataFormats/AlignParam.h"
#include "DetectorsCommonDataFormats/DetectorNameConf.h"
#include "CommonUtils/StringUtils.h"
#include <chrono>

O2ParamImpl(o2::base::Aligner);
Expand Down Expand Up @@ -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<std::vector<o2::detectors::AlignParam>>(path);
std::vector<o2::detectors::AlignParam>* 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<std::vector<o2::detectors::AlignParam>>(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<std::vector<o2::detectors::AlignParam>>(path);
}
if (!algV) {
throw std::runtime_error(fmt::format("Failed to fetch alignment from {}:{}", o2::base::NameConf::getCCDBServer(), path));
}
Expand All @@ -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::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
Expand Down
57 changes: 57 additions & 0 deletions Detectors/Base/test/testAligner.cxx
Original file line number Diff line number Diff line change
@@ -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 <boost/test/unit_test.hpp>
#include <stdexcept>
#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");
}