Skip to content

Commit

Permalink
[ntuple] remove RField<ClusterSize_t> from RNTupleCollectionView
Browse files Browse the repository at this point in the history
  • Loading branch information
jblomer committed Oct 10, 2024
1 parent 1c358d4 commit 04fd37f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 25 deletions.
12 changes: 12 additions & 0 deletions tree/ntuple/v7/inc/ROOT/RField.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,18 @@ public:
/// It is used in the templated RField<RNTupleCardinality<SizeT>> form, which represents the collection sizes either
/// as 32bit unsigned int (std::uint32_t) or as 64bit unsigned int (std::uint64_t).
class RCardinalityField : public RFieldBase {
friend class RNTupleCollectionView; // to access GetCollectionInfo()

private:
void GetCollectionInfo(NTupleSize_t globalIndex, RClusterIndex *collectionStart, ClusterSize_t *size)
{
fPrincipalColumn->GetCollectionInfo(globalIndex, collectionStart, size);
}
void GetCollectionInfo(RClusterIndex clusterIndex, RClusterIndex *collectionStart, ClusterSize_t *size)
{
fPrincipalColumn->GetCollectionInfo(clusterIndex, collectionStart, size);
}

protected:
RCardinalityField(std::string_view fieldName, std::string_view typeName)
: RFieldBase(fieldName, typeName, ENTupleStructure::kLeaf, false /* isSimple */)
Expand Down
48 changes: 23 additions & 25 deletions tree/ntuple/v7/inc/ROOT/RNTupleView.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -307,35 +307,36 @@ public:
\brief A view for a collection, that can itself generate new ntuple views for its nested fields.
*/
// clang-format on
class RNTupleCollectionView : public RNTupleView<ClusterSize_t> {
class RNTupleCollectionView {
friend class RNTupleReader;

private:
Internal::RPageSource *fSource;
RField<RNTupleCardinality<std::uint64_t>> fField;
RFieldBase::RValue fValue;

RNTupleCollectionView(std::unique_ptr<RFieldBase> field, Internal::RPageSource *source)
: RNTupleView<ClusterSize_t>(std::move(field)), fSource(source)
{}
RNTupleCollectionView(DescriptorId_t fieldId, const std::string &fieldName, Internal::RPageSource *source)
: fSource(source), fField(fieldName), fValue(fField.CreateValue())
{
fField.SetOnDiskId(fieldId);
Internal::CallConnectPageSourceOnField(fField, *source);
}

static RNTupleCollectionView Create(DescriptorId_t fieldId, Internal::RPageSource *source)
{
std::unique_ptr<RFieldBase> field;
std::string fieldName;
{
const auto &desc = source->GetSharedDescriptorGuard().GetRef();
const auto &fieldDesc = desc.GetFieldDescriptor(fieldId);
if (fieldDesc.GetStructure() != ENTupleStructure::kCollection) {
throw RException(
R__FAIL("invalid attemt to create collection view on non-collection field " + fieldDesc.GetFieldName()));
}
field = std::make_unique<RField<ClusterSize_t>>(fieldDesc.GetFieldName());
fieldName = fieldDesc.GetFieldName();
}
field->SetOnDiskId(fieldId);
Internal::CallConnectPageSourceOnField(*field, *source);
return RNTupleCollectionView(std::move(field), source);
return RNTupleCollectionView(fieldId, fieldName, source);
}

RField<ClusterSize_t> *AsClusterSizeField() { return static_cast<RField<ClusterSize_t> *>(fField.get()); }

public:
RNTupleCollectionView(const RNTupleCollectionView &other) = delete;
RNTupleCollectionView(RNTupleCollectionView &&other) = default;
Expand All @@ -346,15 +347,15 @@ public:
RNTupleClusterRange GetCollectionRange(NTupleSize_t globalIndex) {
ClusterSize_t size;
RClusterIndex collectionStart;
AsClusterSizeField()->GetCollectionInfo(globalIndex, &collectionStart, &size);
fField.GetCollectionInfo(globalIndex, &collectionStart, &size);
return RNTupleClusterRange(collectionStart.GetClusterId(), collectionStart.GetIndex(),
collectionStart.GetIndex() + size);
}
RNTupleClusterRange GetCollectionRange(RClusterIndex clusterIndex)
{
ClusterSize_t size;
RClusterIndex collectionStart;
AsClusterSizeField()->GetCollectionInfo(clusterIndex, &collectionStart, &size);
fField.GetCollectionInfo(clusterIndex, &collectionStart, &size);
return RNTupleClusterRange(collectionStart.GetClusterId(), collectionStart.GetIndex(),
collectionStart.GetIndex() + size);
}
Expand All @@ -363,7 +364,7 @@ public:
template <typename T>
RNTupleView<T> GetView(std::string_view fieldName)
{
auto fieldId = fSource->GetSharedDescriptorGuard()->FindFieldId(fieldName, fField->GetOnDiskId());
auto fieldId = fSource->GetSharedDescriptorGuard()->FindFieldId(fieldName, fField.GetOnDiskId());
if (fieldId == kInvalidDescriptorId) {
throw RException(R__FAIL("no field named '" + std::string(fieldName) + "' in RNTuple '" +
fSource->GetSharedDescriptorGuard()->GetName() + "'"));
Expand All @@ -374,27 +375,24 @@ public:
/// Raises an exception if there is no field with the given name.
RNTupleCollectionView GetCollectionView(std::string_view fieldName)
{
auto fieldId = fSource->GetSharedDescriptorGuard()->FindFieldId(fieldName, fField->GetOnDiskId());
auto fieldId = fSource->GetSharedDescriptorGuard()->FindFieldId(fieldName, fField.GetOnDiskId());
if (fieldId == kInvalidDescriptorId) {
throw RException(R__FAIL("no field named '" + std::string(fieldName) + "' in RNTuple '" +
fSource->GetSharedDescriptorGuard()->GetName() + "'"));
}
return RNTupleCollectionView::Create(fieldId, fSource);
}

ClusterSize_t operator()(NTupleSize_t globalIndex) {
ClusterSize_t size;
RClusterIndex collectionStart;
AsClusterSizeField()->GetCollectionInfo(globalIndex, &collectionStart, &size);
return size;
std::uint64_t operator()(NTupleSize_t globalIndex)
{
fValue.Read(globalIndex);
return fValue.GetRef<std::uint64_t>();
}

ClusterSize_t operator()(RClusterIndex clusterIndex)
std::uint64_t operator()(RClusterIndex clusterIndex)
{
ClusterSize_t size;
RClusterIndex collectionStart;
AsClusterSizeField()->GetCollectionInfo(clusterIndex, &collectionStart, &size);
return size;
fValue.Read(clusterIndex);
return fValue.GetRef<std::uint64_t>();
}
};

Expand Down

0 comments on commit 04fd37f

Please sign in to comment.