forked from AliceO2Group/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigurableParamHelper.cxx
More file actions
480 lines (419 loc) · 15.5 KB
/
ConfigurableParamHelper.cxx
File metadata and controls
480 lines (419 loc) · 15.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
//first version 8/2018, Sandro Wenzel
#include "CommonUtils/ConfigurableParamHelper.h"
#include "CommonUtils/ConfigurableParam.h"
#include <TClass.h>
#include <TDataMember.h>
#include <TDataType.h>
#include <TEnum.h>
#include <TEnumConstant.h>
#include <TIterator.h>
#include <TList.h>
#include <iostream>
#include <sstream>
#include <fairlogger/Logger.h>
#include <boost/property_tree/ptree.hpp>
#include <boost/functional/hash.hpp>
#include <functional>
#include <format>
#ifdef NDEBUG
#undef NDEBUG
#endif
#include <cassert>
using namespace o2::conf;
// ----------------------------------------------------------------------
std::string ParamDataMember::toString(std::string const& prefix, bool showProv, size_t padding) const
{
const std::string label = prefix + "." + name + " : " + value;
std::ostringstream out;
out << label;
if (showProv) {
std::string prov = (provenance.compare("") == 0 ? "<null>" : provenance);
if (padding) {
size_t len = label.size() - prefix.size() - 5; // 4 four the extra chars + 1 for the maxpad
if (len < padding) {
out << std::string(padding - len, ' ');
} else {
out << ' ';
}
out << "[ " + prov + " ]";
} else {
out << "\t\t[ " + prov + " ]";
}
}
return out.str();
}
std::ostream& operator<<(std::ostream& out, const ParamDataMember& pdm)
{
out << pdm.toString("", false) << "\n";
return out;
}
// ----------------------------------------------------------------------
bool isString(TDataMember const& dm)
{
return strcmp(dm.GetTrueTypeName(), "string") == 0;
}
// ----------------------------------------------------------------------
// a generic looper of data members of a TClass; calling a callback
// reused in various functions below
void loopOverMembers(TClass* cl, void* obj,
std::function<void(const TDataMember*, int, int)>&& callback)
{
auto memberlist = cl->GetListOfDataMembers();
for (int i = 0; i < memberlist->GetEntries(); ++i) {
auto dm = (TDataMember*)memberlist->At(i);
auto isValidComplex = [dm]() {
return isString(*dm) || dm->IsEnum();
};
// filter out static members for now
if (dm->Property() & kIsStatic) {
continue;
}
if (dm->IsaPointer()) {
LOG(warning) << "Pointer types not supported in ConfigurableParams: " << dm->GetFullTypeName() << " " << dm->GetName();
continue;
}
if (!dm->IsBasic() && !isValidComplex()) {
LOG(warning) << "Generic complex types not supported in ConfigurableParams: " << dm->GetFullTypeName() << " " << dm->GetName();
continue;
}
const auto dim = dm->GetArrayDim();
// we support very simple vectored data in 1D for now
if (dim > 1) {
LOG(warning) << "We support at most 1 dimensional arrays in ConfigurableParams: " << dm->GetFullTypeName() << " " << dm->GetName();
continue;
}
const auto size = (dim == 1) ? dm->GetMaxIndex(dim - 1) : 1; // size of array (1 if scalar)
for (int index = 0; index < size; ++index) {
callback(dm, index, size);
}
}
}
// ----------------------------------------------------------------------
// construct name (in dependence on vector or scalar data and index)
std::string getName(const TDataMember* dm, int index, int size)
{
std::stringstream namestream;
namestream << dm->GetName();
if (size > 1) {
namestream << "[" << index << "]";
}
return namestream.str();
}
// ----------------------------------------------------------------------
size_t getSizeOfUnderlyingType(const TDataMember& dm)
{
auto dt = dm.GetDataType();
if (dt) {
// if basic built-in type supported by ROOT
return dt->Size();
} else {
// for now only catch std::string as other supported type
auto tname = dm.GetFullTypeName();
if (strcmp(tname, "string") == 0 || strcmp(tname, "std::string")) {
return sizeof(std::string);
}
LOG(error) << "ENCOUNTERED AN UNSUPPORTED TYPE " << tname << "IN A CONFIGURABLE PARAMETER";
}
return 0;
}
// ----------------------------------------------------------------------
std::string asString(TDataMember const& dm, char* pointer)
{
// first check if this is a basic data type, in which case
// we let ROOT do the work
if (auto dt = dm.GetDataType()) {
// we put the numeric interpration for char / unsigned char
// instead of the string one
if (dt->GetType() == EDataType::kChar_t) {
auto c = (char)(*pointer);
return std::to_string((int)c).c_str();
} else if (dt->GetType() == EDataType::kUChar_t) {
auto u = (unsigned char)(*pointer);
return std::to_string((unsigned int)u).c_str();
}
auto val = dt->AsString(pointer);
// For enums we grab the string value of the member
// and use that instead if its int value
if (dm.IsEnum()) {
const auto enumtype = TEnum::GetEnum(dm.GetTypeName());
assert(enumtype != nullptr);
const auto constantlist = enumtype->GetConstants();
assert(constantlist != nullptr);
if (enumtype) {
for (int i = 0; i < constantlist->GetEntries(); ++i) {
const auto e = (TEnumConstant*)(constantlist->At(i));
if (val == std::to_string((int)e->GetValue())) {
return std::string(e->GetName());
}
}
}
}
return std::string(val);
}
// if data member is a std::string just return
else if (isString(dm)) {
return ((std::string*)pointer)->c_str();
}
// potentially other cases to be added here
LOG(error) << "COULD NOT REPRESENT AS STRING";
return std::string();
}
// ----------------------------------------------------------------------
std::vector<ParamDataMember>* _ParamHelper::getDataMembersImpl(std::string const& mainkey, TClass* cl, void* obj,
std::map<std::string, ConfigurableParam::EParamProvenance> const* provmap, size_t globaloffset)
{
std::vector<ParamDataMember>* members = new std::vector<ParamDataMember>;
auto toDataMember = [&members, obj, mainkey, provmap, globaloffset](const TDataMember* dm, int index, int size) {
auto TS = getSizeOfUnderlyingType(*dm);
char* pointer = ((char*)obj) + dm->GetOffset() + index * TS + globaloffset;
const std::string name = getName(dm, index, size);
auto value = asString(*dm, pointer);
std::string prov = "";
auto iter = provmap->find(mainkey + "." + name);
if (iter != provmap->end()) {
prov = ConfigurableParam::toString(iter->second);
}
ParamDataMember member{name, value, prov};
members->push_back(member);
};
loopOverMembers(cl, obj, toDataMember);
return members;
}
// ----------------------------------------------------------------------
// a function converting a string representing a type to the type_info
// because unfortunately typeid(double) != typeid("double")
// but we can take the TDataType (if it exists) as a hint in order to
// minimize string comparisons
std::type_info const& nameToTypeInfo(const char* tname, TDataType const* dt)
{
if (dt) {
switch (dt->GetType()) {
case kChar_t: {
return typeid(char);
}
case kUChar_t: {
return typeid(unsigned char);
}
case kShort_t: {
return typeid(short);
}
case kUShort_t: {
return typeid(unsigned short);
}
case kInt_t: {
return typeid(int);
}
case kUInt_t: {
return typeid(unsigned int);
}
case kLong_t: {
return typeid(long);
}
case kULong_t: {
return typeid(unsigned long);
}
case kFloat_t: {
return typeid(float);
}
case kDouble_t: {
return typeid(double);
}
case kDouble32_t: {
return typeid(double);
}
case kBool_t: {
return typeid(bool);
}
case kLong64_t: {
return typeid(long long);
}
case kULong64_t: {
return typeid(unsigned long long);
}
default: {
break;
}
}
}
// if we get here none of the above worked
if (strcmp(tname, "string") == 0 || strcmp(tname, "std::string")) {
return typeid(std::string);
}
LOG(error) << "ENCOUNTERED AN UNSUPPORTED TYPE " << tname << "IN A CONFIGURABLE PARAMETER";
return typeid("ERROR");
}
// ----------------------------------------------------------------------
void _ParamHelper::fillKeyValuesImpl(std::string const& mainkey, TClass* cl, void* obj, boost::property_tree::ptree* tree,
std::map<std::string, std::pair<std::type_info const&, void*>>* keytostoragemap,
EnumRegistry* enumRegistry, size_t globaloffset)
{
boost::property_tree::ptree localtree;
auto fillMap = [obj, &mainkey, &localtree, &keytostoragemap, &enumRegistry, globaloffset](const TDataMember* dm, int index, int size) {
const auto name = getName(dm, index, size);
auto dt = dm->GetDataType();
auto TS = getSizeOfUnderlyingType(*dm);
char* pointer = ((char*)obj) + dm->GetOffset() + index * TS + globaloffset;
localtree.put(name, asString(*dm, pointer));
auto key = mainkey + "." + name;
// If it's an enum, we need to store separately all the legal
// values so that we can map to them from the command line
if (dm->IsEnum()) {
enumRegistry->add(key, dm);
}
using mapped_t = std::pair<std::type_info const&, void*>;
auto& ti = nameToTypeInfo(dm->GetTrueTypeName(), dt);
keytostoragemap->insert(std::pair<std::string, mapped_t>(key, mapped_t(ti, pointer)));
};
loopOverMembers(cl, obj, fillMap);
tree->add_child(mainkey, localtree);
}
// ----------------------------------------------------------------------
void _ParamHelper::printMembersImpl(std::string const& mainkey, std::vector<ParamDataMember> const* members, bool showProv, bool useLogger, bool withPadding, bool showHash)
{
_ParamHelper::outputMembersImpl(std::cout, mainkey, members, showProv, useLogger, withPadding, showHash);
}
void _ParamHelper::outputMembersImpl(std::ostream& out, std::string const& mainkey, std::vector<ParamDataMember> const* members, bool showProv, bool useLogger, bool withPadding, bool showHash)
{
if (members == nullptr) {
return;
}
size_t maxpad{0};
if (withPadding) {
for (auto& member : *members) {
maxpad = std::max(maxpad, member.name.size() + member.value.size());
}
}
if (showHash) {
std::string shash = std::format("{:07x}", getHashImpl(mainkey, members));
shash = shash.substr(0, 7);
if (useLogger) {
LOG(info) << mainkey << " [Hash#" << shash << "]";
} else {
out << mainkey << " [Hash#" << shash << "]\n";
}
}
for (auto& member : *members) {
if (useLogger) {
LOG(info) << member.toString(mainkey, showProv, maxpad);
} else {
out << member.toString(mainkey, showProv, maxpad) << "\n";
}
}
}
size_t _ParamHelper::getHashImpl(std::string const& mainkey, std::vector<ParamDataMember> const* members)
{
size_t hash = 0;
boost::hash_combine(hash, mainkey);
for (auto& member : *members) {
boost::hash_combine(hash, member.value);
}
return hash;
}
// ----------------------------------------------------------------------
bool isMemblockDifferent(char const* block1, char const* block2, int sizeinbytes)
{
// loop over thing in elements of bytes
for (int i = 0; i < sizeinbytes / sizeof(char); ++i) {
if (block1[i] != block2[i]) {
return false;
}
}
return true;
}
// ----------------------------------------------------------------------
void _ParamHelper::assignmentImpl(std::string const& mainkey, TClass* cl, void* to, void* from,
std::map<std::string, ConfigurableParam::EParamProvenance>* provmap, size_t globaloffset)
{
auto assignifchanged = [to, from, &mainkey, provmap, globaloffset](const TDataMember* dm, int index, int size) {
const auto name = getName(dm, index, size);
auto dt = dm->GetDataType();
auto TS = getSizeOfUnderlyingType(*dm);
char* pointerto = ((char*)to) + dm->GetOffset() + index * TS + globaloffset;
char* pointerfrom = ((char*)from) + dm->GetOffset() + index * TS + globaloffset;
// lambda to update the provenance
auto updateProv = [&mainkey, name, provmap]() {
auto key = mainkey + "." + name;
auto iter = provmap->find(key);
if (iter != provmap->end()) {
iter->second = ConfigurableParam::EParamProvenance::kCCDB; // TODO: change to "current STATE"??
} else {
LOG(warn) << "KEY " << key << " NOT FOUND WHILE UPDATING PARAMETER PROVENANCE";
}
};
// TODO: this could dispatch to the same method used in ConfigurableParam::setValue
// but will be slower
// test if a complicated case
if (isString(*dm)) {
std::string& target = *(std::string*)pointerto;
std::string const& origin = *(std::string*)pointerfrom;
if (target.compare(origin) != 0) {
updateProv();
target = origin;
}
return;
}
//
if (!isMemblockDifferent(pointerto, pointerfrom, TS)) {
updateProv();
// actually copy
std::memcpy(pointerto, pointerfrom, getSizeOfUnderlyingType(*dm));
}
};
loopOverMembers(cl, to, assignifchanged);
}
// ----------------------------------------------------------------------
void _ParamHelper::syncCCDBandRegistry(const std::string& mainkey, TClass* cl, void* to, void* from,
std::map<std::string, ConfigurableParam::EParamProvenance>* provmap, size_t globaloffset)
{
auto sync = [to, from, &mainkey, provmap, globaloffset](const TDataMember* dm, int index, int size) {
const auto name = getName(dm, index, size);
auto dt = dm->GetDataType();
auto TS = getSizeOfUnderlyingType(*dm);
char* pointerto = ((char*)to) + dm->GetOffset() + index * TS + globaloffset;
char* pointerfrom = ((char*)from) + dm->GetOffset() + index * TS + globaloffset;
// check current provenance
auto key = mainkey + "." + name;
auto proviter = provmap->find(key);
bool isRT = proviter != provmap->end() && proviter->second == ConfigurableParam::EParamProvenance::kRT;
if (isRT) {
return;
}
// lambda to update the provenance
auto updateProv = [&proviter]() {
proviter->second = ConfigurableParam::EParamProvenance::kCCDB;
};
// test if a complicated case
if (isString(*dm)) {
std::string& target = *(std::string*)pointerto;
std::string const& origin = *(std::string*)pointerfrom;
// if (target.compare(origin) != 0) {
updateProv();
target = origin;
// }
return;
}
//
// if (!isMemblockDifferent(pointerto, pointerfrom, TS)) {
updateProv();
// actually copy
std::memcpy(pointerto, pointerfrom, getSizeOfUnderlyingType(*dm));
// }
};
loopOverMembers(cl, to, sync);
}
// ----------------------------------------------------------------------
void _ParamHelper::printWarning(std::type_info const& tinfo)
{
LOG(warning) << "Registered parameter class with name " << tinfo.name()
<< " has no ROOT dictionary and will not be available in the configurable parameter system";
}