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 
15 namespace ShowerRecoTools {
16 
18 
19  public:
21 
22  //Calculate the shower direction from the initial track hits.
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 (Position of Hit - Vertex)
31  //rather than (Position of Hit - Track Start Point)
34 
39  };
40 
42  : IShowerTool(pset.get<fhicl::ParameterSet>("BaseTools"))
43  , fVerbose(pset.get<int>("Verbose"))
44  , fUsePandoraVertex(pset.get<bool>("UsePandoraVertex"))
45  , fHitModuleLabel(pset.get<art::InputTag>("HitModuleLabel"))
46  , fPFParticleLabel(pset.get<art::InputTag>("PFParticleLabel"))
47  , fInitialTrackHitsInputLabel(pset.get<std::string>("InitialTrackHitsInputLabel"))
48  , fShowerStartPositionInputLabel(pset.get<std::string>("ShowerStartPositionInputLabel"))
49  , fInitialTrackInputLabel(pset.get<std::string>("InitialTrackInputLabel"))
50  , fShowerDirectionOutputLabel(pset.get<std::string>("ShowerDirectionOutputLabel"))
51  {}
52 
53  int
55  art::Event& Event,
56  reco::shower::ShowerElementHolder& ShowerEleHolder)
57  {
58 
59  //Check the Track Hits has been defined
60  if (!ShowerEleHolder.CheckElement(fInitialTrackHitsInputLabel)) {
61  if (fVerbose)
62  mf::LogError("ShowerTrackHitDirection") << "Initial track hits not set" << std::endl;
63  return 0;
64  }
65 
66  //Check the start position is set.
68  if (fVerbose)
69  mf::LogError("ShowerTrackHitDirection")
70  << "Start position not set, returning " << std::endl;
71  return 0;
72  }
73 
74  //Get the start poistion
75  TVector3 StartPosition = {-999, -999, -99};
76  if (fUsePandoraVertex) {
77  ShowerEleHolder.GetElement(fShowerStartPositionInputLabel, StartPosition);
78  }
79  else {
80  //Check the Tracks has been defined
81  if (!ShowerEleHolder.CheckElement(fInitialTrackInputLabel)) {
82  if (fVerbose)
83  mf::LogError("ShowerTrackHitDirection") << "Initial track not set" << std::endl;
84  return 0;
85  }
86  recob::Track InitialTrack;
87  ShowerEleHolder.GetElement(fInitialTrackInputLabel, InitialTrack);
88  geo::Point_t Start_point = InitialTrack.Start();
89  StartPosition = {Start_point.X(), Start_point.Y(), Start_point.Z()};
90  }
91 
92  //Get the spacepoints associated to hits
93  auto const hitHandle = Event.getValidHandle<std::vector<recob::Hit>>(fHitModuleLabel);
94 
95  //Get the spacepoint handle. We need to do this in 3D.
96  const art::FindManyP<recob::SpacePoint>& fmsp =
97  ShowerEleHolder.GetFindManyP<recob::SpacePoint>(hitHandle, Event, fPFParticleLabel);
98 
99  //Get the initial track hits.
100  std::vector<art::Ptr<recob::Hit>> InitialTrackHits;
101  ShowerEleHolder.GetElement(fInitialTrackHitsInputLabel, InitialTrackHits);
102 
103  //Calculate the mean direction and the the standard deviation
104  float sumX = 0, sumX2 = 0;
105  float sumY = 0, sumY2 = 0;
106  float sumZ = 0, sumZ2 = 0;
107 
108  //Get the spacepoints associated to the track hit
109  std::vector<art::Ptr<recob::SpacePoint>> intitaltrack_sp;
110  for (auto const hit : InitialTrackHits) {
111  std::vector<art::Ptr<recob::SpacePoint>> sps = fmsp.at(hit.key());
112  for (auto const sp : sps) {
113  intitaltrack_sp.push_back(sp);
114 
115  //Get the direction relative to the start positon
116  TVector3 pos = IShowerTool::GetLArPandoraShowerAlg().SpacePointPosition(sp) - StartPosition;
117  if (pos.Mag() == 0) { continue; }
118 
119  sumX = pos.X();
120  sumX2 += pos.X() * pos.X();
121  sumY = pos.Y();
122  sumY2 += pos.Y() * pos.Y();
123  sumZ = pos.Z();
124  sumZ2 += pos.Z() * pos.Z();
125  }
126  }
127 
128  float NumSps = intitaltrack_sp.size();
129  TVector3 Mean = {sumX / NumSps, sumY / NumSps, sumZ / NumSps};
130  Mean = Mean.Unit();
131 
132  float RMSX = 999;
133  float RMSY = 999;
134  float RMSZ = 999;
135  if (sumX2 / NumSps - ((sumX / NumSps) * ((sumX / NumSps))) > 0) {
136  RMSX = std::sqrt(sumX2 / NumSps - ((sumX / NumSps) * ((sumX / NumSps))));
137  }
138  if (sumY2 / NumSps - ((sumY / NumSps) * ((sumY / NumSps))) > 0) {
139  RMSY = std::sqrt(sumY2 / NumSps - ((sumY / NumSps) * ((sumY / NumSps))));
140  }
141  if (sumZ2 / NumSps - ((sumZ / NumSps) * ((sumZ / NumSps))) > 0) {
142  RMSZ = std::sqrt(sumZ2 / NumSps - ((sumZ / NumSps) * ((sumZ / NumSps))));
143  }
144 
145  //Loop over the spacepoints and remove ones the relative direction is not within one sigma.
146  TVector3 Direction_Mean = {0, 0, 0};
147  int N = 0;
148  for (auto const sp : intitaltrack_sp) {
149  TVector3 Direction =
151  if ((std::abs((Direction - Mean).X()) < 1 * RMSX) &&
152  (std::abs((Direction - Mean).Y()) < 1 * RMSY) &&
153  (std::abs((Direction - Mean).Z()) < 1 * RMSZ)) {
154  if (Direction.Mag() == 0) { continue; }
155  ++N;
156  Direction_Mean += Direction;
157  }
158  }
159 
160  if (N > 0) {
161  //Take the mean value
162  TVector3 Direction = Direction_Mean.Unit();
163  ShowerEleHolder.SetElement(Direction, fShowerDirectionOutputLabel);
164  }
165  else {
166  if (fVerbose)
167  mf::LogError("ShowerTrackHitDirection")
168  << "None of the points are within 1 sigma" << std::endl;
169  return 1;
170  }
171  return 0;
172  }
173 }
174 
#define DEFINE_ART_CLASS_TOOL(tool)
Definition: ToolMacros.h:42
std::string string
Definition: nybbler.cc:12
void SetElement(T &dataproduct, const std::string &Name, bool checktag=false)
STL namespace.
const art::FindManyP< T1 > & GetFindManyP(const art::ValidHandle< std::vector< T2 > > &handle, const art::Event &evt, const art::InputTag &moduleTag)
MaybeLogger_< ELseverityLevel::ELsev_error, false > LogError
T abs(T value)
Point_t const & Start() const
Access to track position at different points.
Definition: Track.h:123
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
ShowerTrackHitDirection(const fhicl::ParameterSet &pset)
Definition: types.h:32
Direction
Definition: AssnsIter.h:13
int CalculateElement(const art::Ptr< recob::PFParticle > &pfparticle, art::Event &Event, reco::shower::ShowerElementHolder &ShowerEleHolder) override
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)
TVector3 SpacePointPosition(art::Ptr< recob::SpacePoint > const &sp) const