File indexing completed on 2025-09-17 09:14:31
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #ifndef ROOT_RNTupleReader
0015 #define ROOT_RNTupleReader
0016
0017 #include <ROOT/RConfig.hxx> // for R__unlikely
0018 #include <ROOT/REntry.hxx>
0019 #include <ROOT/RError.hxx>
0020 #include <ROOT/RNTupleDescriptor.hxx>
0021 #include <ROOT/RNTupleMetrics.hxx>
0022 #include <ROOT/RNTupleModel.hxx>
0023 #include <ROOT/RNTupleReadOptions.hxx>
0024 #include <ROOT/RNTupleUtil.hxx>
0025 #include <ROOT/RNTupleView.hxx>
0026 #include <ROOT/RPageStorage.hxx>
0027 #include <ROOT/RSpan.hxx>
0028
0029 #include <iostream>
0030 #include <iterator>
0031 #include <memory>
0032 #include <string>
0033 #include <string_view>
0034
0035 namespace ROOT {
0036 class RNTuple;
0037
0038
0039 enum class ENTupleInfo {
0040 kSummary,
0041 kStorageDetails,
0042 kMetrics,
0043 };
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066 class RNTupleReader {
0067 private:
0068
0069
0070 std::unique_ptr<Internal::RPageStorage::RTaskScheduler> fUnzipTasks;
0071
0072 std::unique_ptr<Internal::RPageSource> fSource;
0073
0074 std::unique_ptr<ROOT::RNTupleModel> fModel;
0075
0076
0077
0078 std::unique_ptr<RNTupleReader> fDisplayReader;
0079
0080
0081
0082
0083
0084 std::optional<ROOT::RNTupleDescriptor> fCachedDescriptor;
0085 Experimental::Detail::RNTupleMetrics fMetrics;
0086
0087 std::optional<ROOT::RNTupleDescriptor::RCreateModelOptions> fCreateModelOptions;
0088
0089 RNTupleReader(std::unique_ptr<ROOT::RNTupleModel> model, std::unique_ptr<Internal::RPageSource> source,
0090 const ROOT::RNTupleReadOptions &options);
0091
0092 explicit RNTupleReader(std::unique_ptr<Internal::RPageSource> source, const ROOT::RNTupleReadOptions &options);
0093
0094 void ConnectModel(ROOT::RNTupleModel &model);
0095 RNTupleReader *GetDisplayReader();
0096 void InitPageSource(bool enableMetrics);
0097
0098 ROOT::DescriptorId_t RetrieveFieldId(std::string_view fieldName) const;
0099
0100 public:
0101
0102 class RIterator {
0103 private:
0104 ROOT::NTupleSize_t fIndex = ROOT::kInvalidNTupleIndex;
0105
0106 public:
0107 using iterator = RIterator;
0108 using iterator_category = std::forward_iterator_tag;
0109 using value_type = ROOT::NTupleSize_t;
0110 using difference_type = ROOT::NTupleSize_t;
0111 using pointer = ROOT::NTupleSize_t *;
0112 using reference = ROOT::NTupleSize_t &;
0113
0114 RIterator() = default;
0115 explicit RIterator(ROOT::NTupleSize_t index) : fIndex(index) {}
0116 ~RIterator() = default;
0117
0118 iterator operator++(int)
0119 {
0120 auto r = *this;
0121 fIndex++;
0122 return r;
0123 }
0124 iterator &operator++()
0125 {
0126 ++fIndex;
0127 return *this;
0128 }
0129 reference operator*() { return fIndex; }
0130 pointer operator->() { return &fIndex; }
0131 bool operator==(const iterator &rh) const { return fIndex == rh.fIndex; }
0132 bool operator!=(const iterator &rh) const { return fIndex != rh.fIndex; }
0133 };
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147 static std::unique_ptr<RNTupleReader> Open(std::string_view ntupleName, std::string_view storage,
0148 const ROOT::RNTupleReadOptions &options = ROOT::RNTupleReadOptions());
0149 static std::unique_ptr<RNTupleReader>
0150 Open(const RNTuple &ntuple, const ROOT::RNTupleReadOptions &options = ROOT::RNTupleReadOptions());
0151
0152
0153 static std::unique_ptr<RNTupleReader> Open(std::unique_ptr<ROOT::RNTupleModel> model, std::string_view ntupleName,
0154 std::string_view storage,
0155 const ROOT::RNTupleReadOptions &options = ROOT::RNTupleReadOptions());
0156 static std::unique_ptr<RNTupleReader> Open(std::unique_ptr<ROOT::RNTupleModel> model, const RNTuple &ntuple,
0157 const ROOT::RNTupleReadOptions &options = ROOT::RNTupleReadOptions());
0158
0159
0160 static std::unique_ptr<RNTupleReader> Open(const ROOT::RNTupleDescriptor::RCreateModelOptions &createModelOpts,
0161 std::string_view ntupleName, std::string_view storage,
0162 const ROOT::RNTupleReadOptions &options = ROOT::RNTupleReadOptions());
0163 static std::unique_ptr<RNTupleReader> Open(const ROOT::RNTupleDescriptor::RCreateModelOptions &createModelOpts,
0164 const RNTuple &ntuple,
0165 const ROOT::RNTupleReadOptions &options = ROOT::RNTupleReadOptions());
0166 std::unique_ptr<RNTupleReader> Clone()
0167 {
0168 auto options = ROOT::RNTupleReadOptions{};
0169 options.SetEnableMetrics(fMetrics.IsEnabled());
0170 return std::unique_ptr<RNTupleReader>(new RNTupleReader(fSource->Clone(), options));
0171 }
0172 ~RNTupleReader();
0173
0174 ROOT::NTupleSize_t GetNEntries() const { return fSource->GetNEntries(); }
0175 const ROOT::RNTupleModel &GetModel();
0176 std::unique_ptr<ROOT::REntry> CreateEntry();
0177
0178
0179
0180 const ROOT::RNTupleDescriptor &GetDescriptor();
0181
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201
0202
0203
0204 void PrintInfo(const ENTupleInfo what = ENTupleInfo::kSummary, std::ostream &output = std::cout) const;
0205
0206
0207
0208
0209 void Show(ROOT::NTupleSize_t index, std::ostream &output = std::cout);
0210
0211
0212
0213 void LoadEntry(ROOT::NTupleSize_t index)
0214 {
0215
0216 if (R__unlikely(!fModel)) {
0217 fModel = fSource->GetSharedDescriptorGuard()->CreateModel(
0218 fCreateModelOptions.value_or(ROOT::RNTupleDescriptor::RCreateModelOptions{}));
0219 ConnectModel(*fModel);
0220 }
0221 LoadEntry(index, fModel->GetDefaultEntry());
0222 }
0223
0224 void LoadEntry(ROOT::NTupleSize_t index, ROOT::REntry &entry)
0225 {
0226 if (R__unlikely(entry.GetModelId() != fModel->GetModelId()))
0227 throw RException(R__FAIL("mismatch between entry and model"));
0228
0229 entry.Read(index);
0230 }
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244 ROOT::RNTupleGlobalRange GetEntryRange() { return ROOT::RNTupleGlobalRange(0, GetNEntries()); }
0245
0246
0247
0248
0249
0250
0251
0252
0253
0254
0255
0256
0257
0258
0259
0260
0261
0262
0263
0264
0265
0266
0267
0268
0269
0270 template <typename T>
0271 ROOT::RNTupleView<T> GetView(std::string_view fieldName)
0272 {
0273 return GetView<T>(RetrieveFieldId(fieldName));
0274 }
0275
0276
0277
0278
0279
0280
0281
0282
0283
0284
0285
0286
0287
0288
0289
0290
0291
0292
0293
0294
0295
0296
0297
0298
0299 template <typename T>
0300 ROOT::RNTupleView<T> GetView(std::string_view fieldName, std::shared_ptr<T> objPtr)
0301 {
0302 return GetView<T>(RetrieveFieldId(fieldName), objPtr);
0303 }
0304
0305
0306
0307
0308 template <typename T>
0309 ROOT::RNTupleView<T> GetView(std::string_view fieldName, T *rawPtr)
0310 {
0311 return GetView<T>(RetrieveFieldId(fieldName), rawPtr);
0312 }
0313
0314
0315
0316
0317 ROOT::RNTupleView<void> GetView(std::string_view fieldName, void *rawPtr, std::string_view typeName)
0318 {
0319 return GetView(RetrieveFieldId(fieldName), rawPtr, typeName);
0320 }
0321
0322
0323
0324
0325 ROOT::RNTupleView<void> GetView(std::string_view fieldName, void *rawPtr, const std::type_info &ti)
0326 {
0327 return GetView(RetrieveFieldId(fieldName), rawPtr, ROOT::Internal::GetRenormalizedDemangledTypeName(ti));
0328 }
0329
0330
0331
0332
0333 template <typename T>
0334 ROOT::RNTupleView<T> GetView(ROOT::DescriptorId_t fieldId)
0335 {
0336 auto field = ROOT::RNTupleView<T>::CreateField(fieldId, *fSource);
0337 auto range = ROOT::Internal::GetFieldRange(*field, *fSource);
0338 return ROOT::RNTupleView<T>(std::move(field), range);
0339 }
0340
0341
0342
0343
0344 template <typename T>
0345 ROOT::RNTupleView<T> GetView(ROOT::DescriptorId_t fieldId, std::shared_ptr<T> objPtr)
0346 {
0347 auto field = ROOT::RNTupleView<T>::CreateField(fieldId, *fSource);
0348 auto range = ROOT::Internal::GetFieldRange(*field, *fSource);
0349 return ROOT::RNTupleView<T>(std::move(field), range, objPtr);
0350 }
0351
0352
0353
0354
0355 template <typename T>
0356 ROOT::RNTupleView<T> GetView(ROOT::DescriptorId_t fieldId, T *rawPtr)
0357 {
0358 auto field = ROOT::RNTupleView<T>::CreateField(fieldId, *fSource);
0359 auto range = ROOT::Internal::GetFieldRange(*field, *fSource);
0360 return ROOT::RNTupleView<T>(std::move(field), range, rawPtr);
0361 }
0362
0363
0364
0365
0366
0367 ROOT::RNTupleView<void> GetView(ROOT::DescriptorId_t fieldId, void *rawPtr, std::string_view typeName)
0368 {
0369 auto field = RNTupleView<void>::CreateField(fieldId, *fSource, typeName);
0370 auto range = ROOT::Internal::GetFieldRange(*field, *fSource);
0371 return RNTupleView<void>(std::move(field), range, rawPtr);
0372 }
0373
0374
0375
0376
0377
0378 ROOT::RNTupleView<void> GetView(ROOT::DescriptorId_t fieldId, void *rawPtr, const std::type_info &ti)
0379 {
0380 return GetView(fieldId, rawPtr, ROOT::Internal::GetRenormalizedDemangledTypeName(ti));
0381 }
0382
0383
0384
0385
0386
0387
0388
0389 template <typename T>
0390 ROOT::RNTupleDirectAccessView<T> GetDirectAccessView(std::string_view fieldName)
0391 {
0392 return GetDirectAccessView<T>(RetrieveFieldId(fieldName));
0393 }
0394
0395
0396
0397
0398 template <typename T>
0399 ROOT::RNTupleDirectAccessView<T> GetDirectAccessView(ROOT::DescriptorId_t fieldId)
0400 {
0401 auto field = ROOT::RNTupleDirectAccessView<T>::CreateField(fieldId, *fSource);
0402 auto range = ROOT::Internal::GetFieldRange(field, *fSource);
0403 return ROOT::RNTupleDirectAccessView<T>(std::move(field), range);
0404 }
0405
0406
0407
0408
0409
0410
0411
0412
0413 ROOT::RNTupleCollectionView GetCollectionView(std::string_view fieldName)
0414 {
0415 auto fieldId = fSource->GetSharedDescriptorGuard()->FindFieldId(fieldName);
0416 if (fieldId == ROOT::kInvalidDescriptorId) {
0417 throw RException(R__FAIL("no field named '" + std::string(fieldName) + "' in RNTuple '" +
0418 fSource->GetSharedDescriptorGuard()->GetName() + "'"));
0419 }
0420 return GetCollectionView(fieldId);
0421 }
0422
0423
0424
0425
0426
0427 ROOT::RNTupleCollectionView GetCollectionView(ROOT::DescriptorId_t fieldId)
0428 {
0429 return ROOT::RNTupleCollectionView::Create(fieldId, fSource.get());
0430 }
0431
0432 RIterator begin() { return RIterator(0); }
0433 RIterator end() { return RIterator(GetNEntries()); }
0434
0435
0436
0437
0438
0439
0440
0441
0442
0443
0444
0445
0446
0447
0448
0449
0450
0451 void EnableMetrics() { fMetrics.Enable(); }
0452 const Experimental::Detail::RNTupleMetrics &GetMetrics() const { return fMetrics; }
0453 };
0454
0455 }
0456
0457 #endif