LArPandoraModularShowerCreation_module.cc
Go to the documentation of this file.
1 //################################################################################
2 //### Name: LArPandoraModularShowerCreation ###
3 //### Author: Dominic Barker and Ed Tyley (e.tyley@sheffield.ac.uk) ###
4 //### Date: 15.05.19 ###
5 //### Description: Generic Shower Charaterisation module which allows the ###
6 //### the user choose which tool to calculate shower metrics. ###
7 //### For a complete shower the tools must define (use the exact ###
8 //### name) the following metrics in the shower property holder: ###
9 //### ShowerStartPosition ###
10 //### ShowerDirection ###
11 //### ShowerEnergy ###
12 //### ShowerdEdx ###
13 //### ShowerLength ###
14 //### ShowerOpeningAngle ###
15 //################################################################################
16 
17 //Framework includes
21 
22 //LArSoft includes
27 
28 namespace reco::shower {
29  class LArPandoraModularShowerCreation;
30 }
31 
32 //Class
33 
35 public:
37 
38 private:
39  void produce(art::Event& evt);
40 
41  //This function returns the art::Ptr to the data object InstanceName.
42  //In the background it uses the PtrMaker which requires the element index of
43  //the unique ptr (iter).
44  template <class T>
45  art::Ptr<T> GetProducedElementPtr(const std::string& InstanceName,
46  const reco::shower::ShowerElementHolder& ShowerEleHolder,
47  const int& iter = -1);
48 
49  //fcl object names
50  unsigned int fNumPlanes;
53  const int fVerbose;
54  const bool fUseAllParticles;
55 
56  //tool tags which calculate the characteristics of the shower
64 
65  //fcl tools
66  std::vector<std::unique_ptr<ShowerRecoTools::IShowerTool>> fShowerTools;
67  std::vector<std::string> fShowerToolNames;
68 
69  //map to the unique ptrs to
71 
72  // Required services
74 };
75 
76 //This function returns the art::Ptr to the data object InstanceName.
77 //In the background it uses the PtrMaker which requires the element index of
78 //the unique ptr (iter).
79 template <class T>
82  const std::string& InstanceName,
83  const reco::shower::ShowerElementHolder& ShowerEleHolder,
84  const int& iter)
85 {
86 
87  bool check_element = ShowerEleHolder.CheckElement(InstanceName);
88  if (!check_element) {
89  throw cet::exception("LArPandoraModularShowerCreation")
90  << "To get a element that does not exist" << std::endl;
91  }
92 
93  bool check_ptr = uniqueproducerPtrs.CheckUniqueProduerPtr(InstanceName);
94  if (!check_ptr) {
95  throw cet::exception("LArPandoraModularShowerCreation")
96  << "Tried to get a ptr that does not exist" << std::endl;
97  }
98 
99  //Get the number of the shower we are on.
100  int index;
101  if (iter != -1) { index = iter; }
102  else {
103  index = ShowerEleHolder.GetShowerNumber();
104  }
105 
106  //Make the ptr
107  art::Ptr<T> artptr = uniqueproducerPtrs.GetArtPtr<T>(InstanceName, index);
108  return artptr;
109 }
110 
112  fhicl::ParameterSet const& pset)
113  : EDProducer{pset}
114  , fPFParticleLabel(pset.get<art::InputTag>("PFParticleLabel"))
115  , fAllowPartialShowers(pset.get<bool>("AllowPartialShowers"))
116  , fVerbose(pset.get<int>("Verbose", 0))
117  , fUseAllParticles(pset.get<bool>("UseAllParticles", false))
118  , fShowerStartPositionLabel(pset.get<std::string>("ShowerStartPositionLabel"))
119  , fShowerDirectionLabel(pset.get<std::string>("ShowerDirectionLabel"))
120  , fShowerEnergyLabel(pset.get<std::string>("ShowerEnergyLabel"))
121  , fShowerLengthLabel(pset.get<std::string>("ShowerLengthLabel"))
122  , fShowerOpeningAngleLabel(pset.get<std::string>("ShowerOpeningAngleLabel"))
123  , fShowerdEdxLabel(pset.get<std::string>("ShowerdEdxLabel"))
124  , fShowerBestPlaneLabel(pset.get<std::string>("ShowerBestPlaneLabel"))
125 {
126  //Intialise the tools
127  auto tool_psets = pset.get<std::vector<fhicl::ParameterSet>>("ShowerFinderTools");
128  for (auto& tool_pset : tool_psets) {
129 
130  const std::string tool_name(tool_pset.get<std::string>("tool_type"));
131  // If the PFPaticle label is not set for the tool, make it use the one for the module
132  // Note we also need to set the label in the Alg, via the base tool
133  if (!tool_pset.has_key("PFParticleLabel")) {
134 
135  // I cannot pass an art::InputTag as it is mangled, so lets make a string instead
136  const std::string PFParticleLabelString(fPFParticleLabel.label() + ":" +
137  fPFParticleLabel.instance() + ":" +
139 
140  tool_pset.put<std::string>("PFParticleLabel", PFParticleLabelString);
141  fhicl::ParameterSet base_pset = tool_pset.get<fhicl::ParameterSet>("BaseTools");
142  fhicl::ParameterSet alg_pset = base_pset.get<fhicl::ParameterSet>("LArPandoraShowerAlg");
143  alg_pset.put<std::string>("PFParticleLabel", PFParticleLabelString);
144  base_pset.put_or_replace<fhicl::ParameterSet>("LArPandoraShowerAlg", alg_pset);
145  tool_pset.put_or_replace<fhicl::ParameterSet>("BaseTools", base_pset);
146 
147  if (tool_pset.has_key("LArPandoraShowerCheatingAlg")) {
148  fhicl::ParameterSet cheat_alg_pset =
149  tool_pset.get<fhicl::ParameterSet>("LArPandoraShowerCheatingAlg");
150  cheat_alg_pset.put<std::string>("PFParticleLabel", PFParticleLabelString);
151  cheat_alg_pset.put_or_replace<fhicl::ParameterSet>("LArPandoraShowerAlg", alg_pset);
152  tool_pset.put_or_replace<fhicl::ParameterSet>("LArPandoraShowerCheatingAlg",
153  cheat_alg_pset);
154  }
155  }
156 
157  // If we have not explicitly set verboseness for a given tool, use global level
158  if (!tool_pset.has_key("Verbose")) { tool_pset.put<int>("Verbose", fVerbose); }
159 
160  fShowerTools.push_back(art::make_tool<ShowerRecoTools::IShowerTool>(tool_pset));
161  fShowerToolNames.push_back(tool_name);
162 
163  fNumPlanes = fGeom->Nplanes();
164  }
165 
166  // Initialise the EDProducer ptr in the tools
167  std::vector<std::string> SetupTools;
168  for (unsigned int i = 0; i < fShowerTools.size(); ++i) {
169  if (std::find(SetupTools.begin(), SetupTools.end(), fShowerToolNames[i]) != SetupTools.end()) {
170  continue;
171  }
172  fShowerTools[i]->SetPtr(&producesCollector());
173  fShowerTools[i]->InitaliseProducerPtr(uniqueproducerPtrs);
174  fShowerTools[i]->InitialiseProducers();
175  }
176 
177  //Initialise the other paramters.
178 
179  produces<std::vector<recob::Shower>>();
180  produces<art::Assns<recob::Shower, recob::Hit>>();
181  produces<art::Assns<recob::Shower, recob::Cluster>>();
182  produces<art::Assns<recob::Shower, recob::SpacePoint>>();
183  produces<art::Assns<recob::Shower, recob::PFParticle>>();
184 
185  // Output -- showers and associations with hits and clusters
186  uniqueproducerPtrs.SetShowerUniqueProduerPtr(type<std::vector<recob::Shower>>(), "shower");
188  "clusterAssociationsbase");
190  "hitAssociationsbase");
192  "spShowerAssociationsbase");
194  "pfShowerAssociationsbase");
195 
197 }
198 
199 void
201 {
202 
203  //Ptr makers for the products
205  reco::shower::ShowerElementHolder showerEleHolder;
206 
207  //Get the PFParticles
208  auto const pfpHandle = evt.getValidHandle<std::vector<recob::PFParticle>>(fPFParticleLabel);
209  std::vector<art::Ptr<recob::PFParticle>> pfps;
210  art::fill_ptr_vector(pfps, pfpHandle);
211 
212  //Handle to access the pandora hits assans
213  auto const clusterHandle = evt.getValidHandle<std::vector<recob::Cluster>>(fPFParticleLabel);
214 
215  //Get the assoications to hits, clusters and spacespoints
216  const art::FindManyP<recob::Hit>& fmh =
217  showerEleHolder.GetFindManyP<recob::Hit>(clusterHandle, evt, fPFParticleLabel);
218  const art::FindManyP<recob::Cluster>& fmcp =
219  showerEleHolder.GetFindManyP<recob::Cluster>(pfpHandle, evt, fPFParticleLabel);
220  const art::FindManyP<recob::SpacePoint>& fmspp =
221  showerEleHolder.GetFindManyP<recob::SpacePoint>(pfpHandle, evt, fPFParticleLabel);
222 
223  //Holder to pass to the functions, contains the 6 properties of the shower
224  // - Start Poistion
225  // - Direction
226  // - Initial Track
227  // - Initial Track Hits
228  // - Energy
229  // - dEdx
230  // - Length
231  // - Opening Angle
232 
233  int shower_iter = 0;
234  //Loop of the pf particles
235  for (auto const& pfp : pfps) {
236 
237  //Update the shower iterator
238  showerEleHolder.SetShowerNumber(shower_iter);
239 
240  //loop only over showers unless otherwise specified
241  if (!fUseAllParticles && pfp->PdgCode() != 11 && pfp->PdgCode() != 22) continue;
242 
243  //Get the associated hits,clusters and spacepoints
244  const std::vector<art::Ptr<recob::Cluster>> showerClusters = fmcp.at(pfp.key());
245  const std::vector<art::Ptr<recob::SpacePoint>> showerSpacePoints = fmspp.at(pfp.key());
246 
247  // Check the pfp has at least 1 cluster (i.e. not a pfp neutrino)
248  if (!showerClusters.size()) continue;
249 
250  if (fVerbose > 1)
251  mf::LogInfo("LArPandoraModularShowerCreation")
252  << "Running on shower: " << shower_iter << std::endl;
253 
254  //Calculate the shower properties
255  //Loop over the shower tools
256  int err = 0;
257  for (unsigned int i = 0; i < fShowerTools.size(); i++) {
258 
259  //Calculate the metric
260  if (fVerbose > 1)
261  mf::LogInfo("LArPandoraModularShowerCreation")
262  << "Running shower tool: " << fShowerToolNames[i] << std::endl;
263  std::string evd_disp_append = fShowerToolNames[i] + "_iteration" + std::to_string(0) + "_" +
264  this->moduleDescription().moduleLabel();
265 
266  err = fShowerTools[i]->RunShowerTool(pfp, evt, showerEleHolder, evd_disp_append);
267 
268  if (err && fVerbose) {
269  mf::LogError("LArPandoraModularShowerCreation")
270  << "Error in shower tool: " << fShowerToolNames[i] << " with code: " << err << std::endl;
271  }
272  }
273 
274  //If we are are not allowing partial shower check all of the things
275  if (!fAllowPartialShowers) {
276  // If we recieved an error call from a tool return;
277 
278  // Check everything we need is in the shower element holder
279  if (!showerEleHolder.CheckElement(fShowerStartPositionLabel)) {
280  if (fVerbose)
281  mf::LogError("LArPandoraModularShowerCreation")
282  << "The start position is not set in the element holder. bailing" << std::endl;
283  continue;
284  }
285  if (!showerEleHolder.CheckElement(fShowerDirectionLabel)) {
286  if (fVerbose)
287  mf::LogError("LArPandoraModularShowerCreation")
288  << "The direction is not set in the element holder. bailing" << std::endl;
289  continue;
290  }
291  if (!showerEleHolder.CheckElement(fShowerEnergyLabel)) {
292  if (fVerbose)
293  mf::LogError("LArPandoraModularShowerCreation")
294  << "The energy is not set in the element holder. bailing" << std::endl;
295  continue;
296  }
297  if (!showerEleHolder.CheckElement(fShowerdEdxLabel)) {
298  if (fVerbose)
299  mf::LogError("LArPandoraModularShowerCreation")
300  << "The dEdx is not set in the element holder. bailing" << std::endl;
301  continue;
302  }
303  if (!showerEleHolder.CheckElement(fShowerBestPlaneLabel)) {
304  if (fVerbose)
305  mf::LogError("LArPandoraModularShowerCreation")
306  << "The BestPlane is not set in the element holder. bailing" << std::endl;
307  continue;
308  }
309  if (!showerEleHolder.CheckElement(fShowerLengthLabel)) {
310  if (fVerbose)
311  mf::LogError("LArPandoraModularShowerCreation")
312  << "The length is not set in the element holder. bailing" << std::endl;
313  continue;
314  }
315  if (!showerEleHolder.CheckElement(fShowerOpeningAngleLabel)) {
316  if (fVerbose)
317  mf::LogError("LArPandoraModularShowerCreation")
318  << "The opening angle is not set in the element holder. bailing" << std::endl;
319  continue;
320  }
321 
322  //Check All of the products that have been asked to be checked.
323  bool elements_are_set = showerEleHolder.CheckAllElementTags();
324  if (!elements_are_set) {
325  if (fVerbose)
326  mf::LogError("LArPandoraModularShowerCreation")
327  << "Not all the elements in the property holder which should be set are not. Bailing. "
328  << std::endl;
329  continue;
330  }
331 
332  ///Check all the producers
333  bool producers_are_set = uniqueproducerPtrs.CheckAllProducedElements(showerEleHolder);
334  if (!producers_are_set) {
335  if (fVerbose)
336  mf::LogError("LArPandoraModularShowerCreation")
337  << "Not all the elements in the property holder which are produced are not set. "
338  "Bailing. "
339  << std::endl;
340  continue;
341  }
342  }
343 
344  //Get the properties
345  TVector3 ShowerStartPosition(-999, -999, -999);
346  TVector3 ShowerDirection(-999, -999, -999);
347  std::vector<double> ShowerEnergy(fNumPlanes, -999);
348  std::vector<double> ShowerdEdx(fNumPlanes, -999);
349  int BestPlane(-999);
350  double ShowerLength(-999);
351  double ShowerOpeningAngle(-999);
352 
353  TVector3 ShowerStartPositionErr(-999, -999, -999);
354  TVector3 ShowerDirectionErr(-999, -999, -999);
355  std::vector<double> ShowerEnergyErr(fNumPlanes, -999);
356  std::vector<double> ShowerdEdxErr(fNumPlanes, -999);
357 
358  err = 0;
359  if (showerEleHolder.CheckElement(fShowerStartPositionLabel))
360  err += showerEleHolder.GetElementAndError(
361  fShowerStartPositionLabel, ShowerStartPosition, ShowerStartPositionErr);
362  if (showerEleHolder.CheckElement(fShowerDirectionLabel))
363  err += showerEleHolder.GetElementAndError(
364  fShowerDirectionLabel, ShowerDirection, ShowerDirectionErr);
365  if (showerEleHolder.CheckElement(fShowerEnergyLabel))
366  err += showerEleHolder.GetElementAndError(fShowerEnergyLabel, ShowerEnergy, ShowerEnergyErr);
367  if (showerEleHolder.CheckElement(fShowerdEdxLabel))
368  err += showerEleHolder.GetElementAndError(fShowerdEdxLabel, ShowerdEdx, ShowerdEdxErr);
369  if (showerEleHolder.CheckElement(fShowerBestPlaneLabel))
370  err += showerEleHolder.GetElement(fShowerBestPlaneLabel, BestPlane);
371  if (showerEleHolder.CheckElement(fShowerLengthLabel))
372  err += showerEleHolder.GetElement(fShowerLengthLabel, ShowerLength);
373  if (showerEleHolder.CheckElement(fShowerOpeningAngleLabel))
374  err += showerEleHolder.GetElement(fShowerOpeningAngleLabel, ShowerOpeningAngle);
375 
376  if (err) {
377  throw cet::exception("LArPandoraModularShowerCreation")
378  << "Error in LArPandoraModularShowerCreation Module. A Check on a shower property failed "
379  << std::endl;
380  }
381 
382  if (fVerbose > 1) {
383  //Check the shower
384  std::cout << "Shower Vertex: X:" << ShowerStartPosition.X()
385  << " Y: " << ShowerStartPosition.Y() << " Z: " << ShowerStartPosition.Z()
386  << std::endl;
387  std::cout << "Shower Direction: X:" << ShowerDirection.X() << " Y: " << ShowerDirection.Y()
388  << " Z: " << ShowerDirection.Z() << std::endl;
389  std::cout << "Shower dEdx:";
390  for (unsigned int i = 0; i < fNumPlanes; i++) {
391  std::cout << " Plane " << i << ": " << ShowerdEdx.at(i);
392  }
393  std::cout << std::endl;
394  std::cout << "Shower Energy:";
395  for (unsigned int i = 0; i < fNumPlanes; i++) {
396  std::cout << " Plane " << i << ": " << ShowerEnergy.at(i);
397  }
398  std::cout << std::endl;
399  std::cout << "Shower Best Plane: " << BestPlane << std::endl;
400  std::cout << "Shower Length: " << ShowerLength << std::endl;
401  std::cout << "Shower Opening Angle: " << ShowerOpeningAngle << std::endl;
402 
403  //Print what has been created in the shower
404  showerEleHolder.PrintElements();
405  }
406 
407  if (ShowerdEdx.size() != fNumPlanes) {
408  throw cet::exception("LArPandoraModularShowerCreation")
409  << "dEdx vector is wrong size: " << ShowerdEdx.size()
410  << " compared to Nplanes: " << fNumPlanes << std::endl;
411  }
412  if (ShowerEnergy.size() != fNumPlanes) {
413  throw cet::exception("LArPandoraModularShowerCreation")
414  << "Energy vector is wrong size: " << ShowerEnergy.size()
415  << " compared to Nplanes: " << fNumPlanes << std::endl;
416  }
417 
418  //Make the shower
419  recob::Shower shower(ShowerDirection,
420  ShowerDirectionErr,
421  ShowerStartPosition,
422  ShowerDirectionErr,
423  ShowerEnergy,
424  ShowerEnergyErr,
425  ShowerdEdx,
426  ShowerdEdxErr,
427  BestPlane,
429  ShowerLength,
430  ShowerOpeningAngle);
431  showerEleHolder.SetElement(shower, "shower");
432  ++shower_iter;
433  art::Ptr<recob::Shower> ShowerPtr =
434  this->GetProducedElementPtr<recob::Shower>("shower", showerEleHolder);
435 
436  //Associate the pfparticle
438  ShowerPtr, pfp, "pfShowerAssociationsbase");
439 
440  //Add the hits for each "cluster"
441  for (auto const& cluster : showerClusters) {
442 
443  //Associate the clusters
444  std::vector<art::Ptr<recob::Hit>> ClusterHits = fmh.at(cluster.key());
446  ShowerPtr, cluster, "clusterAssociationsbase");
447 
448  //Associate the hits
449  for (auto const& hit : ClusterHits) {
451  ShowerPtr, hit, "hitAssociationsbase");
452  }
453  }
454 
455  //Associate the spacepoints
456  for (auto const& sp : showerSpacePoints) {
458  ShowerPtr, sp, "spShowerAssociationsbase");
459  }
460 
461  //Loop over the tool data products and add them.
462  uniqueproducerPtrs.AddDataProducts(showerEleHolder);
463 
464  //AddAssociations
465  int assn_err = 0;
466  for (auto const& fShowerTool : fShowerTools) {
467  //AddAssociations
468  assn_err += fShowerTool->AddAssociations(pfp, evt, showerEleHolder);
469  }
470  if (!fAllowPartialShowers && assn_err > 0) {
471  if (fVerbose)
472  mf::LogError("LArPandoraModularShowerCreation")
473  << "A association failed and not allowing partial showers. The association will not be "
474  "added to the event "
475  << std::endl;
476  }
477 
478  //Reset the showerproperty holder.
479  showerEleHolder.ClearShower();
480  }
481 
482  //Put everything in the event.
484 
485  //Reset the ptrs to the data products
487 }
488 
void cluster(In first, In last, Out result, Pred *pred)
Definition: NNClusters.h:41
std::string string
Definition: nybbler.cc:12
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
void AddDataProducts(const reco::shower::ShowerElementHolder &selement_holder)
std::string const & moduleLabel() const
EDProducer(fhicl::ParameterSet const &pset)
Definition: EDProducer.h:20
void SetElement(T &dataproduct, const std::string &Name, bool checktag=false)
std::string const & instance() const noexcept
Definition: InputTag.cc:85
constexpr int kBogusI
obviously bogus integer value
Set of hits with a 2D structure.
Definition: Cluster.h:71
const art::FindManyP< T1 > & GetFindManyP(const art::ValidHandle< std::vector< T2 > > &handle, const art::Event &evt, const art::InputTag &moduleTag)
double ShowerEnergy(const ShowerStruct3D &ss3)
Definition: TCShower.cxx:3952
MaybeLogger_< ELseverityLevel::ELsev_error, false > LogError
Cluster finding and building.
std::string const & process() const noexcept
Definition: InputTag.cc:91
bool CheckUniqueProduerPtr(const std::string &Name) const
art::Ptr< T > GetProducedElementPtr(const std::string &InstanceName, const reco::shower::ShowerElementHolder &ShowerEleHolder, const int &iter=-1)
std::string const & label() const noexcept
Definition: InputTag.cc:79
unsigned int Nplanes(unsigned int tpc=0, unsigned int cstat=0) const
Returns the total number of wire planes in the specified TPC.
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
void AddSingle(A &a, B &b, const std::string &Name)
T get(std::string const &key) const
Definition: ParameterSet.h:271
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
void err(const char *fmt,...)
Definition: message.cpp:226
Detector simulation of raw signals on wires.
ProducesCollector & producesCollector() noexcept
bool CheckAllProducedElements(reco::shower::ShowerElementHolder &selement_holder) const
std::vector< std::unique_ptr< ShowerRecoTools::IShowerTool > > fShowerTools
int GetElementAndError(const std::string &Name, T &Element, T2 &ElementErr) const
2D representation of charge deposited in the TDC/wire plane
Definition: Hit.h:48
art::Ptr< T > GetArtPtr(const std::string &Name, const int &iter) const
TCEvent evt
Definition: DataStructs.cxx:7
void fill_ptr_vector(std::vector< Ptr< T >> &ptrs, H const &h)
Definition: Ptr.h:297
void put_or_replace(std::string const &key)
int SetShowerUniqueProduerPtr(type< T >, const std::string &Name, const std::string &Instance="")
std::string to_string(ModuleType const mt)
Definition: ModuleType.h:34
Definition: fwd.h:31
void put(std::string const &key)
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
QTextStream & endl(QTextStream &s)
ModuleDescription const & moduleDescription() const
Definition: ModuleBase.cc:15