RMSHitFinderAlg.cxx
Go to the documentation of this file.
1 /****************************************
2 
3 Searches waveforms for hits where the threshold
4 is determined on-the-fly from the actual channel noise,
5 rather than a set value for all channels.
6 
7 October 2016
8 m.thiesse@sheffield.ac.uk
9 
10 ****************************************/
11 
12 #include "RMSHitFinderAlg.h"
13 
15 {
16  this->reconfigure(p);
17  SetSearchTicks(-1,-1);
18 }
19 
21 {
22  fWindowWidth = p.get<int>("WindowWidth");
23  fFilterWidth = p.get<float>("FilterWidth");
24  fSigmaRiseThreshold = p.get<float>("SigmaRiseThreshold");
25  fSigmaFallThreshold = p.get<float>("SigmaFallThreshold");
26 }
27 
29 {
30  if (fSearchTickStart < 0 || fSearchTickEnd < 0)
31  {
33  }
34  std::vector<float> signalFilter(chan.signalFilterVec.begin()+fSearchTickStart,chan.signalFilterVec.begin()+fSearchTickEnd);
35  //FilterWaveform(chan.signalVec,chan.signalFilterVec);
36  //FilterWaveform(signal,signalFilter);
37  //RobustRMSBase(chan.signalVec,chan.baseline,chan.rms);
38  //RobustRMSBase(signalFilter,chan.baselineFilter,chan.rmsFilter);
39  FindPulses(signalFilter,chan.baselineFilter,chan.rmsFilter,chan.pulse_ends);
40  MergeHits(chan.pulse_ends);
41 }
42 
43 void dune::RMSHitFinderAlg::FilterWaveform(std::vector<float> wf, std::vector<float> & fwf)
44 {
45  fwf.clear();
46  unsigned int wfs = wf.size();
47  fwf.resize(wfs);
48  std::vector<float> intermediate_wf(wfs);
49  float filter_coef = static_cast<float>(TMath::Exp(static_cast<double>(-1.0/fFilterWidth)));
50  float a0 = 1.0-filter_coef;
51  float b1 = filter_coef;
52  unsigned int order = 1;
53  for (size_t i = 0; i < wfs; ++i)
54  {
55  if (i<order)
56  {
57  intermediate_wf[i] = wf[i];
58  }
59  else
60  {
61  intermediate_wf[i] = a0*wf[i]+b1*intermediate_wf[i-1];
62  }
63  }
64  for (size_t i = 0; i < wfs; ++i)
65  {
66  if (i<order)
67  {
68  fwf[wfs-1-i] = intermediate_wf[wfs-1-i];
69  }
70  else
71  {
72  fwf[wfs-1-i] = a0*intermediate_wf[wfs-1-i]+b1*fwf[wfs-i];
73  }
74  }
75 }
76 
77 void dune::RMSHitFinderAlg::RobustRMSBase(std::vector<float> wf, float & bl, float & r)
78 {
79  unsigned int window_size = (unsigned int)(0.10*wf.size());
80  std::vector<float> bl_collection;
81  for (size_t i_wf = 0; i_wf < wf.size()-window_size; ++i_wf)
82  {
83  std::vector<float> partial_window(wf.begin()+i_wf,wf.begin()+i_wf+window_size);
84  bl_collection.push_back(static_cast<float>(TMath::Mean(partial_window.size(),partial_window.data())));
85  }
86  bl = static_cast<float>(TMath::Median(bl_collection.size(),bl_collection.data()));
87 
88  std::vector<float> bl_sub_wf;
89  for (size_t i_wf = 0; i_wf < wf.size(); ++i_wf) bl_sub_wf.push_back(wf[i_wf]-bl);
90 
91  std::vector<float> rms_collection;
92  for (size_t i_wf = 0; i_wf < bl_sub_wf.size()-window_size; ++i_wf)
93  {
94  std::vector<float> partial_window(bl_sub_wf.begin()+i_wf,bl_sub_wf.begin()+i_wf+window_size);
95  rms_collection.push_back(static_cast<float>(TMath::RMS(partial_window.size(),partial_window.data())));
96 
97  }
98  r = static_cast<float>(TMath::Median(rms_collection.size(),rms_collection.data()));
99 }
100 
101 
102 void dune::RMSHitFinderAlg::FindPulses(std::vector<float> wf, float bl, float r, std::vector<std::pair<int,int> > & pulse_ends)
103 {
104  pulse_ends.clear();
105  int start = 0, end = 0;
106  bool started = false;
107  for (int i_wf = 0; i_wf < static_cast<int>(wf.size())-fWindowWidth; ++i_wf)
108  {
109  std::vector<float> window(wf.begin()+i_wf,wf.begin()+i_wf+fWindowWidth);
110  float window_mean = static_cast<float>(TMath::Mean(window.size(),window.data()));
111  if ((window_mean > bl+fSigmaRiseThreshold*r) && !started)
112  {
113  started = true;
114  for (int i_wf_back = i_wf-1; i_wf_back >= 0; --i_wf_back)
115  {
116  std::vector<float> window_back(wf.begin()+i_wf_back,wf.begin()+i_wf_back+fWindowWidth);
117  float window_mean_back = static_cast<float>(TMath::Mean(window_back.size(),window_back.data()));
118  if (window_mean_back < bl+fSigmaFallThreshold*r)
119  {
120  start = i_wf_back;
121  break;
122  }
123  }
124  continue;
125  }
126  if ((window_mean < bl+fSigmaFallThreshold*r && window_mean > bl-fSigmaFallThreshold*r) && started)
127  {
128  started = false;
129  end = i_wf+fWindowWidth;
130  pulse_ends.push_back(std::make_pair(start+fSearchTickStart,end+fSearchTickStart));
131  continue;
132  }
133  }
134  if (started)
135  {
136  pulse_ends.push_back(std::make_pair(start+fSearchTickStart,wf.size()-1+fSearchTickStart));
137  }
138 }
139 
140 void dune::RMSHitFinderAlg::MergeHits(std::vector<std::pair<int,int> > & pulse_ends)
141 {
142  std::vector<std::pair<int,int> > oldpulse_ends = std::move(pulse_ends);
143  pulse_ends.clear();
144  std::sort(oldpulse_ends.begin(),oldpulse_ends.end());
145  int start = 0, end = 0;
146  bool started = false;
147  for (size_t i_p = 0; i_p < oldpulse_ends.size(); ++i_p)
148  {
149  if (!started) start = oldpulse_ends[i_p].first;
150  end = oldpulse_ends[i_p].second;
151  if (i_p < oldpulse_ends.size()-1 && oldpulse_ends[i_p+1].first < oldpulse_ends[i_p].second)
152  {
153  end = oldpulse_ends[i_p+1].second;
154  started = true;
155  }
156  else
157  {
158  started = false;
159  pulse_ends.push_back(std::make_pair(start,end));
160  }
161  }
162 }
163 
end
while True: pbar.update(maxval-len(onlies[E][S])) #print iS, "/", len(onlies[E][S]) found = False for...
void reconfigure(fhicl::ParameterSet const &p)
void FilterWaveform(std::vector< float > wf, std::vector< float > &fwf)
RMSHitFinderAlg(fhicl::ParameterSet const &p)
#define a0
void RobustRMSBase(std::vector< float > wf, float &bl, float &r)
std::vector< float > signalFilterVec
struct vector vector
std::vector< std::pair< int, int > > pulse_ends
void FindPulses(std::vector< float > wf, float bl, float r, std::vector< std::pair< int, int > > &pulse_ends)
void FindHits(dune::ChannelInformation &chan)
def move(depos, offset)
Definition: depos.py:107
T get(std::string const &key) const
Definition: ParameterSet.h:271
p
Definition: test.py:223
void MergeHits(std::vector< std::pair< int, int > > &pulse_ends)
void SetSearchTicks(int s, int e)