Canvas will not show

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","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
			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.


After “Draw”, try to add:

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).

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:

        // (...) your code
        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



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


This MUST work:

// ...
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.

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


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, …).

You need to inform ROOT to process your requests:

if(nHits) gr_xy->Draw("AP");
cout << "\nNext Event?" << endl;

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


@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.

1 Like

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 …

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

I remember a similar strange issue (with the X11 backend) which was finally solved by a call to “ProcessEvents” repeated twice.
Maybe one needs to fix the “Update” method (in the Cocoa backend) 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).

I opened to follow up on this issue.

1 Like

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