#include "TApplication.h" #include "TGClient.h" #include "TCanvas.h" #include "TF1.h" #include "TRandom.h" #include "TGButton.h" #include "TRootEmbeddedCanvas.h" #include "TH1F.h" #include "TFormula.h" #include "TTimer.h" #include "TGFrame.h" #include "TThread.h" class MyMainFrame : public TGMainFrame { private: TRootEmbeddedCanvas *fEcanvas; TRootEmbeddedCanvas *fEcanvas2; static TH1F *fH1f; TThread *fThread; // canvas used by the static thread method static TCanvas *fC1; public: MyMainFrame(const TGWindow *p, UInt_t w, UInt_t h); virtual ~MyMainFrame() { } void CloseWindow(); void DoDraw(); // thread method static void *ThreadFunc(void *ptr=0); ClassDef(MyMainFrame, 0) }; // initialize the static variables TH1F *MyMainFrame::fH1f = 0; TCanvas *MyMainFrame::fC1 = 0; //______________________________________________________________________ MyMainFrame::MyMainFrame(const TGWindow *p,UInt_t w,UInt_t h) : TGMainFrame(p,w,h) { // Creates widgets of the example SetCleanup(kDeepCleanup); fEcanvas = new TRootEmbeddedCanvas ("Ecanvas",this,200,200); AddFrame(fEcanvas, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY, 10,10,10,1)); fC1 = fEcanvas->GetCanvas(); fEcanvas2 = new TRootEmbeddedCanvas ("Ecanvas2",this,200,200); AddFrame(fEcanvas2, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY, 10,10,10,1)); TGHorizontalFrame *hframe=new TGHorizontalFrame(this, 200,40); TGTextButton *draw = new TGTextButton(hframe,"&Draw"); draw->Connect("Clicked()","MyMainFrame",this,"DoDraw()"); hframe->AddFrame(draw, new TGLayoutHints(kLHintsCenterX,5,5,3,4)); TGTextButton *exit = new TGTextButton(hframe,"&Exit ", "gApplication->Terminate()"); hframe->AddFrame(exit, new TGLayoutHints(kLHintsCenterX,5,5,3,4)); AddFrame(hframe,new TGLayoutHints(kLHintsCenterX,2,2,2,2)); // Sets window name and shows the main frame SetWindowName("Simple Example"); MapSubwindows(); Resize(GetDefaultSize()); MapWindow(); // set up the thread fThread = new TThread("MyThread", ThreadFunc, (void*)0); } //______________________________________________________________________ void MyMainFrame::DoDraw() { // Starts the thread if (fThread->GetState() != TThread::kRunningState) { fThread->Run(); fThread->SetCancelAsynchronous(); } } //______________________________________________________________________________ void MyMainFrame::CloseWindow() { // terminate and delete the thread, then quit the application fThread->Kill(); delete fThread; gApplication->Terminate(); } //______________________________________________________________________________ void *MyMainFrame::ThreadFunc(void* ptr) { // this method is called by the thread fC1->cd(); // create the histogram first if (!fH1f) { TFormula *form1 = new TFormula("form1","abs(sin(x)/x)"); TF1 *sqroot = new TF1("sqroot","x*gaus(0) + [3]*form1",0,10); sqroot->SetParameters(10,4,1,20); fH1f = new TH1F("h1f","Test random numbers",100,0,10); fH1f->SetFillColor(45); } fH1f->FillRandom("sqroot",10000); fH1f->Draw(); // infinite loop updating the histogram while (true) { if (fH1f) { // locking avoids data loss TThread::Lock(); fH1f->Reset(); fH1f->FillRandom("sqroot",10000); TThread::UnLock(); } fC1->Modified(); fC1->Update(); // sleep for 10 ms: sleep not necessary, slows things only a bit down // because the threads are actually doing nothing, which is eventually // very fast ;-) TThread::Sleep(0, 10000000); } return ptr; } #ifndef __CINT__ //______________________________________________________________________________ int main(int argc, char *argv[]) { TApplication theApp( "TestDatePlot", &argc, argv ); new MyMainFrame(gClient->GetRoot(), 800, 600); theApp.Run(); } #endif