test_InterpolatingAdcMitigationService.cxx
Go to the documentation of this file.
1 // test_InterpolatingAdcMitigationService.cxx
2 //
3 // David Adams
4 // May 2016
5 //
6 // Test InterpolatingAdcMitigationService.
7 
8 #include <string>
9 #include <iostream>
10 #include <sstream>
11 #include <fstream>
12 #include <iomanip>
16 
17 #undef NDEBUG
18 #include <cassert>
19 
20 using std::string;
21 using std::vector;
22 using std::cout;
23 using std::endl;
24 using std::istringstream;
25 using std::ofstream;
26 using std::setw;
27 using std::setprecision;
28 using std::fixed;
29 using art::ServiceHandle;
30 
31 typedef vector<unsigned int> IndexVector;
32 
33 bool sigequal(AdcSignal sig1, AdcSignal sig2) {
34  AdcSignal sigdiff = sig2 - sig1;
35  if ( sigdiff < -0.5 ) return false;
36  if ( sigdiff > 0.5 ) return false;
37  return true;
38 }
39 
40 //**********************************************************************
41 
42 int test_InterpolatingAdcMitigationService(int a_LogLevel =1, int a_MaxConsecutiveFlag =0) {
43  const string myname = "test_InterpolatingAdcMitigationService: ";
44 #ifdef NDEBUG
45  cout << myname << "NDEBUG must be off." << endl;
46  abort();
47 #endif
48  string line = "-----------------------------";
49 
50  cout << myname << line << endl;
51  cout << myname << "Create top-level FCL." << endl;
52  std::ostringstream oss;
53  oss << "services.AdcMitigationService: {" << endl;
54  oss << " service_provider: InterpolatingAdcMitigationService" << endl;
55  oss << " LogLevel: " << a_LogLevel << endl;
56  oss << " SkipUnderflows: true" << endl;
57  oss << " SkipOverflows: false" << endl;
58  oss << " MaxConsecutiveSamples: 3 " << endl;
59  oss << " MaxConsecutiveFlag: " << a_MaxConsecutiveFlag << endl;
60  oss << "}" << endl;
62 
63  const unsigned int nsig = 50;
64  AdcChannelData acd;
65  for ( unsigned int isig=0; isig<50; ++isig ) {
66  acd.samples.push_back(10+isig);
67  acd.flags.push_back(AdcGood);
68  }
69  AdcSignalVector& sigs = acd.samples;
70  AdcFlagVector& flags = acd.flags;
71 
72  // Expected results.
73  AdcSignalVector sigsexp = acd.samples;
74  AdcFlagVector flagsexp = acd.flags;
75 
76  // Add overflows. These are mitigated.
77  IndexVector isigOvers = {5, 6, 9};
78  for ( unsigned int isig : isigOvers ) {
79  sigs[isig] = 9999;
80  flags[isig] = AdcOverflow;
81  flagsexp[isig] = AdcInterpolated;
82  }
83 
84  // Add underflows. These are not mitigated.
85  IndexVector isigUnders = {15, 16, 19};
86  for ( unsigned int isig : isigUnders ) {
87  sigs[isig] = -9999;
88  flags[isig] = AdcUnderflow;
89  sigsexp[isig] = sigs[isig];
90  flagsexp[isig] = flags[isig];
91  }
92 
93  // Add samples with stuck bits on.
94  // These are over short ranges so they are mitigated.
95  IndexVector isigOns = {21, 23, 24, 28, 29, 30};
96  for ( unsigned int isig : isigOns ) {
97  sigs[isig] = 1111;
98  flags[isig] = AdcStuckOn;
99  flagsexp[isig] = AdcInterpolated;
100  }
101 
102  // Add samples with struck bits off.
103  // These are at edges or over long ranges so they are zeroed.
104  IndexVector isigOffs = {0, 1, 33, 34, 35, 36, 41, 42, 43, 44, 45, 46, nsig-1};
105  for ( unsigned int isig : isigOffs ) {
106  sigs[isig] = 1100;
107  flags[isig] = AdcStuckOff;
108  if ( a_MaxConsecutiveFlag == 1 ) {
109  sigsexp[isig] = 0.0;
110  flagsexp[isig] = AdcSetFixed;
111  } else {
112  assert( a_MaxConsecutiveFlag == 0 );
113  sigsexp[isig] = sigs[isig];
114  flagsexp[isig] = flags[isig];
115  }
116  }
117 
118  // Data before mitigation.
119  AdcSignalVector sigsin = acd.samples;
120  AdcFlagVector flagsin = acd.flags;
121 
122  cout << myname << "Fetch ADC mitigation service." << endl;
124  hams->print();
125 
126  cout << myname << line << endl;
127  cout << myname << "Mitigate." << endl;
128  assert( hams->update(acd) == 0 );
129  cout << myname << "Output vector size: " << sigs.size() << endl;
130  cout << myname << " Output flags size: " << flags.size() << endl;
131  assert( sigs.size() == nsig );
132  assert( flags.size() == nsig );
133  for ( unsigned int isig=0; isig<nsig; ++isig ) {
134  cout << setw(4) << isig << ": "
135  << fixed << setprecision(1) << setw(8) << sigsin[isig]
136  << " [" << flagsin[isig] << "]"
137  << fixed << setprecision(1) << setw(10) << sigs[isig]
138  << " [" << flags[isig] << "]"
139  << " [" << flagsexp[isig] << "]"
140  << endl;
141  assert( flags[isig] == flagsexp[isig] );
142  assert( sigs[isig] == sigsexp[isig] );
143  }
144 
145  return 0;
146 }
147 
148 //**********************************************************************
149 
150 int main(int argc, char* argv[]) {
151  int a_LogLevel = 1;
152  int a_MaxConsecutiveFlag = 0;
153  if ( argc > 1 ) {
154  istringstream ssarg(argv[1]);
155  ssarg >> a_LogLevel;
156  }
157  if ( argc > 2 ) {
158  istringstream ssarg(argv[1]);
159  ssarg >> a_MaxConsecutiveFlag;
160  }
161  return test_InterpolatingAdcMitigationService(a_LogLevel, a_MaxConsecutiveFlag);
162 }
163 
164 //**********************************************************************
int test_InterpolatingAdcMitigationService(int a_LogLevel=1, int a_MaxConsecutiveFlag=0)
bool sigequal(AdcSignal sig1, AdcSignal sig2)
std::vector< Index > IndexVector
virtual int update(AdcChannelData &data) const =0
std::string string
Definition: nybbler.cc:12
float AdcSignal
Definition: AdcTypes.h:21
int main(int argc, char *argv[])
std::vector< AdcFlag > AdcFlagVector
Definition: AdcTypes.h:30
struct vector vector
const AdcFlag AdcUnderflow
Definition: AdcTypes.h:33
static void load_services(std::string const &config)
Q_EXPORT QTSManip setprecision(int p)
Definition: qtextstream.h:343
const AdcFlag AdcSetFixed
Definition: AdcTypes.h:41
const AdcFlag AdcGood
Definition: AdcTypes.h:32
const AdcFlag AdcOverflow
Definition: AdcTypes.h:34
const AdcFlag AdcStuckOn
Definition: AdcTypes.h:37
Q_EXPORT QTSManip setw(int w)
Definition: qtextstream.h:331
const AdcFlag AdcStuckOff
Definition: AdcTypes.h:36
const AdcFlag AdcInterpolated
Definition: AdcTypes.h:42
void line(double t, double *p, double &x, double &y, double &z)
std::vector< AdcSignal > AdcSignalVector
Definition: AdcTypes.h:22
vector< unsigned int > IndexVector
virtual std::ostream & print(std::ostream &out=std::cout, std::string prefix="") const =0
AdcSignalVector samples
QTextStream & endl(QTextStream &s)
AdcFlagVector flags