Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RNTupleModel.cxx
Go to the documentation of this file.
1/// \file RNTupleModel.cxx
2/// \ingroup NTuple ROOT7
3/// \author Jakob Blomer <jblomer@cern.ch>
4/// \date 2018-10-15
5/// \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback
6/// is welcome!
7
8/*************************************************************************
9 * Copyright (C) 1995-2019, Rene Brun and Fons Rademakers. *
10 * All rights reserved. *
11 * *
12 * For the licensing terms see $ROOTSYS/LICENSE. *
13 * For the list of contributors see $ROOTSYS/README/CREDITS. *
14 *************************************************************************/
15
16#include <ROOT/RError.hxx>
17#include <ROOT/RField.hxx>
18#include <ROOT/RNTupleModel.hxx>
19#include <ROOT/RNTuple.hxx>
20
21#include <cstdlib>
22#include <memory>
23#include <utility>
24
25
27{
29 if (!nameValid) {
30 nameValid.Throw();
31 }
32 auto fieldNameStr = std::string(fieldName);
33 if (fFieldNames.insert(fieldNameStr).second == false) {
34 throw RException(R__FAIL("field name '" + fieldNameStr + "' already exists in NTuple model"));
35 }
36}
37
39 : fFieldZero(std::make_unique<RFieldZero>())
40 , fDefaultEntry(std::make_unique<REntry>())
41{}
42
43std::unique_ptr<ROOT::Experimental::RNTupleModel> ROOT::Experimental::RNTupleModel::Clone() const
44{
45 auto cloneModel = std::make_unique<RNTupleModel>();
46 auto cloneFieldZero = fFieldZero->Clone("");
47 cloneModel->fFieldZero = std::unique_ptr<RFieldZero>(static_cast<RFieldZero *>(cloneFieldZero.release()));
48 cloneModel->fDefaultEntry = cloneModel->fFieldZero->GenerateEntry();
49 return cloneModel;
50}
51
52
53void ROOT::Experimental::RNTupleModel::AddField(std::unique_ptr<Detail::RFieldBase> field)
54{
55 EnsureValidFieldName(field->GetName());
56 fDefaultEntry->AddValue(field->GenerateValue());
57 fFieldZero->Attach(std::move(field));
58}
59
60
61std::shared_ptr<ROOT::Experimental::RCollectionNTuple> ROOT::Experimental::RNTupleModel::MakeCollection(
62 std::string_view fieldName, std::unique_ptr<RNTupleModel> collectionModel)
63{
64 EnsureValidFieldName(fieldName);
65 auto collectionNTuple = std::make_shared<RCollectionNTuple>(std::move(collectionModel->fDefaultEntry));
66 auto field = std::make_unique<RCollectionField>(fieldName, collectionNTuple, std::move(collectionModel));
67 fDefaultEntry->CaptureValue(field->CaptureValue(collectionNTuple->GetOffsetPtr()));
68 fFieldZero->Attach(std::move(field));
69 return collectionNTuple;
70}
71
72std::unique_ptr<ROOT::Experimental::REntry> ROOT::Experimental::RNTupleModel::CreateEntry()
73{
74 auto entry = std::make_unique<REntry>();
75 for (auto& f : *fFieldZero) {
76 if (f.GetParent() != GetFieldZero())
77 continue;
78 entry->AddValue(f.GenerateValue());
79 }
80 return entry;
81}
#define R__FAIL(msg)
Short-hand to return an RResult<T> in an error state; the RError is implicitly converted into RResult...
Definition RError.hxx:292
#define f(i)
Definition RSha256.hxx:104
static RResult< void > EnsureValidFieldName(std::string_view fieldName)
Check whether a given string is a valid field name.
Definition RField.cxx:229
void Throw()
Throws an RException with fError.
Definition RError.cxx:69
The REntry is a collection of values in an ntuple corresponding to a complete row in the data set.
Definition REntry.hxx:42
Base class for all ROOT issued exceptions.
Definition RError.hxx:115
The container field for an ntuple model, which itself has no physical representation.
Definition RField.hxx:260
std::unordered_set< std::string > fFieldNames
Keeps track of which field names are taken.
void EnsureValidFieldName(std::string_view fieldName)
Checks that user-provided field names are valid in the context of this NTuple model.
std::shared_ptr< RCollectionNTuple > MakeCollection(std::string_view fieldName, std::unique_ptr< RNTupleModel > collectionModel)
Ingests a model for a sub collection and attaches it to the current model.
std::unique_ptr< RNTupleModel > Clone() const
std::unique_ptr< REntry > CreateEntry()
void AddField(std::unique_ptr< Detail::RFieldBase > field)
Adds a field whose type is not known at compile time. Thus there is no shared pointer returned.
The class is used as a return type for operations that can fail; wraps a value of type T or an RError...
Definition RError.hxx:196