Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
27#include "ROOT/RCanvas.hxx"
28
29#include "TRandom3.h"
30#include "TEnv.h"
31#include "TROOT.h"
32
33#include <thread>
34
35// macro must be here while cling is not capable to load
36// library automatically for outlined function see ROOT-10336
37R__LOAD_LIBRARY(libROOTHistDraw)
38
39using namespace ROOT::Experimental;
40
41void draw_canvas(const std::string &title, RColor col)
42{
43 // Create histograms
44 RAxisConfig xaxis(100, -10., 10.);
45 auto pHist = std::make_shared<RH1D>(xaxis);
46 auto pHist2 = std::make_shared<RH1D>(xaxis);
47
48 TRandom3 random;
49 Float_t px, py;
50
51 for (int n = 0; n < 10000; ++n) {
52 random.Rannor(px, py);
53 pHist->Fill(px - 2);
54 pHist2->Fill(py + 2);
55 }
56
57 // Create a canvas to be displayed.
58 auto canvas = RCanvas::Create(title + " canvas");
59 canvas->Draw(pHist)->AttrLine().SetColor(col);
60 canvas->Draw(pHist2)->AttrLine().SetColor(RColor::kBlue);
61
62 int maxloop = 50;
63
64 canvas->Show();
65
66 printf("%s started\n", title.c_str());
67
68 for (int loop = 0; loop < maxloop; ++loop) {
69
70 for (int n = 0; n < 10000; ++n) {
71 random.Rannor(px, py);
72 pHist->Fill(px - 2);
73 pHist2->Fill(py + 2);
74 }
75
76 canvas->Modified();
77
78 canvas->Update();
79 canvas->Run(0.5); // let run canvas code for next 0.5 seconds
80
81 // if (loop == 0)
82 // canvas->SaveAs(title + "_first.png");
83 // if (loop == maxloop - 1)
84 // canvas->SaveAs(title + "_last.png");
85 }
86
87 printf("%s completed\n", title.c_str());
88
89 // remove from global list, will be destroyed with thread exit
90 canvas->Remove();
91}
92
93void draw_mt()
94{
95 gEnv->SetValue("WebGui.HttpThrd", "yes");
96 gEnv->SetValue("WebGui.SenderThrds", "yes");
97
99
100 // create instance in main thread, used to assign thread id as well
101 RWebWindowsManager::Instance();
102
103 std::thread thrd1(draw_canvas, "First", RColor::kRed);
104 std::thread thrd2(draw_canvas, "Second", RColor::kBlue);
105 std::thread thrd3(draw_canvas, "Third", RColor::kGreen);
106
107 thrd1.join();
108 thrd2.join();
109 thrd3.join();
110}
float Float_t
Definition RtypesCore.h:57
#define R__LOAD_LIBRARY(LIBRARY)
Definition Rtypes.h:472
R__EXTERN TEnv * gEnv
Definition TEnv.h:171
Objects used to configure the different axis types.
The color class.
Definition RColor.hxx:35
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:494