# Drawing histograms on a TCanvas in a Loop

**URL:** <https://root-forum.cern.ch/t/drawing-histograms-on-a-tcanvas-in-a-loop/38050>\
**Category:** ROOT\
**Created:** [February 19, 2020, 2:29pm UTC](https://root-forum.cern.ch/t/drawing-histograms-on-a-tcanvas-in-a-loop/38050 "2020-02-19T14:29:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ilomi](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ilomi/32/21077_2.png) [@ilomi](https://root-forum.cern.ch/u/ilomi)\
**Post date:** [February 19, 2020, 2:29pm UTC](https://root-forum.cern.ch/t/drawing-histograms-on-a-tcanvas-in-a-loop/38050/1 "2020-02-19T14:29:52Z")

</div>

Hi

I’m trying to compare 2 histograms visually.

When drawing single histograms on a single canvas (or different canvases also) I have no problem.  
However, if I try to do it in a loop (try to change histograms through the time) - there is a problem:

- I can’t close the canvas with ‘x’
- There is no ‘menu bar’
- I can’t zoom in / zoom out
- Basically, I can do nothing - there is just picture of histograms

I can change method and try not to use for and while loop, but is there a way to fix this?  
Also, what is the reason for it?

Here is the example:

```auto
//Test histograms
    TH1D *histMassive[2];

    TH1D *h1 = new TH1D("h1", "Histo from a Gaussian", 100, -3, 3);
    h1->FillRandom("gaus", 10000);

    TH1D *h2 = new TH1D("h2", "Histo from a Gaussian", 100, -3, 3);
    h2->FillRandom("gaus", 10000);

    histMassive[0] = h1;
    histMassive[1] = h2;

    TCanvas *can1 = new TCanvas("canvas1");
    can1->Divide(2, 1);
    //--

    for (int i = 0; i < 2; i++)
    {
        can1->cd(1);
        histMassive[i]->Draw();
        can1->Update();

        TH1D *h = (TH1D *)histMassive[i]->Clone("hclone");
        can1->cd(2);

        int reb = 2;
        while (true)
        {
            int stopInt = 0;
            h->Rebin(reb);
            h->Draw();
            can1->Update();
            cout << "Enter 0 if it's OK:";
            cin >> stopInt;
            if (!stopInt)
                break;
            h = (TH1D *)hADC[i]->Clone("hclone");
            reb++;
        }
    }

```

\_ROOT Version: 6.18/04  
_Platform:_ kubuntu 18.04  
_Compiler:_ vs code

* * *

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [February 19, 2020, 3:41pm UTC](https://root-forum.cern.ch/t/drawing-histograms-on-a-tcanvas-in-a-loop/38050/2 "2020-02-19T15:41:58Z")

</div>

You cannot use “`cin >> stopInt;`”. You need to use some “non-blocking I/O” ([and “`gSystem->ProcessEvents();`” calls inside](https://root-forum.cern.ch/search?q=gSystem%20ProcessEvents%20order%3Alatest)).

---

<div class="post-metadata">

**Author:** ![ilomi](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ilomi/32/21077_2.png) [@ilomi](https://root-forum.cern.ch/u/ilomi)\
**Post date:** [February 19, 2020, 7:06pm UTC](https://root-forum.cern.ch/t/drawing-histograms-on-a-tcanvas-in-a-loop/38050/3 "2020-02-19T19:06:24Z")

</div>

I don’t quite understand why I cant use Blocking I/O -  
I want the user to choose if (s)he wants to “stay in loop” and make another step or not

P.S. Can you tell some NIO’s I can use here also?

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [February 19, 2020, 8:41pm UTC](https://root-forum.cern.ch/t/drawing-histograms-on-a-tcanvas-in-a-loop/38050/4 "2020-02-19T20:41:02Z")

</div>

The current GUI is single threaded, if the thread is blocked waiting for user input (`cin >> stopInt`) nothing will happen since no GUI code will be executed until it returns (and the outstanding events are processed (indirectly) by ProcessEvents). See also [TPad::WaitPrimitive](https://root.cern.ch/doc/master/classTPad.html#ab46ccbf6950adac842fed22cd4eb7e46)

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [March 4, 2020, 8:55pm UTC](https://root-forum.cern.ch/t/drawing-histograms-on-a-tcanvas-in-a-loop/38050/5 "2020-03-04T20:55:24Z")

</div>

Closed on March 4, 2020, 8:55pm UTC

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