Adding a Field to an RNTupleModel with a Runtime-Determined Type

Hello Root,

I’m working on adding fields to an RNTupleModel using AddField, which requires an RFieldBase. However, I don’t know the field type at compile time.

This works well for vectors, but I’m struggling to find a similar solution for primitive types. I see that RField exists, but it requires specifying a type at compile time, is there any way to do this without having to write a switch over every type?

Here’s how I currently add a vector field, and I would like to do something similar for primitive types:

auto fieldName = "fieldName";
auto fieldTypeName = "std::vector<float>";

auto result = RVectorField::Create(fieldName, fieldTypeName);

auto vectorField = RVectorField::CreateUntyped(fieldName, result.Unwrap());
 

Cheers,
Nikolaj


ROOT Version: 6.33.01
Platform: Not Provided
Compiler: Not Provided


1 Like

Hi @Krogh, have you tried using RFieldBase::Create?

auto fieldName = "foo";
auto fieldTypeName = "int";
auto fieldRes = RFieldBase::Create(fieldName, fieldTypeName);
if (fieldRes) {
  auto field = fieldRes.Unwrap();
  // ...
}

Thanks, that solved it!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.