ShowerTrackTrajToSpacePoint_tool.cc
Go to the documentation of this file.
1 //############################################################################
2 //### Name: ShowerTrackTrajToSpacePoint ###
3 //### Author: Dominic Barker ###
4 //### Date: 01.10.19 ###
5 //### Description: Tool to associate the initial track trajectory points ###
6 //### to the spacepoints ###
7 //############################################################################
8 
9 //Framework Includes
11 
12 //LArSoft Includes
15 
16 namespace ShowerRecoTools {
17 
19 
20  public:
22 
23  //Match trajectory points to the spacepoints
24  int CalculateElement(const art::Ptr<recob::PFParticle>& pfparticle,
26  reco::shower::ShowerElementHolder& ShowerEleHolder) override;
27 
28  private:
29  float fMaxDist; //Max distance that a spacepoint can be from a trajectory
30  //point to be matched
32  int fVerbose;
33 
39  };
40 
42  : IShowerTool(pset.get<fhicl::ParameterSet>("BaseTools"))
43  , fMaxDist(pset.get<float>("MaxDist"))
44  , fPFParticleLabel(pset.get<art::InputTag>("PFParticleLabel"))
45  , fVerbose(pset.get<int>("Verbose"))
47  pset.get<std::string>("InitialTrackSpacePointsOutputLabel"))
48  , fInitialTrackHitsOutputLabel(pset.get<std::string>("InitialTrackHitsOutputLabel"))
49  , fInitialTrackInputTag(pset.get<std::string>("InitialTrackInputTag"))
50  , fShowerStartPositionInputTag(pset.get<std::string>("ShowerStartPositionInputTag"))
51  , fInitialTrackSpacePointsInputTag(pset.get<std::string>("InitialTrackSpacePointsInputTag"))
52  {}
53 
54  int
56  art::Event& Event,
57  reco::shower::ShowerElementHolder& ShowerEleHolder)
58  {
59 
60  //Check the Track has been defined
61  if (!ShowerEleHolder.CheckElement(fInitialTrackInputTag)) {
62  if (fVerbose)
63  mf::LogError("ShowerTrackTrajToSpacePoint") << "Initial track not set" << std::endl;
64  return 0;
65  }
66 
67  //Check the start position is set.
68  if (!ShowerEleHolder.CheckElement(fShowerStartPositionInputTag)) {
69  if (fVerbose)
70  mf::LogError("ShowerTrackTrajToSpacePoint")
71  << "Start position not set, returning " << std::endl;
72  return 0;
73  }
74 
75  //Check the Track Hits has been defined
76  if (!ShowerEleHolder.CheckElement(fInitialTrackSpacePointsInputTag)) {
77  if (fVerbose)
78  mf::LogError("ShowerTrackTrajToSpacePoint")
79  << "Initial track spacepoints not set" << std::endl;
80  return 0;
81  }
82 
83  //Get the start poistion
84  TVector3 ShowerStartPosition = {-999, -999, -999};
85  ShowerEleHolder.GetElement(fShowerStartPositionInputTag, ShowerStartPosition);
86 
87  //Get the initial track hits.
88  std::vector<art::Ptr<recob::SpacePoint>> intitaltrack_sp;
89  ShowerEleHolder.GetElement(fInitialTrackSpacePointsInputTag, intitaltrack_sp);
90 
91  //Get the track
92  recob::Track InitialTrack;
93  ShowerEleHolder.GetElement(fInitialTrackInputTag, InitialTrack);
94 
95  std::vector<art::Ptr<recob::SpacePoint>> new_intitaltrack_sp;
96  //Loop over the trajectory points
97  for (unsigned int traj = 0; traj < InitialTrack.NumberTrajectoryPoints(); ++traj) {
98 
99  //ignore bogus info.
100  auto flags = InitialTrack.FlagsAtPoint(traj);
101  if (flags.isSet(recob::TrajectoryPointFlagTraits::NoPoint)) { continue; }
102 
103  geo::Point_t TrajPositionPoint = InitialTrack.LocationAtPoint(traj);
104  TVector3 TrajPosition = {TrajPositionPoint.X(), TrajPositionPoint.Y(), TrajPositionPoint.Z()};
105 
106  geo::Point_t TrajPositionStartPoint = InitialTrack.LocationAtPoint(0);
107  TVector3 TrajPositionStart = {
108  TrajPositionStartPoint.X(), TrajPositionStartPoint.Y(), TrajPositionStartPoint.Z()};
109 
110  //Ignore values with 0 mag from the start position
111  if ((TrajPosition - TrajPositionStart).Mag() == 0) { continue; }
112  if ((TrajPosition - ShowerStartPosition).Mag() == 0) { continue; }
113 
114  float MinDist = 9999;
115  unsigned int index = 999;
116  for (unsigned int sp = 0; sp < intitaltrack_sp.size(); ++sp) {
117  //Find the spacepoint closest to the trajectory point.
118  art::Ptr<recob::SpacePoint> spacepoint = intitaltrack_sp[sp];
119  TVector3 pos =
120  IShowerTool::GetLArPandoraShowerAlg().SpacePointPosition(spacepoint) - TrajPosition;
121  if (pos.Mag() < MinDist && pos.Mag() < fMaxDist) {
122  MinDist = pos.Mag();
123  index = sp;
124  }
125  }
126 
127  if (index == 999) { continue; }
128  //Add the spacepoint to the track spacepoints.
129  new_intitaltrack_sp.push_back(intitaltrack_sp[index]);
130 
131  //Delete the spacepoint so it can not be used again.
132  intitaltrack_sp.erase(intitaltrack_sp.begin() + index);
133  }
134 
135  // Get the spacepoints
136  auto const spHandle = Event.getValidHandle<std::vector<recob::SpacePoint>>(fPFParticleLabel);
137 
138  // Get the hits associated with the space points
139  const art::FindOneP<recob::Hit>& fohsp =
140  ShowerEleHolder.GetFindOneP<recob::Hit>(spHandle, Event, fPFParticleLabel);
141 
142  //Save the corresponding hits
143  std::vector<art::Ptr<recob::Hit>> trackHits;
144  for (auto const& spacePoint : new_intitaltrack_sp) {
145  //Get the hits
146  const art::Ptr<recob::Hit> hit = fohsp.at(spacePoint.key());
147  trackHits.push_back(hit);
148  }
149 
150  //Save the spacepoints.
151  ShowerEleHolder.SetElement(new_intitaltrack_sp, fInitialTrackSpacePointsOutputLabel);
152  ShowerEleHolder.SetElement(trackHits, fInitialTrackHitsOutputLabel);
153 
154  return 0;
155  }
156 
157 }
158 
#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.
const art::FindOneP< T1 > & GetFindOneP(const art::ValidHandle< std::vector< T2 > > &handle, const art::Event &evt, const art::InputTag &moduleTag)
MaybeLogger_< ELseverityLevel::ELsev_error, false > LogError
int CalculateElement(const art::Ptr< recob::PFParticle > &pfparticle, art::Event &Event, reco::shower::ShowerElementHolder &ShowerEleHolder) override
ValidHandle< PROD > getValidHandle(InputTag const &tag) const
Definition: DataViewImpl.h:441
bool CheckElement(const std::string &Name) const
int GetElement(const std::string &Name, T &Element) const
Detector simulation of raw signals on wires.
const shower::LArPandoraShowerAlg & GetLArPandoraShowerAlg() const
Definition: IShowerTool.h:87
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
Provides recob::Track data product.
PointFlags_t const & FlagsAtPoint(size_t i) const
Definition: Track.h:118
2D representation of charge deposited in the TDC/wire plane
Definition: Hit.h:48
auto const & get(AssnsNode< L, R, D > const &r)
Definition: AssnsNode.h:115
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)
TVector3 SpacePointPosition(art::Ptr< recob::SpacePoint > const &sp) const