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 <ROOT/RStringView.hxx>
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/// 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 const std::string &GetSampleName() const
63 {
65 return fSample->GetSampleName();
66 }
67
68 unsigned int GetSampleId() const
69 {
71 return fSample->GetSampleId();
72 }
73
74 int GetI(const std::string &key) const
75 {
77 return fSample->GetMetaData().GetI(key);
78 }
79
80 double GetD(const std::string &key) const
81 {
83 return fSample->GetMetaData().GetD(key);
84 }
85
86 std::string GetS(const std::string &key) const
87 {
89 return fSample->GetMetaData().GetS(key);
90 }
91
92 /// Check whether the sample name contains the given substring.
93 bool Contains(std::string_view substr) const
94 {
95 // C++14 needs the conversion from std::string_view to std::string
96 return fID.find(std::string(substr)) != std::string::npos;
97 }
98
99 /// Check whether the sample name is empty.
100 ///
101 /// This is the case e.g. when using a RDataFrame with no input data, constructed as `RDataFrame(nEntries)`.
102 bool Empty() const {
103 return fID.empty();
104 }
105
106 /// Return a string representation of the sample name.
107 ///
108 /// The representation is of the form "<filename>/<treename>" if the input data comes from a TTree or a TChain.
109 const std::string &AsString() const
110 {
111 return fID;
112 }
113
114 /// Return the entry range in this sample that is being taken into consideration.
115 ///
116 /// Multiple multi-threading tasks might process different entry ranges of the same sample.
117 std::pair<ULong64_t, ULong64_t> EntryRange() const { return fEntryRange; }
118
119 /// Return the number of entries of this sample that is being taken into consideration.
120 ULong64_t NEntries() const { return fEntryRange.second - fEntryRange.first; }
121
122 bool operator==(const RSampleInfo &other) const { return fID == other.fID; }
123 bool operator!=(const RSampleInfo &other) const { return !(*this == other); }
124};
125
126/// The type of a data-block callback, registered with a RDataFrame computation graph via e.g.
127/// DefinePerSample() or by certain actions (e.g. Snapshot()).
128using SampleCallback_t = std::function<void(unsigned int, const ROOT::RDF::RSampleInfo &)>;
129
130} // namespace RDF
131} // namespace ROOT
132
133#endif
unsigned long long ULong64_t
Definition RtypesCore.h:81
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
Definition RMetaData.cxx:56
double GetD(const std::string &key) const
Definition RMetaData.cxx:47
int GetI(const std::string &key) const
Definition RMetaData.cxx:38
Class representing a sample (grouping of trees (and their fileglobs) and (optional) metadata)
Definition RSample.hxx:29
const std::string & GetSampleName() const
Definition RSample.cxx:74
const RMetaData & GetMetaData() const
Definition RSample.cxx:89
This type represents a sample identifier, to be used in conjunction with RDataFrame features such as ...
unsigned int GetSampleId() const
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
std::string GetS(const std::string &key) const
RSampleInfo(const RSampleInfo &)=default
std::pair< ULong64_t, ULong64_t > fEntryRange
RSampleInfo & operator=(const RSampleInfo &)=default
double GetD(const std::string &key) const
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
std::pair< ULong64_t, ULong64_t > EntryRange() const
Return the entry range in this 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 a RDataFrame computation graph via e....
This file contains a specialised ROOT message handler to test for diagnostic in unit tests.