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, DS_t >::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
41 void ThrowIfNoSample() const
42 {
43 if (fSample == nullptr) {
44 const auto msg = "RSampleInfo: sample data was requested but no samples are available.";
45 throw std::logic_error(msg);
46 }
47 }
48
49public:
50 RSampleInfo(std::string_view id, std::pair<ULong64_t, ULong64_t> entryRange,
51 const ROOT::RDF::Experimental::RSample *sample = nullptr)
52 : fID(id), fEntryRange(entryRange), fSample(sample)
53 {
54 }
55 RSampleInfo() = default;
56 RSampleInfo(const RSampleInfo &) = default;
57 RSampleInfo &operator=(const RSampleInfo &) = default;
58 RSampleInfo(RSampleInfo &&) = default;
60 ~RSampleInfo() = default;
61
62 /// @brief Get the name of the sample as a string.
63 const std::string &GetSampleName() const
64 {
66 return fSample->GetSampleName();
67 }
68
69 /// @brief Get the sample id as an int.
70 unsigned int GetSampleId() const
71 {
73 return fSample->GetSampleId();
74 }
75
76 /// @brief Return the metadata value of type int given the key.
77 int GetI(const std::string &key) const
78 {
80 return fSample->GetMetaData().GetI(key);
81 }
82
83 /// @brief Return the metadata value of type double given the key.
84 double GetD(const std::string &key) const
85 {
87 return fSample->GetMetaData().GetD(key);
88 }
89
90 /// @brief Return the metadata value of type string given the key.
91 std::string GetS(const std::string &key) const
92 {
94 return fSample->GetMetaData().GetS(key);
95 }
96
97 /// @brief Check whether the sample name contains the given substring.
98 bool Contains(std::string_view substr) const
99 {
100 // C++14 needs the conversion from std::string_view to std::string
101 return fID.find(std::string(substr)) != std::string::npos;
102 }
103
104 /// @brief Check whether the sample name is empty.
105 ///
106 /// This is the case e.g. when using a RDataFrame with no input data, constructed as `RDataFrame(nEntries)`.
107 bool Empty() const {
108 return fID.empty();
109 }
110
111 /// @brief Return a string representation of the sample name.
112 ///
113 /// The representation is of the form "<filename>/<treename>" if the input data comes from a TTree or a TChain.
114 const std::string &AsString() const
115 {
116 return fID;
117 }
118
119 /// @brief Return the entry range in the sample that is being taken into consideration.
120 ///
121 /// Multiple multi-threading tasks might process different entry ranges of the same sample.
122 std::pair<ULong64_t, ULong64_t> EntryRange() const { return fEntryRange; }
123
124 /// @brief Return the number of entries of this sample that is being taken into consideration.
125 ULong64_t NEntries() const { return fEntryRange.second - fEntryRange.first; }
126
127 bool operator==(const RSampleInfo &other) const { return fID == other.fID; }
128 bool operator!=(const RSampleInfo &other) const { return !(*this == other); }
129};
130
131/// The type of a data-block callback, registered with an RDataFrame computation graph via e.g. \ref
132/// ROOT::RDF::RInterface< Proxied, DS_t >::DefinePerSample "DefinePerSample()" or by certain actions (e.g. \ref
133/// ROOT::RDF::RInterface<Proxied,DataSource>::Snapshot "Snapshot()").
134using SampleCallback_t = std::function<void(unsigned int, const ROOT::RDF::RSampleInfo &)>;
135
136} // namespace RDF
137} // namespace ROOT
138
139#endif
unsigned long long ULong64_t
Definition RtypesCore.h:70
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize id
std::string GetS(const std::string &key) const
Return the metadata value of type string given the key, or an error if the metadata value is of a non...
Definition RMetaData.cxx:96
double GetD(const std::string &key) const
Return the metadata value of type double given the key, or an error if the metadata value is of a non...
Definition RMetaData.cxx:84
int GetI(const std::string &key) const
Return the metadata value of type int given the key, or an error if the metadata value is of a non-in...
Definition RMetaData.cxx:73
Class representing a sample which is a grouping of trees and their fileglobs, and,...
Definition RSample.hxx:39
const std::string & GetSampleName() const
Get the name of the sample (RSample object).
Definition RSample.cxx:75
const RMetaData & GetMetaData() const
Get an instance of the RMetaData class.
Definition RSample.cxx:99
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
RSampleInfo(std::string_view id, std::pair< ULong64_t, ULong64_t > entryRange, const ROOT::RDF::Experimental::RSample *sample=nullptr)
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
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 sample that is being taken into consideration.
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....
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...