geo_vectors_utils_TVector.h
Go to the documentation of this file.
1 /**
2  * @file garsoft/Utilities/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 `garsoft/Utilities/geo_vectors_utils.h`
7  * @ingroup Geometry
8  *
9  * This library provides facilities that can be used for both GArSoft 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 GARSOFT_UTILITIES_GEO_VECTORS_UTILS_TVECTOR_H
19 #define GARSOFT_UTILITIES_GEO_VECTORS_UTILS_TVECTOR_H
20 
21 #include "geo_vectors_utils.h"
22 
23 // ROOT library
24 #include "TVector2.h"
25 #include "TVector3.h"
26 #include "TLorentzVector.h"
27 
28 // C/C++ standard library
29 #include <ostream>
30 
31 
32 namespace geo {
33  namespace 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  //--------------------------------------------------------------------------
47 
48  /// Utilities to print vector types.
49  namespace dump {
50 
51  // --- BEGIN Output of old-style ROOT vectors (TVector3 etc.) ------------
52  /// @name Output of old-style ROOT vectors (TVector3 etc.)
53  /// @ingroup Geometry
54  /// @{
55 
56  /// Print a `TVector2` to an output stream.
57  template <typename Stream>
58  void Vector2(Stream&& out, TVector2 const& v)
59  { out << "( " << v.X() << ", " << v.Y() << " )"; }
60 
61  /// Print a `TVector3` to an output stream.
62  template <typename Stream>
63  void Vector3(Stream&& out, TVector3 const& v)
64  { out << "( " << v.X() << ", " << v.Y() << ", " << v.Z() << " )"; }
65 
66  /// Print a `TLorentzVector` to an output stream.
67  template <typename Stream>
68  void LorentzVector(Stream&& out, TLorentzVector const& v) {
69  out
70  << "( " << v.X() << ", " << v.Y() << ", " << v.Z() << "; " << v.T()
71  << " )";
72  } // LorentzVector()
73 
74  /// Print a `TVector2` to an output stream.
75  inline std::ostream& operator<< (std::ostream& out, TVector2 const& v)
76  { Vector2(out, v); return out; }
77 
78  /// Print a `TVector3` to an output stream.
79  inline std::ostream& operator<< (std::ostream& out, TVector3 const& v)
80  { Vector3(out, v); return out; }
81 
82  /// Print a `TLorentzVector` to an output stream.
83  inline std::ostream& operator<<
84  (std::ostream& out, TLorentzVector const& v)
85  { LorentzVector(out, v); return out; }
86 
87  // --- END Output of old-style ROOT vectors (TVector3 etc.) --------------
88  /// @}
89 
90  } // namespace dump
91  //--------------------------------------------------------------------------
92 
93  /// @}
94  // --- END TVector3 conversions --------------------------------------------
95 
96  } // namespace vect
97 } // namespace geo
98 
99 
100 // The only way some generic code has to see the operator<< is for them to be
101 // exposed in the same namespace as the vectors they dump are; in TVector case,
102 // that's the global namespace... (Boost unit test checks still fail though)
103 using geo::vect::dump::operator<<;
104 
105 
106 //------------------------------------------------------------------------------
107 //--- Specialisations
108 //---
109 namespace geo {
110  namespace vect {
111 
112  //--------------------------------------------------------------------------
113  // Specialisations for: TVector2
114  template <>
115  inline auto mag2<TVector2>(TVector2 const& v) { return v.Mod2(); }
116 
117  //--------------------------------------------------------------------------
118 
119  } // namespace vect
120 } // namespace geo
121 //------------------------------------------------------------------------------
122 
123 
124 #endif // GARSOFT_UTILITIES_GEO_VECTORS_UTILS_TVECTOR_H
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::ostream & operator<<(std::ostream &out, TVector2 const &v)
Print a TVector2 to an output stream.
Utilities to extend the interface of geometry vectors.
void LorentzVector(Stream &&out, TLorentzVector const &v)
Print a TLorentzVector to an output stream.
Collection of utilities for dumping data on screen.
Definition: DumperBase.h:30
TVector3 toTVector3(Vector const &v)
Converts a vector into a TVector3.
LArSoft geometry interface.
Definition: ChannelGeo.h:16
auto mag2< TVector2 >(TVector2 const &v)