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
16
17/*************************************************************************
18 * Copyright (C) 1995-2025, 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"
28
29#include "TRandom3.h"
30#include "TEnv.h"
31#include "TROOT.h"
32#include "TH1.h"
33#include "TColor.h"
34
35#include <thread>
36#include <iostream>
37
38using namespace ROOT::Experimental;
39
40void draw_canvas(const std::string &title, Int_t col1)
41{
42 // Create histograms
43 auto hist1 = new TH1D("hist1", "hist1", 100, -10, 10);
44 auto hist2 = new TH1D("hist2", "hist2", 100, -10, 10);
45
46 hist1->SetLineColor(col1);
47 hist2->SetLineColor(kBlue);
48
50 Float_t px, py;
51
52 for (int n = 0; n < 10000; ++n) {
53 random.Rannor(px, py);
54 hist1->Fill(px - 2);
55 hist2->Fill(py + 2);
56 }
57
58 // Create a canvas to be displayed.
59 auto canvas = RCanvas::Create(title);
60
61 canvas->Draw<TObjectDrawable>(hist1, "");
62 canvas->Draw<TObjectDrawable>(hist2, "same");
63
64 int maxloop = 100;
65
66 canvas->Show();
67
68 std::cout << title << " started" << std::endl;
69
70 for (int loop = 0; loop < maxloop; ++loop) {
71
72 for (int n = 0; n < 10000; ++n) {
73 random.Rannor(px, py);
74 hist1->Fill(px - 2);
75 hist2->Fill(py + 2);
76 }
77
78 canvas->Modified();
79
80 canvas->Update();
81 canvas->Run(0.2); // let run canvas code for next 0.5 seconds
82
83 // if (loop == 0)
84 // canvas->SaveAs(title + "_first.png");
85 // if (loop == maxloop - 1)
86 // canvas->SaveAs(title + "_last.png");
87 }
88
89 std::cout << title << " completed" << std::endl;
90
91 // remove from global list, will be destroyed with thread exit
92 canvas->Remove();
93}
94
95void rcanvas_mt(bool block_main_thread = true)
96{
97 TH1::AddDirectory(false);
98
100 // let use special http thread to process requests, do not need main thread
101 // required while gSystem->ProcessEvents() will be blocked
102 gEnv->SetValue("WebGui.HttpThrd", "yes");
103
104 // let create special threads for data sending, optional
105 gEnv->SetValue("WebGui.SenderThrds", "yes");
106 }
107
109
110 // create instance in main thread, used to assign thread id as well
112
113 std::thread thrd1(draw_canvas, "First canvas", kRed);
114 std::thread thrd2(draw_canvas, "Second canvas", kBlue);
115 std::thread thrd3(draw_canvas, "Third canvas", kGreen);
116
117 if (block_main_thread) {
118 // wait until threads execution finished
119 thrd1.join();
120 thrd2.join();
121 thrd3.join();
122 } else {
123 // detach threads and return to CLING
124 thrd1.detach();
125 thrd2.detach();
126 thrd3.detach();
127 }
128}
int Int_t
Definition RtypesCore.h:45
float Float_t
Definition RtypesCore.h:57
@ kRed
Definition Rtypes.h:66
@ kGreen
Definition Rtypes.h:66
@ kBlue
Definition Rtypes.h:66
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
R__EXTERN TEnv * gEnv
Definition TEnv.h:170
Provides v7 drawing facilities for TObject types (TGraph, TH1, TH2, etc).
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
1-D histogram with a double per channel (see TH1 documentation)
Definition TH1.h:698
static void AddDirectory(Bool_t add=kTRUE)
Sets the flag controlling the automatic add of histograms in memory.
Definition TH1.cxx:1264
Random number generator class based on M.
Definition TRandom3.h:27
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