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