LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
ShowerTrackHitDirection_tool.cc
Go to the documentation of this file.
1 //############################################################################
2 //### Name: ShowerTrackHitDirection ###
3 //### Author: Dominic Barker ###
4 //### Date: 13.05.19 ###
5 //### Description: Tool for finding the shower direction using the ###
6 //### initial track the average direction of the initial hits ###
7 //############################################################################
8 
9 //Framework Includes
11 
12 //LArSoft Includes
14 
18 
19 namespace ShowerRecoTools {
20 
22 
23  public:
25 
26  //Calculate the shower direction from the initial track hits.
27  int CalculateElement(const art::Ptr<recob::PFParticle>& pfparticle,
28  art::Event& Event,
29  reco::shower::ShowerElementHolder& ShowerEleHolder) override;
30 
31  private:
32  //fcl
33  int fVerbose;
34  bool fUsePandoraVertex; //Direction from point defined as (Position of Hit - Vertex)
35  //rather than (Position of Hit - Track Start Point)
38 
43  };
44 
46  : IShowerTool(pset.get<fhicl::ParameterSet>("BaseTools"))
47  , fVerbose(pset.get<int>("Verbose"))
48  , fUsePandoraVertex(pset.get<bool>("UsePandoraVertex"))
49  , fHitModuleLabel(pset.get<art::InputTag>("HitModuleLabel"))
50  , fPFParticleLabel(pset.get<art::InputTag>("PFParticleLabel"))
51  , fInitialTrackHitsInputLabel(pset.get<std::string>("InitialTrackHitsInputLabel"))
52  , fShowerStartPositionInputLabel(pset.get<std::string>("ShowerStartPositionInputLabel"))
53  , fInitialTrackInputLabel(pset.get<std::string>("InitialTrackInputLabel"))
54  , fShowerDirectionOutputLabel(pset.get<std::string>("ShowerDirectionOutputLabel"))
55  {}
56 
58  art::Event& Event,
59  reco::shower::ShowerElementHolder& ShowerEleHolder)
60  {
61 
62  //Check the Track Hits has been defined
63  if (!ShowerEleHolder.CheckElement(fInitialTrackHitsInputLabel)) {
64  if (fVerbose)
65  mf::LogError("ShowerTrackHitDirection") << "Initial track hits not set" << std::endl;
66  return 0;
67  }
68 
69  //Check the start position is set.
71  if (fVerbose)
72  mf::LogError("ShowerTrackHitDirection")
73  << "Start position not set, returning " << std::endl;
74  return 0;
75  }
76 
77  //Get the start poistion
78  geo::Point_t StartPosition = {-999, -999, -99};
79  if (fUsePandoraVertex) {
80  ShowerEleHolder.GetElement(fShowerStartPositionInputLabel, StartPosition);
81  }
82  else {
83  //Check the Tracks has been defined
84  if (!ShowerEleHolder.CheckElement(fInitialTrackInputLabel)) {
85  if (fVerbose)
86  mf::LogError("ShowerTrackHitDirection") << "Initial track not set" << std::endl;
87  return 0;
88  }
89  recob::Track InitialTrack;
90  ShowerEleHolder.GetElement(fInitialTrackInputLabel, InitialTrack);
91  geo::Point_t Start_point = InitialTrack.Start();
92  StartPosition = {Start_point.X(), Start_point.Y(), Start_point.Z()};
93  }
94 
95  //Get the spacepoints associated to hits
96  auto const hitHandle = Event.getValidHandle<std::vector<recob::Hit>>(fHitModuleLabel);
97 
98  //Get the spacepoint handle. We need to do this in 3D.
100  ShowerEleHolder.GetFindManyP<recob::SpacePoint>(hitHandle, Event, fPFParticleLabel);
101 
102  //Get the initial track hits.
103  std::vector<art::Ptr<recob::Hit>> InitialTrackHits;
104  ShowerEleHolder.GetElement(fInitialTrackHitsInputLabel, InitialTrackHits);
105 
106  //Calculate the mean direction and the the standard deviation
107  float sumX = 0, sumX2 = 0;
108  float sumY = 0, sumY2 = 0;
109  float sumZ = 0, sumZ2 = 0;
110 
111  //Get the spacepoints associated to the track hit
112  std::vector<art::Ptr<recob::SpacePoint>> intitaltrack_sp;
113  for (auto const hit : InitialTrackHits) {
114  std::vector<art::Ptr<recob::SpacePoint>> sps = fmsp.at(hit.key());
115  for (auto const sp : sps) {
116  intitaltrack_sp.push_back(sp);
117 
118  //Get the direction relative to the start positon
119  auto const pos = sp->position() - StartPosition;
120  if (pos.R() == 0) { continue; }
121 
122  sumX = pos.X();
123  sumX2 += pos.X() * pos.X();
124  sumY = pos.Y();
125  sumY2 += pos.Y() * pos.Y();
126  sumZ = pos.Z();
127  sumZ2 += pos.Z() * pos.Z();
128  }
129  }
130 
131  float NumSps = intitaltrack_sp.size();
132  auto const Mean = geo::Vector_t{sumX / NumSps, sumY / NumSps, sumZ / NumSps}.Unit();
133 
134  float RMSX = 999;
135  float RMSY = 999;
136  float RMSZ = 999;
137  if (sumX2 / NumSps - ((sumX / NumSps) * ((sumX / NumSps))) > 0) {
138  RMSX = std::sqrt(sumX2 / NumSps - ((sumX / NumSps) * ((sumX / NumSps))));
139  }
140  if (sumY2 / NumSps - ((sumY / NumSps) * ((sumY / NumSps))) > 0) {
141  RMSY = std::sqrt(sumY2 / NumSps - ((sumY / NumSps) * ((sumY / NumSps))));
142  }
143  if (sumZ2 / NumSps - ((sumZ / NumSps) * ((sumZ / NumSps))) > 0) {
144  RMSZ = std::sqrt(sumZ2 / NumSps - ((sumZ / NumSps) * ((sumZ / NumSps))));
145  }
146 
147  //Loop over the spacepoints and remove ones the relative direction is not within one sigma.
148  geo::Vector_t Direction_Mean{0, 0, 0};
149  int N = 0;
150  for (auto const sp : intitaltrack_sp) {
151  auto const Direction = sp->position() - StartPosition;
152  if ((std::abs((Direction - Mean).X()) < 1 * RMSX) &&
153  (std::abs((Direction - Mean).Y()) < 1 * RMSY) &&
154  (std::abs((Direction - Mean).Z()) < 1 * RMSZ)) {
155  if (Direction.R() == 0) { continue; }
156  ++N;
157  Direction_Mean += Direction;
158  }
159  }
160 
161  if (N > 0) {
162  //Take the mean value
163  Direction_Mean = Direction_Mean.Unit();
164  ShowerEleHolder.SetElement(Direction_Mean, fShowerDirectionOutputLabel);
165  }
166  else {
167  if (fVerbose)
168  mf::LogError("ShowerTrackHitDirection")
169  << "None of the points are within 1 sigma" << std::endl;
170  return 1;
171  }
172  return 0;
173  }
174 }
175 
#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
Declaration of signal hit object.
Float_t Y
Definition: plot.C:37
void SetElement(T &dataproduct, const std::string &Name, bool checktag=false)
constexpr auto abs(T v)
Returns the absolute value of the argument.
STL namespace.
MaybeLogger_< ELseverityLevel::ELsev_error, false > LogError
Float_t Z
Definition: plot.C:37
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
Detector simulation of raw signals on wires.
ShowerTrackHitDirection(const fhicl::ParameterSet &pset)
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
ValidHandle< PROD > getValidHandle(InputTag const &tag) const
decltype(auto) get(T &&obj)
ADL-aware version of std::to_string.
Definition: StdUtils.h:120
Definition: MVAAlg.h:12
Direction
Definition: types.h:12
int CalculateElement(const art::Ptr< recob::PFParticle > &pfparticle, art::Event &Event, reco::shower::ShowerElementHolder &ShowerEleHolder) override
Float_t X
Definition: plot.C:37
const art::FindManyP< T1 > & GetFindManyP(const art::ValidHandle< std::vector< T2 >> &handle, const art::Event &evt, const art::InputTag &moduleTag)
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