MIDAS
Loading...
Searching...
No Matches
odbxx_test.cxx
Go to the documentation of this file.
1/********************************************************************\
2
3 Name: odbxx_test.cxx
4 Created by: Stefan Ritt
5
6 Contents: Test and Demo of Object oriented interface to ODB
7
8\********************************************************************/
9
10#include <string>
11#include <iostream>
12#include <array>
13#include <functional>
14
15#include "midas.h"
16#include "odbxx.h"
17
18/*------------------------------------------------------------------*/
19
20int main() {
21
24
25 // delete /Test/Settings to start from scratch
26 midas::odb::delete_key("/Test/Settings");
27
28 // create ODB structure...
29 midas::odb o = {
30 {"Int32 Key", 42},
31 {"Bool Key", true},
32 {"Subdir", {
33 {"Int32 key", 123 },
34 {"Double Key", 1.2},
35 {"Subsub", {
36 {"Float key", 1.2f}, // floats must be explicitly specified
37 {"String Key", "Hello"},
38 }}
39 }},
40 {"Int Array", {1, 2, 3}},
41 {"Double Array", {1.2, 2.3, 3.4}},
42 {"String Array", {"Hello1", "Hello2", "Hello3"}},
43 {"Large Array", std::array<int, 10>{} }, // array with explicit size
44 {"Large String", std::string(63, '\0') }, // string with explicit size
45 {"String Array 10", std::array<std::string, 10>{}}, // string array with explicit size
46 // string array with 10 strings of each 63 chars
47 {"Large String Array 10", std::array<std::string, 10>{std::string(63, '\0')}}
48 };
49
50 // ...and push it to ODB. If keys are present in the
51 // ODB, their value is kept. If not, the default values
52 // from above are copied to the ODB
53 o.connect("/Test/Settings");
54
55 // alternatively, a structure can be created from an existing ODB subtree
56 midas::odb o2("/Test/Settings");
57 std::cout << o2 << std::endl;
58
59 // set, retrieve, and change ODB value
60 o["Int32 Key"] = 42;
61 int i = o["Int32 Key"];
62 o["Int32 Key"] = i+1;
63 o["Int32 Key"]++;
64 o["Int32 Key"] *= 1.3;
65 std::cout << "Should be 57: " << o["Int32 Key"] << std::endl;
66
67 // test with 64-bit variables
68 o["Int64 Key"] = -1LL;
69 int64_t i64 = o["Int64 Key"];
70 std::cout << std::hex << "0x" << i64 << std::dec << std::endl;
71
72 o["UInt64 Key"] = 0x1234567812345678u;
73 uint64_t ui64 = o["UInt64 Key"];
74 std::cout << std::hex << "0x" << ui64 << std::dec << std::endl;
75
76 // test with bool
77 o["Bool Key"] = false;
78 o["Bool Key"] = !o["Bool Key"];
79
80 // test with std::string
81 o["Subdir"]["Subsub"]["String Key"] = "Hello";
82 std::string s = o["Subdir"]["Subsub"]["String Key"];
83 s += " world!";
84 o["Subdir"]["Subsub"]["String Key"] = s;
85 s = s + o["Subdir"]["Subsub"]["String Key"].s(); // need .s() for concatenations
86
87 // use std::string as index
88 std::string substr("Int32 Key");
89 o[substr] = 43;
90
91 // test with a vector
92 std::vector<int> v = o["Int Array"]; // read vector
93 std::fill(v.begin(), v.end(), 10);
94 o["Int Array"] = v; // assign vector to ODB array
95 o["Int Array"][1] = 2; // modify array element
96 i = o["Int Array"][1]; // read from array element
97 o["Int Array"].resize(5); // resize array
98 o["Int Array"]++; // increment all values of array
99 std::cout << "Arrays size is " << o["Int Array"].size() << std::endl;
100
101 // auto-enlarge arrays
102 o["Int Array"][10] = 10;
103
104 // test with a string vector
105 std::vector<std::string> sv;
106 sv = o["String Array"];
107 sv[1] = "New String";
108 o["String Array"] = sv;
109 o["String Array"][2] = "Another String";
110 o["String Array"][3] = std::string("One more");
111 s = o["String Array"][1].s(); // need .s() to explicitly convert to std::string
112
113 // test with strings with given size
114 o["String Array 2"][0].set_string_size("Hello", 64);
115 o["String Array 2"][1] = "Second string";
116 o["String Array 2"][2] = "Third string";
117
118 // test with bool arrays/vectors
119 o["Bool Array"] = std::array<bool, 3>{true, false, true};
120 o["Bool Array from Vector"] = std::vector<bool>{true, false, true};
121
122 // iterate over array
123 int sum = 0;
124 for (int e : o["Int Array"])
125 sum += e;
126 std::cout << "Sum should be 37: " << sum << std::endl;
127
128 // create key from other key
129 midas::odb oi(o["Int32 Key"]);
130 oi = 123;
131
132 // test auto refresh read
133 std::cout << oi << std::endl; // each read access reads value from ODB
134 oi.set_auto_refresh_read(false); // turn off auto refresh
135 std::cout << oi << std::endl; // this does not read value from ODB
136 oi.read(); // this forces a manual read
137 std::cout << oi << std::endl;
138
139 // test auto refresh write
140 oi.set_auto_refresh_write(false); // turn off auto refresh write
141 oi = 321; // this does not write a value to the ODB
142 oi.write(); // this forces a manual write
143
144 // create ODB entries on-the-fly
146 ot.connect("/Test/Settings/OTF");// this forces /Test/OTF to be created if not already there
147 ot["Int32 Key"] = 1; // create all these keys with different types
148 ot["Double Key"] = 1.23;
149 ot["String Key"] = "Hello";
150 ot["Int Array"] = std::array<int, 10>{};
151 ot["Subdir"]["Int32 Key"] = 42;
152 ot["String Array"] = std::vector<std::string>{"S1", "S2", "S3"};
153 ot["Other String Array"][0] = "OSA0";
154 ot["Other String Array"][1] = "OSA1";
155
156 // create key with default value
157 i = ot["Int32 Key"](123); // key exists already (created above) -> use key value i=1
158 i = ot["New Int32 Key"](123); // key does not exist -> set it to default value 123 i=123
159 // std::string s1 = ot["New String Key"]("Hi"); // same for strings
160 std::cout << ot << std::endl;
161
162 o.read(); // re-read the underlying ODB tree which got changed by above OTF code
163 std::cout << o.print() << std::endl;
164
165 // iterate over sub-keys
167 for (midas::odb& oit : o)
168 std::cout << oit.get_name() << ": " << oit << std::endl;
169
170 // print whole sub-tree
171 std::cout << o.print() << std::endl;
172
173 // print whole subtree
174 std::cout << o.dump() << std::endl;
175
176 // update structure - create keys if needed, keep existing values if key already exists,
177 // delete keys that are in ODB but not the list of defaults.
178 midas::odb o3 = {
179 {"Int32 Key", 456},
180 {"New Bool Key", true},
181 {"String Array", {"Hello1", "Hello2", "Hello3"}},
182 {"Bool Key", true},
183 {"Subdir", {
184 {"Int32 key", 135 },
185 {"New Sub Bool Key", false},
186 {"Double Key", 1.5}
187 }}
188 };
189 o3.connect_and_fix_structure("/Test/Settings");
190
191 // Print new structure
192 std::cout << "After changing structure with o3:" << std::endl;
193 std::cout << o3.print() << std::endl;
194
195 // delete test key from ODB
196 o.delete_key();
197
198 // don't clutter watch callbacks
200
201 // watch ODB key for any change with lambda function
202 midas::odb ow("/Experiment");
203 ow.watch([](midas::odb &o) {
204 std::cout << "Value of key \"" + o.get_full_path() + "\" changed to " << o << std::endl;
205 });
206
207 do {
208 int status = cm_yield(100);
209 if (status == SS_ABORT || status == RPC_SHUTDOWN)
210 break;
211 } while (!ss_kbhit());
212
214 return 1;
215}
void delete_key()
Definition odbxx.cxx:1384
static void set_debug(bool flag)
Definition odbxx.h:1239
void connect_and_fix_structure(std::string path)
Definition odbxx.cxx:1370
void connect(const std::string &path, const std::string &name, bool write_defaults, bool delete_keys_not_in_defaults=false)
Definition odbxx.cxx:1291
int main()
INT cm_yield(INT millisec)
Definition midas.cxx:5642
INT cm_connect_experiment(const char *host_name, const char *exp_name, const char *client_name, void(*func)(char *))
Definition midas.cxx:2278
INT cm_disconnect_experiment(void)
Definition midas.cxx:2846
#define SS_ABORT
Definition midas.h:677
#define RPC_SHUTDOWN
Definition midas.h:707
BOOL ss_kbhit()
Definition system.cxx:3664
INT i
Definition mdump.cxx:32
DWORD status
Definition odbhist.cxx:39
TH1X EXPRT * h1_book(const char *name, const char *title, int bins, double min, double max)
Definition rmidas.h:24
static double e(void)
Definition tinyexpr.c:136