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