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