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