-
Notifications
You must be signed in to change notification settings - Fork 60
/
SIOBlock.cc
247 lines (212 loc) · 7.26 KB
/
SIOBlock.cc
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
#include "podio/SIOBlock.h"
#include <algorithm>
#include <cstdlib>
#include <dlfcn.h>
#include <map>
#include <sstream>
#ifdef USE_BOOST_FILESYSTEM
#include <boost/filesystem.hpp>
#else
#include <filesystem>
#endif
namespace podio {
SIOCollectionIDTableBlock::SIOCollectionIDTableBlock(podio::EventStore* store) :
sio::block("CollectionIDs", sio::version::encode_version(0, 3)) {
const auto table = store->getCollectionIDTable();
_names = table->names();
_ids = table->ids();
_types.reserve(_names.size());
_isSubsetColl.reserve(_names.size());
for (const int id : _ids) {
CollectionBase* tmp;
if (!store->get(id, tmp)) {
std::cerr
<< "PODIO-ERROR cannot construct CollectionIDTableBlock because a collection is missing from the store (id: "
<< id << ", name: " << table->name(id) << ")" << std::endl;
}
_types.push_back(tmp->getValueTypeName());
_isSubsetColl.push_back(tmp->isSubsetCollection());
}
}
void SIOCollectionIDTableBlock::read(sio::read_device& device, sio::version_type version) {
device.data(_names);
device.data(_ids);
device.data(_types);
if (version >= sio::version::encode_version(0, 2)) {
device.data(_isSubsetColl);
}
}
void SIOCollectionIDTableBlock::write(sio::write_device& device) {
device.data(_names);
device.data(_ids);
device.data(_types);
device.data(_isSubsetColl);
}
void writeGenericParameters(sio::write_device& device, const GenericParameters& params) {
writeMapLike(device, params.getIntMap());
writeMapLike(device, params.getFloatMap());
writeMapLike(device, params.getStringMap());
writeMapLike(device, params.getDoubleMap());
}
void readGenericParameters(sio::read_device& device, GenericParameters& params, sio::version_type version) {
readMapLike(device, params.getIntMap());
readMapLike(device, params.getFloatMap());
readMapLike(device, params.getStringMap());
if (version >= sio::version::encode_version(0, 2)) {
readMapLike(device, params.getDoubleMap());
}
}
void SIOEventMetaDataBlock::read(sio::read_device& device, sio::version_type version) {
readGenericParameters(device, *metadata, version);
}
void SIOEventMetaDataBlock::write(sio::write_device& device) {
writeGenericParameters(device, *metadata);
}
void SIONumberedMetaDataBlock::read(sio::read_device& device, sio::version_type version) {
int size;
device.data(size);
while (size--) {
int id;
device.data(id);
GenericParameters params;
readGenericParameters(device, params, version);
data->emplace(id, std::move(params));
}
}
void SIONumberedMetaDataBlock::write(sio::write_device& device) {
device.data((int)data->size());
for (const auto& [id, params] : *data) {
device.data(id);
writeGenericParameters(device, params);
}
}
std::shared_ptr<SIOBlock> SIOBlockFactory::createBlock(const std::string& typeStr, const std::string& name,
const bool isSubsetColl) const {
const auto it = _map.find(typeStr);
if (it != _map.end()) {
auto blk = std::shared_ptr<SIOBlock>(it->second->create(name));
blk->createBuffers(isSubsetColl);
return blk;
} else {
return nullptr;
}
}
std::shared_ptr<SIOBlock> SIOBlockFactory::createBlock(const podio::CollectionBase* col,
const std::string& name) const {
const std::string typeStr = col->getValueTypeName();
const auto it = _map.find(typeStr);
if (it != _map.end()) {
auto blk = std::shared_ptr<SIOBlock>(it->second->create(name));
blk->setCollection(const_cast<podio::CollectionBase*>(col));
return blk;
} else {
return nullptr;
}
}
SIOBlockLibraryLoader::SIOBlockLibraryLoader() {
for (const auto& [lib, dir] : getLibNames()) {
const auto status = loadLib(lib);
switch (status) {
case LoadStatus::Success:
std::cerr << "Loaded SIOBlocks library \'" << lib << "\' (from " << dir << ")" << std::endl;
break;
case LoadStatus::AlreadyLoaded:
std::cerr << "SIOBlocks library \'" << lib << "\' already loaded. Not loading again from " << dir << std::endl;
break;
case LoadStatus::Error:
std::cerr << "ERROR while loading SIOBlocks library \'" << lib << "\' (from " << dir << ")" << std::endl;
break;
}
}
}
SIOBlockLibraryLoader::LoadStatus SIOBlockLibraryLoader::loadLib(const std::string& libname) {
if (_loadedLibs.find(libname) != _loadedLibs.end()) {
return LoadStatus::AlreadyLoaded;
}
void* libhandle = dlopen(libname.c_str(), RTLD_LAZY | RTLD_GLOBAL);
if (libhandle) {
_loadedLibs.insert({libname, libhandle});
return LoadStatus::Success;
}
return LoadStatus::Error;
}
std::vector<std::tuple<std::string, std::string>> SIOBlockLibraryLoader::getLibNames() {
#ifdef USE_BOOST_FILESYSTEM
namespace fs = boost::filesystem;
#else
namespace fs = std::filesystem;
#endif
std::vector<std::tuple<std::string, std::string>> libs;
std::string dir;
const auto ldLibPath = std::getenv("LD_LIBRARY_PATH");
if (!ldLibPath) {
return libs;
}
std::istringstream stream(ldLibPath);
while (std::getline(stream, dir, ':')) {
if (not fs::exists(dir)) {
continue;
}
for (auto& lib : fs::directory_iterator(dir)) {
const auto filename = lib.path().filename().string();
if (filename.find("SioBlocks") != std::string::npos) {
libs.emplace_back(std::move(filename), dir);
}
}
}
return libs;
}
void SIOFileTOCRecord::addRecord(const std::string& name, PositionType startPos) {
auto it =
std::find_if(m_recordMap.begin(), m_recordMap.end(), [&name](const auto& entry) { return entry.first == name; });
if (it == m_recordMap.end()) {
m_recordMap.push_back({name, {startPos}});
} else {
it->second.push_back(startPos);
}
}
size_t SIOFileTOCRecord::getNRecords(const std::string& name) const {
const auto it = std::find_if(m_recordMap.cbegin(), m_recordMap.cend(),
[&name](const auto& entry) { return entry.first == name; });
if (it != m_recordMap.cend()) {
return it->second.size();
}
return 0;
}
SIOFileTOCRecord::PositionType SIOFileTOCRecord::getPosition(const std::string& name, unsigned iEntry) const {
const auto it = std::find_if(m_recordMap.cbegin(), m_recordMap.cend(),
[&name](const auto& keyVal) { return keyVal.first == name; });
if (it != m_recordMap.end()) {
if (iEntry < it->second.size()) {
return it->second[iEntry];
}
}
return 0;
}
std::vector<std::string_view> SIOFileTOCRecord::getRecordNames() const {
std::vector<std::string_view> cats;
cats.reserve(m_recordMap.size());
for (const auto& [cat, _] : m_recordMap) {
cats.emplace_back(cat);
}
return cats;
}
void SIOFileTOCRecordBlock::read(sio::read_device& device, sio::version_type) {
int size;
device.data(size);
while (size--) {
std::string name;
device.data(name);
std::vector<SIOFileTOCRecord::PositionType> positions;
device.data(positions);
record->m_recordMap.emplace_back(std::move(name), std::move(positions));
}
}
void SIOFileTOCRecordBlock::write(sio::write_device& device) {
device.data((int)record->m_recordMap.size());
for (const auto& [name, positions] : record->m_recordMap) {
device.data(name);
device.data(positions);
}
}
} // namespace podio