Dune35tZeroSuppressService_service.cc
Go to the documentation of this file.
1 // Dune35tZeroSuppressService.cxx
2 
5 #include <iomanip>
8 
9 using std::string;
10 using std::ostream;
11 using std::cout;
12 using std::endl;
13 using std::setw;
14 
16 
17 //**********************************************************************
18 
19 // Signal state.
20 // OUT - outside the signal (may be in the tail of an earlier signal)
21 // LIVE - Live region of the signal (above TL or after)
22 // DEAD - Dead region of the signal (below TD)
23 // END - End of dead region (followed by tail)
24 enum SigState { OUT, LIVE, DEAD, END };
25 
26 //**********************************************************************
27 
28 namespace {
29 
30 // Calculate sum for nsig bins before isig.
31 // If skipStuck, the stuck bins do not contribute to sum or count.
32 // Bins with sig <= thresh, do not contribute to sum.
33 struct RunningSum {
34  RunningSum(const AdcCountVector& sigs, AdcPedestal ped, Index isig, Index ns, AdcCount thresh, bool skipStuck);
35  AdcCount sigsum;
36  Index count;
37  SignalTypeConverter sigcon;
38 };
39 
40 RunningSum::
41 RunningSum(const AdcCountVector& sigs, AdcPedestal ped, Index isig, Index nsig, AdcCount thresh, bool skipStuck) {
42  sigsum = 0;
43  count = 0;
44  Index jsig1 = 0;
45  if ( isig > nsig ) jsig1 = isig - nsig;
46  Index jsig2 = isig;
47  if ( jsig2 > sigs.size() ) jsig2 = sigs.size();
48  for ( Index jsig=jsig1; jsig<jsig2; ++jsig ) {
49  AdcCount rawsig = sigs[jsig];
50  AdcCount sig = sigcon.convert<AdcCount>(rawsig - ped);
51  if ( skipStuck ) {
52  Index lsb = rawsig & 0x3f;
53  if ( lsb == 0 ) continue;
54  if ( lsb == 0x3f ) continue;
55  }
56  ++count;
57  if ( abs(sig) > thresh ) {
58  sigsum += sig;
59  }
60  }
61 }
62 
63 // Convert state to string.
64 string sstate(SigState state) {
65  if ( state == OUT ) return "OUT";
66  if ( state == LIVE ) return "LIVE";
67  if ( state == DEAD ) return "DEAD";
68  if ( state == END ) return "END";
69  return "NONE";
70 }
71 
72 } // End unnamed namespace.
73 
74 //**********************************************************************
75 
78  Index ns, Index nl, Index nd, Index nt)
79 : m_ts(ts), m_tl(tl), m_td(td), m_ns(ns), m_nl(nl), m_nd(nd), m_nt(nt),
80  m_LogLevel(1) { }
81 
82 //**********************************************************************
83 
86 : m_LogLevel(1) {
87  const string myname = "Dune35tZeroSuppressService::ctor: ";
88  m_ts = pset.get<AdcCount>("TS");
89  m_tl = pset.get<AdcCount>("TL");
90  m_td = pset.get<AdcCount>("TD");
91  m_ns = pset.get<Index>("NS");
92  m_nl = pset.get<Index>("NL");
93  m_nd = pset.get<Index>("ND");
94  m_nt = pset.get<Index>("NT");
95  pset.get_if_present<int>("LogLevel", m_LogLevel);
96  if ( m_LogLevel >= 1 ) print(cout, myname);
97 }
98 
99 //**********************************************************************
100 
102 filter(const AdcCountVector& sigs, Channel chan, AdcPedestal ped, AdcFilterVector& keep) const {
103  const string myname = "ZeroSuppress35t::filter: ";
104  if ( m_LogLevel >= 2 ) cout << myname << "Filtering channel " << chan << " with tick count " << sigs.size() << endl;
105  bool m_skipStuck = false;
106  AdcCount m_ts = 0;
107  unsigned int nsig = sigs.size();
108  keep.clear();
109  keep.resize(nsig, false);
110  // Loop over signals.
111  SigState state = OUT;
112  unsigned int nlow = 0;
113  for ( unsigned int isig=0; isig<nsig; ++isig ) {
114  AdcCount sig = sigs[isig];
115  // Evaluate a running signal sum of the preceding m_nl signals.
116  RunningSum rs(sigs, ped, isig, m_ns, m_ts, m_skipStuck);
117  AdcCount asigsum = std::abs(rs.sigsum);
118  if ( m_LogLevel >= 3 ) cout << myname << setw(6) << isig << setw(6) << sig << setw(5) << sstate(state) << endl;
119  // Last tick is outside a signal.
120  if ( state == OUT || state == END ) {
121  // If this tick is above TL, we are in the live region of a signal.
122  // Keep the NL preceding signals.
123  AdcCount sumthresh = m_tl*rs.count;
124  bool keepit = asigsum > sumthresh;
125  if ( m_LogLevel >= 3 ) cout << myname << " RS sum/thresh=" << setw(3) << rs.sigsum << "/" << setw(3) << sumthresh << endl;
126  if ( keepit ) {
127  if ( m_LogLevel == 2 ) cout << myname << setw(6) << isig << ": RS sum/thresh=" << setw(3) << rs.sigsum
128  << "/" << setw(3) << sumthresh << endl;
129  state = LIVE;
130  unsigned int jsig1 = 0;
131  if ( isig > m_nl ) jsig1 = isig - m_nl;
132  unsigned int jsig2 = isig;
133  for ( unsigned int jsig=jsig1; jsig<jsig2; ++jsig) {
134  keep[jsig] = true;
135  }
136  } else {
137  state = OUT;
138  }
139  } else {
140  // Last tick is is in the live region of a signal.
141  AdcCount sumthresh = m_td*rs.count;
142  if ( m_LogLevel >= 3 ) cout << myname << " RS sum/thresh=" << setw(3) << rs.sigsum << "/" << setw(3) << sumthresh << endl;
143  if ( state == LIVE ) {
144  // If this tick is below TD, we are in the dead region of a signal.
145  if ( asigsum <= sumthresh ) {
146  state = DEAD;
147  nlow = 1;
148  }
149  // Last tick is is in the dead region of a signal.
150  } else if ( state == DEAD ) {
151  // If signal is above TD, we are back in the live region.
152  if ( asigsum > sumthresh ) {
153  state = LIVE;
154  nlow = 0;
155  // If this is the ND'th consecutive signal in the dead region, we
156  // have reached the end of the signal.
157  // Keep this signal and a tail.
158  } else if ( ++nlow >= m_nd ) {
159  state = END;
160  nlow = 0;
161  // Protect the tail.
162  unsigned int jsig1 = isig + 1;
163  unsigned int jsig2 = jsig1 + m_nt;
164  if ( jsig2 > nsig ) jsig2 = nsig;
165  for ( unsigned int jsig=jsig1; jsig<jsig2; ++jsig) {
166  keep[jsig] = true;
167  }
168  }
169  } else {
170  assert(false);
171  }
172  }
173  if ( state != OUT ) keep[isig] = true;
174  }
175  return 0;
176 }
177 
178 //**********************************************************************
179 
180 ostream& Dune35tZeroSuppressService::print(ostream& out, string prefix) const {
181  out << prefix << " TS = " << m_ts << endl;
182  out << prefix << " TL = " << m_tl << endl;
183  out << prefix << " TD = " << m_td << endl;
184  out << prefix << " NS = " << m_ns << endl;
185  out << prefix << " NL = " << m_nl << endl;
186  out << prefix << " ND = " << m_nd << endl;
187  out << prefix << " NT = " << m_nt << endl;
188  out << prefix << "LogLevel: " << m_LogLevel << endl;
189  return out;
190 }
191 
192 //**********************************************************************
193 
195  m_LogLevel = dbg;
196 }
197 
198 //**********************************************************************
199 
201 
202 //**********************************************************************
std::vector< AdcCount > AdcCountVector
Definition: AdcTypes.h:19
bool dbg
std::string string
Definition: nybbler.cc:12
Dune35tZeroSuppressService(AdcCount ts, AdcCount tl, AdcCount td, Index ns, Index nl, Index nd, Index nt)
int filter(const AdcCountVector &sigs, Channel chan, AdcPedestal ped, AdcFilterVector &keep) const
std::ostream & print(std::ostream &out=std::cout, std::string prefix="") const
unsigned int Index
Dune35tZeroSuppressService::Index Index
T abs(T value)
T get(std::string const &key) const
Definition: ParameterSet.h:271
Q_EXPORT QTSManip setw(int w)
Definition: qtextstream.h:331
std::vector< bool > AdcFilterVector
Definition: AdcTypes.h:27
float AdcPedestal
Definition: AdcTypes.h:25
std::optional< T > get_if_present(std::string const &key) const
Definition: ParameterSet.h:224
short AdcCount
Definition: AdcTypes.h:18
QAsciiDict< Entry > ns
std::string nl(std::size_t i=1)
QTextStream & endl(QTextStream &s)
#define DEFINE_ART_SERVICE_INTERFACE_IMPL(svc, iface)