Skip to content

Commit 06ea023

Browse files
committed
Add Real32Quant
1 parent ce8a39a commit 06ea023

File tree

4 files changed

+173
-3
lines changed

4 files changed

+173
-3
lines changed

types/fundamental/README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44
* [`misc`](misc): `Bit`, `Byte`, `Char`
55
* [`real`](real): `Real{16,32,64}`, `SplitReal{32,64}`
66
* [`real32trunc`](real32trunc): `Real32Trunc`
7+
* [`real32quant`](real32quant): `Real32Quant`
78

89
__Covered under a different category:__
910
* `[Split]Index{32,64}`: with [`std::string`](../string) and [`std::vector`](../vector)
1011
* `Switch`: with [`std::variant`](../variant)
11-
12-
__Missing:__
13-
* `Real32Quant`
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Real Column Types with variable width
2+
3+
## Fields
4+
5+
* `FloatReal32Quant2_-1_1`
6+
* `DoubleReal32Quant2_-1_1`
7+
* `DoubleReal32Quant20_-1_1`
8+
* `FloatReal32Quant8_0_1`
9+
* `FloatReal32Quant16_0_1`
10+
* `DoubleReal32Quant31_-100_25`
11+
12+
with the corresponding field, column types, bit width and value range.
13+
14+
## Entries
15+
16+
1. Ascending values
17+
2. All 1s in the mantissa
18+
3. Middle values of the fields' value range
19+
4. Min values of the fields' value range
20+
5. Max values of the fields' value range

types/fundamental/real32quant/read.C

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include <ROOT/REntry.hxx>
2+
#include <ROOT/RNTupleReader.hxx>
3+
4+
using ROOT::Experimental::REntry;
5+
using ROOT::Experimental::RNTupleReader;
6+
7+
#include <fstream>
8+
#include <ios>
9+
#include <ostream>
10+
#include <string>
11+
#include <string_view>
12+
13+
template <typename T>
14+
static void PrintRealValue(const REntry &entry, std::string_view name,
15+
std::ostream &os, bool last = false) {
16+
T value = *entry.GetPtr<T>(name);
17+
os << " \"" << name << "\": \"" << value << "\"";
18+
if (!last) {
19+
os << ",";
20+
}
21+
os << "\n";
22+
}
23+
24+
void read(std::string_view input = "types.fundamental.real32quant.root",
25+
std::string_view output = "types.fundamental.real32quant.json") {
26+
std::ofstream os(std::string{output});
27+
// Print floating-point numbers as hexadecimal literals.
28+
os << std::hexfloat;
29+
os << "[\n";
30+
31+
auto reader = RNTupleReader::Open("ntpl", input);
32+
auto &entry = reader->GetModel().GetDefaultEntry();
33+
bool first = true;
34+
for (auto index : *reader) {
35+
reader->LoadEntry(index);
36+
37+
if (first) {
38+
first = false;
39+
} else {
40+
os << ",\n";
41+
}
42+
os << " {\n";
43+
44+
PrintRealValue<float>(entry, "FloatReal32Quant2_-1_1", os);
45+
PrintRealValue<double>(entry, "DoubleReal32Quant2_-1_1", os);
46+
PrintRealValue<double>(entry, "DoubleReal32Quant20_-1_1", os);
47+
PrintRealValue<float>(entry, "FloatReal32Quant8_0_1", os);
48+
PrintRealValue<float>(entry, "FloatReal32Quant16_0_1", os);
49+
PrintRealValue<double>(entry, "DoubleReal32Quant31_-100_25", os, /*last=*/true);
50+
51+
os << " }";
52+
// Newline is intentionally missing, may need to print a comma before the
53+
// next entry.
54+
}
55+
os << "\n";
56+
os << "]\n";
57+
}

