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