#include #include #include #include #include #include bool flag = 1; void MyExec(); void RootDrawingBug() { //Make a TCanvas with 2 pads: // A pad to show 3 static vertical lines and a TExec to get mouse position, // A pad to with text that shows mouse position and can be updated with that TExec std::string name; TCanvas* cnvs = nullptr; TPad* pad1 = nullptr; TPad* pad2 = nullptr; name = "MyCanvas"; cnvs = (TCanvas*)gROOT->FindObject(name.c_str()); if(!cnvs) { cnvs = new TCanvas ( name.c_str(), name.c_str(), 0, 0, 600, 800 ); } name = "MyPad1"; pad1 = (TPad*)gROOT->FindObject(name.c_str()); if(!pad1) { pad1 = new TPad ( name.c_str(), name.c_str(), 0.0, 0.5, 1.0, 1.0 ); pad1->Range(0.0, 0.0, 1.0, 1.0); cnvs->cd(); pad1->Draw(); //Draw 3 vertical lines on MyPad1 on MyCanvas pad1->cd(); double temp; TLine line; for(temp = 0.25; temp < 1.0; temp += 0.25) { line.SetX1(temp); line.SetY1(0.0); line.SetX2(temp); line.SetY2(1.0); line.DrawClone(); } pad1->AddExec ( "MyExec", "MyExec()" ); } name = "MyPad2"; pad2 = (TPad*)gROOT->FindObject(name.c_str()); if(!pad2) { pad2 = new TPad ( name.c_str(), name.c_str(), 0.0, 0.0, 1.0, 0.5 ); pad2->Range(0.0, 0.0, 1.0, 1.0); cnvs->cd(); pad2->Draw(); //Add some text on MyPad2 on MyCanvas pad2->cd(); TText* disp_text = new TText ( 0.5, 0.5, Form("X: %0.3f Y: %0.3f", 0.0, 0.0) ); disp_text->SetTextAlign(22); disp_text->SetName("DispText"); //so we can find it later with gROOT->FindObject disp_text->Draw(); } //cnvs->Update(); cnvs->Show(); //cnvs->SetEditable(0); } void MyExec() { //"Update" MyPad2 to show mouse position float x = gPad->AbsPixeltoX(gPad->GetEventX()); float y = gPad->AbsPixeltoY(gPad->GetEventY()); TText* disp_text = (TText*)gROOT->FindObject("DispText"); if(disp_text)disp_text->SetTitle(Form("X: %0.3f Y: %0.3f", x, y)); TPad* disp_pad = (TPad*)gROOT->FindObject("MyPad2"); if(disp_pad)disp_pad->Update(); if(flag) { std::cout << std::endl; std::cout << "The mouse position is retrieved successfully and we've updated the text and pad correctly:" << std::endl; std::cout << disp_text->GetTitle() << std::endl; std::cout << disp_pad->GetName() << std::endl; std::cout << "However, \"MyPad2\" isn't updated even though we called Update()" << std::endl; std::cout << std::endl; } //Draw another pad when left-clicked if(gPad->GetEvent() != 11)return; std::string name; TCanvas* cnvs = nullptr; TPad* pad1 = nullptr; name = "MyExecCanvas"; cnvs = (TCanvas*)gROOT->FindObject(name.c_str()); if(!cnvs) { cnvs = new TCanvas ( name.c_str(), name.c_str(), 650, 0, 600, 800 ); } name = "MyExecPad1"; pad1 = (TPad*)gROOT->FindObject(name.c_str()); if(!pad1) { pad1 = new TPad ( name.c_str(), name.c_str(), 0.0, 0.0, 1.0, 1.0 ); pad1->Range(0.0, 0.0, 1.0, 1.0); cnvs->cd(); pad1->Draw(); pad1->cd(); double temp; TLine line; std::cout << "I cd the newly created \"MyExecPad1\"" << std::endl; std::cout << "We can see that gPad is indeed this pad:" << std::endl; std::cout << "gPad->GetName(): " << gPad->GetName() << std::endl; std::cout << "However, these lines appear on \"MyPad1\"" << std::endl; flag = 0; for(temp = 0.25; temp < 1.0; temp += 0.25) { line.SetX1(0.0); line.SetY1(temp); line.SetX2(1.0); line.SetY2(temp); line.DrawClone(); } } cnvs->Show(); }