-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathntuple_dump.C
209 lines (186 loc) · 7.99 KB
/
ntuple_dump.C
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
/**
* Copyright CERN; j.lopez@cern.ch
*/
#include <ROOT/RNTupleDescriptor.hxx>
#include <ROOT/RNTupleSerialize.hxx>
#include <ROOT/RPageStorage.hxx>
#include <filesystem>
#include <fstream>
#include <memory>
#include <sstream>
#include <string>
#include <unistd.h>
using DescriptorId_t = ROOT::Experimental::DescriptorId_t;
using RColumnDescriptor = ROOT::Experimental::RColumnDescriptor;
using RFieldDescriptor = ROOT::Experimental::RFieldDescriptor;
using RNTupleDescriptor = ROOT::Experimental::RNTupleDescriptor;
using RNTupleReadOptions = ROOT::Experimental::RNTupleReadOptions;
using RNTupleSerializer = ROOT::Experimental::Internal::RNTupleSerializer;
using RPageSource = ROOT::Experimental::Internal::RPageSource;
using RPageStorage = ROOT::Experimental::Internal::RPageStorage;
using RClusterIndex = ROOT::Experimental::RClusterIndex;
/// \brief Helper class to dump RNTuple pages / metadata to separate files.
///
class RNTupleDumper {
std::unique_ptr<RPageSource> fSource;
struct RColumnInfo {
const RColumnDescriptor &fColumnDesc;
const RFieldDescriptor &fFieldDesc;
const std::string fQualName;
RColumnInfo(const RColumnDescriptor &columnDesc, const RFieldDescriptor &fieldDesc)
: fColumnDesc(columnDesc), fFieldDesc(fieldDesc),
fQualName(fieldDesc.GetFieldName() + "-" + std::to_string(columnDesc.GetIndex())) {}
};
void AddColumnsFromField(std::vector<RColumnInfo> &vec,
const RNTupleDescriptor &desc, const RFieldDescriptor &fieldDesc) {
for (const auto &F : desc.GetFieldIterable(fieldDesc)) {
for (const auto &C : desc.GetColumnIterable(F)) {
vec.emplace_back(C, F);
}
AddColumnsFromField(vec, desc, F);
}
}
public:
RNTupleDumper(std::unique_ptr<RPageSource> source)
: fSource(std::move(source)) {}
/// Recursively collect all the columns for all the fields rooted at field zero.
std::vector<RColumnInfo> CollectColumns() {
auto desc = fSource->GetSharedDescriptorGuard();
std::vector<RColumnInfo> columns;
AddColumnsFromField(columns, desc.GetRef(),
desc->GetFieldDescriptor(desc->GetFieldZeroId()));
return columns;
}
/// Iterate over all the clusters and dump the contents of each page for each column.
/// Generated file names follow the template `filenameTmpl` and are placed in directory `outputPath`.
/// TODO(jalopezg): format filenames according to the provided template
void DumpPages(const std::vector<RColumnInfo> &columns,
const std::string &outputPath, const std::string &/*filenameTmpl*/) {
auto desc = fSource->GetSharedDescriptorGuard();
std::uint64_t count = 0;
for (const auto &Cluster : desc->GetClusterIterable()) {
printf("\rDumping pages... [%lu / %lu clusters processed]", count++, desc->GetNClusters());
for (const auto &Col : columns) {
auto columnId = Col.fColumnDesc.GetPhysicalId();
if (!Cluster.ContainsColumn(columnId))
continue;
const auto &pages = Cluster.GetPageRange(columnId);
size_t idx = 0, page_nr = 0;
for (auto &PI : pages.fPageInfos) {
RClusterIndex index(Cluster.GetId(), idx);
RPageStorage::RSealedPage sealedPage;
fSource->LoadSealedPage(columnId, index, sealedPage);
auto buffer = std::make_unique<unsigned char[]>(sealedPage.fSize);
sealedPage.fBuffer = buffer.get();
fSource->LoadSealedPage(columnId, index, sealedPage);
{
std::ostringstream oss(outputPath, std::ios_base::ate);
oss << "/cluster" << Cluster.GetId()
<< "_" << Col.fQualName << "_pg" << page_nr++ << ".page";
std::ofstream of(oss.str(), std::ios_base::binary);
of.write(reinterpret_cast<const char *>(sealedPage.fBuffer), sealedPage.fSize);
}
idx += PI.fNElements;
}
}
}
printf("\nDumped data in %lu clusters!\n", count);
}
/// Dump ntuple header and footer to separate files.
void DumpMetadata(const std::string &outputPath) {
printf("Dumping ntuple metadata...\n");
auto desc = fSource->GetSharedDescriptorGuard();
auto context = RNTupleSerializer::SerializeHeader(nullptr, desc.GetRef());
auto szHeader = context.GetHeaderSize();
auto headerBuffer = std::make_unique<unsigned char[]>(szHeader);
context = RNTupleSerializer::SerializeHeader(headerBuffer.get(), desc.GetRef());
{
std::ofstream of(outputPath + "/header", std::ios_base::binary);
of.write(reinterpret_cast<char *>(headerBuffer.get()), szHeader);
}
for (const auto &CG : desc->GetClusterGroupIterable()) {
std::vector<DescriptorId_t> physClusterIds;
for (const auto &C : CG.GetClusterIds())
physClusterIds.emplace_back(context.MapClusterId(C));
context.MapClusterGroupId(CG.GetId());
{
auto szPageList = RNTupleSerializer::SerializePageList(nullptr, desc.GetRef(),
physClusterIds, context);
auto pageListBuffer = std::make_unique<unsigned char[]>(szPageList);
RNTupleSerializer::SerializePageList(pageListBuffer.get(), desc.GetRef(),
physClusterIds, context);
std::ofstream of(outputPath + "/cg" + std::to_string(CG.GetId()) + ".pagelist",
std::ios_base::binary);
of.write(reinterpret_cast<char *>(pageListBuffer.get()), szPageList);
}
}
auto szFooter = RNTupleSerializer::SerializeFooter(nullptr, desc.GetRef(), context);
auto footerBuffer = std::make_unique<unsigned char[]>(szFooter);
RNTupleSerializer::SerializeFooter(footerBuffer.get(), desc.GetRef(), context);
{
std::ofstream of(outputPath + "/footer", std::ios_base::binary);
of.write(reinterpret_cast<char *>(footerBuffer.get()), szFooter);
}
}
};
enum EDumpFlags {
kDumpNone = 0,
kDumpPages = 0x01,
kDumpMetadata = 0x02,
};
[[noreturn]]
static void Usage(char *argv0) {
printf("Usage: %s [-p] [-m] [-o output-path] file-name ntuple-name\n\n", argv0);
printf("Options:\n");
printf(" -p\t\tDump pages for all the columns\n");
printf(" -m\t\tDump ntuple metadata\n");
printf(" -o output-path\tGenerated files will be written to output-path (defaults to `./`)\n");
printf("\nAt least one of `-p` or `-m` is required.\n");
exit(0);
}
int main(int argc, char *argv[]) {
std::string inputFile;
std::string ntupleName;
std::string outputPath{"./"};
std::string filenameTmpl{"cluster%d_%s_pg%d.page"};
unsigned dumpFlags = kDumpNone;
int c;
while ((c = getopt(argc, argv, "hpmo:")) != -1) {
switch (c) {
case 'p':
dumpFlags |= kDumpPages;
break;
case 'm':
dumpFlags |= kDumpMetadata;
break;
case 'o':
outputPath = optarg;
break;
case 'h':
default:
Usage(argv[0]);
}
}
if ((argc - optind) != 2 || dumpFlags == kDumpNone)
Usage(argv[0]);
if (!std::filesystem::is_directory(outputPath)) {
fprintf(stderr, "'%s' is not a directory\n", outputPath.c_str());
exit(1);
}
auto source = RPageSource::Create(argv[optind + 1], argv[optind], RNTupleReadOptions());
source->Attach();
RNTupleDumper dumper(std::move(source));
if (dumpFlags & kDumpMetadata) {
dumper.DumpMetadata(outputPath);
}
if (dumpFlags & kDumpPages) {
auto columns = dumper.CollectColumns();
for (const auto &C : columns) {
printf("Column %lu: %s[%lu]\n", (unsigned long)C.fColumnDesc.GetPhysicalId(),
C.fFieldDesc.GetFieldName().c_str(),
(unsigned long)C.fColumnDesc.GetIndex());
}
dumper.DumpPages(columns, outputPath, filenameTmpl);
}
return 0;
}