test_AdcCodeMitigator.cxx
Go to the documentation of this file.
1 // test_AdcCodeMitigator.cxx
2 //
3 // David Adams
4 // April 2017
5 //
6 // Test AdcCodeMitigator.
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::setw;
22 using std::ofstream;
24 using Index = unsigned int;
25 
26 bool isEqual(float x1, float x2) {
27  if ( fabs(x2 - x1) < 1.e-5 ) return true;
28  cout << "isEqual: " << x1 << " != " << x2 << endl;
29  return false;
30 }
31 
32 //**********************************************************************
33 
34 int test_AdcCodeMitigator(bool useExistingFcl =false, bool interp =false) {
35  const string myname = "test_AdcCodeMitigator: ";
36 #ifdef NDEBUG
37  cout << myname << "NDEBUG must be off." << endl;
38  abort();
39 #endif
40  string line = "-----------------------------";
41 
42  cout << myname << line << endl;
43  string fclfile = "test_AdcCodeMitigator.fcl";
44  if ( ! useExistingFcl ) {
45  cout << myname << "Creating top-level FCL." << endl;
46  ofstream fout(fclfile.c_str());
47  fout << "tools: {" << endl;
48  fout << " mytool: {" << endl;
49  fout << " tool_type: AdcCodeMitigator" << endl;
50  fout << " LogLevel: 1" << endl;
51  if ( interp ) {
52  fout << " FixFlags: []" << endl;
53  fout << " InterpolateFlags: [8]" << endl;
54  } else {
55  fout << " FixFlags: [8]" << endl;
56  fout << " InterpolateFlags: []" << endl;
57  }
58  fout << " SkipFlags: []" << endl;
59  fout << " FixedCurvThresh: 0.0" << endl;
60  fout << " }" << endl;
61  fout << "}" << endl;
62  fout.close();
63  } else {
64  cout << myname << "Using existing top-level FCL." << endl;
65  }
66 
67  cout << myname << line << endl;
68  cout << myname << "Fetching tool manager." << endl;
70  assert ( ptm != nullptr );
71  DuneToolManager& tm = *ptm;
72  tm.print();
73  assert( tm.toolNames().size() == 1 );
74 
75  cout << myname << line << endl;
76  cout << myname << "Fetching tool." << endl;
77  auto pmit = tm.getPrivate<TpcDataTool>("mytool");
78  assert( pmit != nullptr );
79 
80  cout << myname << line << endl;
81  cout << myname << "Create data and call tool." << endl;
82  AdcSignalVector chkSigs = { 5, 5, 11.0, 11.5, 12.0, 12.5, 13, 14, 14};
83  AdcSignalVector inpSigs = {1000, 5, 11.0, 1000, 1000, 1000, 13, 14, 1000};
84  AdcFlagVector inpFlgs = { 8, 0, 0, 8, 8, 8, 0, 0, 8};
85  AdcFlagVector chkFlgs = { AdcExtrapolated,
87  0, 0, AdcExtrapolated};
88  Index nsam = chkSigs.size();
89  if ( ! interp ) {
90  for ( AdcIndex isam=0; isam<nsam; ++isam ) {
91  if ( inpFlgs[isam] != 0 ) {
92  chkSigs[isam] = 0.0;
93  chkFlgs[isam] = AdcSetFixed;
94  }
95  }
96  }
97  AdcChannelData acd;
98  acd.samples = inpSigs;
99  acd.flags = inpFlgs;
100  DataMap ret = pmit->update(acd);
101  ret.print();
102  assert( ret == 0 );
103  assert( ret.getInt("mitCount") == 5 );
104  for ( AdcIndex isam=0; isam<nsam; ++isam ) {
105  cout << isam << ":" << setw(6) << inpSigs[isam] << setw(6) << inpFlgs[isam]
106  << setw(6) << acd.samples[isam] << setw(6) << acd.flags[isam] << endl;
107  assert( isEqual(acd.samples[isam], chkSigs[isam]) );
108  assert( acd.flags[isam] == chkFlgs[isam] );
109  }
110 
111  cout << myname << line << endl;
112  cout << myname << "Done." << endl;
113  return 0;
114 }
115 
116 //**********************************************************************
117 
118 int main(int argc, char* argv[]) {
119  bool useExistingFcl = false;
120  bool interp = true;
121  if ( argc > 1 ) {
122  string sarg(argv[1]);
123  if ( sarg == "-h" ) {
124  cout << "Usage: " << argv[0] << " [ARG] [FIX]" << endl;
125  cout << " If ARG = true, existing FCL file is used." << endl;
126  cout << " If FIX = true, fixed value is used instead of interpolation." << endl;
127  return 0;
128  }
129  useExistingFcl = sarg == "true" || sarg == "1";
130  if ( argc > 2 ) {
131  interp = false;
132  string sarg(argv[2]);
133  interp = sarg == "true" || sarg == "1";
134  }
135  }
136  return test_AdcCodeMitigator(useExistingFcl, interp);
137 }
138 
139 //**********************************************************************
bool isEqual(float x1, float x2)
const std::vector< std::string > & toolNames() const
std::string string
Definition: nybbler.cc:12
std::vector< AdcFlag > AdcFlagVector
Definition: AdcTypes.h:30
void print() const
unsigned int Index
void print(std::ostream *pout) const
Definition: DataMap.h:245
tm
Definition: demo.py:21
const AdcFlag AdcSetFixed
Definition: AdcTypes.h:41
const double e
const AdcFlag AdcExtrapolated
Definition: AdcTypes.h:43
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)
int getInt(Name name, int def=0) const
Definition: DataMap.h:218
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
int test_AdcCodeMitigator(bool useExistingFcl=false, bool interp=false)
static DuneToolManager * instance(std::string fclname="", int dbg=1)
AdcSignalVector samples
int main(int argc, char *argv[])
QTextStream & endl(QTextStream &s)
AdcFlagVector flags