]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EVE/EveHLT/AliEveEventBuffer.cxx
e7817e414f4f0a698a79640cb45ef0bb0100f569
[u/mrichter/AliRoot.git] / EVE / EveHLT / AliEveEventBuffer.cxx
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
12 using namespace std;
13
14 ClassImp(AliEveEventBuffer)
15
16 ///_______________________________________________________________________
17 AliEveEventBuffer::AliEveEventBuffer() :
18   fBufferSize(10),
19   fPreBuffer(4),
20   fBusy(kFALSE),
21   fEventBuffer(NULL),
22   fCurrentEvent(NULL),
23   fBIndex(),
24   fTimer(NULL),
25   fEventId(),
26   fBufferMonStarted(kFALSE)
27  {
28   // see header file for class documentation
29   fEventBuffer = new TObjArray(fBufferSize, 0);
30   fEventBuffer->SetOwner(kFALSE);
31   
32   for(int id = 0; id < kSize; id++) {
33     fBIndex[id] = -1;
34   }
35   
36   fTimer = new TTimer();
37   fTimer->Connect("Timeout()", "AliEveEventBuffer", this, "CreateBufferThread()");
38
39   fEventId = new ULong64_t[fBufferSize];
40   for(Int_t id = 0; id < fBufferSize; id++ ) {
41     fEventId[id] = -2;
42   }
43   
44
45 }
46
47
48
49 ///_______________________________________________________________________
50 AliEveEventBuffer::~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 ///___________________________________________________________________________
65 void AliEveEventBuffer::CreateBufferThread() {
66   cout << "CreateBufferThread()"<<endl;
67   TThread * fThread = new TThread(AliEveEventBuffer::BufferThread, (void*) this);
68   fThread->Run();
69   cout << "Done BufferThread"<<endl;
70 }
71
72 ///___________________________________________________________________________
73 void * AliEveEventBuffer::BufferThread(void * buffer) {
74   cout <<"BufferThread : " <<endl;
75   if(buffer) {
76     if (!reinterpret_cast<AliEveEventBuffer*>(buffer)->GetBusy()) {
77       reinterpret_cast<AliEveEventBuffer*>(buffer)->MonitorBuffer();
78     } else {
79       cout << "busy"<<endl;
80     }
81     
82   } else {
83     cout << "no buffer"<<endl;
84   }
85   return (void*)0;
86 }
87
88 ///_____________________________________________________________________________
89 void AliEveEventBuffer::MonitorBuffer() {
90   cout << "Monitorbuffer: " << endl;
91   if ( (CalculateDifference(fBIndex[kTop],fBIndex[kLast]) < fPreBuffer) ) {
92     if(GetBusy()) {
93       cout << "Already called FetchEvent()" << endl;
94       return;
95     } else {
96       fBusy = kTRUE;
97       FetchEvent();
98       fBusy = kFALSE;
99     }
100   } else {
101     //StopBufferMonitor();
102     
103     fBusy = kFALSE;
104   }
105 }
106
107
108 ///_______________________________________________________________________________
109 TObject * AliEveEventBuffer::NextEvent() {
110   //See header file for documentation
111   cout << "In enxtevent"<<endl;
112
113
114   // if(fBusy) {
115   //   cout << "Event Buffer busy"<<endl;
116   //   return NULL;
117   // }
118   //  else SetBusy(kTRUE);
119   TObject * nextEvent = GetNextUnSeen();
120   //SetBusy(kFALSE);
121   return nextEvent;
122 }
123
124 ///______________________________________________________________________________
125 TObject * 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
140
141
142 ///______________________________________________________________________________
143 TObject * 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 ///________________________________________________________________________________
158 TObject * AliEveEventBuffer::GetNextUnSeen() {
159   //See header file for documentation
160   cout << "GetNextUnSeen"<<endl;
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 ///_________________________________________________________________________________
173 void AliEveEventBuffer::PrintIndeces() {
174   for(Int_t i = 0; i < kSize; i++) {
175     cout << i << ": " << fBIndex[i] << endl;
176   }
177 }
178 ///_________________________________________________________________________________
179 void 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 ///____________________________________________________________________________________
189 void AliEveEventBuffer::FetchEvent() {
190   cout << "FetchEvent " << endl;
191   TObject * event = GetEventFromSource();
192   ULong64_t eventId = GetEventIdFromSource();
193   if(event) {
194     AddToBuffer(event);
195     fEventId[fBIndex[kTop]] = eventId;  
196   }
197   
198   PrintIndeces();
199   cout << "FetchedEvent " << endl;
200   
201 }
202
203 ///_________________________________________________________________________________
204 void 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?)
210   //TObject * object = fEventBuffer->At(fBIndex[kTop]);
211   fEventBuffer->RemoveAt(fBIndex[kTop]);
212   //if (object) delete object;
213   fEventBuffer->AddAt(event, fBIndex[kTop]);
214
215 }
216
217 ///_____________________________________________________________________________________
218 Int_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 ///_____________________________________________________________________________________
227 Int_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 ///__________________________________________________________________________________
237 Int_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 ///___________________________________________________________________________________
252 void AliEveEventBuffer::StartBufferMonitor() {
253   //cout << "NOT !!! starting buffer mon"<<endl;
254   cout << "starting buffer mon"<<endl;
255   if(!GetBufferMonStarted()) {
256     CreateBufferThread();
257     SetBufferMonStarted(kTRUE);
258     fTimer->Start(3000);
259   } else {
260     cout << "Stopping buffer monitor"<<endl;
261     fTimer->Stop();
262     SetBufferMonStarted(kFALSE);
263   }
264 }
265 ///___________________________________________________________________________________
266 void AliEveEventBuffer::StopBufferMonitor() {
267   cout << "Stopping buffer mon"<<endl;
268   SetBufferMonStarted(kFALSE);
269   fTimer->Stop();
270 }
271
272
273 // //_________________________________________________________________________________
274 // Int_t AliEveEventBuffer::NavigateEventBufferBack() { 
275 //   // see header file for class documentation
276
277 //   // -- reached the end of the buffer
278 //   if ( fNavigateBufferIdx == fBufferLowIdx )
279 //     return -1;
280
281 //   Int_t newIdx = fNavigateBufferIdx - 1;
282 //   if ( newIdx == -1 )
283 //     newIdx = BUFFERSIZE-1;
284
285 //   fCurrentBufferIdx = fNavigateBufferIdx = newIdx;
286
287 //   return newIdx;
288 // }
289
290 // //_______________________________________________________________
291 // Int_t AliEveEventBuffer::NavigateEventBufferFwd() {
292 //   // see header file for class documentation
293
294 //   // -- reached the top of the buffer
295 //   if ( fNavigateBufferIdx == fBufferTopIdx )
296 //     return -1;
297
298 //   Int_t newIdx = fNavigateBufferIdx + 1;
299 //   if ( newIdx == BUFFERSIZE )
300 //     newIdx = 0;
301   
302 //   fCurrentBufferIdx = fNavigateBufferIdx = newIdx;
303
304 //   return newIdx;
305 // }
306
307 // void AliEveEventBuffer::MonitorBuffer() {
308 //   //See header file for documentation
309 //   if( GetNAvailableEvents() < 10) {
310 //     StopBufferChecker();
311 //     StartLoop();
312 //   }
313 // }
314
315 // void AliEveEventBuffer::StartLoop() {
316 //   //See header file for documentation
317 //   fTimer->Start(2000);
318 // }
319 // void AliEveEventBuffer::StopLoop() {
320 //   //See header file for documentation
321 //   fTimer->Stop();
322 // }
323
324 // void AliEveEventBuffer::StartBufferChecker() {
325 //   //See header file for documentation
326 //   fBufferTimer->Start(2000);
327 // }
328 // void AliEveEventBuffer::StopBufferChecker() {
329 //   //See header file for documentation
330 //   fBufferTimer->Stop();
331 // }
332
333 // AliESDEvent * GetNextEvent() {
334   
335 //   tree->GetEntry(fEvent++);
336
337 //   AliESDEvent * event = new AliESDEvent();
338 //   event->ReadFromTree(fTree);
339 //   if (event) {
340 //     return event;
341 //   } else {
342 //     cout << "error getting event" << endl;
343 //     return NULL;
344 //   }
345 // }