30e7579c |
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 | fThread(NULL), |
26 | fEventId() |
27 | { |
28 | // see header file for class documentation |
29 | fEventBuffer = new TObjArray(10, 0); |
30 | fEventBuffer->SetOwner(kTRUE); |
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, "MonitorBuffer()"); |
38 | |
39 | fEventId = new Int_t[fBufferSize]; |
40 | |
41 | |
42 | } |
43 | |
44 | |
45 | |
46 | ///_______________________________________________________________________ |
47 | AliEveEventBuffer::~AliEveEventBuffer() { |
48 | // see header file for class documentation |
49 | |
50 | if ( fEventBuffer ) { |
51 | fEventBuffer->Clear(); |
52 | delete fEventBuffer; |
53 | } |
54 | fEventBuffer = NULL; |
55 | |
56 | if(fCurrentEvent) |
57 | delete fCurrentEvent; |
58 | fCurrentEvent = NULL; |
59 | |
60 | } |
61 | |
62 | ///___________________________________________________________________________ |
63 | void * AliEveEventBuffer::BufferThread(void * buffer) { |
64 | if(buffer) |
65 | reinterpret_cast<AliEveEventBuffer*>(buffer)->StartBufferMonitor(); |
66 | return (void*)0; |
67 | } |
68 | |
69 | ///_____________________________________________________________________________ |
70 | void AliEveEventBuffer::MonitorBuffer() { |
71 | cout << "Monitorbuffer: " << endl; |
72 | if(fBusy) { |
73 | cout << "Already called FetchEvent()" << endl; |
74 | return; |
75 | } else { |
76 | cout << "fbusy = false"<<endl; |
77 | |
78 | if ( (CalculateDifference(fBIndex[kTop],fBIndex[kLast]) < fPreBuffer) ) { |
79 | fBusy = kTRUE; |
80 | FetchEvent(); |
81 | fBusy = kFALSE; |
82 | } else { |
83 | //StopBufferMonitor(); |
84 | fBusy = kFALSE; |
85 | } |
86 | } |
87 | } |
88 | |
89 | ///_______________________________________________________________________________ |
90 | TObject * AliEveEventBuffer::NextEvent() { |
91 | //See header file for documentation |
92 | if(fBusy) { |
93 | cout << "Event Buffer busy"<<endl; |
94 | return NULL; |
95 | } |
96 | else SetBusy(kTRUE); |
97 | cout << "In enxtevent"<<endl; |
98 | TObject * nextEvent = GetNextUnSeen(); |
99 | SetBusy(kFALSE); |
100 | return nextEvent; |
101 | } |
102 | |
103 | ///______________________________________________________________________________ |
104 | TObject * AliEveEventBuffer::Back() { |
105 | cout << "go back"<<endl; |
106 | PrintIndeces(); |
107 | Int_t prevId = CalculatePrevious(fBIndex[kCurrent]); |
108 | if(prevId == fBIndex[kTop]) { |
109 | cout << "returning NULL" << endl; |
110 | return NULL; |
111 | } else { |
112 | fBIndex[kCurrent] = prevId; |
113 | PrintIndeces(); |
114 | cout <<"returning: "<< fBIndex[kCurrent] << " " << fEventBuffer->At(fBIndex[kCurrent]); |
115 | return fEventBuffer->At(fBIndex[kCurrent]); |
116 | } |
117 | } |
118 | |
119 | ///______________________________________________________________________________ |
120 | TObject * AliEveEventBuffer::Fwd() { |
121 | PrintIndeces(); |
122 | if (fBIndex[kCurrent] == fBIndex[kLast]) { |
123 | cout<< "returning NULL"<<endl; |
124 | return NULL; |
125 | } |
126 | |
127 | fBIndex[kCurrent] = CalculateNext(fBIndex[kCurrent]); |
128 | TObject * event = fEventBuffer->At(fBIndex[kCurrent]); |
129 | return event; |
130 | } |
131 | |
132 | |
133 | |
134 | ///________________________________________________________________________________ |
135 | TObject * AliEveEventBuffer::GetNextUnSeen() { |
136 | //See header file for documentation |
137 | cout << "GetNextUnSeend"<<endl; |
138 | PrintIndeces(); |
139 | if(CalculateDifference(fBIndex[kTop], fBIndex[kLast])) { |
140 | fBIndex[kLast] = CalculateNext(fBIndex[kLast]); |
141 | fBIndex[kCurrent] = fBIndex[kLast]; |
142 | PrintIndeces(); |
143 | return fEventBuffer->At(fBIndex[kCurrent]); |
144 | } else { |
145 | cout << "No new event available, only events in buffer available!"<<endl; |
146 | return NULL; |
147 | } |
148 | } |
149 | ///_________________________________________________________________________________ |
150 | void AliEveEventBuffer::PrintIndeces() { |
151 | for(Int_t i = 0; i < kSize; i++) { |
152 | cout << i << ": " << fBIndex[i] << endl; |
153 | } |
154 | } |
155 | ///_________________________________________________________________________________ |
156 | void AliEveEventBuffer::PrintBuffer() { |
157 | for(Int_t i = 0; i < 10; i++) { |
158 | AliESDEvent * event = dynamic_cast<AliESDEvent*>(fEventBuffer->At(i)); |
159 | if(event) { |
160 | cout << i << ": " <<event << " " << event->GetEventNumberInFile() << endl;; |
161 | } |
162 | } |
163 | } |
164 | |
165 | ///____________________________________________________________________________________ |
166 | void AliEveEventBuffer::FetchEvent() { |
167 | cout << "FetchEvent " << endl; |
168 | TObject * event = GetEventFromSource(); |
169 | if(event) AddToBuffer(event); |
170 | PrintIndeces(); |
171 | cout << "FetchedEvent " << endl; |
172 | |
173 | } |
174 | |
175 | ///_________________________________________________________________________________ |
176 | void AliEveEventBuffer::AddToBuffer(TObject * event) { |
177 | cout << "Add to buffer"<<endl; |
178 | if(!event) return; |
179 | |
180 | fBIndex[kTop] = CalculateNext(fBIndex[kTop]); |
181 | //Delete the event already there (ok to delete as object, not aliesdevent, TList?) |
182 | TObject * object = fEventBuffer->At(fBIndex[kTop]); |
183 | if (object) delete object; |
184 | fEventBuffer->AddAt(event, fBIndex[kTop]); |
185 | } |
186 | |
187 | |
188 | |
189 | ///_____________________________________________________________________________________ |
190 | Int_t AliEveEventBuffer::CalculateNext(Int_t current) { |
191 | //See header file for documentation |
192 | current++; |
193 | if(current == fBufferSize) current = 0; |
194 | return current; |
195 | } |
196 | |
197 | |
198 | ///_____________________________________________________________________________________ |
199 | Int_t AliEveEventBuffer::CalculatePrevious(Int_t current) { |
200 | //See header file for documentation |
201 | cout << "CalculatePrev: " << current; |
202 | current--; |
203 | if(current == -1) current += fBufferSize; |
204 | cout << "... " << current << endl; |
205 | return current; |
206 | } |
207 | |
208 | ///__________________________________________________________________________________ |
209 | Int_t AliEveEventBuffer::CalculateDifference(Int_t top, Int_t low) { |
210 | //See header file for documentation |
211 | if (top > low) { |
212 | // cout << "top > low"<<endl; |
213 | return (top - low); |
214 | } else if (top < low) { |
215 | // cout << "low < top"<<endl; |
216 | return (fBufferSize - low + top); |
217 | } else { |
218 | //cout << "calculated to 0"<<endl; |
219 | return 0; |
220 | } |
221 | } |
222 | |
223 | ///___________________________________________________________________________________ |
224 | void AliEveEventBuffer::StartBufferMonitor() { |
225 | //cout << "NOT !!! starting buffer mon"<<endl; |
226 | cout << "starting buffer mon"<<endl; |
227 | fTimer->Start(5000); |
228 | } |
229 | ///___________________________________________________________________________________ |
230 | void AliEveEventBuffer::StopBufferMonitor() { |
231 | cout << "Stopping buffer mon"<<endl; |
232 | fTimer->Stop(); |
233 | } |
234 | |
235 | |
236 | // //_________________________________________________________________________________ |
237 | // Int_t AliEveEventBuffer::NavigateEventBufferBack() { |
238 | // // see header file for class documentation |
239 | |
240 | // // -- reached the end of the buffer |
241 | // if ( fNavigateBufferIdx == fBufferLowIdx ) |
242 | // return -1; |
243 | |
244 | // Int_t newIdx = fNavigateBufferIdx - 1; |
245 | // if ( newIdx == -1 ) |
246 | // newIdx = BUFFERSIZE-1; |
247 | |
248 | // fCurrentBufferIdx = fNavigateBufferIdx = newIdx; |
249 | |
250 | // return newIdx; |
251 | // } |
252 | |
253 | // //_______________________________________________________________ |
254 | // Int_t AliEveEventBuffer::NavigateEventBufferFwd() { |
255 | // // see header file for class documentation |
256 | |
257 | // // -- reached the top of the buffer |
258 | // if ( fNavigateBufferIdx == fBufferTopIdx ) |
259 | // return -1; |
260 | |
261 | // Int_t newIdx = fNavigateBufferIdx + 1; |
262 | // if ( newIdx == BUFFERSIZE ) |
263 | // newIdx = 0; |
264 | |
265 | // fCurrentBufferIdx = fNavigateBufferIdx = newIdx; |
266 | |
267 | // return newIdx; |
268 | // } |
269 | |
270 | // void AliEveEventBuffer::MonitorBuffer() { |
271 | // //See header file for documentation |
272 | // if( GetNAvailableEvents() < 10) { |
273 | // StopBufferChecker(); |
274 | // StartLoop(); |
275 | // } |
276 | // } |
277 | |
278 | // void AliEveEventBuffer::StartLoop() { |
279 | // //See header file for documentation |
280 | // fTimer->Start(2000); |
281 | // } |
282 | // void AliEveEventBuffer::StopLoop() { |
283 | // //See header file for documentation |
284 | // fTimer->Stop(); |
285 | // } |
286 | |
287 | // void AliEveEventBuffer::StartBufferChecker() { |
288 | // //See header file for documentation |
289 | // fBufferTimer->Start(2000); |
290 | // } |
291 | // void AliEveEventBuffer::StopBufferChecker() { |
292 | // //See header file for documentation |
293 | // fBufferTimer->Stop(); |
294 | // } |
295 | |
296 | // AliESDEvent * GetNextEvent() { |
297 | |
298 | // tree->GetEntry(fEvent++); |
299 | |
300 | // AliESDEvent * event = new AliESDEvent(); |
301 | // event->ReadFromTree(fTree); |
302 | // if (event) { |
303 | // return event; |
304 | // } else { |
305 | // cout << "error getting event" << endl; |
306 | // return NULL; |
307 | // } |
308 | // } |