7 #include "AliEveEventBuffer.h"
10 //Not needed, only for debug
11 #include "AliESDEvent.h"
15 ClassImp(AliEveEventBuffer)
17 ///_______________________________________________________________________
18 AliEveEventBuffer::AliEveEventBuffer() :
27 fBufferMonStarted(kFALSE),
31 // see header file for class documentation
32 fEventBuffer = new TObjArray(fBufferSize, 0);
33 fEventBuffer->SetOwner(kFALSE);
35 for(int id = 0; id < kSize; id++) {
39 fTimer = new TTimer();
40 fTimer->Connect("Timeout()", "AliEveEventBuffer", this, "CreateBufferThread()");
42 fEventId = new ULong64_t[fBufferSize];
43 for(Int_t id = 0; id < fBufferSize; id++ ) {
47 fThread = new TThread(AliEveEventBuffer::BufferThread, (void*) this);
48 fMutex = new TMutex();
55 ///_______________________________________________________________________
56 AliEveEventBuffer::~AliEveEventBuffer() {
57 // see header file for class documentation
60 fEventBuffer->Clear();
71 ///___________________________________________________________________________
72 void AliEveEventBuffer::CreateBufferThread() {
75 cout << "Threadexists: " << fThread->Exists() << endl;
76 if( fMutex->TryLock() ) {
77 cout << "Buffer is busy, no thread created"<< endl;
80 if ( (CalculateDifference(fBIndex[kTop],fBIndex[kLast]) < fPreBuffer) ) {
82 cout << "StartBufferThread()"<<endl;
84 cout << "Started BufferThread"<<endl;
86 cout << "Buffer is full already"<<endl;
92 ///___________________________________________________________________________
93 void * AliEveEventBuffer::BufferThread(void * buffer) {
94 cout <<"BufferThread : " <<endl;
96 reinterpret_cast<AliEveEventBuffer*>(buffer)->MonitorBuffer();
98 cout << "no buffer"<<endl;
103 ///_____________________________________________________________________________
104 void AliEveEventBuffer::MonitorBuffer() {
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 cout << "go back"<<endl;
125 Int_t prevId = CalculatePrevious(fBIndex[kCurrent]);
126 if(prevId == fBIndex[kTop]) {
127 cout << "returning NULL" << endl;
130 fBIndex[kCurrent] = prevId;
132 cout <<"returning: "<< fBIndex[kCurrent] << " " << fEventBuffer->At(fBIndex[kCurrent]);
133 return fEventBuffer->At(fBIndex[kCurrent]);
139 ///______________________________________________________________________________
140 TObject * AliEveEventBuffer::Fwd() {
142 if (fBIndex[kCurrent] == fBIndex[kLast]) {
143 cout<< "returning NULL"<<endl;
147 fBIndex[kCurrent] = CalculateNext(fBIndex[kCurrent]);
148 TObject * event = fEventBuffer->At(fBIndex[kCurrent]);
154 ///________________________________________________________________________________
155 TObject * AliEveEventBuffer::GetNextUnSeen() {
156 //See header file for documentation
157 cout << "GetNextUnSeen"<<endl;
159 if(CalculateDifference(fBIndex[kTop], fBIndex[kLast])) {
160 fBIndex[kLast] = CalculateNext(fBIndex[kLast]);
161 fBIndex[kCurrent] = fBIndex[kLast];
163 return fEventBuffer->At(fBIndex[kCurrent]);
165 cout << "No new event available, only events in buffer available!"<<endl;
169 ///_________________________________________________________________________________
170 void AliEveEventBuffer::PrintIndeces() {
171 for(Int_t i = 0; i < kSize; i++) {
172 cout << i << ": " << fBIndex[i] << endl;
175 ///_________________________________________________________________________________
176 void AliEveEventBuffer::PrintBuffer() {
177 for(Int_t i = 0; i < 10; i++) {
178 AliESDEvent * event = dynamic_cast<AliESDEvent*>(fEventBuffer->At(i));
180 cout << i << ": " <<event << " " << event->GetEventNumberInFile() << endl;;
185 ///____________________________________________________________________________________
186 void AliEveEventBuffer::FetchEvent() {
187 cout << "FetchEvent " << endl;
188 TObject * event = GetEventFromSource();
189 ULong64_t eventId = GetEventIdFromSource();
192 fEventId[fBIndex[kTop]] = eventId;
196 cout << "FetchedEvent " << endl;
200 ///_________________________________________________________________________________
201 void AliEveEventBuffer::AddToBuffer(TObject * event) {
202 cout << "Add to buffer"<<endl;
205 fBIndex[kTop] = CalculateNext(fBIndex[kTop]);
206 //Delete the event already there (ok to delete as object, not aliesdevent, TList?)
207 //TObject * object = fEventBuffer->At(fBIndex[kTop]);
208 fEventBuffer->RemoveAt(fBIndex[kTop]);
209 //if (object) delete object;
210 fEventBuffer->AddAt(event, fBIndex[kTop]);
214 ///_____________________________________________________________________________________
215 Int_t AliEveEventBuffer::CalculateNext(Int_t current) {
216 //See header file for documentation
218 if(current == fBufferSize) current = 0;
223 ///_____________________________________________________________________________________
224 Int_t AliEveEventBuffer::CalculatePrevious(Int_t current) {
225 //See header file for documentation
226 cout << "CalculatePrev: " << current;
228 if(current == -1) current += fBufferSize;
229 cout << "... " << current << endl;
233 ///__________________________________________________________________________________
234 Int_t AliEveEventBuffer::CalculateDifference(Int_t top, Int_t low) {
235 //See header file for documentation
237 // cout << "top > low"<<endl;
239 } else if (top < low) {
240 // cout << "low < top"<<endl;
241 return (fBufferSize - low + top);
243 //cout << "calculated to 0"<<endl;
248 ///___________________________________________________________________________________
249 void AliEveEventBuffer::StartBufferMonitor() {
250 //cout << "NOT !!! starting buffer mon"<<endl;
251 cout << "starting buffer mon"<<endl;
252 if(!GetBufferMonStarted()) {
253 CreateBufferThread();
254 SetBufferMonStarted(kTRUE);
255 fTimer->Start(15000);
257 cout << "Stopping buffer monitor"<<endl;
259 SetBufferMonStarted(kFALSE);
262 ///___________________________________________________________________________________
263 void AliEveEventBuffer::StopBufferMonitor() {
264 cout << "Stopping buffer mon"<<endl;
265 SetBufferMonStarted(kFALSE);
270 // //_________________________________________________________________________________
271 // Int_t AliEveEventBuffer::NavigateEventBufferBack() {
272 // // see header file for class documentation
274 // // -- reached the end of the buffer
275 // if ( fNavigateBufferIdx == fBufferLowIdx )
278 // Int_t newIdx = fNavigateBufferIdx - 1;
279 // if ( newIdx == -1 )
280 // newIdx = BUFFERSIZE-1;
282 // fCurrentBufferIdx = fNavigateBufferIdx = newIdx;
287 // //_______________________________________________________________
288 // Int_t AliEveEventBuffer::NavigateEventBufferFwd() {
289 // // see header file for class documentation
291 // // -- reached the top of the buffer
292 // if ( fNavigateBufferIdx == fBufferTopIdx )
295 // Int_t newIdx = fNavigateBufferIdx + 1;
296 // if ( newIdx == BUFFERSIZE )
299 // fCurrentBufferIdx = fNavigateBufferIdx = newIdx;
304 // void AliEveEventBuffer::MonitorBuffer() {
305 // //See header file for documentation
306 // if( GetNAvailableEvents() < 10) {
307 // StopBufferChecker();
312 // void AliEveEventBuffer::StartLoop() {
313 // //See header file for documentation
314 // fTimer->Start(2000);
316 // void AliEveEventBuffer::StopLoop() {
317 // //See header file for documentation
321 // void AliEveEventBuffer::StartBufferChecker() {
322 // //See header file for documentation
323 // fBufferTimer->Start(2000);
325 // void AliEveEventBuffer::StopBufferChecker() {
326 // //See header file for documentation
327 // fBufferTimer->Stop();
330 // AliESDEvent * GetNextEvent() {
332 // tree->GetEntry(fEvent++);
334 // AliESDEvent * event = new AliESDEvent();
335 // event->ReadFromTree(fTree);
339 // cout << "error getting event" << endl;