WaveformRoiFinder_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // 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.
14 ////////////////////////////////////////////////////////////////////////
15 
24 #include "fhiclcpp/ParameterSet.h"
26 
29 #include "lardataobj/RawData/raw.h"
32 
33 #include <memory>
34 
35 namespace nnet {
36  class WaveformRoiFinder;
37 }
38 
40 public:
41  explicit WaveformRoiFinder(fhicl::ParameterSet const& p);
42  // The compiler-generated destructor is fine for non-base
43  // classes without bare pointers or other resource use.
44 
45  // Plugins should not be copied or assigned.
46  WaveformRoiFinder(WaveformRoiFinder const&) = delete;
50 
51  // Required functions.
52  void produce(art::Event& e) override;
53 
54 private:
57 
58  std::vector<std::unique_ptr<wavrec_tool::IWaveformRecog>> fWaveformRecogToolVec;
59 
60  int fNPlanes;
61  unsigned int fWaveformSize; // Full waveform size
62 };
63 
65  : EDProducer{p}
66  , fRawProducerLabel(p.get<art::InputTag>("RawProducerLabel", ""))
67  , fWireProducerLabel(p.get<art::InputTag>("WireProducerLabel", ""))
68 {
69  // use either raw waveform or recob waveform
71  throw cet::exception("WaveformRoiFinder")
72  << "Both RawProducerLabel and WireProducerLabel are empty";
73  }
74 
76  throw cet::exception("WaveformRoiFinder")
77  << "Only one of RawProducerLabel and WireProducerLabel should be set";
78  }
79 
80  auto const* geo = lar::providerFrom<geo::Geometry>();
81  fNPlanes = geo->Nplanes();
82 
83  // Signal/Noise waveform recognition tool
84  fWaveformRecogToolVec.reserve(fNPlanes);
85  auto const tool_psets = p.get<std::vector<fhicl::ParameterSet>>("WaveformRecogs");
86  fWaveformSize = tool_psets[0].get<unsigned int>("WaveformSize");
87  for (auto const& pset : tool_psets) {
88  fWaveformRecogToolVec.push_back(art::make_tool<wavrec_tool::IWaveformRecog>(pset));
89  }
90 
91  produces<std::vector<recob::Wire>>();
92 }
93 
94 void
96 {
98  std::vector<art::Ptr<raw::RawDigit>> rawlist;
99  if (e.getByLabel(fRawProducerLabel, rawListHandle)) art::fill_ptr_vector(rawlist, rawListHandle);
100 
101  art::Handle<std::vector<recob::Wire>> wireListHandle;
102  std::vector<art::Ptr<recob::Wire>> wirelist;
103  if (e.getByLabel(fWireProducerLabel, wireListHandle))
104  art::fill_ptr_vector(wirelist, wireListHandle);
105 
106  std::unique_ptr<std::vector<recob::Wire>> outwires(new std::vector<recob::Wire>);
107 
108  auto const* geo = lar::providerFrom<geo::Geometry>();
109 
110  //##############################
111  //### Looping over the wires ###
112  //##############################
113  for (unsigned int ich = 0; ich < (rawlist.empty() ? wirelist.size() : rawlist.size()); ++ich) {
114 
115  std::vector<float> inputsignal(fWaveformSize);
116 
117  int view = -1;
118 
119  if (!wirelist.empty()) {
120  const auto& wire = wirelist[ich];
121  const auto& signal = wire->Signal();
122 
123  view = wire->View();
124 
125  for (size_t itck = 0; itck < inputsignal.size(); ++itck) {
126  inputsignal[itck] = signal[itck];
127  }
128  }
129  else if (!rawlist.empty()) {
130  const auto& digitVec = rawlist[ich];
131 
132  view = geo->View(rawlist[ich]->Channel());
133 
134  std::vector<short> rawadc(fWaveformSize);
135  raw::Uncompress(digitVec->ADCs(), rawadc, digitVec->GetPedestal(), digitVec->Compression());
136  for (size_t itck = 0; itck < rawadc.size(); ++itck) {
137  inputsignal[itck] = rawadc[itck] - digitVec->GetPedestal();
138  }
139  }
140 
141  // ... use waveform recognition CNN to perform inference on each window
142  std::vector<bool> inroi(fWaveformSize, false);
143  inroi = fWaveformRecogToolVec[view]->findROI(inputsignal);
144 
145  std::vector<float> sigs;
146  int lastsignaltick = -1;
147  int roistart = -1;
148 
150 
151  for (size_t i = 0; i < fWaveformSize; ++i) {
152  if (inroi[i]) {
153  if (sigs.empty()) {
154  sigs.push_back(inputsignal[i]);
155  lastsignaltick = i;
156  roistart = i;
157  }
158  else {
159  if (int(i) != lastsignaltick + 1) {
160  rois.add_range(roistart, std::move(sigs));
161  sigs.clear();
162  sigs.push_back(inputsignal[i]);
163  lastsignaltick = i;
164  roistart = i;
165  }
166  else {
167  sigs.push_back(inputsignal[i]);
168  lastsignaltick = i;
169  }
170  }
171  }
172  }
173  if (!sigs.empty()) { rois.add_range(roistart, std::move(sigs)); }
174  if (!wirelist.empty()) {
175  outwires->emplace_back(recob::Wire(rois, wirelist[ich]->Channel(), wirelist[ich]->View()));
176  }
177  else if (!rawlist.empty()) {
178  outwires->emplace_back(
179  recob::Wire(rois, rawlist[ich]->Channel(), geo->View(rawlist[ich]->Channel())));
180  }
181  }
182 
183  e.put(std::move(outwires));
184 }
185 
std::vector< std::unique_ptr< wavrec_tool::IWaveformRecog > > fWaveformRecogToolVec
AdcChannelData::View View
EDProducer(fhicl::ParameterSet const &pset)
Definition: EDProducer.h:20
const datarange_t & add_range(size_type offset, ITER first, ITER last)
Adds a sequence of elements as a range with specified offset.
art framework interface to geometry description
bool empty() const noexcept
Definition: InputTag.cc:73
bool getByLabel(std::string const &label, std::string const &instance, Handle< PROD > &result) const
Definition: DataViewImpl.h:633
WaveformRoiFinder(fhicl::ParameterSet const &p)
const double e
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
def move(depos, offset)
Definition: depos.py:107
p
Definition: test.py:223
ProductID put(std::unique_ptr< PROD > &&edp, std::string const &instance={})
Definition: DataViewImpl.h:686
Class holding the regions of interest of signal from a channel.
Definition: Wire.h:118
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:297
void Uncompress(const std::vector< short > &adc, std::vector< short > &uncompressed, raw::Compress_t compress)
Uncompresses a raw data buffer.
Definition: raw.cxx:776
LArSoft geometry interface.
Definition: ChannelGeo.h:16
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
void produce(art::Event &e) override