Public Member Functions | Private Member Functions | Private Attributes | List of all members
ShowerRecoTools::ShowerPCADirection Class Reference
Inheritance diagram for ShowerRecoTools::ShowerPCADirection:
ShowerRecoTools::IShowerTool

Public Member Functions

 ShowerPCADirection (const fhicl::ParameterSet &pset)
 
int CalculateElement (const art::Ptr< recob::PFParticle > &pfparticle, art::Event &Event, reco::shower::ShowerElementHolder &ShowerEleHolder) override
 
- Public Member Functions inherited from ShowerRecoTools::IShowerTool
 IShowerTool (const fhicl::ParameterSet &pset)
 
virtual ~IShowerTool () noexcept=default
 
int RunShowerTool (const art::Ptr< recob::PFParticle > &pfparticle, art::Event &Event, reco::shower::ShowerElementHolder &ShowerEleHolder, std::string evd_display_name_append="")
 
void SetPtr (art::ProducesCollector *collector)
 
void InitaliseProducerPtr (reco::shower::ShowerProducedPtrsHolder &uniqueproducerPtrs)
 

Private Member Functions

void InitialiseProducers () override
 
int AddAssociations (const art::Ptr< recob::PFParticle > &pfpPtr, art::Event &Event, reco::shower::ShowerElementHolder &ShowerEleHolder) override
 
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 GetPCAxisVector (recob::PCAxis &PCAxis)
 

Private Attributes

art::InputTag fPFParticleLabel
 
int fVerbose
 
unsigned int fNSegments
 
bool fUseStartPosition
 
bool fChargeWeighted
 
std::string fShowerStartPositionInputLabel
 
std::string fShowerDirectionOutputLabel
 
std::string fShowerCentreOutputLabel
 
std::string fShowerPCAOutputLabel
 

Additional Inherited Members

- Protected Member Functions inherited from ShowerRecoTools::IShowerTool
const shower::LArPandoraShowerAlgGetLArPandoraShowerAlg () const
 
template<class T >
art::Ptr< T > GetProducedElementPtr (std::string Name, reco::shower::ShowerElementHolder &ShowerEleHolder, int iter=-1)
 
template<class T >
void InitialiseProduct (std::string Name, std::string InstanceName="")
 
template<class T , class A , class B >
void AddSingle (A &a, B &b, std::string Name)
 
int GetVectorPtrSize (std::string Name)
 
void PrintPtrs ()
 
void PrintPtr (std::string Name)
 

Detailed Description

Definition at line 22 of file ShowerPCADirection_tool.cc.

Constructor & Destructor Documentation

ShowerRecoTools::ShowerPCADirection::ShowerPCADirection ( const fhicl::ParameterSet pset)

Definition at line 65 of file ShowerPCADirection_tool.cc.

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  {}
std::string string
Definition: nybbler.cc:12
T get(std::string const &key) const
Definition: ParameterSet.h:271
IShowerTool(const fhicl::ParameterSet &pset)
Definition: IShowerTool.h:28

Member Function Documentation

int ShowerRecoTools::ShowerPCADirection::AddAssociations ( const art::Ptr< recob::PFParticle > &  pfpPtr,
art::Event Event,
reco::shower::ShowerElementHolder ShowerEleHolder 
)
overrideprivatevirtual

Reimplemented from ShowerRecoTools::IShowerTool.

Definition at line 282 of file ShowerPCADirection_tool.cc.

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  }
int GetVectorPtrSize(std::string Name)
Definition: IShowerTool.h:167
MaybeLogger_< ELseverityLevel::ELsev_error, false > LogError
bool CheckElement(const std::string &Name) const
Definition: fwd.h:31
QTextStream & endl(QTextStream &s)
int ShowerRecoTools::ShowerPCADirection::CalculateElement ( const art::Ptr< recob::PFParticle > &  pfparticle,
art::Event Event,
reco::shower::ShowerElementHolder ShowerEleHolder 
)
overridevirtual

Implements ShowerRecoTools::IShowerTool.

Definition at line 87 of file ShowerPCADirection_tool.cc.

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  }
void SetElement(T &dataproduct, const std::string &Name, bool checktag=false)
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
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
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)
2D representation of charge deposited in the TDC/wire plane
Definition: Hit.h:48
QTextStream & endl(QTextStream &s)
recob::PCAxis ShowerRecoTools::ShowerPCADirection::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 
)
private

Definition at line 182 of file ShowerPCADirection_tool.cc.

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  }
double SpacePointTime(art::Ptr< recob::SpacePoint > const &sp, art::FindManyP< recob::Hit > const &fmh) 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
TVector3 ShowerCentre(std::vector< art::Ptr< recob::SpacePoint >> const &showersps) const
double sampling_rate(DetectorClocksData const &data)
Returns the period of the TPC readout electronics clock.
TVector3 SpacePointPosition(art::Ptr< recob::SpacePoint > const &sp) const
TVector3 ShowerRecoTools::ShowerPCADirection::GetPCAxisVector ( recob::PCAxis PCAxis)
private

Definition at line 272 of file ShowerPCADirection_tool.cc.

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  }
const EigenVectors & getEigenVectors() const
Definition: PCAxis.h:66
void ShowerRecoTools::ShowerPCADirection::InitialiseProducers ( )
overrideprivatevirtual

Reimplemented from ShowerRecoTools::IShowerTool.

Definition at line 79 of file ShowerPCADirection_tool.cc.

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  }

Member Data Documentation

bool ShowerRecoTools::ShowerPCADirection::fChargeWeighted
private

Definition at line 57 of file ShowerPCADirection_tool.cc.

unsigned int ShowerRecoTools::ShowerPCADirection::fNSegments
private

Definition at line 54 of file ShowerPCADirection_tool.cc.

art::InputTag ShowerRecoTools::ShowerPCADirection::fPFParticleLabel
private

Definition at line 51 of file ShowerPCADirection_tool.cc.

std::string ShowerRecoTools::ShowerPCADirection::fShowerCentreOutputLabel
private

Definition at line 61 of file ShowerPCADirection_tool.cc.

std::string ShowerRecoTools::ShowerPCADirection::fShowerDirectionOutputLabel
private

Definition at line 60 of file ShowerPCADirection_tool.cc.

std::string ShowerRecoTools::ShowerPCADirection::fShowerPCAOutputLabel
private

Definition at line 62 of file ShowerPCADirection_tool.cc.

std::string ShowerRecoTools::ShowerPCADirection::fShowerStartPositionInputLabel
private

Definition at line 59 of file ShowerPCADirection_tool.cc.

bool ShowerRecoTools::ShowerPCADirection::fUseStartPosition
private

Definition at line 55 of file ShowerPCADirection_tool.cc.

int ShowerRecoTools::ShowerPCADirection::fVerbose
private

Definition at line 52 of file ShowerPCADirection_tool.cc.


The documentation for this class was generated from the following file: