Skip to content

Commit e9ddf09

Browse files
WIP: trying to load rntuple into a jagged array data structure
1 parent 192c835 commit e9ddf09

File tree

2 files changed

+77
-11
lines changed

2 files changed

+77
-11
lines changed

examples/common/ttjet_13tev_june2019.hpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
using bit = bool;
99
using byte = unsigned char;
10-
using Index = std::uint64_t;
10+
using Index = std::uint32_t;
1111

1212
// clang-format off
1313
struct run {};
@@ -1538,7 +1538,7 @@ using Electron = llama::Record<
15381538
llama::Field<Electron_pdgId, std::int32_t>,
15391539
llama::Field<Electron_photonIdx, std::int32_t>,
15401540
llama::Field<Electron_tightCharge, std::int32_t>,
1541-
llama::Field<Electron_vidNestedWPbitmap, std::int32_t>,
1541+
//llama::Field<Electron_vidNestedWPbitmap, std::int32_t>,
15421542
llama::Field<Electron_convVeto, bit>,
15431543
llama::Field<Electron_cutBased_HEEP, bit>,
15441544
llama::Field<Electron_isPFcand, bit>,
@@ -1947,6 +1947,7 @@ using Event = llama::Record<
19471947
llama::Field<ChsMET_sumEt, float>,
19481948
//llama::Field<nCorrT1METJet, Index>,
19491949
//llama::Field<nElectron, Index>,
1950+
llama::Field<nElectron, Electron[]>,
19501951
llama::Field<Flag_ecalBadCalibFilterV2, bit>,
19511952
//llama::Field<nFatJet, Index>,
19521953
//llama::Field<nGenJetAK8, Index>,

examples/hep_rntuple/hep_rntuple.cpp

+74-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// This example uses a non-public CMS NanoAOD file called: ttjet_13tev_june2019_lzma.
2-
// Please ask contact us if you need it.
2+
// Please contact us if you need it.
33

44
#include "../common/ttjet_13tev_june2019.hpp"
55

@@ -14,6 +14,8 @@
1414
#include <llama/DumpMapping.hpp>
1515
#include <llama/llama.hpp>
1616

