test_FclFloatArray.cxx
Go to the documentation of this file.
1 // test_FclFloatArray.cxx
2 //
3 // David Adams
4 // January 2019
5 //
6 // Test FclFloatArray.
7 
8 #include <string>
9 #include <iostream>
10 #include <fstream>
11 #include <sstream>
14 #include "TH1F.h"
15 
16 #undef NDEBUG
17 #include <cassert>
18 
19 using std::string;
20 using std::cout;
21 using std::endl;
22 using std::ofstream;
23 using std::istringstream;
25 using Index = unsigned int;
26 
27 //**********************************************************************
28 
29 bool isequal(float lhs, float rhs ) {
30  string myname = "isequal: ";
31  if ( fabs(lhs - rhs) < 1.e-10 ) return true;
32  cout << myname << lhs << " != " << rhs << endl;
33  return false;
34 }
35 
36 int test_FclFloatArray(bool useExistingFcl =false) {
37  const string myname = "test_FclFloatArray: ";
38 #ifdef NDEBUG
39  cout << myname << "NDEBUG must be off." << endl;
40  abort();
41 #endif
42  string line = "-----------------------------";
43 
44  cout << myname << line << endl;
45  string fclfile = "test_FclFloatArray.fcl";
46  Index ioff = 10;
47  if ( ! useExistingFcl ) {
48  cout << myname << "Creating top-level FCL." << endl;
49  ofstream fout(fclfile.c_str());
50  fout << "tools: {" << endl;
51  fout << " mytool: {" << endl;
52  fout << " tool_type: FclFloatArray" << endl;
53  fout << " LogLevel: 2" << endl;
54  fout << " DefaultValue: -1" << endl;
55  fout << " Offset: " << ioff << endl;
56  fout << " Label: myvals" << endl;
57  fout << " Unit: ke" << endl;
58  fout << " Values: [ 0, 1.01, 2.02, 3.03, 4.04, 5.05, 6.06, 7.07, 8.08, 9.09," << endl;
59  fout << " 10.10, 11.11, 12.12, 13.13, 14.14, 15.15 ]" << 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 pto = tm.getPrivate<FloatArrayTool>("mytool");
78  assert( pto != nullptr );
79 
80  cout << myname << line << endl;
81  cout << "Check tool params." << endl;
82  Index nval = 16;
83  assert( pto->offset() == ioff );
84  assert( pto->label() == "myvals" );
85  assert( pto->size() == nval );
86  assert( pto->offset() == ioff );
87 
88  cout << myname << line << endl;
89  cout << "Check fill." << endl;
91  float fval = 1234.5;
92  assert( pto->fill(fvals, fval) == 0 );
93  assert( fvals.size() == nval + ioff );
94  for ( Index ival=0; ival<ioff; ++ival ) {
95  assert( fvals[ival] == fval );
96  }
97 
98  cout << myname << line << endl;
99  cout << "Check values." << endl;
100  for ( Index ival=0; ival<nval; ++ival ) {
101  assert( pto->values()[ival] == fvals[ioff+ival] );
102  assert( isequal(pto->values()[ival], 1.01*ival) );
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  bool useExistingFcl = false;
114  if ( argc > 1 ) {
115  string sarg(argv[1]);
116  if ( sarg == "-h" ) {
117  cout << "Usage: " << argv[0] << " [keepFCL] [RUN]" << endl;
118  cout << " If keepFCL = true, existing FCL file is used." << endl;
119  cout << " If RUN is nonzero, the data for that run are displayed." << endl;
120  return 0;
121  }
122  useExistingFcl = sarg == "true" || sarg == "1";
123  }
124  return test_FclFloatArray(useExistingFcl);
125 }
126 
127 //**********************************************************************
const std::vector< std::string > & toolNames() const
std::string string
Definition: nybbler.cc:12
bool isequal(float lhs, float rhs)
void print() const
unsigned int Index
tm
Definition: demo.py:21
const double e
std::vector< float > FloatVector
int test_FclFloatArray(bool useExistingFcl=false)
std::unique_ptr< T > getPrivate(std::string name)
void line(double t, double *p, double &x, double &y, double &z)
static DuneToolManager * instance(std::string fclname="", int dbg=1)
QTextStream & endl(QTextStream &s)
int main(int argc, char *argv[])