test_PdspOnlineChannel.cxx
Go to the documentation of this file.
1 // test_PdspOnlineChannel.cxx
2 //
3 // David Adams
4 // May 2018
5 //
6 // Test PdspOnlineChannel.
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::setw;
30 using IndexVector = std::vector<Index>;
31 
32 //**********************************************************************
33 
34 int test_PdspOnlineChannel(bool useExistingFcl =false, Index nshow =64) {
35  const string myname = "test_PdspOnlineChannel: ";
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_PdspOnlineChannel.fcl";
44  if (useExistingFcl) {
45  cout << myname << "Using existing top-level FCL." << endl;
46  } else {
47  cout << myname << "Creating top-level FCL." << endl;
48  ofstream fout(fclfile.c_str());
49  fout << "#include \"PdspChannelMapService.fcl\"" << endl;
50  fout << "services: { PdspChannelMapService: @local::pdspchannelmap }" << endl;
51  fout << "tools: {" << endl;
52  fout << " mytool: {" << endl;
53  fout << " tool_type: PdspOnlineChannel" << endl;
54  fout << " LogLevel: 1" << endl;
55  fout << " Ordering: \"FEMB\"" << endl;
56  fout << " }" << endl;
57  fout << " reftool: {" << endl;
58  fout << " tool_type: ProtoduneOnlineChannel" << endl;
59  fout << " LogLevel: 1" << endl;
60  fout << " }" << endl;
61  fout << "}" << endl;
62  fout.close();
63  }
64 
65  cout << myname << line << endl;
66  cout << myname << "Fetching tool manager." << endl;
68  assert ( ptm != nullptr );
69  DuneToolManager& tm = *ptm;
70  tm.print();
71  assert( tm.toolNames().size() >= 1 );
72 
73  std::ifstream config{fclfile};
75 
76  cout << myname << line << endl;
77  cout << myname << "Fetching tool." << endl;
78  auto cma = tm.getPrivate<IndexMapTool>("mytool");
79  assert( cma != nullptr );
80 
82 
83  cout << myname << line << endl;
84  cout << myname << "Check some good values." << endl;
85  for ( Index ichaOff : { 0, 102, 1234, 2560, 8480, 15359 } ) {
86  Index ichaOn = cma->get(ichaOff);
87  cout << myname << setw(5) << ichaOff << " --> " << setw(5) << ichaOn << endl;
88  assert( ichaOff != badIndex );
89  }
90 
91  cout << myname << line << endl;
92  cout << myname << "Check some bad values." << endl;
93  for ( Index ichaOff : { -1, 15360, 20000 } ) {
94  Index ichaOn = cma->get(ichaOff);
95  cout << myname << ichaOff << " --> " << ichaOn << endl;
96  assert( ichaOn == badIndex );
97  }
98 
99  cout << myname << line << endl;
100  cout << myname << "Check each online index appears exactly once." << endl;
101  const Index ncha = 15360;
102  IndexVector onlineCounts(ncha);
103  IndexVector offlineChannel(ncha, badIndex);
104  for ( Index ichaOff=0; ichaOff<ncha; ++ichaOff ) {
105  Index ichaOn = cma->get(ichaOff);
106  if ( nshow*(ichaOff/nshow) == ichaOff || ichaOn >= ncha ) {
107  Index irem = ichaOn;
108  Index itps = irem/2560;
109  irem = irem%2560;
110  Index ifmb = irem/128 + 1;
111  irem = irem%128;
112  Index iasc = irem/16 + 1;
113  Index ifch = irem%16;
114  cout << myname << " " << setw(4) << ichaOff << " --> " << setw(4) << ichaOn
115  << " (" << itps << ", " << setw(2) << ifmb << ", "
116  << iasc << ", " << setw(2) << ifch << ")" << endl;
117  }
118  assert( ichaOn < ncha );
119  if ( offlineChannel[ichaOn] != badIndex ) {
120  cout << myname << "ERROR: Online channel " << ichaOn
121  << " is mapped to two offline channels:" << endl;
122  cout << " " << offlineChannel[ichaOn] << endl;
123  cout << " " << ichaOff << endl;
124  assert( false );
125  }
126  assert( onlineCounts[ichaOn] == 0 );
127  onlineCounts[ichaOn] += 1;
128  offlineChannel[ichaOn] = ichaOff;
129  }
130  for ( Index ichaOn=0; ichaOn<ncha; ++ichaOn ) {
131  assert( onlineCounts[ichaOn] == 1 );
132  assert( offlineChannel[ichaOn] != badIndex );
133  }
134 
135  cout << myname << line << endl;
136  cout << myname << "Compare with ProtoduneChannelmap." << endl;
137  auto ref = tm.getPrivate<IndexMapTool>("reftool");
138  assert( ref != nullptr );
139  bool skipDiv1 = false; // Set this false to check every channel.
140  for ( Index idiv : {2560, 128, 1} ) {
141  if ( skipDiv1 && idiv == 1 ) {
142  cout << myname << "WARNING: Skipping div 1 test" << endl;
143  continue;
144  }
145  cout << myname << "...checking div " << idiv << endl;
146  for ( Index ichaOff=0; ichaOff<ncha; ++ichaOff ) {
147  Index ichaOnl = cma->get(ichaOff);
148  Index ichaRef = ref->get(ichaOff);
149  Index ichaOnlDiv = ichaOnl/idiv;
150  Index ichaRefDiv = ichaRef/idiv;
151  if ( ichaOnlDiv != ichaRefDiv ) {
152  cout << myname << "Maps disagree:" << endl;
153  cout << myname << " Offline: " << ichaOff << endl;
154  cout << myname << " Online: " << ichaOnl << " (" << ichaOnlDiv << ")" << endl;
155  cout << myname << " Ref: " << ichaRef << " (" << ichaRefDiv << ")" << endl;
156  assert(false);
157  }
158  }
159  }
160 
161  cout << myname << line << endl;
162  cout << myname << "Done." << endl;
163  return 0;
164 }
165 
166 //**********************************************************************
167 
168 int main(int argc, char* argv[]) {
169  bool useExistingFcl = false;
170  Index nshow = 64; // Every nshow'th value is displayed in log.
171  if ( argc > 1 ) {
172  string sarg(argv[1]);
173  if ( sarg == "-h" ) {
174  cout << "Usage: " << argv[0] << " [keepFCL] [NSHOW]" << endl;
175  cout << " keepFCL [false]: If true, existing FCL file is used." << endl;
176  cout << " NSHOW [64]: Every nshow'th channels will be displayed in log." << endl;
177  return 0;
178  }
179  useExistingFcl = sarg == "true" || sarg == "1";
180  }
181  if ( argc > 2 ) {
182  string sarg(argv[2]);
183  nshow = std::stoi(sarg);
184  }
185  return test_PdspOnlineChannel(useExistingFcl, nshow);
186 }
187 
188 //**********************************************************************
std::vector< Index > IndexVector
Index badIndex
const std::vector< std::string > & toolNames() const
std::string string
Definition: nybbler.cc:12
void print() const
unsigned int Index
static void load_services(std::string const &config)
tm
Definition: demo.py:21
int main(int argc, char *argv[])
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)
int test_PdspOnlineChannel(bool useExistingFcl=false, Index nshow=64)
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)