# SetBatch causes different graphics output for pngs

**URL:** <https://root-forum.cern.ch/t/setbatch-causes-different-graphics-output-for-pngs/19682>\
**Category:** ROOT\
**Created:** [August 12, 2015, 7:44pm UTC](https://root-forum.cern.ch/t/setbatch-causes-different-graphics-output-for-pngs/19682 "2015-08-12T19:44:25Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![sidebo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/8e8cbc/32.png) [@sidebo](https://root-forum.cern.ch/u/sidebo)\
**Post date:** [August 12, 2015, 7:44pm UTC](https://root-forum.cern.ch/t/setbatch-causes-different-graphics-output-for-pngs/19682/1 "2015-08-12T19:44:25Z")

</div>

I might be ignorant, but I thought gROOT.SetBatch only made sure the graphics is not drawn, without altering the graphics content in a saved figure? I’m experiencing different (poorer, it seems to be) quality for a png when having called SetBatch as compared to without. For a pdf this is not the case. Why is this?

I attach the two figures

 ![](https://root-forum.cern.ch/uploads/default/original/2X/8/8a0c6aa2f648d90a2949a3476f03c5f92f72eac4.png)  
  
 ![](https://root-forum.cern.ch/uploads/default/original/2X/1/1d732b525515acbbdd72cb9064006ff10606f3ca.png)

---

<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:** [August 13, 2015, 5:25am UTC](https://root-forum.cern.ch/t/setbatch-causes-different-graphics-output-for-pngs/19682/2 "2015-08-13T05:25:57Z")

</div>

can you provide a small example showing the problem ?

---

<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:** [August 13, 2015, 12:29pm UTC](https://root-forum.cern.ch/t/setbatch-causes-different-graphics-output-for-pngs/19682/3 "2015-08-13T12:29:55Z")

</div>

I think the problem begins with sizes of images. It seems to me that, after “gROOT-\>SetBatch(kTRUE);”, the PNG image is always several pixels smaller in both, horizontal and vertical, dimensions (and this problem possibly “propagates” from the canvas to its sub-pads).

---

<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:** [August 13, 2015, 12:48pm UTC](https://root-forum.cern.ch/t/setbatch-causes-different-graphics-output-for-pngs/19682/4 "2015-08-13T12:48:36Z")

</div>

Here [root.cern.ch/root/html604/TCanvas.html](https://root.cern.ch/root/html604/TCanvas.html) one can read:

At creation time, in interactive mode, the canvas size defines the size of the canvas window (including the window manager’s decoration). To define precisely the graphics area size of a canvas, the following four lines of code should be used:

```auto
   {
      Double_t w = 600;
      Double_t h = 600;
      TCanvas * c1 = new TCanvas("c", "c", w, h);
      c->SetWindowSize(w + (w - c->GetWw()), h + (h - c->GetWh()));
   }
```

in batch mode simply do:  
c-\>SetCanvasSize(w,h);

---

<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:** [August 13, 2015, 1:05pm UTC](https://root-forum.cern.ch/t/setbatch-causes-different-graphics-output-for-pngs/19682/5 "2015-08-13T13:05:34Z")

</div>

In other words, “c-\>SetWindowSize(w + (w - c-\>GetWw()), h + (h - c-\>GetWh()));” misbehaves in batch mode.  
To my surprise, in ROOT 5.34, “c-\>SetCanvasSize(w,h);” sets the canvas size ALSO in interactive / non-batch mode.

---

<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:** [August 13, 2015, 1:31pm UTC](https://root-forum.cern.ch/t/setbatch-causes-different-graphics-output-for-pngs/19682/6 "2015-08-13T13:31:24Z")

</div>

trying to reproduce it it seems to me it is more a matter of pad drawing order.  
I did a small reproducer .  
seems to me it is a clipping issue not a canvas dimension issue.

 ![](https://root-forum.cern.ch/uploads/default/original/2X/6/65ae4425777cfe33b8478b09e173c74c42c0d2af.png)

---

<div class="post-metadata">

**Author:** ![sidebo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/8e8cbc/32.png) [@sidebo](https://root-forum.cern.ch/u/sidebo)\
**Post date:** [August 13, 2015, 1:45pm UTC](https://root-forum.cern.ch/t/setbatch-causes-different-graphics-output-for-pngs/19682/7 "2015-08-13T13:45:53Z")

</div>

Here’s a small reproducer.

```cpp
{
  gROOT->Reset();
  gROOT->SetBatch(); // comment out to attain a nicer figure
  
  double x[] = {1., 2.};
  double y[] = {2., 4.};
  TGraph g(2, x, y);
  g.SetMarkerStyle(8);
  g.SetMarkerSize(2);

  TCanvas c("c","c",800,600);
  g.Draw("AP");
  c.SaveAs("fig.png");

}

```

with batch the png is 8kb and without it’s 12kb. I’m running root 6.04.00.

I’m not sure I’m following the discussion. Including c.SetCanvasSize(800,600) in batch mode doesn’t change anything.

---

<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:** [August 13, 2015, 1:51pm UTC](https://root-forum.cern.ch/t/setbatch-causes-different-graphics-output-for-pngs/19682/8 "2015-08-13T13:51:36Z")

</div>

I’m afraid [TCanvas::SetWindowSize](https://root.cern.ch/root/html534/TCanvas.html#TCanvas:SetWindowSize) is completely unreliable (it can misbehave even in interactive mode).  
When I try (ROOT 5.34 and 6.04.02):

```cpp
{
  Int_t w = 800;
  Int_t h = 600;
  TCanvas *c;
  gROOT->SetBatch(kFALSE);
  c = new TCanvas("c", "c", w, h); c->Modified(); c->Update();
  std::cout << c->GetWw() << " " << c->GetWh() << std::endl;
  c->SetWindowSize(w + (w - c->GetWw()), h + (h - c->GetWh()));
  c->Modified(); c->Update();
  std::cout << c->GetWw() << " " << c->GetWh() << std::endl;
  delete c;
  c = new TCanvas("c", "c", w, h); c->Modified(); c->Update();
  std::cout << c->GetWw() << " " << c->GetWh() << std::endl;
  c->SetCanvasSize(w, h);
  c->Modified(); c->Update();
  std::cout << c->GetWw() << " " << c->GetWh() << std::endl;
  delete c;
  gROOT->SetBatch(kTRUE);
  c = new TCanvas("c", "c", w, h); c->Modified(); c->Update();
  std::cout << c->GetWw() << " " << c->GetWh() << std::endl;
  c->SetWindowSize(w + (w - c->GetWw()), h + (h - c->GetWh()));
  c->Modified(); c->Update();
  std::cout << c->GetWw() << " " << c->GetWh() << std::endl;
  delete c;
  c = new TCanvas("c", "c", w, h); c->Modified(); c->Update();
  std::cout << c->GetWw() << " " << c->GetWh() << std::endl;
  c->SetCanvasSize(w, h);
  c->Modified(); c->Update();
  std::cout << c->GetWw() << " " << c->GetWh() << std::endl;
}

```

I sometimes get:

```nohighlight
798 573
800 600 SetWindowSize WORKS in interactive mode
798 573
800 600 SetCanvasSize WORKS in interactive mode
796 572
796 572 !!! SetWindowSize FAILS in batch mode !!!
796 572
800 600 SetCanvasSize WORKS in batch mode

```

and sometimes:

```nohighlight
798 573
798 573 !!! SetWindowSize FAILS in interactive mode !!!
798 573
800 600 SetCanvasSize WORKS in interactive mode
796 572
796 572 !!! SetWindowSize FAILS in batch mode !!!
796 572
800 600 SetCanvasSize WORKS in batch mode

```

BTW. I can switch from interactive to batch mode, but not the other way (I get “Warning in TCanvas::ResizePad: c width changed from 0 to 10” if I try it).

JIRA: [sft.its.cern.ch/jira/browse/ROOT-7552](https://sft.its.cern.ch/jira/browse/ROOT-7552)

---

<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:** [August 14, 2015, 7:25am UTC](https://root-forum.cern.ch/t/setbatch-causes-different-graphics-output-for-pngs/19682/9 "2015-08-14T07:25:13Z")

</div>

> [@](#):
>
> Here’s a small reproducer.

See what I get with your macro. On the left in batch mode on the right in interactive mode… seems to me it is the same.

 ![](https://root-forum.cern.ch/uploads/default/original/2X/2/2a5a08be560bbbe04d1b6a9e9ede9b3ab9e2f0e8.png)

---

<div class="post-metadata">

**Author:** ![amarcine](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/amarcine/32/17539_2.png) [@amarcine](https://root-forum.cern.ch/u/amarcine)\
**Post date:** [November 8, 2015, 8:24pm UTC](https://root-forum.cern.ch/t/setbatch-causes-different-graphics-output-for-pngs/19682/10 "2015-11-08T20:24:24Z")

</div>

Hello,

This is not strictly within the topic, but I think it is somehow related to it.

Working in batch mode with ROOT 5.34/32, I noticed that there is a difference in canvas size given by GetWh() and GetWw(), as well as in produced PDF file, depending on whether I build canvas with this code:

```auto
  TCanvas can1("", "", 700, 500);
```

or that code:

```auto
  TCanvas can2;
  can2.SetCanvasSize(700, 500);
```

From the quoted documentation of TCanvas about difference of graphics area between interactive and batch mode, I understood that in batch mode (that I use) there shouldn’t be difference in outcome of above code snippets.

Did I misunderstood the documentation or is it a bug? A bug possibly related to the topic of discussion?

Regards,  
Antoni

---

<div class="post-metadata">

**Author:** ![Pepe\_Le\_Pew](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pepe_le_pew/32/12365_2.png) [@Pepe\_Le\_Pew](https://root-forum.cern.ch/u/Pepe_Le_Pew)\
**Post date:** [November 8, 2015, 9:16pm UTC](https://root-forum.cern.ch/t/setbatch-causes-different-graphics-output-for-pngs/19682/11 "2015-11-08T21:16:10Z")

</div>

> [@SetBatch causes different graphics output for pngs](https://root-forum.cern.ch/t/setbatch-causes-different-graphics-output-for-pngs/19682/8):
>
> I’m afraid [TCanvas::SetWindowSize](https://root.cern.ch/root/html534/TCanvas.html#TCanvas:SetWindowSize) is completely unreliable (it can misbehave even in interactive mode). When I try (ROOT 5.34 and 6.04.02): { Int\_t w = 800; Int\_t h = 600; TCanvas \*c; gROOT-\>SetBatch(kFALSE); c = new TCanvas("c", "c", w, h); c-\>Modified(); c-\>Update(); std::cout \<\< c-\>GetWw() \<\< " " \<\< c-\>GetWh() \<\< std::endl; c-\>SetWindowSize(w + (w - c-\>GetWw()), h + (h - c-\>GetWh())); c-\>Modified(); c-\>Update(); std::cout \<\< c-\>GetWw() \<\< " " \<\< c-\>GetWh() \<\< std::endl; …

---

<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:** [November 9, 2015, 8:07am UTC](https://root-forum.cern.ch/t/setbatch-causes-different-graphics-output-for-pngs/19682/12 "2015-11-09T08:07:13Z")

</div>

See the end of the class description here: [root.cern.ch/doc/master/classTCanvas.html](https://root.cern.ch/doc/master/classTCanvas.html)

---

<div class="post-metadata">

**Author:** ![amarcine](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/amarcine/32/17539_2.png) [@amarcine](https://root-forum.cern.ch/u/amarcine)\
**Post date:** [November 9, 2015, 11:14am UTC](https://root-forum.cern.ch/t/setbatch-causes-different-graphics-output-for-pngs/19682/13 "2015-11-09T11:14:41Z")

</div>

By the end of class description you mean the fragment about window and drawing area size? This is precisely the fragment that I refered to in my post. I asked if I understand that fragment correctly if I work in batch mode only - that in batch mode there should be no difference between the Constructor and the SetCanvasSize.

---

<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:** [November 9, 2015, 1:00pm UTC](https://root-forum.cern.ch/t/setbatch-causes-different-graphics-output-for-pngs/19682/14 "2015-11-09T13:00:23Z")

</div>

Sorry I add not seen you were referring already to that.  
Yes i batch mode you need to use SetCanvasSize also:

```auto

$ root -b
  *******************************************
  * *
  * W E L C O M E to R O O T *
  * *
  * Version 5.34/35 2 October 2015 *
  * *
  * You are welcome to visit our Web site *
  * http://root.cern.ch *
  * *
  *******************************************

ROOT 5.34/35 (v5-34-34@v5-34-34, Oct 19 2015, 11:21:00 on macosx64)

CINT/ROOT C/C++ Interpreter version 5.18.00, July 2, 2010
Type ? for help. Commands must be C++ statements.
Enclose multiple statements between { }.
root [0] TCanvas can1("", "", 700, 500);
root [1] can1.GetWh()
(const unsigned int)472
root [2] can1.SetCanvasSize(700, 500);
root [3] can1.GetWh()
(const unsigned int)500
root [4] 

```

---

<div class="post-metadata">

**Author:** ![amarcine](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/amarcine/32/17539_2.png) [@amarcine](https://root-forum.cern.ch/u/amarcine)\
**Post date:** [November 9, 2015, 1:17pm UTC](https://root-forum.cern.ch/t/setbatch-causes-different-graphics-output-for-pngs/19682/15 "2015-11-09T13:17:18Z")

</div>

Is this intentional?

If yes, then maybe you can modify the fragment on this in the TCanvas documentation. Namely instead of

> [@](#):
>
> At creation time, in interactive mode, the canvas size defines the size of the canvas window

better is

> [@](#):
>
> At creation time the canvas size given in constructor defines the size of the canvas window

or

> [@](#):
>
> At creation time, no matter if in interactive or batch mode, the canvas size defines the size of the canvas window

And then later

> [@](#):
>
> To define precisely the graphics area size of a canvas in the interactive mode, the following four lines of code should be used: (…) and in the batch mode simply do: (…)

Finally, maybe add warning to the documentation of TCanvas::SetCanvasSize() that its parameters ww and wh have different meaning than in constructors (possibly explaining the difference due to window size).

---

<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:** [November 9, 2015, 1:33pm UTC](https://root-forum.cern.ch/t/setbatch-causes-different-graphics-output-for-pngs/19682/16 "2015-11-09T13:33:17Z")

</div>

Yes it is an early design decision/mistake…  
Thanks for your comments. I have included them in the documentation.

---

<div class="post-metadata">

**Author:** ![amarcine](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/amarcine/32/17539_2.png) [@amarcine](https://root-forum.cern.ch/u/amarcine)\
**Post date:** [November 9, 2015, 1:43pm UTC](https://root-forum.cern.ch/t/setbatch-causes-different-graphics-output-for-pngs/19682/17 "2015-11-09T13:43:47Z")

</div>

Oh. Ok.  
Thank you for updating the documentation.

---

<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 14, 2017, 6:36am UTC](https://root-forum.cern.ch/t/setbatch-causes-different-graphics-output-for-pngs/19682/18 "2017-09-14T06:36:12Z")

</div>

This is Kubuntu 14.04.5 LTS / x86\_64 with gcc 4.8.4 (so, running KDE desktop, if that matters) here.  
I tried [the macro from my previous post here](https://root-forum.cern.ch/t/setbatch-causes-different-graphics-output-for-pngs/19682/8) and I still get the same problem with the newest “v5-34-00-patches” and “6.10.04” (see the second line of every output -\> it’s sometimes “800 600” and sometimes “798 573”):

```auto
[...] $ for i in {1..10}; do root -l -n -q TestSetWindowSize.cxx; done
root [0] 
Processing TestSetWindowSize.cxx...
798 573
800 600
798 573
800 600
796 572
796 572
796 572
800 600
root [0] 
Processing TestSetWindowSize.cxx...
798 573
800 600
798 573
800 600
796 572
796 572
796 572
800 600
root [0] 
Processing TestSetWindowSize.cxx...
798 573
800 600
798 573
800 600
796 572
796 572
796 572
800 600
root [0] 
Processing TestSetWindowSize.cxx...
798 573
798 573
798 573
800 600
796 572
796 572
796 572
800 600
root [0] 
Processing TestSetWindowSize.cxx...
798 573
800 600
798 573
800 600
796 572
796 572
796 572
800 600
root [0] 
Processing TestSetWindowSize.cxx...
798 573
800 600
798 573
800 600
796 572
796 572
796 572
800 600
root [0] 
Processing TestSetWindowSize.cxx...
798 573
798 573
798 573
800 600
796 572
796 572
796 572
800 600
root [0] 
Processing TestSetWindowSize.cxx...
798 573
798 573
798 573
800 600
796 572
796 572
796 572
800 600
root [0] 
Processing TestSetWindowSize.cxx...
798 573
800 600
798 573
800 600
796 572
796 572
796 572
800 600
root [0] 
Processing TestSetWindowSize.cxx...
798 573
800 600
798 573
800 600
796 572
796 572
796 572
800 600

```

BTW. Even the newest [TCanvas](https://root.cern.ch/doc/master/classTCanvas.html) class description does not clearly say what one should expect from the [TCanvas::SetWindowSize](https://root.cern.ch/doc/master/classTCanvas.html#a03de689513900a38d3c8e45c30194501) in batch mode. See the “last line - 2” of every output above -\> there’s always “796 572”, so it seems to be completely ignored. Is this a bug or a “feature”?

---

<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 14, 2017, 3:57pm UTC](https://root-forum.cern.ch/t/setbatch-causes-different-graphics-output-for-pngs/19682/19 "2017-09-14T15:57:07Z")

</div>

> [@Wile\_E\_Coyote](#):
>
> I still get the same problem with the newest “v5-34-00-patches” and “6.10.04” (see the second line of every output -\> it’s sometimes “800 600” and sometimes “798 573”):

Dear Wile,

I see also (with the master and 5.34 version on Mac with the X11 backend) that the output varies when I execute the macro in a loop like you. I do not have this issue with the Cocoa backend. But Cocoa does not have the synchronisation mechanism X11 has. I found out that in `TRootCanvas::SetWindowSize` the synchronisation instructions were not the same one can see in `TASimage`. Making them the same fixes the issue for me. i.e.: I now get the exact same output when I execute le macro 10 times in a row. I just committed the fix both for the master and 5.34 version. I hope it will fixe the issue for you also.

Cheers,  
Olivier

---

<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 14, 2017, 5:31pm UTC](https://root-forum.cern.ch/t/setbatch-causes-different-graphics-output-for-pngs/19682/20 "2017-09-14T17:31:51Z")

</div>

I tried the newest “v5-34-00-patches” code but I still see this problem.

Try to run this “10-times-in-a-row” command several times and see if you always get it right (in my case it’s sometimes better, sometimes worse).

[Next page](https://root-forum.cern.ch/t/setbatch-causes-different-graphics-output-for-pngs/19682.md?page=2)
