RunningSumTPFinderPass2_tool.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // Class: RunningSumTPFinderPass2
3 // Plugin Type: service (art v2_10_03)
4 // File: RunningSumTPFinderPass2_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" , 10)),
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  // Default filter taps calculated by:
30  // np.round(scipy.signal.firwin(7, 0.1)*100)
31  // Initialize member data here.
32 {
33  std::cout << "Threshold is " << m_threshold << std::endl;
34 }
35 
36 std::vector<short> RunningSumTPFinderPass2::downSample(const std::vector<short>& orig) {
37 
38  //---------------------------------------------
39  // Do the downsampling
40  //---------------------------------------------
41  if (m_downsampleFactor==1) {
42  return orig;
43  }
44  else {
45  std::vector<short> waveform;
46  for(size_t i=0; i<orig.size(); i+=m_downsampleFactor) {
47  waveform.push_back(orig[i]);
48  }
49  return waveform;
50  }
51 }
52 
53 std::vector<short> RunningSumTPFinderPass2::findPedestal(const std::vector<short>& waveform)
54 {
55  //---------------------------------------------
56  // Pedestal subtraction
57  //---------------------------------------------
58  const std::vector<short>& pedestal=m_useSignalKill ?
59  frugal_pedestal_sigkill(waveform,
64  return pedestal;
65 }
66 
67 std::vector<short> RunningSumTPFinderPass2::filter(const std::vector<short>& pedsub) {
68 
69  //---------------------------------------------
70  // Filtering
71  //---------------------------------------------
72  const size_t ntaps = m_filterTaps.size();
73  const short* taps = m_filterTaps.data();
74 
75  std::vector<short> filtered(m_doFiltering ?
76  apply_fir_filter(pedsub, ntaps, taps) :
77  pedsub);
78  if (!m_doFiltering) {
79  std::transform(filtered.begin(), filtered.end(),
80  filtered.begin(),
81  [=](short a) { return a*m_multiplier; });
82  }
83  return filtered;
84 }
85 
86 void
87 RunningSumTPFinderPass2::hitFinding(const std::vector<short>& waveform,
88  std::vector<RunningSumTPFinderTool::Hit>& hits,
89  int channel) {
90 
91  //---------------------------------------------
92  // Hit finding
93  //---------------------------------------------
94  bool is_hit = false;
95  bool was_hit = false;
96  RunningSumTPFinderTool::Hit hit(channel, 0, 0, 0);
97  for(size_t isample=0; isample<waveform.size()-1; ++isample){
98  int sample_time = isample * m_downsampleFactor;
99  short adc = waveform[isample];
100  is_hit = adc > (short)m_threshold*10;
101  if(is_hit && !was_hit) {
102  hit.startTime = sample_time;
103  hit.charge = adc;
105  }
106  if(is_hit && was_hit) {
107  hit.charge += adc*m_downsampleFactor;
109  }
110  if(!is_hit && was_hit) {
111  hit.charge /= m_multiplier;
112  hits.push_back(hit);
113  }
114  was_hit = is_hit;
115  }
116 }
117 
118 std::vector<RunningSumTPFinderTool::Hit>
119 RunningSumTPFinderPass2::findHits(const std::vector<unsigned int>& channel_numbers,
120  const std::vector<std::vector<short>>& collection_samples) {
121 
122  auto hits = std::vector<RunningSumTPFinderTool::Hit>();
123  std::cout << "findHits called with " << collection_samples.size()
124  << " channels. First chan has " << collection_samples[0].size() << " samples" << std::endl;
125 
126  for(size_t ich=0; ich<collection_samples.size(); ++ich){
127  const std::vector<short>& waveformOrig = collection_samples[ich];
128 
129  std::vector<short> waveform = downSample (waveformOrig);
130  std::vector<short> pedestal = findPedestal(waveform );
131  std::vector<short> pedsub(waveform.size(), 0);
132  for(size_t i=0; i<pedsub.size(); ++i)
133  pedsub[i]=waveform[i]-pedestal[i];
134  std::vector<short> filtered = filter(pedsub);
135  hitFinding(filtered, hits, channel_numbers[ich]);
136  }
137  std::cout << "Returning " << hits.size() << " hits" << std::endl;
138  std::cout << "hits/channel=" << float(hits.size())/collection_samples .size() << std::endl;
139  std::cout << "hits/tick=" << float(hits.size())/collection_samples[0].size() << std::endl;
140  return hits;
141 }
142 
#define DEFINE_ART_CLASS_TOOL(tool)
Definition: ToolMacros.h:42
std::vector< short > filter(const std::vector< short > &orig)
struct vector vector
RunningSumTPFinderPass2(fhicl::ParameterSet const &p)
int16_t adc
Definition: CRTFragment.hh:202
STL namespace.
void hitFinding(const std::vector< short > &waveform, std::vector< RunningSumTPFinderTool::Hit > &hits, int channel)
uint8_t channel
Definition: CRTFragment.hh:201
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:92
std::vector< short > downSample(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 > findPedestal(const std::vector< short > &orig)
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
virtual std::vector< RunningSumTPFinderTool::Hit > findHits(const std::vector< unsigned int > &channel_numbers, const std::vector< std::vector< short >> &collection_samples)
std::vector< short > m_filterTaps
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
QTextStream & endl(QTextStream &s)