Skip to content

Commit a4305a7

Browse files
committed
ROOT IO for ConfigurableParameters
Adding possibility to stream ConfigurableParameters and deserialize therefrom. This enables writing/retrieving the Parameters from/to CCDB.
1 parent d170c48 commit a4305a7

File tree

6 files changed

+72
-6
lines changed

6 files changed

+72
-6
lines changed

Common/SimConfig/include/SimConfig/ConfigurableParam.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
#include <boost/property_tree/ptree.hpp>
1919
#include <typeinfo>
2020

21+
class TFile;
22+
class TRootIOCtor;
23+
2124
namespace o2
2225
{
2326
namespace conf
@@ -77,7 +80,7 @@ class ConfigurableParam
7780
{
7881
public:
7982
//
80-
virtual std::string getName() = 0; // print the name of the configurable Parameter
83+
virtual std::string getName() const = 0; // print the name of the configurable Parameter
8184

8285
// print the current keys and values to screen
8386
virtual void printKeyValues() = 0;
@@ -120,6 +123,11 @@ class ConfigurableParam
120123

121124
static void initialize();
122125

126+
static void toCCDB(std::string filename);
127+
static void fromCCDB(std::string filename);
128+
virtual void serializeTo(TFile*) const = 0;
129+
virtual void initFrom(TFile*) = 0;
130+
123131
// allows to provide a file from which to update
124132
// (certain) key-values
125133
// propagates changes down to each registered configuration
@@ -149,19 +157,24 @@ class ConfigurableParam
149157
// (internal use to easily sync updates, this is ok since parameter classes are singletons)
150158
static std::map<std::string, std::pair<int, void*>>* sKeyToStorageMap;
151159

160+
void setRegisterMode(bool b) { sRegisterMode = b; }
161+
152162
private:
153163
// static registry for implementations of this type
154164
static std::vector<ConfigurableParam*>* sRegisteredParamClasses; //!
155165
// static property tree (stocking all key - value pairs from instances of type ConfigurableParam)
156166
static boost::property_tree::ptree* sPtree; //!
157167
static bool sIsFullyInitialized; //!
168+
static bool sRegisterMode; //! (flag to enable/disable autoregistering of child classes)
158169
};
159170

160171
} // end namespace conf
161172
} // end namespace o2
162173

163174
// a helper macro for boilerplate code in parameter classes
164175
#define O2ParamDef(classname, key) \
176+
public: \
177+
classname(TRootIOCtor *) {} \
165178
private: \
166179
static constexpr char const* const sKey = key; \
167180
static classname sInstance; \

Common/SimConfig/include/SimConfig/ConfigurableParamHelper.h

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "TClass.h"
1818
#include <type_traits>
1919
#include <typeinfo>
20+
#include "TFile.h"
2021

2122
namespace o2
2223
{
@@ -50,7 +51,7 @@ class ConfigurableParamHelper : virtual public ConfigurableParam
5051
return P::sInstance;
5152
}
5253

53-
std::string getName() final
54+
std::string getName() const final
5455
{
5556
return P::sKey;
5657
}
@@ -82,6 +83,27 @@ class ConfigurableParamHelper : virtual public ConfigurableParam
8283
}
8384
_ParamHelper::fillKeyValuesImpl(getName(), cl, (void*)this, tree, sKeyToStorageMap);
8485
}
86+
87+
void initFrom(TFile* file) final
88+
{
89+
// switch off auto registering since the readback object is
90+
// only a "temporary" singleton
91+
setRegisterMode(false);
92+
P* readback = nullptr;
93+
file->GetObject(getName().c_str(), readback);
94+
if (readback != nullptr) {
95+
// ATTENTION: override existing singleton object
96+
// It would be better to have a way of reading INTO an existing object
97+
std::memcpy((void*)this, readback, sizeof(P));
98+
delete readback;
99+
}
100+
setRegisterMode(true);
101+
}
102+
103+
void serializeTo(TFile* file) const final
104+
{
105+
file->WriteObjectAny((void*)this, TClass::GetClass(typeid(P)), getName().c_str());
106+
}
85107
};
86108
} // namespace conf
87109
} // namespace o2

Common/SimConfig/src/ConfigurableParam.cxx

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <typeinfo>
2222
#include <cassert>
2323
#include "TDataType.h"
24+
#include "TFile.h"
2425

