Logo ROOT  
Reference Guide
RNTupleModel.hxx
Go to the documentation of this file.
1 /// \file ROOT/RNTupleModel.hxx
2 /// \ingroup NTuple ROOT7
3 /// \author Jakob Blomer <jblomer@cern.ch>
4 /// \date 2018-10-04
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 #ifndef ROOT7_RNTupleModel
17 #define ROOT7_RNTupleModel
18 
19 #include <ROOT/REntry.hxx>
20 #include <ROOT/RField.hxx>
21 #include <ROOT/RNTupleUtil.hxx>
22 #include <ROOT/RFieldValue.hxx>
23 #include <ROOT/RStringView.hxx>
24 
25 #include <memory>
26 #include <unordered_set>
27 #include <utility>
28 
29 namespace ROOT {
30 namespace Experimental {
31 
32 class RCollectionNTuple;
33 
34 // clang-format off
35 /**
36 \class ROOT::Experimental::RNTupleModel
37 \ingroup NTuple
38 \brief The RNTupleModel encapulates the schema of an ntuple.
39 
40 The ntuple model comprises a collection of hierarchically organized fields. From a frozen model, "entries"
41 can be extracted. For convenience, the model provides a default entry. Models have a unique model identifier
42 that faciliates checking whether entries are compatible with it (i.e.: have been extracted from that model).
43 A model needs to be frozen before it can be used to create a live ntuple.
44 */
45 // clang-format on
46 class RNTupleModel {
47  /// Hierarchy of fields consisting of simple types and collections (sub trees)
48  std::unique_ptr<RFieldZero> fFieldZero;
49  /// Contains field values corresponding to the created top-level fields
50  std::unique_ptr<REntry> fDefaultEntry;
51  /// Keeps track of which field names are taken.
52  std::unordered_set<std::string> fFieldNames;
53 
54  /// Checks that user-provided field names are valid in the context
55  /// of this NTuple model. Throws an RException for invalid names.
56  void EnsureValidFieldName(std::string_view fieldName);
57 
58 public:
59  RNTupleModel();
60  RNTupleModel(const RNTupleModel&) = delete;
62  ~RNTupleModel() = default;
63 
64  std::unique_ptr<RNTupleModel> Clone() const;
65  static std::unique_ptr<RNTupleModel> Create() { return std::make_unique<RNTupleModel>(); }
66 
67  /// Creates a new field and a corresponding tree value that is managed by a shared pointer.
68  template <typename T, typename... ArgsT>
69  std::shared_ptr<T> MakeField(std::string_view fieldName, ArgsT&&... args) {
70  EnsureValidFieldName(fieldName);
71  auto field = std::make_unique<RField<T>>(fieldName);
72  auto ptr = fDefaultEntry->AddValue<T>(field.get(), std::forward<ArgsT>(args)...);
73  fFieldZero->Attach(std::move(field));
74  return ptr;
75  }
76 
77  /// Adds a field whose type is not known at compile time. Thus there is no shared pointer returned.
78  void AddField(std::unique_ptr<Detail::RFieldBase> field);
79 
80  template <typename T>
81  void AddField(std::string_view fieldName, T* fromWhere) {
82  EnsureValidFieldName(fieldName);
83  auto field = std::make_unique<RField<T>>(fieldName);
84  fDefaultEntry->CaptureValue(field->CaptureValue(fromWhere));
85  fFieldZero->Attach(std::move(field));
86  }
87 
88  template <typename T>
89  T* Get(std::string_view fieldName) {
90  return fDefaultEntry->Get<T>(fieldName);
91  }
92 
93  /// Ingests a model for a sub collection and attaches it to the current model
94  std::shared_ptr<RCollectionNTuple> MakeCollection(
95  std::string_view fieldName,
96  std::unique_ptr<RNTupleModel> collectionModel);
97 
98  RFieldZero *GetFieldZero() const { return fFieldZero.get(); }
99  REntry *GetDefaultEntry() { return fDefaultEntry.get(); }
100  std::unique_ptr<REntry> CreateEntry();
102  std::string GetDescription() const { return ""; /* TODO */ }
103  RNTupleUuid GetUuid() const { return RNTupleUuid(); /* TODO */ }
104 };
105 
106 } // namespace Experimental
107 } // namespace ROOT
108 
109 #endif
ROOT::Experimental::RNTupleModel::GetUuid
RNTupleUuid GetUuid() const
Definition: RNTupleModel.hxx:103
ROOT::Experimental::RNTupleModel::GetVersion
RNTupleVersion GetVersion() const
Definition: RNTupleModel.hxx:101
ROOT::Experimental::RNTupleModel::CreateEntry
std::unique_ptr< REntry > CreateEntry()
Definition: RNTupleModel.cxx:72
ROOT::Experimental::RNTupleModel::MakeField
std::shared_ptr< T > MakeField(std::string_view fieldName, ArgsT &&... args)
Creates a new field and a corresponding tree value that is managed by a shared pointer.
Definition: RNTupleModel.hxx:69
RField.hxx
string_view
basic_string_view< char > string_view
Definition: libcpp_string_view.h:785
ROOT::Experimental::RNTupleModel::Clone
std::unique_ptr< RNTupleModel > Clone() const
Definition: RNTupleModel.cxx:43
ROOT::Experimental::RNTupleModel::AddField
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.
Definition: RNTupleModel.cxx:53
ROOT::Experimental::RNTupleModel::MakeCollection
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.
Definition: RNTupleModel.cxx:61
ROOT::Experimental::REntry
The REntry is a collection of values in an ntuple corresponding to a complete row in the data set.
Definition: REntry.hxx:42
ROOT::Experimental::RNTupleUuid
std::string RNTupleUuid
Every NTuple is identified by a UUID. TODO(jblomer): should this be a TUUID?
Definition: RNTupleUtil.hxx:120
ROOT::Experimental::RNTupleModel::fDefaultEntry
std::unique_ptr< REntry > fDefaultEntry
Contains field values corresponding to the created top-level fields.
Definition: RNTupleModel.hxx:50
ROOT::Experimental::RNTupleVersion
For forward and backward compatibility, attach version information to the consitituents of the file f...
Definition: RNTupleUtil.hxx:127
ROOT::Experimental::RNTupleModel::GetDefaultEntry
REntry * GetDefaultEntry()
Definition: RNTupleModel.hxx:99
RStringView.hxx
ROOT::Experimental::RNTupleModel::fFieldNames
std::unordered_set< std::string > fFieldNames
Keeps track of which field names are taken.
Definition: RNTupleModel.hxx:52
ROOT::Experimental::RNTupleModel::fFieldZero
std::unique_ptr< RFieldZero > fFieldZero
Hierarchy of fields consisting of simple types and collections (sub trees)
Definition: RNTupleModel.hxx:48
ROOT::Experimental::RNTupleModel
The RNTupleModel encapulates the schema of an ntuple.
Definition: RNTupleModel.hxx:46
ROOT::Experimental::RNTupleModel::AddField
void AddField(std::string_view fieldName, T *fromWhere)
Definition: RNTupleModel.hxx:81
RNTupleUtil.hxx
ROOT::Experimental::RNTupleModel::Create
static std::unique_ptr< RNTupleModel > Create()
Definition: RNTupleModel.hxx:65
ROOT::Experimental::RFieldZero
The container field for an ntuple model, which itself has no physical representation.
Definition: RField.hxx:260
ROOT::Experimental::RNTupleModel::EnsureValidFieldName
void EnsureValidFieldName(std::string_view fieldName)
Checks that user-provided field names are valid in the context of this NTuple model.
Definition: RNTupleModel.cxx:26
ROOT::Experimental::RNTupleModel::GetDescription
std::string GetDescription() const
Definition: RNTupleModel.hxx:102
ROOT::Math::Chebyshev::T
double T(double x)
Definition: ChebyshevPol.h:34
ROOT::Experimental::RNTupleModel::operator=
RNTupleModel & operator=(const RNTupleModel &)=delete
REntry.hxx
ROOT::Experimental::RNTupleModel::RNTupleModel
RNTupleModel()
Definition: RNTupleModel.cxx:38
ROOT::Experimental::RNTupleModel::~RNTupleModel
~RNTupleModel()=default
ROOT::Experimental::RNTupleModel::Get
T * Get(std::string_view fieldName)
Definition: RNTupleModel.hxx:89
ROOT::Experimental::RNTupleModel::RNTupleModel
RNTupleModel(const RNTupleModel &)=delete
ROOT
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
Definition: EExecutionPolicy.hxx:4
RFieldValue.hxx
ROOT::Experimental::RNTupleModel::GetFieldZero
RFieldZero * GetFieldZero() const
Definition: RNTupleModel.hxx:98