LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
WaveformRoiFinder_module.cc
Go to the documentation of this file.
1 // Class: WaveformRoiFinder
3 // Plugin Type: producer (art v3_05_00)
4 // File: WaveformRoiFinder_module.cc
5 //
6 // Authors: Mike Wang (mwang@fnal.gov)
7 // Lorenzo Uboldi (uboldi.lorenzo@gmail.com)
8 // Tingjun Yang (tjyang@fnal.gov)
9 // Wanwei Wu (wwu@fnal.gov)
10 //
11 // Generated at Fri Apr 10 23:30:12 2020 by Tingjun Yang using cetskelgen
12 // from cetlib version v3_10_00.
13 // Based on the Analyzer module written by Mike Wang.
15 
19 #include "lardataobj/RawData/raw.h"
21 #include "larrecodnn/ImagePatternAlgs/ToolInterfaces/IWaveformRecog.h"
22 
32 #include "cetlib_except/exception.h"
33 #include "fhiclcpp/ParameterSet.h"
35 
36 #include <memory>
37 #include <utility> // std::move()
38 #include <vector>
39 
40 namespace nnet {
41  class WaveformRoiFinder;
42 }
43 
45 public:
46  explicit WaveformRoiFinder(fhicl::ParameterSet const& p);
47  // The compiler-generated destructor is fine for non-base
48  // classes without bare pointers or other resource use.
49 
50  // Plugins should not be copied or assigned.
51  WaveformRoiFinder(WaveformRoiFinder const&) = delete;
55 
56  // Required functions.
57  void produce(art::Event& e) override;
58 
59 private:
62 
63  std::vector<std::unique_ptr<wavrec_tool::IWaveformRecog>> fWaveformRecogToolVec;
64 
65  int fNPlanes;
66  unsigned int fWaveformSize; // Full waveform size
67 };
68 
70  : EDProducer{p}
71  , fRawProducerLabel(p.get<art::InputTag>("RawProducerLabel", ""))
72  , fWireProducerLabel(p.get<art::InputTag>("WireProducerLabel", ""))
73 {
74  // use either raw waveform or recob waveform
76  throw cet::exception("WaveformRoiFinder")
77  << "Both RawProducerLabel and WireProducerLabel are empty";
78  }
79 
81  throw cet::exception("WaveformRoiFinder")
82  << "Only one of RawProducerLabel and WireProducerLabel should be set";
83  }
84 
85  auto const* geo = lar::providerFrom<geo::Geometry>();
86  fNPlanes = geo->Nplanes();
87 
88  // Signal/Noise waveform recognition tool
89  fWaveformRecogToolVec.reserve(fNPlanes);
90  auto const tool_psets = p.get<std::vector<fhicl::ParameterSet>>("WaveformRecogs");
91  fWaveformSize = tool_psets[0].get<unsigned int>("WaveformSize");
92  for (auto const& pset : tool_psets) {
93  fWaveformRecogToolVec.push_back(art::make_tool<wavrec_tool::IWaveformRecog>(pset));
94  }
95 
96  produces<std::vector<recob::Wire>>();
97 }
98 
100 {
102  std::vector<art::Ptr<raw::RawDigit>> rawlist;
103  if (e.getByLabel(fRawProducerLabel, rawListHandle)) art::fill_ptr_vector(rawlist, rawListHandle);
104 
105  art::Handle<std::vector<recob::Wire>> wireListHandle;
106  std::vector<art::Ptr<recob::Wire>> wirelist;
107  if (e.getByLabel(fWireProducerLabel, wireListHandle))
108  art::fill_ptr_vector(wirelist, wireListHandle);
109 
110  std::unique_ptr<std::vector<recob::Wire>> outwires(new std::vector<recob::Wire>);
111 
112  auto const* geo = lar::providerFrom<geo::Geometry>();
113 
114  //##############################
115  //### Looping over the wires ###
116  //##############################
117  for (unsigned int ich = 0; ich < (rawlist.empty() ? wirelist.size() : rawlist.size()); ++ich) {
118 
119  std::vector<float> inputsignal(fWaveformSize);
120 
121  int view = -1;
122 
123  if (!wirelist.empty()) {
124  const auto& wire = wirelist[ich];
125  const auto& signal = wire->Signal();
126 
127  view = wire->View();
128 
129  for (size_t itck = 0; itck < inputsignal.size(); ++itck) {
130  inputsignal[itck] = signal[itck];
131  }
132  }
133  else if (!rawlist.empty()) {
134  const auto& digitVec = rawlist[ich];
135 
136  view = geo->View(rawlist[ich]->Channel());
137 
138  std::vector<short> rawadc(fWaveformSize);
139  raw::Uncompress(digitVec->ADCs(), rawadc, digitVec->GetPedestal(), digitVec->Compression());
140  for (size_t itck = 0; itck < rawadc.size(); ++itck) {
141  inputsignal[itck] = rawadc[itck] - digitVec->GetPedestal();
142  }
143  }
144 
145  // ... use waveform recognition CNN to perform inference on each window
146  std::vector<bool> inroi(fWaveformSize, false);
147  inroi = fWaveformRecogToolVec[view]->findROI(inputsignal);
148 
149  std::vector<float> sigs;
150  int lastsignaltick = -1;
151  int roistart = -1;
152  bool hasROI = false;
153 
155 
156  for (size_t i = 0; i < fWaveformSize; ++i) {
157  if (inroi[i]) {
158  hasROI = true;
159  if (sigs.empty()) {
160  sigs.push_back(inputsignal[i]);
161  lastsignaltick = i;
162  roistart = i;
163  }
164  else {
165  if (int(i) != lastsignaltick + 1) {
166  rois.add_range(roistart, std::move(sigs));
167  sigs.clear();
168  sigs.push_back(inputsignal[i]);
169  lastsignaltick = i;
170  roistart = i;
171  }
172  else {
173  sigs.push_back(inputsignal[i]);
174  lastsignaltick = i;
175  }
176  }
177  }
178  }
179  if (!sigs.empty()) { rois.add_range(roistart, std::move(sigs)); }
180  if (!wirelist.empty() && hasROI) {
181  outwires->emplace_back(recob::Wire(rois, wirelist[ich]->Channel(), wirelist[ich]->View()));
182  }
183  else if (!rawlist.empty() && hasROI) {
184  outwires->emplace_back(
185  recob::Wire(rois, rawlist[ich]->Channel(), geo->View(rawlist[ich]->Channel())));
186  }
187  }
188 
189  e.put(std::move(outwires));
190 }
191 
std::vector< std::unique_ptr< wavrec_tool::IWaveformRecog > > fWaveformRecogToolVec
Utilities related to art service access.
EDProducer(fhicl::ParameterSet const &pset)
Definition: EDProducer.cc:6
const datarange_t & add_range(size_type offset, ITER first, ITER last)
Adds a sequence of elements as a range with specified offset.
Definition of basic raw digits.
bool empty() const noexcept
Definition: InputTag.cc:73
PutHandle< PROD > put(std::unique_ptr< PROD > &&edp, std::string const &instance={})
Definition: Event.h:77
WaveformRoiFinder(fhicl::ParameterSet const &p)
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:65
Collect all the RawData header files together.
Definition: EmTrack.h:40
bool getByLabel(std::string const &label, std::string const &instance, Handle< PROD > &result) const
Class holding the regions of interest of signal from a channel.
Definition: Wire.h:116
Declaration of basic channel signal object.
WaveformRoiFinder & operator=(WaveformRoiFinder const &)=delete
void fill_ptr_vector(std::vector< Ptr< T >> &ptrs, H const &h)
Definition: Ptr.h:306
void Uncompress(const std::vector< short > &adc, std::vector< short > &uncompressed, raw::Compress_t compress)
Uncompresses a raw data buffer.
Definition: raw.cxx:744
Float_t e
Definition: plot.C:35
Namespace collecting geometry-related classes utilities.
art framework interface to geometry description
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
void produce(art::Event &e) override