2526
namespace o2
2627
{
@@ -31,6 +32,7 @@ std::vector<ConfigurableParam*>* ConfigurableParam::sRegisteredParamClasses = nu
3132
boost::property_tree::ptree* ConfigurableParam::sPtree = nullptr;
3233
std::map<std::string, std::pair<int, void*>>* ConfigurableParam::sKeyToStorageMap = nullptr;
3334
bool ConfigurableParam::sIsFullyInitialized = false;
35+
bool ConfigurableParam::sRegisterMode = true;
3436

3537
void ConfigurableParam::writeINI(std::string filename)
3638
{
@@ -54,9 +56,31 @@ void ConfigurableParam::updatePropertyTree()
5456

5557
void ConfigurableParam::printAllKeyValuePairs()
5658
{
59+
std::cout << "####\n";
5760
for (auto p : *sRegisteredParamClasses) {
5861
p->printKeyValues();
5962
}
63+
std::cout << "----\n";
64+
}
65+
66+
// evidently this could be a local file or an OCDB server
67+
// ... we need to generalize this ... but ok for demonstration purpose
68+
void ConfigurableParam::toCCDB(std::string filename)
69+
{
70+
TFile file(filename.c_str(), "RECREATE");
71+
for (auto p : *sRegisteredParamClasses) {
72+
p->serializeTo(&file);
73+
}
74+
file.Close();
75+
}
76+
77+
void ConfigurableParam::fromCCDB(std::string filename)
78+
{
79+
TFile file(filename.c_str(), "READ");
80+
for (auto p : *sRegisteredParamClasses) {
81+
p->initFrom(&file);
82+
}
83+
file.Close();
6084
}
6185

6286
ConfigurableParam::ConfigurableParam()
@@ -70,7 +94,9 @@ ConfigurableParam::ConfigurableParam()
7094
if (sKeyToStorageMap == nullptr) {
7195
sKeyToStorageMap = new std::map<std::string, std::pair<int, void*>>;
7296
}
73-
sRegisteredParamClasses->push_back(this);
97+
if (sRegisterMode == true) {
98+
sRegisteredParamClasses->push_back(this);
99+
}
74100
}
75101

76102
void ConfigurableParam::initialize()

Common/SimConfig/src/ConfigurableParamHelper.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ void _ParamHelper::printParametersImpl(TClass* cl, void* obj)
7474
// pointer to object
7575
auto dt = dm->GetDataType();
7676
char* pointer = ((char*)obj) + dm->GetOffset() + index * dt->Size();
77-
std::cout << getName(dm, index, size) << " : " << dt->AsString(pointer);
77+
std::cout << getName(dm, index, size) << " : " << dt->AsString(pointer) << "\n";
7878
};
7979
loopOverMembers(cl, obj, printMembers);
8080
}

Detectors/Passive/src/PassiveLinkDef.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
#pragma link C++ class o2::passive::FrameStructure+;
3737
#pragma link C++ class o2::passive::Shil+;
3838
#pragma link C++ class o2::passive::Hall+;
39+
#pragma link C++ class o2::conf::ConfigurableParam+;
3940
#pragma link C++ class o2::passive::HallSimParam+;
40-
41+
#pragma link C++ class o2::conf::ConfigurableParamHelper<o2::passive::HallSimParam>+;
4142
#endif
4243

macro/o2sim.C

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,17 @@
2929
FairRunSim* o2sim_init(bool asservice)
3030
{
3131
auto& confref = o2::conf::SimConfig::Instance();
32-
// update the parameters from local configuration file (TODO)
32+
// we can read from CCDB (for the moment faking with a TFile)
33+
// o2::conf::ConfigurableParam::fromCCDB("params_ccdb.root", runid);
3334

3435
// update the parameters from stuff given at command line
3536
o2::conf::ConfigurableParam::updateFromString(confref.getKeyValueString());
3637
// write the configuration file
3738
o2::conf::ConfigurableParam::writeINI("o2sim_configuration.ini");
3839

40+
// we can update the binary CCDB entry something like this ( + timestamp key )
41+
// o2::conf::ConfigurableParam::toCCDB("params_ccdb.root");
42+
3943
auto genconfig = confref.getGenerator();
4044

4145
FairRunSim* run;

0 commit comments

Comments
 (0)