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