Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
threadsh2.C File Reference

Detailed Description

Example of a simple script creating 2 threads each with one canvas.

This script can only be executed via ACliC .x threadsh2.C++. The canvases are saved in a animated gif file.

#include "TROOT.h"
#include "TCanvas.h"
#include "TRootCanvas.h"
#include "TFrame.h"
#include "TH1F.h"
#include "TRandom.h"
#include "TThread.h"
#include "TMethodCall.h"
TH1F *hpx, *total, *hmain, *s1, *s2;
TThread *thread1, *thread2, *threadj;
Bool_t finished;
void *handle1(void *)
{
int nfills = 10000;
int upd = 500;
hpx = new TH1F("hpx", "This is the px distribution", 100, -4, 4);
hpx->SetFillColor(48);
Float_t px, py, pz;
for (Int_t i = 0; i < nfills; i++) {
gRandom->Rannor(px, py);
pz = px*px + py*py;
hpx->Fill(px);
if (i && (i%upd) == 0) {
if (i == upd) {
c1->cd();
hpx->Draw();
}
c1->Modified();
c1->Update();
gSystem->Sleep(10);
TMethodCall c(c1->IsA(), "Print", "");
void *arr[4];
arr[1] = &c;
arr[2] = (void *)c1;
arr[3] = (void*)"\"hpxanim.gif+50\"";
(*gThreadXAR)("METH", 4, arr, NULL);
}
}
c1->Modified();
c1->Update();
TMethodCall c(c1->IsA(), "Print", "");
void *arr[4];
arr[1] = &c;
arr[2] = (void *)c1;
arr[3] = (void*)"\"hpxanim.gif++\"";
(*gThreadXAR)("METH", 4, arr, NULL);
return 0;
}
void *handle2(void *)
{
int nfills = 10000;
int upd = 500;
total = new TH1F("total","This is the total distribution",100,-4,4);
hmain = new TH1F("hmain","Main contributor",100,-4,4);
s1 = new TH1F("s1","This is the first signal",100,-4,4);
s2 = new TH1F("s2","This is the second signal",100,-4,4);
total->Sumw2(); // this makes sure that the sum of squares of weights will be stored
total->SetMarkerStyle(21);
total->SetMarkerSize(0.7);
hmain->SetFillColor(16);
s1->SetFillColor(42);
s2->SetFillColor(46);
Float_t xs1, xs2, xmain;
for (Int_t i = 0; i < nfills; i++) {
xmain = gRandom->Gaus(-1,1.5);
xs1 = gRandom->Gaus(-0.5,0.5);
xs2 = gRandom->Landau(1,0.15);
hmain->Fill(xmain);
s1->Fill(xs1,0.3);
s2->Fill(xs2,0.2);
total->Fill(xmain);
total->Fill(xs1,0.3);
total->Fill(xs2,0.2);
if (i && (i%upd) == 0) {
if (i == upd) {
c2->cd();
total->Draw("e1p");
hmain->Draw("same");
s1->Draw("same");
s2->Draw("same");
}
c2->Modified();
c2->Update();
gSystem->Sleep(10);
TMethodCall c(c2->IsA(), "Print", "");
void *arr[4];
arr[1] = &c;
arr[2] = (void *)c2;
arr[3] = (void*)"\"hsumanim.gif+50\"";
(*gThreadXAR)("METH", 4, arr, NULL);
}
}
total->Draw("sameaxis"); // to redraw axis hidden by the fill area
c2->Modified();
c2->Update();
// make infinite animation by adding "++" to the file name
TMethodCall c(c2->IsA(), "Print", "");
void *arr[4];
arr[1] = &c;
arr[2] = (void *)c2;
arr[3] = (void*)"\"hsumanim.gif++\"";
(*gThreadXAR)("METH", 4, arr, NULL);
return 0;
}
void *joiner(void *)
{
thread1->Join();
thread2->Join();
finished = kTRUE;
return 0;
}
void tryclosing(Int_t id)
{
// allow to close the canvas only after the threads are done
if (!finished) return;
if (id == 1) ((TRootCanvas *)c1->GetCanvasImp())->CloseWindow();
else if (id == 2) ((TRootCanvas *)c2->GetCanvasImp())->CloseWindow();
}
#include "TClass.h"
void threadsh2()
{
if (gROOT->IsBatch()) {
return;
}
c1 = new TCanvas("c1","Dynamic Filling Example", 100, 30, 400, 300);
c1->SetFillColor(42);
c1->GetFrame()->SetFillColor(21);
c1->GetFrame()->SetBorderSize(6);
c1->GetFrame()->SetBorderMode(-1);
// connect to the CloseWindow() signal and prevent to close the canvas
// while the thread is running
TRootCanvas *rc = dynamic_cast<TRootCanvas*>(c1->GetCanvasImp());
if (!rc) return;
rc->Connect("CloseWindow()", 0, 0,
"tryclosing(Int_t=1)");
c2 = new TCanvas("c2","Dynamic Filling Example", 515, 30, 400, 300);
c2->SetGrid();
// connect to the CloseWindow() signal and prevent to close the canvas
// while the thread is running
rc = dynamic_cast<TRootCanvas*>(c2->GetCanvasImp());
if (!rc) return;
rc->Connect("CloseWindow()", 0, 0,
"tryclosing(Int_t=2)");
finished = kFALSE;
//gDebug = 1;
gSystem->Unlink("hpxanim.gif");
gSystem->Unlink("hsumanim.gif");
printf("Starting Thread 0\n");
thread1 = new TThread("t0", handle1, (void*) 0);
thread1->Run();
printf("Starting Thread 1\n");
thread2 = new TThread("t1", handle2, (void*) 1);
thread2->Run();
printf("Starting Joiner Thread \n");
threadj = new TThread("t4", joiner, (void*) 3);
threadj->Run();
while (!finished) {
gSystem->Sleep(100);
}
threadj->Join();
delete thread1;
delete thread2;
delete threadj;
}
#define c(i)
Definition RSha256.hxx:101
#define s1(x)
Definition RSha256.hxx:91
int Int_t
Definition RtypesCore.h:45
const Bool_t kFALSE
Definition RtypesCore.h:101
bool Bool_t
Definition RtypesCore.h:63
float Float_t
Definition RtypesCore.h:57
const Bool_t kTRUE
Definition RtypesCore.h:100
static unsigned int total
#define gROOT
Definition TROOT.h:404
R__EXTERN TRandom * gRandom
Definition TRandom.h:62
R__EXTERN TSystem * gSystem
Definition TSystem.h:559
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition TAttFill.h:37
The Canvas class.
Definition TCanvas.h:23
void DontCallClose()
Typically call this method in the slot connected to the CloseWindow() signal to prevent the calling o...
Definition TGFrame.cxx:1780
1-D histogram with a float per channel (see TH1 documentation)}
Definition TH1.h:575
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition TH1.cxx:3351
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition TH1.cxx:3074
Method or function calling interface.
Definition TMethodCall.h:37
Bool_t Connect(const char *signal, const char *receiver_class, void *receiver, const char *slot)
Non-static method is used to connect from the signal of this object to the receiver slot.
Definition TQObject.cxx:869
virtual Double_t Gaus(Double_t mean=0, Double_t sigma=1)
Samples a random number from the standard Normal (Gaussian) Distribution with the given mean and sigm...
Definition TRandom.cxx:274
virtual void SetSeed(ULong_t seed=0)
Set the random generator seed.
Definition TRandom.cxx:608
virtual void Rannor(Float_t &a, Float_t &b)
Return 2 numbers distributed following a gaussian with mean=0 and sigma=1.
Definition TRandom.cxx:500
virtual Double_t Landau(Double_t mean=0, Double_t sigma=1)
Generate a random number following a Landau distribution with location parameter mu and scale paramet...
Definition TRandom.cxx:380
This class creates a main window with menubar, scrollbars and a drawing area.
Definition TRootCanvas.h:34
virtual void Sleep(UInt_t milliSec)
Sleep milliSec milli seconds.
Definition TSystem.cxx:440
virtual Bool_t ProcessEvents()
Process pending events (GUI, timers, sockets).
Definition TSystem.cxx:419
virtual int Unlink(const char *name)
Unlink, i.e.
Definition TSystem.cxx:1381
static void Ps()
Static method listing the existing threads.
Definition TThread.cxx:848
static Int_t UnLock()
Static method to unlock the main thread mutex.
Definition TThread.cxx:793
Long_t Join(void **ret=nullptr)
Join this thread.
Definition TThread.cxx:515
Int_t Run(void *arg=nullptr, const int affinity=-1)
Start the thread.
Definition TThread.cxx:571
static Int_t Lock()
Static method to lock the main thread mutex.
Definition TThread.cxx:777
return c1
Definition legend1.C:41
return c2
Definition legend2.C:14
Author
Victor Perevovchikov

Definition in file threadsh2.C.