forked from root-project/root
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreadsh2.C
More file actions
205 lines (185 loc) · 5.28 KB
/
threadsh2.C
File metadata and controls
205 lines (185 loc) · 5.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/// \file
/// \ingroup tutorial_thread
/// 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.
///
/// \macro_code
///
/// \author Victor Perevovchikov
#include "TROOT.h"
#include "TCanvas.h"
#include "TRootCanvas.h"
#include "TFrame.h"
#include "TH1F.h"
#include "TRandom.h"
#include "TThread.h"
#include "TMethodCall.h"
TCanvas *c1, *c2;
TH1F *hpx, *total, *hmain, *s1, *s2;
TThread *thread1, *thread2, *threadj;
Bool_t finished;
void *handle1(void *)
{
int nfills = 10000;
int upd = 500;
TThread::Lock();
hpx = new TH1F("hpx", "This is the px distribution", 100, -4, 4);
hpx->SetFillColor(48);
TThread::UnLock();
Float_t px, py, pz;
gRandom->SetSeed();
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;
TThread::Lock();
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);
TThread::UnLock();
Float_t xs1, xs2, xmain;
gRandom->SetSeed();
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)");
rc->DontCallClose();
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)");
rc->DontCallClose();
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();
TThread::Ps();
while (!finished) {
gSystem->Sleep(100);
gSystem->ProcessEvents();
}
threadj->Join();
TThread::Ps();
delete thread1;
delete thread2;
delete threadj;
}