How to draw object immediately

Dear experts,
Could you explain how to draw object immediately?

TCanvas *c1 = new TCanvas("c1", "canvas", 500, 500);
TH1F *h1 = new TH1F("h1", "histo from a gaussian", 100, -5, 5);
h1->FillRandom("gaus", 10000);
h1->Draw();

c1->Update();
gSystem->Sleep(5000);

I see canvas, but hist appears only in 5 sec.

Thank you in advance.

Are you using a ROOT macro or a standalone program you link with the ROOT libraries ?

Try to add gSystem->ProcessEvents(); right before gSystem->Sleep(5000);

Dear Couet,
It is ROOT macro.

Dear Bertrand,
It does not work

void test()
{
    TCanvas *c1 = new TCanvas("c1", "canvas", 500, 500);
    TH1F *h1 = new TH1F("h1", "histo from a gaussian", 100, -5, 5);
    h1->FillRandom("gaus", 10000);
    h1->Draw();

    c1->Update();
    gSystem->ProcessEvents();
    gSystem->Sleep(5000);
}

Which ROOT version, which platform/OS, which compiler?

This is weird. I imagine you have this code inside a macro named test.C. You enter root and then type .x test.C. Then you should see the canvas with the histogram in it and you should get back the ROOT prompt . In principal you do not even need: any code after h1->Draw();.

Which ROOT version, which platform/OS, which compiler?

ROOT 5.34/36 (v5-34-36@v5-34-36, Apr 05 2016, 10:25:45 on linuxx8664gcc)

CINT/ROOT C/C++ Interpreter version 5.18.00, July 2, 2010
NAME="Scientific Linux"
VERSION="7.3 (Nitrogen)"
ID="rhel"
ID_LIKE="fedora"
VERSION_ID="7.3"
PRETTY_NAME="Scientific Linux 7.3 (Nitrogen)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:scientificlinux:scientificlinux:7.3:GA"
HOME_URL="http://www.scientificlinux.org//"
BUG_REPORT_URL="mailto:scientific-linux-devel@listserv.fnal.gov"

REDHAT_BUGZILLA_PRODUCT="Scientific Linux 7"
REDHAT_BUGZILLA_PRODUCT_VERSION=7.3
REDHAT_SUPPORT_PRODUCT="Scientific Linux"
REDHAT_SUPPORT_PRODUCT_VERSION="7.3"

To launch I use:

root -l
.x test.cpp

Well, sorry, but it works for me…

For me too … what do you get exactly ? a white canvas ? nothing ?
try to start with the option -n in case you have something weird in your rootlogon,C

Note that the canvas will be “unresponsive” during gSystem->Sleep(5000); (so it will NOT be redrawn / refreshed if its contents is “blanked” by the X11 system when switching active windows or desktops).

what do you get exactly ? a white canvas ? nothing ?

I write online monitor with two threads and GUI (standalone app).
In order to test some code I use simple macro with one thread.
I need opportunity to emulate to two threads, so gSystem->Sleep() is good choice for me.
But I need to update my canvas inside macro.

Yes, just white canvas.

Thanks!
But what is solution?

Try:

while (1) { gSystem->ProcessEvents(); gSystem->Sleep(100); }
1 Like

Dear Bertrand, Couet and Wile_E_Coyote
Thanks!!!

Also I found topic with the same solution from Bertrand).

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.