Loading [MathJax]/extensions/tex2jax.js
Logo ROOT  
Reference Guide
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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
28#include "TRandom3.h"
29#include "TEnv.h"
30#include "TROOT.h"
31
32#include <thread>
33
34// macro must be here while cling is not capable to load
35// library automatically for outlined function see ROOT-10336
36R__LOAD_LIBRARY(libROOTHistDraw)
37
38using namespace ROOT::Experimental;
39
40void draw_canvas(const std::string &title, RColor col)
41{
42 // Create histograms
43 RAxisConfig xaxis(100, -10., 10.);
44 auto pHist = std::make_shared<RH1D>(xaxis);
45 auto pHist2 = std::make_shared<RH1D>(xaxis);
46
47 TRandom3 random;
48 Float_t px, py;
49
50 for (int n = 0; n < 10000; ++n) {
51 random.Rannor(px, py);
52 pHist->Fill(px - 2);
53 pHist2->Fill(py + 2);
54 }
55
56 // Create a canvas to be displayed.
57 auto canvas = RCanvas::Create(title + " canvas");
58 canvas->Draw(pHist)->AttrLine().SetColor(col);
59 canvas->Draw(pHist2)->AttrLine().SetColor(RColor::kBlue);
60
61 int maxloop = 50;
62
63 canvas->Show();
64
65 printf("%s started\n", title.c_str());
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.5); // 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 printf("%s completed\n", title.c_str());
87
88 // remove from global list, will be destroyed with thread exit
89 canvas->Remove();
90}
91
92void draw_mt()
93{
94 gEnv->SetValue("WebGui.HttpThrd", "yes");
95 gEnv->SetValue("WebGui.SenderThrds", "yes");
96
98
99 // create instance in main thread, used to assign thread id as well
100 RWebWindowsManager::Instance();
101
102 std::thread thrd1(draw_canvas, "First", RColor::kRed);
103 std::thread thrd2(draw_canvas, "Second", RColor::kBlue);
104 std::thread thrd3(draw_canvas, "Third", RColor::kGreen);
105
106 thrd1.join();
107 thrd2.join();
108 thrd3.join();
109}
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
The color class.
Definition: RColor.hxx:32
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:549