11#ifndef ROOT_RACTIONSNAPSHOT
12#define ROOT_RACTIONSNAPSHOT
28namespace GraphDrawing {
29std::shared_ptr<GraphNode>
AddDefinesToGraph(std::shared_ptr<GraphNode> node,
const RColumnRegister &colRegister,
30 const std::vector<std::string> &prevNodeDefines,
31 std::unordered_map<
void *, std::shared_ptr<GraphNode>> &visitedMap);
34class SnapshotHelperWithVariations;
36template <
typename Helper,
typename PrevNode>
44 using PrevNodeCommon_t = std::conditional_t<std::is_same_v<PrevNode, ROOT::Detail::RDF::RJittedFilter>,
47 std::vector<std::shared_ptr<PrevNodeCommon_t>>
fPrevNodes;
50 std::vector<std::vector<RColumnReaderBase *>>
fValues;
63 assert(fPrevNodes.size() == 1);
64 const auto ¤tVariations = GetVariations();
67 auto nominalPrevNode = fPrevNodes.front();
69 fPrevNodes.resize(1 + currentVariations.size(), nominalPrevNode);
74 const auto &prevVariations = nominalPrevNode->GetVariations();
75 fPrevNodes.reserve(1 + currentVariations.size());
77 for (
const auto &variation : currentVariations) {
78 if (IsStrInVec(variation, prevVariations)) {
79 fPrevNodes.emplace_back(
80 std::static_pointer_cast<PrevNodeCommon_t>(nominalPrevNode->GetVariedFilter(variation)));
82 fPrevNodes.push_back(nominalPrevNode);
89 const std::vector<const std::type_info *> &colTypeIDs, std::shared_ptr<PrevNode> pd,
91 :
RActionBase(pd->GetLoopManagerUnchecked(), columns, colRegister, pd->GetVariations()),
92 fHelper(std::move(
h)),
95 fColTypeIDs(colTypeIDs)
97 fLoopManager->Register(
this);
99 const auto nColumns = columns.size();
100 fIsDefine.reserve(nColumns);
101 for (
auto i = 0u; i < nColumns; ++i)
104 if constexpr (std::is_same_v<Helper, SnapshotHelperWithVariations>) {
107 if (!GetVariations().empty())
110 AppendVariedPrevNodes();
112 for (
auto i = 0u; i < nColumns; ++i) {
114 auto define = colRegister.
GetDefine(columns[i]);
134 return fHelper.GetMergeableValue();
142 RActionBase::GetColumnNames(), fColTypeIDs);
144 if constexpr (std::is_same_v<Helper, SnapshotHelperWithVariations>) {
147 auto const &variations = GetVariations();
148 for (
unsigned int variationIndex = 0; variationIndex < variations.size(); ++variationIndex) {
149 auto const &readers =
151 RActionBase::GetColumnNames(), fColTypeIDs, variations[variationIndex]);
152 for (
unsigned int i = 0; i < readers.size(); ++i) {
153 if (fValues[slot][i] != readers[i]) {
155 fValues[slot].push_back(readers[i]);
157 fHelper.RegisterVariedColumn(slot, i, i, 0,
159 fHelper.RegisterVariedColumn(slot, fValues[slot].
size() - 1, i, variationIndex + 1,
160 variations[variationIndex]);
166 fHelper.InitTask(
r, slot);
171 assert(slot < fValues.size());
172 assert(readerIdx < fValues[slot].
size());
173 if (
auto *val = fValues[slot][readerIdx]->
template TryGet<void>(entry))
176 throw std::out_of_range{
"RDataFrame: Action (" + fHelper.GetActionName() +
177 ") could not retrieve value for column '" + fColumnNames[readerIdx] +
"' for entry " +
178 std::to_string(entry) +
179 ". You can use the DefaultValueFor operation to provide a default value, or "
180 "FilterAvailable/FilterMissing to discard/keep entries with missing values instead."};
185 std::vector<void *> untypedValues;
186 auto nReaders = fValues[slot].size();
187 untypedValues.reserve(nReaders);
188 for (
decltype(nReaders) readerIdx{}; readerIdx < nReaders; readerIdx++)
189 untypedValues.push_back(GetValue(slot, readerIdx, entry));
191 fHelper.Exec(slot, untypedValues);
196 if constexpr (std::is_same_v<Helper, SnapshotHelperWithVariations>) {
198 std::vector<bool> filterPassed(fPrevNodes.size(),
false);
199 for (
unsigned int variation = 0; variation < fPrevNodes.size(); ++variation) {
200 filterPassed[variation] = fPrevNodes[variation]->CheckFilters(slot, entry);
205 if (std::any_of(filterPassed.begin(), filterPassed.end(), [](
bool val) { return val; })) {
207 std::vector<void *> untypedValues;
208 auto nReaders = fValues[slot].size();
209 untypedValues.reserve(nReaders);
210 for (
decltype(nReaders) readerIdx{}; readerIdx < nReaders; readerIdx++)
211 untypedValues.push_back(GetValue(slot, readerIdx, entry));
213 fHelper.Exec(slot, untypedValues, filterPassed);
216 if (fPrevNodes.front()->CheckFilters(slot, entry))
217 CallExec(slot, entry);
223 for (
auto const &node : fPrevNodes)
224 node->IncrChildrenCount();
230 fValues[slot].clear();
231 fHelper.CallFinalizeTask(slot);
242 std::shared_ptr<GraphDrawing::GraphNode>
243 GetGraph(std::unordered_map<
void *, std::shared_ptr<GraphDrawing::GraphNode>> &visitedMap)
final
246 const auto nodeType = HasRun() ? GraphDrawing::ENodeType::kUsedAction : GraphDrawing::ENodeType::kAction;
247 auto thisNode = std::make_shared<GraphDrawing::GraphNode>(fHelper.GetActionName(), visitedMap.size(), nodeType);
248 visitedMap[(
void *)
this] = thisNode;
250 for (
auto const &node : fPrevNodes) {
251 auto prevNode = node->GetGraph(visitedMap);
252 const auto &prevColumns = prevNode->GetDefinedColumns();
253 auto upmostNode = AddDefinesToGraph(thisNode, GetColRegister(), prevColumns, visitedMap);
255 thisNode->AddDefinedColumns(GetColRegister().GenerateColumnNames());
256 upmostNode->SetPrevNode(prevNode);
262 void *
PartialUpdate(
unsigned int slot)
final {
return fHelper.CallPartialUpdate(slot); }
265 [[maybe_unused]] std::unique_ptr<RActionBase>
MakeVariedAction(std::vector<void *> && ) final
267 throw std::logic_error(
"RDataFrame::Snapshot: The snapshot action cannot be varied. Instead, switch on "
268 "variations in RSnapshotOptions.");
279 return std::make_unique<RActionSnapshot>(fHelper.CallMakeNew(newResult), GetColumnNames(), fColTypeIDs,
280 std::static_pointer_cast<PrevNode>(fPrevNodes.front()),
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
long long Long64_t
Portable signed long integer 8 bytes.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
virtual void MakeVariations(const std::vector< std::string > &variations)=0
Create clones of this Define that work with values in varied "universes".
Base class for non-leaf nodes of the computational graph.
RActionSnapshot & operator=(const RActionSnapshot &)=delete
void CallExec(unsigned int slot, Long64_t entry)
ROOT::RDF::SampleCallback_t GetSampleCallback() final
void TriggerChildrenCount() final
std::unique_ptr< RActionBase > MakeVariedAction(std::vector< void * > &&) final
Will throw, since varied actions are unsupported. Instead, set a flag in RSnapshotOptions.
std::vector< bool > fIsDefine
The nth flag signals whether the nth input column is a custom column or not.
std::vector< std::vector< RColumnReaderBase * > > fValues
Column readers per slot and per input column.
void InitSlot(TTreeReader *r, unsigned int slot) final
void * PartialUpdate(unsigned int slot) final
Forwards to the action helpers; will throw since PartialUpdate not supported for most snapshot helper...
void FinalizeSlot(unsigned int slot) final
Clean-up operations to be performed at the end of a task.
std::conditional_t< std::is_same_v< PrevNode, ROOT::Detail::RDF::RJittedFilter >, ROOT::Detail::RDF::RFilterBase, PrevNode > PrevNodeCommon_t
std::unique_ptr< RActionBase > CloneAction(void *newResult) final
Returns a new action with a cloned helper.
RActionSnapshot(Helper &&h, const std::vector< std::string > &columns, const std::vector< const std::type_info * > &colTypeIDs, std::shared_ptr< PrevNode > pd, const RColumnRegister &colRegister)
std::vector< std::shared_ptr< PrevNodeCommon_t > > fPrevNodes
Previous nodes in the computation graph. First element is nominal, others are varied.
void * GetValue(unsigned int slot, std::size_t readerIdx, Long64_t entry)
RActionSnapshot(RActionSnapshot &&)=delete
std::vector< const std::type_info * > fColTypeIDs
Types of the columns to Snapshot.
void Finalize() final
Clean-up and finalize the action result (e.g.
std::unique_ptr< ROOT::Detail::RDF::RMergeableValueBase > GetMergeableValue() const final
Retrieve a wrapper to the result of the action that knows how to merge with others of the same type.
std::shared_ptr< GraphDrawing::GraphNode > GetGraph(std::unordered_map< void *, std::shared_ptr< GraphDrawing::GraphNode > > &visitedMap) final
void AppendVariedPrevNodes()
RActionSnapshot(const RActionSnapshot &)=delete
RActionSnapshot & operator=(RActionSnapshot &&)=delete
void Run(unsigned int slot, Long64_t entry) final
A binder for user-defined columns, variations and aliases.
bool IsDefineOrAlias(std::string_view name) const
Check if the provided name is tracked in the names list.
RDFDetail::RDefineBase * GetDefine(std::string_view colName) const
Return the RDefine for the requested column name, or nullptr.
A simple, robust and fast interface to read values from ROOT columnar datasets such as TTree,...
std::shared_ptr< GraphNode > AddDefinesToGraph(std::shared_ptr< GraphNode > node, const RColumnRegister &colRegister, const std::vector< std::string > &prevNodeDefines, std::unordered_map< void *, std::shared_ptr< GraphNode > > &visitedMap)
std::vector< RDFDetail::RColumnReaderBase * > GetUntypedColumnReaders(unsigned int slot, TTreeReader *treeReader, ROOT::Internal::RDF::RColumnRegister &colRegister, ROOT::Detail::RDF::RLoopManager &lm, const std::vector< std::string > &colNames, const std::vector< const std::type_info * > &colTypeIDs, const std::string &variationName="nominal")
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....