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