RunningSumTPFinderPass1_tool.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // Class: RunningSumTPFinderPass1
3 // Plugin Type: service (art v2_10_03)
4 // File: RunningSumTPFinderPass1_service.cc
5 //
6 // Generated at Tue Jun 5 07:51:38 2018 by Philip Rodrigues using cetskelgen
7 // from cetlib version v3_02_00.
8 ////////////////////////////////////////////////////////////////////////
9 
11 
13 
14 #include <algorithm> // for std::transform
15 #include <numeric> // for std::accumulate
16 
17 
19  : m_threshold (p.get<unsigned int> ("Threshold" )),
20  m_useSignalKill (p.get<bool> ("UseSignalKill" , true)),
21  m_signalKillLookahead(p.get<short> ("SignalKillLookahead" , 5)),
22  m_signalKillThreshold(p.get<short> ("SignalKillThreshold" , 15)),
23  m_signalKillNContig (p.get<short> ("SignalKillNContig" , 1)),
24  m_frugalNContig (p.get<short> ("FrugalPedestalNContig", 10)),
25  m_doFiltering (p.get<bool> ("DoFiltering" , true)),
26  m_downsampleFactor (p.get<unsigned int> ("DownsampleFactor" , 1)),
27  m_filterTaps (p.get<std::vector<short>>("FilterCoeffs" , {2, 9, 23, 31, 23, 9, 2})),
28  m_multiplier (std::accumulate(m_filterTaps.begin(), m_filterTaps.end(), 0)),
29  m_runningSumAlpha (p.get<short> ("RunningSumAlpha" , 97))
30  // Default filter taps calculated by:
31  // np.round(scipy.signal.firwin(7, 0.1)*100)
32  // Initialize member data here.
33 {
34 
35 }
36 
37 std::vector<short> RunningSumTPFinderPass1::downSample(const std::vector<short>& orig) {
38 
39  //---------------------------------------------
40  // Do the downsampling
41  //---------------------------------------------
42  if (m_downsampleFactor==1) {
43  return orig;
44  }
45  else {
46  std::vector<short> waveform;
47  for(size_t i=0; i<orig.size(); i+=m_downsampleFactor) {
48  waveform.push_back(orig[i]);
49  }
50  return waveform;
51  }
52 }
53 
54 std::vector<short> RunningSumTPFinderPass1::findPedestal(const std::vector<short>& waveform)
55 {
56  //---------------------------------------------
57  // Pedestal subtraction
58  //---------------------------------------------
59  const std::vector<short>& pedestal=m_useSignalKill ?
60  frugal_pedestal_sigkill(waveform,
65  return pedestal;
66 }
67 
68 std::vector<short> RunningSumTPFinderPass1::filter(const std::vector<short>& pedsub) {
69 
70  //---------------------------------------------
71  // Filtering
72  //---------------------------------------------
73  const size_t ntaps = m_filterTaps.size();
74  const short* taps = m_filterTaps.data();
75 
76  std::vector<short> filtered(m_doFiltering ?
77  apply_fir_filter(pedsub, ntaps, taps) :
78  pedsub);
79  if (!m_doFiltering) {
80  std::transform(filtered.begin(), filtered.end(),
81  filtered.begin(),
82  [=](short a) { return a*m_multiplier; });
83  }
84  return filtered;
85 }
86 
87 std::vector<short> RunningSumTPFinderPass1::runSum(const std::vector<short>& filtered) {
88 
89  //---------------------------------------------
90  // Running Sum
91  //---------------------------------------------
92  std::vector<short> runningSum(filtered.size(), 0); runningSum[0] = 0;
93  for (size_t i=0; i<filtered.size(); ++i) {
94  short sumValue = (filtered[i]/10) + ((runningSum[i-1]/10)*m_runningSumAlpha);
95  sumValue /= 10;
96  if (sumValue < 0) runningSum[i] = 0;
97  else runningSum[i] = sumValue;
98  }
99  return runningSum;
100 }
101 
102 
103 void
104 RunningSumTPFinderPass1::hitFinding(const std::vector<short>& waveform,
105  std::vector<RunningSumTPFinderTool::Hit>& hits,
106  int channel) {
107 
108  //---------------------------------------------
109  // Hit finding
110  //---------------------------------------------
111  bool is_hit = false;
112  bool was_hit = false;
113  RunningSumTPFinderTool::Hit hit(channel, 0, 0, 0);
114  for(size_t isample=0; isample<waveform.size()-1; ++isample){
115  int sample_time = isample * m_downsampleFactor;
116  short adc = waveform[isample];
117  is_hit = adc > (short)m_threshold;
118  if(is_hit && !was_hit) {
119  hit.startTime = sample_time;
120  hit.charge = adc;
122  }
123  if(is_hit && was_hit) {
124  hit.charge += adc*m_downsampleFactor;
126  }
127  if(!is_hit && was_hit) {
128  hit.charge /= m_multiplier;
129  hits.push_back(hit);
130  }
131  was_hit = is_hit;
132  }
133 }
134 
135 std::vector<RunningSumTPFinderTool::Hit>
136 RunningSumTPFinderPass1::findHits(const std::vector<unsigned int>& channel_numbers,
137  const std::vector<std::vector<short>>& collection_samples) {
138 
139  auto hits = std::vector<RunningSumTPFinderTool::Hit>();
140  std::cout << "findHits called with " << collection_samples.size()
141  << " channels. First chan has " << collection_samples[0].size() << " samples" << std::endl;
142 
143  for(size_t ich=0; ich<collection_samples.size(); ++ich){
144  const std::vector<short>& waveformOrig = collection_samples[ich];
145 
146  std::vector<short> waveform = downSample (waveformOrig);
147  std::vector<short> pedestal = findPedestal(waveform );
148  std::vector<short> pedsub(waveform.size(), 0);
149  for(size_t i=0; i<pedsub.size(); ++i)
150  pedsub[i]=waveform[i]-pedestal[i];
151  std::vector<short> filtered = filter(pedsub);
152  std::vector<short> runingSum = runSum(filtered);
153  hitFinding(runingSum, hits, channel_numbers[ich]);
154  }
155  std::cout << "Returning " << hits.size() << " hits" << std::endl;
156  std::cout << "hits/channel=" << float(hits.size())/collection_samples .size() << std::endl;
157  std::cout << "hits/tick=" << float(hits.size())/collection_samples[0].size() << std::endl;
158  return hits;
159 }
160 
std::vector< short > m_filterTaps
#define DEFINE_ART_CLASS_TOOL(tool)
Definition: ToolMacros.h:42
struct vector vector
void hitFinding(const std::vector< short > &waveform, std::vector< RunningSumTPFinderTool::Hit > &hits, int channel)
int16_t adc
Definition: CRTFragment.hh:202
STL namespace.
uint8_t channel
Definition: CRTFragment.hh:201
std::vector< short > filter(const std::vector< short > &orig)
std::vector< short > findPedestal(const std::vector< short > &orig)
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:92
std::vector< short > runSum(const std::vector< short > &orig)
const double a
p
Definition: test.py:223
std::vector< short > apply_fir_filter(const std::vector< short > &input, const size_t ntaps, const short *taps)
Definition: AlgParts.h:104
std::vector< short > frugal_pedestal_sigkill(const std::vector< short > &raw_in, const int lookahead, const int threshold, const int ncontig)
Definition: AlgParts.h:21
std::vector< short > downSample(const std::vector< short > &orig)
auto const & get(AssnsNode< L, R, D > const &r)
Definition: AssnsNode.h:115
int bool
Definition: qglobal.h:345
std::vector< short > frugal_pedestal(const std::vector< short > &raw_in, const int ncontig)
Definition: AlgParts.h:86
virtual std::vector< RunningSumTPFinderTool::Hit > findHits(const std::vector< unsigned int > &channel_numbers, const std::vector< std::vector< short >> &collection_samples)
RunningSumTPFinderPass1(fhicl::ParameterSet const &p)
QTextStream & endl(QTextStream &s)