ShowerTrackSpacePointDirection_tool.cc
Go to the documentation of this file.
1 //############################################################################
2 //### Name: ShowerTrackSpacePointDirection ###
3 //### Author: Dominic Barker ###
4 //### Date: 13.05.19 ###
5 //### Description: Tool for finding the shower direction using the ###
6 //### the average direction of theinitial track spacepoints. ###
7 //############################################################################
8 
9 //Framework Includes
11 
12 //LArSoft Includes
14 
15 namespace ShowerRecoTools {
16 
18 
19  public:
21 
22  //Calculate the direction using the initial track spacepoints
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 SP - Vertex) rather than
32  //(Position of SP - Track Start Point).
33 
38  };
39 
41  : IShowerTool(pset.get<fhicl::ParameterSet>("BaseTools"))
42  , fVerbose(pset.get<int>("Verbose"))
43  , fUsePandoraVertex(pset.get<bool>("UsePandoraVertex"))
44  , fInitialTrackSpacePointsInputLabel(pset.get<std::string>("InitialTrackSpacePointsInputLabel"))
45  , fShowerStartPositionInputLabel(pset.get<std::string>("ShowerStartPositionInputLabel"))
46  , fInitialTrackInputLabel(pset.get<std::string>("InitialTrackInputLabel"))
47  , fShowerDirectionOutputLabel(pset.get<std::string>("ShowerDirectionOutputLabel"))
48  {}
49 
50  int
52  const art::Ptr<recob::PFParticle>& pfparticle,
53  art::Event& Event,
54  reco::shower::ShowerElementHolder& ShowerEleHolder)
55  {
56 
57  //Check the Track Hits has been defined
58  if (!ShowerEleHolder.CheckElement(fInitialTrackSpacePointsInputLabel)) {
59  if (fVerbose)
60  mf::LogError("ShowerTrackSpacePointDirection")
61  << "Initial track spacepoints not set" << std::endl;
62  return 0;
63  }
64 
65  //Check the start position is set.
67  if (fVerbose)
68  mf::LogError("ShowerTrackSpacePointDirection")
69  << "Start position not set, returning " << std::endl;
70  return 0;
71  }
72 
73  //Get the start poistion
74  TVector3 StartPosition = {-999, -999, -999};
75  if (fUsePandoraVertex) {
76  ShowerEleHolder.GetElement(fShowerStartPositionInputLabel, StartPosition);
77  }
78  else {
79  //Check the Tracks has been defined
80  if (!ShowerEleHolder.CheckElement(fInitialTrackInputLabel)) {
81  if (fVerbose)
82  mf::LogError("ShowerTrackSpacePointDirection") << "Initial track not set" << std::endl;
83  return 0;
84  }
85  recob::Track InitialTrack;
86  ShowerEleHolder.GetElement(fInitialTrackInputLabel, InitialTrack);
87  geo::Point_t Start_point = InitialTrack.Start();
88  StartPosition = {Start_point.X(), Start_point.Y(), Start_point.Z()};
89  }
90 
91  //Get the initial track hits.
92  std::vector<art::Ptr<recob::SpacePoint>> intitaltrack_sp;
93  ShowerEleHolder.GetElement(fInitialTrackSpacePointsInputLabel, intitaltrack_sp);
94 
95  //Calculate the mean direction and the the standard deviation
96  float sumX = 0, sumX2 = 0;
97  float sumY = 0, sumY2 = 0;
98  float sumZ = 0, sumZ2 = 0;
99 
100  //Get the spacepoints associated to the track hit
101  for (auto const& sp : intitaltrack_sp) {
102 
103  //Get the direction relative to the start positon
104  TVector3 pos = IShowerTool::GetLArPandoraShowerAlg().SpacePointPosition(sp) - StartPosition;
105  if (pos.Mag() == 0) { continue; }
106 
107  sumX = pos.X();
108  sumX2 += pos.X() * pos.X();
109  sumY = pos.Y();
110  sumY2 += pos.Y() * pos.Y();
111  sumZ = pos.Z();
112  sumZ2 += pos.Z() * pos.Z();
113  }
114 
115  float NumSps = intitaltrack_sp.size();
116  TVector3 Mean = {sumX / NumSps, sumY / NumSps, sumZ / NumSps};
117  Mean = Mean.Unit();
118 
119  float RMSX = 999;
120  float RMSY = 999;
121  float RMSZ = 999;
122  if (sumX2 / NumSps - ((sumX / NumSps) * ((sumX / NumSps))) > 0) {
123  RMSX = std::sqrt(sumX2 / NumSps - ((sumX / NumSps) * ((sumX / NumSps))));
124  }
125  if (sumY2 / NumSps - ((sumY / NumSps) * ((sumY / NumSps))) > 0) {
126  RMSY = std::sqrt(sumY2 / NumSps - ((sumY / NumSps) * ((sumY / NumSps))));
127  }
128  if (sumZ2 / NumSps - ((sumZ / NumSps) * ((sumZ / NumSps))) > 0) {
129  RMSZ = std::sqrt(sumZ2 / NumSps - ((sumZ / NumSps) * ((sumZ / NumSps))));
130  }
131 
132  //Loop over the spacepoints and remove ones the relative direction is not within one sigma.
133  TVector3 Direction_Mean = {0, 0, 0};
134  int N = 0;
135  for (auto const sp : intitaltrack_sp) {
136  TVector3 Direction =
138  if ((std::abs((Direction - Mean).X()) < 1 * RMSX) &&
139  (std::abs((Direction - Mean).Y()) < 1 * RMSY) &&
140  (std::abs((Direction - Mean).Z()) < 1 * RMSZ)) {
141  if (Direction.Mag() == 0) { continue; }
142  ++N;
143  Direction_Mean += Direction;
144  }
145  }
146 
147  if (N > 0) {
148  //Take the mean value
149  TVector3 Direction = Direction_Mean.Unit();
150  ShowerEleHolder.SetElement(Direction, fShowerDirectionOutputLabel);
151  }
152  else {
153  if (fVerbose)
154  mf::LogError("ShowerTrackSpacePointDirection")
155  << "None of the points are within 1 sigma" << std::endl;
156  return 1;
157  }
158  return 0;
159  }
160 }
161 
#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.
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
bool CheckElement(const std::string &Name) const
int GetElement(const std::string &Name, T &Element) const
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
Direction
Definition: AssnsIter.h:13
auto const & get(AssnsNode< L, R, D > const &r)
Definition: AssnsNode.h:115
int bool
Definition: qglobal.h:345
int CalculateElement(const art::Ptr< recob::PFParticle > &pfparticle, art::Event &Event, reco::shower::ShowerElementHolder &ShowerEleHolder) override
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