Interactive canvas not shown on MacOS

Hello experts,

I would like to plot a graph that updates after each step in a for-loop. The MWE is simple and looks like this

#include <iostream>
#include <thread>
#include <chrono>
#include "TApplication.h"
#include "TAxis.h"
#include "TCanvas.h"
#include "TGraph.h"

int main ( ) {

        TApplication app("app",0,0);
        TGraph * myGraph = new TGraph();
        TCanvas *c = new TCanvas();
        c->cd();
        c->Update();

        myGraph->GetXaxis()->SetTitle("Tempo [s]");
        myGraph->GetYaxis()->SetTitle("Posizione x [m]");

        double t = 0;

        for (int step=0; step<100; step++) {
                std::cout << "t " << t << std::endl;

                myGraph->SetPoint(step,t,sin(t));
                myGraph->Draw("AL*");
                c->Modified();
                c->Update();
                std::this_thread::sleep_for(std::chrono::milliseconds(100));
                t = t+0.2;
        }

        app.Run();
        return 0;
}

and it is compiled with

g++ -o main main.cxx `root-config --libs` `root-config --cflags`

This codes works on linux, the canvas is opened as soon as the main script is called, and each step of the sine function is displayed after the interval. On Mac (both on Intel and on M1) instead the canvas is opened only at the end, when the for-loop is done. Is there any additional commands to be included to make the application works properly on Mac? Thanks


Linux version
ROOT Version: 6.28/10
Platform: linuxx8664gcc
Compiler: g++ (GCC) 11.3.0

Mac M1
ROOT Version: 6.28/04
Platform: macosxarm64


@couet can you give it a try?

On Mac, you might also need:

gSystem->ProcessEvents();

Where? on each step of the for-loop iteration or just at the start?

Sorry, it was not clear.
Replace

   c->Modified();
   c->Update();

by:

   c->Update();
   gSystem->ProcessEvents();

Thanks, indeed it works! (needed to include TSystem.h but fine)

1 Like

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