Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files 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 
25 #include "ROOT/RHistDrawable.hxx"
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
36 R__LOAD_LIBRARY(libROOTHistDraw)
37 
38 using namespace ROOT::Experimental;
39 
40 void 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 
92 void draw_mt()
93 {
94  gEnv->SetValue("WebGui.HttpThrd", "yes");
95  gEnv->SetValue("WebGui.SenderThrds", "yes");
96 
97  ROOT::EnableThreadSafety();
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 }