-Fixed bug in offline buffer that deleted event object.
[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
39 fEventId = new Int_t[fBufferSize];
40
41
42}
43
44
45
46///_______________________________________________________________________
47AliEveEventBuffer::~AliEveEventBuffer() {
48 // see header file for class documentation
49
50 if ( fEventBuffer ) {
51 fEventBuffer->Clear();
52 delete fEventBuffer;
53 }
54 fEventBuffer = NULL;
55
56 if(fCurrentEvent)
57 delete fCurrentEvent;
58 fCurrentEvent = NULL;
59
60}
61
239fdf4e 62void AliEveEventBuffer::CreateBufferThread() {
63 cout << "hereherehere"<<endl;
64 TThread * fThread = new TThread(AliEveEventBuffer::BufferThread, (void*) this);
65 fThread->Run();
66
67}
68
30e7579c 69///___________________________________________________________________________
70void * AliEveEventBuffer::BufferThread(void * buffer) {
239fdf4e 71 cout <<"BufferThread : " <<endl;
b088c0a1 72 if(buffer) {
239fdf4e 73 if (!reinterpret_cast<AliEveEventBuffer*>(buffer)->GetBusy()) {
74 reinterpret_cast<AliEveEventBuffer*>(buffer)->MonitorBuffer();
75 } else {
76 cout << "busy"<<endl;
b088c0a1 77 }
239fdf4e 78
79 } else {
80 cout << "no buffer"<<endl;
b088c0a1 81 }
30e7579c 82 return (void*)0;
83}
84
85///_____________________________________________________________________________
86void AliEveEventBuffer::MonitorBuffer() {
87 cout << "Monitorbuffer: " << endl;
88 if(fBusy) {
89 cout << "Already called FetchEvent()" << endl;
90 return;
91 } else {
92 cout << "fbusy = false"<<endl;
93
94 if ( (CalculateDifference(fBIndex[kTop],fBIndex[kLast]) < fPreBuffer) ) {
95 fBusy = kTRUE;
96 FetchEvent();
97 fBusy = kFALSE;
98 } else {
99 //StopBufferMonitor();
100 fBusy = kFALSE;
101 }
102 }
103}
104
105///_______________________________________________________________________________
106TObject * AliEveEventBuffer::NextEvent() {
107 //See header file for documentation
30e7579c 108 cout << "In enxtevent"<<endl;
239fdf4e 109
110
111 // if(fBusy) {
112 // cout << "Event Buffer busy"<<endl;
113 // return NULL;
114 // }
115 // else SetBusy(kTRUE);
30e7579c 116 TObject * nextEvent = GetNextUnSeen();
239fdf4e 117 //SetBusy(kFALSE);
30e7579c 118 return nextEvent;
119}
120
121///______________________________________________________________________________
122TObject * 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
137///______________________________________________________________________________
138TObject * AliEveEventBuffer::Fwd() {
139 PrintIndeces();
140 if (fBIndex[kCurrent] == fBIndex[kLast]) {
141 cout<< "returning NULL"<<endl;
142 return NULL;
143 }
144
145 fBIndex[kCurrent] = CalculateNext(fBIndex[kCurrent]);
146 TObject * event = fEventBuffer->At(fBIndex[kCurrent]);
147 return event;
148}
149
150
151
152///________________________________________________________________________________
153TObject * AliEveEventBuffer::GetNextUnSeen() {
154 //See header file for documentation
239fdf4e 155 cout << "GetNextUnSeen"<<endl;
30e7579c 156 PrintIndeces();
157 if(CalculateDifference(fBIndex[kTop], fBIndex[kLast])) {
158 fBIndex[kLast] = CalculateNext(fBIndex[kLast]);
159 fBIndex[kCurrent] = fBIndex[kLast];
160 PrintIndeces();
161 return fEventBuffer->At(fBIndex[kCurrent]);
162 } else {
163 cout << "No new event available, only events in buffer available!"<<endl;
164 return NULL;
165 }
166}
167///_________________________________________________________________________________
168void AliEveEventBuffer::PrintIndeces() {
169 for(Int_t i = 0; i < kSize; i++) {
170 cout << i << ": " << fBIndex[i] << endl;
171 }
172}
173///_________________________________________________________________________________
174void AliEveEventBuffer::PrintBuffer() {
175 for(Int_t i = 0; i < 10; i++) {
176 AliESDEvent * event = dynamic_cast<AliESDEvent*>(fEventBuffer->At(i));
177 if(event) {
178 cout << i << ": " <<event << " " << event->GetEventNumberInFile() << endl;;
179 }
180 }
181}
182
183///____________________________________________________________________________________
184void AliEveEventBuffer::FetchEvent() {
185 cout << "FetchEvent " << endl;
186 TObject * event = GetEventFromSource();
187 if(event) AddToBuffer(event);
188 PrintIndeces();
189 cout << "FetchedEvent " << endl;
190
191}
192
193///_________________________________________________________________________________
194void AliEveEventBuffer::AddToBuffer(TObject * event) {
195 cout << "Add to buffer"<<endl;
196 if(!event) return;
197
198 fBIndex[kTop] = CalculateNext(fBIndex[kTop]);
199 //Delete the event already there (ok to delete as object, not aliesdevent, TList?)
239fdf4e 200 //TObject * object = fEventBuffer->At(fBIndex[kTop]);
201 fEventBuffer->RemoveAt(fBIndex[kTop]);
202 //if (object) delete object;
30e7579c 203 fEventBuffer->AddAt(event, fBIndex[kTop]);
204}
205
206
207
208///_____________________________________________________________________________________
209Int_t AliEveEventBuffer::CalculateNext(Int_t current) {
210 //See header file for documentation
211 current++;
212 if(current == fBufferSize) current = 0;
213 return current;
214}
215
216
217///_____________________________________________________________________________________
218Int_t AliEveEventBuffer::CalculatePrevious(Int_t current) {
219 //See header file for documentation
220 cout << "CalculatePrev: " << current;
221 current--;
222 if(current == -1) current += fBufferSize;
223 cout << "... " << current << endl;
224 return current;
225}
226
227///__________________________________________________________________________________
228Int_t AliEveEventBuffer::CalculateDifference(Int_t top, Int_t low) {
229 //See header file for documentation
230 if (top > low) {
231 // cout << "top > low"<<endl;
232 return (top - low);
233 } else if (top < low) {
234 // cout << "low < top"<<endl;
235 return (fBufferSize - low + top);
236 } else {
237 //cout << "calculated to 0"<<endl;
238 return 0;
239 }
240}
241
242///___________________________________________________________________________________
243void AliEveEventBuffer::StartBufferMonitor() {
244 //cout << "NOT !!! starting buffer mon"<<endl;
245 cout << "starting buffer mon"<<endl;
239fdf4e 246 SetBufferMonStarted(kTRUE);
30e7579c 247 fTimer->Start(5000);
248}
249///___________________________________________________________________________________
250void AliEveEventBuffer::StopBufferMonitor() {
251 cout << "Stopping buffer mon"<<endl;
239fdf4e 252 SetBufferMonStarted(kFALSE);
30e7579c 253 fTimer->Stop();
254}
255
256
257// //_________________________________________________________________________________
258// Int_t AliEveEventBuffer::NavigateEventBufferBack() {
259// // see header file for class documentation
260
261// // -- reached the end of the buffer
262// if ( fNavigateBufferIdx == fBufferLowIdx )
263// return -1;
264
265// Int_t newIdx = fNavigateBufferIdx - 1;
266// if ( newIdx == -1 )
267// newIdx = BUFFERSIZE-1;
268
269// fCurrentBufferIdx = fNavigateBufferIdx = newIdx;
270
271// return newIdx;
272// }
273
274// //_______________________________________________________________
275// Int_t AliEveEventBuffer::NavigateEventBufferFwd() {
276// // see header file for class documentation
277
278// // -- reached the top of the buffer
279// if ( fNavigateBufferIdx == fBufferTopIdx )
280// return -1;
281
282// Int_t newIdx = fNavigateBufferIdx + 1;
283// if ( newIdx == BUFFERSIZE )
284// newIdx = 0;
285
286// fCurrentBufferIdx = fNavigateBufferIdx = newIdx;
287
288// return newIdx;
289// }
290
291// void AliEveEventBuffer::MonitorBuffer() {
292// //See header file for documentation
293// if( GetNAvailableEvents() < 10) {
294// StopBufferChecker();
295// StartLoop();
296// }
297// }
298
299// void AliEveEventBuffer::StartLoop() {
300// //See header file for documentation
301// fTimer->Start(2000);
302// }
303// void AliEveEventBuffer::StopLoop() {
304// //See header file for documentation
305// fTimer->Stop();
306// }
307
308// void AliEveEventBuffer::StartBufferChecker() {
309// //See header file for documentation
310// fBufferTimer->Start(2000);
311// }
312// void AliEveEventBuffer::StopBufferChecker() {
313// //See header file for documentation
314// fBufferTimer->Stop();
315// }
316
317// AliESDEvent * GetNextEvent() {
318
319// tree->GetEntry(fEvent++);
320
321// AliESDEvent * event = new AliESDEvent();
322// event->ReadFromTree(fTree);
323// if (event) {
324// return event;
325// } else {
326// cout << "error getting event" << endl;
327// return NULL;
328// }
329// }