Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 <TError.h>
26 
27 #include <memory>
28 #include <utility>
29 
30 namespace ROOT {
31 namespace Experimental {
32 
33 class RCollectionNTuple;
34 
35 // clang-format off
36 /**
37 \class ROOT::Experimental::RNTupleModel
38 \ingroup NTuple
39 \brief The RNTupleModel encapulates the schema of an ntuple.
40 
41 The ntuple model comprises a collection of hierarchically organized fields. From a frozen model, "entries"
42 can be extracted. For convenience, the model provides a default entry. Models have a unique model identifier
43 that faciliates checking whether entries are compatible with it (i.e.: have been extracted from that model).
44 A model needs to be frozen before it can be used to create a live ntuple.
45 */
46 // clang-format on
47 class RNTupleModel {
48  /// Hierarchy of fields consisting of simple types and collections (sub trees)
49  std::unique_ptr<RFieldRoot> fRootField;
50  /// Contains field values corresponding to the created top-level fields
51  std::unique_ptr<REntry> fDefaultEntry;
52 
53 public:
54  RNTupleModel();
55  RNTupleModel(const RNTupleModel&) = delete;
56  RNTupleModel& operator =(const RNTupleModel&) = delete;
57  ~RNTupleModel() = default;
58 
59  RNTupleModel* Clone();
60  static std::unique_ptr<RNTupleModel> Create() { return std::make_unique<RNTupleModel>(); }
61 
62  /// Creates a new field and a corresponding tree value that is managed by a shared pointer.
63  template <typename T, typename... ArgsT>
64  std::shared_ptr<T> MakeField(std::string_view fieldName, ArgsT&&... args) {
65  auto field = std::make_unique<RField<T>>(fieldName);
66  auto ptr = fDefaultEntry->AddValue<T>(field.get(), std::forward<ArgsT>(args)...);
67  fRootField->Attach(std::move(field));
68  return ptr;
69  }
70 
71  /// Adds a field whose type is not known at compile time. Thus there is no shared pointer returned.
72  void AddField(std::unique_ptr<Detail::RFieldBase> field);
73 
74  template <typename T>
75  void AddField(std::string_view fieldName, T* fromWhere) {
76  auto field = std::make_unique<RField<T>>(fieldName);
77  fDefaultEntry->CaptureValue(field->CaptureValue(fromWhere));
78  fRootField->Attach(std::move(field));
79  }
80 
81  template <typename T>
82  T* Get(std::string_view fieldName) {
83  return fDefaultEntry->Get<T>(fieldName);
84  }
85 
86  /// Ingests a model for a sub collection and attaches it to the current model
87  std::shared_ptr<RCollectionNTuple> MakeCollection(
88  std::string_view fieldName,
89  std::unique_ptr<RNTupleModel> collectionModel);
90 
91  RFieldRoot *GetRootField() const { return fRootField.get(); }
92  REntry* GetDefaultEntry() { return fDefaultEntry.get(); }
93  std::unique_ptr<REntry> CreateEntry();
94  RNTupleVersion GetVersion() const { return RNTupleVersion(); }
95  std::string GetDescription() const { return ""; /* TODO */ }
96  RNTupleUuid GetUuid() const { return RNTupleUuid(); /* TODO */ }
97 };
98 
99 } // namespace Experimental
100 } // namespace ROOT
101 
102 #endif