All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ShowerTrackColinearTrajPointDirection_tool.cc
Go to the documentation of this file.
1 //############################################################################
2 //### Name: ShowerTrackColinearTrajPointDirection ###
3 //### Author: Dominic Barker ###
4 //### Date: 13.05.19 ###
5 //### Description: Tool for finding the shower direction using the ###
6 //### first trajectory of the initial track ###
7 //############################################################################
8 
9 //Framework Includes
10 #include "art/Utilities/ToolMacros.h"
11 
12 //LArSoft Includes
14 
16 
17 namespace ShowerRecoTools {
18 
20 
21  public:
22  ShowerTrackColinearTrajPointDirection(const fhicl::ParameterSet& pset);
23 
24  //Calculate the direction from the inital track.
25  int CalculateElement(const art::Ptr<recob::PFParticle>& pfparticle,
26  art::Event& Event,
27  reco::shower::ShowerElementHolder& ShowerEleHolder) override;
28 
29  private:
30  //fcl
31  int fVerbose;
32  bool fUsePandoraVertex; //Direction from point defined as
33  //(Position of traj point - Vertex) rather than
34  //(Position of traj point - Track Start Point).
35  bool fAllowDynamicSliding; //Rather than evualte the angle from the start use
36  //the previous trajectory point position.
37  bool fUsePositionInfo; //Don't use the DirectionAtPoint rather than
38  //definition above.
39  //((Position of traj point + 1)-(Position of traj point)
40  bool fUseStartPos; //Rather the using the angles between the directions
41  //from start position to the trajectory points
42  //use the angle between the the points themselves
43  float fAngleCut;
44 
48  };
49 
51  const fhicl::ParameterSet& pset)
52  : IShowerTool(pset.get<fhicl::ParameterSet>("BaseTools"))
53  , fVerbose(pset.get<int>("Verbose"))
54  , fUsePandoraVertex(pset.get<bool>("UsePandoraVertex"))
55  , fAllowDynamicSliding(pset.get<bool>("AllowDynamicSliding"))
56  , fUsePositionInfo(pset.get<bool>("UsePositionInfo"))
57  , fUseStartPos(pset.get<bool>("UseStartPos"))
58  , fAngleCut(pset.get<float>("AngleCut"))
59  , fInitialTrackInputLabel(pset.get<std::string>("InitialTrackInputLabel"))
60  , fShowerStartPositionInputLabel(pset.get<std::string>("ShowerStartPositionInputLabel"))
61  , fShowerDirectionOutputLabel(pset.get<std::string>("ShowerDirectionOutputLabel"))
62 
63  {}
64 
65  int
67  const art::Ptr<recob::PFParticle>& pfparticle,
68  art::Event& Event,
69  reco::shower::ShowerElementHolder& ShowerEleHolder)
70  {
71 
72  //Check the Track has been defined
73  if (!ShowerEleHolder.CheckElement(fInitialTrackInputLabel)) {
74  if (fVerbose)
75  mf::LogError("ShowerTrackColinearTrajPointDirection")
76  << "Initial track not set" << std::endl;
77  return 1;
78  }
79  recob::Track InitialTrack;
80  ShowerEleHolder.GetElement(fInitialTrackInputLabel, InitialTrack);
81 
82  //Smartly choose the which trajectory point to look at by ignoring the smush of hits at the vertex.
83  if (InitialTrack.NumberTrajectoryPoints() == 1) {
84  if (fVerbose)
85  mf::LogError("ShowerTrackColinearTrajPointDirection")
86  << "Not Enough trajectory points." << std::endl;
87  return 1;
88  }
89 
90  //Trajectory point which the direction is calcualted for.
91  int trajpoint = 0;
92  geo::Vector_t Direction_vec;
93 
94  if (fUsePositionInfo) {
95  //Get the start position.
96  geo::Point_t StartPosition;
97 
98  if (fUsePandoraVertex) {
99  //Check the Track has been defined
100  if (!ShowerEleHolder.CheckElement(fShowerStartPositionInputLabel)) {
101  if (fVerbose)
102  mf::LogError("ShowerTrackColinearTrajPointDirection")
103  << "Shower start position not set" << std::endl;
104  return 1;
105  }
106  TVector3 StartPosition_vec = {-999, -999, -999};
107  ShowerEleHolder.GetElement(fShowerStartPositionInputLabel, StartPosition_vec);
108  StartPosition.SetCoordinates(
109  StartPosition_vec.X(), StartPosition_vec.Y(), StartPosition_vec.Z());
110  }
111  else {
112  StartPosition = InitialTrack.Start();
113  }
114 
115  //Loop over the trajectory points and find two corresponding trajectory points where the angle between themselves (or themsleves and the start position) is less the fMinAngle.
116  for (unsigned int traj = 0; traj < InitialTrack.NumberTrajectoryPoints() - 2; ++traj) {
117  ++trajpoint;
118 
119  //ignore bogus info.
120  auto trajflags = InitialTrack.FlagsAtPoint(trajpoint);
121  if (trajflags.isSet(recob::TrajectoryPointFlagTraits::NoPoint)) { continue; }
122 
123  bool bail = false;
124 
125  //ignore bogus info.
126  auto flags = InitialTrack.FlagsAtPoint(traj);
127  if (flags.isSet(recob::TrajectoryPointFlagTraits::NoPoint)) { continue; }
128 
129  //find the next non bogus traj point.
130  int nexttraj = traj + 1;
131  auto nextflags = InitialTrack.FlagsAtPoint(nexttraj);
132  while (nextflags.isSet(recob::TrajectoryPointFlagTraits::NoPoint)) {
133  if (nexttraj == (int)InitialTrack.NumberTrajectoryPoints() - 2) {
134  bail = true;
135  break;
136  }
137  ++nexttraj;
138  nextflags = InitialTrack.FlagsAtPoint(nexttraj);
139  }
140 
141  //find the next next non bogus traj point.
142  int nextnexttraj = nexttraj + 1;
143  auto nextnextflags = InitialTrack.FlagsAtPoint(nextnexttraj);
144  while (nextnextflags.isSet(recob::TrajectoryPointFlagTraits::NoPoint)) {
145  if (nexttraj == (int)InitialTrack.NumberTrajectoryPoints() - 1) {
146  bail = true;
147  break;
148  }
149  ++nextnexttraj;
150  nextnextflags = InitialTrack.FlagsAtPoint(nextnexttraj);
151  }
152 
153  if (bail) {
154  if (fVerbose)
155  mf::LogError("ShowerTrackColinearTrajPointDirection")
156  << "Trajectory point not set as rest of the traj points are bogus." << std::endl;
157  break;
158  }
159 
160  //Get the directions.
161  geo::Vector_t TrajPosition_vec = InitialTrack.LocationAtPoint(traj) - StartPosition;
162  geo::Vector_t NextTrajPosition_vec;
163  geo::Vector_t NextNextTrajPosition_vec;
164  if (fUseStartPos) {
165  NextTrajPosition_vec = InitialTrack.LocationAtPoint(nexttraj) - StartPosition;
166  NextNextTrajPosition_vec = InitialTrack.LocationAtPoint(nextnexttraj) - StartPosition;
167  }
168  else {
169  NextTrajPosition_vec =
170  InitialTrack.LocationAtPoint(nexttraj) - InitialTrack.LocationAtPoint(traj);
171  NextNextTrajPosition_vec =
172  InitialTrack.LocationAtPoint(nextnexttraj) - InitialTrack.LocationAtPoint(traj + 1);
173  }
174 
175  //Get the directions.
176  TVector3 TrajPosition = {TrajPosition_vec.X(), TrajPosition_vec.Y(), TrajPosition_vec.Z()};
177  TVector3 NextTrajPosition = {
178  NextTrajPosition_vec.X(), NextTrajPosition_vec.Y(), NextTrajPosition_vec.Z()};
179  TVector3 NextNextTrajPosition = {
180  NextNextTrajPosition_vec.X(), NextNextTrajPosition_vec.Y(), NextNextTrajPosition_vec.Z()};
181 
182  //Might still be bogus and we can't use the start point
183  if (TrajPosition.Mag() == 0) { continue; }
184  if (NextTrajPosition.Mag() == 0) { continue; }
185  if (NextNextTrajPosition.Mag() == 0) { continue; }
186 
187  //Check to see if the angle between the directions is small enough.
188  if (TrajPosition.Angle(NextTrajPosition) < fAngleCut &&
189  TrajPosition.Angle(NextNextTrajPosition) < fAngleCut) {
190  break;
191  }
192 
193  //Move the start position onwords.
194  if (fAllowDynamicSliding) {
195  StartPosition = {InitialTrack.LocationAtPoint(traj).X(),
196  InitialTrack.LocationAtPoint(traj).Y(),
197  InitialTrack.LocationAtPoint(traj).Z()};
198  }
199  }
200 
201  geo::Point_t TrajPosition = InitialTrack.LocationAtPoint(trajpoint);
202  Direction_vec = (TrajPosition - StartPosition).Unit();
203  }
204  else {
205  //Loop over the trajectory points and find two corresponding trajectory points where the angle between themselves (or themsleves and the start position) is less the fMinAngle.
206  for (unsigned int traj = 0; traj < InitialTrack.NumberTrajectoryPoints() - 2; ++traj) {
207  ++trajpoint;
208 
209  //ignore bogus info.
210  auto trajflags = InitialTrack.FlagsAtPoint(trajpoint);
211  if (trajflags.isSet(recob::TrajectoryPointFlagTraits::NoPoint)) { continue; }
212 
213  //ignore bogus info.
214  auto flags = InitialTrack.FlagsAtPoint(traj);
215  if (flags.isSet(recob::TrajectoryPointFlagTraits::NoPoint)) { continue; }
216 
217  bool bail = false;
218 
219  geo::Vector_t TrajDirection_vec;
220 
221  //Get the next non bogus trajectory points
222  if (fUseStartPos) {
223  int prevtraj = 0;
224  auto prevflags = InitialTrack.FlagsAtPoint(prevtraj);
225  while (prevflags.isSet(recob::TrajectoryPointFlagTraits::NoPoint)) {
226  if (prevtraj == (int)InitialTrack.NumberTrajectoryPoints() - 2) {
227  bail = true;
228  break;
229  }
230  ++prevtraj;
231  prevflags = InitialTrack.FlagsAtPoint(prevtraj);
232  }
233  TrajDirection_vec = InitialTrack.DirectionAtPoint(prevtraj);
234  }
235  else if (fAllowDynamicSliding && traj != 0) {
236  int prevtraj = traj - 1;
237  auto prevflags = InitialTrack.FlagsAtPoint(prevtraj);
238  while (prevflags.isSet(recob::TrajectoryPointFlagTraits::NoPoint)) {
239  if (prevtraj == 0) {
240  bail = true;
241  break;
242  }
243  --prevtraj;
244  prevflags = InitialTrack.FlagsAtPoint(prevtraj);
245  }
246  TrajDirection_vec = InitialTrack.DirectionAtPoint(prevtraj);
247  }
248  else {
249  TrajDirection_vec = InitialTrack.DirectionAtPoint(traj);
250  }
251 
252  //find the next non bogus traj point.
253  int nexttraj = traj + 1;
254  auto nextflags = InitialTrack.FlagsAtPoint(nexttraj);
255  while (nextflags.isSet(recob::TrajectoryPointFlagTraits::NoPoint)) {
256  if (nexttraj == (int)InitialTrack.NumberTrajectoryPoints() - 2) {
257  bail = true;
258  break;
259  }
260  ++nexttraj;
261  nextflags = InitialTrack.FlagsAtPoint(nexttraj);
262  }
263 
264  //find the next next non bogus traj point.
265  int nextnexttraj = nexttraj + 1;
266  auto nextnextflags = InitialTrack.FlagsAtPoint(nextnexttraj);
267  while (nextnextflags.isSet(recob::TrajectoryPointFlagTraits::NoPoint)) {
268  if (nexttraj == (int)InitialTrack.NumberTrajectoryPoints() - 1) {
269  bail = true;
270  break;
271  }
272  ++nextnexttraj;
273  nextnextflags = InitialTrack.FlagsAtPoint(nextnexttraj);
274  }
275 
276  if (bail) {
277  if (fVerbose)
278  mf::LogError("ShowerTrackColinearTrajPointDirection")
279  << "Trajectory point not set as rest of the traj points are bogus." << std::endl;
280  break;
281  }
282 
283  //Get the directions.
284  geo::Vector_t NextTrajDirection_vec = InitialTrack.DirectionAtPoint(nexttraj);
285  geo::Vector_t NextNextTrajDirection_vec = InitialTrack.DirectionAtPoint(nextnexttraj);
286 
287  TVector3 TrajDirection = {
288  TrajDirection_vec.X(), TrajDirection_vec.Y(), TrajDirection_vec.Z()};
289  TVector3 NextTrajDirection = {
290  NextTrajDirection_vec.X(), NextTrajDirection_vec.Y(), NextTrajDirection_vec.Z()};
291  TVector3 NextNextTrajDirection = {NextNextTrajDirection_vec.X(),
292  NextNextTrajDirection_vec.Y(),
293  NextNextTrajDirection_vec.Z()};
294 
295  //Might still be bogus and we can't use the start point
296  if (TrajDirection.Mag() == 0) { continue; }
297  if (NextTrajDirection.Mag() == 0) { continue; }
298  if (NextNextTrajDirection.Mag() == 0) { continue; }
299 
300  //See if the angle is small enough.
301  if (TrajDirection.Angle(NextTrajDirection) < fAngleCut &&
302  TrajDirection.Angle(NextNextTrajDirection) < fAngleCut) {
303  break;
304  }
305  }
306  Direction_vec = InitialTrack.DirectionAtPoint(trajpoint).Unit();
307  }
308 
309  if (trajpoint == (int)InitialTrack.NumberTrajectoryPoints() - 3) {
310  if (fVerbose)
311  mf::LogError("ShowerSmartTrackTrajectoryPointDirectio")
312  << "Trajectory point not set." << std::endl;
313  return 1;
314  }
315 
316  //Set the direction.
317  TVector3 Direction = {Direction_vec.X(), Direction_vec.Y(), Direction_vec.Z()};
318  TVector3 DirectionErr = {-999, -999, -999};
319  ShowerEleHolder.SetElement(Direction, DirectionErr, fShowerDirectionOutputLabel);
320  return 0;
321  }
322 }
323 
ROOT::Math::DisplacementVector3D< ROOT::Math::Cartesian3D< double >, ROOT::Math::GlobalCoordinateSystemTag > Vector_t
Type for representation of momenta in 3D space.
Definition: geo_vectors.h:164
double std(const std::vector< short > &wf, const double ped_mean, size_t start, size_t nsample)
Definition: UtilFunc.cxx:42
static constexpr Flag_t NoPoint
The trajectory point is not defined.
Point_t const & LocationAtPoint(size_t i) const
void SetElement(T &dataproduct, const std::string &Name, bool checktag=false)
size_t NumberTrajectoryPoints() const
Various functions related to the presence and the number of (valid) points.
static constexpr bool
Point_t const & Start() const
Access to track position at different points.
bool CheckElement(const std::string &Name) const
int GetElement(const std::string &Name, T &Element) const
Provides recob::Track data product.
PointFlags_t const & FlagsAtPoint(size_t i) const
int CalculateElement(const art::Ptr< recob::PFParticle > &pfparticle, art::Event &Event, reco::shower::ShowerElementHolder &ShowerEleHolder) override
Vector_t DirectionAtPoint(size_t i) const
ROOT::Math::PositionVector3D< ROOT::Math::Cartesian3D< double >, ROOT::Math::GlobalCoordinateSystemTag > Point_t
Type for representation of position in physical 3D space.
Definition: geo_vectors.h:184
Track from a non-cascading particle.A recob::Track consists of a recob::TrackTrajectory, plus additional members relevant for a &quot;fitted&quot; track: