All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ShowerReco3D_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // Class: ShowerReco3D
3 // Module Type: producer
4 // File: ShowerReco3D_module.cc
5 //
6 // Generated at Tue Jan 20 07:26:48 2015 by Kazuhiro Terao using artmod
7 // from cetpkgsupport v1_08_02.
8 ////////////////////////////////////////////////////////////////////////
9 
10 #include "art/Framework/Core/EDProducer.h"
11 #include "art/Framework/Core/ModuleMacros.h"
12 #include "art/Framework/Principal/Event.h"
13 #include "art/Framework/Principal/Handle.h"
14 #include "art/Framework/Services/Registry/ServiceHandle.h"
15 #include "canvas/Persistency/Common/FindManyP.h"
16 #include "fhiclcpp/ParameterSet.h"
17 
35 
38 
39 #include <memory>
40 #include <string>
41 
42 class ShowerReco3D : public art::EDProducer {
43 public:
44  explicit ShowerReco3D(fhicl::ParameterSet const& p);
45  // The destructor generated by the compiler is fine for classes
46  // without bare pointers or other resource use.
47 
48  // Plugins should not be copied or assigned.
49  ShowerReco3D(ShowerReco3D const&) = delete;
50  ShowerReco3D(ShowerReco3D&&) = delete;
51  ShowerReco3D& operator=(ShowerReco3D const&) = delete;
52  ShowerReco3D& operator=(ShowerReco3D&&) = delete;
53 
54 private:
55  // Required functions.
56  void produce(art::Event& e) override;
57 
58  // Declare member data here.
60  std::string fInputProducer;
68 };
69 
70 ShowerReco3D::ShowerReco3D(fhicl::ParameterSet const& p) : EDProducer{p}
71 {
72  fInputProducer = p.get<std::string>("InputProducer");
73  fUsePFParticle = p.get<bool>("UsePFParticle");
74  produces<std::vector<recob::Shower>>();
75  produces<art::Assns<recob::Shower, recob::Cluster>>();
76  produces<art::Assns<recob::Shower, recob::Hit>>();
77  if (fUsePFParticle) produces<art::Assns<recob::PFParticle, recob::Shower>>();
78 
79  // Instantiate algorithms
80  fCaloAlgo = new ::calo::CalorimetryAlg(p.get<fhicl::ParameterSet>("CalorimetryAlg"));
81  fShowerAlgo = new ::showerreco::ShowerRecoAlg;
82  fCPAlgoArray = new ::cmtool::CPAlgoArray;
83  fCPAlgoNHits = new ::cmtool::CPAlgoNHits;
84  fCPAlgoIgnoreTracks = new ::cmtool::CPAlgoIgnoreTracks;
85  fCFAlgoTimeOverlap = new ::cmtool::CFAlgoTimeOverlap;
86 
87  // Configure
88  fCPAlgoNHits->SetMinHits(p.get<int>("MinHits"));
89 
90  fCPAlgoArray->AddAlgo(fCPAlgoNHits);
91  fCPAlgoArray->AddAlgo(fCPAlgoIgnoreTracks);
92 
93  fManager.MatchManager().AddPriorityAlgo(fCPAlgoArray);
94  fManager.MatchManager().AddMatchAlgo(fCFAlgoTimeOverlap);
95 
96  fShowerAlgo->Verbose(p.get<bool>("Verbosity"));
97  fShowerAlgo->SetUseArea(p.get<bool>("UseArea"));
98  fShowerAlgo->setEcorrection(p.get<bool>("ApplyMCEnergyCorrection"));
99  fShowerAlgo->CaloAlgo(fCaloAlgo);
100 
101  fManager.Algo(fShowerAlgo);
102 }
103 
104 void
106 {
107  auto const& geom = *lar::providerFrom<geo::Geometry>();
108  auto const clockData = art::ServiceHandle<detinfo::DetectorClocksService const>()->DataFor(e);
109  auto const detProp =
110  art::ServiceHandle<detinfo::DetectorPropertiesService const>()->DataFor(e, clockData);
111  util::GeometryUtilities const gser{geom, clockData, detProp};
112 
113  // Create output data product containers
114  std::unique_ptr<std::vector<recob::Shower>> out_shower_v(new std::vector<recob::Shower>);
115  std::unique_ptr<art::Assns<recob::Shower, recob::Cluster>> sc_assn(
116  new art::Assns<recob::Shower, recob::Cluster>);
117  std::unique_ptr<art::Assns<recob::Shower, recob::Hit>> sh_assn(
118  new art::Assns<recob::Shower, recob::Hit>);
119  std::unique_ptr<art::Assns<recob::PFParticle, recob::Shower>> sp_assn(
120  new art::Assns<recob::PFParticle, recob::Shower>);
121 
122  // Preparation
123 
124  // Reset ShowerRecoManager
125  fManager.Reset();
126 
127  // Retrieve input clusters
128  art::Handle<std::vector<recob::Cluster>> cHandle;
129  e.getByLabel(fInputProducer, cHandle);
130 
131  if (!cHandle.isValid())
132  throw cet::exception(__FUNCTION__) << "Invalid input cluster label!" << std::endl;
133 
134  // Cluster type conversion: recob::Hit => util::PxHit
135  std::vector<std::vector<::util::PxHit>> local_clusters;
136  art::FindManyP<recob::Hit> hit_m(cHandle, e, fInputProducer);
137  ::util::PxHitConverter conv{gser};
138  for (size_t i = 0; i < cHandle->size(); ++i) {
139  local_clusters.push_back(std::vector<::util::PxHit>());
140 
141  const std::vector<art::Ptr<recob::Hit>>& hits = hit_m.at(i);
142 
143  conv.GeneratePxHit(hits, local_clusters.back());
144  }
145 
146  //
147  // Run shower reconstruction
148  //
149  // shower pfparticle index (later used to make an association)
150  std::vector<size_t> shower_pfpart_index;
151  // cluster combination in terms of cluster index (for ShowerRecoManager::Reconstruct)
152  std::vector<std::vector<unsigned int>> matched_pairs;
153  // shower vector container to receive from ShowerRecoManager::Reconstruct
154  std::vector<recob::Shower> shower_v;
155 
156  if (!fUsePFParticle) {
157  matched_pairs = fManager.Reconstruct(geom, clockData, detProp, local_clusters, shower_v);
158  }
159  else {
160 
161  // Retrieve PFParticle
162  art::Handle<std::vector<recob::PFParticle>> pfHandle;
163  e.getByLabel(fInputProducer, pfHandle);
164  if (!pfHandle.isValid())
165  throw cet::exception(__FUNCTION__) << "Invalid input PFParticle label!" << std::endl;
166 
167  // Make a cluster ptr => index map to fill matched_pairs
168  std::map<art::Ptr<recob::Cluster>, size_t> cmap;
169  for (size_t i = 0; i < cHandle->size(); ++i) {
170 
171  const art::Ptr<recob::Cluster> cptr(cHandle, i);
172 
173  cmap[cptr] = i;
174  }
175  // Now to fill matched_pairs retrieve association of pfpart => cluster(s)
176  art::FindManyP<recob::Cluster> cluster_m(pfHandle, e, fInputProducer);
177 
178  for (size_t i = 0; i < pfHandle->size(); ++i) {
179 
180  const art::Ptr<recob::PFParticle> pf(pfHandle, i);
181 
182  if (pf->PdgCode() != 11) continue;
183 
184  const std::vector<art::Ptr<recob::Cluster>>& clusters = cluster_m.at(i);
185 
186  std::vector<unsigned int> one_pair;
187  one_pair.reserve(clusters.size());
188 
189  for (auto const& cptr : clusters) {
190 
191  auto iter = cmap.find(cptr);
192  if (iter == cmap.end())
193  throw cet::exception(__FUNCTION__)
194  << "PFParticle=>Cluster association not valid!" << std::endl;
195 
196  one_pair.push_back((*iter).second);
197  }
198  matched_pairs.push_back(one_pair);
199  shower_pfpart_index.push_back(i);
200  }
201  // Run reconstruction
202  fManager.Reconstruct(geom, clockData, detProp, local_clusters, matched_pairs, shower_v);
203  }
204 
205  // Make sure output shower vector size is same as expected length
206  if (shower_v.size() != matched_pairs.size())
207  throw cet::exception(__FUNCTION__)
208  << "Logic error: # of matched pairs != # of reco-ed showers!" << std::endl;
209 
210  // Fill output shower vector data container
211  out_shower_v->reserve(shower_v.size());
212 
213  for (size_t i = 0; i < shower_v.size(); ++i) {
214 
215  // Set output shower ID
216  shower_v[i].set_id(i);
217 
218  out_shower_v->push_back(shower_v[i]);
219 
220  // Create shower=>cluster association
221  std::vector<art::Ptr<recob::Cluster>> ass_clusters;
222  // Create shower=>hit association
223  std::vector<art::Ptr<recob::Hit>> ass_hits;
224  for (auto const& cindex : matched_pairs[i]) {
225 
226  ass_clusters.push_back(art::Ptr<recob::Cluster>(cHandle, cindex));
227 
228  const std::vector<art::Ptr<recob::Hit>>& hits = hit_m.at(cindex);
229 
230  for (auto const& ptr : hits)
231  ass_hits.push_back(ptr);
232  }
233  // Shower=>Cluster
234  util::CreateAssn(*this, e, *(out_shower_v.get()), ass_clusters, *(sc_assn.get()));
235  // Shower=>Hits
236  util::CreateAssn(*this, e, *(out_shower_v.get()), ass_hits, *(sh_assn.get()));
237 
238  if (fUsePFParticle) {
239 
240  art::Handle<std::vector<recob::PFParticle>> pfHandle;
241  e.getByLabel(fInputProducer, pfHandle);
242 
243  art::Ptr<recob::PFParticle> pf_ptr(pfHandle, shower_pfpart_index[i]);
244 
245  util::CreateAssn(*this, e, *(out_shower_v.get()), pf_ptr, *(sp_assn.get()));
246  }
247  }
248 
249  // Store in an event record
250  e.put(std::move(out_shower_v));
251  e.put(std::move(sh_assn));
252  e.put(std::move(sc_assn));
253  if (fUsePFParticle) e.put(std::move(sp_assn));
254 }
255 
256 DEFINE_ART_MODULE(ShowerReco3D)
::calo::CalorimetryAlg * fCaloAlgo
Utilities related to art service access.
::cmtool::CPAlgoArray * fCPAlgoArray
Declaration of signal hit object.
::showerreco::ShowerRecoManager fManager
pdgs p
Definition: selectors.fcl:22
::cmtool::CFAlgoTimeOverlap * fCFAlgoTimeOverlap
void produce(art::Event &e) override
std::string fInputProducer
BEGIN_PROLOG local::showertrajpointdedx icarus_pandorashower_3dTraj ShowerFinderTools[3] CalorimetryAlg
Class def header for a class CPAlgoArray.
Declaration of cluster object.
Class def header for a class CPAlgoNHits.
bool CreateAssn(art::Event &evt, std::vector< T > const &a, art::Ptr< U > const &b, art::Assns< U, T > &assn, std::string a_instance, size_t index=UINT_MAX)
Creates a single one-to-one association.
ShowerReco3D(fhicl::ParameterSet const &p)
::cmtool::CPAlgoIgnoreTracks * fCPAlgoIgnoreTracks
::showerreco::ShowerRecoAlg * fShowerAlgo
Class def header for a class CMatchManager.
Class def header for a class CPAlgoIgnoreTracks.
ShowerReco3D & operator=(ShowerReco3D const &)=delete
do i e
Class def header for a class CFAlgoTimeOverlap.
::cmtool::CPAlgoNHits * fCPAlgoNHits
art framework interface to geometry description
auto const detProp
ClusterAss_t Reconstruct(geo::GeometryCore const &geom, detinfo::DetectorClocksData const &clockData, detinfo::DetectorPropertiesData const &detProp, const std::vector< std::vector< util::PxHit >> &clusters, std::vector<::recob::Shower > &showers)
Class def header for a class ShowerRecoManager.