Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RSampleInfo.hxx
Go to the documentation of this file.
1// Author: Enrico Guiraud, 2021
2
3/*************************************************************************
4 * Copyright (C) 1995-2021, Rene Brun and Fons Rademakers. *
5 * All rights reserved. *
6 * *
7 * For the licensing terms see $ROOTSYS/LICENSE. *
8 * For the list of contributors see $ROOTSYS/README/CREDITS. *
9 *************************************************************************/
10
11#ifndef ROOT_RDF_RSAMPLEINFO
12#define ROOT_RDF_RSAMPLEINFO
13
14#include <ROOT/RDF/RSample.hxx>
15#include <string_view>
16#include <Rtypes.h>
17
18#include <functional>
19#include <stdexcept>
20#include <string>
21
22#include <tuple>
23
24namespace ROOT {
25namespace RDF {
26
27/// This type represents a sample identifier, to be used in conjunction with RDataFrame features such as
28/// \ref ROOT::RDF::RInterface<Proxied>::DefinePerSample "DefinePerSample()" and per-sample callbacks.
29///
30/// When the input data comes from a TTree, the string representation of RSampleInfo (which is returned by AsString()
31/// and that can be queried e.g. with Contains()) is of the form "<filename>/<treename>".
32///
33/// In multi-thread runs, different tasks might process different entry ranges of the same sample,
34/// so RSampleInfo also provides methods to inspect which part of a sample is being taken into consideration.
36 std::string fID;
37 std::pair<ULong64_t, ULong64_t> fEntryRange;
38
39 const ROOT::RDF::Experimental::RSample *fSample = nullptr; // non-owning
40 ULong64_t fTotalEntries = 0; // Number of entries in current file (if known).
41
42 void ThrowIfNoSample() const
43 {
44 if (fSample == nullptr) {
45 const auto msg = "RSampleInfo: sample data was requested but no samples are available.";
46 throw std::logic_error(msg);
47 }
48 }
49
50public:
51 RSampleInfo(std::string_view id, std::pair<ULong64_t, ULong64_t> entryRange,
54 {
55 }
56 RSampleInfo() = default;
57 RSampleInfo(const RSampleInfo &) = default;
58 RSampleInfo &operator=(const RSampleInfo &) = default;
59 RSampleInfo(RSampleInfo &&) = default;
61 ~RSampleInfo() = default;
62
63 /// @brief Get the name of the sample as a string.
64 const std::string &GetSampleName() const
65 {
67 return fSample->GetSampleName();
68 }
69
70 /// @brief Get the sample id as an int.
71 unsigned int GetSampleId() const
72 {
74 return fSample->GetSampleId();
75 }
76
77 /// @brief Return the metadata value of type int given the key.
78 int GetI(const std::string &key) const
79 {
81 return fSample->GetMetaData().GetI(key);
82 }
83
84 /// @brief Return the metadata value of type double given the key.
85 double GetD(const std::string &key) const
86 {
88 return fSample->GetMetaData().GetD(key);
89 }
90
91 /// @brief Return the metadata value of type string given the key.
92 std::string GetS(const std::string &key) const
93 {
95 return fSample->GetMetaData().GetS(key);
96 }
97
98 /// @brief Check whether the sample name contains the given substring.
99 bool Contains(std::string_view substr) const
100 {
101 // C++14 needs the conversion from std::string_view to std::string
102 return fID.find(std::string(substr)) != std::string::npos;
103 }
104
105 /// @brief Check whether the sample name is empty.
106 ///
107 /// This is the case e.g. when using a RDataFrame with no input data, constructed as `RDataFrame(nEntries)`.
108 bool Empty() const {
109 return fID.empty();
110 }
111
112 /// @brief Return a string representation of the sample name.
113 ///
114 /// The representation is of the form "<filename>/<treename>" if the input data comes from a TTree or a TChain.
115 const std::string &AsString() const
116 {
117 return fID;
118 }
119
120 /// @brief Return the entry range in the sample that is being taken into consideration.
121 ///
122 /// Multiple multi-threading tasks might process different entry ranges of the same sample.
123 std::pair<ULong64_t, ULong64_t> EntryRange() const { return fEntryRange; }
124
125 /// @brief Return the number of entries of this range of the sample.
126 ULong64_t NEntries() const { return fEntryRange.second - fEntryRange.first; }
127
128 /// Return the total number of entries in the underlying dataset.
129 /// If the total number of entries is not known, the end of the current range is returned.
130 /// This can be larger than NEntries() if the sample is split in multiple ranges.
131 ULong64_t NEntriesTotal() const { return std::max(fTotalEntries, fEntryRange.second); }
132
133 bool operator==(const RSampleInfo &other) const { return fID == other.fID; }
134 bool operator!=(const RSampleInfo &other) const { return !(*this == other); }
135};
136
137/// The type of a data-block callback, registered with an RDataFrame computation graph via e.g. \ref
138/// ROOT::RDF::RInterface<Proxied>::DefinePerSample "DefinePerSample()" or by certain actions (e.g. \ref
139/// ROOT::RDF::RInterface<Proxied>::Snapshot "Snapshot()").
140using SampleCallback_t = std::function<void(unsigned int, const ROOT::RDF::RSampleInfo &)>;
141
142} // namespace RDF
143} // namespace ROOT
144
145#endif
unsigned long long ULong64_t
Portable unsigned long integer 8 bytes.
Definition RtypesCore.h:84
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Class representing a sample which is a grouping of trees and their fileglobs, and,...
Definition RSample.hxx:39
This type represents a sample identifier, to be used in conjunction with RDataFrame features such as ...
unsigned int GetSampleId() const
Get the sample id as an int.
RSampleInfo(RSampleInfo &&)=default
void ThrowIfNoSample() const
bool Contains(std::string_view substr) const
Check whether the sample name contains the given substring.
bool operator==(const RSampleInfo &other) const
int GetI(const std::string &key) const
Return the metadata value of type int given the key.
std::string GetS(const std::string &key) const
Return the metadata value of type string given the key.
RSampleInfo(const RSampleInfo &)=default
RSampleInfo(std::string_view id, std::pair< ULong64_t, ULong64_t > entryRange, const ROOT::RDF::Experimental::RSample *sample=nullptr, ULong64_t totalEntries=0)
std::pair< ULong64_t, ULong64_t > fEntryRange
RSampleInfo & operator=(const RSampleInfo &)=default
double GetD(const std::string &key) const
Return the metadata value of type double given the key.
bool Empty() const
Check whether the sample name is empty.
const ROOT::RDF::Experimental::RSample * fSample
const std::string & AsString() const
Return a string representation of the sample name.
bool operator!=(const RSampleInfo &other) const
ULong64_t NEntries() const
Return the number of entries of this range of the sample.
ULong64_t NEntriesTotal() const
Return the total number of entries in the underlying dataset.
RSampleInfo & operator=(RSampleInfo &&)=default
const std::string & GetSampleName() const
Get the name of the sample as a string.
std::pair< ULong64_t, ULong64_t > EntryRange() const
Return the entry range in the sample that is being taken into consideration.
std::function< void(unsigned int, const ROOT::RDF::RSampleInfo &)> SampleCallback_t
The type of a data-block callback, registered with an RDataFrame computation graph via e....