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