test_MultiChannelNoiseRemovalService.cxx
Go to the documentation of this file.
1 // test_MultiChannelNoiseRemovalService.cxx
2 //
3 // David Adams
4 // May 2016
5 //
6 // Test MultiChannelNoiseRemovalService.
7 //
8 
9 #include <string>
10 #include <iostream>
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 //**********************************************************************
32 
33 int test_MultiChannelNoiseRemovalService(int a_LogLevel =1) {
34  const string myname = "test_MultiChannelNoiseRemovalService: ";
35 #ifdef NDEBUG
36  cout << myname << "NDEBUG must be off." << endl;
37  abort();
38 #endif
39  string line = "-----------------------------";
40 
41  cout << myname << line << endl;
42  cout << myname << "Create top-level FCL." << endl;
43  AdcSignal threshold = 4.0;
44  std::ostringstream oss;
45  oss << "services.AdcChannelNoiseRemovalService: {" << endl;
46  oss << " service_provider: ThresholdNoiseRemovalService" << endl;
47  oss << " Threshold: " << threshold << endl;
48  oss << " LogLevel: " << a_LogLevel << endl;
49  oss << "}" << endl;
50  oss << "services.AdcNoiseRemovalService: {" << endl;
51  oss << " service_provider: MultiChannelNoiseRemovalService" << endl;
52  oss << " LogLevel: " << a_LogLevel << endl;
53  oss << "}" << endl;
55 
56  cout << myname << line << endl;
57  cout << myname << "Create samples." << endl;
58  const unsigned int nchan = 5;
59  double f[nchan] = {0.11, 0.22, 0.33, 0.44, 0.55};
60  const unsigned int nsig = 100;
61  AdcChannelDataMap acdmap;
62  vector<AdcSignalVector> sigsin(nchan); // Record the stae before noise removal.
63  vector<AdcSignalVector> sigsexp(nchan); // Record the expected state.
64  for ( unsigned int icha=0; icha<nchan; ++icha ) {
65  AdcChannel chan = 100 + icha;
66  acdmap[chan] = AdcChannelData();
67  AdcChannelData& acd = acdmap[chan];
68  AdcSignalVector& exp = sigsexp[icha];
69  AdcSignalVector& in = sigsin[icha];
70  acd.setChannelInfo(chan);
71  for ( unsigned int isig=0; isig<nsig; ++isig ) {
72  acd.samples.push_back(20*sin(f[icha]*isig));
73  acd.flags.push_back(AdcGood);
74  }
75  AdcSignalVector& sigs = acd.samples;
76  AdcSignalVector sigsin = sigs;
77  in = sigs;
78  exp = sigs;
79  for ( AdcSignal& sig : exp ) if ( fabs(sig) < threshold ) sig = 0.0;
80  }
81 
82  cout << myname << line << endl;
83  cout << myname << "Fetch the noise removal." << endl;
85  hanr->print(cout, myname);
86 
87  cout << myname << line << endl;
88  cout << myname << "Remove noise." << endl;
89  assert( hanr->update(acdmap) == 0 );
90  for ( unsigned int icha=0; icha<nchan; ++icha ) {
91  AdcChannel chan = 100 + icha;
92  AdcChannelData& acd = acdmap[chan];
93  AdcSignalVector& sigs = acd.samples;
94  cout << myname << "-------" << endl;
95  cout << myname << "Channel " << chan << endl;
96  for ( unsigned int isig=0; isig<nsig; ++isig ) {
97  cout << myname << setw(4) << isig << ": "
98  << fixed << setprecision(1) << setw(10) << sigsin[icha][isig]
99  << fixed << setprecision(1) << setw(10) << sigs[isig]
100  << endl;
101  assert( sigs[isig] == sigsexp[icha][isig] );
102  }
103  }
104 
105  cout << myname << line << endl;
106  cout << myname << "Done." << endl;
107  return 0;
108 }
109 
110 //**********************************************************************
111 
112 int main(int argc, char* argv[]) {
113  int a_LogLevel = -1;
114  if ( argc > 1 ) {
115  istringstream ssarg(argv[1]);
116  ssarg >> a_LogLevel;
117  }
118  return test_MultiChannelNoiseRemovalService(a_LogLevel);
119 }
120 
121 //**********************************************************************
std::string string
Definition: nybbler.cc:12
float AdcSignal
Definition: AdcTypes.h:21
struct vector vector
static void load_services(std::string const &config)
Q_EXPORT QTSManip setprecision(int p)
Definition: qtextstream.h:343
const AdcFlag AdcGood
Definition: AdcTypes.h:32
int main(int argc, char *argv[])
void setChannelInfo(ChannelInfoPtr pchi)
int test_MultiChannelNoiseRemovalService(int a_LogLevel=1)
virtual int update(AdcChannelDataMap &datamap) const =0
Q_EXPORT QTSManip setw(int w)
Definition: qtextstream.h:331
virtual std::ostream & print(std::ostream &out=std::cout, std::string prefix="") const =0
unsigned int AdcChannel
Definition: AdcTypes.h:50
void line(double t, double *p, double &x, double &y, double &z)
std::vector< AdcSignal > AdcSignalVector
Definition: AdcTypes.h:22
std::map< AdcChannel, AdcChannelData > AdcChannelDataMap
AdcSignalVector samples
QTextStream & endl(QTextStream &s)
AdcFlagVector flags