# Updating and popping the TCanvas for each loop

**URL:** <https://root-forum.cern.ch/t/updating-and-popping-the-tcanvas-for-each-loop/60268>\
**Category:** ROOT\
**Tags:** hist\
**Created:** [July 19, 2024, 12:13am UTC](https://root-forum.cern.ch/t/updating-and-popping-the-tcanvas-for-each-loop/60268 "2024-07-19T00:13:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kyuyeong\_Hwang](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/kyuyeong_hwang/32/34986_2.png) [@Kyuyeong\_Hwang](https://root-forum.cern.ch/u/Kyuyeong_Hwang)\
**Post date:** [July 19, 2024, 12:13am UTC](https://root-forum.cern.ch/t/updating-and-popping-the-tcanvas-for-each-loop/60268/1 "2024-07-19T00:13:03Z")

</div>

Hi all,

Currently, I am trying to making monitoring system.  
We are taking data with a few hour length, and we want to check some histograms during data taking.

So, what I tried is fill the histogram ~O(1000) events and show it via the canvas like below.

```auto
TApplication* theApp = new TApplication("app", &argc, argv);
TCanvas* c = new TCanvas("", "");

while(1) {
  for (int i = 0; i < 1000; i++)
    aHist->Fill(value);
  
  c->cd();
  aHist->Draw("Hist");
  theApp->Run();
}

```

We want to use the compiled script, so we tried it with TApplication.

The plot for the first for-loop is well pop. But after that, it is stuck at `theApp->Run();`.

Is there any way to make it possible?

---

<div class="post-metadata">

**Author:** ![Kyuyeong\_Hwang](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/kyuyeong_hwang/32/34986_2.png) [@Kyuyeong\_Hwang](https://root-forum.cern.ch/u/Kyuyeong_Hwang)\
**Post date:** [July 19, 2024, 12:35am UTC](https://root-forum.cern.ch/t/updating-and-popping-the-tcanvas-for-each-loop/60268/2 "2024-07-19T00:35:53Z")

</div>

And also I tried this one with searching live updates for plot

```auto
TApplication* theApp = new TApplication("app", &argc, argv);
TCanvas* c = new TCanvas("", "");
TH1D* aHist = new TH1D(...);
aHist->Draw();

while(1) {
  for (int i = 0; i < 1000; i++)
    aHist->Fill(value);
  
  c->Update();
  c->Pad()->Draw();
}

```

It also does not help.

---

<div class="post-metadata">

**Author:** ![Kyuyeong\_Hwang](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/kyuyeong_hwang/32/34986_2.png) [@Kyuyeong\_Hwang](https://root-forum.cern.ch/u/Kyuyeong_Hwang)\
**Post date:** [July 19, 2024, 12:53am UTC](https://root-forum.cern.ch/t/updating-and-popping-the-tcanvas-for-each-loop/60268/3 "2024-07-19T00:53:32Z")

</div>

Okay… I make it works with

```auto
TApplication* theApp = new TApplication("app", &argc, argv);
theApp->SetReturnFromRun(true);
TCanvas* c = new TCanvas("", "");
TH1D* aHist = new TH1D(...);
aHist->Draw();

while(1) {
  for (int i = 0; i < 1000; i++)
    aHist->Fill(value);
  
  c->Update();
  c->Pad()->Draw();
  gSystem->ProcessEvents();
}

```

But after plot is pop, I cannot make any changes on the plot while the system is under loop. Like adjusting the axis range, rebin the plot and etc.  
Is there any possible solution for it? I’d very appreciate any suggestion!

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [July 19, 2024, 5:26am UTC](https://root-forum.cern.ch/t/updating-and-popping-the-tcanvas-for-each-loop/60268/4 "2024-07-19T05:26:56Z")

</div>

Hi Kyuyeong,

Welcome to the ROOT Community!

Yes: please have a look to the following snippet:

```cpp
using namespace std::this_thread; // sleep_for, sleep_until
using namespace std::chrono; // nanoseconds, system_clock, seconds

TApplication *app = new TApplication("app", argc, argv);

TCanvas* c = new TCanvas("", "");

auto aHist = new TH1D("h","h",128, 0, 64);
aHist->Draw("Hist");

while(1) {
  for (int i = 0; i < 64; i++)
    aHist->Fill(i);

  c->Modified(); 
  c->Update(); 
  gSystem->ProcessEvents();
  
  sleep_for(milliseconds(300));
}

```

I hope this gets you started!

Cheers,  
D

---

<div class="post-metadata">

**Author:** ![Kyuyeong\_Hwang](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/kyuyeong_hwang/32/34986_2.png) [@Kyuyeong\_Hwang](https://root-forum.cern.ch/u/Kyuyeong_Hwang)\
**Post date:** [July 19, 2024, 7:44am UTC](https://root-forum.cern.ch/t/updating-and-popping-the-tcanvas-for-each-loop/60268/5 "2024-07-19T07:44:01Z")

</div>

Hi Danilo,

Thank you for suggestion!  
Unfortunately, I already make a system sleep ~5 seconds with `gSystem->Sleep(5000);` right after updating the canvas.  
Also `std::this_thread::sleep_for(std::chrono::milliseconds(300))` does not works.  
Anyway, thank you very much!

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [July 19, 2024, 11:26am UTC](https://root-forum.cern.ch/t/updating-and-popping-the-tcanvas-for-each-loop/60268/6 "2024-07-19T11:26:30Z")

</div>

Sure, that was one of the many possible examples. Glad to read you solved the issue!

D

---

<div class="post-metadata">

**Author:** ![linev](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/2bfe46/32.png) [@linev](https://root-forum.cern.ch/u/linev)\
**Post date:** [July 19, 2024, 12:05pm UTC](https://root-forum.cern.ch/t/updating-and-popping-the-tcanvas-for-each-loop/60268/7 "2024-07-19T12:05:43Z")

</div>

> [@Kyuyeong\_Hwang](#):
>
> But after plot is pop, I cannot make any changes on the plot while the system is under loop. Like adjusting the axis range, rebin the plot and etc.

Why it is not possible?  
`gSystem->ProcessEvents()` returns fast and you can modify canvas or histograms as much as you want. Just not forget to invoke `c->Modified(); c-Update();` after your modifications.

Like:

```auto
TH1D* aHist = new TH1D(...);
aHist->Draw();

for (int i = 0; i < 1000; i++)
  aHist->Fill(value);
  
c->Modified(); 
c->Update();
gSystem->ProcessEvents();
  

aHist->Rebin(2);
c->Modified(); 
c->Update();

while(true) {
  gSystem->Sleep(10);
  gSystem->ProcessEvents();
}

```

---

<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:** [August 2, 2024, 12:06pm UTC](https://root-forum.cern.ch/t/updating-and-popping-the-tcanvas-for-each-loop/60268/8 "2024-08-02T12:06:18Z")

</div>

Closed on August 2, 2024, 12:06pm UTC

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