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;
91 ///___________________________________________________________________________
92 void * AliEveEventBuffer::BufferThread(void * buffer) {
93 cout <<"BufferThread : " <<endl;
95 reinterpret_cast<AliEveEventBuffer*>(buffer)->MonitorBuffer();
97 cout << "no buffer"<<endl;
102 ///_____________________________________________________________________________
103 void AliEveEventBuffer::MonitorBuffer() {
104 cout << "Monitorbuffer() ";
108 cout << "done " << endl;
112 ///_______________________________________________________________________________
113 TObject * AliEveEventBuffer::NextEvent() {
114 //See header file for documentation
115 cout << "NextEvent()"<<endl;
116 TObject * nextEvent = GetNextUnSeen();
120 ///______________________________________________________________________________
121 TObject * AliEveEventBuffer::Back() {
122 cout << "go back"<<endl;
124 Int_t prevId = CalculatePrevious(fBIndex[kCurrent]);
125 if(prevId == fBIndex[kTop]) {
126 cout << "returning NULL" << endl;
129 fBIndex[kCurrent] = prevId;
131 cout <<"returning: "<< fBIndex[kCurrent] << " " << fEventBuffer->At(fBIndex[kCurrent]);
132 return fEventBuffer->At(fBIndex[kCurrent]);
138 ///______________________________________________________________________________
139 TObject * AliEveEventBuffer::Fwd() {
141 if (fBIndex[kCurrent] == fBIndex[kLast]) {
142 cout<< "returning NULL"<<endl;
146 fBIndex[kCurrent] = CalculateNext(fBIndex[kCurrent]);
147 TObject * event = fEventBuffer->At(fBIndex[kCurrent]);
153 ///________________________________________________________________________________
154 TObject * AliEveEventBuffer::GetNextUnSeen() {
155 //See header file for documentation
156 cout << "GetNextUnSeen"<<endl;
158 if(CalculateDifference(fBIndex[kTop], fBIndex[kLast])) {
159 fBIndex[kLast] = CalculateNext(fBIndex[kLast]);
160 fBIndex[kCurrent] = fBIndex[kLast];
162 return fEventBuffer->At(fBIndex[kCurrent]);
164 cout << "No new event available, only events in buffer available!"<<endl;
168 ///_________________________________________________________________________________
169 void AliEveEventBuffer::PrintIndeces() {
170 for(Int_t i = 0; i < kSize; i++) {
171 cout << i << ": " << fBIndex[i] << endl;
174 ///_________________________________________________________________________________
175 void AliEveEventBuffer::PrintBuffer() {
176 for(Int_t i = 0; i < 10; i++) {
177 AliESDEvent * event = dynamic_cast<AliESDEvent*>(fEventBuffer->At(i));
179 cout << i << ": " <<event << " " << event->GetEventNumberInFile() << endl;;
184 ///____________________________________________________________________________________
185 void AliEveEventBuffer::FetchEvent() {
186 cout << "FetchEvent " << endl;
187 TObject * event = GetEventFromSource();
188 ULong64_t eventId = GetEventIdFromSource();
191 fEventId[fBIndex[kTop]] = eventId;
195 cout << "FetchedEvent " << endl;
199 ///_________________________________________________________________________________
200 void AliEveEventBuffer::AddToBuffer(TObject * event) {
201 cout << "Add to buffer"<<endl;
204 fBIndex[kTop] = CalculateNext(fBIndex[kTop]);
205 //Delete the event already there (ok to delete as object, not aliesdevent, TList?)
206 //TObject * object = fEventBuffer->At(fBIndex[kTop]);
207 fEventBuffer->RemoveAt(fBIndex[kTop]);
208 //if (object) delete object;
209 fEventBuffer->AddAt(event, fBIndex[kTop]);
213 ///_____________________________________________________________________________________
214 Int_t AliEveEventBuffer::CalculateNext(Int_t current) {
215 //See header file for documentation
217 if(current == fBufferSize) current = 0;
222 ///_____________________________________________________________________________________
223 Int_t AliEveEventBuffer::CalculatePrevious(Int_t current) {
224 //See header file for documentation
225 cout << "CalculatePrev: " << current;
227 if(current == -1) current += fBufferSize;
228 cout << "... " << current << endl;
232 ///__________________________________________________________________________________
233 Int_t AliEveEventBuffer::CalculateDifference(Int_t top, Int_t low) {
234 //See header file for documentation
236 // cout << "top > low"<<endl;
238 } else if (top < low) {
239 // cout << "low < top"<<endl;
240 return (fBufferSize - low + top);
242 //cout << "calculated to 0"<<endl;
247 ///___________________________________________________________________________________
248 void AliEveEventBuffer::StartBufferMonitor() {
249 //cout << "NOT !!! starting buffer mon"<<endl;
250 cout << "starting buffer mon"<<endl;
251 if(!GetBufferMonStarted()) {
252 CreateBufferThread();
253 SetBufferMonStarted(kTRUE);
254 fTimer->Start(15000);
256 cout << "Stopping buffer monitor"<<endl;
258 SetBufferMonStarted(kFALSE);
261 ///___________________________________________________________________________________
262 void AliEveEventBuffer::StopBufferMonitor() {
263 cout << "Stopping buffer mon"<<endl;
264 SetBufferMonStarted(kFALSE);
269 // //_________________________________________________________________________________
270 // Int_t AliEveEventBuffer::NavigateEventBufferBack() {
271 // // see header file for class documentation
273 // // -- reached the end of the buffer
274 // if ( fNavigateBufferIdx == fBufferLowIdx )
277 // Int_t newIdx = fNavigateBufferIdx - 1;
278 // if ( newIdx == -1 )
279 // newIdx = BUFFERSIZE-1;
281 // fCurrentBufferIdx = fNavigateBufferIdx = newIdx;
286 // //_______________________________________________________________
287 // Int_t AliEveEventBuffer::NavigateEventBufferFwd() {
288 // // see header file for class documentation
290 // // -- reached the top of the buffer
291 // if ( fNavigateBufferIdx == fBufferTopIdx )
294 // Int_t newIdx = fNavigateBufferIdx + 1;
295 // if ( newIdx == BUFFERSIZE )
298 // fCurrentBufferIdx = fNavigateBufferIdx = newIdx;
303 // void AliEveEventBuffer::MonitorBuffer() {
304 // //See header file for documentation
305 // if( GetNAvailableEvents() < 10) {
306 // StopBufferChecker();
311 // void AliEveEventBuffer::StartLoop() {
312 // //See header file for documentation
313 // fTimer->Start(2000);
315 // void AliEveEventBuffer::StopLoop() {
316 // //See header file for documentation
320 // void AliEveEventBuffer::StartBufferChecker() {
321 // //See header file for documentation
322 // fBufferTimer->Start(2000);
324 // void AliEveEventBuffer::StopBufferChecker() {
325 // //See header file for documentation
326 // fBufferTimer->Stop();
329 // AliESDEvent * GetNextEvent() {
331 // tree->GetEntry(fEvent++);
333 // AliESDEvent * event = new AliESDEvent();
334 // event->ReadFromTree(fTree);
338 // cout << "error getting event" << endl;