-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathntuple_change_compression.cxx
76 lines (62 loc) · 2.45 KB
/
ntuple_change_compression.cxx
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
/*
ntuple_change_compression
A small utility used to test the capability of RNTupleMerger to change compression of its inputs.
Accepts one or more ROOT input files containing one or more RNTuples and outputs one file with
all the RNTuples merged with possibly changed compression algorithm and level.
@author Giacomo Parolini, 2024
*/
#include <ROOT/RLogger.hxx>
#include <ROOT/RNTupleReader.hxx>
#include <ROOT/RNTupleMerger.hxx>
#include <ROOT/RPageStorageFile.hxx>
#include <TFile.h>
#include <TROOT.h>
#include <ROOT/RNTupleWriteOptions.hxx>
#include <iostream>
using namespace ROOT::Experimental;
using namespace ROOT::Experimental::Internal;
using namespace std::chrono;
int main(int argc, char **argv) {
if (argc < 5) {
fprintf(stderr, "Usage: %s <compression_settings> <ntuple_file_out> <ntuple_name> <ntuple_file1.root> [ntuple_file2.root ...]\n", argv[0]);
fprintf(stderr, "Common compression settings:\n\t-1: preserve;\n\t0: uncompressed\n\t505: Zstd\n\t207: LZMA\n");
return 1;
}
#ifdef R__USE_IMT
ROOT::EnableImplicitMT();
#endif
auto noWarn = RLogScopedVerbosity(NTupleLog(), ELogLevel::kError);
const int compSettings = std::atoi(argv[1]);
const char *ntuple_file_out = argv[2];
const char *ntuple_name = argv[3];
std::vector<const char *> ntuple_files;
for (int i = 4; i < argc; ++i)
ntuple_files.push_back(argv[i]);
std::vector<std::unique_ptr<RPageSource>> srcs;
std::vector<RPageSource *> srcsRaw;
srcs.reserve(ntuple_files.size());
srcsRaw.reserve(ntuple_files.size());
for (const char *ntuple_file : ntuple_files) {
auto file = std::unique_ptr<TFile>(TFile::Open(ntuple_file));
if (!file)
return 1;
auto *anchor = file->Get<RNTuple>(ntuple_name);
if (!anchor) {
std::cerr << "Error reading RNTuple " << ntuple_name << " from " << ntuple_file << ": skipping.\n";
continue;
}
auto src = RPageSourceFile::CreateFromAnchor(*anchor);
auto &s = srcs.emplace_back(std::move(src));
srcsRaw.push_back(s.get());
}
auto dst = RPageSinkFile { ntuple_name, ntuple_file_out, RNTupleWriteOptions {} };
RNTupleMerger merger;
RNTupleMergeOptions merge_opts;
merge_opts.fCompressionSettings = compSettings;
merger.Merge(srcsRaw, dst, merge_opts);
std::cout << "Merged " << srcsRaw.size() << " ntuples.";
if (compSettings != -1)
std::cout << " Compression changed to " << compSettings << ".";
std::cout << "\nOut file is " << ntuple_file_out << "\n";
return 0;
}