Logo ROOT  
Reference Guide
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 <s.linev@gsi.de>
16
17/*************************************************************************
18 * Copyright (C) 1995-2021, 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
27#include "ROOT/RCanvas.hxx"
28
29#include "TRandom3.h"
30#include "TEnv.h"
31#include "TROOT.h"
32
33#include <thread>
34#include <iostream>
35
36// macro must be here while cling is not capable to load
37// library automatically for outlined function see ROOT-10336
38R__LOAD_LIBRARY(libROOTHistDraw)
39
40using namespace ROOT::Experimental;
41
42void draw_canvas(const std::string &title, RColor col)
43{
44 // Create histograms
45 RAxisConfig xaxis(100, -10., 10.);
46 auto pHist = std::make_shared<RH1D>(xaxis);
47 auto pHist2 = std::make_shared<RH1D>(xaxis);
48
49 TRandom3 random;
50 Float_t px, py;
51
52 for (int n = 0; n < 10000; ++n) {
53 random.Rannor(px, py);
54 pHist->Fill(px - 2);
55 pHist2->Fill(py + 2);
56 }
57
58 // Create a canvas to be displayed.
59 auto canvas = RCanvas::Create(title);
60
61 canvas->Draw<RFrameTitle>(title);
62 canvas->Draw(pHist)->line.color = col;
63 canvas->Draw(pHist2)->line.color = RColor::kBlue;
64
65 int maxloop = 100;
66
67 canvas->Show();
68
69 std::cout << title << " started" <<std::endl;
70
71 for (int loop = 0; loop < maxloop; ++loop) {
72
73 for (int n = 0; n < 10000; ++n) {
74 random.Rannor(px, py);
75 pHist->Fill(px - 2);
76 pHist2->Fill(py + 2);
77 }
78
79 canvas->Modified();
80
81 canvas->Update();
82 canvas->Run(0.2); // let run canvas code for next 0.5 seconds
83
84 // if (loop == 0)
85 // canvas->SaveAs(title + "_first.png");
86 // if (loop == maxloop - 1)
87 // canvas->SaveAs(title + "_last.png");
88 }
89
90 std::cout << title << " completed" <<std::endl;
91
92 // remove from global list, will be destroyed with thread exit
93 canvas->Remove();
94}
95
96void rcanvas_mt(bool block_main_thread = true)
97{
98 if (block_main_thread) {
99 // let use special http thread to process requests, do not need main thread
100 // required while gSystem->ProcessEvents() will be blocked
101 gEnv->SetValue("WebGui.HttpThrd", "yes");
102
103 // let create special threads for data sending, optional
104 gEnv->SetValue("WebGui.SenderThrds", "yes");
105 }
106
108
109 // create instance in main thread, used to assign thread id as well
110 RWebWindowsManager::Instance();
111
112 std::thread thrd1(draw_canvas, "First canvas", RColor::kRed);
113 std::thread thrd2(draw_canvas, "Second canvas", RColor::kBlue);
114 std::thread thrd3(draw_canvas, "Third canvas", RColor::kGreen);
115
116 if (block_main_thread) {
117 // wait until threads execution finished
118 thrd1.join();
119 thrd2.join();
120 thrd3.join();
121 } else {
122 // detach threads and return to CLING
123 thrd1.detach();
124 thrd2.detach();
125 thrd3.detach();
126 }
127}
float Float_t
Definition: RtypesCore.h:57
#define R__LOAD_LIBRARY(LIBRARY)
Definition: Rtypes.h:472
@ kRed
Definition: Rtypes.h:66
@ kGreen
Definition: Rtypes.h:66
@ kBlue
Definition: Rtypes.h:66
R__EXTERN TEnv * gEnv
Definition: TEnv.h:170
Objects used to configure the different axis types.
Definition: RAxisConfig.hxx:35
The color class.
Definition: RColor.hxx:33
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:736
Random number generator class based on M.
Definition: TRandom3.h:27
virtual void Rannor(Float_t &a, Float_t &b)
Return 2 numbers distributed following a gaussian with mean=0 and sigma=1.
Definition: TRandom.cxx:500
const Int_t n
Definition: legend1.C:16
void EnableThreadSafety()
Enables the global mutex to make ROOT thread safe/aware.
Definition: TROOT.cxx:493