ShowerPCADirection_tool.cc
Go to the documentation of this file.
1 //############################################################################
2 //### Name: ShowerPCADirection ###
3 //### Author: Dominic Barker and Ed Tyley ###
4 //### Date: 13.05.19 ###
5 //### Description: Tool for finding the shower direction using PCA ###
6 //### methods. Derived from LArPandoraModularShowers Method. ###
7 //############################################################################
8 
9 //Framework Includes
11 
12 //LArSoft Includes
16 
17 //C++ Includes
18 #include <Eigen/Dense>
19 
20 namespace ShowerRecoTools {
21 
23 
24  public:
26 
27  //Calculate the direction of the shower.
28  int CalculateElement(const art::Ptr<recob::PFParticle>& pfparticle,
30  reco::shower::ShowerElementHolder& ShowerEleHolder) override;
31 
32  private:
33  void InitialiseProducers() override;
34 
35  //Function to add the assoctions
37  art::Event& Event,
38  reco::shower::ShowerElementHolder& ShowerEleHolder) override;
39 
40  // Define standard art tool interface
42  const detinfo::DetectorClocksData& clockData,
43  const detinfo::DetectorPropertiesData& detProp,
44  const std::vector<art::Ptr<recob::SpacePoint>>& spacePoints_pfp,
45  const art::FindManyP<recob::Hit>& fmh,
46  TVector3& ShowerCentre);
47 
48  TVector3 GetPCAxisVector(recob::PCAxis& PCAxis);
49 
50  //fcl
52  int fVerbose;
53  unsigned int
54  fNSegments; //Used in the RMS gradient. How many segments should we split the shower into.
55  bool fUseStartPosition; //If we use the start position the drection of the
56  //PCA vector is decided as (Shower Centre - Shower Start Position).
57  bool fChargeWeighted; //Should the PCA axis be charge weighted.
58 
63  };
64 
66  : IShowerTool(pset.get<fhicl::ParameterSet>("BaseTools"))
67  , fPFParticleLabel(pset.get<art::InputTag>("PFParticleLabel"))
68  , fVerbose(pset.get<int>("Verbose"))
69  , fNSegments(pset.get<unsigned int>("NSegments"))
70  , fUseStartPosition(pset.get<bool>("UseStartPosition"))
71  , fChargeWeighted(pset.get<bool>("ChargeWeighted"))
72  , fShowerStartPositionInputLabel(pset.get<std::string>("ShowerStartPositionInputLabel"))
73  , fShowerDirectionOutputLabel(pset.get<std::string>("ShowerDirectionOutputLabel"))
74  , fShowerCentreOutputLabel(pset.get<std::string>("ShowerCentreOutputLabel"))
75  , fShowerPCAOutputLabel(pset.get<std::string>("ShowerPCAOutputLabel"))
76  {}
77 
78  void
80  {
81  InitialiseProduct<std::vector<recob::PCAxis>>(fShowerPCAOutputLabel);
82  InitialiseProduct<art::Assns<recob::Shower, recob::PCAxis>>("ShowerPCAxisAssn");
83  InitialiseProduct<art::Assns<recob::PFParticle, recob::PCAxis>>("PFParticlePCAxisAssn");
84  }
85 
86  int
88  art::Event& Event,
89  reco::shower::ShowerElementHolder& ShowerEleHolder)
90  {
91 
92  // Get the assocated pfParicle vertex PFParticles
93  auto const pfpHandle = Event.getValidHandle<std::vector<recob::PFParticle>>(fPFParticleLabel);
94 
95  const art::FindManyP<recob::SpacePoint>& fmspp =
96  ShowerEleHolder.GetFindManyP<recob::SpacePoint>(pfpHandle, Event, fPFParticleLabel);
97 
98  //Get the spacepoints handle and the hit assoication
99  auto const spHandle = Event.getValidHandle<std::vector<recob::SpacePoint>>(fPFParticleLabel);
100 
101  const art::FindManyP<recob::Hit>& fmh =
102  ShowerEleHolder.GetFindManyP<recob::Hit>(spHandle, Event, fPFParticleLabel);
103 
104  //Spacepoints
105  std::vector<art::Ptr<recob::SpacePoint>> spacePoints_pfp = fmspp.at(pfparticle.key());
106 
107  //We cannot progress with no spacepoints.
108  if (spacePoints_pfp.size() < 3) {
109  mf::LogWarning("ShowerPCADirection")
110  << spacePoints_pfp.size() << " spacepoints in shower, not calculating direction"
111  << std::endl;
112  return 1;
113  }
114 
115  auto const clockData =
117  auto const detProp =
119 
120  //Find the PCA vector
121  TVector3 ShowerCentre;
122  recob::PCAxis PCA = CalculateShowerPCA(clockData, detProp, spacePoints_pfp, fmh, ShowerCentre);
123  TVector3 PCADirection = GetPCAxisVector(PCA);
124 
125  //Save the shower the center for downstream tools
126  TVector3 ShowerCentreErr = {-999, -999, -999};
127  ShowerEleHolder.SetElement(ShowerCentre, ShowerCentreErr, fShowerCentreOutputLabel);
128  ShowerEleHolder.SetElement(PCA, fShowerPCAOutputLabel);
129 
130  //Check if we are pointing the correct direction or not, First try the start position
131  if (fUseStartPosition) {
132  if (!ShowerEleHolder.CheckElement(fShowerStartPositionInputLabel)) {
133  if (fVerbose)
134  mf::LogError("ShowerPCADirection")
135  << "fUseStartPosition is set but ShowerStartPosition is not set. Bailing" << std::endl;
136  return 1;
137  }
138  //Get the General direction as the vector between the start position and the centre
139  TVector3 StartPositionVec = {-999, -999, -999};
140  ShowerEleHolder.GetElement(fShowerStartPositionInputLabel, StartPositionVec);
141 
142  // Calculate the general direction of the shower
143  TVector3 GeneralDir = (ShowerCentre - StartPositionVec).Unit();
144 
145  //Calculate the dot product between eigenvector and general direction
146  double DotProduct = PCADirection.Dot(GeneralDir);
147 
148  //If the dotproduct is negative the Direction needs Flipping
149  if (DotProduct < 0) {
150  PCADirection[0] = -PCADirection[0];
151  PCADirection[1] = -PCADirection[1];
152  PCADirection[2] = -PCADirection[2];
153  }
154 
155  //To do
156  TVector3 PCADirectionErr = {-999, -999, -999};
157  ShowerEleHolder.SetElement(PCADirection, PCADirectionErr, fShowerDirectionOutputLabel);
158  return 0;
159  }
160 
161  //Otherwise Check against the RMS of the shower. Method adapated from EMShower Thanks Mike.
163  spacePoints_pfp, ShowerCentre, PCADirection, fNSegments);
164 
165  // If the alg fails to calculate the gradient it will return 0. In this case do nothing
166  // If the gradient is negative, flip the direction of the shower
167  if (RMSGradient < -std::numeric_limits<double>::epsilon()) {
168  PCADirection[0] = -PCADirection[0];
169  PCADirection[1] = -PCADirection[1];
170  PCADirection[2] = -PCADirection[2];
171  }
172 
173  //To do
174  TVector3 PCADirectionErr = {-999, -999, -999};
175 
176  ShowerEleHolder.SetElement(PCADirection, PCADirectionErr, fShowerDirectionOutputLabel);
177  return 0;
178  }
179 
180  //Function to calculate the shower direction using a charge weight 3D PCA calculation.
183  const detinfo::DetectorPropertiesData& detProp,
185  const art::FindManyP<recob::Hit>& fmh,
186  TVector3& ShowerCentre)
187  {
188 
189  float TotalCharge = 0;
190  float sumWeights = 0;
191  float xx = 0;
192  float yy = 0;
193  float zz = 0;
194  float xy = 0;
195  float xz = 0;
196  float yz = 0;
197 
198  //Get the Shower Centre
199  if (fChargeWeighted) {
201  clockData, detProp, sps, fmh, TotalCharge);
202  }
203  else {
205  }
206 
207  //Normalise the spacepoints, charge weight and add to the PCA.
208  for (auto& sp : sps) {
209 
210  TVector3 sp_position = IShowerTool::GetLArPandoraShowerAlg().SpacePointPosition(sp);
211 
212  float wht = 1;
213 
214  //Normalise the spacepoint position.
215  sp_position = sp_position - ShowerCentre;
216 
217  if (fChargeWeighted) {
218 
219  //Get the charge.
220  float Charge = IShowerTool::GetLArPandoraShowerAlg().SpacePointCharge(sp, fmh);
221 
222  //Get the time of the spacepoint
223  float Time = IShowerTool::GetLArPandoraShowerAlg().SpacePointTime(sp, fmh);
224 
225  //Correct for the lifetime at the moment.
226  Charge *= std::exp((sampling_rate(clockData) * Time) / (detProp.ElectronLifetime() * 1e3));
227 
228  //Charge Weight
229  wht *= std::sqrt(Charge / TotalCharge);
230  }
231 
232  xx += sp_position.X() * sp_position.X() * wht;
233  yy += sp_position.Y() * sp_position.Y() * wht;
234  zz += sp_position.Z() * sp_position.Z() * wht;
235  xy += sp_position.X() * sp_position.Y() * wht;
236  xz += sp_position.X() * sp_position.Z() * wht;
237  yz += sp_position.Y() * sp_position.Z() * wht;
238  sumWeights += wht;
239  }
240 
241  // Using Eigen package
242  Eigen::Matrix3f matrix;
243 
244  // Construct covariance matrix
245  matrix << xx, xy, xz, xy, yy, yz, xz, yz, zz;
246 
247  // Normalise from the sum of weights
248  matrix /= sumWeights;
249 
250  // Run the PCA
251  Eigen::SelfAdjointEigenSolver<Eigen::Matrix3f> eigenMatrix(matrix);
252 
253  Eigen::Vector3f eigenValuesVector = eigenMatrix.eigenvalues();
254  Eigen::Matrix3f eigenVectorsMatrix = eigenMatrix.eigenvectors();
255 
256  // Put in the required form for a recob::PCAxis
257  const bool svdOk = true; //TODO: Should probably think about this a bit more
258  const int nHits = sps.size();
259  // For some reason eigen sorts the eigenvalues from smallest to largest, reverse it
260  const double eigenValues[3] = {
261  eigenValuesVector(2), eigenValuesVector(1), eigenValuesVector(0)};
262  std::vector<std::vector<double>> eigenVectors = {
263  {eigenVectorsMatrix(0, 2), eigenVectorsMatrix(1, 2), eigenVectorsMatrix(2, 2)},
264  {eigenVectorsMatrix(0, 1), eigenVectorsMatrix(1, 1), eigenVectorsMatrix(2, 1)},
265  {eigenVectorsMatrix(0, 0), eigenVectorsMatrix(1, 0), eigenVectorsMatrix(2, 0)}};
266  const double avePos[3] = {ShowerCentre[0], ShowerCentre[1], ShowerCentre[2]};
267 
268  return recob::PCAxis(svdOk, nHits, eigenValues, eigenVectors, avePos);
269  }
270 
271  TVector3
273  {
274 
275  //Get the Eigenvectors.
276  std::vector<double> EigenVector = PCAxis.getEigenVectors()[0];
277 
278  return TVector3(EigenVector[0], EigenVector[1], EigenVector[2]);
279  }
280 
281  int
283  art::Event& Event,
284  reco::shower::ShowerElementHolder& ShowerEleHolder)
285  {
286 
287  //First check the element has been set
288  if (!ShowerEleHolder.CheckElement(fShowerPCAOutputLabel)) {
289  if (fVerbose) mf::LogError("ShowerPCADirection: Add Assns") << "PCA not set." << std::endl;
290  return 1;
291  }
292 
294 
295  const art::Ptr<recob::PCAxis> pcaPtr =
296  GetProducedElementPtr<recob::PCAxis>(fShowerPCAOutputLabel, ShowerEleHolder, ptrSize - 1);
297  const art::Ptr<recob::Shower> showerPtr =
298  GetProducedElementPtr<recob::Shower>("shower", ShowerEleHolder);
299 
300  AddSingle<art::Assns<recob::Shower, recob::PCAxis>>(showerPtr, pcaPtr, "ShowerPCAxisAssn");
301  AddSingle<art::Assns<recob::PFParticle, recob::PCAxis>>(pfpPtr, pcaPtr, "PFParticlePCAxisAssn");
302 
303  return 0;
304  }
305 }
const EigenVectors & getEigenVectors() const
Definition: PCAxis.h:66
#define DEFINE_ART_CLASS_TOOL(tool)
Definition: ToolMacros.h:42
std::string string
Definition: nybbler.cc:12
int AddAssociations(const art::Ptr< recob::PFParticle > &pfpPtr, art::Event &Event, reco::shower::ShowerElementHolder &ShowerEleHolder) override
int GetVectorPtrSize(std::string Name)
Definition: IShowerTool.h:167
void SetElement(T &dataproduct, const std::string &Name, bool checktag=false)
struct vector vector
STL namespace.
const art::FindManyP< T1 > & GetFindManyP(const art::ValidHandle< std::vector< T2 > > &handle, const art::Event &evt, const art::InputTag &moduleTag)
double RMSShowerGradient(std::vector< art::Ptr< recob::SpacePoint >> &sps, const TVector3 &ShowerCentre, const TVector3 &Direction, const unsigned int nSegments) const
MaybeLogger_< ELseverityLevel::ELsev_error, false > LogError
double SpacePointTime(art::Ptr< recob::SpacePoint > const &sp, art::FindManyP< recob::Hit > const &fmh) const
ShowerPCADirection(const fhicl::ParameterSet &pset)
int CalculateElement(const art::Ptr< recob::PFParticle > &pfparticle, art::Event &Event, reco::shower::ShowerElementHolder &ShowerEleHolder) override
TVector3 GetPCAxisVector(recob::PCAxis &PCAxis)
key_type key() const noexcept
Definition: Ptr.h:216
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
const shower::LArPandoraShowerAlg & GetLArPandoraShowerAlg() const
Definition: IShowerTool.h:87
double SpacePointCharge(art::Ptr< recob::SpacePoint > const &sp, art::FindManyP< recob::Hit > const &fmh) const
Contains all timing reference information for the detector.
Definition: types.h:32
MaybeLogger_< ELseverityLevel::ELsev_warning, false > LogWarning
recob::PCAxis CalculateShowerPCA(const detinfo::DetectorClocksData &clockData, const detinfo::DetectorPropertiesData &detProp, const std::vector< art::Ptr< recob::SpacePoint >> &spacePoints_pfp, const art::FindManyP< recob::Hit > &fmh, TVector3 &ShowerCentre)
TVector3 ShowerCentre(std::vector< art::Ptr< recob::SpacePoint >> const &showersps) const
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
double sampling_rate(DetectorClocksData const &data)
Returns the period of the TPC readout electronics clock.
int bool
Definition: qglobal.h:345
QTextStream & endl(QTextStream &s)
TVector3 SpacePointPosition(art::Ptr< recob::SpacePoint > const &sp) const