Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion tree/dataframe/src/RDFSnapshotHelpers.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,18 @@ void ROOT::Internal::RDF::UntypedSnapshotRNTupleHelper::Initialize()
? ROOT::Internal::RDF::GetTypeNameWithOpts(*fInputLoopManager->GetDataSource(),
fInputFieldNames[i], fOptions.fVector2RVec)
: ROOT::Internal::RDF::TypeID2TypeName(*fInputColumnTypeIDs[i]);
model->AddField(ROOT::RFieldBase::Create(fOutputFieldNames[i], typeName).Unwrap());

// Cardinality fields are read-only, so instead we snapshot them as their inner type.
if (typeName.substr(0, 25) == "ROOT::RNTupleCardinality<") {
// Get "T" from "ROOT::RNTupleCardinality<T>".
std::string cardinalityType = typeName.substr(25, typeName.size() - 26);
Warning("Snapshot",
"Column \"%s\" is a read-only \"%s\" column. It will be snapshot as its inner type \"%s\" instead.",
fInputFieldNames[i].c_str(), typeName.c_str(), cardinalityType.c_str());
model->AddField(ROOT::RFieldBase::Create(fOutputFieldNames[i], cardinalityType).Unwrap());
} else {
model->AddField(ROOT::RFieldBase::Create(fOutputFieldNames[i], typeName).Unwrap());
}
fFieldTokens[i] = model->GetToken(fOutputFieldNames[i]);
}
model->Freeze();
Expand Down
34 changes: 34 additions & 0 deletions tree/dataframe/test/dataframe_snapshot_ntuple.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,40 @@ TEST(RDFSnapshotRNTuple, TDirectory)
EXPECT_EQ(expected, sdf.GetColumnNames());
}

TEST(RDFSnapshotRNTuple, CardinalityColumns)
{
FileRAII fileGuard{"RDFSnapshotRNTuple_cardinality_columns.root"};

{
auto model = ROOT::RNTupleModel::Create();

model->MakeField<std::vector<Electron>>("electron");

auto cardinalityFld = std::make_unique<ROOT::RField<ROOT::RNTupleCardinality<std::uint32_t>>>("nElectrons");
model->AddProjectedField(std::move(cardinalityFld), [](const std::string &) { return "electron"; });

auto writer = ROOT::RNTupleWriter::Recreate(std::move(model), "ntuple", fileGuard.GetPath());
auto electron = writer->GetModel().GetDefaultEntry().GetPtr<std::vector<Electron>>("electron");

for (unsigned i = 0; i < 5; ++i) {
*electron = {Electron{1.f * i}, Electron{2.f * i}, Electron{3.f * i}};
writer->Fill();
}
}

ROOT::RDF::RSnapshotOptions opts;
opts.fMode = "UPDATE";
opts.fOutputFormat = ROOT::RDF::ESnapshotOutputFormat::kRNTuple;
ROOT::RDataFrame df("ntuple", fileGuard.GetPath());

ROOT_EXPECT_WARNING(df.Snapshot("ntuple_snap", fileGuard.GetPath(), "", opts), "Snapshot",
"Column \"nElectrons\" is a read-only \"ROOT::RNTupleCardinality<std::uint32_t>\" column. It "
"will be snapshot as its inner type \"std::uint32_t\" instead.");

ROOT::RDataFrame sdf("ntuple_snap", fileGuard.GetPath());
EXPECT_EQ("std::uint32_t", sdf.GetColumnType("nElectrons"));
}

class RDFSnapshotRNTupleFromTTreeTest : public ::testing::Test {
protected:
const std::string fFileName = "RDFSnapshotRNTuple_ttree_fixture.root";
Expand Down
Loading