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