Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RDFHelpers.cxx
Go to the documentation of this file.
1// Author: Stefan Wunsch, Enrico Guiraud CERN 09/2020
2
3/*************************************************************************
4 * Copyright (C) 1995-2020, 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#include "ROOT/RDFHelpers.hxx"
12
13#include "ROOT/RDF/RActionImpl.hxx" // for RActionImpl
14#include "ROOT/RDF/RFilterBase.hxx" // for RDFInternal
15#include "ROOT/RDF/RLoopManager.hxx" // for RLoopManager
16#include "ROOT/RDF/Utils.hxx"
17#include "ROOT/RResultHandle.hxx" // for RResultHandle, RunGraphs
18
19#include "TROOT.h" // IsImplicitMTEnabled
20#include "TError.h" // Warning
21#include "TStopwatch.h"
22#include "RConfigure.h" // R__USE_IMT
23#include "ROOT/RLogger.hxx"
24#include "ROOT/RSlotStack.hxx"
25#ifdef R__USE_IMT
27#endif // R__USE_IMT
28
29#include <algorithm>
30#include <cstdio>
31#include <iomanip>
32#include <iostream>
33#include <set>
34#include <sstream>
35
36#include <numeric>
37
38#if defined(_WIN32) || defined(_WIN64)
39#define WIN32_LEAN_AND_MEAN
40#define VC_EXTRALEAN
41#include <io.h>
42#include <Windows.h>
43#else
44#include <sys/ioctl.h>
45#include <unistd.h>
46#endif
47
48class TTreeReader;
49
50namespace {
51// Get terminal size for progress bar
52// TODO: Put this in core libraries?
53int get_tty_size()
54{
55#if defined(_WIN32) || defined(_WIN64)
56 if (!_isatty(_fileno(stdout)))
57 return 0;
58 int width = 0;
59 CONSOLE_SCREEN_BUFFER_INFO csbi;
60 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
61 width = (int)(csbi.srWindow.Right - csbi.srWindow.Left + 1);
62 return width;
63#else
64 int width = 0;
65 struct winsize w;
66 ioctl(fileno(stdout), TIOCGWINSZ, &w);
67 width = (int)(w.ws_col);
68 return width;
69#endif
70}
71
72/// Restore an output stream to its previous state using RAII.
73struct RestoreStreamState {
74 RestoreStreamState(std::ostream &stream) : fStream(stream) { fPreservedState.copyfmt(stream); }
75 ~RestoreStreamState() { fStream.copyfmt(fPreservedState); }
76
77 std::ostream &fStream;
78 std::ios fPreservedState{nullptr};
79};
80
81/// Format std::chrono::seconds as `1:30m`.
82std::string prettyPrint(std::chrono::seconds const &elapsedSeconds)
83{
84 std::stringstream str;
85 auto h = std::chrono::duration_cast<std::chrono::hours>(elapsedSeconds);
86 auto m = std::chrono::duration_cast<std::chrono::minutes>(elapsedSeconds - h);
87 auto s = (elapsedSeconds - h - m).count();
88
89 if (h.count() > 0)
90 str << h.count() << ':' << std::setw(2) << std::right << std::setfill('0');
91 str << m.count() << ':' << std::setw(2) << std::right << std::setfill('0') << s;
92 str << (h.count() > 0 ? 'h' : 'm');
93
94 return str.str();
95}
96
97} // namespace
98
100
101unsigned int ROOT::RDF::RunGraphs(std::vector<RResultHandle> handles)
102{
103 if (handles.empty()) {
104 Warning("RunGraphs", "Got an empty list of handles, now quitting.");
105 return 0u;
106 }
107
108 // Check that there are results which have not yet been run
109 const unsigned int nToRun =
110 std::count_if(handles.begin(), handles.end(), [](const auto &h) { return !h.IsReady(); });
111 if (nToRun < handles.size()) {
112 Warning("RunGraphs", "Got %zu handles from which %zu link to results which are already ready.", handles.size(),
113 handles.size() - nToRun);
114 }
115 if (nToRun == 0u)
116 return 0u;
117
118 // Find the unique event loops
119 auto sameGraph = [](const RResultHandle &a, const RResultHandle &b) { return a.fLoopManager < b.fLoopManager; };
120 std::set<RResultHandle, decltype(sameGraph)> s(handles.begin(), handles.end(), sameGraph);
121 std::vector<RResultHandle> uniqueLoops(s.begin(), s.end());
122
123 // Trigger jitting. One call is enough to jit the code required by all computation graphs.
124 TStopwatch sw;
125 sw.Start();
126 {
129 if (effectiveVerbosity >= ROOT::ELogLevel::kDebug + 10) {
130 // a very high verbosity was requested, let's not silence anything
131 uniqueLoops[0].fLoopManager->Jit();
132 } else {
133 // silence logs from RLoopManager::Jit: RunGraphs does its own logging
135 uniqueLoops[0].fLoopManager->Jit();
136 }
137 }
138 sw.Stop();
140 << "Just-in-time compilation phase for RunGraphs (" << uniqueLoops.size()
141 << " unique computation graphs) completed"
142 << (sw.RealTime() > 1e-3 ? " in " + std::to_string(sw.RealTime()) + " seconds." : " in less than 1ms.");
143
144 // Trigger the unique event loops
145 auto slotStack = std::make_shared<ROOT::Internal::RSlotStack>(ROOT::GetThreadPoolSize());
146 auto run = [&slotStack](RResultHandle &h) {
147 if (h.fLoopManager) {
148 h.fLoopManager->SetSlotStack(slotStack);
149 h.fLoopManager->Run(/*jit=*/false);
150 }
151 };
152
153 // If called from Python, release the GIL until this function returns: other threads need it to run Python code
155
156 sw.Start();
157#ifdef R__USE_IMT
159 ROOT::TThreadExecutor{}.Foreach(run, uniqueLoops);
160 } else {
161#endif
162 std::for_each(uniqueLoops.begin(), uniqueLoops.end(), run);
163#ifdef R__USE_IMT
164 }
165#endif
166 sw.Stop();
168 << "Finished RunGraphs run (" << uniqueLoops.size() << " unique computation graphs, " << sw.CpuTime() << "s CPU, "
169 << sw.RealTime() << "s elapsed).";
170
171 return uniqueLoops.size();
172}
173
174namespace ROOT::RDF::Experimental {
175
176void ThreadsPerTH3(unsigned int N)
177{
179}
180
181ProgressHelper::ProgressHelper(std::size_t increment, unsigned int totalFiles, unsigned int printInterval,
182 bool useColors)
183 :
184#if defined(_WIN32) || defined(_WIN64)
185 fIsTTY{_isatty(_fileno(stdout)) != 0},
186 fUseShellColours{false && useColors},
187#else
188 fIsTTY{isatty(fileno(stdout)) == 1},
189 fUseShellColours{useColors && fIsTTY}, // Control characters only with terminals.
190#endif
191 fIncrement{increment},
192 fNColumns(fIsTTY ? (get_tty_size() == 0 ? 60 : get_tty_size()) : 50),
193 fTotalFiles{totalFiles},
194 fPrintInterval{printInterval == 0 ? (fIsTTY ? 1 : 10) : printInterval}
195{
196 std::fill(fEventsPerSecondStatistics.begin(), fEventsPerSecondStatistics.end(), -1.);
197}
198
199/// Register a new sample for completion statistics.
200/// \see ROOT::RDF::RInterface::DefinePerSample().
201/// The *id.AsString()* refers to the name of the currently processed file.
202/// The idea is to populate the event entries in the *fSampleNameToEventEntries* map
203/// by selecting the greater of the two values:
204/// *id.EntryRange().second* which is the upper event entry range of the processed sample
205/// and the current value of the event entries in the *fSampleNameToEventEntries* map.
206/// In the single threaded case, the two numbers are the same as the entry range corresponds
207/// to the number of events in an individual file (each sample is simply a single file).
208/// In the multithreaded case, the idea is to accumulate the higher event entry value until
209/// the total number of events in a given file is reached.
210void ProgressHelper::RegisterNewSample(unsigned int /*slot*/, const ROOT::RDF::RSampleInfo &id)
211{
212 std::scoped_lock lock(fSampleNameToEventEntriesMutex);
213 fSampleNameToEventEntries[id.AsString()] = std::max(id.NEntriesTotal(), fSampleNameToEventEntries[id.AsString()]);
214}
215
216/// Compute a running mean of events/s.
218{
219 double sum = 0;
220 unsigned int n = 0;
221 for (auto item : fEventsPerSecondStatistics) {
222 if (item >= 0.) {
223 sum += item;
224 ++n;
225 }
226 }
227
228 return n > 0 ? sum / n : 0.;
229}
230
231/// Compute total events in all open files.
233{
234 std::scoped_lock lock(fSampleNameToEventEntriesMutex);
235 std::size_t result = 0;
236 for (const auto &item : fSampleNameToEventEntries)
237 result += item.second;
238 return result;
239}
240
241/// Record current event counts and time stamp, populate evts/s statistics array.
242/// The function assumes that a lock on the update mutex is held.
243std::pair<std::size_t, std::chrono::seconds> ProgressHelper::RecordEvtCountAndTime()
244{
245 using namespace std::chrono;
246
247 auto currentEventCount = fProcessedEvents.load(std::memory_order_acquire);
248 auto eventsPerTimeInterval = currentEventCount - fLastProcessedEvents;
249 fLastProcessedEvents = currentEventCount;
250
251 auto oldPrintTime = fLastPrintTime;
252 auto newPrintTime = system_clock::now();
253 fLastPrintTime = newPrintTime;
254
255 duration<double> secondsCurrentInterval = newPrintTime - oldPrintTime;
258 eventsPerTimeInterval / secondsCurrentInterval.count();
259
260 return {currentEventCount, duration_cast<seconds>(newPrintTime - fBeginTime)};
261}
262
263/// Print event and time statistics.
264void ProgressHelper::PrintProgressAndStats(std::ostream &stream, std::size_t currentEventCount,
265 std::chrono::seconds elapsedSeconds) const
266{
267 std::ostringstream buffer;
268 auto evtpersec = EvtPerSec();
269 auto totalEventsInOpenFiles = ComputeTotalEvents();
270 std::size_t currentFileIdx;
271 {
272 std::scoped_lock lock(fSampleNameToEventEntriesMutex);
273 currentFileIdx = fSampleNameToEventEntries.size();
274 }
275
276 if (totalEventsInOpenFiles == 0)
277 return;
278
279 double completion = 0.;
280 if (currentFileIdx < fTotalFiles) {
281 const double fractionSeenFiles = double(currentFileIdx) / fTotalFiles;
282 completion = fractionSeenFiles * (double(currentEventCount) / totalEventsInOpenFiles);
283 } else {
284 completion = double(currentEventCount) / totalEventsInOpenFiles;
285 }
286
287 // Print the bar
288 {
289 const auto barWidth = fNColumns / 4;
290 unsigned int nBar = std::min(completion, 1.) * barWidth;
291 std::string bars(std::max(nBar, 1u), '=');
292 bars.back() = (nBar == barWidth) ? '=' : '>';
293
295 buffer << "\033[33m";
296 buffer << '|' << std::setfill(' ') << std::setw(barWidth) << std::left << bars << "| ";
298 buffer << "\033[0m";
299 }
300
301 // Elapsed time
302 buffer << "[";
304 buffer << "\033[35m";
305 buffer << "Elapsed: " << prettyPrint(elapsedSeconds) << " ";
307 buffer << "\033[0m";
308 buffer << "files: " << currentFileIdx << " / " << fTotalFiles << " ";
309
310 // Event counts:
312 buffer << "\033[32m";
313
314 buffer << "events: " << currentEventCount;
315 if (totalEventsInOpenFiles != 0) {
316 buffer << " / " << std::scientific << std::setprecision(2);
317 if (currentFileIdx == fTotalFiles)
318 buffer << totalEventsInOpenFiles;
319 else
320 buffer << "(" << totalEventsInOpenFiles << " + x)";
321 }
322 buffer << " ";
323
325 buffer << "\033[0m";
326
327 // events/s
328 buffer << std::scientific << std::setprecision(2) << evtpersec << " evt/s";
329
330 // Time statistics:
331 // As long as not all files have been opened, estimate when 100% completion will be reached
332 // based on current completion elapsed time. (This assumes that unopened files have about the
333 // same size as the files that have been seen.)
334 // Once the total event count is known, use "missing events / evt/s".
335 if (totalEventsInOpenFiles != 0) {
337 buffer << "\033[35m";
338
339 std::chrono::seconds remainingSeconds;
340 if (currentFileIdx == fTotalFiles) {
341 remainingSeconds =
342 std::chrono::seconds{static_cast<long long>((ComputeTotalEvents() - currentEventCount) / evtpersec)};
343 } else {
344 remainingSeconds =
345 std::chrono::seconds{static_cast<long long>(elapsedSeconds.count() / completion - elapsedSeconds.count())};
346 }
347 buffer << " remaining ca.: " << prettyPrint(remainingSeconds);
348
350 buffer << "\033[0m";
351 }
352
353 buffer << "]";
354
355 RestoreStreamState restore(stream);
356 stream << std::left << std::setw(fNColumns - 1) << buffer.str();
357}
358
359void ProgressHelper::PrintStatsFinal(std::size_t exactTotalEvents) const
360{
361 auto &stream = std::cout;
362 RestoreStreamState restore(stream);
363 const std::chrono::duration<double> elapsed = std::chrono::system_clock::now() - fBeginTime;
364 const auto elapsedSeconds = std::chrono::duration_cast<std::chrono::seconds>(elapsed);
365 const auto totalEvents = exactTotalEvents;
366
367 // The next line resets the current line output in the terminal.
368 // Brings the cursor at the beginning ('\r'), prints whitespace with the
369 // same length as the terminal size, then resets the cursor again so we
370 // can print the final stats on a clean line.
371 if (fIsTTY)
372 stream << '\r' << std::string(fNColumns, ' ') << '\r';
373
375 stream << "\033[35m";
376 stream << "["
377 << "Total elapsed time: " << prettyPrint(elapsedSeconds) << " ";
379 stream << "\033[0m";
380 stream << "processed files: " << fTotalFiles << " ";
381
382 // Event counts:
384 stream << "\033[32m";
385
386 stream << "processed events: " << totalEvents;
387
389 stream << "\033[0m";
390
391 // Divide by the full-precision duration rather than by `elapsedSeconds`: the latter is truncated
392 // to whole seconds, so it is zero for any event loop shorter than a second.
393 stream << " " << std::scientific << std::setprecision(2) << (double)totalEvents / elapsed.count()
394 << " evt/s";
395
396 stream << "]\n";
397}
398
399/// Record number of events processed and update progress bar.
400/// This function will atomically record elapsed times and event statistics, and one thread will udpate the progress bar
401/// every n seconds (set by the fPrintInterval).
403{
404 using namespace std::chrono;
405 // ***************************************************
406 // Warning: Here, everything needs to be thread safe:
407 // ***************************************************
408 fProcessedEvents.fetch_add(fIncrement, std::memory_order_relaxed);
409
410 // We only print every n seconds.
411 if (duration_cast<seconds>(system_clock::now() - fLastPrintTime) < fPrintInterval) {
412 return;
413 }
414
415 // ******************************************************
416 // Update the progress bar. Only one thread can proceed.
417 // ******************************************************
418 std::unique_lock<std::mutex> lockGuard(fUpdateMutex, std::try_to_lock);
419 if (!lockGuard)
420 return;
421
422 auto const [eventCount, elapsedSeconds] = RecordEvtCountAndTime();
423
424 if (fIsTTY)
425 std::cout << "\r";
426
427 PrintProgressAndStats(std::cout, eventCount, elapsedSeconds);
428
429 if (fIsTTY)
430 std::cout << std::flush;
431 else
432 std::cout << std::endl;
433}
434
435class ProgressBarAction final : public ROOT::Detail::RDF::RActionImpl<ProgressBarAction> {
436public:
437 using Result_t = int;
438
439private:
440 std::shared_ptr<ProgressHelper> fHelper;
441 std::shared_ptr<int> fDummyResult = std::make_shared<int>();
442 std::vector<unsigned long long> fCounts;
443
444public:
445 ProgressBarAction(std::shared_ptr<ProgressHelper> r) : fHelper(std::move(r)), fCounts(ROOT::IsImplicitMTEnabled() ? ROOT::GetThreadPoolSize() : 1, 0) {}
446
447 std::shared_ptr<Result_t> GetResultPtr() const { return fDummyResult; }
448
449 void Initialize() {}
450 void InitTask(TTreeReader *, unsigned int) {}
451
452 void Exec(unsigned int slot) { fCounts[slot]++; }
453
454 void Finalize() {
455 std::size_t exactTotal = 0;
456 for (auto c : fCounts) exactTotal += c;
457 fHelper->PrintStatsFinal(exactTotal);
458 }
459
460 std::string GetActionName() { return "ProgressBar"; }
461 // dummy implementation of PartialUpdate
462 int &PartialUpdate(unsigned int) { return *fDummyResult; }
463
465 {
466 return
467 [this](unsigned int slot, const ROOT::RDF::RSampleInfo &id) { this->fHelper->RegisterNewSample(slot, id); };
468 }
469};
470
472{
473 constexpr std::size_t callbackEveryNEvents = 1000;
474 auto total_files = node.GetNFiles();
475 auto progress = std::make_shared<ProgressHelper>(callbackEveryNEvents, total_files);
476 ProgressBarAction c(progress);
477 auto r = node.Book<>(c);
478 r.OnPartialResultSlot(callbackEveryNEvents, [progress](unsigned int slot, auto &&arg) { (*progress)(slot, arg); });
479}
480
482{
483 auto node = ROOT::RDF::AsRNode(dataframe);
485}
486
487} // namespace ROOT::RDF::Experimental
#define R__LOG_INFO(...)
Definition RLogger.hxx:358
#define b(i)
Definition RSha256.hxx:100
#define c(i)
Definition RSha256.hxx:101
#define a(i)
Definition RSha256.hxx:99
#define h(i)
Definition RSha256.hxx:106
#define e(i)
Definition RSha256.hxx:103
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
Definition TError.cxx:252
#define N
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
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 Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize id
Option_t Option_t width
TCanvas * bars()
Definition bars.C:1
Base class for action helpers, see RInterface::Book() for more information.
Calls release when created, if isHeld says so, and restore when destroyed.
std::vector< unsigned long long > fCounts
ROOT::RDF::SampleCallback_t GetSampleCallback() final
Override this method to register a callback that is executed before the processing a new data sample ...
std::shared_ptr< ProgressHelper > fHelper
ProgressBarAction(std::shared_ptr< ProgressHelper > r)
void InitTask(TTreeReader *, unsigned int)
std::shared_ptr< Result_t > GetResultPtr() const
std::size_t ComputeTotalEvents() const
Compute total events in all open files.
ProgressHelper(std::size_t increment, unsigned int totalFiles, unsigned int printInterval=0, bool useColors=true)
Create a progress helper.
void PrintStatsFinal(std::size_t exactTotalEvents) const
std::pair< std::size_t, std::chrono::seconds > RecordEvtCountAndTime()
Record current event counts and time stamp, populate evts/s statistics array.
void RegisterNewSample(unsigned int, const ROOT::RDF::RSampleInfo &id)
Register a new sample for completion statistics.
std::chrono::time_point< std::chrono::system_clock > const fBeginTime
void Update()
Record number of events processed and update progress bar.
std::map< std::string, ULong64_t > fSampleNameToEventEntries
std::chrono::seconds const fPrintInterval
double EvtPerSec() const
Compute a running mean of events/s.
std::atomic< std::size_t > fProcessedEvents
std::array< double, 10 > fEventsPerSecondStatistics
std::chrono::time_point< std::chrono::system_clock > fLastPrintTime
void PrintProgressAndStats(std::ostream &stream, std::size_t currentEventCount, std::chrono::seconds totalElapsedSeconds) const
Print event and time statistics.
The public interface to the RDataFrame federation of classes.
RResultPtr< typename std::decay_t< Helper >::Result_t > Book(Helper &&helper, const ColumnNames_t &columns={})
Book execution of a custom action using a user-defined helper object.
A type-erased version of RResultPtr and RResultMap.
This type represents a sample identifier, to be used in conjunction with RDataFrame features such as ...
ROOT's RDataFrame offers a modern, high-level interface for analysis of data stored in TTree ,...
ELogLevel GetEffectiveVerbosity(const RLogManager &mgr) const
Definition RLogger.hxx:309
static RLogManager & Get()
Definition RLogger.cxx:59
Change the verbosity level (global or specific to the RLogChannel passed to the constructor) for the ...
Definition RLogger.hxx:239
This class provides a simple interface to execute the same task multiple times in parallel threads,...
void Foreach(F func, unsigned nTimes, unsigned nChunks=0)
Execute a function without arguments several times in parallel, dividing the execution in nChunks.
Stopwatch class.
Definition TStopwatch.h:28
Double_t RealTime()
Stop the stopwatch (if it is running) and return the realtime (in seconds) passed between the start a...
void Start(Bool_t reset=kTRUE)
Start the stopwatch.
Double_t CpuTime()
Stop the stopwatch (if it is running) and return the cputime (in seconds) passed between the start an...
void Stop()
Stop the stopwatch.
A simple, robust and fast interface to read values from ROOT columnar datasets such as TTree,...
Definition TTreeReader.h:46
const Int_t n
Definition legend1.C:16
ROOT::RLogChannel & RDFLogChannel()
Definition RDFUtils.cxx:43
unsigned int & NThreadPerTH3()
Obtain or set the number of threads that will share a clone of a thread-safe 3D histogram.
Definition RDFUtils.cxx:88
RLogChannel & GetChannelOrManager()
Definition RLogger.hxx:298
void ThreadsPerTH3(unsigned int nThread=1)
Set the number of threads sharing one TH3 in RDataFrame.
void AddProgressBar(ROOT::RDF::RNode df)
Add ProgressBar to a ROOT::RDF::RNode.
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....
unsigned int RunGraphs(std::vector< RResultHandle > handles)
Run the event loops of multiple RDataFrames concurrently.
RNode AsRNode(NodeType node)
Cast a RDataFrame node to the common type ROOT::RDF::RNode.
Bool_t IsImplicitMTEnabled()
Returns true if the implicit multi-threading in ROOT is enabled.
Definition TROOT.cxx:673
UInt_t GetThreadPoolSize()
Returns the size of ROOT's thread pool.
Definition TROOT.cxx:680
@ kDebug
Debug information; only useful for developers; can have added verbosity up to 255-kDebug.
@ kError
An error.
TMarker m
Definition textangle.C:8
static uint64_t sum(uint64_t i)
Definition Factory.cxx:2335