# Canvas will not show

**URL:** <https://root-forum.cern.ch/t/canvas-will-not-show/27582>\
**Category:** ROOT\
**Created:** [January 16, 2018, 10:50pm UTC](https://root-forum.cern.ch/t/canvas-will-not-show/27582 "2018-01-16T22:50:50Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![lucianoristori](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/8797f3/32.png) [@lucianoristori](https://root-forum.cern.ch/u/lucianoristori)\
**Post date:** [January 16, 2018, 10:50pm UTC](https://root-forum.cern.ch/t/canvas-will-not-show/27582/1 "2018-01-16T22:50:50Z")

</div>

I am trying to draw a canvas with a TGraph inside a loop for some kind of simple event display, using cin to pause the program for each event.  
Nothing is shown, except for the last event in the loop which displays correctly. I get the same result with the C++ compiler or the interpreter.  
Here is the code with an example drawing a random TGraph:

```auto
#include "TRandom.h"
#include "TGraph.h"
#include "TCanvas.h"
#include <iostream>

void test(){

	TCanvas *eventCanvas = new TCanvas("eventCanvas","eventCanvas",100,100,500,500);
	unsigned markerStyle = 20;
	double markerSize = 1.0;		
	unsigned nEvents = 10;
	const unsigned int nHits = 10;
	double xg1[nHits], yg1[nHits];
	
	// begin loop on events 
	
	for(unsigned int iEvt = 0; iEvt != nEvents; ++iEvt) {
			// begin loop on points to build random TGraph					
			for(int iH = 0; iH != nHits; ++iH){ 				
				xg1[iH] = 2.*gRandom->Rndm()-1.;
				yg1[iH] = 2.*gRandom->Rndm()-1.;				
			}// end loop on points
			
			TGraph *gr_xy = new TGraph(nHits, xg1, yg1);
			// draw the graph on eventCanvas
			eventCanvas->cd();
			gr_xy->SetMarkerStyle(20);
			gr_xy->SetMarkerSize(1.);
			if(nHits) gr_xy->Draw("AP");
	
                        // pause

			cout << endl << "Next Event?" << endl;
			string testString;
			cin >> testString;
			if(testString == "stop") return;
			cout << "OK" << endl;
			
		}// end loop on events	
	
	cout << "End of job" << endl;
			
}// end test

```

Thanks for reading down to here!  
I will appreciate any help you can give me.

Luciano

---

<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:** [January 16, 2018, 10:58pm UTC](https://root-forum.cern.ch/t/canvas-will-not-show/27582/2 "2018-01-16T22:58:32Z")

</div>

After “Draw”, try to add:

```auto
eventCanvas->Modified(); eventCanvas->Update();

```

BTW. When you post “source code” or “output” here, do remember to enclose them into two lines which contain just three characters ``` (see how your post has been edited above).

---

<div class="post-metadata">

**Author:** ![behrenhoff](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/behrenhoff/32/12285_2.png) [@behrenhoff](https://root-forum.cern.ch/u/behrenhoff)\
**Post date:** [January 16, 2018, 11:02pm UTC](https://root-forum.cern.ch/t/canvas-will-not-show/27582/3 "2018-01-16T23:02:50Z")

</div>

In addition, I suggest you change the `cin >> line` to `cin.get` as shown here - that makes it easier to get the next event - just hit enter:

```auto
        // (...) your code
        eventCanvas->Update();
        cout << "\nNext Event?" << endl;
        int key = cin.get();
        if (key == EOF || key == 'n' || key == 'N') return;
        if (key != '\n') cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cout << "OK" << endl;

    }// end loop on events

```

---

<div class="post-metadata">

**Author:** ![lucianoristori](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/8797f3/32.png) [@lucianoristori](https://root-forum.cern.ch/u/lucianoristori)\
**Post date:** [January 18, 2018, 6:29pm UTC](https://root-forum.cern.ch/t/canvas-will-not-show/27582/4 "2018-01-18T18:29:22Z")

</div>

> [@Canvas will not show](https://root-forum.cern.ch/t/canvas-will-not-show/27582):
>
> I am trying to draw a canvas with a TGraph inside a loop for some kind of simple event display, using cin to pause the program for each event. Nothing is shown, except for the last event in the loop which displays correctly. I get the same result with the C++ compiler or the interpreter. Here is the code with an example drawing a random TGraph: #include "TRandom.h" #include "TGraph.h" #include "TCanvas.h" #include \<iostream\> void test(){ TCanvas \*eventCanvas = new TCanvas("eventCanvas","e…

Unfortunately

```auto
eventCanvas->Modified();
eventCanvas->Update();

```

have no apparent effect.  
On top of that, the forum does not allow me to post anymore ("Error 403”).

Luciano

---

<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:** [January 18, 2018, 6:49pm UTC](https://root-forum.cern.ch/t/canvas-will-not-show/27582/5 "2018-01-18T18:49:11Z")

</div>

This MUST work:

```auto
// ...
if(nHits) gr_xy->Draw("AP");
eventCanvas->Modified(); eventCanvas->Update();
// ...

```

For the forum problem, try the following …  
Open the “Preferences” of your web browser (I assume here it’s the Firefox), go to “Privacy & Security”, find and click the link called “remove individual cookies” (in the “History” section). In the “Cookies Search” field, type “cern” and then click “Remove All Shown”. Afterwards, you should be able to login to the forum again.

---

<div class="post-metadata">

**Author:** ![lucianoristori](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/8797f3/32.png) [@lucianoristori](https://root-forum.cern.ch/u/lucianoristori)\
**Post date:** [January 18, 2018, 7:24pm UTC](https://root-forum.cern.ch/t/canvas-will-not-show/27582/6 "2018-01-18T19:24:11Z")

</div>

As I told you,

eventCanvas-\>Modified(); eventCanvas-\>Update();

has no effect.  
You can maybe try to copy and paste my code into your root to see if it is a specific problem of my installation.  
I am running ROOT 6.06/06 on a MacBook Air with Mac OX 10.11.16.

Also, after purging all CERN cookies I get “Unknown Error” when I try to login to the forum.

Thanks for your help

Luciano

---

<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:** [January 18, 2018, 7:33pm UTC](https://root-forum.cern.ch/t/canvas-will-not-show/27582/7 "2018-01-18T19:33:24Z")

</div>

I use Linux, sorry.

For the forum problem, try the following …  
Start your web browser, close all tabs and windows which display any “cern” related web pages and then exit the browser. Start your web browser again (no “cern” related web page should be displayed), then remove all “cern” related cookies as described in my previous post and exit the browser again. Finally, start your web browser again and try to login to the forum.  
Well, you may also try to “clear your recent history” (forms, cookies, cache, offline data, site preferences, …).

---

<div class="post-metadata">

**Author:** ![ksmith](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ksmith/32/12567_2.png) [@ksmith](https://root-forum.cern.ch/u/ksmith)\
**Post date:** [January 18, 2018, 7:38pm UTC](https://root-forum.cern.ch/t/canvas-will-not-show/27582/8 "2018-01-18T19:38:53Z")

</div>

You need to inform ROOT to process your requests:

```auto
//...
if(nHits) gr_xy->Draw("AP");
eventCanvas->Update();
gSystem->ProcessEvents();
cout << "\nNext Event?" << endl;
//...

```

---

<div class="post-metadata">

**Author:** ![lucianoristori](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/8797f3/32.png) [@lucianoristori](https://root-forum.cern.ch/u/lucianoristori)\
**Post date:** [January 18, 2018, 8:34pm UTC](https://root-forum.cern.ch/t/canvas-will-not-show/27582/9 "2018-01-18T20:34:35Z")

</div>

This is the correct solution!  
This just works!  
Thank you!

Luciano

---

<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:** [January 18, 2018, 8:41pm UTC](https://root-forum.cern.ch/t/canvas-will-not-show/27582/10 "2018-01-18T20:41:46Z")

</div>

@couet @Axel It seems to me that the `gSystem->ProcessEvents();` requirement on Mac OS X introduces a serious incompatibility. Maybe it’s a bug in some particular ROOT versions and/or graphics backend? I have no way to test it on Mac OS X.

---

<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:** [January 19, 2018, 8:50am UTC](https://root-forum.cern.ch/t/canvas-will-not-show/27582/11 "2018-01-19T08:50:01Z")

</div>

Is it with the Cocoa version on Mac ?  
If it is the case I think @tpochep may have an idea.  
I remember he posted some explanation on this forum regarding this question but I cannot find it now …

---

<div class="post-metadata">

**Author:** ![ksmith](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ksmith/32/12567_2.png) [@ksmith](https://root-forum.cern.ch/u/ksmith)\
**Post date:** [January 19, 2018, 2:37pm UTC](https://root-forum.cern.ch/t/canvas-will-not-show/27582/12 "2018-01-19T14:37:56Z")

</div>

I needed the `gSystem->ProcessEvents()` when executing the example on my Mac with Cocoa.

---

<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:** [January 19, 2018, 2:42pm UTC](https://root-forum.cern.ch/t/canvas-will-not-show/27582/13 "2018-01-19T14:42:00Z")

</div>

[I remember a similar strange issue (with the X11 backend) which was finally solved by a call to “ProcessEvents” repeated twice.](https://root-forum.cern.ch/t/setbatch-causes-different-graphics-output-for-pngs/19682/22)  
Maybe one needs to fix the “Update” method (in the Cocoa backend) in a similar way?

---

<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:** [January 19, 2018, 3:05pm UTC](https://root-forum.cern.ch/t/canvas-will-not-show/27582/14 "2018-01-19T15:05:28Z")

</div>

> [@Wile\_E\_Coyote](#):
>
> Maybe one needs to fix the “Update” method in a similar way?

That’s Cocoa … not X11… I remember @tpochep had an explanation for this case (he is the author of the Cocoa backend).

---

<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:** [January 19, 2018, 8:11pm UTC](https://root-forum.cern.ch/t/canvas-will-not-show/27582/15 "2018-01-19T20:11:24Z")

</div>

I opened [https://sft.its.cern.ch/jira/browse/ROOT-9200](https://sft.its.cern.ch/jira/browse/ROOT-9200) to follow up on this issue.

---

<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:** [February 2, 2018, 8:11pm UTC](https://root-forum.cern.ch/t/canvas-will-not-show/27582/16 "2018-02-02T20:11:25Z")

</div>

Closed on February 2, 2018, 8:11pm UTC

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