test_DataMap.cxx
Go to the documentation of this file.
1 // test_DataMap.cxx
2 //
3 // David Adams
4 // September 2017
5 //
6 // Test DataMap.
7 
8 #include <string>
9 #include <iostream>
10 #include <fstream>
12 
13 #undef NDEBUG
14 #include <cassert>
15 
16 using std::string;
17 using std::cout;
18 using std::endl;
19 
20 //**********************************************************************
21 
22 int test_DataMap(bool useExistingFcl =false) {
23  const string myname = "test_DataMap: ";
24 #ifdef NDEBUG
25  cout << myname << "NDEBUG must be off." << endl;
26  abort();
27 #endif
28  string line = "-----------------------------";
29 
30  cout << myname << line << endl;
31  cout << myname << "Creating failed result." << endl;
32  DataMap resf(3);
33  assert( resf );
34  assert( resf != 0 );
35  assert( resf == 3 );
36 
37  cout << myname << line << endl;
38  cout << myname << "Creating success data." << endl;
39  DataMap res;
40  assert( ! res );
41 
42  cout << myname << line << endl;
43  cout << myname << "Add int." << endl;
44  assert( res.getIntMap().size() == 0 );
45  assert( ! res.haveInt("myint") );
46  res.setInt("myint", 123);
47  assert( res.getIntMap().size() == 1 );
48  assert( ! res.haveInt("nosuch") );
49  assert( res.haveInt("myint") );
50  assert( ! res.haveFloat("myint") );
51  assert( res.getInt("nosuch") == 0 );
52  assert( res.getInt("myint") == 123 );
53  res.setInt("myint", 456);
54  assert( res.getIntMap().size() == 1 );
55  assert( res.getFloatMap().size() == 0 );
56  assert( res.getFloatVectorMap().size() == 0 );
57  assert( res.getHistMap().size() == 0 );
58  assert( res.getInt("myint") == 456 );
59  res.print();
60 
61  cout << myname << line << endl;
62  cout << myname << "Add int vector." << endl;
63  assert( res.getIntVectorMap().size() == 0 );
64  assert( ! res.haveIntVector("myintvec") );
65  DataMap::IntVector myintvec;
66  myintvec.push_back(1);
67  myintvec.push_back(2);
68  myintvec.push_back(3);
69  res.setIntVector("myintvec", myintvec);
70  assert( res.getIntVectorMap().size() == 1 );
71  assert( ! res.haveIntVector("nosuch") );
72  assert( res.haveIntVector("myintvec") );
73  assert( ! res.haveFloat("myint") );
74  res.setInt("myint", 456);
75  assert( res.getIntMap().size() == 1 );
76  assert( res.getFloatMap().size() == 0 );
77  assert( res.getFloatVectorMap().size() == 0 );
78  assert( res.getHistMap().size() == 0 );
79  assert( res.getIntVector("myintvec") == myintvec );
80  res.print();
81 
82  cout << myname << line << endl;
83  cout << myname << "Add float." << endl;
84  assert( res.getFloatMap().size() == 0 );
85  assert( ! res.haveFloat("myflt") );
86  float fltval = 1.23;
87  res.setFloat("myflt", fltval);
88  res.print();
89  assert( res.haveFloat("myflt") );
90  assert( res.getFloat("myflt") == fltval );
91  assert( res.getIntMap().size() == 1 );
92  assert( res.getFloatMap().size() == 1 );
93  assert( res.getFloatVectorMap().size() == 0 );
94  assert( res.getHistMap().size() == 0 );
95  assert( res.getHistVectorMap().size() == 0 );
96 
97  cout << myname << line << endl;
98  cout << myname << line << endl;
99  cout << myname << "Add float vector." << endl;
100  std::vector<float> flts;
101  flts.push_back(1.234);
102  flts.push_back(12.34);
103  flts.push_back(123.4);
104  assert( res.getFloatVectorMap().size() == 0 );
105  assert( ! res.haveFloatVector("myflt") );
106  res.setFloatVector("myfltvec", flts);
107  assert( res.haveFloatVector("myfltvec") );
108  assert( res.getFloatVector("myfltvec") == flts );
109  assert( res.getIntMap().size() == 1 );
110  assert( res.getFloatMap().size() == 1 );
111  assert( res.getFloatVectorMap().size() == 1 );
112  assert( res.getHistMap().size() == 0 );
113  assert( res.getHistVectorMap().size() == 0 );
114  res.print();
115 
116  cout << myname << line << endl;
117  cout << myname << "Add histogram." << endl;
118  TH1* ph = (TH1*) 0xdeadbeef;
119  assert( res.getHistMap().size() == 0 );
120  assert( ! res.haveHist("myhst") );
121  res.setHist("myhst", ph);
122  assert( res.haveHist("myhst") );
123  assert( res.getHist("myhst") == ph );
124  assert( res.getIntMap().size() == 1 );
125  assert( res.getFloatMap().size() == 1 );
126  assert( res.getFloatVectorMap().size() == 1 );
127  assert( res.getHistMap().size() == 1 );
128  assert( res.getHistVectorMap().size() == 0 );
129  res.print();
130 
131  cout << myname << line << endl;
132  cout << myname << "Add histogram vector." << endl;
133  std::vector<TH1*> hsts = { (TH1*) 0xdeadbee1, (TH1*) 0xdeadbee2, (TH1*) 0xdeadbee3 };
134  assert( res.getHistVectorMap().size() == 0 );
135  assert( ! res.haveHistVector("myhstvec") );
136  res.setHistVector("myhstvec", hsts);
137  assert( res.haveHistVector("myhstvec") );
138  assert( res.getHistVector("myhstvec") == hsts );
139  assert( ! res.haveHist("nosuch") );
140  assert( res.getHistVector("nosuch").size() == 0 );
141  assert( res.getIntMap().size() == 1 );
142  assert( res.getFloatMap().size() == 1 );
143  assert( res.getFloatVectorMap().size() == 1 );
144  assert( res.getHistMap().size() == 1 );
145  assert( res.getHistVectorMap().size() == 1 );
146  assert( res.getGraphMap().size() == 0 );
147  res.print();
148 
149  cout << myname << line << endl;
150  cout << myname << "Add graph." << endl;
151  TGraph* pg = new TGraph;
152  assert( ! res.haveGraph("mygrf") );
153  res.setGraph("mygrf", pg);
154  assert( res.haveGraph("mygrf") );
155  assert( res.getGraph("mygrf") == pg );
156  assert( res.getIntMap().size() == 1 );
157  assert( res.getFloatMap().size() == 1 );
158  assert( res.getFloatVectorMap().size() == 1 );
159  assert( res.getHistMap().size() == 1 );
160  assert( res.getHistVectorMap().size() == 1 );
161  assert( res.getGraphMap().size() == 1 );
162  res.print();
163 
164  cout << myname << line << endl;
165  cout << myname << line << endl;
166  cout << myname << "Extend data map." << endl;
167  DataMap res2;
168  TH1* ph2 = (TH1*) 0xdeadbee5;
169  std::vector<TH1*> hsts2 = { (TH1*) 0xdeadbee6, (TH1*) 0xdeadbee7 };
170  res2.setInt("myint", 135);
171  res2.setInt("myint2", 578);
172  float fltval2 = 5.78;
173  res2.setFloat("myflt2", fltval2);
174  res2.setHist("myhst2", ph2);
175  res2.setHistVector("myhstvec2", hsts2);
176  res.extend(res2);
177  assert( res == 0 );
178  assert( res.getIntMap().size() == 2 );
179  assert( res.getFloatMap().size() == 2 );
180  assert( res.getFloatVectorMap().size() == 1 );
181  assert( res.getHistMap().size() == 2 );
182  assert( res.getInt("myint") == 135 );
183  assert( res.getInt("myint2") == 578 );
184  assert( res.getFloat("myflt") == fltval );
185  assert( res.getFloat("myflt2") == fltval2 );
186  assert( res.getHist("myhst") == ph );
187  assert( res.getHist("myhst2") == ph2 );
188  assert( res.getHistVector("myhstvec") == hsts );
189  assert( res.getHistVector("myhstvec2") == hsts2 );
190  res2.print();
191 
192  cout << myname << line << endl;
193  cout << myname << "Done." << endl;
194  return 0;
195 }
196 
197 //**********************************************************************
198 
199 int main(int argc, char* argv[]) {
200  bool useExistingFcl = false;
201  if ( argc > 1 ) {
202  string sarg(argv[1]);
203  if ( sarg == "-h" ) {
204  cout << "Usage: " << argv[0] << " [ARG]" << endl;
205  cout << " If ARG = true, existing FCL file is used." << endl;
206  return 0;
207  }
208  useExistingFcl = sarg == "true" || sarg == "1";
209  }
210  return test_DataMap(useExistingFcl);
211 }
212 
213 //**********************************************************************
void setFloat(Name name, float val)
Definition: DataMap.h:133
const IntVectorMap & getIntVectorMap() const
Definition: DataMap.h:229
std::string string
Definition: nybbler.cc:12
bool haveHist(Name name) const
Definition: DataMap.h:212
bool haveInt(Name name) const
Definition: DataMap.h:207
const IntVector & getIntVector(Name name) const
Definition: DataMap.h:219
TGraph * getGraph(Name name) const
Definition: DataMap.h:225
void setHist(Name name, TH1 *ph, bool own=false)
Definition: DataMap.h:136
const FloatVector & getFloatVector(Name name) const
Definition: DataMap.h:221
int main(int argc, char *argv[])
void print(std::ostream *pout) const
Definition: DataMap.h:245
std::vector< int > IntVector
Definition: DataMap.h:50
const FloatVectorMap & getFloatVectorMap() const
Definition: DataMap.h:231
const HistVector & getHistVector(Name name) const
Definition: DataMap.h:224
void setIntVector(Name name, const IntVector &val)
Definition: DataMap.h:132
void setHistVector(Name name, const HistVector &hsts, bool own=false)
Definition: DataMap.h:150
void extend(const DataMap &rhs)
Definition: DataMap.h:186
const GraphMap & getGraphMap() const
Definition: DataMap.h:235
const IntMap & getIntMap() const
Definition: DataMap.h:228
void setInt(Name name, int val)
Definition: DataMap.h:131
const FloatMap & getFloatMap() const
Definition: DataMap.h:230
Float getFloat(Name name, Float def=0.0) const
Definition: DataMap.h:220
const HistVectorMap & getHistVectorMap() const
Definition: DataMap.h:234
int getInt(Name name, int def=0) const
Definition: DataMap.h:218
int test_DataMap(bool useExistingFcl=false)
bool haveFloat(Name name) const
Definition: DataMap.h:209
void line(double t, double *p, double &x, double &y, double &z)
bool haveGraph(Name name) const
Definition: DataMap.h:214
void setGraph(Name name, TGraph *pg)
Definition: DataMap.h:178
TH1 * getHist(Name name, TH1 *def=nullptr) const
Definition: DataMap.h:223
bool haveHistVector(Name name) const
Definition: DataMap.h:213
void setFloatVector(Name name, const FloatVector &val)
Definition: DataMap.h:134
const HistMap & getHistMap() const
Definition: DataMap.h:233
bool haveFloatVector(Name name) const
Definition: DataMap.h:210
bool haveIntVector(Name name) const
Definition: DataMap.h:208
QTextStream & endl(QTextStream &s)