# Draw canvases produced in batch mode

**URL:** <https://root-forum.cern.ch/t/draw-canvases-produced-in-batch-mode/19893>\
**Category:** ROOT\
**Created:** [September 24, 2015, 1:41pm UTC](https://root-forum.cern.ch/t/draw-canvases-produced-in-batch-mode/19893 "2015-09-24T13:41:04Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nicola\_Mori](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/nicola_mori/32/23583_2.png) [@Nicola\_Mori](https://root-forum.cern.ch/u/Nicola_Mori)\
**Post date:** [September 24, 2015, 1:41pm UTC](https://root-forum.cern.ch/t/draw-canvases-produced-in-batch-mode/19893/1 "2015-09-24T13:41:04Z")

</div>

I need to produce many canvases in batch in my application. What I do is something like this:

```auto
gROOT->SetBatch(kTRUE);
std::vector<TCanvas*> canvases;
for(int i = 0; int < nCanvases; i++){
	TCanvas *c = new TCanvas;
	TH1F *h1 = new TH1F(...);
	TH1F *h2 = new TH1F(...);
	// Fill h1 and h2 and then:
	h1->Draw();
	h2->Draw("SAME");
	canvases.push_back(c);
}
gROOT->SetBatch(kFALSE);
```

At the end I have a vector of TCanvas. At this point the user may ask the GUI to show a certain canvas, so I need to draw it. The problem is that it seems that if a Canvas is produced when Root is in batch mode then it is not displayed. Calling TCanvas::SetBatch(kFALSE) before drawing results in a segfault. I attach a small example to illustrate the issue (compile with: g++ -g `root-config --cflags --libs` batch.cpp).  
Thanks for the help.  
[batch.cpp](https://root-forum.cern.ch/uploads/default/original/2X/f/f7c3241ceacf21893797af05237ed19cedb08467.cpp) (520 Bytes)

---

<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:** [September 24, 2015, 1:52pm UTC](https://root-forum.cern.ch/t/draw-canvases-produced-in-batch-mode/19893/2 "2015-09-24T13:52:45Z")

</div>

I reported the same here (I can switch from interactive to batch mode, but not the other way): [SetBatch causes different graphics output for pngs](https://root-forum.cern.ch/t/setbatch-causes-different-graphics-output-for-pngs/19682/8)

---

<div class="post-metadata">

**Author:** ![Nicola\_Mori](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/nicola_mori/32/23583_2.png) [@Nicola\_Mori](https://root-forum.cern.ch/u/Nicola_Mori)\
**Post date:** [September 24, 2015, 1:56pm UTC](https://root-forum.cern.ch/t/draw-canvases-produced-in-batch-mode/19893/3 "2015-09-24T13:56:01Z")

</div>

Thanks Coyote. That’s a pretty old post, must I assume that this is a known issue that will never be fixed?

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [September 24, 2015, 2:31pm UTC](https://root-forum.cern.ch/t/draw-canvases-produced-in-batch-mode/19893/4 "2015-09-24T14:31:02Z")

</div>

Yes the following macro does not produce any graphics.  
Intuitively it should… I guess

```auto
void can()
{
   gROOT->SetBatch(kTRUE);
   TCanvas *c = new TCanvas;
   TH1F *h1 = new TH1F("h1", "h1", 10, 0,10);
   h1->Draw();
   gROOT->SetBatch(kFALSE);
   c->Draw();
}
```

---

<div class="post-metadata">

**Author:** ![Nicola\_Mori](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/nicola_mori/32/23583_2.png) [@Nicola\_Mori](https://root-forum.cern.ch/u/Nicola_Mori)\
**Post date:** [September 24, 2015, 2:36pm UTC](https://root-forum.cern.ch/t/draw-canvases-produced-in-batch-mode/19893/5 "2015-09-24T14:36:52Z")

</div>

Olivier, I think the reason your macro does not produce anything is because a canvas created in batch mode is itself put in batch mode, which I assume makes the TCanvas::Draw call actually draw nothing. The canvas can in principle be brought back in interactive mode by TCanvas::SetBatch(kFALSE), but in this case a segfault is generated when calling Draw, as shown by the example I attached to my first post.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [September 24, 2015, 2:45pm UTC](https://root-forum.cern.ch/t/draw-canvases-produced-in-batch-mode/19893/6 "2015-09-24T14:45:05Z")

</div>

I tried:

```auto
void can()
{
   gROOT->SetBatch(kTRUE);
   TCanvas *c = new TCanvas;
   TH1F *h1 = new TH1F("h1", "h1", 10, 0,10);
   h1->Draw();
   gROOT->SetBatch(kFALSE);
   c->SetBatch(kTRUE);
   c->Draw();
}
```

I do not get any graphics with this… but no crash either (ROOT6 on Mac).

---

<div class="post-metadata">

**Author:** ![Nicola\_Mori](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/nicola_mori/32/23583_2.png) [@Nicola\_Mori](https://root-forum.cern.ch/u/Nicola_Mori)\
**Post date:** [September 24, 2015, 3:00pm UTC](https://root-forum.cern.ch/t/draw-canvases-produced-in-batch-mode/19893/7 "2015-09-24T15:00:07Z")

</div>

[quote=“couet”]I tried:

```auto
void can()
{
   gROOT->SetBatch(kTRUE);
   TCanvas *c = new TCanvas;
   TH1F *h1 = new TH1F("h1", "h1", 10, 0,10);
   h1->Draw();
   gROOT->SetBatch(kFALSE);
   c->SetBatch(kTRUE);
   c->Draw();
}
```

I do not get any graphics with this… but no crash either (ROOT6 on Mac).[/quote]  
That’s because your canvas is still in batch mode when you draw it. You should call c-\>SetBatch(kFALSE) before Draw (i.e. change the argument of the call to SetBatch). This gives a crash for me with Root 5.34.30.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [September 24, 2015, 3:03pm UTC](https://root-forum.cern.ch/t/draw-canvases-produced-in-batch-mode/19893/8 "2015-09-24T15:03:35Z")

</div>

I do:

```auto
void can()
{
   gROOT->SetBatch(kTRUE);
   TCanvas *c = new TCanvas;
   TH1F *h1 = new TH1F("h1", "h1", 10, 0,10);
   h1->Draw();
   gROOT->SetBatch(kFALSE);
   c->SetBatch(kFALSE);
   c->Draw();
}
```

No crash with root 6

---

<div class="post-metadata">

**Author:** ![Nicola\_Mori](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/nicola_mori/32/23583_2.png) [@Nicola\_Mori](https://root-forum.cern.ch/u/Nicola_Mori)\
**Post date:** [September 24, 2015, 3:08pm UTC](https://root-forum.cern.ch/t/draw-canvases-produced-in-batch-mode/19893/9 "2015-09-24T15:08:52Z")

</div>

OK, at least the crash is no longer a problem with Root 6, but the canvas is not drawn anyway, if I understand correctly. So my original problem is not solved by Root 6, and it seems to be a long-standing issue even if not so popular, maybe.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [September 24, 2015, 3:10pm UTC](https://root-forum.cern.ch/t/draw-canvases-produced-in-batch-mode/19893/10 "2015-09-24T15:10:16Z")

</div>

> [@](#):
>
> but the canvas is not drawn anyway

Yes that’s right. I will look at that.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [September 25, 2015, 8:51am UTC](https://root-forum.cern.ch/t/draw-canvases-produced-in-batch-mode/19893/11 "2015-09-25T08:51:48Z")

</div>

Actually may be the comment in TCanvas::SetBatch explains the fact it stays blocked in batch mode. It says:

```auto
/// Toggle batch mode. However, if the canvas is created without a window
/// then batch mode always stays set.
```

---

<div class="post-metadata">

**Author:** ![Nicola\_Mori](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/nicola_mori/32/23583_2.png) [@Nicola\_Mori](https://root-forum.cern.ch/u/Nicola_Mori)\
**Post date:** [September 25, 2015, 9:07am UTC](https://root-forum.cern.ch/t/draw-canvases-produced-in-batch-mode/19893/12 "2015-09-25T09:07:02Z")

</div>

Ok, so I have to find another way… I found that if I clone the canvas when Root is not in batch mode then the clone canvas is correctly drawn:

```auto
gROOT->SetBatch(kTRUE);
TCanvas *c = new TCanvas;
TH1F *h1 = new TH1F("h1", "h1", 10, 0,10);
h1->Draw();
gROOT->SetBatch(kFALSE);
c->DrawClone();
```

The clone process makes the clone canvas drawable. This makes me think that the streamer does not suffer from the problem which seems to plague the constructor call after the interactive mode has been restored for gROOT in your previous post. Since it is possible to obtain a drawable clone canvas starting from a non-drawable one I’d say that there should be a way to make the non-drawable canvas itself drawable, perhaps doing the same initialization steps (which are unknown to me) that the streamer does when it creates the clone canvas.

Sadly, drawing a clone of the canvas is not optimal for me since it seems that there are memory leaks related to the clone process which make my program crash in the long run.

---

<div class="post-metadata">

**Author:** ![Nicola\_Mori](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/nicola_mori/32/23583_2.png) [@Nicola\_Mori](https://root-forum.cern.ch/u/Nicola_Mori)\
**Post date:** [September 25, 2015, 9:09am UTC](https://root-forum.cern.ch/t/draw-canvases-produced-in-batch-mode/19893/13 "2015-09-25T09:09:30Z")

</div>

[quote=“couet”]Actually may be the comment in TCanvas::SetBatch explains the fact it stays blocked in batch mode. It says:

`
/// Toggle batch mode. However, if the canvas is created without a window
/// then batch mode always stays set.
`[/quote]  
I understand, but it does not explain why if you create a new canvas after putting back gROOT in interactive mode then you get those warnings you reported in the first version of your previous post. Correct?

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [September 25, 2015, 9:12am UTC](https://root-forum.cern.ch/t/draw-canvases-produced-in-batch-mode/19893/14 "2015-09-25T09:12:14Z")

</div>

> [@](#):
>
> why if you create a new canvas after putting back gROOT in interactive mode then you get those warnings you reported in the first version of your previous post. Correct?

Correct. I need to look where these warnings comes from.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [September 28, 2015, 12:33pm UTC](https://root-forum.cern.ch/t/draw-canvases-produced-in-batch-mode/19893/15 "2015-09-28T12:33:16Z")

</div>

I notice that if you do:

```auto
root [0] gROOT->SetBatch(kTRUE);
root [1] TCanvas *c1 = new TCanvas;
root [2] gROOT->SetBatch(kFALSE);
root [3] TCanvas *c2 = new TCanvas;
Warning in <TCanvas::ResizePad>: c1_n2 width changed from 0 to 10
Warning in <TCanvas::ResizePad>: c1_n2 height changed from 0 to 10
```

Then you get the warning and no canvas c2. But if you start with a canvas not in batch then all is fine :

```auto
root [0] TCanvas *c1 = new TCanvas;
root [1] gROOT->SetBatch(kTRUE);
root [2] TCanvas *c2 = new TCanvas;
root [3] gROOT->SetBatch(kFALSE);
root [4] TCanvas *c3 = new TCanvas;
```

---

<div class="post-metadata">

**Author:** ![Nicola\_Mori](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/nicola_mori/32/23583_2.png) [@Nicola\_Mori](https://root-forum.cern.ch/u/Nicola_Mori)\
**Post date:** [September 28, 2015, 12:42pm UTC](https://root-forum.cern.ch/t/draw-canvases-produced-in-batch-mode/19893/16 "2015-09-28T12:42:17Z")

</div>

Hi Olivier, I’d guess then that the first canvas initializes some global variables according to the current status of gROOT (batch or interactive). Then these global variables are not changed when the status of gROOT changes, event if their value depend on this status. This sound like a buggy behaviour, but knowing it maybe I can workaround it.  
Thanks again for your help.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [September 28, 2015, 12:57pm UTC](https://root-forum.cern.ch/t/draw-canvases-produced-in-batch-mode/19893/17 "2015-09-28T12:57:52Z")

</div>

Hi Nicola, Yes you are right. I am using this thread to keep track of my investigations as it is hard for me to work continuously on this bug …

---

<div class="post-metadata">

**Author:** ![Nicola\_Mori](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/nicola_mori/32/23583_2.png) [@Nicola\_Mori](https://root-forum.cern.ch/u/Nicola_Mori)\
**Post date:** [September 28, 2015, 1:03pm UTC](https://root-forum.cern.ch/t/draw-canvases-produced-in-batch-mode/19893/18 "2015-09-28T13:03:08Z")

</div>

OK, then I’ll keep watching the thread 🙂

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [September 29, 2015, 4:35pm UTC](https://root-forum.cern.ch/t/draw-canvases-produced-in-batch-mode/19893/19 "2015-09-29T16:35:24Z")

</div>

In the following case (1st canvas in batch mode) gVirtualX points to TVirtualX:

```auto
root [0] gROOT->SetBatch(kTRUE);
root [1] TCanvas *c1 = new TCanvas;
root [2] gROOT->SetBatch(kFALSE);
root [3] TCanvas *c2 = new TCanvas;
Warning in <TCanvas::ResizePad>: c1_n2 width changed from 0 to 10
Warning in <TCanvas::ResizePad>: c1_n2 height changed from 0 to 10
```

And the method TVirtualX::GetGeometry(…) returns 0 for window width and heigh…

Whereas is the following case (first canvas in interactive mode) gVirtualX points to TGX11:

```auto
root [0] TCanvas *c1 = new TCanvas;
root [1] gROOT->SetBatch(kTRUE);
root [2] TCanvas *c2 = new TCanvas;
root [3] gROOT->SetBatch(kFALSE);
root [4] TCanvas *c3 = new TCanvas;
```

And the method TGX11::GetGeometry(…) returns the correct window size.

GetGeometry() is called from TCanvas::Build().

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [September 30, 2015, 1:55pm UTC](https://root-forum.cern.ch/t/draw-canvases-produced-in-batch-mode/19893/20 "2015-09-30T13:55:58Z")

</div>

I think I found the fix. But that needs to be checked closely.

[Next page](https://root-forum.cern.ch/t/draw-canvases-produced-in-batch-mode/19893.md?page=2)
