- bug fix
[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}
64
239fdf4e 65void AliEveEventBuffer::CreateBufferThread() {
d63d227e 66 // cout << "hereherehere"<<endl;
239fdf4e 67 TThread * fThread = new TThread(AliEveEventBuffer::BufferThread, (void*) this);
68 fThread->Run();
69
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;
d63d227e 255 CreateBufferThread();
239fdf4e 256 SetBufferMonStarted(kTRUE);
afa52141 257 fTimer->Start(3000);
30e7579c 258}
259///___________________________________________________________________________________
260void AliEveEventBuffer::StopBufferMonitor() {
261 cout << "Stopping buffer mon"<<endl;
239fdf4e 262 SetBufferMonStarted(kFALSE);
30e7579c 263 fTimer->Stop();
264}
265
266
267// //_________________________________________________________________________________
268// Int_t AliEveEventBuffer::NavigateEventBufferBack() {
269// // see header file for class documentation
270
271// // -- reached the end of the buffer
272// if ( fNavigateBufferIdx == fBufferLowIdx )
273// return -1;
274
275// Int_t newIdx = fNavigateBufferIdx - 1;
276// if ( newIdx == -1 )
277// newIdx = BUFFERSIZE-1;
278
279// fCurrentBufferIdx = fNavigateBufferIdx = newIdx;
280
281// return newIdx;
282// }
283
284// //_______________________________________________________________
285// Int_t AliEveEventBuffer::NavigateEventBufferFwd() {
286// // see header file for class documentation
287
288// // -- reached the top of the buffer
289// if ( fNavigateBufferIdx == fBufferTopIdx )
290// return -1;
291
292// Int_t newIdx = fNavigateBufferIdx + 1;
293// if ( newIdx == BUFFERSIZE )
294// newIdx = 0;
295
296// fCurrentBufferIdx = fNavigateBufferIdx = newIdx;
297
298// return newIdx;
299// }
300
301// void AliEveEventBuffer::MonitorBuffer() {
302// //See header file for documentation
303// if( GetNAvailableEvents() < 10) {
304// StopBufferChecker();
305// StartLoop();
306// }
307// }
308
309// void AliEveEventBuffer::StartLoop() {
310// //See header file for documentation
311// fTimer->Start(2000);
312// }
313// void AliEveEventBuffer::StopLoop() {
314// //See header file for documentation
315// fTimer->Stop();
316// }
317
318// void AliEveEventBuffer::StartBufferChecker() {
319// //See header file for documentation
320// fBufferTimer->Start(2000);
321// }
322// void AliEveEventBuffer::StopBufferChecker() {
323// //See header file for documentation
324// fBufferTimer->Stop();
325// }
326
327// AliESDEvent * GetNextEvent() {
328
329// tree->GetEntry(fEvent++);
330
331// AliESDEvent * event = new AliESDEvent();
332// event->ReadFromTree(fTree);
333// if (event) {
334// return event;
335// } else {
336// cout << "error getting event" << endl;
337// return NULL;
338// }
339// }