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
18from dask.distributed import LocalCluster, Client
19import ROOT
20
21# Import the live visualization function
22LiveVisualize = ROOT.RDF.Experimental.Distributed.LiveVisualize
23
24# Point RDataFrame calls to Dask RDataFrame object
25RDataFrame = ROOT.RDF.Experimental.Distributed.Dask.RDataFrame
26
27# Function to create a Dask cluster and return the client
28def create_connection():
29 cluster = LocalCluster(n_workers=4, threads_per_worker=1, processes=True, memory_limit="2GiB")
30 client = Client(cluster)
31 return client
32
33# Function to fit a Gaussian function to the plot
34def fit_gaus(plot):
35 plot.Fit("gaus")
36
37if __name__ == "__main__":
38 # Setup connection to a Dask cluster
39 connection = create_connection()
40
41 # Create an RDataFrame that will use Dask as a backend for computations
42 num_entries = 100000000
43 d = RDataFrame(num_entries, daskclient=connection, npartitions=30)
44
45 # Define a gaussean distribution with a variable mean
46 dd = d.Define("x", f"gRandom->Gaus(10*rdfentry_/{num_entries}, 2)")\
47 .Define("y", f"gRandom->Gaus(10*rdfentry_/{num_entries}, 3)")\
48
49 # Create a 1D and a 2D histogram using the defined columns
50 h_normal_1d = dd.Histo1D(("normal_1d", "1D Histogram of a Normal Distribution",
51 100, -10, 20),
52 "x")
53
54 h_normal_2d = dd.Histo2D(("normal_2d", "2D Histogram of a Normal Distribution",
55 100, -15, 25,
56 100, -15, 25
57 ), "x", "y")
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)
69 h_normal_1d.Draw()
70 c.cd(2)
71 h_normal_2d.Draw()
72
73 c.Update()