StandardRawDigitPrepService_service.cc
Go to the documentation of this file.
1 // StandardRawDigitPrepService_service.cc
2 
4 #include <iostream>
5 #include <sstream>
6 #include <set>
25 
26 using std::string;
27 using std::cout;
28 using std::endl;
29 using std::vector;
30 using std::set;
31 using std::ostringstream;
32 using raw::RawDigit;
33 
34 //**********************************************************************
35 
38 : m_LogLevel(1),
39  m_ChannelStatusOnline(false),
40  m_WiresWithoutROIFlag(2),
41  m_DoDump(false), m_DumpChannel(0), m_DumpTick(0),
42  m_pChannelMappingService(0),
43  m_pChannelStatusProvider(nullptr),
44  m_pExtractSvc(nullptr),
45  m_pmitigateSvc(nullptr),
46  m_pAdcSignalFindingService(nullptr),
47  m_pNoiseRemoval(nullptr),
48  m_pPedestalEvaluation(nullptr),
49  m_pDeconvolutionService(nullptr),
50  m_pRoiBuildingService(nullptr),
51  m_pWireBuildingService(nullptr),
52  m_pAdcChannelDataCopyService(nullptr) {
53  const string myname = "StandardRawDigitPrepService::ctor: ";
54  pset.get_if_present<int>("LogLevel", m_LogLevel);
55  m_SkipBad = pset.get<bool>("SkipBad");
56  m_SkipNoisy = pset.get<bool>("SkipNoisy");
57  pset.get_if_present<bool>("ChannelStatusOnline", m_ChannelStatusOnline);
58  m_DoMitigation = pset.get<bool>("DoMitigation");
59  m_DoEarlySignalFinding = pset.get<bool>("DoEarlySignalFinding");
60  m_DoNoiseRemoval = pset.get<bool>("DoNoiseRemoval");
61  m_DoPedestalAdjustment = pset.get<bool>("DoPedestalAdjustment");
62  m_DoDeconvolution = pset.get<bool>("DoDeconvolution");
63  m_DoROI = pset.get<bool>("DoROI");
64  m_DoWires = pset.get<bool>("DoWires");
65  m_DoIntermediateStates = pset.get<bool>("DoIntermediateStates");
66  pset.get_if_present<bool>("DoDump", m_DoDump);
67  pset.get_if_present<bool>("DoDump", m_DoDump);
68  pset.get_if_present<unsigned int>("WiresWithoutROIFlag", m_WiresWithoutROIFlag);
69  pset.get_if_present<unsigned int>("DumpTick", m_DumpTick);
70  pset.get_if_present<vector<string>>("DisplayTools", m_DisplayTools);
71  if ( m_LogLevel ) cout << myname << "Fetching extract service." << endl;
73  if ( m_SkipBad || m_SkipNoisy ) {
74  if ( m_ChannelStatusOnline ) {
75  if ( m_LogLevel ) cout << myname << "Fetching channel mapping service." << endl;
77  if ( m_LogLevel ) cout << myname << " Channel mapping service: @"
79  }
80  if ( m_LogLevel ) cout << myname << "Fetching channel status provider." << endl;
82  if ( m_LogLevel ) cout << myname << " Channel status provider: @"
84  }
85  if ( m_LogLevel ) cout << myname << " Extract service: @" << m_pExtractSvc << endl;
86  if ( m_DoMitigation ) {
87  if ( m_LogLevel ) cout << myname << "Fetching mitigation service." << endl;
89  if ( m_LogLevel ) cout << myname << " Mitigation service: @" << m_pmitigateSvc << endl;
90  }
91  if ( m_DoEarlySignalFinding ) {
92  if ( m_LogLevel ) cout << myname << "Fetching signal finding service." << endl;
94  if ( m_LogLevel ) cout << myname << " Signal finding service: @" << m_pAdcSignalFindingService << endl;
95  }
96  if ( m_DoNoiseRemoval ) {
97  if ( m_LogLevel ) cout << myname << "Fetching noise removal service." << endl;
99  if ( m_LogLevel ) cout << myname << " Noise removal service: @" << m_pNoiseRemoval << endl;
100  }
101  if ( m_DoPedestalAdjustment ) {
102  if ( m_LogLevel ) cout << myname << "Fetching pedestal evaluation service." << endl;
104  if ( m_LogLevel ) cout << myname << " Pedestal evalution service: @" << m_pPedestalEvaluation << endl;
105  }
106  if ( m_DoDeconvolution ) {
107  if ( m_LogLevel ) cout << myname << "Fetching deconvolution service." << endl;
109  if ( m_LogLevel ) cout << myname << " Deconvolution service: @" << m_pDeconvolutionService << endl;
110  }
111  if ( m_DoROI ) {
112  if ( m_LogLevel ) cout << myname << "Fetching ROI building service." << endl;
114  if ( m_LogLevel ) cout << myname << " ROI building service: @" << m_pRoiBuildingService << endl;
115  }
116  if ( m_DoWires ) {
117  if ( m_LogLevel ) cout << myname << "Fetching wire building service." << endl;
119  if ( m_LogLevel ) cout << myname << " Wire building service: @" << m_pWireBuildingService << endl;
120  }
121  if ( m_DoIntermediateStates > 0 ) {
122  if ( m_LogLevel ) cout << myname << "Fetching intermediate state copying building service." << endl;
124  if ( m_LogLevel ) cout << myname << " Intermediate state copying service: @" << m_pAdcChannelDataCopyService << endl;
125  }
126  if ( m_DoWires && !m_DoROI ) {
127  if ( m_WiresWithoutROIFlag >= 1 ) cout << myname << "WARNING: Wire building requested without ROI building." << endl;
128  if ( m_WiresWithoutROIFlag >= 3 ) abort();
129  }
130  if ( m_DisplayTools.size() ) {
132  if ( ptm == nullptr ) {
133  cout << myname << "ERROR: Unable to retrieve tool manaager." << endl;
134  } else {
135  cout << myname << " Fetching display tools: " << endl;
136  for ( string tname : m_DisplayTools ) {
137  if ( m_LogLevel ) cout << myname << " Fetching " << tname << endl;
138  auto padv = ptm->getPrivate<AdcChannelTool>(tname);
139  if ( padv ) {
140  if ( m_LogLevel ) cout << myname << " Display tool " << tname << ": @" << padv.get() << endl;
141  m_DisplayToolPtrs.push_back(std::move(padv));
142  m_FoundDisplayToolNames.push_back(tname);
143  } else {
144  cout << myname << "ERROR: Unable to retrieve display tool " << tname << endl;
145  }
146  }
147  }
148  }
149  if ( m_LogLevel >=1 ) print(cout, myname);
150 }
151 
152 //**********************************************************************
153 
156  AdcChannelDataMap& datamap,
157  std::vector<recob::Wire>* pwires, WiredAdcChannelDataMap* pintStates) const {
158  const string myname = "StandardRawDigitPrepService:prepare: ";
159  // Extract digits.
160  if ( m_LogLevel >= 2 ) {
161  cout << myname << "Processing digits..." << endl;
162  cout << myname << " # input digits: " << datamap.size() << endl;
163  }
164  int nbad = 0;
165  unsigned int ichan = m_DumpChannel;
166  unsigned int isig = m_DumpTick;
167  set<string> snames;
168  vector<AdcChannel> skipChannels; // Channels to drop from the data map.
169  for ( AdcChannelDataMap::value_type& iacd : datamap ) {
170  AdcChannel chan = iacd.first;
171  AdcChannelData& data = iacd.second;
172  if ( m_LogLevel >= 3 ) cout << myname << "Processing digit for channel " << chan << endl;
173  if ( data.digit == nullptr ) {
174  if ( m_LogLevel >= 2 ) cout << myname << "Skipping null digit." << endl;
175  skipChannels.push_back(chan);
176  continue;
177  }
178  m_pExtractSvc->extract(data);
179  if ( chan != data.digit->Channel() || chan != data.channel() ) {
180  cout << myname << "ERROR: Inconsistent channel number!" << endl;
181  skipChannels.push_back(chan);
182  continue;
183  }
184  if ( m_SkipBad || m_SkipNoisy ) {
185  unsigned int chanstat = chan;
186  if ( m_ChannelStatusOnline ) {
187  unsigned int chanon = m_pChannelMappingService->online(chan);
188  chanstat = chanon;
189  }
190  if ( m_SkipBad && m_pChannelStatusProvider->IsBad(chanstat) ) {
191  if ( m_LogLevel >= 3 ) cout << myname << "Skipping bad channel " << chanstat << endl;
192  skipChannels.push_back(chan);
193  continue;
194  }
195  if ( m_SkipNoisy && m_pChannelStatusProvider->IsNoisy(chanstat) ) {
196  if ( m_LogLevel >= 3 ) cout << myname << "Skipping noisy channel " << chanstat << endl;
197  skipChannels.push_back(chan);
198  continue;
199  }
200  }
201  string state = "extracted";
202  if ( pintStates != nullptr && pintStates->hasData(state) && m_pAdcChannelDataCopyService != nullptr ) {
203  if ( m_LogLevel >= 3 ) cout << myname << "Saving intermediate state " << state << "." << endl;
204  m_pAdcChannelDataCopyService->copy(data, pintStates->dataMaps[state][chan]);
205  snames.insert(state);
206  }
207  if ( m_DoMitigation ) {
208  m_pmitigateSvc->update(data);
209  string state = "mitigated";
210  if ( pintStates != nullptr && pintStates->hasData(state) && m_pAdcChannelDataCopyService != nullptr ) {
211  if ( m_LogLevel >= 3 ) cout << myname << "Saving intermediate state " << state << "." << endl;
212  m_pAdcChannelDataCopyService->copy(data, pintStates->dataMaps[state][chan]);
213  snames.insert(state);
214  }
215  }
216  if ( m_DoEarlySignalFinding ) {
218  }
219  }
220  // Remove the skipped channels from the data map.
221  for ( AdcChannel chan : skipChannels ) {
222  datamap.erase(chan);
223  }
224  if ( m_DoDump ) {
225  cout << myname << "Dumping channel " << m_DumpChannel << ", Tick " << isig << endl;
226  if ( datamap.find(m_DumpChannel) == datamap.end() ) {
227  if ( datamap.size() == 0 ) {
228  cout << "Prepared data is empty." << endl;
229  } else {
230  cout << "Prepared data does not include channel " << m_DumpChannel << endl;
231  cout << " First channel is " << datamap.begin()->first << endl;
232  cout << " Last channel is " << datamap.end()->first << endl;
233  }
234  } else {
235  cout << myname << " Pedestal: " << datamap[ichan].pedestal << endl;
236  if ( isig >= datamap[ichan].raw.size() ) {
237  cout << myname << "Raw data does not include tick. Size is " << datamap[ichan].raw.size() << "." << endl;
238  } else {
239  cout << myname << " raw: " << datamap[ichan].raw[isig] << endl;
240  cout << myname << " flag: " << datamap[ichan].flags[isig] << endl;
241  cout << myname << " After ext: " << datamap[ichan].samples[isig] << endl;
242  }
243  }
244  }
245  if ( m_DoNoiseRemoval ) {
246  m_pNoiseRemoval->update(datamap);
247  string state = "noiseRemoved";
248  if ( pintStates != nullptr && pintStates->hasData(state) && m_pAdcChannelDataCopyService != nullptr ) {
249  if ( m_LogLevel >= 3 ) cout << myname << "Saving intermediate state " << state << "." << endl;
250  for ( const auto& idat : datamap ) {
251  AdcChannel chan = idat.first;
252  const AdcChannelData& data = idat.second;
253  m_pAdcChannelDataCopyService->copy(data, pintStates->dataMaps[state][chan]);
254  snames.insert(state);
255  }
256  }
257  }
258  if ( m_DoDeconvolution ) {
259  for ( AdcChannelDataMap::value_type& chdata : datamap ) {
260  m_pDeconvolutionService->update(clockData, chdata.second);
261  }
262  }
263  if ( m_DoDump ) cout << myname << " After dco: " << datamap[ichan].samples[isig] << endl;
264  if ( m_DoPedestalAdjustment ) {
265  for ( auto& chdata : datamap ) {
266  AdcChannelData& data = chdata.second;
267  AdcSignal ped = 0.0;
268  m_pPedestalEvaluation->evaluate(data, &ped);
269  for ( AdcSignal& sig : data.samples ) sig -= ped;
270  }
271  }
272  if ( m_DoROI ) {
273  for ( auto& chdata : datamap ) {
274  AdcChannelData& acd = chdata.second;
276  if ( m_DoDump && chdata.first == ichan ) {
277  vector<AdcRoi> rois;
278  for ( AdcRoi roi : acd.rois ) if ( isig >= roi.first && isig <= roi.second ) rois.push_back(roi);
279  cout << myname << " After roi: " << rois.size() << " of " << acd.rois.size() << " match: ";
280  for ( AdcRoi roi : rois ) cout << " (" << roi.first << " , " << roi.second << ")";
281  cout << endl;
282  }
283  }
284  }
285  if ( m_DoWires ) {
286  if ( ! m_DoROI ) {
287  if ( m_WiresWithoutROIFlag >= 2 ) {
288  cout << myname << "WARNING: Wire building requested without ROI building." << endl;
289  }
290  if ( m_WiresWithoutROIFlag >= 3 ) abort();
291  }
292  for ( auto& chdata : datamap ) {
293  AdcChannelData& acd = chdata.second;
294  m_pWireBuildingService->build(acd, pwires);
295  if ( m_DoDump && acd.channel() == ichan ) {
296  cout << myname << " Wire: " << pwires->back().Signal().at(isig) << endl;
297  }
298  }
299  for ( string sname : snames ) {
300  WiredAdcChannelDataMap& intStates = *pintStates;
301  auto inamedacdmap = intStates.dataMaps.find(sname);
302  if ( inamedacdmap == intStates.dataMaps.end() ) {
303  cout << myname << "WARNING: State " << sname << " does not have data." << endl;
304  continue;
305  }
306  auto inamedwires = intStates.wires.find(sname);
307  if ( inamedwires == intStates.wires.end() ) {
308  cout << myname << "WARNING: State " << sname << " does not have a wire container." << endl;
309  continue;
310  }
311  AdcChannelDataMap& acdmapState = inamedacdmap->second;
312  std::vector<recob::Wire>* pwiresState = inamedwires->second;
313  for ( auto& chdata : acdmapState ) {
314  AdcChannelData& acd = chdata.second;
315  // Create a single ROI.
316  acd.signal.clear();
317  acd.signal.resize(acd.samples.size(), true);
318  acd.roisFromSignal();
319  // Build wires.
320  m_pWireBuildingService->build(acd, pwiresState);
321  if ( m_DoDump && acd.channel() == ichan ) {
322  cout << myname << " State " << sname << " wire: "
323  << pwires->back().Signal().at(isig) << endl;
324  }
325  }
326  }
327  }
328  if ( m_DisplayToolPtrs.size() ) {
329  unsigned int idto = 0;
330  if ( m_LogLevel >= 2 ) cout << myname << "Running display tools." << endl;
331  for ( const AdcChannelToolPtr& padv : m_DisplayToolPtrs ) {
332  if ( m_LogLevel >= 2 ) {
333  cout << myname << "Running tool " << m_FoundDisplayToolNames[idto] << endl;
334  }
335  padv->viewMap(datamap);
336  ++idto;
337  }
338  }
339 
340  return nbad;
341 }
342 
343 //**********************************************************************
344 
345 std::ostream& StandardRawDigitPrepService::
346 print(std::ostream& out, std::string prefix) const {
347  out << prefix << "StandardRawDigitPrepService:" << endl;
348  out << prefix << " LogLevel: " << m_LogLevel << endl;
349  out << prefix << " SkipBad: " << m_SkipBad << endl;
350  out << prefix << " SkipNoisy: " << m_SkipNoisy << endl;
351  out << prefix << " ChannelStatusOnline: " << m_ChannelStatusOnline << endl;
352  out << prefix << " DoMitigation: " << m_DoMitigation << endl;
353  out << prefix << " DoEarlySignalFinding: " << m_DoEarlySignalFinding << endl;
354  out << prefix << " DoNoiseRemoval: " << m_DoNoiseRemoval << endl;
355  out << prefix << " DoDeconvolution: " << m_DoDeconvolution << endl;
356  out << prefix << " DoPedestalAdjustment: " << m_DoPedestalAdjustment << endl;
357  out << prefix << " DoROI: " << m_DoROI << endl;
358  out << prefix << " DoWires: " << m_DoWires << endl;
359  out << prefix << " DoDump: " << m_DoDump << endl;
360  out << prefix << " DoIntermediateStates: " << m_DoIntermediateStates << endl;
361  if ( m_DoDump ) {
362  out << prefix << " DumpChannel: " << m_DumpChannel << endl;
363  out << prefix << " DumpTick: " << m_DumpTick << endl;
364  }
365  if ( m_DisplayTools.size() ) {
366  cout << prefix << " Display tools:";
367  for ( string tname : m_DisplayTools ) {
368  cout << " " << tname;
369  }
370  cout << endl;
371  } else {
372  cout << prefix << "No display tools." << endl;
373  }
374  return out;
375 }
376 
377 //**********************************************************************
378 
380 
381 //**********************************************************************
virtual bool IsBad(raw::ChannelID_t channel) const =0
Returns whether the specified channel is bad in the current run.
virtual int update(AdcChannelData &data) const =0
Collection of charge vs time digitized from a single readout channel.
Definition: RawDigit.h:69
virtual int find(AdcChannelData &data) const =0
virtual int extract(AdcChannelData &acd) const =0
std::string string
Definition: nybbler.cc:12
virtual int build(AdcChannelData &data) const =0
float AdcSignal
Definition: AdcTypes.h:21
virtual bool IsNoisy(raw::ChannelID_t channel) const =0
Returns whether the specified channel is noisy in the current run.
const AdcNoiseRemovalService * m_pNoiseRemoval
ChannelID_t Channel() const
DAQ channel this raw data was read from.
Definition: RawDigit.h:212
struct vector vector
std::pair< AdcIndex, AdcIndex > AdcRoi
Definition: AdcTypes.h:54
std::vector< std::string > m_DisplayTools
virtual int evaluate(const AdcChannelData &data, AdcSignal *pped=nullptr, AdcSignal *prms=nullptr, AdcSignal *ppederr=nullptr, AdcSignal *prmserr=nullptr) const =0
Raw data description.
const ChannelMappingService * m_pChannelMappingService
const AdcChannelDataCopyService * m_pAdcChannelDataCopyService
const raw::RawDigit * digit
const AdcMitigationService * m_pmitigateSvc
int prepare(detinfo::DetectorClocksData const &clockData, AdcChannelDataMap &prepdigs, std::vector< recob::Wire > *pwires, WiredAdcChannelDataMap *pintStates) const override
const PedestalEvaluationService * m_pPedestalEvaluation
std::map< Name, AdcChannelDataMap > dataMaps
virtual int build(AdcChannelData &data, WireVector *wires) const =0
virtual int copy(const AdcChannelData &oldacd, AdcChannelData &newacd) const =0
StandardRawDigitPrepService(fhicl::ParameterSet const &pset, art::ActivityRegistry &)
const AdcRoiBuildingService * m_pRoiBuildingService
const AdcWireBuildingService * m_pWireBuildingService
AdcRoiVector rois
def move(depos, offset)
Definition: depos.py:107
T get(std::string const &key) const
Definition: ParameterSet.h:271
const AdcSignalFindingService * m_pAdcSignalFindingService
std::vector< AdcChannelToolPtr > m_DisplayToolPtrs
const AdcDeconvolutionService * m_pDeconvolutionService
virtual int update(AdcChannelDataMap &datamap) const =0
const lariov::ChannelStatusProvider * m_pChannelStatusProvider
std::map< Name, WireContainer * > wires
std::unique_ptr< T > getPrivate(std::string name)
Channel channel() const
AdcFilterVector signal
Contains all timing reference information for the detector.
unsigned int AdcChannel
Definition: AdcTypes.h:50
std::optional< T > get_if_present(std::string const &key) const
Definition: ParameterSet.h:224
Interface for experiment-specific channel quality info provider.
std::unique_ptr< AdcChannelTool > AdcChannelToolPtr
int flags() const
Definition: qtextstream.h:232
std::map< AdcChannel, AdcChannelData > AdcChannelDataMap
Interface for experiment-specific service for channel quality info.
const RawDigitExtractService * m_pExtractSvc
virtual int update(detinfo::DetectorClocksData const &clockData, AdcChannelData &data) const =0
std::vector< std::string > m_FoundDisplayToolNames
static DuneToolManager * instance(std::string fclname="", int dbg=1)
AdcSignalVector samples
QTextStream & endl(QTextStream &s)
virtual Channel online(Channel offlineChannel) const =0
std::ostream & print(std::ostream &out=std::cout, std::string prefix="") const override
#define DEFINE_ART_SERVICE_INTERFACE_IMPL(svc, iface)