test_AdcRegularSignalFinder.cxx
Go to the documentation of this file.
1 // test_AdcRegularSignalFinder.cxx
2 //
3 // David Adams
4 // April 2017
5 //
6 // Test AdcRegularSignalFinder.
7 
8 #include <string>
9 #include <iostream>
10 #include <fstream>
11 #include <iomanip>
14 
15 #undef NDEBUG
16 #include <cassert>
17 
18 using std::string;
19 using std::cout;
20 using std::endl;
21 using std::ofstream;
22 using std::setw;
24 using Index = AdcIndex;
25 
26 //**********************************************************************
27 
28 int test_AdcRegularSignalFinder(bool useExistingFcl =false) {
29  const string myname = "test_AdcRegularSignalFinder: ";
30 #ifdef NDEBUG
31  cout << myname << "NDEBUG must be off." << endl;
32  abort();
33 #endif
34  string line = "-----------------------------";
35 
36  cout << myname << line << endl;
37  string fclfile = "test_AdcRegularSignalFinder.fcl";
38  if ( ! useExistingFcl ) {
39  cout << myname << "Creating top-level FCL." << endl;
40  ofstream fout(fclfile.c_str());
41  fout << "tools: {" << endl;
42  fout << " mytool: {" << endl;
43  fout << " tool_type: AdcRegularSignalFinder" << endl;
44  fout << " LogLevel: 1" << endl;
45  fout << " Period: 30" << endl;
46  fout << " Length: 20" << endl;
47  fout << " }" << endl;
48  fout << "}" << endl;
49  fout.close();
50  } else {
51  cout << myname << "Using existing top-level FCL." << endl;
52  }
53 
54  cout << myname << line << endl;
55  cout << myname << "Fetching tool manager." << endl;
57  assert ( ptm != nullptr );
58  DuneToolManager& tm = *ptm;
59  tm.print();
60  assert( tm.toolNames().size() == 1 );
61 
62  cout << myname << line << endl;
63  cout << myname << "Fetching tool." << endl;
64  auto psgf = tm.getPrivate<TpcDataTool>("mytool");
65  assert( psgf != nullptr );
66  auto psgfmod = tm.getPrivate<TpcDataTool>("mytool");
67  assert( psgfmod != nullptr );
68 
69  cout << myname << line << endl;
70  cout << myname << "Create data and call tool." << endl;
72  Index nsam = 100;
73  for ( AdcIndex itic=0; itic<nsam; ++itic ) {
74  float xadc = rand()%20 - 10.0;
75  data.samples.push_back(xadc);
76  }
77  data.samples[30] = 150.0;
78  assert( data.signal.size() == 0 );
79  assert( data.rois.size() == 0 );
80  assert( data.samples.size() == nsam );
81  assert( data.samples[30] = 150 );
82 
83  cout << myname << line << endl;
84  cout << myname << "Running tool." << endl;
85  DataMap resmod = psgfmod->update(data);
86  resmod.print();
87 
88  cout << myname << line << endl;
89  cout << myname << "Checking results." << endl;
90  Index per = 30;
91  Index len = 20;
92  Index nroi = 4;
93 
94  assert( resmod == 0 );
95  assert( resmod.getInt("roiPeriod") == int(per) );
96  assert( resmod.getInt("roiLength") == int(len) );
97  assert( resmod.getInt("roiCount") == int(nroi) );
98  assert( data.signal.size() == nsam );
99  assert( data.rois.size() == nroi );
100  for ( Index iroi=0; iroi<nroi; ++iroi ) {
101  AdcRoi roi = data.rois[iroi];
102  cout << myname << "ROI " << iroi << ": ["
103  << roi.first << ", "
104  << roi.second << "]" << endl;
105  Index isam1 = iroi*per;
106  Index isam2 = isam1 + len;
107  if ( isam2 > nsam ) isam2 = nsam;
108  Index isam3 = isam1 + per;
109  if ( isam3 > nsam ) isam3 = nsam;
110  for ( Index isam=isam1; isam<isam3; ++isam ) {
111  bool sig = data.signal[isam];
112  string ssig = sig ? "true" : "false";
113  cout << myname << setw(4) << isam << ": " << ssig << endl;
114  if ( isam < isam2 ) assert( data.signal[isam]);
115  else assert( ! data.signal[isam]);
116  }
117  assert( roi.first == isam1 );
118  assert( roi.second == isam2 - 1 );
119  }
120  cout << myname << line << endl;
121  cout << myname << "Done." << endl;
122  return 0;
123 }
124 
125 //**********************************************************************
126 
127 int main(int argc, char* argv[]) {
128  bool useExistingFcl = false;
129  if ( argc > 1 ) {
130  string sarg(argv[1]);
131  if ( sarg == "-h" ) {
132  cout << "Usage: " << argv[0] << " [ARG]" << endl;
133  cout << " If ARG = true, existing FCL file is used." << endl;
134  return 0;
135  }
136  useExistingFcl = sarg == "true" || sarg == "1";
137  }
138  return test_AdcRegularSignalFinder(useExistingFcl);
139 }
140 
141 //**********************************************************************
const std::vector< std::string > & toolNames() const
std::string string
Definition: nybbler.cc:12
std::pair< AdcIndex, AdcIndex > AdcRoi
Definition: AdcTypes.h:54
void print() const
unsigned int Index
void print(std::ostream *pout) const
Definition: DataMap.h:245
tm
Definition: demo.py:21
AdcRoiVector rois
unsigned int AdcIndex
Definition: AdcTypes.h:15
Q_EXPORT QTSManip setw(int w)
Definition: qtextstream.h:331
std::unique_ptr< T > getPrivate(std::string name)
AdcFilterVector signal
int main(int argc, char *argv[])
int getInt(Name name, int def=0) const
Definition: DataMap.h:218
int test_AdcRegularSignalFinder(bool useExistingFcl=false)
void line(double t, double *p, double &x, double &y, double &z)
static DuneToolManager * instance(std::string fclname="", int dbg=1)
AdcSignalVector samples
QTextStream & endl(QTextStream &s)