30e7579c |
1 | #include <iostream> |
2 | |
3 | #include "TObjArray.h" |
4 | #include "TTimer.h" |
5 | #include "TThread.h" |
765675f2 |
6 | #include "TMutex.h" |
30e7579c |
7 | #include "AliEveEventBuffer.h" |
8 | |
9 | |
10 | //Not needed, only for debug |
11 | #include "AliESDEvent.h" |
12 | |
13 | using namespace std; |
14 | |
15 | ClassImp(AliEveEventBuffer) |
16 | |
17 | ///_______________________________________________________________________ |
18 | AliEveEventBuffer::AliEveEventBuffer() : |
19 | fBufferSize(10), |
20 | fPreBuffer(4), |
21 | fBusy(kFALSE), |
22 | fEventBuffer(NULL), |
23 | fCurrentEvent(NULL), |
24 | fBIndex(), |
25 | fTimer(NULL), |
b088c0a1 |
26 | fEventId(), |
1c2d08b8 |
27 | fBufferMonStarted(kFALSE), |
765675f2 |
28 | fThread(NULL), |
29 | fMutex(NULL) |
30e7579c |
30 | { |
31 | // see header file for class documentation |
239fdf4e |
32 | fEventBuffer = new TObjArray(fBufferSize, 0); |
33 | fEventBuffer->SetOwner(kFALSE); |
30e7579c |
34 | |
35 | for(int id = 0; id < kSize; id++) { |
36 | fBIndex[id] = -1; |
37 | } |
38 | |
39 | fTimer = new TTimer(); |
239fdf4e |
40 | fTimer->Connect("Timeout()", "AliEveEventBuffer", this, "CreateBufferThread()"); |
30e7579c |
41 | |
8676f4b1 |
42 | fEventId = new ULong64_t[fBufferSize]; |
43 | for(Int_t id = 0; id < fBufferSize; id++ ) { |
44 | fEventId[id] = -2; |
45 | } |
1c2d08b8 |
46 | |
47 | fThread = new TThread(AliEveEventBuffer::BufferThread, (void*) this); |
765675f2 |
48 | fMutex = new TMutex(); |
8676f4b1 |
49 | |
30e7579c |
50 | |
51 | } |
52 | |
53 | |
54 | |
55 | ///_______________________________________________________________________ |
56 | AliEveEventBuffer::~AliEveEventBuffer() { |
57 | // see header file for class documentation |
58 | |
59 | if ( fEventBuffer ) { |
60 | fEventBuffer->Clear(); |
61 | delete fEventBuffer; |
62 | } |
63 | fEventBuffer = NULL; |
64 | |
65 | if(fCurrentEvent) |
66 | delete fCurrentEvent; |
67 | fCurrentEvent = NULL; |
68 | |
69 | } |
48d5b578 |
70 | |
8b515ee4 |
71 | ///___________________________________________________________________________ |
239fdf4e |
72 | void AliEveEventBuffer::CreateBufferThread() { |
765675f2 |
73 | |
74 | |
1c2d08b8 |
75 | cout << "Threadexists: " << fThread->Exists() << endl; |
765675f2 |
76 | if( fMutex->TryLock() ) { |
910533c9 |
77 | cout << "Buffer is busy, no thread created"<< endl; |
765675f2 |
78 | return; |
48d5b578 |
79 | } else { |
910533c9 |
80 | if ( (CalculateDifference(fBIndex[kTop],fBIndex[kLast]) < fPreBuffer) ) { |
7e8a8b86 |
81 | SetBusy(kTRUE); |
1c2d08b8 |
82 | cout << "StartBufferThread()"<<endl; |
910533c9 |
83 | fThread->Run(); |
84 | cout << "Started BufferThread"<<endl; |
85 | } else { |
86 | cout << "Buffer is full already"<<endl; |
55f78c0d |
87 | fMutex->UnLock(); |
910533c9 |
88 | } |
89 | } |
239fdf4e |
90 | } |
1c2d08b8 |
91 | |
30e7579c |
92 | ///___________________________________________________________________________ |
93 | void * AliEveEventBuffer::BufferThread(void * buffer) { |
239fdf4e |
94 | cout <<"BufferThread : " <<endl; |
b088c0a1 |
95 | if(buffer) { |
239fdf4e |
96 | reinterpret_cast<AliEveEventBuffer*>(buffer)->MonitorBuffer(); |
239fdf4e |
97 | } else { |
98 | cout << "no buffer"<<endl; |
b088c0a1 |
99 | } |
30e7579c |
100 | return (void*)0; |
101 | } |
102 | |
103 | ///_____________________________________________________________________________ |
104 | void AliEveEventBuffer::MonitorBuffer() { |
9110310d |
105 | cout << "Monitorbuffer() "; |
7161ac51 |
106 | FetchEvent(); |
765675f2 |
107 | fMutex->UnLock(); |
108 | |
7161ac51 |
109 | cout << "done " << endl; |
30e7579c |
110 | } |
111 | |
d63d227e |
112 | |
30e7579c |
113 | ///_______________________________________________________________________________ |
114 | TObject * AliEveEventBuffer::NextEvent() { |
115 | //See header file for documentation |
910533c9 |
116 | cout << "NextEvent()"<<endl; |
30e7579c |
117 | TObject * nextEvent = GetNextUnSeen(); |
30e7579c |
118 | return nextEvent; |
119 | } |
120 | |
121 | ///______________________________________________________________________________ |
122 | TObject * AliEveEventBuffer::Back() { |
123 | cout << "go back"<<endl; |
124 | PrintIndeces(); |
125 | Int_t prevId = CalculatePrevious(fBIndex[kCurrent]); |
126 | if(prevId == fBIndex[kTop]) { |
127 | cout << "returning NULL" << endl; |
128 | return NULL; |
129 | } else { |
130 | fBIndex[kCurrent] = prevId; |
131 | PrintIndeces(); |
132 | cout <<"returning: "<< fBIndex[kCurrent] << " " << fEventBuffer->At(fBIndex[kCurrent]); |
133 | return fEventBuffer->At(fBIndex[kCurrent]); |
134 | } |
135 | } |
136 | |
8676f4b1 |
137 | |
138 | |
30e7579c |
139 | ///______________________________________________________________________________ |
140 | TObject * AliEveEventBuffer::Fwd() { |
141 | PrintIndeces(); |
142 | if (fBIndex[kCurrent] == fBIndex[kLast]) { |
143 | cout<< "returning NULL"<<endl; |
144 | return NULL; |
145 | } |
146 | |
147 | fBIndex[kCurrent] = CalculateNext(fBIndex[kCurrent]); |
148 | TObject * event = fEventBuffer->At(fBIndex[kCurrent]); |
149 | return event; |
150 | } |
151 | |
152 | |
153 | |
154 | ///________________________________________________________________________________ |
155 | TObject * AliEveEventBuffer::GetNextUnSeen() { |
156 | //See header file for documentation |
239fdf4e |
157 | cout << "GetNextUnSeen"<<endl; |
30e7579c |
158 | PrintIndeces(); |
159 | if(CalculateDifference(fBIndex[kTop], fBIndex[kLast])) { |
160 | fBIndex[kLast] = CalculateNext(fBIndex[kLast]); |
161 | fBIndex[kCurrent] = fBIndex[kLast]; |
162 | PrintIndeces(); |
163 | return fEventBuffer->At(fBIndex[kCurrent]); |
164 | } else { |
165 | cout << "No new event available, only events in buffer available!"<<endl; |
166 | return NULL; |
167 | } |
168 | } |
169 | ///_________________________________________________________________________________ |
170 | void AliEveEventBuffer::PrintIndeces() { |
171 | for(Int_t i = 0; i < kSize; i++) { |
172 | cout << i << ": " << fBIndex[i] << endl; |
173 | } |
174 | } |
175 | ///_________________________________________________________________________________ |
176 | void AliEveEventBuffer::PrintBuffer() { |
177 | for(Int_t i = 0; i < 10; i++) { |
178 | AliESDEvent * event = dynamic_cast<AliESDEvent*>(fEventBuffer->At(i)); |
179 | if(event) { |
180 | cout << i << ": " <<event << " " << event->GetEventNumberInFile() << endl;; |
181 | } |
182 | } |
183 | } |
184 | |
185 | ///____________________________________________________________________________________ |
186 | void AliEveEventBuffer::FetchEvent() { |
187 | cout << "FetchEvent " << endl; |
188 | TObject * event = GetEventFromSource(); |
8676f4b1 |
189 | ULong64_t eventId = GetEventIdFromSource(); |
190 | if(event) { |
191 | AddToBuffer(event); |
192 | fEventId[fBIndex[kTop]] = eventId; |
193 | } |
194 | |
30e7579c |
195 | PrintIndeces(); |
196 | cout << "FetchedEvent " << endl; |
197 | |
198 | } |
199 | |
200 | ///_________________________________________________________________________________ |
201 | void AliEveEventBuffer::AddToBuffer(TObject * event) { |
202 | cout << "Add to buffer"<<endl; |
203 | if(!event) return; |
204 | |
205 | fBIndex[kTop] = CalculateNext(fBIndex[kTop]); |
206 | //Delete the event already there (ok to delete as object, not aliesdevent, TList?) |
239fdf4e |
207 | //TObject * object = fEventBuffer->At(fBIndex[kTop]); |
208 | fEventBuffer->RemoveAt(fBIndex[kTop]); |
209 | //if (object) delete object; |
30e7579c |
210 | fEventBuffer->AddAt(event, fBIndex[kTop]); |
30e7579c |
211 | |
8676f4b1 |
212 | } |
30e7579c |
213 | |
214 | ///_____________________________________________________________________________________ |
215 | Int_t AliEveEventBuffer::CalculateNext(Int_t current) { |
216 | //See header file for documentation |
217 | current++; |
218 | if(current == fBufferSize) current = 0; |
219 | return current; |
220 | } |
221 | |
222 | |
223 | ///_____________________________________________________________________________________ |
224 | Int_t AliEveEventBuffer::CalculatePrevious(Int_t current) { |
225 | //See header file for documentation |
226 | cout << "CalculatePrev: " << current; |
227 | current--; |
228 | if(current == -1) current += fBufferSize; |
229 | cout << "... " << current << endl; |
230 | return current; |
231 | } |
232 | |
233 | ///__________________________________________________________________________________ |
234 | Int_t AliEveEventBuffer::CalculateDifference(Int_t top, Int_t low) { |
235 | //See header file for documentation |
236 | if (top > low) { |
237 | // cout << "top > low"<<endl; |
238 | return (top - low); |
239 | } else if (top < low) { |
240 | // cout << "low < top"<<endl; |
241 | return (fBufferSize - low + top); |
242 | } else { |
243 | //cout << "calculated to 0"<<endl; |
244 | return 0; |
245 | } |
246 | } |
247 | |
248 | ///___________________________________________________________________________________ |
249 | void AliEveEventBuffer::StartBufferMonitor() { |
250 | //cout << "NOT !!! starting buffer mon"<<endl; |
251 | cout << "starting buffer mon"<<endl; |
8b515ee4 |
252 | if(!GetBufferMonStarted()) { |
253 | CreateBufferThread(); |
254 | SetBufferMonStarted(kTRUE); |
765675f2 |
255 | fTimer->Start(15000); |
8b515ee4 |
256 | } else { |
257 | cout << "Stopping buffer monitor"<<endl; |
258 | fTimer->Stop(); |
259 | SetBufferMonStarted(kFALSE); |
260 | } |
30e7579c |
261 | } |
262 | ///___________________________________________________________________________________ |
263 | void AliEveEventBuffer::StopBufferMonitor() { |
264 | cout << "Stopping buffer mon"<<endl; |
239fdf4e |
265 | SetBufferMonStarted(kFALSE); |
30e7579c |
266 | fTimer->Stop(); |
267 | } |
268 | |
269 | |
270 | // //_________________________________________________________________________________ |
271 | // Int_t AliEveEventBuffer::NavigateEventBufferBack() { |
272 | // // see header file for class documentation |
273 | |
274 | // // -- reached the end of the buffer |
275 | // if ( fNavigateBufferIdx == fBufferLowIdx ) |
276 | // return -1; |
277 | |
278 | // Int_t newIdx = fNavigateBufferIdx - 1; |
279 | // if ( newIdx == -1 ) |
280 | // newIdx = BUFFERSIZE-1; |
281 | |
282 | // fCurrentBufferIdx = fNavigateBufferIdx = newIdx; |
283 | |
284 | // return newIdx; |
285 | // } |
286 | |
287 | // //_______________________________________________________________ |
288 | // Int_t AliEveEventBuffer::NavigateEventBufferFwd() { |
289 | // // see header file for class documentation |
290 | |
291 | // // -- reached the top of the buffer |
292 | // if ( fNavigateBufferIdx == fBufferTopIdx ) |
293 | // return -1; |
294 | |
295 | // Int_t newIdx = fNavigateBufferIdx + 1; |
296 | // if ( newIdx == BUFFERSIZE ) |
297 | // newIdx = 0; |
298 | |
299 | // fCurrentBufferIdx = fNavigateBufferIdx = newIdx; |
300 | |
301 | // return newIdx; |
302 | // } |
303 | |
304 | // void AliEveEventBuffer::MonitorBuffer() { |
305 | // //See header file for documentation |
306 | // if( GetNAvailableEvents() < 10) { |
307 | // StopBufferChecker(); |
308 | // StartLoop(); |
309 | // } |
310 | // } |
311 | |
312 | // void AliEveEventBuffer::StartLoop() { |
313 | // //See header file for documentation |
314 | // fTimer->Start(2000); |
315 | // } |
316 | // void AliEveEventBuffer::StopLoop() { |
317 | // //See header file for documentation |
318 | // fTimer->Stop(); |
319 | // } |
320 | |
321 | // void AliEveEventBuffer::StartBufferChecker() { |
322 | // //See header file for documentation |
323 | // fBufferTimer->Start(2000); |
324 | // } |
325 | // void AliEveEventBuffer::StopBufferChecker() { |
326 | // //See header file for documentation |
327 | // fBufferTimer->Stop(); |
328 | // } |
329 | |
330 | // AliESDEvent * GetNextEvent() { |
331 | |
332 | // tree->GetEntry(fEvent++); |
333 | |
334 | // AliESDEvent * event = new AliESDEvent(); |
335 | // event->ReadFromTree(fTree); |
336 | // if (event) { |
337 | // return event; |
338 | // } else { |
339 | // cout << "error getting event" << endl; |
340 | // return NULL; |
341 | // } |
342 | // } |