7 #include "AliEveEventBuffer.h"
10 //Not needed, only for debug
11 #include "AliESDEvent.h"
15 ClassImp(AliEveEventBuffer)
17 ///_______________________________________________________________________
18 AliEveEventBuffer::AliEveEventBuffer() :
26 fBufferMonStarted(kFALSE),
30 // see header file for class documentation
31 fEventBuffer = new TObjArray(fBufferSize, 0);
32 fEventBuffer->SetOwner(kFALSE);
34 for(int id = 0; id < kSize; id++) {
38 fTimer = new TTimer();
39 fTimer->Connect("Timeout()", "AliEveEventBuffer", this, "CreateBufferThread()");
41 fEventId = new ULong64_t[fBufferSize];
42 for(Int_t id = 0; id < fBufferSize; id++ ) {
46 fThread = new TThread(AliEveEventBuffer::BufferThread, (void*) this);
47 fMutex = new TMutex();
54 ///_______________________________________________________________________
55 AliEveEventBuffer::~AliEveEventBuffer() {
56 // see header file for class documentation
59 fEventBuffer->Clear();
70 ///___________________________________________________________________________
71 void AliEveEventBuffer::CreateBufferThread() {
72 // see header file for class documentation
74 cout << "Threadexists: " << fThread->Exists() << endl;
75 if( fMutex->TryLock() ) {
76 cout << "Buffer is busy, no thread created"<< endl;
79 if ( (CalculateDifference(fBIndex[kTop],fBIndex[kLast]) < fPreBuffer) ) {
80 cout << "StartBufferThread()"<<endl;
82 cout << "Started BufferThread"<<endl;
84 cout << "Buffer is full already"<<endl;
90 ///___________________________________________________________________________
91 void * AliEveEventBuffer::BufferThread(void * buffer) {
92 // see header file for class documentation
93 cout <<"BufferThread : " <<endl;
95 reinterpret_cast<AliEveEventBuffer*>(buffer)->MonitorBuffer();
97 cout << "no buffer"<<endl;
102 ///_____________________________________________________________________________
103 void AliEveEventBuffer::MonitorBuffer() {
104 // see header file for class documentation
105 cout << "Monitorbuffer() ";
109 cout << "done " << endl;
113 ///_______________________________________________________________________________
114 TObject * AliEveEventBuffer::NextEvent() {
115 //See header file for documentation
116 cout << "NextEvent()"<<endl;
117 TObject * nextEvent = GetNextUnSeen();
121 ///______________________________________________________________________________
122 TObject * AliEveEventBuffer::Back() {
123 // see header file for class documentation
124 cout << "go back"<<endl;
126 Int_t prevId = CalculatePrevious(fBIndex[kCurrent]);
127 if(prevId == fBIndex[kTop]) {
128 cout << "returning NULL" << endl;
131 fBIndex[kCurrent] = prevId;
133 cout <<"returning: "<< fBIndex[kCurrent] << " " << fEventBuffer->At(fBIndex[kCurrent]);
134 return fEventBuffer->At(fBIndex[kCurrent]);
140 ///______________________________________________________________________________
141 TObject * AliEveEventBuffer::Fwd() {
142 // see header file for class documentation
144 if (fBIndex[kCurrent] == fBIndex[kLast]) {
145 cout<< "returning NULL"<<endl;
149 fBIndex[kCurrent] = CalculateNext(fBIndex[kCurrent]);
150 TObject * event = fEventBuffer->At(fBIndex[kCurrent]);
156 ///________________________________________________________________________________
157 TObject * AliEveEventBuffer::GetNextUnSeen() {
158 //See header file for documentation
159 cout << "GetNextUnSeen"<<endl;
161 if(CalculateDifference(fBIndex[kTop], fBIndex[kLast])) {
162 fBIndex[kLast] = CalculateNext(fBIndex[kLast]);
163 fBIndex[kCurrent] = fBIndex[kLast];
165 return fEventBuffer->At(fBIndex[kCurrent]);
167 cout << "No new event available, only events in buffer available!"<<endl;
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;
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));
184 cout << i << ": " <<event << " " << event->GetEventNumberInFile() << endl;;
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();
197 fEventId[fBIndex[kTop]] = eventId;
201 cout << "FetchedEvent " << endl;
205 ///_________________________________________________________________________________
206 void AliEveEventBuffer::AddToBuffer(TObject * event) {
207 // see header file for class documentation
208 cout << "Add to buffer"<<endl;
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]);
220 ///_____________________________________________________________________________________
221 Int_t AliEveEventBuffer::CalculateNext(Int_t current) const {
222 //See header file for documentation
224 if(current == fBufferSize) current = 0;
229 ///_____________________________________________________________________________________
230 Int_t AliEveEventBuffer::CalculatePrevious(Int_t current) const {
231 //See header file for documentation
232 cout << "CalculatePrev: " << current;
234 if(current == -1) current += fBufferSize;
235 cout << "... " << current << endl;
239 ///__________________________________________________________________________________
240 Int_t AliEveEventBuffer::CalculateDifference(Int_t top, Int_t low) const {
241 //See header file for documentation
243 // cout << "top > low"<<endl;
245 } else if (top < low) {
246 // cout << "low < top"<<endl;
247 return (fBufferSize - low + top);
249 //cout << "calculated to 0"<<endl;
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);
263 cout << "Stopping buffer monitor"<<endl;
265 SetBufferMonStarted(kFALSE);
268 ///___________________________________________________________________________________
269 void AliEveEventBuffer::StopBufferMonitor() {
270 // see header file for class documentation
271 cout << "Stopping buffer mon"<<endl;
272 SetBufferMonStarted(kFALSE);
277 // //_________________________________________________________________________________
278 // Int_t AliEveEventBuffer::NavigateEventBufferBack() {
279 // // see header file for class documentation
281 // // -- reached the end of the buffer
282 // if ( fNavigateBufferIdx == fBufferLowIdx )
285 // Int_t newIdx = fNavigateBufferIdx - 1;
286 // if ( newIdx == -1 )
287 // newIdx = BUFFERSIZE-1;
289 // fCurrentBufferIdx = fNavigateBufferIdx = newIdx;
294 // //_______________________________________________________________
295 // Int_t AliEveEventBuffer::NavigateEventBufferFwd() {
296 // // see header file for class documentation
298 // // -- reached the top of the buffer
299 // if ( fNavigateBufferIdx == fBufferTopIdx )
302 // Int_t newIdx = fNavigateBufferIdx + 1;
303 // if ( newIdx == BUFFERSIZE )
306 // fCurrentBufferIdx = fNavigateBufferIdx = newIdx;
311 // void AliEveEventBuffer::MonitorBuffer() {
312 // //See header file for documentation
313 // if( GetNAvailableEvents() < 10) {
314 // StopBufferChecker();
319 // void AliEveEventBuffer::StartLoop() {
320 // //See header file for documentation
321 // fTimer->Start(2000);
323 // void AliEveEventBuffer::StopLoop() {
324 // //See header file for documentation
328 // void AliEveEventBuffer::StartBufferChecker() {
329 // //See header file for documentation
330 // fBufferTimer->Start(2000);
332 // void AliEveEventBuffer::StopBufferChecker() {
333 // //See header file for documentation
334 // fBufferTimer->Stop();
337 // AliESDEvent * GetNextEvent() {
339 // tree->GetEntry(fEvent++);
341 // AliESDEvent * event = new AliESDEvent();
342 // event->ReadFromTree(fTree);
346 // cout << "error getting event" << endl;