LArSoft  v06_85_00
Liquid Argon Software toolkit - http://larsoft.org/
Trajectory.cxx
Go to the documentation of this file.
1 
12 
13 // LArSoft libraries
15 
16 // ROOT libraries
17 #include "TVector3.h" // legacy interface
18 #include "TMatrixD.h" // legacy interface
19 
20 // C/C++ standard libraries
21 #include <ostream>
22 #include <utility> // std::move()
23 #include <string> // std::to_string()
24 #include <stdexcept> // std::runtime_error
25 
26 
27 //------------------------------------------------------------------------------
28 namespace {
29 
30  template <typename Matrix>
31  TMatrixD fillTMatrixD(Matrix const& mat, TMatrixD& Tmat) {
32  ROOT::Math::Rotation3D asMat(mat);
33  Tmat.ResizeTo(3, 3);
34  asMat.GetRotationMatrix(Tmat);
35  return Tmat;
36  } // fillTMatrixD()
37 
38 } // local namespace
39 
40 //------------------------------------------------------------------------------
42  (Positions_t&& positions, Momenta_t&& momenta, bool hasMomenta)
43  : fPositions(std::move(positions))
44  , fMomenta(std::move(momenta))
45  , fHasMomentum(hasMomenta)
46 {
47  // invariant check
48  if (fPositions.size() != fMomenta.size()) {
49  throw std::runtime_error("recob::Trajectory constructed with "
50  + std::to_string(fPositions.size()) + " positions and "
51  + std::to_string(fMomenta.size())
52  + " momenta! it requires the same number for both."
53  );
54  }
55  if (fPositions.size() < 2) {
56  throw std::runtime_error("recob::Trajectory constructed with "
57  + std::to_string(fPositions.size())
58  + " trajectory points! it requires at least 2."
59  );
60  }
61 } // recob::Trajectory::Trajectory()
62 
63 
64 //------------------------------------------------------------------------------
66  (size_t i, TVector3& pos, TVector3& dir) const
67 {
68  if (!HasPoint(i)) return false;
69 
70  auto const& origPos = LocationAtPoint(i);
71  decltype(auto) origDir = DirectionAtPoint(i);
72 
73  pos.SetXYZ(origPos.X(), origPos.Y(), origPos.Z());
74  dir.SetXYZ(origDir.X(), origDir.Y(), origDir.Z());
75  return true;
76 } // recob::Trajectory::TrajectoryAtPoint(TVector&)
77 
78 
79 //------------------------------------------------------------------------------
81  (std::vector<double>& start, std::vector<double>& end) const
82  { details::legacy::FillTwoVectors(Start(), End(), start, end); }
83 
84 
85 
86 //------------------------------------------------------------------------------
103 double recob::Trajectory::Length(size_t startAt /* = 0 */) const {
104 
105  // sanity check
106  if (startAt >= LastPoint()) return 0.;
107 
108  // just sum the distance between all locations in the trajectory
109  Point_t const* curr = &(LocationAtPoint(startAt));
110  Point_t const* next = curr;
111  Point_t const* last = &(End());
112  Coord_t length = 0.0;
113  while (next++ != last) {
114  length += (*next - *curr).R();
115  curr = next;
116  } // while
117  return length;
118 } // recob::Trajectory::Length()
119 
120 
121 //------------------------------------------------------------------------------
122 double recob::Trajectory::ZenithAngle(size_t p /* = 0 */) const {
123 
124  // The zenith angle is defined by the angle between the track starting
125  // direction and the y axis.
126  // The y component of the starting direction is in fact the cosine of that
127  // angle (and std::acos() conveniently returns a value in [0;pi]).
128  // Our convention has the zenith angle the supplemental of the standard one.
129 
130  return util::pi<Coord_t>() - std::acos(DirectionAtPoint(p).Y());
131 
132 } // recob::Trajectory::ZenithAngle()
133 
134 
135 //------------------------------------------------------------------------------
136 double recob::Trajectory::AzimuthAngle(size_t p /* = 0 */) const {
137  //
138  // std::atan2(y, x) returns the angle of a point (x,y) respect to x axis.
139  // In our case, the definition of the angle (0 for z axis, pi/2 for x axis)
140  // translates atan2's y into our x, and x into z.
141  //
142  decltype(auto) startDir = DirectionAtPoint(p);
143  return std::atan2(startDir.X(), startDir.Z());
144 } // recob::Trajectory::AzimuthAngle()
145 
146 
147 //------------------------------------------------------------------------------
158 {
159 
160  auto const& mom = MomentumVectorAtPoint(i);
161  return HasMomentum()? (mom / mom.R()): mom;
162 
163 } // recob::Trajectory::DirectionAtPoint()
164 
165 
166 //------------------------------------------------------------------------------
167 void recob::Trajectory::Direction(double* start, double* end) const
169 
170 
171 //------------------------------------------------------------------------------
173  (size_t p) const
174 {
176 } // recob::Trajectory::GlobalToLocalRotationAtPoint()
177 
178 
179 //------------------------------------------------------------------------------
181  (unsigned int p, TMatrixD& rot) const
182 {
183  auto trackRot = GlobalToLocalRotationAtPoint(p);
184  fillTMatrixD(trackRot, rot);
185 } // recob::Trajectory::GlobalToLocalRotationAtPoint(TMatrixD)
186 
187 
188 //------------------------------------------------------------------------------
190  (size_t p) const
191 {
193 } // recob::Trajectory::GlobalToLocalRotationAtPoint()
194 
195 
196 //------------------------------------------------------------------------------
198  (unsigned int p, TMatrixD &rot) const
199 {
200  auto trackRot = LocalToGlobalRotationAtPoint(p);
201  fillTMatrixD(trackRot, rot);
202 } // recob::Trajectory::LocalToGlobalRotationAtPoint(TMatrixD)
203 
204 
205 //------------------------------------------------------------------------------
206 std::ostream& recob::operator <<
207  (std::ostream& out, recob::Trajectory const& traj)
208  { traj.Dump(out); return out; }
209 
210 
211 //------------------------------------------------------------------------------
Trajectory()=default
Default constructor; do not use it! it&#39;s needed by ROOT I/O.
Data product for reconstructed trajectory in space.
std::pair< Vector_t, Vector_t > Direction() const
Returns the trajectory directions at first and last point.
Definition: Trajectory.h:529
Vector_t EndDirection() const
Returns the direction of the trajectory at the last point.
Definition: Trajectory.h:338
Rotation_t Global3DToLocal3DRotation() const
Calculate rotation matrices from global (x,y,z) to local (u,v,w) coordinates.
tracking::Coord_t Coord_t
Type used for coordinates and values in general.
Definition: Trajectory.h:75
Point_t const & End() const
Returns the position of the last point of the trajectory [cm].
Definition: Trajectory.h:244
bool TrajectoryAtPoint(size_t i, TVector3 &pos, TVector3 &dir) const
Fills position and direction at the specified trajectory point.
Definition: Trajectory.cxx:66
double AzimuthAngle(size_t p=0) const
"Azimuth" angle of trajectory, with respect to the sky.
Definition: Trajectory.cxx:136
double ZenithAngle(size_t p=0) const
"Zenith" angle of trajectory, with respect to the vertical axis.
Definition: Trajectory.cxx:122
double Length(size_t startAt=0) const
Returns the approximate length of the trajectory.
Definition: Trajectory.cxx:103
Rotation_t LocalToGlobalRotationAtPoint(size_t p) const
Returns a rotation matrix bringing relative directions to global.
Definition: Trajectory.cxx:190
std::pair< Point_t, Point_t > Extent() const
Returns a copy of the first and last point in the trajectory.
Definition: Trajectory.h:288
typename BeginEndPackage< L >::End End
Vector_t DirectionAtPoint(size_t i) const
Computes and returns the direction of the trajectory at a point.
Definition: Trajectory.cxx:157
Rotation_t GlobalToLocalRotationAtPoint(size_t p) const
Returns a rotation matrix that brings trajectory direction along z.
Definition: Trajectory.cxx:173
tracking::Vector_t Vector_t
Type for representation of momenta in 3D space.
Definition: Trajectory.h:81
size_t LastPoint() const
Returns the index of the last point in the trajectory.
Definition: Trajectory.h:180
Double_t R
Point_t const & LocationAtPoint(size_t i) const
Returns the position at the specified trajectory point.
Definition: Trajectory.h:255
Float_t mat
Definition: plot.C:40
tracking::Positions_t Positions_t
Type of trajectory point list.
Definition: Trajectory.h:84
tracking::Momenta_t Momenta_t
Type of momentum list.
Definition: Trajectory.h:87
Vector_t const & MomentumVectorAtPoint(size_t i) const
Returns the momentum vector at a point.
Definition: Trajectory.h:492
Vector_t StartDirection() const
Returns the direction of the trajectory at the first point.
Definition: Trajectory.h:334
A trajectory in space reconstructed from hits.
Definition: Trajectory.h:72
std::string to_string(Flag_t< Storage > const flag)
Convert a flag into a stream (shows its index).
Definition: BitMask.h:187
bool HasMomentum() const
Returns whether information about the momentum is available.
Definition: Trajectory.h:460
TDirectory * dir
Definition: macro.C:5
tracking::Point_t Point_t
Type for representation of position in physical 3D space.
Definition: Trajectory.h:78
std::vector< evd::details::RawDigitInfo_t >::const_iterator end(RawDigitCacheDataClass const &cache)
Rotation_t Local3DToGlobal3DRotation() const
Calculate rotation matrices from local (u,v,w) to global (x,y,z) coordinates.
tracking::Rotation_t Rotation_t
Type for representation of space rotations.
Definition: Trajectory.h:102
void FillTwoVectors(SrcVect const &firstSource, SrcVect const &secondSource, DestVect &&firstDest, DestVect &&secondDest)
Converts two vectors into another type of vector using FillVector.