Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
distrdf003_live_visualization.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_dataframe
3## \notebook -draw
4## Configure a Dask connection and visualize the filling of a 1D and 2D
5## histograms distributedly.
6##
7## This tutorial showcases the process of setting up real-time data representation
8## for distributed computations.
9## By calling the LiveVisualize function, you can observe the canvas updating
10## with the intermediate results of the histograms as the
11## distributed computation progresses.
12##
13## \macro_code
14## \macro_image
15##
16## \date August 2023
17## \author Silia Taider
18import ROOT
19from dask.distributed import Client, LocalCluster
20
21# Import the live visualization function
23
24# Point RDataFrame calls to Dask RDataFrame object
26
27
28# Function to create a Dask cluster and return the client
30 cluster = LocalCluster(n_workers=4, threads_per_worker=1, processes=True, memory_limit="2GiB")
31 client = Client(cluster)
32 return client
33
34
35# Function to fit a Gaussian function to the plot
36def fit_gaus(plot):
37 plot.Fit("gaus", "Q")
38
39
40if __name__ == "__main__":
41 # Setup connection to a Dask cluster
42 connection = create_connection()
43
44 # Create an RDataFrame that will use Dask as a backend for computations
45 num_entries = 100000000
46 d = RDataFrame(num_entries, executor=connection, npartitions=30)
47
48 # Define a gaussean distribution with a variable mean
49 dd = d.Define("x", f"gRandom->Gaus(10*rdfentry_/{num_entries}, 2)").Define(
50 "y", f"gRandom->Gaus(10*rdfentry_/{num_entries}, 3)"
51 )
52 # Create a 1D and a 2D histogram using the defined columns
53 h_normal_1d = dd.Histo1D(("normal_1d", "1D Histogram of a Normal Distribution", 100, -10, 20), "x")
54
55 h_normal_2d = dd.Histo2D(
56 ("normal_2d", "2D Histogram of a Normal Distribution", 100, -15, 25, 100, -15, 25), "x", "y"
57 )
58
59 # Apply LiveVisualize to the histograms.
60 # The `fit_gaus` function will be applied to the accumulating partial result
61 # of the 1D histogram. The 2D histogram will not be further modified, just drawn.
62 # Find more details about usage of LiveVisualize in the RDataFrame docs.
63 LiveVisualize({h_normal_1d: fit_gaus, h_normal_2d: None})
64
65 # Plot the histograms side by side on a canvas
66 c = ROOT.TCanvas("distrdf003", "distrdf003", 1600, 400)
67 c.Divide(2, 1)
68 c.cd(1)
70 c.cd(2)
72
73 c.Update()
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.