-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathSharedRootEventSource.cc
More file actions
249 lines (212 loc) · 8.74 KB
/
SharedRootEventSource.cc
File metadata and controls
249 lines (212 loc) · 8.74 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
#include "SharedRootEventSource.h"
#include "SourceFactory.h"
#include "Deserializer.h"
#include "UnrolledDeserializer.h"
#include "TClass.h"
using namespace cce::tf;
SharedRootEventSource::SharedRootEventSource(unsigned int iNLanes, unsigned long long iNEvents, std::string const& iName) :
SharedSourceBase(iNEvents),
file_{TFile::Open(iName.c_str())},
readTime_{std::chrono::microseconds::zero()}
{
//gDebug = 3;
if(not file_) {
std::cout <<"unknown file "<<iName<<std::endl;
throw std::runtime_error("uknown file");
}
eventsTree_ = file_->Get<TTree>("Events");
if(not eventsTree_) {
std::cout <<"no Events TTree in file "<<iName<<std::endl;
throw std::runtime_error("no Events TTree");
}
eventsBranch_ = eventsTree_->GetBranch("offsetsAndBlob");
if( not eventsBranch_) {
std::cout <<"no 'offsetsAndBlob' TBranch in 'Events' TTree in file "<<iName<<std::endl;
throw std::runtime_error("no 'offsetsAndBlob' TBranch");
}
idBranch_ = eventsTree_->GetBranch("EventID");
if(not idBranch_) {
std::cout <<"no 'EventID' TBranch in 'Events' TTree in file "<<iName<<std::endl;
throw std::runtime_error("no 'EventID' TBranch");
}
auto meta = file_->Get<TTree>("Meta");
if(not meta) {
std::cout <<"no 'Meta' TTree in file "<<iName<<std::endl;
throw std::runtime_error("no 'Meta' TTree");
}
std::vector<std::pair<std::string, std::string>> typeAndNames;
int objectSerializationUsed;
std::string compression;
{
auto typeAndNamesBranch = meta->GetBranch("DataProducts");
auto pTemp = &typeAndNames;
typeAndNamesBranch->SetAddress(&pTemp);
typeAndNamesBranch->GetEntry(0);
//std::cout <<"typeAndNames.size() "<<typeAndNames.size()<<std::endl;
//for(auto const& tn: typeAndNames) {
// std::cout <<" "<<tn.first<<" "<<tn.second<<std::endl;
//}
}
{
auto serializerBranch = meta->GetBranch("objectSerializationUsed");
serializerBranch->SetAddress(&objectSerializationUsed);
serializerBranch->GetEntry(0);
//std::cout <<"serialization "<<objectSerializationUsed<<std::endl;
}
{
auto compressionBranch = meta->GetBranch("compressionAlgorithm");
auto pTemp = &compression;
compressionBranch->SetAddress(&pTemp);
compressionBranch->GetEntry(0);
//std::cout <<"compressionAlgorithm "<<compression<<std::endl;
}
assert(objectSerializationUsed == static_cast<int>(pds::Serialization::kRoot) or
objectSerializationUsed == static_cast<int>(pds::Serialization::kRootUnrolled));
pds::Serialization serialization{objectSerializationUsed};
if (compression == "None") {
compression_ = pds::Compression::kNone;
}else if (compression == "LZ4") {
compression_ = pds::Compression::kLZ4;
}else if (compression == "ZSTD") {
compression_ = pds::Compression::kZSTD;
} else {
std::cout <<"Unknown compression algorithm '"<<compression<<"'"<<std::endl;
throw std::runtime_error("unknown compression algorithm");
}
std::vector<pds::ProductInfo> productInfo;
productInfo.reserve(typeAndNames.size());
{
unsigned int index = 0;
for( auto const& [type, name]: typeAndNames) {
productInfo.emplace_back(name, index++, type);
}
}
laneInfos_.reserve(iNLanes);
for(unsigned int i = 0; i< iNLanes; ++i) {
DeserializeStrategy strategy;
switch(serialization) {
case pds::Serialization::kRoot: {
strategy = DeserializeStrategy::make<DeserializeProxy<Deserializer>>(); break;
}
case pds::Serialization::kRootUnrolled: {
strategy = DeserializeStrategy::make<DeserializeProxy<UnrolledDeserializer>>(); break;
}
}
laneInfos_.emplace_back(productInfo, std::move(strategy));
}
}
SharedRootEventSource::LaneInfo::LaneInfo(std::vector<pds::ProductInfo> const& productInfo, DeserializeStrategy deserialize):
deserializers_{std::move(deserialize)},
decompressTime_{std::chrono::microseconds::zero()},
deserializeTime_{std::chrono::microseconds::zero()}
{
dataProducts_.reserve(productInfo.size());
dataBuffers_.resize(productInfo.size(), nullptr);
deserializers_.reserve(productInfo.size());
size_t index =0;
for(auto const& pi : productInfo) {
TClass* cls = TClass::GetClass(pi.className().c_str());
dataBuffers_[index] = cls->New();
assert(cls);
dataProducts_.emplace_back(index,
&dataBuffers_[index],
pi.name(),
cls,
&delayedRetriever_);
deserializers_.emplace_back(cls);
++index;
}
}
SharedRootEventSource::LaneInfo::~LaneInfo() {
auto it = dataProducts_.begin();
for( void * b: dataBuffers_) {
it->classType()->Destructor(b);
++it;
}
}
size_t SharedRootEventSource::numberOfDataProducts() const {
return laneInfos_[0].dataProducts_.size();
}
std::vector<DataProductRetriever>& SharedRootEventSource::dataProducts(unsigned int iLane, long iEventIndex) {
return laneInfos_[iLane].dataProducts_;
}
EventIdentifier SharedRootEventSource::eventIdentifier(unsigned int iLane, long iEventIndex) {
return laneInfos_[iLane].eventID_;
}
void SharedRootEventSource::readEventAsync(unsigned int iLane, long iEventIndex, OptionalTaskHolder iTask) {
queue_.push(*iTask.group(), [iLane, optTask = std::move(iTask), this, iEventIndex]() mutable {
auto start = std::chrono::high_resolution_clock::now();
if(iEventIndex < eventsTree_->GetEntries()) {
std::pair<std::vector<uint32_t>, std::vector<char>> offsetsAndBuffer;
auto pBuffer = &offsetsAndBuffer;
eventsBranch_->SetAddress(&pBuffer);
idBranch_->SetAddress(&this->laneInfos_[iLane].eventID_);
eventsTree_->GetEntry(iEventIndex);
{
//auto const& id = this->laneInfos_[iLane].eventID_;
//std::cout <<"event entry "<<iEventIndex<<std::endl;
//std::cout <<"ID "<<id.run<<" "<<id.lumi<<" "<<id.event<<std::endl;
//std::cout <<"buffer size "<<buffer.size()<<std::endl;
//std::cout <<"offset size "<<offsets.size()<<std::endl;
//for(auto b: buffer) {
// std::cout <<" "<<b<<std::endl;
//}
}
auto group = optTask.group();
group->run([this, offsetsAndBuffer=std::move(offsetsAndBuffer), task = optTask.releaseToTaskHolder(), iLane]() {
auto& laneInfo = this->laneInfos_[iLane];
auto start = std::chrono::high_resolution_clock::now();
std::vector<char> uBuffer = pds::uncompressBuffer(this->compression_, offsetsAndBuffer.second, offsetsAndBuffer.first.back());
std::cout <<"uncompressed buffer size "<<uBuffer.size() <<std::endl;
laneInfo.decompressTime_ +=
std::chrono::duration_cast<decltype(laneInfo.decompressTime_)>(std::chrono::high_resolution_clock::now() - start);
start = std::chrono::high_resolution_clock::now();
//uBuffer.pop_back();
pds::deserializeDataProducts(uBuffer.data(), uBuffer.data()+uBuffer.size(),
offsetsAndBuffer.first.begin(), offsetsAndBuffer.first.end(),
laneInfo.dataProducts_, laneInfo.deserializers_);
laneInfo.deserializeTime_ +=
std::chrono::duration_cast<decltype(laneInfo.deserializeTime_)>(std::chrono::high_resolution_clock::now() - start);
});
}
readTime_ +=std::chrono::duration_cast<decltype(readTime_)>(std::chrono::high_resolution_clock::now() - start);
});
}
void SharedRootEventSource::printSummary() const {
std::cout <<"\nSource:\n"
" read time: "<<readTime().count()<<"us\n"
" decompress time: "<<decompressTime().count()<<"us\n"
" deserialize time: "<<deserializeTime().count()<<"us\n"<<std::endl;
};
std::chrono::microseconds SharedRootEventSource::readTime() const {
return readTime_;
}
std::chrono::microseconds SharedRootEventSource::decompressTime() const {
auto time = std::chrono::microseconds::zero();
for(auto const& l : laneInfos_) {
time += l.decompressTime_;
}
return time;
}
std::chrono::microseconds SharedRootEventSource::deserializeTime() const {
auto time = std::chrono::microseconds::zero();
for(auto const& l : laneInfos_) {
time += l.deserializeTime_;
}
return time;
}
namespace {
class Maker : public SourceMakerBase {
public:
Maker(): SourceMakerBase("SharedRootEventSource") {}
std::unique_ptr<SharedSourceBase> create(unsigned int iNLanes, unsigned long long iNEvents, ConfigurationParameters const& params) const final {
auto fileName = params.get<std::string>("fileName");
if(not fileName) {
std::cout <<"no file name given\n";
return {};
}
return std::make_unique<SharedRootEventSource>(iNLanes, iNEvents, *fileName);
}
};
Maker s_maker;
}