All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
HitMerger_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Class: HitMerger
4 // Module Type: producer
5 // File: HitMerger_module.cc
6 //
7 // This module merges hit collections from multiple producers to create a
8 // single output hit collection (including associations)
9 //
10 // Configuration parameters:
11 //
12 // HitProducerLabels - the producers of the recob::Hit objects
13 //
14 // Created by Tracy Usher (usher@slac.stanford.edu) on July 17, 2018
15 //
16 ////////////////////////////////////////////////////////////////////////
17 
18 #include <cmath>
19 #include <algorithm>
20 #include <vector>
21 
22 #include "art/Framework/Core/ModuleMacros.h"
23 #include "art/Framework/Core/EDProducer.h"
24 #include "art/Framework/Principal/Event.h"
25 #include "art/Framework/Services/Registry/ServiceHandle.h"
26 #include "canvas/Utilities/InputTag.h"
27 #include "messagefacility/MessageLogger/MessageLogger.h"
28 
29 #include "art/Persistency/Common/PtrMaker.h"
30 
32 #include "larcore/CoreUtils/ServiceUtil.h" // lar::providerFrom()
39 
40 class HitMerger : public art::EDProducer
41 {
42 public:
43 
44  // Copnstructors, destructor.
45  explicit HitMerger(fhicl::ParameterSet const & pset);
46  virtual ~HitMerger();
47 
48  // Overrides.
49  virtual void reconfigure(fhicl::ParameterSet const & pset);
50  virtual void produce(art::Event & e);
51  virtual void beginJob();
52  virtual void endJob();
53 
54 private:
55  using RecobHitToPtrMap = std::unordered_map<const recob::Hit*, art::Ptr<recob::Hit>>;
56 
57  /**
58  * @brief Create recob::Wire to recob::Hit associations
59  */
60  void makeWireAssns(const art::Event&, art::Assns<recob::Wire, recob::Hit>&, RecobHitToPtrMap&) const;
61 
62  /**
63  * @brief Create raw::RawDigit to recob::Hit associations
64  */
65 
66  void makeRawDigitAssns(const art::Event&, art::Assns<raw::RawDigit, recob::Hit>&, RecobHitToPtrMap&) const;
67  // define vector for hits to make sure of uniform use
68  using HitPtrVector = std::vector<art::Ptr<recob::Hit>>;
69 
70  // Fcl parameters.
71  std::vector<art::InputTag> HitMergerfHitProducerLabelVec; ///< The full collection of hits
72 };
73 
74 DEFINE_ART_MODULE(HitMerger)
75 
76 //----------------------------------------------------------------------------
77 /// Constructor.
78 ///
79 /// Arguments:
80 ///
81 /// pset - Fcl parameters.
82 ///
83 HitMerger::HitMerger(fhicl::ParameterSet const & pset) : EDProducer{pset}
84 {
85  reconfigure(pset);
86 
87  produces< std::vector<recob::Hit>>();
88  produces< art::Assns<recob::Wire, recob::Hit>>();
89  produces< art::Assns<raw::RawDigit, recob::Hit>>();
90 
91  // Report.
92  mf::LogInfo("HitMerger") << "HitMerger configured\n";
93 }
94 
95 //----------------------------------------------------------------------------
96 /// Destructor.
98 {}
99 
100 //----------------------------------------------------------------------------
101 /// Reconfigure method.
102 ///
103 /// Arguments:
104 ///
105 /// pset - Fcl parameter set.
106 ///
107 void HitMerger::reconfigure(fhicl::ParameterSet const & pset)
108 {
109  HitMergerfHitProducerLabelVec = pset.get<std::vector<art::InputTag>>("HitProducerLabelVec");
110 }
111 
112 //----------------------------------------------------------------------------
113 /// Begin job method.
115 {
116 // auto const* detp = lar::providerFrom<detinfo::DetectorPropertiesService>();
117 // auto const* geo = lar::providerFrom<geo::Geometry>();
118 // auto const* ts = lar::providerFrom<detinfo::DetectorClocksService>();
119 }
120 
121 //----------------------------------------------------------------------------
122 /// Produce method.
123 ///
124 /// Arguments:
125 ///
126 /// evt - Art event.
127 ///
128 /// This is the primary method. The goal is to merge input hit collections and
129 /// output a single hit collection on the backside.
130 ///
131 void HitMerger::produce(art::Event & evt)
132 {
133  // container for our new hit collection
134  std::unique_ptr<std::vector<recob::Hit>> outputHitPtrVec(new std::vector<recob::Hit>);
135 
136  /// Associations with wires.
137  std::unique_ptr<art::Assns<recob::Wire, recob::Hit>> wireAssns(new art::Assns<recob::Wire, recob::Hit>);
138 
139  /// Associations with raw digits.
140  std::unique_ptr<art::Assns<raw::RawDigit, recob::Hit>> rawDigitAssns(new art::Assns<raw::RawDigit, recob::Hit>);
141 
142  RecobHitToPtrMap recobHitToPtrMap;
143 
144  // Use this handy art utility to make art::Ptr objects to the new recob::Hits for use in the output phase
145  art::PtrMaker<recob::Hit> ptrMaker(evt);
146 
147  // Outside loop over the input hit producers
148  for(const auto& inputTag : HitMergerfHitProducerLabelVec)
149  {
150  // Start by looking up the original hits
151  art::Handle< std::vector<recob::Hit> > hitHandle;
152  evt.getByLabel(inputTag, hitHandle);
153 
154  for(size_t hitIdx = 0; hitIdx < hitHandle->size(); hitIdx++)
155  {
156  art::Ptr<recob::Hit> hitPtr(hitHandle,hitIdx);
157 
158  const recob::Hit* hit2D = hitPtr.get();
159 
160  // Create and save the new recob::Hit with the correct WireID
161  outputHitPtrVec->emplace_back(recob::HitCreator(*hit2D, hit2D->WireID()).copy());
162 
163  // Recover a pointer to it...
164  recob::Hit* newHit = &outputHitPtrVec->back();
165 
166  // Create a mapping from this hit to an art Ptr representing it
167  recobHitToPtrMap[newHit] = ptrMaker(outputHitPtrVec->size()-1);
168  }
169  }
170 
171  // Set up to make the associations (if desired)
172  makeWireAssns(evt, *wireAssns, recobHitToPtrMap);
173 
174  makeRawDigitAssns(evt, *rawDigitAssns, recobHitToPtrMap);
175 
176  // Move everything into the event
177  evt.put(std::move(outputHitPtrVec));
178  evt.put(std::move(wireAssns));
179  evt.put(std::move(rawDigitAssns));
180 
181  return;
182 }
183 
184 void HitMerger::makeWireAssns(const art::Event& evt, art::Assns<recob::Wire, recob::Hit>& wireAssns, RecobHitToPtrMap& recobHitPtrMap) const
185 {
186  // Let's make sure the input associations container is empty
187  wireAssns = art::Assns<recob::Wire, recob::Hit>();
188 
189  // First task is to recover all of the previous wire <--> hit associations and map them by channel number
190  // Create the temporary container
191  std::unordered_map<raw::ChannelID_t, art::Ptr<recob::Wire>> channelToWireMap;
192 
193  // Go through the list of input sources and fill out the map
194  for(const auto& inputTag : HitMergerfHitProducerLabelVec)
195  {
196  art::ValidHandle<std::vector<recob::Hit>> hitHandle = evt.getValidHandle<std::vector<recob::Hit>>(inputTag);
197 
198  art::FindOneP<recob::Wire> hitToWireAssns(hitHandle, evt, inputTag);
199 
200  if (hitToWireAssns.isValid())
201  {
202  for(size_t wireIdx = 0; wireIdx < hitToWireAssns.size(); wireIdx++)
203  {
204  art::Ptr<recob::Wire> wire = hitToWireAssns.at(wireIdx);
205 
206  channelToWireMap[wire->Channel()] = wire;
207  }
208  }
209  }
210 
211  // Now fill the container
212  for(const auto& hitPtrPair : recobHitPtrMap)
213  {
214  raw::ChannelID_t channel = hitPtrPair.first->Channel();
215 
216  std::unordered_map<raw::ChannelID_t, art::Ptr<recob::Wire>>::iterator chanWireItr = channelToWireMap.find(channel);
217 
218  if (!(chanWireItr != channelToWireMap.end())) continue;
219 
220  wireAssns.addSingle(chanWireItr->second, hitPtrPair.second);
221  }
222 
223  return;
224 }
225 
226 void HitMerger::makeRawDigitAssns(const art::Event& evt, art::Assns<raw::RawDigit, recob::Hit>& rawDigitAssns, RecobHitToPtrMap& recobHitPtrMap) const
227 {
228  // Let's make sure the input associations container is empty
229  rawDigitAssns = art::Assns<raw::RawDigit, recob::Hit>();
230 
231  // First task is to recover all of the previous wire <--> hit associations and map them by channel number
232  // Create the temporary container
233  std::unordered_map<raw::ChannelID_t, art::Ptr<raw::RawDigit>> channelToRawDigitMap;
234 
235  // Go through the list of input sources and fill out the map
236  for(const auto& inputTag : HitMergerfHitProducerLabelVec)
237  {
238  art::ValidHandle<std::vector<recob::Hit>> hitHandle = evt.getValidHandle<std::vector<recob::Hit>>(inputTag);
239 
240  art::FindOneP<raw::RawDigit> hitToRawDigitAssns(hitHandle, evt, inputTag);
241 
242  if (hitToRawDigitAssns.isValid())
243  {
244  for(size_t rawDigitIdx = 0; rawDigitIdx < hitToRawDigitAssns.size(); rawDigitIdx++)
245  {
246  art::Ptr<raw::RawDigit> rawDigit = hitToRawDigitAssns.at(rawDigitIdx);
247 
248  channelToRawDigitMap[rawDigit->Channel()] = rawDigit;
249  }
250  }
251  }
252 
253  // Now fill the container
254  for(const auto& hitPtrPair : recobHitPtrMap)
255  {
256  raw::ChannelID_t channel = hitPtrPair.first->Channel();
257 
258  std::unordered_map<raw::ChannelID_t, art::Ptr<raw::RawDigit>>::iterator chanRawDigitItr = channelToRawDigitMap.find(channel);
259 
260  if (!(chanRawDigitItr != channelToRawDigitMap.end())) continue;
261 
262  rawDigitAssns.addSingle(chanRawDigitItr->second, hitPtrPair.second);
263  }
264 
265  return;
266 }
267 
268 //----------------------------------------------------------------------------
269 /// End job method.
271 {
272  return;
273 }
virtual void endJob()
End job method.
Utilities related to art service access.
void makeWireAssns(const art::Event &, art::Assns< recob::Wire, recob::Hit > &, RecobHitToPtrMap &) const
Create recob::Wire to recob::Hit associations.
std::vector< art::InputTag > HitMergerfHitProducerLabelVec
The full collection of hits.
geo::WireID WireID() const
Definition: Hit.h:233
Declaration of signal hit object.
for pfile in ack l reconfigure(.*) override"` do echo "checking $
virtual ~HitMerger()
Destructor.
HitMerger(fhicl::ParameterSet const &pset)
virtual void beginJob()
Begin job method.
std::unordered_map< const recob::Hit *, art::Ptr< recob::Hit >> RecobHitToPtrMap
Class managing the creation of a new recob::Hit object.
Definition: HitCreator.h:83
Helper functions to create a hit.
virtual void produce(art::Event &e)
std::vector< art::Ptr< recob::Hit >> HitPtrVector
void makeRawDigitAssns(const art::Event &, art::Assns< raw::RawDigit, recob::Hit > &, RecobHitToPtrMap &) const
Create raw::RawDigit to recob::Hit associations.
do i e
T copy(T const &v)
2D representation of charge deposited in the TDC/wire plane
Definition: Hit.h:48
virtual void reconfigure(fhicl::ParameterSet const &pset)
TCEvent evt
Definition: DataStructs.cxx:8
unsigned int ChannelID_t
Type representing the ID of a readout channel.
Definition: RawTypes.h:28
art framework interface to geometry description