I am trying to make a simple executable that displays a canvas with a certain plot (in interactive mode, so that the user zoom in/out and do other things), and then asks the user whether or not to save a file.
I know I can open the canvas and “stop” the executable from exiting with TApplication, but how can I do input/output (even the simplest cin) while the application is running?
here’s a sample of my code:
int main(int argv, char** argc){
/*
Here I create and fill a TH2F "h"
*/
TApplication* app = new TApplication("app",0,0);
// Draw and save TH2F
TCanvas* c = new TCanvas("c","c",800,600);
TRootCanvas* rc = (TRootCanvas*)c->GetCanvasImp();
h->Draw("colz");
app->Run();
// Ask user if they want to save 2d binning file
std::cout<<"Do you want to save the 2d binning file? (y/n)\n";
std::string answer;
std::cin>>answer;
if(answer=="y"){
//Saving binning file...
}
return 0;
}
I would like the part “ask the user if they want to save the binning file” to be within the TApplication.
Another solution I thought of would be to use Connect and terminate the Application when the canvas is closed, addingrc->Connect("CloseWindow()", "TApplication", gApplication, "Terminate()"); after app->Run(), but this closes my executable entirely, and does not go to the following code where I ask the input to the user.
Any suggestions?
_ROOT Version: 6.26
_Platform: mac
_Compiler:c++14
int main(int argv, char** argc)
{
/*
Here I create and fill a TH2F "h"
*/
TApplication* app = new TApplication("app",0,0);
// Draw and save TH2F
TCanvas* c = new TCanvas("c","c",800,600);
TRootCanvas* rc = (TRootCanvas*)c->GetCanvasImp();
h->Draw("colz");
// Ask user if they want to save 2d binning file
Int_t retval;
new TGMsgBox(gClient->GetRoot(), gClient->GetRoot(),
"Question", "Do you want to save the 2d binning file?",
kMBIconQuestion, kMBYes | kMBNo | kMBCancel, &retval);
if (retval == kMBYes)
std::cout << "Saving binning file..." << std::endl;
app->Run();
return 0;
}
// Ask user if they want to save 2d binning file
std::cout<<"Do you want to save the 2d binning file? (y/n)\n";
std::string answer;
while (1) {
if (_kbhit()) {
std::cin>>answer;
if(answer=="y"){
std::cout << "Saving binning file..." << std::endl;
//Saving binning file...
}
}
gSystem->ProcessEvents();
gSystem->Sleep(20);
}