DuneDPhaseRoiBuildingService_service.cc
Go to the documentation of this file.
1 // DuneDPhaseRoiBuildingService_service.cc
2 
4 #include <iostream>
5 #include <sstream>
6 #include <iomanip>
11 
12 using std::vector;
13 using std::string;
14 using std::ostream;
15 using std::cout;
16 using std::endl;
17 using std::setw;
18 using art::ServiceHandle;
19 
20 //**********************************************************************
21 
24  m_UseFilter( pset.get<bool>("UseFilter") ),
25  m_FltCoeffs( pset.get<std::vector<float>>("FltCoeffs") ),
26  m_LogLevel(1)
27 {
28  const string myname = "DuneDPhaseRoiBuildingService::ctor: ";
29  pset.get_if_present<int>("LogLevel", m_LogLevel);
30  m_NSigmaStart = pset.get<AdcSignal>("NSigmaStart");
31  m_NSigmaEnd = pset.get<AdcSignal>("NSigmaEnd");
32  m_PadLow = pset.get<AdcSignal>("PadLow");
33  m_PadHigh = pset.get<AdcSignal>("PadHigh");
34  if ( m_LogLevel > 0 ) print(cout, myname);
35 }
36 
37 
38 //**********************************************************************
39 
41  const string myname = "DuneDPhaseRoiBuildingService:build: ";
42  if ( m_LogLevel >= 2 ) cout << myname << "Building ROIs for channel "
43  << data.channel() << "." << endl;
44  data.rois.clear();
45  // Get signal shaping service.
47  AdcSignal sigma = hsss->GetDeconNoise(data.channel());
48 
49  // Use filtered or raw ADC.
50  AdcSignalVector sigs;
51  if (m_UseFilter) { sigs = getLowFreqFiltered(data.samples); }
52  else { sigs = data.samples; }
53 
54  // Build ROIS before padding and merging.
55  AdcFilterVector& signal = data.signal;
56  AdcRoiVector& rois = data.rois;
57  signal.clear();
58  signal.resize(sigs.size(), false);
59  bool inroi = false;
60  AdcSignal siglow = m_NSigmaEnd*sigma;
61  AdcSignal sighigh = m_NSigmaStart*sigma;
62  AdcIndex nsig = sigs.size();
63  if ( nsig < 1 ) {
64  if ( m_LogLevel >= 2 ) cout << myname << "Channel " << data.channel()
65  << " has no samples." << endl;
66  return 0;
67  }
68  for ( AdcIndex isig=0; isig<sigs.size(); ++isig ) {
69  AdcSignal sig = sigs[isig];
70  if ( inroi ) {
71  if ( sig > siglow ) {
72  signal[isig] = true;
73  } else {
74  inroi = false;
75  }
76  } else {
77  if ( sig > sighigh ) {
78  signal[isig] = true;
79  inroi = true;
80  }
81  }
82  }
83  // Fill the unpadded ROIs.
84  data.roisFromSignal();
85  // Display ROIs before padding and merging.
86  if ( m_LogLevel >= 3 ) {
87  cout << myname << " ROIs before merge (size = " << rois.size() << "):" << endl;
88  for ( const AdcRoi& roi : rois ) {
89  cout << myname << setw(8) << roi.first << " " << setw(8) << roi.second << endl;
90  }
91  } else if ( m_LogLevel >= 2 ) {
92  cout << myname << " ROI count before merge: " << data.rois.size() << endl;
93  }
94  if ( rois.size() == 0 ) return 0;
95  // Pad ROIs.
96  unsigned int isig1 = 0;
97  unsigned int isig2 = 0;
98  for ( AdcRoi roi : rois ) {
99  isig2 = roi.first;
100  isig1 = isig2 > m_PadLow ? isig2 - m_PadLow : 0;
101  for ( unsigned int isig=isig1; isig<isig2; ++isig ) signal[isig] = true;
102  isig1 = roi.second + 1;
103  isig2 = isig1 + m_PadHigh;
104  if ( isig2 > nsig ) isig2 = nsig;
105  for ( unsigned int isig=isig1; isig<isig2; ++isig ) signal[isig] = true;
106  }
107  // Fill the final ROIs.
108  data.roisFromSignal();
109  // Display final ROIs.
110  if ( m_LogLevel >= 3 ) {
111  cout << myname << " ROIs after merge (size = " << rois.size() << "):" << endl;
112  for ( const AdcRoi& roi : rois ) {
113  cout << myname << setw(8) << roi.first << " " << setw(8) << roi.second << endl;
114  }
115  } else if ( m_LogLevel >= 2 ) {
116  cout << myname << " ROI count after merge: " << data.rois.size() << endl;
117  }
118  return 0;
119 }
120 //**********************************************************************
121 
124 {
126  std::vector< TComplex > ch_spectrum(fft->FFTSize() / 2 + 1);
127  std::vector< float > ch_waveform(fft->FFTSize(), 0);
128 
129  size_t n_samples = adc.size();
130 
131  std::copy(adc.begin(), adc.end(), ch_waveform.begin());
132  for (size_t s = n_samples; s < ch_waveform.size(); ++s)
133  {
134  ch_waveform[s] = ch_waveform[s-1];
135  }
136  fft->DoFFT(ch_waveform, ch_spectrum);
137  for (size_t c = 0; c < m_FltCoeffs.size(); ++c)
138  {
139  ch_spectrum[c] *= m_FltCoeffs[c];
140  }
141  fft->DoInvFFT(ch_spectrum, ch_waveform);
142 
143  AdcSignalVector flt_out(n_samples);
144  std::copy(ch_waveform.begin(), ch_waveform.begin()+n_samples, flt_out.begin());
145  return flt_out;
146 }
147 //**********************************************************************
148 
150 print(ostream& out, string prefix) const {
151  out << prefix << "DuneDPhaseRoiBuildingService:" << endl;
152  out << prefix << " LogLevel: " << m_LogLevel << endl;
153  out << prefix << " NSigmaStart: " << m_NSigmaStart << endl;
154  out << prefix << " NSigmaEnd: " << m_NSigmaEnd << endl;
155  out << prefix << " PadLow: " << m_PadLow << endl;
156  out << prefix << " PadHigh: " << m_PadHigh << endl;
157  out << prefix << " UseFilter: " << m_UseFilter << endl;
158  return out;
159 }
160 //**********************************************************************
161 
163 
164 //**********************************************************************
AdcSignalVector getLowFreqFiltered(const AdcSignalVector &adc) const
DuneDPhaseRoiBuildingService(fhicl::ParameterSet const &pset, art::ActivityRegistry &)
std::string string
Definition: nybbler.cc:12
float AdcSignal
Definition: AdcTypes.h:21
struct vector vector
std::pair< AdcIndex, AdcIndex > AdcRoi
Definition: AdcTypes.h:54
int16_t adc
Definition: CRTFragment.hh:202
STL namespace.
void DoFFT(std::vector< T > &input, std::vector< TComplex > &output)
Definition: LArFFT.h:98
uint size() const
Definition: qcstring.h:201
double GetDeconNoise(Channel channel) const override
void DoInvFFT(std::vector< TComplex > &input, std::vector< T > &output)
Definition: LArFFT.h:120
int FFTSize() const
Definition: LArFFT.h:69
AdcRoiVector rois
T get(std::string const &key) const
Definition: ParameterSet.h:271
unsigned int AdcIndex
Definition: AdcTypes.h:15
Service to provide DUNE dual-phase signal shaping for simulation (convolution) and reconstruction (de...
Q_EXPORT QTSManip setw(int w)
Definition: qtextstream.h:331
std::ostream & print(std::ostream &out=std::cout, std::string prefix="") const
std::vector< AdcRoi > AdcRoiVector
Definition: AdcTypes.h:55
Channel channel() const
AdcFilterVector signal
std::vector< bool > AdcFilterVector
Definition: AdcTypes.h:27
std::optional< T > get_if_present(std::string const &key) const
Definition: ParameterSet.h:224
T copy(T const &v)
std::vector< AdcSignal > AdcSignalVector
Definition: AdcTypes.h:22
auto const & get(AssnsNode< L, R, D > const &r)
Definition: AssnsNode.h:115
int bool
Definition: qglobal.h:345
static QCString * s
Definition: config.cpp:1042
AdcSignalVector samples
QTextStream & endl(QTextStream &s)
#define DEFINE_ART_SERVICE_INTERFACE_IMPL(svc, iface)