-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathRNTupleTFileOutputer.cc
More file actions
136 lines (117 loc) · 5.15 KB
/
RNTupleTFileOutputer.cc
File metadata and controls
136 lines (117 loc) · 5.15 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
#include <iostream>
#include "RNTupleTFileOutputer.h"
#include "OutputerFactory.h"
#include "FunctorTask.h"
#include "RNTupleOutputerConfig.h"
#include "RNTupleOutputerFieldMaker.h"
#include <ROOT/RNTupleModel.hxx>
#include <ROOT/RField.hxx>
#include <ROOT/RFieldVisitor.hxx>
using namespace cce::tf;
RNTupleTFileOutputer::RNTupleTFileOutputer(std::string const& fileName, unsigned int iNLanes, RNTupleOutputerConfig const& iConfig):
fileName_(fileName),
config_(iConfig),
file_(fileName.c_str(), "recreate", ""),
entries_(iNLanes),
collateTime_{std::chrono::microseconds::zero()},
parallelTime_{0}
{ }
void RNTupleTFileOutputer::setupForLane(unsigned int iLaneIndex, std::vector<DataProductRetriever> const& iDPs) {
if ( iLaneIndex == 0 ) {
const std::string eventAuxiliaryBranchName{"EventAuxiliary"};
bool hasEventAuxiliaryBranch = false;
auto model = ROOT::RNTupleModel::CreateBare();
fieldIDs_.reserve(iDPs.size());
RNTupleOutputerFieldMaker fieldMaker(config_);
for(auto const& dp: iDPs) {
// chop last . if present
if(dp.name() == eventAuxiliaryBranchName) {
hasEventAuxiliaryBranch = true;
}
auto name = dp.name().substr(0, dp.name().find("."));
if ( config_.verbose_ > 1 ) std::cout << "-------- Creating field for " << name << " of type " << dp.classType()->GetName() << "\n";
try {
auto field = fieldMaker.make(name, dp.classType()->GetName());
assert(field);
if ( config_.verbose_ > 1 ) ROOT::Internal::RPrintSchemaVisitor(std::cout, '*', 1000, 10).VisitField(*field);
model->AddField(std::move(field));
fieldIDs_.emplace_back(std::move(name));
}
catch (ROOT::RException& e) {
std::cout << "Failed: " << e.what() << "\n";
throw std::runtime_error("Failed to create field");
}
}
if(not hasEventAuxiliaryBranch) {
id_ = std::make_shared<EventIdentifier>();
auto field = ROOT::RFieldBase::Create("EventID", "cce::tf::EventIdentifier").Unwrap();
if ( config_.verbose_ > 1 ) ROOT::Internal::RPrintSchemaVisitor(std::cout, '*', 1000, 10).VisitField(*field);
assert(field);
model->AddField(std::move(field));
fieldIDs_.emplace_back("EventID");
}
// https://root.cern/doc/v626/classROOT_1_1Experimental_1_1RNTupleWriteOptions.html
auto writeOptions = writeOptionsFrom(config_);
ntuple_ = ROOT::RNTupleWriter::Append(std::move(model), "Events", file_, writeOptions);
}
else if ( not ntuple_ ) {
throw std::logic_error("setupForLane should be sequential");
}
entries_[iLaneIndex].retrievers = &iDPs;
}
void RNTupleTFileOutputer::productReadyAsync(unsigned int iLaneIndex, DataProductRetriever const& iDataProduct, TaskHolder iCallback) const {
}
void RNTupleTFileOutputer::outputAsync(unsigned int iLaneIndex, EventIdentifier const& iEventID, TaskHolder iCallback) const {
auto start = std::chrono::high_resolution_clock::now();
auto group = iCallback.group();
collateQueue_.push(*group, [this, iEventID, iLaneIndex, callback=std::move(iCallback)]() mutable {
collateProducts(iEventID, entries_[iLaneIndex], std::move(callback));
});
auto time = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - start);
parallelTime_ += time.count();
}
void RNTupleTFileOutputer::printSummary() const {
auto start = std::chrono::high_resolution_clock::now();
ntuple_.reset();
auto deleteTime = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - start);
start = std::chrono::high_resolution_clock::now();
std::cout <<"RNTupleTFileOutputer\n"
" total serial collate time at end event: "<<collateTime_.count()<<"us\n"
" total non-serializer parallel time at end event: "<<parallelTime_.load()<<"us\n"
" end of job RNTupleWriter shutdown time: "<<deleteTime.count()<<"us\n";
}
void RNTupleTFileOutputer::collateProducts(
EventIdentifier const& iEventID,
RNTupleTFileOutputer::EntryContainer const& entry,
TaskHolder iCallback
) const
{
auto start = std::chrono::high_resolution_clock::now();
auto thisOffset = eventGlobalOffset_++;
if ( config_.verbose_ > 0 ) std::cout << thisOffset << " event id " << iEventID.run << ", "<< iEventID.lumi<<", "<<iEventID.event<<"\n";
auto rentry = ntuple_->CreateEntry();
for(size_t i=0; i < entry.retrievers->size(); ++i) {
void** ptr = (*entry.retrievers)[i].address();
rentry->BindRawPtr(fieldIDs_[i], *ptr);
}
if(id_) {
*id_ = iEventID;
rentry->BindRawPtr("EventID", id_.get());
}
ntuple_->Fill(*rentry);
collateTime_ += std::chrono::duration_cast<decltype(collateTime_)>(std::chrono::high_resolution_clock::now() - start);
}
namespace {
class Maker : public OutputerMakerBase {
public:
Maker(): OutputerMakerBase("RNTupleTFileOutputer") {}
std::unique_ptr<OutputerBase> create(unsigned int iNLanes, ConfigurationParameters const& params) const final {
auto result = parseRNTupleConfig(params);
if(not result) {
return {};
}
return std::make_unique<RNTupleTFileOutputer>(result->first, iNLanes, result->second);
}
};
Maker s_maker;
}