Cannot display canvas while waiting for user input

Hi ROOT Experts,

While writing a simple ROOT macro, I’ve noticed that I’m unable to display a canvas while I’m waiting for user input to a cin. A simple example is below (Save as Example.C and run as “root.exe Example.C”).

You’ll see that I am making a simple plot. When my macro pauses to get user input to a cin however, the canvas is unable to display.

Are there any ideas to work around this?

Thanks in advance,
Will

Example(){

   TH1F *h1 = new TH1F("h1", "h1 title", 4, 0, 4);
   h1->Fill(2);
   h1->Draw();

   bool WhileBool = 1;
   while(WhileBool){

      cout<<"Enter 0 to exit the macro. Enter 0 to stay in while loop: "<<endl;
      cin>>WhileBool;

   }

}

After “Draw” add “gPad->Modified(); gPad->Update();”,
You may also add “gSystem->ProcessEvents();” (sometimes calling it twice in a row is needed) in your “infinite loop”.
(Note that “cin>>WhileBool;” will make the canvas not-responding to any mouse actions.)

Thanks once again Wile E Coyote! Your solution works great!

Is it possible to work around that and be able to use the mouse on the canvas while waiting for input?

Example:

{
   TCanvas c1("c1");
   TFile f("hsimple.root");
   hpx->Draw();        gPad->WaitPrimitive();
   hpxpy->Draw();      gPad->WaitPrimitive();
   hprof->Draw();      gPad->WaitPrimitive();
   ntuple->Draw("px"); gPad->WaitPrimitive();
   ntuple->Draw("py"); gPad->WaitPrimitive();
   ntuple->Draw("pz"); gPad->WaitPrimitive();
}