types/fundamental/real32quant/write.C

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#include <ROOT/RField.hxx>
2+
#include <ROOT/RNTupleModel.hxx>
3+
#include <ROOT/RNTupleUtil.hxx>
4+
#include <ROOT/RNTupleWriteOptions.hxx>
5+
#include <ROOT/RNTupleWriter.hxx>
6+
7+
using ROOT::Experimental::EColumnType;
8+
using ROOT::Experimental::RField;
9+
using ROOT::Experimental::RNTupleModel;
10+
using ROOT::Experimental::RNTupleWriteOptions;
11+
using ROOT::Experimental::RNTupleWriter;
12+
13+
#include <limits>
14+
#include <memory>
15+
#include <string_view>
16+
17+
template <typename T>
18+
static std::shared_ptr<T> MakeFieldReal32Quant(RNTupleModel &model,
19+
std::string_view name,
20+
int nBits, double min, double max) {
21+
assert(nBits >= 2 && nBits < 32);
22+
assert(max > min);
23+
auto field = std::make_unique<RField<T>>(name);
24+
field->SetQuantized(min, max, nBits);
25+
model.AddField(std::move(field));
26+
return model.GetDefaultEntry().GetPtr<T>(name);
27+
}
28+
29+
void write(std::string_view filename = "types.fundamental.real32quant.root") {
30+
auto model = RNTupleModel::Create();
31+
32+
auto FloatReal32Quant2_Min1_1 =
33+
MakeFieldReal32Quant<float>(*model, "FloatReal32Quant2_-1_1", 2, -1, 1);
34+
auto FloatReal32Quant8_0_1 =
35+
MakeFieldReal32Quant<float>(*model, "FloatReal32Quant8_0_1", 8, 0, 1);
36+
auto FloatReal32Quant16_0_1 =
37+
MakeFieldReal32Quant<float>(*model, "FloatReal32Quant16_0_1", 16, 0, 1);
38+
auto DoubleReal32Quant2_Min1_1 =
39+
MakeFieldReal32Quant<double>(*model, "DoubleReal32Quant2_-1_1", 2, -1, 1);
40+
auto DoubleReal32Quant20_Min1_1 =
41+
MakeFieldReal32Quant<double>(*model, "DoubleReal32Quant20_-1_1", 20, -1, 1);
42+
auto DoubleReal32Quant31_Min100_25 =
43+
MakeFieldReal32Quant<double>(*model, "DoubleReal32Quant31_-100_25", 31, -100, 25);
44+
45+
RNTupleWriteOptions options;
46+
options.SetCompression(0);
47+
auto writer =
48+
RNTupleWriter::Recreate(std::move(model), "ntpl", filename, options);
49+
50+
// First entry: ascending values
51+
*FloatReal32Quant2_Min1_1 = -1.0f;
52+
*DoubleReal32Quant2_Min1_1 = -0.5;
53+
*DoubleReal32Quant20_Min1_1 = 0.25;
54+
*FloatReal32Quant8_0_1 = 0.5f;
55+
*FloatReal32Quant16_0_1 = 1.0f;
56+
*DoubleReal32Quant31_Min100_25 = 2.0;
57+
writer->Fill();
58+
59+
// Second entry: a value containing all 1s in the mantissa
60+
static constexpr float ValueAllOnes = 2.3509885e-38f;
61+
*FloatReal32Quant2_Min1_1 = ValueAllOnes;
62+
*DoubleReal32Quant2_Min1_1 = ValueAllOnes;
63+
*DoubleReal32Quant20_Min1_1 = ValueAllOnes;
64+
*FloatReal32Quant8_0_1 = ValueAllOnes;
65+
*FloatReal32Quant16_0_1 = ValueAllOnes;
66+
*DoubleReal32Quant31_Min100_25 = ValueAllOnes;
67+
writer->Fill();
68+
69+
// Third entry: middle values of the value range
70+
*FloatReal32Quant2_Min1_1 = 0;
71+
*DoubleReal32Quant2_Min1_1 = 0;
72+
*DoubleReal32Quant20_Min1_1 = 0;
73+
*FloatReal32Quant8_0_1 = 0.5;
74+
*FloatReal32Quant16_0_1 = 0.5;
75+
*DoubleReal32Quant31_Min100_25 = -37.5;
76+
writer->Fill();
77+
78+
// Fourth entry: min values
79+
*FloatReal32Quant2_Min1_1 = -1;
80+
*DoubleReal32Quant2_Min1_1 = -1;
81+
*DoubleReal32Quant20_Min1_1 = -1;
82+
*FloatReal32Quant8_0_1 = 0;
83+
*FloatReal32Quant16_0_1 = 0;
84+
*DoubleReal32Quant31_Min100_25 = -100;
85+
writer->Fill();
86+
87+
// Fifth entry: max values
88+
*FloatReal32Quant2_Min1_1 = 1;
89+
*DoubleReal32Quant2_Min1_1 = 1;
90+
*DoubleReal32Quant20_Min1_1 = 1;
91+
*FloatReal32Quant8_0_1 = 1;
92+
*FloatReal32Quant16_0_1 = 1;
93+
*DoubleReal32Quant31_Min100_25 = 25;
94+
writer->Fill();
95+
}

0 commit comments

Comments
 (0)