Logo ROOT  
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 <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
36using namespace ROOT::Experimental;
37
38void draw_canvas(const std::string &title, RColor col)
39{
40 // Create histograms
41 RAxisConfig xaxis(100, -10., 10.);
42 auto pHist = std::make_shared<RH1D>(xaxis);
43 auto pHist2 = std::make_shared<RH1D>(xaxis);
44
45 TRandom3 random;
46 Float_t px, py;
47
48 for (int n = 0; n < 10000; ++n) {
49 random.Rannor(px, py);
50 pHist->Fill(px - 2);
51 pHist2->Fill(py + 2);
52 }
53
54 // Create a canvas to be displayed.
55 auto canvas = RCanvas::Create(title);
56
57 canvas->Draw<RFrameTitle>(title);
58 canvas->Draw(pHist)->line.color = col;
59 canvas->Draw(pHist2)->line.color = RColor::kBlue;
60
61 int maxloop = 100;
62
63 canvas->Show();
64
65 std::cout << title << " started" <<std::endl;
66
67 for (int loop = 0; loop < maxloop; ++loop) {
68
69 for (int n = 0; n < 10000; ++n) {
70 random.Rannor(px, py);
71 pHist->Fill(px - 2);
72 pHist2->Fill(py + 2);
73 }
74
75 canvas->Modified();
76
77 canvas->Update();
78 canvas->Run(0.2); // let run canvas code for next 0.5 seconds
79
80 // if (loop == 0)
81 // canvas->SaveAs(title + "_first.png");
82 // if (loop == maxloop - 1)
83 // canvas->SaveAs(title + "_last.png");
84 }
85
86 std::cout << title << " completed" <<std::endl;
87
88 // remove from global list, will be destroyed with thread exit
89 canvas->Remove();
90}
91
92void rcanvas_mt(bool block_main_thread = true)
93{
94 if (block_main_thread) {
95 // let use special http thread to process requests, do not need main thread
96 // required while gSystem->ProcessEvents() will be blocked
97 gEnv->SetValue("WebGui.HttpThrd", "yes");
98
99 // let create special threads for data sending, optional
100 gEnv->SetValue("WebGui.SenderThrds", "yes");
101 }
102
104
105 // create instance in main thread, used to assign thread id as well
107
108 std::thread thrd1(draw_canvas, "First canvas", RColor::kRed);
109 std::thread thrd2(draw_canvas, "Second canvas", RColor::kBlue);
110 std::thread thrd3(draw_canvas, "Third canvas", RColor::kGreen);
111
112 if (block_main_thread) {
113 // wait until threads execution finished
114 thrd1.join();
115 thrd2.join();
116 thrd3.join();
117 } else {
118 // detach threads and return to CLING
119 thrd1.detach();
120 thrd2.detach();
121 thrd3.detach();
122 }
123}
float Float_t
Definition RtypesCore.h:57
R__EXTERN TEnv * gEnv
Definition TEnv.h:170
Objects used to configure the different axis types.
The color class.
Definition RColor.hxx:33
static std::shared_ptr< RWebWindowsManager > & Instance()
Returns default window manager Used to display all standard ROOT elements like TCanvas or TFitPanel.
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:507
const Int_t n
Definition legend1.C:16
void EnableThreadSafety()
Enable support for multi-threading within the ROOT code in particular, enables the global mutex to ma...
Definition TROOT.cxx:501