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