Logo ROOT  
Reference Guide
draw_mt.cxx File Reference

Detailed Description

This macro demonstrate usage of ROOT7 graphics from many threads Three different canvases in three different threads are started and regularly updated.

Extra thread created in background and used to run http protocol, in/out websocket communications and process http requests Main application thread (CLING interactive session) remains fully functional

/*************************************************************************
* Copyright (C) 1995-2015, Rene Brun and Fons Rademakers. *
* All rights reserved. *
* *
* For the licensing terms see $ROOTSYS/LICENSE. *
* For the list of contributors see $ROOTSYS/README/CREDITS. *
*************************************************************************/
#include "ROOT/RCanvas.hxx"
#include "TRandom3.h"
#include "TEnv.h"
#include "TROOT.h"
#include <thread>
#include <iostream>
// macro must be here while cling is not capable to load
// library automatically for outlined function see ROOT-10336
R__LOAD_LIBRARY(libROOTHistDraw)
using namespace ROOT::Experimental;
void draw_canvas(const std::string &title, RColor col)
{
// Create histograms
RAxisConfig xaxis(100, -10., 10.);
auto pHist = std::make_shared<RH1D>(xaxis);
auto pHist2 = std::make_shared<RH1D>(xaxis);
TRandom3 random;
Float_t px, py;
for (int n = 0; n < 10000; ++n) {
random.Rannor(px, py);
pHist->Fill(px - 2);
pHist2->Fill(py + 2);
}
// Create a canvas to be displayed.
auto canvas = RCanvas::Create(title);
canvas->Draw<RFrameTitle>(title);
canvas->Draw(pHist)->AttrLine().SetColor(col);
canvas->Draw(pHist2)->AttrLine().SetColor(RColor::kBlue);
int maxloop = 100;
canvas->Show();
std::cout << title << " started" <<std::endl;
for (int loop = 0; loop < maxloop; ++loop) {
for (int n = 0; n < 10000; ++n) {
random.Rannor(px, py);
pHist->Fill(px - 2);
pHist2->Fill(py + 2);
}
canvas->Modified();
canvas->Update();
canvas->Run(0.2); // let run canvas code for next 0.5 seconds
// if (loop == 0)
// canvas->SaveAs(title + "_first.png");
// if (loop == maxloop - 1)
// canvas->SaveAs(title + "_last.png");
}
std::cout << title << " completed" <<std::endl;
// remove from global list, will be destroyed with thread exit
canvas->Remove();
}
void draw_mt(bool block_main_thread = true)
{
if (block_main_thread) {
// let use special http thread to process requests, do not need main thread
// required while gSystem->ProcessEvents() will be blocked
gEnv->SetValue("WebGui.HttpThrd", "yes");
// let create special threads for data sending, optional
gEnv->SetValue("WebGui.SenderThrds", "yes");
}
// create instance in main thread, used to assign thread id as well
std::thread thrd1(draw_canvas, "First canvas", RColor::kRed);
std::thread thrd2(draw_canvas, "Second canvas", RColor::kBlue);
std::thread thrd3(draw_canvas, "Third canvas", RColor::kGreen);
if (block_main_thread) {
// wait until threads execution finished
thrd1.join();
thrd2.join();
thrd3.join();
} else {
// detach threads and return to CLING
thrd1.detach();
thrd2.detach();
thrd3.detach();
}
}
Date
2018-08-16
Warning
This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback is welcome!
Author
Sergey Linev

Definition in file draw_mt.cxx.

n
const Int_t n
Definition: legend1.C:16
TEnv::SetValue
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
ROOT::Experimental::RAxisConfig
Objects used to configure the different axis types.
Definition: RAxisConfig.hxx:35
RHistDrawable.hxx
TPad::Modified
void Modified(Bool_t flag=1) override
Definition: TPad.h:414
gEnv
R__EXTERN TEnv * gEnv
Definition: TEnv.h:171
ROOT::EnableThreadSafety
void EnableThreadSafety()
Enables the global mutex to make ROOT thread safe/aware.
Definition: TROOT.cxx:494
ROOT::Experimental::RColor
The color class.
Definition: RColor.hxx:33
RWebWindowsManager.hxx
ROOT::Experimental::RCanvas::Create
static std::shared_ptr< RCanvas > Create(const std::string &title)
Create new canvas instance.
Definition: RCanvas.cxx:103
TEnv.h
RCanvas.hxx
ROOT::Experimental::RColor::kBlue
static constexpr R__DLLEXPORT RGB_t kBlue
Definition: RColor.hxx:186
TROOT.h
TRandom::Rannor
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
TRandom3
Random number generator class based on M.
Definition: TRandom3.h:27
TCanvas::Show
void Show()
Show canvas.
Definition: TCanvas.cxx:2238
TRandom3.h
ROOT::Experimental::RColor::kGreen
static constexpr R__DLLEXPORT RGB_t kGreen
Definition: RColor.hxx:179
TCanvas::Draw
void Draw(Option_t *option="") override
Draw a canvas.
Definition: TCanvas.cxx:843
ROOT::Experimental
Definition: RDirectory.hxx:30
Float_t
R__LOAD_LIBRARY
#define R__LOAD_LIBRARY(LIBRARY)
Definition: Rtypes.h:472
TCanvas::Update
void Update() override
Update canvas pad buffers.
Definition: TCanvas.cxx:2502
ROOT::Experimental::RWebWindowsManager::Instance
static std::shared_ptr< RWebWindowsManager > & Instance()
Returns default window manager Used to display all standard ROOT elements like TCanvas or TFitPanel.
Definition: RWebWindowsManager.cxx:73
ROOT::Experimental::RColor::kRed
static constexpr R__DLLEXPORT RGB_t kRed
Definition: RColor.hxx:191