geo_vectors_utils_TVector.h
Go to the documentation of this file.
1 /**
2  * @file larcorealg/Geometry/geo_vectors_utils_TVector.h
3  * @brief Specializations of `geo_vectors_utils.h` for ROOT old vector types.
4  * @author Gianluca Petrillo (petrillo@fnal.gov)
5  * @date December 2, 2017
6  * @see `larcorealg/Geometry/geo_vectors_utils.h`
7  * @ingroup Geometry
8  *
9  * This library provides facilities that can be used for both LArSoft geometry
10  * vectors (`geo_vectors.h`) and ROOT `TVector3` and related, with the same
11  * interface.
12  *
13  * This library depends on ROOT GenVector.
14  * In the CET link list in `CMakeLists.txt`, link to `${ROOT_GENVECTOR}`.
15  *
16  */
17 
18 #ifndef LARCOREOBJ_SIMPLETYPESANDCONSTANTS_GEO_VECTORS_UTILS_TVECTOR_H
19 #define LARCOREOBJ_SIMPLETYPESANDCONSTANTS_GEO_VECTORS_UTILS_TVECTOR_H
20 
21 // LArSoft libraries
23 
24 // ROOT library
25 #include "TVector2.h"
26 #include "TVector3.h"
27 #include "TLorentzVector.h"
28 
29 // C/C++ standard library
30 #include <ostream>
31 
32 
33 namespace geo::vect {
34 
35  // --- BEGIN TVector3 conversions --------------------------------------------
36  /// @name TVector3 conversions
37  /// @ingroup Geometry
38  /// @{
39 
40  //----------------------------------------------------------------------------
41  /// Converts a vector into a `TVector3`.
42  template <typename Vector>
43  TVector3 toTVector3(Vector const& v) { return convertTo<TVector3>(v); }
44 
45  /// @}
46  // --- END TVector3 conversions ----------------------------------------------
47 
48  //----------------------------------------------------------------------------
49 
50  /// Utilities to print vector types.
51  namespace dump {
52 
53  // --- BEGIN Output of old-style ROOT vectors (TVector3 etc.) ------------
54  /// @name Output of old-style ROOT vectors (TVector3 etc.)
55  /// @ingroup Geometry
56  /// @{
57 
58  /// Print a `TVector2` to an output stream.
59  template <typename Stream>
60  void Vector2(Stream&& out, TVector2 const& v)
61  { out << "( " << v.X() << ", " << v.Y() << " )"; }
62 
63  /// Print a `TVector3` to an output stream.
64  template <typename Stream>
65  void Vector3(Stream&& out, TVector3 const& v)
66  { out << "( " << v.X() << ", " << v.Y() << ", " << v.Z() << " )"; }
67 
68  /// Print a `TLorentzVector` to an output stream.
69  template <typename Stream>
70  void LorentzVector(Stream&& out, TLorentzVector const& v) {
71  out
72  << "( " << v.X() << ", " << v.Y() << ", " << v.Z() << "; " << v.T()
73  << " )";
74  } // LorentzVector()
75 
76  /// Print a `TVector2` to an output stream.
77  inline std::ostream& operator<< (std::ostream& out, TVector2 const& v)
78  { Vector2(out, v); return out; }
79 
80  /// Print a `TVector3` to an output stream.
81  inline std::ostream& operator<< (std::ostream& out, TVector3 const& v)
82  { Vector3(out, v); return out; }
83 
84  /// Print a `TLorentzVector` to an output stream.
85  inline std::ostream& operator<<
86  (std::ostream& out, TLorentzVector const& v)
87  { LorentzVector(out, v); return out; }
88 
89  /// @}
90  // --- END Output of old-style ROOT vectors (TVector3 etc.) ----------------
91 
92  } // namespace dump
93  //----------------------------------------------------------------------------
94 
95 
96 } // namespace geo::vect
97 
98 
99 // The only way some generic code has to see the operator<< is for them to be
100 // exposed in the same namespace as the vectors they dump are; in TVector case,
101 // that's the global namespace... (Boost unit test checks still fail though)
102 using geo::vect::dump::operator<<;
103 
104 
105 //------------------------------------------------------------------------------
106 //--- Specialisations
107 //---
108 namespace geo::vect {
109 
110  //----------------------------------------------------------------------------
111  // Specialisations for: TVector2
112  template <>
113  inline auto mag2<TVector2>(TVector2 const& v) { return v.Mod2(); }
114 
115  //----------------------------------------------------------------------------
116 
117 } // namespace geo::vect
118 
119 
120 //------------------------------------------------------------------------------
121 //--- STL specialization for ROOT vectors
122 //------------------------------------------------------------------------------
123 
124 /// @name Overloads of STL C++ functions for ROOT vectors
125 /// @{
126 
127 // in the golden global namespace, as in old ROOT tradition
128 
129 // --- BEGIN 2D vectors --------------------------------------------------------
130 decltype(auto) begin(TVector2 const& v) { return geo::vect::vector_cbegin(v); }
131 
132 decltype(auto) cbegin(TVector2 const& v) { return geo::vect::vector_cbegin(v); }
133 
134 decltype(auto) end(TVector2 const& v) { return geo::vect::vector_cend(v); }
135 
136 decltype(auto) cend(TVector2 const& v) { return geo::vect::vector_cend(v); }
137 
138 // --- END 2D vectors ----------------------------------------------------------
139 
140 // --- BEGIN 3D vectors --------------------------------------------------------
141 decltype(auto) begin(TVector3 const& v) { return geo::vect::vector_cbegin(v); }
142 
143 decltype(auto) cbegin(TVector3 const& v) { return geo::vect::vector_cbegin(v); }
144 
145 decltype(auto) end(TVector3 const& v) { return geo::vect::vector_cend(v); }
146 
147 decltype(auto) cend(TVector3 const& v) { return geo::vect::vector_cend(v); }
148 
149 // --- END 3D vectors ----------------------------------------------------------
150 
151 
152 // --- BEGIN 4D vectors --------------------------------------------------------
153 decltype(auto) begin(TLorentzVector const& v)
154  { return geo::vect::vector_cbegin(v); }
155 
156 decltype(auto) cbegin(TLorentzVector const& v)
157  { return geo::vect::vector_cbegin(v); }
158 
159 decltype(auto) end(TLorentzVector const& v)
160  { return geo::vect::vector_cend(v); }
161 
162 decltype(auto) cend(TLorentzVector const& v)
163  { return geo::vect::vector_cend(v); }
164 
165 // --- END 4D vectors ----------------------------------------------------------
166 
167 
168 /// @}
169 
170 
171 
172 //------------------------------------------------------------------------------
173 
174 
175 #endif // LARCOREOBJ_SIMPLETYPESANDCONSTANTS_GEO_VECTORS_UTILS_TVECTOR_H
std::ostream & operator<<(std::ostream &out, TVector2 const &v)
Print a TVector2 to an output stream.
void Vector2(Stream &&out, TVector2 const &v)
Print a TVector2 to an output stream.
void Vector3(Stream &&out, TVector3 const &v)
Print a TVector3 to an output stream.
std::vector< float > Vector
decltype(auto) begin(TVector2 const &v)
decltype(auto) cbegin(TVector2 const &v)
Utilities to manipulate geometry vectors.The utilities include generic vector interface facilities al...
auto vector_cend(Vector const &v)
Returns a const-iterator pointing after the last coordinate of v.
Utilities to extend the interface of geometry vectors.
void LorentzVector(Stream &&out, TLorentzVector const &v)
Print a TLorentzVector to an output stream.
decltype(auto) cend(TVector2 const &v)
decltype(auto) end(TVector2 const &v)
Collection of utilities for dumping data on screen.
Definition: DumperBase.h:30
auto vector_cbegin(Vector const &v)
Returns a const-iterator pointing to the first coordinate of v.
TVector3 toTVector3(Vector const &v)
Converts a vector into a TVector3.
auto mag2< TVector2 >(TVector2 const &v)