LArSoft  v10_04_05
Liquid Argon Software toolkit - https://larsoft.org/
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
31 
32 namespace reco::shower {
33  class LArPandoraModularShowerCreation;
34 }
35 
36 //Class
37 
39 public:
41 
42 private:
43  void produce(art::Event& evt);
44 
45  //This function returns the art::Ptr to the data object InstanceName.
46  //In the background it uses the PtrMaker which requires the element index of
47  //the unique ptr (iter).
48  template <class T>
49  art::Ptr<T> GetProducedElementPtr(const std::string& InstanceName,
50  const reco::shower::ShowerElementHolder& ShowerEleHolder,
51  const int& iter = -1);
52 
53  //fcl object names
54  unsigned int fNumPlanes;
57  const int fVerbose;
58  const bool fUseAllParticles;
59 
60  //tool tags which calculate the characteristics of the shower
61  const std::string fShowerStartPositionLabel;
62  const std::string fShowerDirectionLabel;
63  const std::string fShowerEnergyLabel;
64  const std::string fShowerLengthLabel;
65  const std::string fShowerOpeningAngleLabel;
66  const std::string fShowerdEdxLabel;
67  const std::string fShowerBestPlaneLabel;
68 
69  //fcl tools
70  std::vector<std::unique_ptr<ShowerRecoTools::IShowerTool>> fShowerTools;
71  std::vector<std::string> fShowerToolNames;
72 
73  //map to the unique ptrs to
75 };
76 
77 //This function returns the art::Ptr to the data object InstanceName.
78 //In the background it uses the PtrMaker which requires the element index of
79 //the unique ptr (iter).
80 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  auto const& wireReadoutGeom = art::ServiceHandle<geo::WireReadout>()->Get();
164  fNumPlanes = wireReadoutGeom.Nplanes();
165  }
166 
167  // Initialise the EDProducer ptr in the tools
168  std::vector<std::string> SetupTools;
169  for (unsigned int i = 0; i < fShowerTools.size(); ++i) {
170  if (std::find(SetupTools.begin(), SetupTools.end(), fShowerToolNames[i]) != SetupTools.end()) {
171  continue;
172  }
173  fShowerTools[i]->SetPtr(&producesCollector());
174  fShowerTools[i]->InitaliseProducerPtr(uniqueproducerPtrs);
175  fShowerTools[i]->InitialiseProducers();
176  }
177 
178  //Initialise the other paramters.
179 
180  produces<std::vector<recob::Shower>>();
181  produces<art::Assns<recob::Shower, recob::Hit>>();
182  produces<art::Assns<recob::Shower, recob::Cluster>>();
183  produces<art::Assns<recob::Shower, recob::SpacePoint>>();
184  produces<art::Assns<recob::Shower, recob::PFParticle>>();
185 
186  // Output -- showers and associations with hits and clusters
187  uniqueproducerPtrs.SetShowerUniqueProduerPtr(type<std::vector<recob::Shower>>(), "shower");
189  "clusterAssociationsbase");
191  "hitAssociationsbase");
193  "spShowerAssociationsbase");
195  "pfShowerAssociationsbase");
196 
198 }
199 
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 
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  geo::Point_t ShowerStartPosition(-999, -999, -999);
346  geo::Vector_t 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  geo::Point_t ShowerStartPositionErr(-999, -999, -999);
354  geo::Vector_t 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  using namespace geo::vect;
420  recob::Shower shower(convertTo<TVector3>(ShowerDirection),
421  convertTo<TVector3>(ShowerDirectionErr),
422  convertTo<TVector3>(ShowerStartPosition),
423  convertTo<TVector3>(ShowerDirectionErr),
424  ShowerEnergy,
425  ShowerEnergyErr,
426  ShowerdEdx,
427  ShowerdEdxErr,
428  BestPlane,
430  ShowerLength,
431  ShowerOpeningAngle);
432  showerEleHolder.SetElement(shower, "shower");
433  ++shower_iter;
434  art::Ptr<recob::Shower> ShowerPtr =
435  this->GetProducedElementPtr<recob::Shower>("shower", showerEleHolder);
436 
437  //Associate the pfparticle
439  ShowerPtr, pfp, "pfShowerAssociationsbase");
440 
441  //Add the hits for each "cluster"
442  for (auto const& cluster : showerClusters) {
443 
444  //Associate the clusters
445  std::vector<art::Ptr<recob::Hit>> ClusterHits = fmh.at(cluster.key());
447  ShowerPtr, cluster, "clusterAssociationsbase");
448 
449  //Associate the hits
450  for (auto const& hit : ClusterHits) {
452  ShowerPtr, hit, "hitAssociationsbase");
453  }
454  }
455 
456  //Associate the spacepoints
457  for (auto const& sp : showerSpacePoints) {
459  ShowerPtr, sp, "spShowerAssociationsbase");
460  }
461 
462  //Loop over the tool data products and add them.
463  uniqueproducerPtrs.AddDataProducts(showerEleHolder);
464 
465  //AddAssociations
466  int assn_err = 0;
467  for (auto const& fShowerTool : fShowerTools) {
468  //AddAssociations
469  assn_err += fShowerTool->AddAssociations(pfp, evt, showerEleHolder);
470  }
471  if (!fAllowPartialShowers && assn_err > 0) {
472  if (fVerbose)
473  mf::LogError("LArPandoraModularShowerCreation")
474  << "A association failed and not allowing partial showers. The association will not be "
475  "added to the event "
476  << std::endl;
477  }
478 
479  //Reset the showerproperty holder.
480  showerEleHolder.ClearShower();
481  }
482 
483  //Put everything in the event.
485 
486  //Reset the ptrs to the data products
488 }
489 
ROOT::Math::DisplacementVector3D< ROOT::Math::Cartesian3D< double >, ROOT::Math::GlobalCoordinateSystemTag > Vector_t
Type for representation of momenta in 3D space.
Definition: geo_vectors.h:160
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
void AddDataProducts(const reco::shower::ShowerElementHolder &selement_holder)
std::string const & moduleLabel() const
Declaration of signal hit object.
EDProducer(fhicl::ParameterSet const &pset)
Definition: EDProducer.cc:6
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
cout<< "Opened file "<< fin<< " ixs= "<< ixs<< endl;if(ixs==0) hhh=(TH1F *) fff-> Get("h1")
Definition: AddMC.C:8
Set of hits with a 2D structure.
Definition: Cluster.h:69
double ShowerEnergy(const ShowerStruct3D &ss3)
Definition: TCShower.cxx:3899
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
Utilities to manipulate geometry vectors.The utilities include generic vector interface facilities al...
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:65
void AddSingle(A &a, B &b, const std::string &Name)
decltype(auto) constexpr to_string(T &&obj)
ADL-aware version of std::to_string.
T get(std::string const &key) const
Definition: ParameterSet.h:314
bool CheckElement(const std::string &Name) const
int GetElement(const std::string &Name, T &Element) const
Declaration of cluster object.
Detector simulation of raw signals on wires.
ProducesCollector & producesCollector() noexcept
bool CheckAllProducedElements(reco::shower::ShowerElementHolder &selement_holder) const
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:180
ValidHandle< PROD > getValidHandle(InputTag const &tag) const
Utility object to perform functions of association.
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:46
art::Ptr< T > GetArtPtr(const std::string &Name, const int &iter) const
TCEvent evt
Definition: DataStructs.cxx:8
void fill_ptr_vector(std::vector< Ptr< T >> &ptrs, H const &h)
Definition: Ptr.h:306
void put_or_replace(std::string const &key)
int SetShowerUniqueProduerPtr(type< T >, const std::string &Name, const std::string &Instance="")
const art::FindManyP< T1 > & GetFindManyP(const art::ValidHandle< std::vector< T2 >> &handle, const art::Event &evt, const art::InputTag &moduleTag)
Definition: fwd.h:26
void put(std::string const &key)
art framework interface to geometry description
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
ModuleDescription const & moduleDescription() const
Definition: ModuleBase.cc:13