InterpolatingAdcMitigationService_service.cc
Go to the documentation of this file.
1 // InterpolatingAdcMitigationService_service.cc
2 
4 #include <iostream>
7 
8 using std::string;
9 using std::cout;
10 using std::endl;
11 
12 //**********************************************************************
13 
16 : m_LogLevel(1) {
17  const string myname = "InterpolatingAdcMitigationService::ctor: ";
18  pset.get_if_present<int>("LogLevel", m_LogLevel);
19  m_SkipUnderflows = pset.get<bool>("SkipUnderflows");
20  m_SkipOverflows = pset.get<bool>("SkipOverflows");
21  m_MaxConsecutiveSamples = pset.get<int>("MaxConsecutiveSamples");
22  m_MaxConsecutiveFlag = pset.get<int>("MaxConsecutiveFlag");
23  print(cout, myname);
24 }
25 
26 //**********************************************************************
27 
30  const string myname = "InterpolatingAdcMitigationService:update: ";
31  if ( m_LogLevel >= 3 ) {
32  cout << myname << "Entering..." << endl;
33  cout << myname << " Channel: " << data.channel() << endl;
34  cout << myname << "Input vector size: " << data.samples.size() << endl;
35  }
36  AdcSignalVector& sigs = data.samples;
37  AdcFlagVector& flags = data.flags;
38  unsigned int isigFirst = sigs.size(); // First sample in the bad sample sequence.
39  bool useMax = m_MaxConsecutiveSamples >= 0;
40  unsigned int maxsig = m_MaxConsecutiveSamples;
41  unsigned int nupdated = 0;
42  // Loop over samples.
43  for ( unsigned int isig=0; isig<sigs.size(); ++isig ) {
44  AdcFlag flag = flags[isig];
45  unsigned int isLast = isig == sigs.size()-1;
46  // Set isBad which indicates if this is value that is flagged to be updated.
47  bool isBad = false;
48  if ( flag != AdcGood ) {
49  isBad = true;
50  if ( m_SkipUnderflows && flag == AdcUnderflow ) isBad = false;
51  else if ( m_SkipOverflows && flag == AdcOverflow ) isBad = false;
52  }
53  // Record if this is the first sample in a new bad sequence.
54  if ( isBad && isigFirst > isig ) isigFirst = isig;
55  // Check if we have found the end of a sequence of bad samples.
56  // This is indicated with a nonzero value for isigLast.
57  // We will update samples in the range [isigFirst, isigLast].
58  bool endBadSequence = false;
59  unsigned int isigLast = 0;
60  if ( !isBad && isigFirst < isig ) {
61  endBadSequence = true;
62  isigLast = isig - 1;
63  }
64  if ( isLast && isBad ) {
65  endBadSequence = true;
66  isigLast = isig;
67  }
68  // Update the current sequence of bad samples.
69  if ( endBadSequence ) {
70  unsigned int nsig = isigLast - isigFirst + 1;
71  bool tooMany = useMax && nsig > maxsig;
72  if ( m_LogLevel > 2 ) {
73  cout << myname << " Updating at sample " << isig << ":"
74  << " range=[" << isigFirst << "," << isigLast << "],"
75  << " tooMany=" << tooMany << ", isLast=" << isLast << endl;
76  }
77  // For too many samples or beginning or end of data, use MaxConsecutiveFlag.
78  if ( tooMany || isigFirst == 0 || isLast ) {
79  if ( m_MaxConsecutiveFlag == 1 ) {
80  for ( unsigned isig=isigFirst; isig<=isigLast; ++isig ) {
81  sigs[isig] = 0.0;
82  flags[isig] = AdcSetFixed;
83  ++nupdated;
84  }
85  }
86  // Otherwise, interpolate: sig_i = a*i + b
87  } else {
88  unsigned int isig1 = isigFirst - 1;
89  unsigned int isig2 = isigLast + 1;
90  double sig1 = sigs[isig1];
91  double sig2 = sigs[isigLast+1];
92  double fac = 1.0/(isig2 - isig1);
93  double a = fac*(sig2 - sig1);
94  double b = fac*(isig2*sig1 - isig1*sig2);
95  for ( unsigned isig=isigFirst; isig<=isigLast; ++isig ) {
96  sigs[isig] = a*isig + b;
97  flags[isig] = AdcInterpolated;
98  ++nupdated;
99  }
100  }
101  isigFirst = sigs.size();
102  }
103  }
104  if ( m_LogLevel >= 2 ) cout << myname << "Channel " << data.channel()
105  << ": # updated/total = " << nupdated << "/" << sigs.size() << endl;
106  return 0;
107 }
108 
109 //**********************************************************************
110 
112 print(std::ostream& out, std::string prefix) const {
113  out << prefix << "InterpolatingAdcMitigationService:" << endl;
114  out << prefix << " LogLevel: " << m_LogLevel << endl;
115  out << prefix << " SkipUnderflows: " << m_SkipUnderflows << endl;
116  out << prefix << " SkipOverflows: " << m_SkipOverflows << endl;
117  out << prefix << " MaxConsecutiveSamples: " << m_MaxConsecutiveSamples << endl;
118  out << prefix << " MaxConsecutiveFlag: " << m_MaxConsecutiveFlag << endl;
119  return out;
120 }
121 
122 //**********************************************************************
123 
125 
126 //**********************************************************************
short AdcFlag
Definition: AdcTypes.h:29
std::string string
Definition: nybbler.cc:12
std::vector< AdcFlag > AdcFlagVector
Definition: AdcTypes.h:30
const AdcFlag AdcUnderflow
Definition: AdcTypes.h:33
const AdcFlag AdcSetFixed
Definition: AdcTypes.h:41
const AdcFlag AdcGood
Definition: AdcTypes.h:32
const AdcFlag AdcOverflow
Definition: AdcTypes.h:34
const double a
T get(std::string const &key) const
Definition: ParameterSet.h:271
Channel channel() const
std::ostream & print(std::ostream &out=std::cout, std::string prefix="") const
const AdcFlag AdcInterpolated
Definition: AdcTypes.h:42
std::optional< T > get_if_present(std::string const &key) const
Definition: ParameterSet.h:224
static bool * b
Definition: config.cpp:1043
std::vector< AdcSignal > AdcSignalVector
Definition: AdcTypes.h:22
AdcSignalVector samples
InterpolatingAdcMitigationService(fhicl::ParameterSet const &pset, art::ActivityRegistry &)
QTextStream & endl(QTextStream &s)
AdcFlagVector flags
#define DEFINE_ART_SERVICE_INTERFACE_IMPL(svc, iface)