17+
using SmallEvent = boost::mp11::mp_take_c<Event, 100>;
18+
1719
int main(int argc, const char* argv[])
1820
{
1921
if (argc != 2)
@@ -25,27 +27,90 @@ int main(int argc, const char* argv[])
2527
using namespace std::chrono;
2628
using namespace ROOT::Experimental;
2729

30+
// auto ntuple
31+
// = RNTupleReader::Open(RNTupleModel::Create(), "NTuple", "/mnt/c/dev/llama/ttjet_13tev_june2019_lzma.root");
2832
auto ntuple = RNTupleReader::Open(RNTupleModel::Create(), "NTuple", argv[1]);
29-
const auto n = ntuple->GetNEntries();
33+
// try
34+
//{
35+
// ntuple->PrintInfo(ROOT::Experimental::ENTupleInfo::kStorageDetails);
36+
//}
37+
// catch (const std::exception& e)
38+
//{
39+
// fmt::print("PrintInfo error: {}", e.what());
40+
//}
41+
const auto eventCount = ntuple->GetNEntries();
42+
const auto& d = ntuple->GetDescriptor();
43+
const auto electronCount
44+
= d.GetNElements(d.FindColumnId(d.FindFieldId("nElectron.nElectron.Electron_deltaEtaSC"), 0));
45+
fmt::print("File contains {} events with {} electrons\n", eventCount, electronCount);
3046

3147
auto start = steady_clock::now();
32-
auto view = llama::allocView(llama::mapping::SoA<llama::ArrayDims<1>, Event, true>{llama::ArrayDims{n}});
48+
auto mapping = llama::mapping::OffsetTable<llama::ArrayDims<1>, SmallEvent>{
49+
llama::ArrayDims{eventCount},
50+
llama::ArrayDims{electronCount}};
51+
auto view = llama::allocView(mapping);
3352
fmt::print("Alloc LLAMA view: {}ms\n", duration_cast<milliseconds>(steady_clock::now() - start).count());
3453

3554
std::size_t totalSize = 0;
3655
for (auto i = 0u; i < view.mapping.blobCount; i++)
3756
totalSize += view.mapping.blobSize(i);
3857
fmt::print("Total LLAMA view memory: {}MiB in {} blobs\n", totalSize / 1024 / 1024, view.mapping.blobCount);
3958

59+
// fill offset table
4060
start = steady_clock::now();
41-
llama::forEachLeaf<Event>(
61+
std::size_t offset = 0;
62+
auto electronViewCollection = ntuple->GetViewCollection("nElectron");
63+
for (std::size_t i = 0; i < eventCount; i++)
64+
{
65+
offset += electronViewCollection(i);
66+
view(i)(llama::EndOffset<nElectron>{}) = offset;
67+
assert(offset <= electronCount);
68+
}
69+
fmt::print("Fill offset table: {}ms\n", duration_cast<milliseconds>(steady_clock::now() - start).count());
70+
71+
using AugmentedSmallEvent = typename decltype(mapping)::RecordDim;
72+
start = steady_clock::now();
73+
llama::forEachLeaf<AugmentedSmallEvent>(
4274
[&](auto coord)
4375
{
44-
using Name = llama::GetTag<Event, decltype(coord)>;
45-
using Type = llama::GetType<Event, decltype(coord)>;
46-
auto column = ntuple->GetView<Type>(llama::structName<Name>());
47-
for (std::size_t i = 0; i < n; i++)
48-
view(i)(coord) = column(i);
76+
using Coord = decltype(coord);
77+
using LeafTag = llama::GetTag<AugmentedSmallEvent, Coord>;
78+
using Type = llama::GetType<AugmentedSmallEvent, Coord>;
79+
80+
fmt::print("Copying {}\n", llama::structName<LeafTag>());
81+
if constexpr (
82+
!llama::mapping::internal::isEndOffsetField<LeafTag> && !llama::mapping::internal::isSizeField<LeafTag>)
83+
{
84+
if constexpr (boost::mp11::mp_contains<typename Coord::List, boost::mp11::mp_size_t<llama::dynamic>>::
85+
value)
86+
{
87+
using Before = llama::mapping::internal::BeforeDynamic<Coord>;
88+
using BeforeBefore = llama::RecordCoordFromList<boost::mp11::mp_pop_front<typename Before::List>>;
89+
using After = llama::mapping::internal::AfterDynamic<Coord>;
90+
using SubCollectionTag = llama::GetTag<AugmentedSmallEvent, Before>;
91+
92+
auto collectionColumn = ntuple->GetViewCollection(llama::structName<SubCollectionTag>());
93+
auto column = collectionColumn.template GetView<Type>(
94+
llama::structName<SubCollectionTag>() + "." + llama::structName<LeafTag>());
95+
for (std::size_t i = 0; i < eventCount; i++)
96+
{
97+
const auto subCollectionCount = view(i)(BeforeBefore{})(llama::Size<SubCollectionTag>{});
98+
assert(subCollectionCount == view(i)(llama::Size<nElectron>{}));
99+
for (std::size_t j = 0; j < subCollectionCount; j++)
100+
{
101+
const auto value = column(j);
102+
auto& dst = view(i)(Before{})(j) (After{});
103+
dst = value;
104+
}
105+
}
106+
}
107+
else
108+
{
109+
auto column = ntuple->GetView<Type>(llama::structName<LeafTag>());
110+
for (std::size_t i = 0; i < eventCount; i++)
111+
view(i)(coord) = column(i);
112+
}
113+
}
49114
});
50115
fmt::print("Copy RNTuple -> LLAMA view: {}ms\n", duration_cast<milliseconds>(steady_clock::now() - start).count());
51116

0 commit comments

Comments
 (0)