Skip to content

Commit 16469f6

Browse files
committed
Vtx: make class printable
Signed-off-by: Felix Schlepper <felix.schlepper@cern.ch>
1 parent 52d7d58 commit 16469f6

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

DataFormats/Reconstruction/include/ReconstructionDataFormats/Vertex.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include <string>
2323
#include <type_traits>
2424
#include <array>
25+
#include <sstream>
26+
#include <fmt/format.h>
2527
#endif
2628

2729
namespace o2
@@ -135,6 +137,11 @@ class Vertex : public VertexBase
135137
{
136138
}
137139

140+
#if !defined(GPUCA_NO_FMT) && !defined(GPUCA_GPUCODE_DEVICE)
141+
void print() const;
142+
std::string asString() const;
143+
#endif
144+
138145
GPUd() ushort getNContributors() const { return mNContributors; }
139146
GPUd() void setNContributors(ushort v) { mNContributors = v; }
140147
GPUd() void addContributor() { mNContributors++; }
@@ -162,6 +169,49 @@ class Vertex : public VertexBase
162169

163170
#if !defined(GPUCA_GPUCODE_DEVICE) && !defined(GPUCA_NO_FMT)
164171
std::ostream& operator<<(std::ostream& os, const o2::dataformats::VertexBase& v);
172+
173+
namespace detail
174+
{
175+
template <typename T>
176+
concept Streamable = requires(std::ostream& os, const T& a) {
177+
{ os << a } -> std::same_as<std::ostream&>;
178+
};
179+
180+
template <typename T>
181+
concept HasFormattableTimeStamp = requires(const T& t) {
182+
{ fmt::format("{}", t.getTimeStamp()) } -> std::convertible_to<std::string>;
183+
};
184+
} // namespace detail
185+
186+
template <typename Stamp>
187+
inline std::string Vertex<Stamp>::asString() const
188+
{
189+
const std::string stamp = [&]() -> std::string {
190+
if constexpr (detail::Streamable<Stamp>) {
191+
std::ostringstream oss;
192+
oss << mTimeStamp;
193+
return oss.str();
194+
} else if constexpr (detail::HasFormattableTimeStamp<Stamp>) {
195+
return fmt::format("{}", mTimeStamp.getTimeStamp());
196+
} else {
197+
return "X";
198+
}
199+
}();
200+
return fmt::format("{} NContrib:{} Chi2:{:.2f} Flags:{:b} Stamp:{}", VertexBase::asString(), mNContributors, mChi2, mBits, stamp);
201+
}
202+
203+
template <typename Stamp>
204+
inline std::ostream& operator<<(std::ostream& os, const o2::dataformats::Vertex<Stamp>& v)
205+
{
206+
os << v.asString();
207+
return os;
208+
}
209+
210+
template <typename Stamp>
211+
inline void Vertex<Stamp>::print() const
212+
{
213+
std::cout << *this << '\n';
214+
}
165215
#endif
166216

167217
} // namespace dataformats

DataFormats/Reconstruction/src/Vertex.cxx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
// or submit itself to any jurisdiction.
1111

1212
#include "ReconstructionDataFormats/Vertex.h"
13-
#include <iostream>
1413
#ifndef GPUCA_NO_FMT
15-
#include <fmt/printf.h>
14+
#include <iostream>
15+
#include <fmt/format.h>
1616
#endif
1717

1818
namespace o2

0 commit comments

Comments
 (0)