]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EVE/EveHLT/AliEveEventBuffer.cxx
Adding forward declarations and include files
[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   fEventBuffer(NULL),
22   fCurrentEvent(NULL),
23   fBIndex(),
24   fTimer(NULL),
25   fEventId(),
26   fBufferMonStarted(kFALSE),
27   fThread(NULL),
28   fMutex(NULL)
29  {
30   // see header file for class documentation
31   fEventBuffer = new TObjArray(fBufferSize, 0);
32   fEventBuffer->SetOwner(kFALSE);
33   
34   for(int id = 0; id < kSize; id++) {
35     fBIndex[id] = -1;
36   }
37   
38   fTimer = new TTimer();
39   fTimer->Connect("Timeout()", "AliEveEventBuffer", this, "CreateBufferThread()");
40
41   fEventId = new ULong64_t[fBufferSize];
42   for(Int_t id = 0; id < fBufferSize; id++ ) {
43     fEventId[id] = -2;
44   }
45
46   fThread = new TThread(AliEveEventBuffer::BufferThread, (void*) this);
47   fMutex = new TMutex();
48   
49
50 }
51
52
53
54 ///_______________________________________________________________________
55 AliEveEventBuffer::~AliEveEventBuffer() {
56   // see header file for class documentation
57   
58   if ( fEventBuffer ) {
59     fEventBuffer->Clear();
60     delete fEventBuffer;
61   }
62   fEventBuffer = NULL;
63
64   if(fCurrentEvent)
65     delete fCurrentEvent;
66   fCurrentEvent = NULL;
67
68 }
69
70 ///___________________________________________________________________________
71 void AliEveEventBuffer::CreateBufferThread() {
72     // see header file for class documentation
73   
74   cout << "Threadexists: " << fThread->Exists() << endl;
75   if( fMutex->TryLock() ) {
76     cout << "Buffer is busy, no thread created"<< endl;
77     return;
78   } else {
79     if ( (CalculateDifference(fBIndex[kTop],fBIndex[kLast]) < fPreBuffer) ) {
80       cout << "StartBufferThread()"<<endl;
81       fThread->Run();
82       cout << "Started BufferThread"<<endl;
83     } else { 
84       cout << "Buffer is full already"<<endl;
85       fMutex->UnLock();
86     }
87   }
88 }
89
90 ///___________________________________________________________________________
91 void * AliEveEventBuffer::BufferThread(void * buffer) {
92   // see header file for class documentation
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   // see header file for class documentation
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   // see header file for class documentation
124   cout << "go back"<<endl;
125   PrintIndeces();
126   Int_t prevId = CalculatePrevious(fBIndex[kCurrent]);
127   if(prevId == fBIndex[kTop]) {
128     cout << "returning NULL" << endl;
129     return NULL;
130   } else {
131     fBIndex[kCurrent] = prevId;
132     PrintIndeces();
133     cout <<"returning: "<< fBIndex[kCurrent] << " " << fEventBuffer->At(fBIndex[kCurrent]);
134     return fEventBuffer->At(fBIndex[kCurrent]);
135   }
136 }
137
138
139
140 ///______________________________________________________________________________
141 TObject * AliEveEventBuffer::Fwd() {
142   // see header file for class documentation
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   // see header file for class documentation
174   for(Int_t i = 0; i < kSize; i++) {
175     cout << i << ": " << fBIndex[i] << endl;
176   }
177 }
178 ///_________________________________________________________________________________
179 void AliEveEventBuffer::PrintBuffer() {
180   // see header file for class documentation
181   for(Int_t i = 0; i < 10; i++) {
182     AliESDEvent * event = dynamic_cast<AliESDEvent*>(fEventBuffer->At(i));
183     if(event) {
184       cout << i << ": " <<event << " " << event->GetEventNumberInFile() << endl;;
185     }
186   }
187 }
188
189 ///____________________________________________________________________________________
190 void AliEveEventBuffer::FetchEvent() {
191   // see header file for class documentation
192   cout << "FetchEvent " << endl;
193   TObject * event = GetEventFromSource();
194   ULong64_t eventId = GetEventIdFromSource();
195   if(event) {
196     AddToBuffer(event);
197     fEventId[fBIndex[kTop]] = eventId;  
198   }
199   
200   PrintIndeces();
201   cout << "FetchedEvent " << endl;
202   
203 }
204
205 ///_________________________________________________________________________________
206 void AliEveEventBuffer::AddToBuffer(TObject * event) {
207   // see header file for class documentation
208   cout << "Add to buffer"<<endl;
209   if(!event) return;
210
211   fBIndex[kTop] = CalculateNext(fBIndex[kTop]);
212   //Delete the event already there (ok to delete as object, not aliesdevent, TList?)
213   //TObject * object = fEventBuffer->At(fBIndex[kTop]);
214   fEventBuffer->RemoveAt(fBIndex[kTop]);
215   //if (object) delete object;
216   fEventBuffer->AddAt(event, fBIndex[kTop]);
217
218 }
219
220 ///_____________________________________________________________________________________
221 Int_t AliEveEventBuffer::CalculateNext(Int_t current) const {
222   //See header file for documentation
223   current++;
224   if(current == fBufferSize) current = 0;
225   return current;
226 }
227
228
229 ///_____________________________________________________________________________________
230 Int_t AliEveEventBuffer::CalculatePrevious(Int_t current) const {
231   //See header file for documentation
232   cout << "CalculatePrev:  " << current; 
233   current--;
234   if(current == -1) current += fBufferSize;
235   cout << "... " << current << endl;
236   return current;
237 }
238
239 ///__________________________________________________________________________________
240 Int_t AliEveEventBuffer::CalculateDifference(Int_t top, Int_t low) const {
241   //See header file for documentation
242   if (top > low) {
243     //    cout << "top > low"<<endl;
244     return (top - low);
245   } else if (top < low) {
246     // cout << "low < top"<<endl;
247     return (fBufferSize - low + top);
248   } else {
249     //cout << "calculated to 0"<<endl;
250     return 0;
251   }
252 }
253
254 ///___________________________________________________________________________________
255 void AliEveEventBuffer::StartBufferMonitor() {
256   //cout << "NOT !!! starting buffer mon"<<endl;
257   cout << "starting buffer mon"<<endl;
258   if(!GetBufferMonStarted()) {
259     CreateBufferThread();
260     SetBufferMonStarted(kTRUE);
261     fTimer->Start(15000);
262   } else {
263     cout << "Stopping buffer monitor"<<endl;
264     fTimer->Stop();
265     SetBufferMonStarted(kFALSE);
266   }
267 }
268 ///___________________________________________________________________________________
269 void AliEveEventBuffer::StopBufferMonitor() {
270   // see header file for class documentation
271   cout << "Stopping buffer mon"<<endl;
272   SetBufferMonStarted(kFALSE);
273   fTimer->Stop();
274 }
275
276
277 // //_________________________________________________________________________________
278 // Int_t AliEveEventBuffer::NavigateEventBufferBack() { 
279 //   // see header file for class documentation
280
281 //   // -- reached the end of the buffer
282 //   if ( fNavigateBufferIdx == fBufferLowIdx )
283 //     return -1;
284
285 //   Int_t newIdx = fNavigateBufferIdx - 1;
286 //   if ( newIdx == -1 )
287 //     newIdx = BUFFERSIZE-1;
288
289 //   fCurrentBufferIdx = fNavigateBufferIdx = newIdx;
290
291 //   return newIdx;
292 // }
293
294 // //_______________________________________________________________
295 // Int_t AliEveEventBuffer::NavigateEventBufferFwd() {
296 //   // see header file for class documentation
297
298 //   // -- reached the top of the buffer
299 //   if ( fNavigateBufferIdx == fBufferTopIdx )
300 //     return -1;
301
302 //   Int_t newIdx = fNavigateBufferIdx + 1;
303 //   if ( newIdx == BUFFERSIZE )
304 //     newIdx = 0;
305   
306 //   fCurrentBufferIdx = fNavigateBufferIdx = newIdx;
307
308 //   return newIdx;
309 // }
310
311 // void AliEveEventBuffer::MonitorBuffer() {
312 //   //See header file for documentation
313 //   if( GetNAvailableEvents() < 10) {
314 //     StopBufferChecker();
315 //     StartLoop();
316 //   }
317 // }
318
319 // void AliEveEventBuffer::StartLoop() {
320 //   //See header file for documentation
321 //   fTimer->Start(2000);
322 // }
323 // void AliEveEventBuffer::StopLoop() {
324 //   //See header file for documentation
325 //   fTimer->Stop();
326 // }
327
328 // void AliEveEventBuffer::StartBufferChecker() {
329 //   //See header file for documentation
330 //   fBufferTimer->Start(2000);
331 // }
332 // void AliEveEventBuffer::StopBufferChecker() {
333 //   //See header file for documentation
334 //   fBufferTimer->Stop();
335 // }
336
337 // AliESDEvent * GetNextEvent() {
338   
339 //   tree->GetEntry(fEvent++);
340
341 //   AliESDEvent * event = new AliESDEvent();
342 //   event->ReadFromTree(fTree);
343 //   if (event) {
344 //     return event;
345 //   } else {
346 //     cout << "error getting event" << endl;
347 //     return NULL;
348 //   }
349 // }