LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
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
11 
12 //LArSoft Includes
14 
16 
17 //ROOT
18 #include "Math/VectorUtil.h"
19 
20 using ROOT::Math::VectorUtil::Angle;
21 
22 namespace ShowerRecoTools {
23 
25 
26  public:
28 
29  //Calculate the direction from the inital track.
30  int CalculateElement(const art::Ptr<recob::PFParticle>& pfparticle,
31  art::Event& Event,
32  reco::shower::ShowerElementHolder& ShowerEleHolder) override;
33 
34  private:
35  //fcl
36  int fVerbose;
37  bool fUsePandoraVertex; //Direction from point defined as
38  //(Position of traj point - Vertex) rather than
39  //(Position of traj point - Track Start Point).
40  bool fAllowDynamicSliding; //Rather than evualte the angle from the start use
41  //the previous trajectory point position.
42  bool fUsePositionInfo; //Don't use the DirectionAtPoint rather than
43  //definition above.
44  //((Position of traj point + 1)-(Position of traj point)
45  bool fUseStartPos; //Rather the using the angles between the directions
46  //from start position to the trajectory points
47  //use the angle between the the points themselves
48  float fAngleCut;
49 
53  };
54 
56  const fhicl::ParameterSet& pset)
57  : IShowerTool(pset.get<fhicl::ParameterSet>("BaseTools"))
58  , fVerbose(pset.get<int>("Verbose"))
59  , fUsePandoraVertex(pset.get<bool>("UsePandoraVertex"))
60  , fAllowDynamicSliding(pset.get<bool>("AllowDynamicSliding"))
61  , fUsePositionInfo(pset.get<bool>("UsePositionInfo"))
62  , fUseStartPos(pset.get<bool>("UseStartPos"))
63  , fAngleCut(pset.get<float>("AngleCut"))
64  , fInitialTrackInputLabel(pset.get<std::string>("InitialTrackInputLabel"))
65  , fShowerStartPositionInputLabel(pset.get<std::string>("ShowerStartPositionInputLabel"))
66  , fShowerDirectionOutputLabel(pset.get<std::string>("ShowerDirectionOutputLabel"))
67 
68  {}
69 
71  const art::Ptr<recob::PFParticle>& pfparticle,
72  art::Event& Event,
73  reco::shower::ShowerElementHolder& ShowerEleHolder)
74  {
75 
76  //Check the Track has been defined
77  if (!ShowerEleHolder.CheckElement(fInitialTrackInputLabel)) {
78  if (fVerbose)
79  mf::LogError("ShowerTrackColinearTrajPointDirection")
80  << "Initial track not set" << std::endl;
81  return 1;
82  }
83  recob::Track InitialTrack;
84  ShowerEleHolder.GetElement(fInitialTrackInputLabel, InitialTrack);
85 
86  //Smartly choose the which trajectory point to look at by ignoring the smush of hits at the vertex.
87  if (InitialTrack.NumberTrajectoryPoints() == 1) {
88  if (fVerbose)
89  mf::LogError("ShowerTrackColinearTrajPointDirection")
90  << "Not Enough trajectory points." << std::endl;
91  return 1;
92  }
93 
94  //Trajectory point which the direction is calcualted for.
95  int trajpoint = 0;
97 
98  if (fUsePositionInfo) {
99  //Get the start position.
100  geo::Point_t StartPosition;
101 
102  if (fUsePandoraVertex) {
103  //Check the Track has been defined
104  if (!ShowerEleHolder.CheckElement(fShowerStartPositionInputLabel)) {
105  if (fVerbose)
106  mf::LogError("ShowerTrackColinearTrajPointDirection")
107  << "Shower start position not set" << std::endl;
108  return 1;
109  }
110  ShowerEleHolder.GetElement(fShowerStartPositionInputLabel, StartPosition);
111  }
112  else {
113  StartPosition = InitialTrack.Start();
114  }
115 
116  //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.
117  for (unsigned int traj = 0; traj < InitialTrack.NumberTrajectoryPoints() - 2; ++traj) {
118  ++trajpoint;
119 
120  //ignore bogus info.
121  auto trajflags = InitialTrack.FlagsAtPoint(trajpoint);
122  if (trajflags.isSet(recob::TrajectoryPointFlagTraits::NoPoint)) { continue; }
123 
124  bool bail = false;
125 
126  //ignore bogus info.
127  auto flags = InitialTrack.FlagsAtPoint(traj);
128  if (flags.isSet(recob::TrajectoryPointFlagTraits::NoPoint)) { continue; }
129 
130  //find the next non bogus traj point.
131  int nexttraj = traj + 1;
132  auto nextflags = InitialTrack.FlagsAtPoint(nexttraj);
133  while (nextflags.isSet(recob::TrajectoryPointFlagTraits::NoPoint)) {
134  if (nexttraj == (int)InitialTrack.NumberTrajectoryPoints() - 2) {
135  bail = true;
136  break;
137  }
138  ++nexttraj;
139  nextflags = InitialTrack.FlagsAtPoint(nexttraj);
140  }
141 
142  //find the next next non bogus traj point.
143  int nextnexttraj = nexttraj + 1;
144  auto nextnextflags = InitialTrack.FlagsAtPoint(nextnexttraj);
145  while (nextnextflags.isSet(recob::TrajectoryPointFlagTraits::NoPoint)) {
146  if (nexttraj == (int)InitialTrack.NumberTrajectoryPoints() - 1) {
147  bail = true;
148  break;
149  }
150  ++nextnexttraj;
151  nextnextflags = InitialTrack.FlagsAtPoint(nextnexttraj);
152  }
153 
154  if (bail) {
155  if (fVerbose)
156  mf::LogError("ShowerTrackColinearTrajPointDirection")
157  << "Trajectory point not set as rest of the traj points are bogus." << std::endl;
158  break;
159  }
160 
161  //Get the directions.
162  geo::Vector_t TrajPosition = InitialTrack.LocationAtPoint(traj) - StartPosition;
163  geo::Vector_t NextTrajPosition;
164  geo::Vector_t NextNextTrajPosition;
165  if (fUseStartPos) {
166  NextTrajPosition = InitialTrack.LocationAtPoint(nexttraj) - StartPosition;
167  NextNextTrajPosition = InitialTrack.LocationAtPoint(nextnexttraj) - StartPosition;
168  }
169  else {
170  NextTrajPosition =
171  InitialTrack.LocationAtPoint(nexttraj) - InitialTrack.LocationAtPoint(traj);
172  NextNextTrajPosition =
173  InitialTrack.LocationAtPoint(nextnexttraj) - InitialTrack.LocationAtPoint(traj + 1);
174  }
175 
176  //Might still be bogus and we can't use the start point
177  if (TrajPosition.R() == 0) { continue; }
178  if (NextTrajPosition.R() == 0) { continue; }
179  if (NextNextTrajPosition.R() == 0) { continue; }
180 
181  //Check to see if the angle between the directions is small enough.
182  if (Angle(TrajPosition, NextTrajPosition) < fAngleCut &&
183  Angle(TrajPosition, NextNextTrajPosition) < fAngleCut) {
184  break;
185  }
186 
187  //Move the start position onwords.
188  if (fAllowDynamicSliding) { StartPosition = InitialTrack.LocationAtPoint(traj); }
189  }
190 
191  geo::Point_t TrajPosition = InitialTrack.LocationAtPoint(trajpoint);
192  Direction = (TrajPosition - StartPosition).Unit();
193  }
194  else {
195  //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.
196  for (unsigned int traj = 0; traj < InitialTrack.NumberTrajectoryPoints() - 2; ++traj) {
197  ++trajpoint;
198 
199  //ignore bogus info.
200  auto trajflags = InitialTrack.FlagsAtPoint(trajpoint);
201  if (trajflags.isSet(recob::TrajectoryPointFlagTraits::NoPoint)) { continue; }
202 
203  //ignore bogus info.
204  auto flags = InitialTrack.FlagsAtPoint(traj);
205  if (flags.isSet(recob::TrajectoryPointFlagTraits::NoPoint)) { continue; }
206 
207  bool bail = false;
208 
209  geo::Vector_t TrajDirection;
210 
211  //Get the next non bogus trajectory points
212  if (fUseStartPos) {
213  int prevtraj = 0;
214  auto prevflags = InitialTrack.FlagsAtPoint(prevtraj);
215  while (prevflags.isSet(recob::TrajectoryPointFlagTraits::NoPoint)) {
216  if (prevtraj == (int)InitialTrack.NumberTrajectoryPoints() - 2) {
217  bail = true;
218  break;
219  }
220  ++prevtraj;
221  prevflags = InitialTrack.FlagsAtPoint(prevtraj);
222  }
223  TrajDirection = InitialTrack.DirectionAtPoint(prevtraj);
224  }
225  else if (fAllowDynamicSliding && traj != 0) {
226  int prevtraj = traj - 1;
227  auto prevflags = InitialTrack.FlagsAtPoint(prevtraj);
228  while (prevflags.isSet(recob::TrajectoryPointFlagTraits::NoPoint)) {
229  if (prevtraj == 0) {
230  bail = true;
231  break;
232  }
233  --prevtraj;
234  prevflags = InitialTrack.FlagsAtPoint(prevtraj);
235  }
236  TrajDirection = InitialTrack.DirectionAtPoint(prevtraj);
237  }
238  else {
239  TrajDirection = InitialTrack.DirectionAtPoint(traj);
240  }
241 
242  //find the next non bogus traj point.
243  int nexttraj = traj + 1;
244  auto nextflags = InitialTrack.FlagsAtPoint(nexttraj);
245  while (nextflags.isSet(recob::TrajectoryPointFlagTraits::NoPoint)) {
246  if (nexttraj == (int)InitialTrack.NumberTrajectoryPoints() - 2) {
247  bail = true;
248  break;
249  }
250  ++nexttraj;
251  nextflags = InitialTrack.FlagsAtPoint(nexttraj);
252  }
253 
254  //find the next next non bogus traj point.
255  int nextnexttraj = nexttraj + 1;
256  auto nextnextflags = InitialTrack.FlagsAtPoint(nextnexttraj);
257  while (nextnextflags.isSet(recob::TrajectoryPointFlagTraits::NoPoint)) {
258  if (nexttraj == (int)InitialTrack.NumberTrajectoryPoints() - 1) {
259  bail = true;
260  break;
261  }
262  ++nextnexttraj;
263  nextnextflags = InitialTrack.FlagsAtPoint(nextnexttraj);
264  }
265 
266  if (bail) {
267  if (fVerbose)
268  mf::LogError("ShowerTrackColinearTrajPointDirection")
269  << "Trajectory point not set as rest of the traj points are bogus." << std::endl;
270  break;
271  }
272 
273  //Get the directions.
274  geo::Vector_t NextTrajDirection = InitialTrack.DirectionAtPoint(nexttraj);
275  geo::Vector_t NextNextTrajDirection = InitialTrack.DirectionAtPoint(nextnexttraj);
276 
277  //Might still be bogus and we can't use the start point
278  if (TrajDirection.R() == 0) { continue; }
279  if (NextTrajDirection.R() == 0) { continue; }
280  if (NextNextTrajDirection.R() == 0) { continue; }
281 
282  //See if the angle is small enough.
283  if (Angle(TrajDirection, NextTrajDirection) < fAngleCut &&
284  Angle(TrajDirection, NextNextTrajDirection) < fAngleCut) {
285  break;
286  }
287  }
288  Direction = InitialTrack.DirectionAtPoint(trajpoint).Unit();
289  }
290 
291  if (trajpoint == (int)InitialTrack.NumberTrajectoryPoints() - 3) {
292  if (fVerbose)
293  mf::LogError("ShowerSmartTrackTrajectoryPointDirectio")
294  << "Trajectory point not set." << std::endl;
295  return 1;
296  }
297 
298  //Set the direction.
299  geo::Vector_t DirectionErr = {-999, -999, -999};
300  ShowerEleHolder.SetElement(Direction, DirectionErr, fShowerDirectionOutputLabel);
301  return 0;
302  }
303 }
304 
#define DEFINE_ART_CLASS_TOOL(tool)
Definition: ToolMacros.h:42
ROOT::Math::DisplacementVector3D< ROOT::Math::Cartesian3D< double >, ROOT::Math::GlobalCoordinateSystemTag > Vector_t
Type for representation of momenta in 3D space.
Definition: geo_vectors.h:160
static constexpr Flag_t NoPoint
The trajectory point is not defined.
Point_t const & LocationAtPoint(size_t i) const
Access to track position at different points.
Definition: Track.h:160
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.
Definition: Track.h:136
STL namespace.
MaybeLogger_< ELseverityLevel::ELsev_error, false > LogError
Point_t const & Start() const
Access to track position at different points.
Definition: Track.h:157
parameter set interface
bool CheckElement(const std::string &Name) const
Provides recob::Track data product.
int GetElement(const std::string &Name, T &Element) 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:180
decltype(auto) get(T &&obj)
ADL-aware version of std::to_string.
Definition: StdUtils.h:120
PointFlags_t const & FlagsAtPoint(size_t i) const
Access to i-th TrajectoryPoint or its Flags.
Definition: Track.h:152
int CalculateElement(const art::Ptr< recob::PFParticle > &pfparticle, art::Event &Event, reco::shower::ShowerElementHolder &ShowerEleHolder) override
Vector_t DirectionAtPoint(size_t i) const
Access to track direction at different points.
Definition: Track.h:168
Direction
Definition: types.h:12
Track from a non-cascading particle.A recob::Track consists of a recob::TrackTrajectory, plus additional members relevant for a "fitted" track:
Definition: Track.h:49