SRVector3D.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 /// \file SRVector3D.h
3 /// \brief 3-vector class with more efficient storage than TVector3.
4 /// Ported from NOvA StandardRecord.
5 /// \author C. Backhouse <c.backhouse@ucl.ac.uk>
6 /// \date Sept. 2021
7 ////////////////////////////////////////////////////////////////////////
8 
9 #ifndef DUNEANAOBJ_SRVECTOR3D_H
10 #define DUNEANAOBJ_SRVECTOR3D_H
11 
12 #if !defined(__GCCXML__) && !defined(__castxml__)
13 
14 #include <cmath>
15 #include <iostream>
16 
17 #include "TVector3.h"
18 
19 #endif
20 
21 
22 namespace caf
23 {
24  /// A 3-vector with more efficient storage than TVector3
25  class SRVector3D
26  {
27  public:
28  SRVector3D();
29  SRVector3D(float x, float y, float z);
30 
31 #if !defined(__GCCXML__) && !defined(__castxml__)
32  /// Easy conversion from TVector3
33  explicit SRVector3D(const TVector3& v);
34 #endif
35 
36  virtual ~SRVector3D() = default;
37 
38  void SetXYZ(float x, float y, float z);
39 
40 #if !defined(__GCCXML__) && !defined(__castxml__)
41  /// Easy conversion back to TVector3
42  operator TVector3() const;
43 
44  void SetX(float _x){x = _x;}
45  void SetY(float _y){y = _y;}
46  void SetZ(float _z){z = _z;}
47 
48  float X() const {return x;}
49  float Y() const {return y;}
50  float Z() const {return z;}
51 
52  // The more common TVector3 operations, mostly for use with TTree::Draw
53  //
54  // NB: you need to specify the initial "rec." when using these
55  float Mag2() const {return x*x+y*y+z*z;}
56  float Mag() const {return sqrt(Mag2());}
57  float Dot(const SRVector3D& v) const {return x*v.x + y*v.y + z*v.z;}
58  SRVector3D Unit() const
59  {
60  const float m = Mag();
61  return SRVector3D(x/m, y/m, z/m);
62  }
63 
64  // a few more of them...
65  SRVector3D operator+(const SRVector3D & other) const { return SRVector3D(x + other.x, y + other.y, z + other.z); }
66  SRVector3D operator-(const SRVector3D & other) const {return SRVector3D(x - other.x, y - other.y, z - other.z); }
67 #endif
68 
69  // -----------------------------
70 
71  float x;
72  float y;
73  float z;
74  };
75 
76 }
77 
78 #if !defined(__GCCXML__) && !defined(__castxml__)
79 // make writing these out easier
80 std::ostream & operator<<(std::ostream & stream, const caf::SRVector3D & vec);
81 #endif
82 
83 #endif //DUNEANAOBJ_SRVECTOR3D_H
A 3-vector with more efficient storage than TVector3.
Definition: SRVector3D.h:25
void SetXYZ(float x, float y, float z)
Definition: SRVector3D.cxx:27
void SetX(float _x)
Definition: SRVector3D.h:44
float Mag2() const
Definition: SRVector3D.h:55
float Y() const
Definition: SRVector3D.h:49
void SetZ(float _z)
Definition: SRVector3D.h:46
virtual ~SRVector3D()=default
float X() const
Definition: SRVector3D.h:48
SRVector3D Unit() const
Definition: SRVector3D.h:58
float Z() const
Definition: SRVector3D.h:50
float Dot(const SRVector3D &v) const
Definition: SRVector3D.h:57
SRVector3D operator+(const SRVector3D &other) const
Definition: SRVector3D.h:65
Common Analysis Files.
Definition: SRGAr.h:13
std::ostream & operator<<(std::ostream &stream, const caf::SRVector3D &vec)
Definition: SRVector3D.cxx:41
float Mag() const
Definition: SRVector3D.h:56
SRVector3D operator-(const SRVector3D &other) const
Definition: SRVector3D.h:66
void SetY(float _y)
Definition: SRVector3D.h:45