test_FclIndexRangeTool.cxx
Go to the documentation of this file.
1 // test_FclIndexRangeTool.cxx
2 //
3 // David Adams
4 // April 2017
5 //
6 // Test FclIndexRangeTool.
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 int test_FclIndexRangeTool(bool useExistingFcl =false, Index runin =0) {
30  const string myname = "test_FclIndexRangeTool: ";
31 #ifdef NDEBUG
32  cout << myname << "NDEBUG must be off." << endl;
33  abort();
34 #endif
35  string line = "-----------------------------";
36 
37  cout << myname << line << endl;
38  string fclfile = "test_FclIndexRangeTool.fcl";
39  if ( ! useExistingFcl ) {
40  cout << myname << "Creating top-level FCL." << endl;
41  ofstream fout(fclfile.c_str());
42  if ( runin == 0 ) {
43  fout << "tools: {" << endl;
44  fout << " mytool: {" << endl;
45  fout << " tool_type: FclIndexRangeTool" << endl;
46  fout << " LogLevel: 2" << endl;
47  fout << " range1: { labels:[\"Range 1\"] begin:10 end:20}" << endl;
48  fout << " range2: { labels:[\"Range 2\"] begin:20 end:30}" << endl;
49  fout << " }" << endl;
50  fout << "}" << endl;
51  } else {
52  fout << "#include \"dunecommon_tools.fcl\"" << endl;
53  fout << "tools.mytool: @local::tools.protoduneIndexRangeTool" << endl;
54  }
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 irt = tm.getPrivate<IndexRangeTool>("mytool");
71  assert( irt != nullptr );
72 
73  cout << myname << line << endl;
74  cout << "Fetch range1." << endl;
75  IndexRange ir1 = irt->get("range1");
76  cout << ir1.rangeString() << endl;
77  assert( ir1.isValid() );
78  assert( ir1.name == "range1" );
79  assert( ir1.label() == "Range 1" );
80  assert( ir1.begin == 10 );
81  assert( ir1.end == 20 );
82 
83  cout << myname << line << endl;
84  cout << "Fetch range2." << endl;
85  IndexRange ir2 = irt->get("range2");
86  cout << ir2.rangeString() << endl;
87  assert( ir2.isValid() );
88  assert( ir2.name == "range2" );
89  assert( ir2.label() == "Range 2" );
90  assert( ir2.begin == 20 );
91  assert( ir2.end == 30 );
92 
93  cout << myname << line << endl;
94  cout << "Fetch bad range" << endl;
95  IndexRange irb = irt->get("rangebad");
96  cout << irb.rangeString() << endl;
97  assert( ! irb.isValid() );
98 
99  cout << myname << line << endl;
100  cout << myname << "Done." << endl;
101  return 0;
102 }
103 
104 //**********************************************************************
105 
106 int main(int argc, char* argv[]) {
107  bool useExistingFcl = false;
108  Index run = 0;
109  if ( argc > 1 ) {
110  string sarg(argv[1]);
111  if ( sarg == "-h" ) {
112  cout << "Usage: " << argv[0] << " [keepFCL] [RUN]" << endl;
113  cout << " If keepFCL = true, existing FCL file is used." << endl;
114  cout << " If RUN is nonzero, the data for that run are displayed." << endl;
115  return 0;
116  }
117  useExistingFcl = sarg == "true" || sarg == "1";
118  }
119  if ( argc > 2 ) {
120  string sarg(argv[2]);
121  istringstream ssarg(argv[2]);
122  ssarg >> run;
123  }
124  return test_FclIndexRangeTool(useExistingFcl, run);
125 }
126 
127 //**********************************************************************
int test_FclIndexRangeTool(bool useExistingFcl=false, Index runin=0)
const std::vector< std::string > & toolNames() const
std::string string
Definition: nybbler.cc:12
Index begin
Definition: IndexRange.h:34
void print() const
unsigned int Index
bool isValid() const
Definition: IndexRange.h:94
Index end
Definition: IndexRange.h:35
tm
Definition: demo.py:21
int main(int argc, char *argv[])
Name name
Definition: IndexRange.h:32
Name label(Index ilab=0) const
Definition: IndexRange.h:106
std::unique_ptr< T > getPrivate(std::string name)
void line(double t, double *p, double &x, double &y, double &z)
std::string rangeString() const
Definition: IndexRange.h:97
static DuneToolManager * instance(std::string fclname="", int dbg=1)
QTextStream & endl(QTextStream &s)