Skip to content
Open
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
12 changes: 12 additions & 0 deletions tree/ntuple/inc/ROOT/RField.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,23 @@ public:
void AcceptVisitor(ROOT::Detail::RFieldVisitor &visitor) const final;
};

namespace Internal {
std::unique_ptr<RFieldBase> CreateEmulatedEnumField(std::string_view fieldName, std::string_view emulatedFromType,
std::string_view underlyingIntType);
}

/// The field for an unscoped or scoped enum with dictionary
class REnumField : public RFieldBase {
friend std::unique_ptr<RFieldBase> Internal::CreateEmulatedEnumField(std::string_view fieldName,
std::string_view emulatedFromType,
std::string_view underlyingIntType);

private:
REnumField(std::string_view fieldName, TEnum *enump);
// Used by CloneImpl()
REnumField(std::string_view fieldName, std::string_view enumName, std::unique_ptr<RFieldBase> intField);
// Used by field emulation
REnumField(std::string_view fieldName, std::string_view emulatedFromType, std::string_view underlyingIntType);

protected:
std::unique_ptr<RFieldBase> CloneImpl(std::string_view newName) const final;
Expand Down
8 changes: 8 additions & 0 deletions tree/ntuple/src/RFieldBase.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,14 @@ ROOT::RFieldBase::Create(const std::string &fieldName, const std::string &typeNa
ROOT::Internal::CreateEmulatedVectorField(fieldName, std::move(itemField), fieldDesc.GetTypeName());
vecField->fTypeAlias = fieldDesc.GetTypeAlias();
return vecField;
} else if (ROOT::Internal::IsCustomEnumFieldDesc(*desc, fieldDesc)) {
R__ASSERT(!fieldDesc.GetLinkIds().empty());
auto underlyingIntFieldId = fieldDesc.GetLinkIds()[0];
const auto &underlyingIntFieldDesc = desc->GetFieldDescriptor(underlyingIntFieldId);
auto enumField = ROOT::Internal::CreateEmulatedEnumField(fieldName, fieldDesc.GetTypeName(),
underlyingIntFieldDesc.GetTypeName());
enumField->fTypeAlias = fieldDesc.GetTypeAlias();
return enumField;
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions tree/ntuple/src/RFieldMeta.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -1120,6 +1120,13 @@ void ROOT::Experimental::RSoAField::AcceptVisitor(ROOT::Detail::RFieldVisitor &v

//------------------------------------------------------------------------------

std::unique_ptr<ROOT::RFieldBase> ROOT::Internal::CreateEmulatedEnumField(std::string_view fieldName,
std::string_view emulatedFromType,
std::string_view underlyingIntType)
{
return std::unique_ptr<RFieldBase>(new REnumField(fieldName, emulatedFromType, underlyingIntType));
}

ROOT::REnumField::REnumField(std::string_view fieldName, std::string_view enumName)
: REnumField(fieldName, EnsureValidEnum(enumName))
{
Expand Down Expand Up @@ -1160,6 +1167,15 @@ ROOT::REnumField::REnumField(std::string_view fieldName, std::string_view enumNa
fTraits |= kTraitTriviallyConstructible | kTraitTriviallyDestructible;
}

ROOT::REnumField::REnumField(std::string_view fieldName, std::string_view emulatedFromType,
std::string_view underlyingIntType)
: ROOT::RFieldBase(fieldName, emulatedFromType, ROOT::ENTupleStructure::kPlain, false /* isSimple */)
{
auto intField = Create("_0", std::string(underlyingIntType)).Unwrap();
Attach(std::move(intField));
fTraits |= kTraitTriviallyConstructible | kTraitTriviallyDestructible | kTraitEmulatedField;
}

std::unique_ptr<ROOT::RFieldBase> ROOT::REnumField::CloneImpl(std::string_view newName) const
{
auto newIntField = fSubfields[0]->Clone(fSubfields[0]->GetFieldName());
Expand Down
67 changes: 67 additions & 0 deletions tree/ntuple/test/ntuple_emulated.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,73 @@ TEST(RNTupleEmulated, CollectionProxy)
EXPECT_EQ(0u, vec->size());
}

TEST(RNTupleEmulated, EmulatedFields_Enum)
{
FileRaii fileGuard("test_ntuple_emulated_fields_enum.root");

ExecInFork([&] {
// The child process writes the file and exits, but the file must be preserved to be read by the parent.
fileGuard.PreserveFile();

ASSERT_TRUE(gInterpreter->Declare(R"(
enum class EmulatedEnum { kZero, kOne };
)"));

auto model = RNTupleModel::Create();
model->AddField(RFieldBase::Create("f", "EmulatedEnum").Unwrap());

auto writer = RNTupleWriter::Recreate(std::move(model), "ntpl", fileGuard.GetPath());
void *ptr = writer->GetModel().GetDefaultEntry().GetPtr<void>("f").get();
DeclarePointer("EmulatedEnum", "ptr", ptr);

ProcessLine("*ptr = EmulatedEnum::kOne");
writer->Fill();
});

auto reader = RNTupleReader::Open("ntpl", fileGuard.GetPath());
const auto &desc = reader->GetDescriptor();

{
RNTupleDescriptor::RCreateModelOptions opts;
opts.SetEmulateUnknownTypes(false);
try {
auto model = desc.CreateModel(opts);
FAIL() << "Creating a model without fEmulateUnknownTypes should fail";
} catch (const ROOT::RException &ex) {
ASSERT_THAT(ex.GetError().GetReport(), testing::HasSubstr("unknown type"));
}
}

{
RNTupleDescriptor::RCreateModelOptions opts;
opts.SetEmulateUnknownTypes(true);
auto model = desc.CreateModel(opts);
ASSERT_NE(model, nullptr);

const auto &e = model->GetConstField("f");
EXPECT_EQ(e.GetTypeName(), "EmulatedEnum");
EXPECT_TRUE(e.GetTraits() & ROOT::RFieldBase::kTraitEmulatedField);
EXPECT_EQ(e.GetStructure(), ROOT::ENTupleStructure::kPlain);

const auto underlyingIntField = e.GetConstSubfields()[0];
EXPECT_EQ(underlyingIntField->GetTypeName(), "std::int32_t");
}

RNTupleDescriptor::RCreateModelOptions cmOpts;
cmOpts.SetEmulateUnknownTypes(true);

std::unique_ptr<TFile> file(TFile::Open(fileGuard.GetPath().c_str()));
std::unique_ptr<ROOT::RNTuple> ntpl(file->Get<ROOT::RNTuple>("ntpl"));
reader = RNTupleReader::Open(cmOpts, *ntpl);
EXPECT_EQ(reader->GetNEntries(), 1);

std::int32_t value = 0;
auto e = reader->CreateEntry();
e->BindRawPtr<void>("f", &value);
reader->LoadEntry(0, *e);
EXPECT_EQ(1, value);
}

TEST(RNTupleEmulated, EmulatedFields_SoA)
{
FileRaii fileGuard("test_ntuple_emulated_fields_soa.root");
Expand Down
Loading