diff --git a/DataFormats/Reconstruction/include/ReconstructionDataFormats/Vertex.h b/DataFormats/Reconstruction/include/ReconstructionDataFormats/Vertex.h index fc89f162a0727..2d13e029f8c00 100644 --- a/DataFormats/Reconstruction/include/ReconstructionDataFormats/Vertex.h +++ b/DataFormats/Reconstruction/include/ReconstructionDataFormats/Vertex.h @@ -18,10 +18,13 @@ #include "CommonDataFormat/TimeStamp.h" #ifndef GPUCA_GPUCODE_DEVICE -#include -#include #include #include +#ifndef GPUCA_NO_FMT +#include +#include +#include +#endif #endif namespace o2 @@ -135,6 +138,11 @@ class Vertex : public VertexBase { } +#if !defined(GPUCA_NO_FMT) && !defined(GPUCA_GPUCODE_DEVICE) + void print() const; + std::string asString() const; +#endif + GPUd() ushort getNContributors() const { return mNContributors; } GPUd() void setNContributors(ushort v) { mNContributors = v; } GPUd() void addContributor() { mNContributors++; } @@ -162,6 +170,49 @@ class Vertex : public VertexBase #if !defined(GPUCA_GPUCODE_DEVICE) && !defined(GPUCA_NO_FMT) std::ostream& operator<<(std::ostream& os, const o2::dataformats::VertexBase& v); + +namespace detail +{ +template +concept Streamable = requires(std::ostream& os, const T& a) { + { os << a } -> std::same_as; +}; + +template +concept HasFormattableTimeStamp = requires(const T& t) { + { fmt::format("{}", t.getTimeStamp()) } -> std::convertible_to; +}; +} // namespace detail + +template +inline std::string Vertex::asString() const +{ + const std::string stamp = [&]() -> std::string { + if constexpr (detail::Streamable) { + std::ostringstream oss; + oss << mTimeStamp; + return oss.str(); + } else if constexpr (detail::HasFormattableTimeStamp) { + return fmt::format("{}", mTimeStamp.getTimeStamp()); + } else { + return "X"; + } + }(); + return fmt::format("{} NContrib:{} Chi2:{:.2f} Flags:{:b} Stamp:{}", VertexBase::asString(), mNContributors, mChi2, mBits, stamp); +} + +template +inline std::ostream& operator<<(std::ostream& os, const o2::dataformats::Vertex& v) +{ + os << v.asString(); + return os; +} + +template +inline void Vertex::print() const +{ + std::cout << *this << '\n'; +} #endif } // namespace dataformats diff --git a/DataFormats/Reconstruction/src/Vertex.cxx b/DataFormats/Reconstruction/src/Vertex.cxx index b902e9972a13d..85145683ddd97 100644 --- a/DataFormats/Reconstruction/src/Vertex.cxx +++ b/DataFormats/Reconstruction/src/Vertex.cxx @@ -10,9 +10,9 @@ // or submit itself to any jurisdiction. #include "ReconstructionDataFormats/Vertex.h" -#include #ifndef GPUCA_NO_FMT -#include +#include +#include #endif namespace o2