TApplication::Run() stucks the terminal

ROOT Version: 6.22
_Platform: ubuntu
_Compiler: g++

Hi,
I have a code in c++ where I use TApplication to stop terminating the program before the graph Shows.
the code:

#include <iostream>
#include "TF1.h"
#include "TApplication.h"
using namespace std;
int main()
{
    TApplication app("app", nullptr, nullptr);
    TF1 *f1 = new TF1("f1","sin(x)", -5, 5);
    f1->SetLineColor(kBlue+1);
    f1->SetTitle("My graph; x");
    f1->Draw();
    app.Run();
    return 0;
}

It perfectly runs, but the terminal stucks before the program returns 0
How could I solve this??
Thank you,
Regards.

Hello,

Is your goal to plot the function and inspect it interactively? If that’s the case, it might be better that you draw your function from the ROOT prompt.

$ root
root [0] TF1 *f1 = new TF1("f1","sin(x)", -5, 5);
root [1] f1->SetLineColor(kBlue+1);
root [2] f1->SetTitle("My graph; x");
root [3] f1->Draw();
1 Like

Also, if a compiled version is a requirement for you, you could do the following:

#include "TF1.h"
#include "TApplication.h"
#include "TCanvas.h"
using namespace std;
int main()
{
        TApplication app("app", nullptr, nullptr);
        TCanvas* c = new TCanvas("c", "Something", 0, 0, 800, 600);
        TF1 *f1 = new TF1("f1","sin(x)", -5, 5);
        f1->SetLineColor(kBlue+1);
        f1->SetTitle("My graph; x");
        f1->Draw();
        c->Modified(); c->Update();
        printf("Type any digit and hit `Enter` to exit: "); Int_t a; std::cin>>a;
        return 0;
}

1 Like

Sir, I require to run the program by g++ compiler

Hello, sir basically you are holding the program by putting input request. Yes I can use that. But is there any other way to hold the program on untill I close the graph canvas, using TApplocation??
By the way thank you sir, it works.

Perhaps @bellenot or @couet know?

If you want to quit the application when closing the canvas:

#include <iostream>
#include "TF1.h"
#include "TApplication.h"
#include "TCanvas.h"
#include "TRootCanvas.h"

int main()
{
    TApplication app("app", nullptr, nullptr);
    TCanvas* c = new TCanvas("c", "Something", 0, 0, 800, 600);
    TF1 *f1 = new TF1("f1","sin(x)", -5, 5);
    f1->SetLineColor(kBlue+1);
    f1->SetTitle("My graph; x");
    f1->Draw();
    c->Modified(); c->Update();
    TRootCanvas *rc = (TRootCanvas *)c->GetCanvasImp();
    rc->Connect("CloseWindow()", "TApplication", gApplication, "Terminate()");
    app.Run();
    return 0;
}

And if you just want the ROOT prompt:

#include <iostream>
#include "TF1.h"
#include "TRint.h"
#include "TCanvas.h"

int main()
{
    TRint app("app", nullptr, nullptr);
    TCanvas* c = new TCanvas("c", "Something", 0, 0, 800, 600);
    TF1 *f1 = new TF1("f1","sin(x)", -5, 5);
    f1->SetLineColor(kBlue+1);
    f1->SetTitle("My graph; x");
    f1->Draw();
    c->Modified(); c->Update();
    app.Run();
    return 0;
}