Logo ROOT   6.18/05
Reference Guide
draw_mt.cxx
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_v7
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-2015, 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
26#include "ROOT/RCanvas.hxx"
27
28R__LOAD_LIBRARY(libROOTWebDisplay);
29
30#include "TRandom3.h"
31#include "TEnv.h"
32#include "TROOT.h"
33
34#include <thread>
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 + " canvas");
56 canvas->Draw(pHist)->Line().SetColor(col);
57 canvas->Draw(pHist2)->Line().SetColor(RColor::kBlue);
58
59 int maxloop = 50;
60
61 canvas->Show();
62
63 printf("%s started\n", title.c_str());
64
65 for (int loop = 0; loop < maxloop; ++loop) {
66
67 for (int n = 0; n < 10000; ++n) {
68 random.Rannor(px, py);
69 pHist->Fill(px - 2);
70 pHist2->Fill(py + 2);
71 }
72
73 canvas->Modified();
74
75 canvas->Update();
76 canvas->Run(0.5); // let run canvas code for next 0.5 seconds
77
78 // if (loop == 0)
79 // canvas->SaveAs(title + "_first.png");
80 // if (loop == maxloop - 1)
81 // canvas->SaveAs(title + "_last.png");
82 }
83
84 printf("%s completed\n", title.c_str());
85
86 // remove from global list, will be destroyed with thread exit
87 canvas->Remove();
88}
89
90void draw_mt()
91{
92 gEnv->SetValue("WebGui.HttpThrd", "yes");
93 gEnv->SetValue("WebGui.SenderThrds", "yes");
94
96
97 std::thread thrd1(draw_canvas, "First", RColor::kRed);
98 std::thread thrd2(draw_canvas, "Second", RColor::kBlue);
99 std::thread thrd3(draw_canvas, "Third", RColor::kGreen);
100
101 thrd1.join();
102 thrd2.join();
103 thrd3.join();
104}
float Float_t
Definition: RtypesCore.h:53
#define R__LOAD_LIBRARY(LIBRARY)
Definition: Rtypes.h:473
@ kRed
Definition: Rtypes.h:64
@ kGreen
Definition: Rtypes.h:64
@ kBlue
Definition: Rtypes.h:64
R__EXTERN TEnv * gEnv
Definition: TEnv.h:171
Objects used to configure the different axis types.
Definition: RAxis.hxx:300
A color: Red|Green|Blue|Alpha, or a position in a RPalette.
Definition: RColor.hxx:28
virtual void SetValue(const char *name, const char *value, EEnvLevel level=kEnvChange, const char *type=0)
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:489
const Int_t n
Definition: legend1.C:16
void EnableThreadSafety()
Enables the global mutex to make ROOT thread safe/aware.
Definition: TROOT.cxx:548