test_AdcRoiSlicer.cxx
Go to the documentation of this file.
1 // test_AdcRoiSlicer.cxx
2 //
3 // David Adams
4 // September 2019
5 //
6 // Test AdcRoiSlicer.
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 
25 using Index = unsigned int;
26 using IndexVector = std::vector<Index>;
27 using Name = string;
28 using NameVector = std::vector<Name>;
29 
30 bool checkint(int i1, int i2) {
31  const Name myname = "checkint: ";
32  if ( i1 == i2 ) return true;
33  cout << myname << i1 << " != " << i2 << endl;
34  return false;
35 }
36 
37 //**********************************************************************
38 
39 int test_AdcRoiSlicer(int opt, Index nsam, bool useExistingFcl =false) {
40  const string myname = "test_AdcRoiSlicer: ";
41 #ifdef NDEBUG
42  cout << myname << "NDEBUG must be off." << endl;
43  abort();
44 #endif
45  string line = "-----------------------------";
46 
47  cout << myname << line << endl;
48  string fclfile = "test_AdcRoiSlicer.fcl";
49  NameVector tnams = {"", "mytool1", "mytool2", "mytool3"};
50  if ( ! useExistingFcl ) {
51  cout << myname << "Creating top-level FCL." << endl;
52  ofstream fout(fclfile.c_str());
53  fout << "tools: {" << endl;
54  for ( int iopt : {1, 2, 3} ) {
55  fout << " mytool" <<iopt << ": {" << endl;
56  fout << " tool_type: AdcRoiSlicer" << endl;
57  fout << " LogLevel: 1" << endl;
58  fout << " OutViewName: rois" << endl;
59  fout << " SliceOpt: " << iopt << endl;
60  fout << " CopyRaw: false" << endl;
61  fout << " }" << endl;
62  }
63  fout << "}" << endl;
64  fout.close();
65  } else {
66  cout << myname << "Using existing top-level FCL." << endl;
67  }
68 
69  cout << myname << line << endl;
70  cout << myname << "Fetching tool manager." << endl;
72  assert ( ptm != nullptr );
73  DuneToolManager& tm = *ptm;
74  tm.print();
75  assert( tm.toolNames().size() == 3 );
76 
77  cout << myname << line << endl;
78  cout << myname << "Fetching tool." << endl;
79  auto psli = tm.getPrivate<TpcDataTool>(tnams[opt]);
80  assert( psli != nullptr );
81 
82  cout << myname << line << endl;
83  cout << myname << "Create data and call tool." << endl;
85  bool endInSig = false;
86  Index isam1 = 20;
87  Index isam2 = 40;
88  Index isam3 = 80;
89  Index isam4 = 90;
90  for ( AdcIndex isam=0; isam<nsam; ++isam ) {
91  float xadc = rand()%20 - 10.0;
92  bool isSig = (isam >= isam1 && isam < isam2) || (isam >= isam3 && isam < isam4);
93  if ( isSig ) xadc += 100;
94  data.signal.push_back(isSig);
95  data.samples.push_back(xadc);
96  endInSig = isSig;
97  cout << myname << setw(4) << isam << setw(3) << data.signal[isam]
98  << " " << data.samples[isam] << endl;
99  }
100  data.roisFromSignal();
101  Index nroi = data.rois.size();
102  Index nnot = nroi;
103  if ( ! endInSig ) ++nnot;
104  cout << myname << " opt: " << opt << endl;
105  cout << myname << "nroi: " << nroi << endl;
106  cout << myname << "nnot: " << nnot << endl;
107  Index nexp = 0;
108  IndexVector viewSizes;
109  IndexVector viewTick0s;
110  if ( nsam < isam4 ) isam4 = nsam;
111  if ( nsam < isam3 ) isam3 = nsam;
112  if ( opt == 1 ) {
113  nexp = nroi;
114  viewTick0s.push_back(isam1);
115  viewTick0s.push_back(isam3);
116  viewSizes.push_back(isam2 - isam1);
117  viewSizes.push_back(isam4 - isam3);
118  } else if ( opt == 2 ) {
119  nexp = nnot;
120  viewTick0s.push_back(0);
121  viewTick0s.push_back(isam2);
122  viewTick0s.push_back(isam4);
123  viewSizes.push_back(isam1);
124  viewSizes.push_back(isam3 - isam2);
125  viewSizes.push_back(nsam - isam4);
126  } else if ( opt == 3 ) {
127  nexp = nroi + nnot;
128  viewTick0s.push_back(0);
129  viewTick0s.push_back(isam1);
130  viewTick0s.push_back(isam2);
131  viewTick0s.push_back(isam3);
132  viewTick0s.push_back(isam4);
133  viewSizes.push_back(isam1);
134  viewSizes.push_back(isam2 - isam1);
135  viewSizes.push_back(isam3 - isam2);
136  viewSizes.push_back(isam4 - isam3);
137  viewSizes.push_back(nsam - isam4);
138  } else {
139  assert(false);
140  }
141  cout << myname << "nexp: " << nexp << endl;
142  assert( data.signal.size() == nsam );
143  assert( data.samples.size() == nsam );
144 
145  cout << myname << line << endl;
146  cout << myname << "Running tool." << endl;
147  DataMap res = psli->update(data);
148  res.print();
149 
150  cout << myname << line << endl;
151  cout << myname << "Checking results." << endl;
152  assert( res == 0 );
153  assert( Index(res.getInt("nRoiView")) == nexp );
154  assert( data.viewSize() == 1 );
155  assert( data.viewNames().size() == 1 );
156  Name vnam = "rois";
157  assert( data.viewNames()[0] == vnam );
158  assert( data.view(vnam).size() == nexp );
159  for ( Index ivie=0; ivie<nexp; ++ivie ) {
160  cout << myname << "..entry " << ivie << endl;
161  const AdcChannelData& acd = data.view(vnam)[ivie];
162  assert( checkint(acd.tick0, int(viewTick0s[ivie])) );
163  assert( acd.viewParent != nullptr );
164  assert( acd.viewParent == &data );
165  Index nvsam = viewSizes[ivie];
166  assert( checkint(acd.samples.size(), nvsam) );
167  assert( checkint(acd.signal.size(), nvsam) );
168  for ( Index ivsam=0; ivsam<nvsam; ++ivsam ) {
169  int qv = 1000*acd.samples[ivsam];
170  int q0 = 1000*data.samples[ivsam + acd.tick0];
171  int sv = acd.signal[ivsam];
172  int s0 = data.signal[ivsam + acd.tick0];
173  cout << myname << setw(3) << ivsam << ": " << sv << " "
174  << setw(7) << qv << endl;
175  assert( checkint(qv, q0) );
176  assert( checkint(sv, s0) );
177  }
178  }
179 
180  cout << myname << line << endl;
181  cout << myname << "Done." << endl;
182  return 0;
183 }
184 
185 //**********************************************************************
186 
187 int main(int argc, char* argv[]) {
188  bool useExistingFcl = false;
189  if ( argc > 1 ) {
190  string sarg(argv[1]);
191  if ( sarg == "-h" ) {
192  cout << "Usage: " << argv[0] << " [ARG]" << endl;
193  cout << " If ARG = true, existing FCL file is used." << endl;
194  return 0;
195  }
196  useExistingFcl = sarg == "true" || sarg == "1";
197  }
198  int rs = 0;
199  if ( (rs = test_AdcRoiSlicer(1, 100, useExistingFcl)) ) return rs;
200  if ( (rs = test_AdcRoiSlicer(2, 100, useExistingFcl)) ) return rs;
201  if ( (rs = test_AdcRoiSlicer(3, 100, useExistingFcl)) ) return rs;
202  if ( (rs = test_AdcRoiSlicer(1, 85, useExistingFcl)) ) return rs;
203  return 0;
204 }
205 
206 //**********************************************************************
std::vector< Index > IndexVector
int test_AdcRoiSlicer(int opt, Index nsam, bool useExistingFcl=false)
const std::vector< std::string > & toolNames() const
std::string string
Definition: nybbler.cc:12
opt
Definition: train.py:196
bool checkint(int i1, int i2)
ChannelGroupService::Name Name
void print() const
unsigned int Index
void print(std::ostream *pout) const
Definition: DataMap.h:245
tm
Definition: demo.py:21
AdcRoiVector rois
NameVector viewNames() const
AdcChannelData * viewParent
unsigned int AdcIndex
Definition: AdcTypes.h:15
unsigned int Index
Q_EXPORT QTSManip setw(int w)
Definition: qtextstream.h:331
const View & view(Name vnam) const
std::unique_ptr< T > getPrivate(std::string name)
int main(int argc, char *argv[])
AdcFilterVector signal
int getInt(Name name, int def=0) const
Definition: DataMap.h:218
std::vector< string > NameVector
void line(double t, double *p, double &x, double &y, double &z)
size_t viewSize() const
static DuneToolManager * instance(std::string fclname="", int dbg=1)
AdcSignalVector samples
QTextStream & endl(QTextStream &s)