test_IdealAdcSimulator.cxx
Go to the documentation of this file.
1 // test_IdealAdcSimulator.cxx
2 //
3 // David Adams
4 // April 2017
5 //
6 // Test IdealAdcSimulator.
7 
8 #include <string>
9 #include <iostream>
10 #include <fstream>
11 #include "fhiclcpp/ParameterSet.h"
16 
17 #undef NDEBUG
18 #include <cassert>
19 
20 using std::string;
21 using std::cout;
22 using std::endl;
23 using std::ofstream;
25 using ToolPtr = std::unique_ptr<AdcSimulator>;
27 
28 //**********************************************************************
29 
30 template<class T1, class T2>
31 void checkEqual(T1 x1, T2 x2, string msg ="Assert failed") {
32  if ( x1 != x2 ) {
33  cout << msg << ": " << x1 << " != " << x2 << endl;
34  assert(false);
35  }
36 }
37 
38 void checkCount(const ToolPtr& ptool, double vin, Count countExpected) {
39  Count countActual = ptool->count(vin);
40  cout << " ADC(" << vin << ") = " << countActual << endl;
41  checkEqual(countActual, countExpected);
42 }
43 
44 //**********************************************************************
45 
46 int test_IdealAdcSimulator(bool useExistingFcl) {
47  const string myname = "test_IdealAdcSimulator: ";
48 #ifdef NDEBUG
49  cout << myname << "NDEBUG must be off." << endl;
50  abort();
51 #endif
52  string line = "-----------------------------";
53 
54  cout << myname << line << endl;
55  string fclfile = "test_IdealAdcSimulator.fcl";
56  if ( ! useExistingFcl ) {
57  cout << myname << "Creating top-level FCL." << endl;
58  ofstream fout(fclfile.c_str());
59  fout << "mytool: {" << endl;
60  fout << " tool_type: IdealAdcSimulator" << endl;
61  fout << " Vsen: 2.0" << endl;
62  fout << " Nbit: 12" << endl;
63  fout << "}" << endl;
64  fout.close();
65  } else {
66  cout << myname << "Using existing top-level FCL." << endl;
67  }
68 
69  // Create pset
70  cout << myname << line << endl;
71  cout << myname << "Creating parameter set." << endl;
72  //putenv(const_cast<char*>("FHICL_FILE_PATH=./test:."));
73  cet::filepath_lookup policy("FHICL_FILE_PATH");
74  auto const psTop =
76 
77  cout << myname << line << endl;
78  cout << myname << "Checking tool type." << endl;
79  ParameterSet psTool = psTop.get<fhicl::ParameterSet>("mytool");
80  cout << "Tool type: " << psTool.get<string>("tool_type") << endl;
81  assert( psTool.get<string>("tool_type") == "IdealAdcSimulator" );
82 
83  cout << myname << line << endl;
84  cout << myname << "Instantiate tool." << endl;
85  std::unique_ptr<AdcSimulator> ptool = art::make_tool<AdcSimulator>(psTool);
86  assert( ptool != nullptr );
87  double vin = 1200;
88  cout << "ADC count is " << ptool->count(vin) << endl;
89  checkCount(ptool, -999.0, 0);
90  checkCount(ptool, -999.0, 0 );
91  checkCount(ptool, -0.01, 0 );
92  checkCount(ptool, 0.0, 0 );
93  checkCount(ptool, 0.2, 0 );
94  checkCount(ptool, 1.0, 1);
95  checkCount(ptool, 201.0, 101 );
96  checkCount(ptool, 2001.0, 1001 );
97  checkCount(ptool, 8187.0, 4094 );
98  checkCount(ptool, 8189.0, 4095 );
99  checkCount(ptool, 8190.0, 4095 );
100  checkCount(ptool, 8191.0, 4095 );
101  checkCount(ptool, 999999.0, 4095 );
102 
103  cout << myname << line << endl;
104  cout << myname << "Done." << endl;
105  return 0;
106 }
107 
108 //**********************************************************************
109 
110 int main(int argc, char* argv[]) {
111  bool useExistingFcl = false;
112  if ( argc > 1 ) {
113  string sarg(argv[1]);
114  if ( sarg == "-h" ) {
115  cout << "Usage: " << argv[0] << " [ARG]" << endl;
116  cout << " If ARG = true, existing FCL file is used." << endl;
117  return 0;
118  }
119  useExistingFcl = sarg == "true" || sarg == "1";
120  }
121  return test_IdealAdcSimulator(useExistingFcl);
122 }
123 
124 //**********************************************************************
void msg(const char *fmt,...)
Definition: message.cpp:107
std::string string
Definition: nybbler.cc:12
static ParameterSet make(intermediate_table const &tbl)
Definition: ParameterSet.cc:68
Definition: 013_class.h:14
std::unique_ptr< TpcDataTool > ToolPtr
int main(int argc, char *argv[])
void checkCount(const ToolPtr &ptool, double vin, Count countExpected)
Definition: 013_class.h:10
T get(std::string const &key) const
Definition: ParameterSet.h:271
int test_IdealAdcSimulator(bool useExistingFcl)
AdcSimulator::Count Count
void checkEqual(T1 x1, T2 x2, string msg="Assert failed")
virtual Count count(double vin, Channel chan=0, Tick tick=0) const =0
intermediate_table parse_document(std::string const &filename, cet::filepath_maker &maker)
Definition: parse.cc:720
void line(double t, double *p, double &x, double &y, double &z)
unsigned short Count
Definition: AdcSimulator.h:24
QTextStream & endl(QTextStream &s)