ROOT
master
Reference Guide
Loading...
Searching...
No Matches
rcanvas_mt.cxx
Go to the documentation of this file.
1
/// \file
2
/// \ingroup tutorial_rcanvas
3
///
4
/// This macro demonstrate usage of ROOT7 graphics from many threads
5
/// Three different canvases in three different threads are started and regularly updated.
6
/// Extra thread created in background and used to run http protocol, in/out websocket communications and process http
7
/// requests
8
/// Main application thread (CLING interactive session) remains fully functional
9
///
10
/// \macro_code
11
///
12
/// \date 2018-08-16
13
/// \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback
14
/// is welcome!
15
/// \author Sergey Linev
16
17
/*************************************************************************
18
* Copyright (C) 1995-2025, Rene Brun and Fons Rademakers. *
19
* All rights reserved. *
20
* *
21
* For the licensing terms see $ROOTSYS/LICENSE. *
22
* For the list of contributors see $ROOTSYS/README/CREDITS. *
23
*************************************************************************/
24
25
#include "
ROOT/RWebWindowsManager.hxx
"
26
#include "
ROOT/RCanvas.hxx
"
27
#include "
ROOT/TObjectDrawable.hxx
"
28
29
#include "
TRandom3.h
"
30
#include "
TEnv.h
"
31
#include "
TROOT.h
"
32
#include "
TH1.h
"
33
#include "
TColor.h
"
34
35
#include <thread>
36
#include <iostream>
37
38
using namespace
ROOT::Experimental
;
39
40
void
draw_canvas
(
const
std::string &title,
Int_t
col1
)
41
{
42
// Create histograms
43
auto
hist1
=
new
TH1D
(
"hist1"
,
"hist1"
, 100, -10, 10);
44
auto
hist2
=
new
TH1D
(
"hist2"
,
"hist2"
, 100, -10, 10);
45
46
hist1
->SetLineColor(
col1
);
47
hist2
->SetLineColor(
kBlue
);
48
49
TRandom3
random
;
50
Float_t
px, py;
51
52
for
(
int
n
= 0;
n
< 10000; ++
n
) {
53
random
.Rannor(px, py);
54
hist1
->Fill(px - 2);
55
hist2
->Fill(py + 2);
56
}
57
58
// Create a canvas to be displayed.
59
auto
canvas = RCanvas::Create(title);
60
61
canvas->Draw<
TObjectDrawable
>(
hist1
,
""
);
62
canvas->Draw<
TObjectDrawable
>(
hist2
,
"same"
);
63
64
int
maxloop
= 100;
65
66
canvas->Show();
67
68
std::cout << title <<
" started"
<< std::endl;
69
70
for
(
int
loop = 0; loop <
maxloop
; ++loop) {
71
72
for
(
int
n
= 0;
n
< 10000; ++
n
) {
73
random
.Rannor(px, py);
74
hist1
->Fill(px - 2);
75
hist2
->Fill(py + 2);
76
}
77
78
canvas->Modified();
79
80
canvas->Update();
81
canvas->Run(0.2);
// let run canvas code for next 0.5 seconds
82
83
// if (loop == 0)
84
// canvas->SaveAs(title + "_first.png");
85
// if (loop == maxloop - 1)
86
// canvas->SaveAs(title + "_last.png");
87
}
88
89
std::cout << title <<
" completed"
<< std::endl;
90
91
// remove from global list, will be destroyed with thread exit
92
canvas->Remove();
93
}
94
95
void
rcanvas_mt
(
bool
block_main_thread
=
true
)
96
{
97
TDirectory::TContext
ctx{
nullptr
};
// Don't register histograms to the current directory
98
99
if
(
block_main_thread
) {
100
// let use special http thread to process requests, do not need main thread
101
// required while gSystem->ProcessEvents() will be blocked
102
gEnv
->
SetValue
(
"WebGui.HttpThrd"
,
"yes"
);
103
104
// let create special threads for data sending, optional
105
gEnv
->
SetValue
(
"WebGui.SenderThrds"
,
"yes"
);
106
}
107
108
ROOT::EnableThreadSafety
();
109
110
// create instance in main thread, used to assign thread id as well
111
ROOT::RWebWindowsManager::Instance
();
112
113
std::thread
thrd1
(
draw_canvas
,
"First canvas"
,
kRed
);
114
std::thread
thrd2
(
draw_canvas
,
"Second canvas"
,
kBlue
);
115
std::thread
thrd3
(
draw_canvas
,
"Third canvas"
,
kGreen
);
116
117
if
(
block_main_thread
) {
118
// wait until threads execution finished
119
thrd1
.join();
120
thrd2
.join();
121
thrd3
.join();
122
}
else
{
123
// detach threads and return to CLING
124
thrd1
.detach();
125
thrd2
.detach();
126
thrd3
.detach();
127
}
128
}
RCanvas.hxx
RWebWindowsManager.hxx
Int_t
int Int_t
Signed integer 4 bytes (int)
Definition
RtypesCore.h:59
Float_t
float Float_t
Float 4 bytes (float)
Definition
RtypesCore.h:71
kRed
@ kRed
Definition
Rtypes.h:67
kGreen
@ kGreen
Definition
Rtypes.h:67
kBlue
@ kBlue
Definition
Rtypes.h:67
TRangeDynCast
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Definition
TCollection.h:360
TColor.h
TEnv.h
gEnv
R__EXTERN TEnv * gEnv
Definition
TEnv.h:170
TH1.h
TObjectDrawable.hxx
TROOT.h
TRandom3.h
ROOT::Detail::TRangeCast
Definition
TCollection.h:313
ROOT::Experimental::TObjectDrawable
Provides v7 drawing facilities for TObject types (TGraph, TH1, TH2, etc).
Definition
TObjectDrawable.hxx:37
ROOT::RWebWindowsManager::Instance
static std::shared_ptr< RWebWindowsManager > & Instance()
Returns default window manager Used to display all standard ROOT elements like TCanvas or TFitPanel.
Definition
RWebWindowsManager.cxx:79
TDirectory::TContext
TDirectory::TContext keeps track and restore the current directory.
Definition
TDirectory.h:89
TEnv::SetValue
virtual void SetValue(const char *name, const char *value, EEnvLevel level=kEnvChange, const char *type=nullptr)
Set the value of a resource or create a new resource.
Definition
TEnv.cxx:744
TH1D
1-D histogram with a double per channel (see TH1 documentation)
Definition
TH1.h:926
TRandom3
Random number generator class based on M.
Definition
TRandom3.h:27
n
const Int_t n
Definition
legend1.C:16
ROOT::Experimental
Definition
TTaskGroup.hxx:24
ROOT::EnableThreadSafety
void EnableThreadSafety()
Enable support for multi-threading within the ROOT code in particular, enables the global mutex to ma...
Definition
TROOT.cxx:506
tutorials
visualisation
rcanvas
rcanvas_mt.cxx
ROOTmaster - Reference Guide Generated on Thu Mar 19 2026 03:32:10 (GVA Time) using Doxygen 1.10.0