-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathsplitzip.C
268 lines (225 loc) · 10.8 KB
/
splitzip.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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
using RNTupleReader = ROOT::Experimental::RNTupleReader;
using RNTupleDescriptor = ROOT::Experimental::RNTupleDescriptor;
using DescriptorId_t = ROOT::Experimental::DescriptorId_t;
TH2D *gHistRatioFloat[3];
TH2D *gHistRatioDouble[3];
TH2D *gHistRatioInt32[3];
TH2D *gHistRatioInt64[3];
TH2D *gHistRatioIndex[3];
struct ColumnSize {
std::string fName;
DescriptorId_t fColId;
std::int64_t fSize;
bool operator <(const ColumnSize &other) {
return fSize > other.fSize;
}
};
std::vector<ColumnSize> gColumnsInMemory;
std::vector<ColumnSize> gColumnsOnDisk;
DescriptorId_t GetOtherId(const RNTupleDescriptor &desc1, const RNTupleDescriptor &desc2, DescriptorId_t fieldId)
{
if (fieldId == desc1.GetFieldZeroId())
return desc2.GetFieldZeroId();
const auto &f = desc1.GetFieldDescriptor(fieldId);
auto otherParent = GetOtherId(desc1, desc2, f.GetParentId());
return desc2.FindFieldId(f.GetFieldName(), otherParent);
}
double GetCompressionOfColumn(const RNTupleDescriptor &desc, DescriptorId_t colId)
{
const auto &colDesc = desc.GetColumnDescriptor(colId);
auto elementSize = ROOT::Experimental::Detail::RColumnElementBase::Generate(colDesc.GetModel().GetType())->GetSize();
std::uint64_t bytesOnStorage = 0;
std::uint64_t bytesInMemory = 0;
auto clusterId = desc.FindClusterId(colId, 0);
while (clusterId != ROOT::Experimental::kInvalidDescriptorId) {
const auto &clusterDesc = desc.GetClusterDescriptor(clusterId);
const auto &pageRange = clusterDesc.GetPageRange(colId);
for (const auto &page : pageRange.fPageInfos) {
bytesOnStorage += page.fLocator.fBytesOnStorage;
bytesInMemory += page.fNElements * elementSize;
}
clusterId = desc.FindNextClusterId(clusterId);
}
return double(bytesOnStorage) / double(bytesInMemory);
}
std::int64_t GetMemSizeOfColumn(const RNTupleDescriptor &desc, DescriptorId_t colId)
{
const auto &colDesc = desc.GetColumnDescriptor(colId);
auto elementSize = ROOT::Experimental::Detail::RColumnElementBase::Generate(colDesc.GetModel().GetType())->GetSize();
return desc.GetNElements(colId) * elementSize;
}
std::int64_t GetOnDiskSizeOfColumn(const RNTupleDescriptor &desc, DescriptorId_t colId)
{
const auto &colDesc = desc.GetColumnDescriptor(colId);
std::uint64_t bytesOnStorage = 0;
auto clusterId = desc.FindClusterId(colId, 0);
while (clusterId != ROOT::Experimental::kInvalidDescriptorId) {
const auto &clusterDesc = desc.GetClusterDescriptor(clusterId);
const auto &pageRange = clusterDesc.GetPageRange(colId);
for (const auto &page : pageRange.fPageInfos)
bytesOnStorage += page.fLocator.fBytesOnStorage;
clusterId = desc.FindNextClusterId(clusterId);
}
return bytesOnStorage;
}
void CompareImpl(const RNTupleDescriptor &desc1, const RNTupleDescriptor &desc2, DescriptorId_t fieldId)
{
auto fieldName = desc1.GetQualifiedFieldName(fieldId);
auto otherFieldId = GetOtherId(desc1, desc2, fieldId);
for (const auto &c : desc1.GetColumnRange(fieldId)) {
const auto otherColumnId = desc2.FindColumnId(otherFieldId, c.GetIndex());
std::cout << std::setw(30);
std::cout << fieldName << " [" << c.GetIndex() << "]";
auto factor1 = GetCompressionOfColumn(desc1, c.GetId());
auto factor2 = GetCompressionOfColumn(desc2, otherColumnId);
std::cout << ": " << factor1 << " " << factor2 << std::endl;
// split / non-split
auto ratio = factor2 / factor1;
auto memSizeMb = double(GetMemSizeOfColumn(desc1, c.GetId())) / 1000. / 1000.;
if (isnan(ratio))
continue;
if (ratio > 1.5)
std::cerr << "WARNING!!!!" << std::endl;
switch (c.GetModel().GetType()) {
case ROOT::Experimental::EColumnType::kReal32:
gHistRatioFloat[0]->Fill(factor1, memSizeMb);
gHistRatioFloat[1]->Fill(factor2, memSizeMb);
gHistRatioFloat[2]->Fill(ratio, memSizeMb);
break;
case ROOT::Experimental::EColumnType::kReal64:
gHistRatioDouble[0]->Fill(factor1, memSizeMb);
gHistRatioDouble[1]->Fill(factor2, memSizeMb);
gHistRatioDouble[2]->Fill(ratio, memSizeMb);
break;
case ROOT::Experimental::EColumnType::kInt32:
gHistRatioInt32[0]->Fill(factor1, memSizeMb);
gHistRatioInt32[1]->Fill(factor2, memSizeMb);
gHistRatioInt32[2]->Fill(ratio, memSizeMb);
break;
case ROOT::Experimental::EColumnType::kInt64:
gHistRatioInt64[0]->Fill(factor1, memSizeMb);
gHistRatioInt64[1]->Fill(factor2, memSizeMb);
gHistRatioInt64[2]->Fill(ratio, memSizeMb);
break;
case ROOT::Experimental::EColumnType::kIndex:
gHistRatioIndex[0]->Fill(factor1, memSizeMb);
gHistRatioIndex[1]->Fill(factor2, memSizeMb);
gHistRatioIndex[2]->Fill(ratio, memSizeMb);
break;
case ROOT::Experimental::EColumnType::kBit:
// Ok, nothing to split
break;
case ROOT::Experimental::EColumnType::kByte:
// Ok, nothing to split
break;
default:
std::cerr << "unsupported column type! " << (int)c.GetModel().GetType() << std::endl;
abort();
}
}
for (const auto &f : desc1.GetFieldRange(fieldId))
CompareImpl(desc1, desc2, f.GetId());
}
void GetColumnSizes(std::int64_t &largestColumn, const RNTupleDescriptor &desc, DescriptorId_t fieldId)
{
for (const auto &c : desc.GetColumnRange(fieldId)) {
auto colSizeInMemory = GetMemSizeOfColumn(desc, c.GetId());
largestColumn = std::max(colSizeInMemory, largestColumn);
ColumnSize cs{desc.GetQualifiedFieldName(fieldId) + "[" + std::to_string(c.GetIndex()) + "]",
c.GetId(),
colSizeInMemory};
gColumnsInMemory.emplace_back(cs);
cs.fSize = GetOnDiskSizeOfColumn(desc, c.GetId());
gColumnsOnDisk.emplace_back(cs);
}
for (const auto &f : desc.GetFieldRange(fieldId))
GetColumnSizes(largestColumn, desc, f.GetId());
}
void splitzip(std::string ntupleName, std::string file1, std::string file2,
std::string outfile = "splitzip.root")
{
auto ntuple1 = RNTupleReader::Open(ntupleName, file1);
auto ntuple2 = RNTupleReader::Open(ntupleName, file2);
const auto &desc1 = ntuple1->GetDescriptor();
const auto &desc2 = ntuple2->GetDescriptor();
std::int64_t largestColumn = 0;
GetColumnSizes(largestColumn, desc1, desc1.GetFieldZeroId());
double largestColumnMB = double(largestColumn) / 1000. / 1000.;
largestColumnMB *= 1.2;
auto f = TFile::Open(outfile.c_str(), "RECREATE");
gHistRatioFloat[0] = new TH2D("n_floats", "Non-split float", 25, 0, 1.1, 25, 0, largestColumnMB);
gHistRatioDouble[0] = new TH2D("n_doubles", "Non-split double", 25, 0, 1.1, 25, 0, largestColumnMB);
gHistRatioInt32[0] = new TH2D("n_int32s", "Non-split int32", 25, 0, 1.1, 25, 0, largestColumnMB);
gHistRatioInt64[0] = new TH2D("n_int64s", "Non-split int64", 25, 0, 1.1, 25, 0, largestColumnMB);
gHistRatioIndex[0] = new TH2D("n_offsets", "Non-split offset", 25, 0, 1.1, 25, 0, largestColumnMB);
gHistRatioFloat[1] = new TH2D("s_floats", "split float", 25, 0, 1.1, 25, 0, largestColumnMB);
gHistRatioDouble[1] = new TH2D("s_doubles", "split double", 25, 0, 1.1, 25, 0, largestColumnMB);
gHistRatioInt32[1] = new TH2D("s_int32s", "split int32", 25, 0, 1.1, 25, 0, largestColumnMB);
gHistRatioInt64[1] = new TH2D("s_int64s", "split int64", 25, 0, 1.1, 25, 0, largestColumnMB);
gHistRatioIndex[1] = new TH2D("s_offsets", "split offset", 25, 0, 1.1, 25, 0, largestColumnMB);
gHistRatioFloat[2] = new TH2D("r_floats", "ratio split/unsplit float", 25, 0.05, 2.5, 25, 0, largestColumnMB);
gHistRatioDouble[2] = new TH2D("r_doubles", "ratio split/unsplit double", 25, 0.05, 2.5, 25, 0, largestColumnMB);
gHistRatioInt32[2] = new TH2D("r_int32s", "ratio split/unsplit int32", 25, 0.05, 2.5, 25, 0, largestColumnMB);
gHistRatioInt64[2] = new TH2D("r_int64s", "ratio split/unsplit int64", 25, 0.05, 2.5, 25, 0, largestColumnMB);
gHistRatioIndex[2] = new TH2D("r_offsets", "ratio split/unsplit offset", 25, 0.05, 2.5, 25, 0, largestColumnMB);
CompareImpl(desc1, desc2, desc1.GetFieldZeroId());
std::sort(gColumnsInMemory.begin(), gColumnsInMemory.end());
std::cout << std::endl << "Largest columns in memory" << std::endl;
for (unsigned i = 0; i < 8; ++i) {
std::cout << gColumnsInMemory[i].fName << " " << gColumnsInMemory[i].fSize / 1000 / 1000 << "MB" << std::endl;
}
std::cout << std::endl << "Largest columns on disk" << std::endl;
for (unsigned i = 0; i < 8; ++i) {
std::cout << gColumnsOnDisk[i].fName << " " << gColumnsInMemory[i].fSize / 1000 / 1000 << "MB" << std::endl;
}
TCanvas *c1 = new TCanvas("c1", "c1", 0, 0, 1000, 1000);
c1->SetTitle((file1 + " vs. " + file2).c_str());
c1->Divide(3, 5);
for (int i = 0; i < 3; ++i) {
c1->cd(0 + (i+1));
gHistRatioFloat[i]->Draw("colz");
if (i < 2) gHistRatioFloat[i]->SetXTitle("Compression Ratio");
gHistRatioFloat[i]->SetYTitle("Column Size (MB)");
c1->cd(3 + (i+1));
gHistRatioDouble[i]->Draw("colz");
if (i < 2) gHistRatioDouble[i]->SetXTitle("Compression Ratio");
gHistRatioDouble[i]->SetYTitle("Column Size (MB)");
c1->cd(6 + (i+1));
gHistRatioIndex[i]->Draw("colz");
if (i < 2) gHistRatioIndex[i]->SetXTitle("Compression Ratio");
gHistRatioIndex[i]->SetYTitle("Column Size (MB)");
c1->cd(9 + (i+1));
gHistRatioInt32[i]->Draw("colz");
if (i < 2) gHistRatioInt32[i]->SetXTitle("Compression Ratio");
gHistRatioInt32[i]->SetYTitle("Column Size (MB)");
c1->cd(12 + (i+1));
gHistRatioInt64[i]->Draw("colz");
if (i < 2) gHistRatioInt64[i]->SetXTitle("Compression Ratio");
gHistRatioInt64[i]->SetYTitle("Column Size (MB)");
}
for (int i = 0; i < 3; ++i) {
gHistRatioFloat[i]->Write();
gHistRatioDouble[i]->Write();
gHistRatioInt32[i]->Write();
gHistRatioInt64[i]->Write();
gHistRatioIndex[i]->Write();
}
c1->Write();
f->Close();
delete c1;
delete f;
}
void runAll() {
std::string samples[4] = { "B2HHH", "h1dstX10", "ttjet_13tev_june2019", "gg_data" };
std::string ntupleNames[4] = { "DecayTree", "h42", "NTuple", "mini" };
std::string zipAlgos[4] = { "lz4", "zlib", "zstd", "lzma" };
std::string unsplitPath = "/data/calibration/";
std::string splitPath = "/data/calibration/split/";
for (unsigned i = 0; i < 4; ++i) {
for (unsigned j = 0; j < 4; ++j) {
std::string inputFile = samples[i] + "~" + zipAlgos[j] + ".ntuple";
std::string outputFile = std::string("splitzip-") + samples[i] + "~" + zipAlgos[j] + ".root";
splitzip(ntupleNames[i], unsplitPath + inputFile, splitPath + inputFile, outputFile);
}
}
}