ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
threadsh2.C
Go to the documentation of this file.
1 //+ Example of a simple script creating 2 threads each with one canvas.
2 // This script can only be executed via ACliC .x threadsh2.C++.
3 // The canvases are saved in a animated gif file.
4 
5 #include "TROOT.h"
6 #include "TCanvas.h"
7 #include "TRootCanvas.h"
8 #include "TFrame.h"
9 #include "TH1F.h"
10 #include "TRandom.h"
11 #include "TThread.h"
12 #include "TMethodCall.h"
13 
15 TH1F *hpx, *total, *hmain, *s1, *s2;
18 
19 void *handle1(void *)
20 {
21  int nfills = 10000;
22  int upd = 500;
23 
24  TThread::Lock();
25  hpx = new TH1F("hpx", "This is the px distribution", 100, -4, 4);
26  hpx->SetFillColor(48);
28  Float_t px, py, pz;
29  gRandom->SetSeed();
30  for (Int_t i = 0; i < nfills; i++) {
31  gRandom->Rannor(px, py);
32  pz = px*px + py*py;
33  hpx->Fill(px);
34  if (i && (i%upd) == 0) {
35  if (i == upd) {
36  c1->cd();
37  hpx->Draw();
38  }
39  c1->Modified();
40  c1->Update();
41  gSystem->Sleep(10);
42  TMethodCall c(c1->IsA(), "Print", "");
43  void *arr[4];
44  arr[1] = &c;
45  arr[2] = (void *)c1;
46  arr[3] = (void*)"\"hpxanim.gif+50\"";
47  (*gThreadXAR)("METH", 4, arr, NULL);
48  }
49  }
50  c1->Modified();
51  c1->Update();
52  TMethodCall c(c1->IsA(), "Print", "");
53  void *arr[4];
54  arr[1] = &c;
55  arr[2] = (void *)c1;
56  arr[3] = (void*)"\"hpxanim.gif++\"";
57  (*gThreadXAR)("METH", 4, arr, NULL);
58  return 0;
59 }
60 
61 void *handle2(void *)
62 {
63  int nfills = 10000;
64  int upd = 500;
65 
66  TThread::Lock();
67  total = new TH1F("total","This is the total distribution",100,-4,4);
68  hmain = new TH1F("hmain","Main contributor",100,-4,4);
69  s1 = new TH1F("s1","This is the first signal",100,-4,4);
70  s2 = new TH1F("s2","This is the second signal",100,-4,4);
71  total->Sumw2(); // this makes sure that the sum of squares of weights will be stored
72  total->SetMarkerStyle(21);
73  total->SetMarkerSize(0.7);
74  hmain->SetFillColor(16);
75  s1->SetFillColor(42);
76  s2->SetFillColor(46);
78  Float_t xs1, xs2, xmain;
79  gRandom->SetSeed();
80  for (Int_t i = 0; i < nfills; i++) {
81  xmain = gRandom->Gaus(-1,1.5);
82  xs1 = gRandom->Gaus(-0.5,0.5);
83  xs2 = gRandom->Landau(1,0.15);
84  hmain->Fill(xmain);
85  s1->Fill(xs1,0.3);
86  s2->Fill(xs2,0.2);
87  total->Fill(xmain);
88  total->Fill(xs1,0.3);
89  total->Fill(xs2,0.2);
90  if (i && (i%upd) == 0) {
91  if (i == upd) {
92  c2->cd();
93  total->Draw("e1p");
94  hmain->Draw("same");
95  s1->Draw("same");
96  s2->Draw("same");
97  }
98  c2->Modified();
99  c2->Update();
100  gSystem->Sleep(10);
101  TMethodCall c(c2->IsA(), "Print", "");
102  void *arr[4];
103  arr[1] = &c;
104  arr[2] = (void *)c2;
105  arr[3] = (void*)"\"hsumanim.gif+50\"";
106  (*gThreadXAR)("METH", 4, arr, NULL);
107  }
108  }
109  total->Draw("sameaxis"); // to redraw axis hidden by the fill area
110  c2->Modified();
111  c2->Update();
112  // make infinite animation by adding "++" to the file name
113  TMethodCall c(c2->IsA(), "Print", "");
114  void *arr[4];
115  arr[1] = &c;
116  arr[2] = (void *)c2;
117  arr[3] = (void*)"\"hsumanim.gif++\"";
118  (*gThreadXAR)("METH", 4, arr, NULL);
119  return 0;
120 }
121 
122 void *joiner(void *)
123 {
124  thread1->Join();
125  thread2->Join();
126 
127  finished = kTRUE;
128 
129  return 0;
130 }
131 
133 {
134  // allow to close the canvas only after the threads are done
135  if (!finished) return;
136  if (id == 1) ((TRootCanvas *)c1->GetCanvasImp())->CloseWindow();
137  else if (id == 2) ((TRootCanvas *)c2->GetCanvasImp())->CloseWindow();
138 }
139 
140 #include "TClass.h"
141 
142 void threadsh2()
143 {
144 #ifdef __CINT__
145  printf("This script can only be executed via ACliC: .x threadsh2.C++\n");
146  return;
147 #endif
148 
149  if (gROOT->IsBatch()) {
150  return;
151  }
152  c1 = new TCanvas("c1","Dynamic Filling Example", 100, 30, 400, 300);
153  c1->SetFillColor(42);
154  c1->GetFrame()->SetFillColor(21);
155  c1->GetFrame()->SetBorderSize(6);
156  c1->GetFrame()->SetBorderMode(-1);
157  // connect to the CloseWindow() signal and prevent to close the canvas
158  // while the thread is running
159  TRootCanvas *rc = dynamic_cast<TRootCanvas*>(c1->GetCanvasImp());
160  if (!rc) return;
161 
162  rc->Connect("CloseWindow()", 0, 0,
163  "tryclosing(Int_t=1)");
164  rc->DontCallClose();
165 
166  c2 = new TCanvas("c2","Dynamic Filling Example", 515, 30, 400, 300);
167  c2->SetGrid();
168  // connect to the CloseWindow() signal and prevent to close the canvas
169  // while the thread is running
170  rc = dynamic_cast<TRootCanvas*>(c2->GetCanvasImp());
171  if (!rc) return;
172  rc->Connect("CloseWindow()", 0, 0,
173  "tryclosing(Int_t=2)");
174  rc->DontCallClose();
175 
176  finished = kFALSE;
177  //gDebug = 1;
178  gSystem->Unlink("hpxanim.gif");
179  gSystem->Unlink("hsumanim.gif");
180 
181  printf("Starting Thread 0\n");
182  thread1 = new TThread("t0", handle1, (void*) 0);
183  thread1->Run();
184  printf("Starting Thread 1\n");
185  thread2 = new TThread("t1", handle2, (void*) 1);
186  thread2->Run();
187  printf("Starting Joiner Thread \n");
188  threadj = new TThread("t4", joiner, (void*) 3);
189  threadj->Run();
190 
191  TThread::Ps();
192 
193  while (!finished) {
194  gSystem->Sleep(100);
196  }
197 
198  threadj->Join();
199  TThread::Ps();
200 
201  delete thread1;
202  delete thread2;
203  delete threadj;
204 }
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3159
virtual Bool_t ProcessEvents()
Process pending events (GUI, timers, sockets).
Definition: TSystem.cxx:420
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:460
Float_t pz
Definition: hprod.C:33
TThread * thread2
Definition: threadsh2.C:16
float Float_t
Definition: RtypesCore.h:53
virtual void SetBorderMode(Short_t bordermode)
Definition: TWbox.h:62
return c
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:235
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:659
#define gROOT
Definition: TROOT.h:344
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:570
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
void * handle2(void *)
Definition: threadsh2.C:61
Float_t py
Definition: hprod.C:33
virtual void SetSeed(UInt_t seed=0)
Set the random generator seed.
Definition: TRandom.cxx:568
void DontCallClose()
Typically call this method in the slot connected to the CloseWindow() signal to prevent the calling o...
Definition: TGFrame.cxx:1738
tuple xs2
Definition: hsum.py:47
TH1F * hmain
Definition: threadsh2.C:15
void * handle1(void *)
Definition: threadsh2.C:19
virtual int Unlink(const char *name)
Unlink, i.e. remove, a file.
Definition: TSystem.cxx:1294
virtual void Sleep(UInt_t milliSec)
Sleep milliSec milli seconds.
Definition: TSystem.cxx:441
TFrame * GetFrame()
Get frame.
Definition: TPad.cxx:2729
virtual void SetGrid(Int_t valuex=1, Int_t valuey=1)
Definition: TPad.h:326
TCanvas * c1
Definition: threadsh2.C:14
TH1F * s1
Definition: threadsh2.C:15
void * joiner(void *)
Definition: threadsh2.C:122
Method or function calling interface.
Definition: TMethodCall.h:41
Int_t Run(void *arg=0)
Start the thread.
Definition: TThread.cxx:552
TCanvas * c2
Definition: threadsh2.C:14
TThread * threadj
Definition: threadsh2.C:16
virtual void SetBorderSize(Short_t bordersize)
Definition: TWbox.h:63
tuple xmain
Definition: hsum.py:45
TThread * thread1
Definition: threadsh2.C:16
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:1135
R__EXTERN TSystem * gSystem
Definition: TSystem.h:545
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2878
virtual void SetFillColor(Color_t fcolor)
Definition: TAttFill.h:50
virtual void SetMarkerStyle(Style_t mstyle=1)
Definition: TAttMarker.h:53
Long_t Join(void **ret=0)
Join this thread.
Definition: TThread.cxx:499
static Int_t Lock()
Static method to lock the main thread mutex.
Definition: TThread.cxx:758
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
static void Ps()
Static method listing the existing threads.
Definition: TThread.cxx:829
virtual void SetMarkerSize(Size_t msize=1)
Definition: TAttMarker.h:54
TH1F * total
Definition: threadsh2.C:15
TH1F * hpx
Definition: threadsh2.C:15
The Canvas class.
Definition: TCanvas.h:48
static Int_t UnLock()
Static method to unlock the main thread mutex.
Definition: TThread.cxx:774
TCanvasImp * GetCanvasImp() const
Get canvas implementation pointer if any.
Definition: TCanvas.h:181
TH1F * s2
Definition: threadsh2.C:15
ClassImp(TMCParticle) void TMCParticle printf(": p=(%7.3f,%7.3f,%9.3f) ;", fPx, fPy, fPz)
void threadsh2()
Definition: threadsh2.C:142
Bool_t finished
Definition: threadsh2.C:17
Float_t px
Definition: hprod.C:33
virtual void Sumw2(Bool_t flag=kTRUE)
Create structure to store sum of squares of weights.
Definition: TH1.cxx:8350
#define NULL
Definition: Rtypes.h:82
tuple xs1
Definition: hsum.py:46
void tryclosing(Int_t id)
Definition: threadsh2.C:132
virtual void Update()
Update canvas pad buffers.
Definition: TCanvas.cxx:2179
const Bool_t kTRUE
Definition: Rtypes.h:91
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:340
void Modified(Bool_t flag=1)
Definition: TPad.h:407