9436280433830378f42a15f10f6bb4d05b58167f
[u/mrichter/AliRoot.git] / HLT / MUON / src / AliRoot / TrackSink.cxx
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Author: Artur Szostak
4 // Email:  artur@alice.phy.uct.ac.za | artursz@iafrica.com
5 //
6 ////////////////////////////////////////////////////////////////////////////////
7
8 #include "AliRoot/TrackSink.hpp"
9 #include "AliRoot/Base.hpp"
10
11 ClassImp(AliMUONHLT::TrackSink)
12 ClassImp(AliMUONHLT::TrackSink::EventData)
13
14 namespace AliMUONHLT
15 {
16
17
18 TrackSink::TrackSink() :
19         TObject(), fEventList(TrackSink::EventData::Class())
20 {
21         fFilename = "";
22         fFoldername = "";
23         ResetAllPointers();
24 }
25
26
27 TrackSink::~TrackSink()
28 {
29         DebugMsg(1, "TrackSink::~TrackSink()");
30         fEventList.Clear("C");
31 }
32
33
34 void TrackSink::AddEvent(Int_t eventnumber)
35 {
36         DebugMsg(1, "TrackSink::AddEvent(" << eventnumber << ")");
37         
38         if (eventnumber < 0)
39         {
40                 Error("AddEvent", "The event number must be positive, got: %d", eventnumber);
41                 return;
42         };
43         
44         // Make sure that the event number is not already added to the list of events.
45         TIter next(&fEventList);
46         EventData* current;
47         while ((current = (EventData*)next()))
48         {
49                 if (current->fEventNumber == eventnumber)
50                 {
51                         Error("AddEvent", "The event number %d is already stored.", eventnumber);
52                         return;
53                 };
54         };
55         
56         fEventIndex = fEventList.GetEntriesFast();
57         new ( fEventList[fEventIndex] ) EventData(eventnumber); 
58         fCurrentEvent = (EventData*) fEventList[fEventIndex];
59         
60         // Remember to reset the other pointers because the new event is empty.
61         ResetBlockPointers();
62         
63         DebugMsg(2, "\tfEventIndex = " << fEventIndex << " , fBlockIndex = " << fBlockIndex
64                 << " , fTrackIndex = " << fTrackIndex
65         );
66 }
67
68
69 void TrackSink::AddBlock()
70 {
71         DebugMsg(1, "TrackSink::AddBlock()");
72         
73         if (fCurrentEvent == NULL)
74         {
75                 Error("AddBlock", "No event selected.");
76                 return;
77         };
78         
79         fBlockIndex = fCurrentEvent->fBlocks.GetEntriesFast();
80         new ( fCurrentEvent->fBlocks[fBlockIndex] ) TClonesArray(Track::Class());
81         fCurrentBlock = (TClonesArray*) fCurrentEvent->fBlocks[fBlockIndex];
82         
83         // Remember to reset the track pointer because the new block is empty.
84         ResetTrackPointers();
85
86         DebugMsg(2, "\tfEventIndex = " << fEventIndex << " , fBlockIndex = " << fBlockIndex
87                 << " , fTrackIndex = " << fTrackIndex
88         );
89 }
90
91
92 void TrackSink::AddTrack(const Track& track)
93 {
94         DebugMsg(1, "TrackSink::AddTrack()");
95
96         if (fCurrentBlock == NULL)
97         {
98                 Error("AddTrack", "No block selected.");
99                 return;
100         };
101         
102         fTrackIndex = fCurrentBlock->GetEntriesFast();
103         new ( (*fCurrentBlock)[fTrackIndex] ) Track(track);
104         fCurrentTrack = (Track*) (*fCurrentBlock)[fTrackIndex];
105         
106         DebugMsg(2, "\tfEventIndex = " << fEventIndex << " , fBlockIndex = " << fBlockIndex
107                 << " , fTrackIndex = " << fTrackIndex
108         );
109 }
110
111
112 Track* TrackSink::AddTrack()
113 {
114         DebugMsg(1, "TrackSink::AddTrack()");
115
116         if (fCurrentBlock == NULL)
117         {
118                 Error("AddTrack", "No block selected.");
119                 return NULL;
120         };
121         
122         fTrackIndex = fCurrentBlock->GetEntriesFast();
123         fCurrentTrack = (Track*) fCurrentBlock->New(fTrackIndex);
124         
125         DebugMsg(2, "\tfEventIndex = " << fEventIndex << " , fBlockIndex = " << fBlockIndex
126                 << " , fTrackIndex = " << fTrackIndex
127         );
128         return fCurrentTrack;
129 };
130
131
132 void TrackSink::AddTrack(
133                 Int_t triggerid, Int_t sign, Float_t momentum,
134                 Float_t pt, const Point hits[10], const Region regions[10]
135         )
136 {
137         DebugMsg(1, "TrackSink::AddTrack(" << triggerid << ", " << sign << ", "
138                 << momentum << ", " << pt << ", " << (void*)(&hits[0]) << ", "
139                 << (void*)(&regions[0]) << ")"
140         );
141
142         if (fCurrentBlock == NULL)
143         {
144                 Error("AddTrack", "No block selected.");
145                 return;
146         };
147         
148         fTrackIndex = fCurrentBlock->GetEntriesFast();
149         new ( (*fCurrentBlock)[fTrackIndex] ) Track(triggerid, sign, momentum, pt, hits, regions);
150         fCurrentTrack = (Track*) (*fCurrentBlock)[fTrackIndex];
151         
152         DebugMsg(2, "\tfEventIndex = " << fEventIndex << " , fBlockIndex = " << fBlockIndex
153                 << " , fTrackIndex = " << fTrackIndex
154         );
155 }
156
157
158 void TrackSink::SetNames(const TriggerSource* triggersource)
159 {
160         fFilename = triggersource->FileName();
161         fFoldername = triggersource->FolderName();
162 }
163
164
165 void TrackSink::Clear(Option_t* /*option*/)
166 {
167         fFilename = "";
168         fFoldername = "";
169         ResetAllPointers();
170         fEventList.Clear("C");
171 }
172
173
174 Bool_t TrackSink::GetEvent(Int_t eventnumber) const
175 {
176         DebugMsg(1, "TrackSink::GetEvent(" << eventnumber << ")" );
177         
178         // Try find the corresponding event in the list of events.
179         for (Int_t i = 0; i < fEventList.GetEntriesFast(); i++)
180         {
181                 EventData* current = (EventData*) fEventList[i];
182                 if (current->fEventNumber == eventnumber)
183                 {
184                         fEventIndex = i;
185                         fCurrentEvent = current;
186                         GetFirstBlock();
187                         DebugMsg(2, "\tfEventIndex = " << fEventIndex << " , fBlockIndex = " << fBlockIndex
188                                 << " , fTrackIndex = " << fTrackIndex
189                         );
190                         return kTRUE;
191                 }
192         }
193         return kFALSE;
194 }
195
196
197 Bool_t TrackSink::GetFirstEvent() const
198 {
199         DebugMsg(1, "TrackSink::GetFirstEvent()");
200         if (fEventList.GetEntriesFast() > 0)
201         {
202                 fEventIndex = 0;
203                 fCurrentEvent = (EventData*) fEventList[0];
204                 GetFirstBlock();
205                 DebugMsg(2, "\tfEventIndex = " << fEventIndex << " , fBlockIndex = " << fBlockIndex
206                         << " , fTrackIndex = " << fTrackIndex
207                 );
208                 return kTRUE;
209         }
210         else
211         {
212                 DebugMsg(2, "\tfEventIndex = " << fEventIndex << " , fBlockIndex = " << fBlockIndex
213                         << " , fTrackIndex = " << fTrackIndex
214                 );
215                 return kFALSE;
216         }
217 }
218
219
220 Bool_t TrackSink::MoreEvents() const
221 {
222         return 0 <= fEventIndex && fEventIndex < fEventList.GetEntriesFast();
223 }
224
225
226 Bool_t TrackSink::GetNextEvent() const
227 {
228         DebugMsg(1, "TrackSink::GetNextEvent()");
229         if (fEventIndex < fEventList.GetEntriesFast() - 1)
230         {
231                 fCurrentEvent = (EventData*) fEventList[ ++fEventIndex ];
232                 GetFirstBlock();
233                 DebugMsg(2, "\tfEventIndex = " << fEventIndex << " , fBlockIndex = " << fBlockIndex
234                         << " , fTrackIndex = " << fTrackIndex
235                 );
236                 return kTRUE;
237         }
238         else
239         {
240                 ResetAllPointers();
241                 return kFALSE;
242         };
243 };
244
245
246 Int_t TrackSink::CurrentEvent() const
247 {
248         if (fCurrentEvent != NULL)
249                 return fCurrentEvent->fEventNumber;
250         else
251                 return -1;
252 }
253
254
255 Int_t TrackSink::NumberOfBlocks() const
256 {
257         DebugMsg(1, "TrackSink::NumberOfBlocks()");
258         if (fCurrentEvent == NULL)
259         {
260                 Error("NumberOfBlocks", "No event selected.");
261                 return -1;
262         }
263         else
264                 return fCurrentEvent->fBlocks.GetEntriesFast();
265 }
266
267
268 Bool_t TrackSink::GetBlock(Int_t index) const
269 {
270         DebugMsg(1, "TrackSink::GetBlock(" << index << ")");
271         
272         // Note NumberOfBlocks() also checks if the event was selected.
273         Int_t numberofblocks = NumberOfBlocks();
274         if (numberofblocks < 0) return kFALSE;
275
276         if ( 0 <= index && index < numberofblocks )
277         {
278                 fBlockIndex = index;
279                 fCurrentBlock = (TClonesArray*) fCurrentEvent->fBlocks[index];
280                 GetFirstTrack();
281                 DebugMsg(2, "\tfEventIndex = " << fEventIndex << " , fBlockIndex = " << fBlockIndex
282                         << " , fTrackIndex = " << fTrackIndex
283                 );
284                 return kTRUE;
285         }
286         else
287         {
288                 // The index is out of bounds so inform the user.
289                 if (numberofblocks > 0)
290                         Error(  "GetBlock",
291                                 "The block index (%d) is out of bounds. Valid range is [0, %d]",
292                                 index, numberofblocks - 1
293                         );
294                 else
295                         Error(  "GetBlock",
296                                 "The block index (%d) is out of bounds. No blocks found.",
297                                 index
298                         );
299                 return kFALSE;
300         }
301 }
302
303
304 Bool_t TrackSink::GetFirstBlock() const
305 {
306         DebugMsg(1, "TrackSink::GetFirstBlock()");
307         // Note: NumberOfBlocks() also checks if fCurrentEvent != NULL.
308         if (NumberOfBlocks() > 0)
309         {
310                 fBlockIndex = 0;
311                 fCurrentBlock = (TClonesArray*) fCurrentEvent->fBlocks[fBlockIndex];
312                 GetFirstTrack();
313                 DebugMsg(2, "\tfEventIndex = " << fEventIndex << " , fBlockIndex = " << fBlockIndex
314                         << " , fTrackIndex = " << fTrackIndex
315                 );
316                 return kTRUE;
317         }
318         else
319                 return kFALSE;
320 }
321
322
323 Bool_t TrackSink::MoreBlocks() const
324 {
325         return 0 <= fBlockIndex && fBlockIndex < NumberOfBlocks();
326 }
327
328
329 Bool_t TrackSink::GetNextBlock() const
330 {
331         DebugMsg(1, "TrackSink::GetNextBlock()");
332
333         // Note: NumberOfBlocks() checks if fCurrentEvent != NULL. If it is then it returns -1
334         // and since fBlockIndex is always >= -1 the if statement must go to the else part.
335         if (fBlockIndex < NumberOfBlocks() - 1)
336         {
337                 fCurrentBlock = (TClonesArray*) fCurrentEvent->fBlocks[ ++fBlockIndex ];
338                 GetFirstTrack();
339                 DebugMsg(2, "\tfEventIndex = " << fEventIndex << " , fBlockIndex = " << fBlockIndex
340                         << " , fTrackIndex = " << fTrackIndex
341                 );
342                 return kTRUE;
343         }
344         else
345         {
346                 ResetBlockPointers();
347                 return kFALSE;
348         }
349 }
350
351
352 Int_t TrackSink::NumberOfTracks() const
353 {
354         DebugMsg(1, "TrackSink::NumberOfTracks()");
355         if (fCurrentBlock == NULL)
356         {
357                 Error("NumberOfTracks", "No block selected.");
358                 return -1;
359         }
360         else
361                 return fCurrentBlock->GetEntriesFast();
362 };
363
364
365 const Track* TrackSink::GetTrack(Int_t index) const
366 {
367         DebugMsg(1, "TrackSink::GetTrack(" << index << ")");
368
369         // Note NumberOfTracks() also checks if the event and block was selected.
370         Int_t numberoftracks = NumberOfTracks();
371         if (numberoftracks < 0) return NULL;
372         
373         if ( 0 <= index && index < numberoftracks )
374         {
375                 fTrackIndex = index;
376                 fCurrentTrack = (Track*) fCurrentBlock->At(index);
377                 DebugMsg(2, "\tfEventIndex = " << fEventIndex << " , fBlockIndex = " << fBlockIndex
378                         << " , fTrackIndex = " << fTrackIndex
379                 );
380                 return fCurrentTrack;
381         }
382         else
383         {
384                 // The index is out of bounds so inform the user.
385                 if (numberoftracks > 0)
386                         Error(  "GetTrack",
387                                 "The track index (%d) is out of bounds. Valid range is [0, %d]",
388                                 index, numberoftracks - 1
389                         );
390                 else
391                         Error(  "GetTrack",
392                                 "The track index (%d) is out of bounds. No tracks found.",
393                                 index
394                         );
395                 return NULL;
396         }
397 }
398
399
400 const Track* TrackSink::GetFirstTrack() const
401 {
402         DebugMsg(1, "TrackSink::GetFirstTrack()");
403         // Note: NumberOfTracks() also checks if fCurrentBlock != NULL.
404         if (NumberOfTracks() > 0)
405         {
406                 fTrackIndex = 0;
407                 fCurrentTrack = (Track*) fCurrentBlock->At(0);
408                 DebugMsg(2, "\tfEventIndex = " << fEventIndex << " , fBlockIndex = " << fBlockIndex
409                         << " , fTrackIndex = " << fTrackIndex
410                 );
411                 return fCurrentTrack;
412         }
413         else
414                 return NULL;
415 }
416
417
418 Bool_t TrackSink::MoreTracks() const
419 {
420         return 0 <= fTrackIndex && fTrackIndex < NumberOfTracks();
421 }
422
423
424 const Track* TrackSink::GetNextTrack() const
425 {
426         DebugMsg(1, "TrackSink::GetNextTrack()");
427         
428         // Note: NumberOfTracks() checks if fCurrentBlock != NULL. If it is then it returns -1
429         // and since fTrackIndex is always >= -1 the if statement must go to the else part.
430         if (fTrackIndex < NumberOfTracks() - 1)
431         {
432                 fCurrentTrack = (Track*) fCurrentBlock->At( ++fTrackIndex );
433                 DebugMsg(2, "\tfEventIndex = " << fEventIndex << " , fBlockIndex = " << fBlockIndex
434                         << " , fTrackIndex = " << fTrackIndex
435                 );
436                 return fCurrentTrack;
437         }
438         else
439         {
440                 ResetTrackPointers();
441                 return NULL;
442         }
443 }
444
445
446 void TrackSink::ResetAllPointers() const
447 {
448         fEventIndex = -1;
449         fCurrentEvent = NULL;
450         fBlockIndex = -1;
451         fCurrentBlock = NULL;
452         fTrackIndex = -1;
453         fCurrentTrack = NULL;
454         DebugMsg(2, "\tfEventIndex = " << fEventIndex << " , fBlockIndex = " << fBlockIndex
455                 << " , fTrackIndex = " << fTrackIndex
456         );
457 }
458
459
460 void TrackSink::ResetBlockPointers() const
461 {
462         fBlockIndex = -1;
463         fCurrentBlock = NULL;
464         fTrackIndex = -1;
465         fCurrentTrack = NULL;
466         DebugMsg(2, "\tfEventIndex = " << fEventIndex << " , fBlockIndex = " << fBlockIndex
467                 << " , fTrackIndex = " << fTrackIndex
468         );
469 }
470
471
472 void TrackSink::ResetTrackPointers() const
473 {
474         fTrackIndex = -1;
475         fCurrentTrack = NULL;
476         DebugMsg(2, "\tfEventIndex = " << fEventIndex << " , fBlockIndex = " << fBlockIndex
477                 << " , fTrackIndex = " << fTrackIndex
478         );
479 }
480
481
482 TrackSink::EventData::EventData() : fBlocks(TClonesArray::Class())
483 {
484         fEventNumber = -1;
485 }
486
487
488 TrackSink::EventData::EventData(Int_t eventnumber)
489         : fBlocks(TClonesArray::Class())
490 {
491         fEventNumber = eventnumber;
492         
493         // If the following is not set then we do not write the fBlocks properly.
494         fBlocks.BypassStreamer(kFALSE);
495 }
496
497
498 TrackSink::EventData::~EventData()
499 {
500         DebugMsg(1, "TrackSink::EventData::~EventData()");
501         fBlocks.Clear("C");
502 }
503
504
505 } // AliMUONHLT