Skip to content

Commit a9585ba

Browse files
ferdymercuryguitargeek
authored andcommitted
[math] add structured bindings to displacement vectors 2D 3D and LorentzVector
Fixes #11806
1 parent e3ebeb6 commit a9585ba

File tree

3 files changed

+76
-4
lines changed

3 files changed

+76
-4
lines changed

math/genvector/inc/Math/GenVector/DisplacementVector2D.h

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ namespace ROOT {
166166
/**
167167
get internal data into 2 Scalar numbers.
168168
These are for example (x,y) for a cartesian vector or (r,phi) for a polar vector
169+
\note Alternatively, you may use structured bindings: `auto const [a, b] = v`.
169170
*/
170171
void GetCoordinates( Scalar& a, Scalar& b) const
171172
{ fCoordinates.GetCoordinates(a, b); }
@@ -538,12 +539,32 @@ namespace ROOT {
538539

539540
} // op>> <>()
540541

541-
542+
// Structured bindings
543+
template <std::size_t I, class CoordSystem, class Tag>
544+
typename CoordSystem::Scalar get(DisplacementVector2D<CoordSystem, Tag> const& p)
545+
{
546+
static_assert(I < 2);
547+
if constexpr (I == 0) {
548+
return p.x();
549+
} else {
550+
return p.y();
551+
}
552+
}
542553

543554
} // namespace Math
544555

545556
} // namespace ROOT
546557

558+
// Structured bindings
559+
#include <tuple>
560+
namespace std {
561+
template <class CoordSystem, class Tag>
562+
struct tuple_size<ROOT::Math::DisplacementVector2D<CoordSystem, Tag>> : integral_constant<size_t, 2> {};
563+
template <size_t I, class CoordSystem, class Tag>
564+
struct tuple_element<I, ROOT::Math::DisplacementVector2D<CoordSystem, Tag>> {
565+
static_assert(I < 2);
566+
using type = typename CoordSystem::Scalar;
567+
};
568+
}
547569

548570
#endif /* ROOT_Math_GenVector_DisplacementVector2D */
549-

math/genvector/inc/Math/GenVector/DisplacementVector3D.h

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ namespace ROOT {
209209

210210
/**
211211
get internal data into 3 Scalar numbers
212+
\note Alternatively, you may use structured bindings: `auto const [a, b, c] = v`.
212213
*/
213214
void GetCoordinates( Scalar& a, Scalar& b, Scalar& c ) const
214215
{ fCoordinates.GetCoordinates(a, b, c); }
@@ -698,12 +699,35 @@ namespace ROOT {
698699

699700
} // op>> <>()
700701

701-
702+
// Structured bindings
703+
template <std::size_t I, class CoordSystem, class Tag>
704+
typename CoordSystem::Scalar get(DisplacementVector3D<CoordSystem, Tag> const& p)
705+
{
706+
static_assert(I < 3);
707+
if constexpr (I == 0) {
708+
return p.x();
709+
} else if constexpr (I == 1) {
710+
return p.y();
711+
} else {
712+
return p.z();
713+
}
714+
}
702715

703716
} // namespace Math
704717

705718
} // namespace ROOT
706719

720+
// Structured bindings
721+
#include <tuple>
722+
namespace std {
723+
template <class CoordSystem, class Tag>
724+
struct tuple_size<ROOT::Math::DisplacementVector3D<CoordSystem, Tag>> : integral_constant<size_t, 3> {};
725+
template <size_t I, class CoordSystem, class Tag>
726+
struct tuple_element<I, ROOT::Math::DisplacementVector3D<CoordSystem, Tag>> {
727+
static_assert(I < 3);
728+
using type = typename CoordSystem::Scalar;
729+
};
730+
}
707731

708732
#endif /* ROOT_Math_GenVector_DisplacementVector3D */
709733

math/genvector/inc/Math/GenVector/LorentzVector.h

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ Pt (or rho) refers to transverse momentum, whereas eta refers to pseudorapidity.
203203

204204
/**
205205
get internal data into 4 Scalar numbers
206+
\note Alternatively, you may use structured bindings: `auto const [a, b, c, d] = v`.
206207
*/
207208
void GetCoordinates( Scalar& a, Scalar& b, Scalar& c, Scalar & d ) const
208209
{ fCoordinates.GetCoordinates(a, b, c, d); }
@@ -861,12 +862,38 @@ Pt (or rho) refers to transverse momentum, whereas eta refers to pseudorapidity.
861862

862863
} // op>> <>()
863864

864-
865+
// Structured bindings
866+
template <std::size_t I, class CoordSystem>
867+
typename CoordSystem::Scalar get(LorentzVector<CoordSystem> const& p)
868+
{
869+
static_assert(I < 4);
870+
if constexpr (I == 0) {
871+
return p.X();
872+
} else if constexpr (I == 1) {
873+
return p.Y();
874+
} else if constexpr (I == 2) {
875+
return p.Z();
876+
} else {
877+
return p.E();
878+
}
879+
}
865880

866881
} // end namespace Math
867882

868883
} // end namespace ROOT
869884

885+
// Structured bindings
886+
#include <tuple>
887+
namespace std {
888+
template <class CoordSystem>
889+
struct tuple_size<ROOT::Math::LorentzVector<CoordSystem>> : integral_constant<size_t, 4> {};
890+
template <size_t I, class CoordSystem>
891+
struct tuple_element<I, ROOT::Math::LorentzVector<CoordSystem>> {
892+
static_assert(I < 4);
893+
using type = typename CoordSystem::Scalar;
894+
};
895+
}
896+
870897
#include <sstream>
871898
namespace cling
872899
{

0 commit comments

Comments
 (0)