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