Skip to content

Commit e3f7328

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

File tree

2 files changed

+55
-4
lines changed

2 files changed

+55
-4
lines changed

DataFormats/Reconstruction/include/ReconstructionDataFormats/Vertex.h

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@
1818

1919
#include "CommonDataFormat/TimeStamp.h"
2020
#ifndef GPUCA_GPUCODE_DEVICE
21-
#include <iosfwd>
22-
#include <string>
2321
#include <type_traits>
2422
#include <array>
23+
#ifndef GPUCA_NO_FMT
24+
#include <sstream>
25+
#include <string>
26+
#include <fmt/format.h>
27+
#endif
2528
#endif
2629

2730
namespace o2
@@ -135,6 +138,11 @@ class Vertex : public VertexBase
135138
{
136139
}
137140

141+
#if !defined(GPUCA_NO_FMT) && !defined(GPUCA_GPUCODE_DEVICE)
142+
void print() const;
143+
std::string asString() const;
144+
#endif
145+
138146
GPUd() ushort getNContributors() const { return mNContributors; }
139147
GPUd() void setNContributors(ushort v) { mNContributors = v; }
140148
GPUd() void addContributor() { mNContributors++; }
@@ -162,6 +170,49 @@ class Vertex : public VertexBase
162170

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

167218
} // 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)