test_VDColdboxOnlineChannel.cxx
Go to the documentation of this file.
1 // test_VDColdboxOnlineChannel.cxx
2 //
3 // David Adams
4 // May 2018
5 //
6 // Test VDColdboxOnlineChannel.
7 
8 #include <string>
9 #include <iostream>
10 #include <fstream>
11 #include <sstream>
12 #include <iomanip>
17 #include "TH1F.h"
18 
19 #undef NDEBUG
20 #include <cassert>
21 
22 using std::string;
23 using std::cout;
24 using std::endl;
25 using std::ofstream;
26 using std::istringstream;
27 using std::ostringstream;
28 using std::setw;
31 using IndexVector = std::vector<Index>;
32 
33 string sonline(Index ichaOn) {
34  ostringstream ssout;
35  Index irem = ichaOn;
36  Index ifmb = irem/128;
37  irem = irem%128;
38  Index iasc = irem/16 + 1;
39  Index iach = irem%16;
40  ssout << (ifmb<10 ? "0" : "") << ifmb << "-" << iasc << "-" << (iach<10 ? "0" : "") << iach;
41  return ssout.str();
42 }
43 
44 //**********************************************************************
45 
46 int test_VDColdboxOnlineChannel(bool useExistingFcl =false, Index nshow =64) {
47  const string myname = "test_VDColdboxOnlineChannel: ";
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_VDColdboxOnlineChannel.fcl";
56  if (useExistingFcl) {
57  cout << myname << "Using existing top-level FCL." << endl;
58  } else {
59  cout << myname << "Creating top-level FCL." << endl;
60  ofstream fout(fclfile.c_str());
61  fout << "#include \"VDColdboxChannelMapService.fcl\"" << endl;
62  fout << "services: { VDColdboxChannelMapService: @local::vdcoldboxchannelmap }" << endl;
63  fout << "tools: {" << endl;
64  fout << " mytool: {" << endl;
65  fout << " tool_type: VDColdboxOnlineChannel" << endl;
66  fout << " LogLevel: 1" << endl;
67  fout << " }" << endl;
68  fout << "}" << endl;
69  fout.close();
70  }
71 
72  cout << myname << line << endl;
73  cout << myname << "Fetching tool manager." << endl;
75  assert ( ptm != nullptr );
76  DuneToolManager& tm = *ptm;
77  tm.print();
78  assert( tm.toolNames().size() == 1 );
79 
80  std::ifstream config{fclfile};
82 
83  cout << myname << line << endl;
84  cout << myname << "Fetching tool." << endl;
85  auto cma = tm.getPrivate<IndexMapTool>("mytool");
86  assert( cma != nullptr );
87 
89 
90  cout << myname << line << endl;
91  cout << myname << "Check some good values." << endl;
92  for ( Index ichaOff : { 1600, 1700, 3199, 3200, 3391 } ) {
93  Index ichaOn = cma->get(ichaOff);
94  cout << myname << setw(5) << ichaOff << " --> " << setw(5) << ichaOn << endl;
95  assert( ichaOff != badIndex );
96  }
97 
98  cout << myname << line << endl;
99  cout << myname << "Check some bad values." << endl;
100  for ( Index ichaOff : { -1, 0, 100, 1599, 3392, 4000 } ) {
101  Index ichaOn = cma->get(ichaOff);
102  cout << myname << ichaOff << " --> " << ichaOn << endl;
103  assert( ichaOn == badIndex );
104  }
105 
106  cout << myname << line << endl;
107  cout << myname << "Check each online index appears exactly once." << endl;
108  const Index nchaOn = 14*128;
109  const Index ichaOn1 = 128;
110  const Index ichaOn2 = ichaOn1 + nchaOn;
111  const Index ichaOff1 = 1600;
112  const Index ichaOff2 = ichaOff1 + nchaOn;
113  IndexVector offlineChannel(ichaOn2, badIndex);
114  IndexVector onlineCounts(ichaOn2, 0);
115  Index ndup = 0;
116  for ( Index ichaOff=ichaOff1; ichaOff<ichaOff2; ++ichaOff ) {
117  Index ichaOn = cma->get(ichaOff);
118  if ( nshow*(ichaOff/nshow) == ichaOff ) {
119  cout << myname << " " << setw(4) << ichaOff << " --> " << setw(4) << ichaOn
120  << " (" << sonline(ichaOn) << ")" << endl;
121  }
122  assert( ichaOn >= ichaOn1 );
123  assert( ichaOn < ichaOn2 );
124  if ( offlineChannel[ichaOn] != badIndex ) {
125  cout << myname << "ERROR: Online channel " << setw(4) << ichaOn
126  << " (" << sonline(ichaOn) << ")"
127  << " is mapped to two offline channels: "
128  << offlineChannel[ichaOn]
129  << " " << ichaOff << endl;
130  ++ndup;
131  }
132  onlineCounts[ichaOn] += 1;
133  offlineChannel[ichaOn] = ichaOff;
134  }
135  Index nbadOn = 0;
136  Index nbadOff = 0;
137  for ( Index ichaOn=ichaOn1; ichaOn<ichaOn2; ++ichaOn ) {
138  if ( onlineCounts[ichaOn] != 1 ) {
139  cout << "ERROR: Map count for online channel " << ichaOn << " is " << onlineCounts[ichaOn] << endl;;
140  ++nbadOn;
141  } else if ( offlineChannel[ichaOn] == badIndex ) {
142  cout << "ERROR: Online channel " << ichaOn << " is not mapped." << endl;
143  }
144  }
145  assert( ndup + nbadOn + nbadOff == 0 );
146 
147  cout << myname << line << endl;
148  cout << myname << "Done." << endl;
149  return 0;
150 }
151 
152 //**********************************************************************
153 
154 int main(int argc, char* argv[]) {
155  bool useExistingFcl = false;
156  Index nshow = 1; // Every nshow'th value is displayed in log.
157  if ( argc > 1 ) {
158  string sarg(argv[1]);
159  if ( sarg == "-h" ) {
160  cout << "Usage: " << argv[0] << " [keepFCL] [NSHOW]" << endl;
161  cout << " keepFCL [false]: If true, existing FCL file is used." << endl;
162  cout << " NSHOW [64]: Every nshow'th channels will be displayed in log." << endl;
163  return 0;
164  }
165  useExistingFcl = sarg == "true" || sarg == "1";
166  }
167  if ( argc > 2 ) {
168  string sarg(argv[2]);
169  nshow = std::stoi(sarg);
170  }
171  return test_VDColdboxOnlineChannel(useExistingFcl, nshow);
172 }
173 
174 //**********************************************************************
std::vector< Index > IndexVector
Index badIndex
const std::vector< std::string > & toolNames() const
std::string string
Definition: nybbler.cc:12
int main(int argc, char *argv[])
string sonline(Index ichaOn)
void print() const
unsigned int Index
static void load_services(std::string const &config)
tm
Definition: demo.py:21
static Config * config
Definition: config.cpp:1054
Q_EXPORT QTSManip setw(int w)
Definition: qtextstream.h:331
std::unique_ptr< T > getPrivate(std::string name)
unsigned int Index
Definition: IndexMapTool.h:16
static Index badIndex()
Definition: IndexMapTool.h:18
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 test_VDColdboxOnlineChannel(bool useExistingFcl=false, Index nshow=64)