test_AdcRangeSampleScaler.cxx
Go to the documentation of this file.
1 // test_AdcRangeSampleScaler.cxx
2 //
3 // David Adams
4 // March 2019
5 //
6 // Test AdcRangeSampleScaler.
7 
8 #include <string>
9 #include <iostream>
10 #include <fstream>
11 #include <iomanip>
14 #include "TRandom.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;
24 using std::setw;
26 
27 using Vector = std::vector<float>;
28 using Index = unsigned int;
29 
30 //**********************************************************************
31 
32 int test_AdcRangeSampleScaler(bool useExistingFcl, bool useMod) {
33  const string myname = "test_AdcRangeSampleScaler: ";
34 #ifdef NDEBUG
35  cout << myname << "NDEBUG must be off." << endl;
36  abort();
37 #endif
38  string line = "-----------------------------";
39 
40  cout << myname << line << endl;
41  string fclfile = "test_AdcRangeSampleScaler.fcl";
42  Index chaMod = useMod ? 30 : 0;
43  if ( ! useExistingFcl ) {
44  cout << myname << "Creating top-level FCL." << endl;
45  ofstream fout(fclfile.c_str());
46  fout << "tools: {" << endl;
47  fout << " mytool: {" << endl;
48  fout << " tool_type: AdcRangeSampleScaler" << endl;
49  fout << " LogLevel: 1" << endl;
50  fout << " RangeLimits: [10, 20]" << endl;
51  fout << " RangeModulus: " << chaMod << endl;
52  fout << " ScaleFactors: [2, 4, 8]" << endl;
53  fout << " }" << endl;
54  fout << "}" << endl;
55  fout.close();
56  } else {
57  cout << myname << "Using existing top-level FCL." << endl;
58  }
59 
60  cout << myname << line << endl;
61  cout << myname << "Fetching tool manager." << endl;
63  assert ( ptm != nullptr );
64  DuneToolManager& tm = *ptm;
65  tm.print();
66  assert( tm.toolNames().size() == 1 );
67 
68  cout << myname << line << endl;
69  cout << myname << "Fetching tool." << endl;
70  auto ptoo = tm.getPrivate<TpcDataTool>("mytool");
71  assert( ptoo != nullptr );
72 
73  cout << myname << line << endl;
74  cout << myname << "Create data." << endl;
75  Index ncha = 50;
76  Index nsam = 20;
77  AdcChannelDataMap acds;
78  for ( Index icha=0; icha<ncha; ++icha ) {
79  AdcChannelData& acd = acds[icha];
80  acd.setEventInfo(123, 456);
81  acd.setChannelInfo(icha);
82  acd.samples.resize(nsam, 10.0);
83  acd.sampleUnit = "ADC";
84  }
85 
86  cout << myname << line << endl;
87  cout << myname << "Create expected data." << endl;
88  Vector qexps(ncha, 10.);
89  for ( Index icha=0; icha<ncha; ++icha ) {
90  Index ichaMod = useMod ? icha%chaMod : icha;
91  if ( ichaMod >= 20 ) qexps[icha] = 80;
92  else if ( ichaMod >= 10 ) qexps[icha] = 40;
93  else qexps[icha] = 20;
94  }
95 
96  cout << myname << line << endl;
97  cout << myname << "Call tool." << endl;
98  DataMap ret = ptoo->updateMap(acds);
99  assert( ! ret );
100 
101  cout << myname << line << endl;
102  cout << myname << "Check samples." << endl;
103  for ( const AdcChannelDataMap::value_type& iacd : acds ) {
104  Index icha = iacd.first;
105  const AdcChannelData& acd = iacd.second;
106  assert( acd.channel() == icha );
107  assert( acd.samples.size() == nsam );
108  float qsam = acd.samples[0];
109  float qexp = qexps[icha];
110  float qdif = fabs(qsam - qexp);
111  cout << myname << setw(4) << icha << ":" << setw(10) << qexp << setw(10) << qsam << endl;
112  assert( qdif < 1.e-5 );
113  for ( float qchk : acd.samples ) {
114  assert( qchk == qsam );
115  }
116  }
117 
118  cout << myname << line << endl;
119  cout << myname << "Done." << endl;
120  return 0;
121 }
122 
123 //**********************************************************************
124 
125 int main(int argc, char* argv[]) {
126  bool useExistingFcl = false;
127  bool useMod = false;
128  if ( argc > 1 ) {
129  string sarg(argv[1]);
130  if ( sarg == "-h" ) {
131  cout << "Usage: " << argv[0] << " [ARG [qsig [ped [noise]]]]" << endl;
132  cout << " If ARG = true, existing FCL file is used." << endl;
133  return 0;
134  }
135  useExistingFcl = sarg == "true" || sarg == "1";
136  }
137  if ( argc > 2 ) {
138  useMod = true;
139  }
140  return test_AdcRangeSampleScaler(useExistingFcl, useMod);
141 }
142 
143 //**********************************************************************
const std::vector< std::string > & toolNames() const
std::string string
Definition: nybbler.cc:12
void print() const
unsigned int Index
tm
Definition: demo.py:21
int main(int argc, char *argv[])
void setChannelInfo(ChannelInfoPtr pchi)
const double e
void setEventInfo(EventInfoPtr pevi)
Q_EXPORT QTSManip setw(int w)
Definition: qtextstream.h:331
std::unique_ptr< T > getPrivate(std::string name)
Channel channel() const
void line(double t, double *p, double &x, double &y, double &z)
std::map< AdcChannel, AdcChannelData > AdcChannelDataMap
int test_AdcRangeSampleScaler(bool useExistingFcl, bool useMod)
static DuneToolManager * instance(std::string fclname="", int dbg=1)
AdcSignalVector samples
QTextStream & endl(QTextStream &s)