Skip to content

Commit 514d9dc

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

2 files changed

Lines changed: 43 additions & 3 deletions

File tree

DataFormats/Reconstruction/include/ReconstructionDataFormats/Vertex.h

Lines changed: 40 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 <format>
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,39 @@ 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+
// small helper concept
174+
template <typename T>
175+
concept Streamable = requires(std::ostream& os, T a) {
176+
{ os << a } -> std::same_as<std::ostream&>;
177+
};
178+
179+
template <typename Stamp>
180+
inline std::string Vertex<Stamp>::asString() const
181+
{
182+
if constexpr (Streamable<Stamp>) {
183+
std::ostringstream oss;
184+
oss << mTimeStamp;
185+
return std::format("{} NContrib:{} Chi2:{:.2f} Flags:{:b} Stamp:{}", VertexBase::asString(), mNContributors, mChi2, mBits, oss.str());
186+
} else if constexpr (requires { mTimeStamp.getTimeStamp(); }) {
187+
return std::format("{} NContrib:{} Chi2:{:.2f} Flags:{:b} Stamp:{}", VertexBase::asString(), mNContributors, mChi2, mBits, mTimeStamp.getTimeStamp());
188+
} else {
189+
return std::format("{} NContrib:{} Chi2:{:.2f} Flags:{:b} Stamp:X", VertexBase::asString(), mNContributors, mChi2, mBits);
190+
}
191+
}
192+
193+
template <typename Stamp>
194+
inline std::ostream& operator<<(std::ostream& os, const o2::dataformats::Vertex<Stamp>& v)
195+
{
196+
os << v.asString();
197+
return os;
198+
}
199+
200+
template <typename Stamp>
201+
inline void Vertex<Stamp>::print() const
202+
{
203+
std::cout << *this << '\n';
204+
}
165205
#endif
166206

167207
} // namespace dataformats

DataFormats/Reconstruction/src/Vertex.cxx

Lines changed: 3 additions & 3 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 <format>
1616
#endif
1717

1818
namespace o2
@@ -24,7 +24,7 @@ namespace dataformats
2424
#ifndef GPUCA_NO_FMT
2525
std::string VertexBase::asString() const
2626
{
27-
return fmt::format("Vtx {{{:+.4e},{:+.4e},{:+.4e}}} Cov.:{{{{{:.3e}..}},{{{:.3e},{:.3e}..}},{{{:.3e},{:.3e},{:.3e}}}}}",
27+
return std::format("Vtx {{{:+.4e},{:+.4e},{:+.4e}}} Cov.:{{{{{:.3e}..}},{{{:.3e},{:.3e}..}},{{{:.3e},{:.3e},{:.3e}}}}}",
2828
mPos.X(), mPos.Y(), mPos.Z(), mCov[0], mCov[1], mCov[2], mCov[3], mCov[4], mCov[5]);
2929
}
3030

0 commit comments

Comments
 (0)