]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/MUON/OnlineAnalysis/AliHLTMUONMansoTrackerFSMComponent.cxx
Important updates for Manso Tracker to use magnetic field integrals based on global...
[u/mrichter/AliRoot.git] / HLT / MUON / OnlineAnalysis / AliHLTMUONMansoTrackerFSMComponent.cxx
1 /**************************************************************************
2  * This file is property of and copyright by the ALICE HLT Project        *
3  * All rights reserved.                                                   *
4  *                                                                        *
5  * Primary Authors:                                                       *
6  *   Artur Szostak <artursz@iafrica.com>                                  *
7  *   Indranil Das <indra.das@saha.ac.in>                                  *
8  *                                                                        *
9  * Permission to use, copy, modify and distribute this software and its   *
10  * documentation strictly for non-commercial purposes is hereby granted   *
11  * without fee, provided that the above copyright notice appears in all   *
12  * copies and that both the copyright notice and this permission notice   *
13  * appear in the supporting documentation. The authors make no claims     *
14  * about the suitability of this software for any purpose. It is          *
15  * provided "as is" without express or implied warranty.                  *
16  **************************************************************************/
17
18 // $Id$
19
20 ///
21 ///  @file   AliHLTMUONMansoTrackerFSMComponent.cxx
22 ///  @author Artur Szostak <artursz@iafrica.com>,
23 ///          Indranil Das <indra.das@saha.ac.in>
24 ///  @date   18 Sep 2007
25 ///  @brief  Implementation of AliHLTMUONMansoTrackerFSMComponent class.
26 ///
27
28 #include "AliHLTMUONMansoTrackerFSMComponent.h"
29 #include "AliHLTMUONConstants.h"
30 #include "AliHLTMUONCalculations.h"
31 #include "AliHLTMUONUtils.h"
32 #include "AliHLTMUONMansoTrackerFSM.h"
33 #include "AliHLTMUONDataBlockReader.h"
34 #include "AliHLTMUONDataBlockWriter.h"
35 #include <cstdlib>
36 #include <cstring>
37 #include <cerrno>
38 #include <new>
39
40 ClassImp(AliHLTMUONMansoTrackerFSMComponent);
41
42
43 AliHLTMUONMansoTrackerFSMComponent::AliHLTMUONMansoTrackerFSMComponent() :
44         AliHLTMUONProcessor(),
45         AliHLTMUONMansoTrackerFSMCallback(),
46         fTracker(NULL),
47         fTrackCount(0),
48         fBlock(NULL),
49         fRecHitBlockArraySize(0),
50         fWarnForUnexpecedBlock(false),
51         fCanLoadZmiddle(true),
52         fCanLoadBL(true)
53 {
54         ///
55         /// Default constructor.
56         ///
57         
58         for (int i = 0; i < 4; i++)
59         {
60                 fRecHitBlockCount[i] = 0;
61                 fRecHitBlock[i] = NULL;
62         }
63         
64         ResetCanLoadFlags();
65 }
66
67
68 AliHLTMUONMansoTrackerFSMComponent::~AliHLTMUONMansoTrackerFSMComponent()
69 {
70         ///
71         /// Default destructor.
72         ///
73         
74         // Should never have the following 2 pointers non-NULL since DoDeinit
75         // should have been called before, but handle this case anyway.
76         if (fTracker != NULL) delete fTracker;
77         
78         // Remember that only fRecHitBlock[0] stores the pointer to the allocated
79         // memory. The other pointers are just reletive to this.
80         if (fRecHitBlock[0] != NULL) delete [] fRecHitBlock[0];
81 }
82
83
84 const char* AliHLTMUONMansoTrackerFSMComponent::GetComponentID()
85 {
86         ///
87         /// Inherited from AliHLTComponent. Returns the component ID.
88         ///
89         
90         return AliHLTMUONConstants::MansoTrackerFSMId();
91 }
92
93
94 void AliHLTMUONMansoTrackerFSMComponent::GetInputDataTypes(
95                 AliHLTComponentDataTypeList& list
96         )
97 {
98         ///
99         /// Inherited from AliHLTProcessor. Returns the list of expected input data types.
100         ///
101         
102         assert( list.empty() );
103         list.push_back( AliHLTMUONConstants::TriggerRecordsBlockDataType() );
104         list.push_back( AliHLTMUONConstants::RecHitsBlockDataType() );
105 }
106
107
108 AliHLTComponentDataType AliHLTMUONMansoTrackerFSMComponent::GetOutputDataType()
109 {
110         ///
111         /// Inherited from AliHLTComponent. Returns the output data type.
112         ///
113         
114         return kAliHLTMultipleDataType;
115 }
116
117
118 int AliHLTMUONMansoTrackerFSMComponent::GetOutputDataTypes(AliHLTComponentDataTypeList& list)
119 {
120         /// Inherited from AliHLTComponent. Returns the output data types.
121         
122         assert( list.empty() );
123         list.push_back( AliHLTMUONConstants::MansoTracksBlockDataType() );
124         list.push_back( AliHLTMUONConstants::MansoCandidatesBlockDataType() );
125         return list.size();
126 }
127
128
129 void AliHLTMUONMansoTrackerFSMComponent::GetOutputDataSize(
130                 unsigned long& constBase, double& inputMultiplier
131         )
132 {
133         ///
134         /// Inherited from AliHLTComponent. Returns an estimate of the expected output data size.
135         ///
136         
137         constBase = sizeof(AliHLTMUONMansoTracksBlockStruct) + 1024*1024;
138         inputMultiplier = 1;
139 }
140
141
142 AliHLTComponent* AliHLTMUONMansoTrackerFSMComponent::Spawn()
143 {
144         ///
145         /// Inherited from AliHLTComponent. Creates a new object instance.
146         ///
147         
148         return new AliHLTMUONMansoTrackerFSMComponent;
149 }
150
151
152 int AliHLTMUONMansoTrackerFSMComponent::DoInit(int argc, const char** argv)
153 {
154         ///
155         /// Inherited from AliHLTComponent.
156         /// Parses the command line parameters and initialises the component.
157         ///
158         
159         HLTInfo("Initialising dHLT manso tracker FSM component.");
160         
161         // Inherit the parents functionality.
162         int result = AliHLTMUONProcessor::DoInit(argc, argv);
163         if (result != 0) return result;
164
165         // Just in case for whatever reason we still have some of the internal
166         // object allocated previously still hanging around delete them now.
167         FreeMemory();
168         
169         fWarnForUnexpecedBlock = false;
170         bool makeCandidates = false;
171         ResetCanLoadFlags();
172         double zmiddle = 0;
173         double bfieldintegral = 0;
174         double roiA[4] = {0, 0, 0, 0};
175         double roiB[4] = {0, 0, 0, 0};
176         double chamberZ[6] = {0, 0, 0, 0, 0, 0};
177         
178         for (int i = 0; i < argc; i++)
179         {
180                 if (ArgumentAlreadyHandled(i, argv[i])) continue;
181
182                 if (strcmp( argv[i], "-zmiddle" ) == 0)
183                 {
184                         if (not fCanLoadZmiddle)
185                         {
186                                 HLTWarning("The Z coordinate for the middle of the dipole was already specified."
187                                         " Will replace previous value given by -zmiddle."
188                                 );
189                         }
190                         
191                         if ( argc <= i+1 )
192                         {
193                                 HLTError("The Z coordinate for the middle of the dipole was not specified." );
194                                 return -EINVAL;
195                         }
196                         
197                         char* cpErr = NULL;
198                         zmiddle = strtod(argv[i+1], &cpErr);
199                         if (cpErr == NULL or *cpErr != '\0')
200                         {
201                                 HLTError("Cannot convert '%s' to a valid floating point number.",
202                                         argv[i+1]
203                                 );
204                                 return -EINVAL;
205                         }
206                         
207                         fCanLoadZmiddle = false;  // Prevent loading from CDB.
208                         i++;
209                         continue;
210                 }
211         
212                 if (strcmp( argv[i], "-bfieldintegral" ) == 0)
213                 {
214                         if (not fCanLoadBL)
215                         {
216                                 HLTWarning("The magnetic field integral was already specified."
217                                         " Will replace previous value given by -bfieldintegral."
218                                 );
219                         }
220                         
221                         if ( argc <= i+1 )
222                         {
223                                 HLTError("The magnetic field integral was not specified." );
224                                 return -EINVAL;
225                         }
226                         
227                         char* cpErr = NULL;
228                         bfieldintegral = strtod(argv[i+1], &cpErr);
229                         if (cpErr == NULL or *cpErr != '\0')
230                         {
231                                 HLTError("Cannot convert '%s' to a valid floating point number.",
232                                         argv[i+1]
233                                 );
234                                 return -EINVAL;
235                         }
236                         
237                         fCanLoadBL = false;  // Prevent loading from CDB.
238                         i++;
239                         continue;
240                 }
241         
242                 if (strcmp(argv[i], "-a7") == 0 or strcmp(argv[i], "-a8") == 0 or
243                     strcmp(argv[i], "-a9") == 0 or strcmp(argv[i], "-a10") == 0
244                    )
245                 {
246                         int chamber = 7; int chamberIndex = 0;
247                         switch (argv[i][2])
248                         {
249                         case '7': chamber = 7; chamberIndex = 0; break;
250                         case '8': chamber = 8; chamberIndex = 1; break;
251                         case '9': chamber = 9; chamberIndex = 2; break;
252                         case '1': chamber = 10; chamberIndex = 3; break;
253                         }
254                         
255                         if (not fCanLoadA[chamberIndex])
256                         {
257                                 HLTWarning("The region of interest parameter 'A' for chamber %d was"
258                                         " already specified. Will replace previous value given by -a%d.",
259                                         chamber, chamber
260                                 );
261                         }
262                         
263                         if ( argc <= i+1 )
264                         {
265                                 HLTError("The region of interest parameter was not specified." );
266                                 return -EINVAL;
267                         }
268                         
269                         char* cpErr = NULL;
270                         roiA[chamberIndex] = strtod(argv[i+1], &cpErr);
271                         if (cpErr == NULL or *cpErr != '\0')
272                         {
273                                 HLTError("Cannot convert '%s' to a valid floating point number.",
274                                         argv[i+1]
275                                 );
276                                 return -EINVAL;
277                         }
278                         
279                         fCanLoadA[chamberIndex] = false;  // Prevent loading from CDB.
280                         i++;
281                         continue;
282                 }
283         
284                 if (strcmp(argv[i], "-b7") == 0 or strcmp(argv[i], "-b8") == 0 or
285                     strcmp(argv[i], "-b9") == 0 or strcmp(argv[i], "-b10") == 0
286                    )
287                 {
288                         int chamber = 7; int chamberIndex = 0;
289                         switch (argv[i][2])
290                         {
291                         case '7': chamber = 7; chamberIndex = 0; break;
292                         case '8': chamber = 8; chamberIndex = 1; break;
293                         case '9': chamber = 9; chamberIndex = 2; break;
294                         case '1': chamber = 10; chamberIndex = 3; break;
295                         }
296                         
297                         if (not fCanLoadB[chamberIndex])
298                         {
299                                 HLTWarning("The region of interest parameter 'B' for chamber %d was"
300                                         " already specified. Will replace previous value given by -b%d.",
301                                         chamber, chamber
302                                 );
303                         }
304                         
305                         if ( argc <= i+1 )
306                         {
307                                 HLTError("The region of interest parameter was not specified." );
308                                 return -EINVAL;
309                         }
310                         
311                         char* cpErr = NULL;
312                         roiB[chamberIndex] = strtod(argv[i+1], &cpErr);
313                         if (cpErr == NULL or *cpErr != '\0')
314                         {
315                                 HLTError("Cannot convert '%s' to a valid floating point number.",
316                                         argv[i+1]
317                                 );
318                                 return -EINVAL;
319                         }
320                         
321                         fCanLoadB[chamberIndex] = false;  // Prevent loading from CDB.
322                         i++;
323                         continue;
324                 }
325                 
326                 if (strcmp(argv[i], "-z7") == 0 or strcmp(argv[i], "-z8") == 0 or
327                     strcmp(argv[i], "-z9") == 0 or strcmp(argv[i], "-z10") == 0 or
328                     strcmp(argv[i], "-z11") == 0 or strcmp(argv[i], "-z13") == 0
329                    )
330                 {
331                         int chamber = 7; int chamberIndex = 0;
332                         switch (argv[i][2])
333                         {
334                         case '7': chamber = 7; chamberIndex = 0; break;
335                         case '8': chamber = 8; chamberIndex = 1; break;
336                         case '9': chamber = 9; chamberIndex = 2; break;
337                         case '1':
338                                 switch (argv[i][3])
339                                 {
340                                 case '0': chamber = 10; chamberIndex = 3; break;
341                                 case '1': chamber = 11; chamberIndex = 4; break;
342                                 case '3': chamber = 13; chamberIndex = 5; break;
343                                 }
344                                 break;
345                         }
346                         
347                         if (not fCanLoadZ[chamberIndex])
348                         {
349                                 HLTWarning("The nominal Z coordinate of chamber %d was already"
350                                         " specified. Will replace previous value given by -z%d.",
351                                         chamber, chamber
352                                 );
353                         }
354                         
355                         if ( argc <= i+1 )
356                         {
357                                 HLTError("The region of interest parameter was not specified." );
358                                 return -EINVAL;
359                         }
360                         
361                         char* cpErr = NULL;
362                         chamberZ[chamberIndex] = strtod(argv[i+1], &cpErr);
363                         if (cpErr == NULL or *cpErr != '\0')
364                         {
365                                 HLTError("Cannot convert '%s' to a valid floating point number.",
366                                         argv[i+1]
367                                 );
368                                 return -EINVAL;
369                         }
370                         
371                         fCanLoadZ[chamberIndex] = false;  // Prevent loading from CDB.
372                         i++;
373                         continue;
374                 }
375                 
376                 if (strcmp(argv[i], "-warn_on_unexpected_block") == 0)
377                 {
378                         fWarnForUnexpecedBlock = true;
379                         continue;
380                 }
381                 
382                 if (strcmp(argv[i], "-makecandidates") == 0)
383                 {
384                         makeCandidates = true;
385                         continue;
386                 }
387                 
388                 HLTError("Unknown option '%s'.", argv[i]);
389                 return -EINVAL;
390         }
391         
392         try
393         {
394                 fTracker = new AliHLTMUONMansoTrackerFSM();
395         }
396         catch (const std::bad_alloc&)
397         {
398                 HLTError("Could not allocate more memory for the tracker component.");
399                 return -ENOMEM;
400         }
401         fTracker->SetCallback(this);
402         fTracker->MakeCandidates(makeCandidates);
403         
404         // Set all the parameters that were found on the command line.
405         if (not fCanLoadZmiddle) AliHLTMUONCalculations::Zf(zmiddle);
406         if (not fCanLoadBL) AliHLTMUONCalculations::QBL(bfieldintegral);
407         if (not fCanLoadA[0]) fTracker->SetA7(roiA[0]);
408         if (not fCanLoadA[1]) fTracker->SetA8(roiA[1]);
409         if (not fCanLoadA[2]) fTracker->SetA9(roiA[2]);
410         if (not fCanLoadA[3]) fTracker->SetA10(roiA[3]);
411         if (not fCanLoadB[0]) fTracker->SetB7(roiB[0]);
412         if (not fCanLoadB[1]) fTracker->SetB8(roiB[1]);
413         if (not fCanLoadB[2]) fTracker->SetB9(roiB[2]);
414         if (not fCanLoadB[3]) fTracker->SetB10(roiB[3]);
415         if (not fCanLoadZ[0]) fTracker->SetZ7(chamberZ[0]);
416         if (not fCanLoadZ[1]) fTracker->SetZ8(chamberZ[1]);
417         if (not fCanLoadZ[2]) fTracker->SetZ9(chamberZ[2]);
418         if (not fCanLoadZ[3]) fTracker->SetZ10(chamberZ[3]);
419         if (not fCanLoadZ[4]) fTracker->SetZ11(chamberZ[4]);
420         if (not fCanLoadZ[5]) fTracker->SetZ13(chamberZ[5]);
421         
422         if (not DelaySetup())
423         {
424                 if (AtLeastOneCanLoadFlagsIsSet())
425                 {
426                         HLTInfo("Loading configuration parameters from CDB.");
427                         
428                         result = ReadConfigFromCDB();
429                         if (result != 0)
430                         {
431                                 // Error messages already generated in ReadConfigFromCDB.
432                                 FreeMemory(); // Make sure we cleanup to avoid partial initialisation.
433                                 return result;
434                         }
435                 }
436                 else
437                 {
438                         // Print the debug messages here since ReadConfigFromCDB does not get called,
439                         // in-which the debug messages would have been printed.
440                         HLTDebug("Using the following configuration parameters:");
441                         HLTDebug("                    Middle of dipole Z coordinate = %f cm", AliHLTMUONCalculations::Zf());
442                         HLTDebug("                          Magnetic field integral = %f T.m", AliHLTMUONCalculations::QBL());
443                         HLTDebug("   Region of interest parameter 'A' for chamber 7 = %f",    fTracker->GetA7());
444                         HLTDebug("   Region of interest parameter 'B' for chamber 7 = %f cm", fTracker->GetB7());
445                         HLTDebug("   Region of interest parameter 'A' for chamber 8 = %f",    fTracker->GetA8());
446                         HLTDebug("   Region of interest parameter 'B' for chamber 8 = %f cm", fTracker->GetB8());
447                         HLTDebug("   Region of interest parameter 'A' for chamber 9 = %f",    fTracker->GetA9());
448                         HLTDebug("   Region of interest parameter 'B' for chamber 9 = %f cm", fTracker->GetB9());
449                         HLTDebug("  Region of interest parameter 'A' for chamber 10 = %f",    fTracker->GetA10());
450                         HLTDebug("  Region of interest parameter 'B' for chamber 10 = %f cm", fTracker->GetB10());
451                         HLTDebug("               Nominal Z coordinate for chamber 7 = %f cm", fTracker->GetZ7());
452                         HLTDebug("               Nominal Z coordinate for chamber 8 = %f cm", fTracker->GetZ8());
453                         HLTDebug("               Nominal Z coordinate for chamber 9 = %f cm", fTracker->GetZ9());
454                         HLTDebug("              Nominal Z coordinate for chamber 10 = %f cm", fTracker->GetZ10());
455                         HLTDebug("              Nominal Z coordinate for chamber 11 = %f cm", fTracker->GetZ11());
456                         HLTDebug("              Nominal Z coordinate for chamber 13 = %f cm", fTracker->GetZ13());
457                 }
458                 
459                 ResetCanLoadFlags();  // From this point read all parameters from CDB.
460         }
461         
462         const int initArraySize = 10;
463         // Allocate some initial memory for the reconstructed hit arrays.
464         try
465         {
466                 fRecHitBlock[0] = new AliRecHitBlockInfo[initArraySize*4];
467         }
468         catch (const std::bad_alloc&)
469         {
470                 HLTError("Could not allocate more memory for the reconstructed hit arrays.");
471                 FreeMemory(); // Make sure we cleanup to avoid partial initialisation.
472                 return -ENOMEM;
473         }
474         // Only set the arrays' size once we have successfully allocated the memory for the arrays.
475         fRecHitBlockArraySize = initArraySize;
476         // Now we need to set the pointers fRecHitBlock[i] {i>0} relative to fRecHitBlock[0].
477         for (Int_t i = 1; i < 4; i++)
478         {
479                 fRecHitBlock[i] = fRecHitBlock[i-1] + fRecHitBlockArraySize;
480         }
481         // And reset the number of records actually stored in the arrays.
482         for (Int_t i = 0; i < 4; i++)
483         {
484                 fRecHitBlockCount[i] = 0;
485         }
486         
487         return 0;
488 }
489
490
491 int AliHLTMUONMansoTrackerFSMComponent::Reconfigure(
492                 const char* cdbEntry, const char* componentId
493         )
494 {
495         /// Inherited from AliHLTComponent. This method will reload CDB configuration
496         /// entries for this component from the CDB.
497         /// \param cdbEntry If this is NULL or equals "HLT/ConfigMUON/MansoTrackerFSM"
498         ///     then new configuration parameters are loaded, otherwise nothing is done.
499         /// \param componentId  The name of the component in the current chain.
500         
501         bool givenConfigPath = strcmp(cdbEntry, AliHLTMUONConstants::MansoTrackerFSMCDBPath()) == 0;
502         
503         if (cdbEntry == NULL or givenConfigPath)
504         {
505                 HLTInfo("Reading new configuration entries from CDB for component '%s'.", componentId);
506                 ResetCanLoadFlags();  // Make sure to allow reading all values from CDB.
507                 int result = ReadConfigFromCDB();
508                 if (result != 0) return result;
509         }
510         
511         return 0;
512 }
513
514
515 int AliHLTMUONMansoTrackerFSMComponent::ReadPreprocessorValues(const char* modules)
516 {
517         /// Inherited from AliHLTComponent. 
518         /// Updates the configuration of this component if HLT or ALL has been
519         /// specified in the 'modules' list.
520
521         TString mods = modules;
522         if (mods.Contains("ALL"))
523         {
524                 return Reconfigure(NULL, GetComponentID());
525         }
526         if (mods.Contains("HLT"))
527         {
528                 return Reconfigure(AliHLTMUONConstants::MansoTrackerFSMCDBPath(), GetComponentID());
529         }
530         return 0;
531 }
532
533
534 int AliHLTMUONMansoTrackerFSMComponent::ReadConfigFromCDB()
535 {
536         /// Reads this component's configuration parameters from the CDB.
537         /// These include the middle of the dipole Z coordinate (zmiddle), the
538         /// integrated magnetic field of the dipole, Z coordinates of the chambers
539         /// and the region of interest parameters used during the tracking.
540         /// \param setZmiddle Indicates if the zmiddle parameter should be set
541         ///       (default true).
542         /// \param setBL Indicates if the integrated magnetic field parameter should
543         ///       be set (default true).
544         /// \return 0 if no errors occured and negative error code compatible with
545         ///       the HLT framework on errors.
546
547         const char* pathToEntry = AliHLTMUONConstants::MansoTrackerFSMCDBPath();
548         
549         TMap* map = NULL;
550         int result = FetchTMapFromCDB(pathToEntry, map);
551         if (result != 0) return result;
552         
553         Double_t value = 0;
554         if (fCanLoadZmiddle)
555         {
556                 result = GetFloatFromTMap(map, "zmiddle", value, pathToEntry, "dipole middle Z coordinate");
557                 if (result != 0) return result;
558                 AliHLTMUONCalculations::Zf(value);
559         }
560         
561         if (fCanLoadBL)
562         {
563                 Double_t bfieldintegral;
564                 result = FetchFieldIntegral(bfieldintegral);
565                 if (result == 0)
566                 {
567                         AliHLTMUONCalculations::QBL(bfieldintegral);
568                 }
569                 else
570                 {
571                         HLTWarning("Failed to load the magnetic field integral from GRP information.");
572                         result = GetFloatFromTMap(map, "bfieldintegral", value, pathToEntry, "integrated magnetic field");
573                         if (result != 0) return result;
574                         HLTWarning(Form("Using deprecated magnetic field integral value of %f T.m.", value));
575                         AliHLTMUONCalculations::QBL(value);
576                 }
577         }
578         
579         if (fCanLoadA[0])
580         {
581                 result = GetFloatFromTMap(map, "roi_paramA_chamber7", value, pathToEntry, "chamber 7 region of interest 'A'");
582                 if (result != 0) return result;
583                 fTracker->SetA7(value);
584         }
585         if (fCanLoadA[1])
586         {
587                 result = GetFloatFromTMap(map, "roi_paramA_chamber8", value, pathToEntry, "chamber 8 region of interest 'A'");
588                 if (result != 0) return result;
589                 fTracker->SetA8(value);
590         }
591         if (fCanLoadA[2])
592         {
593                 result = GetFloatFromTMap(map, "roi_paramA_chamber9", value, pathToEntry, "chamber 9 region of interest 'A'");
594                 if (result != 0) return result;
595                 fTracker->SetA9(value);
596         }
597         if (fCanLoadA[3])
598         {
599                 result = GetFloatFromTMap(map, "roi_paramA_chamber10", value, pathToEntry, "chamber 10 region of interest 'A'");
600                 if (result != 0) return result;
601                 fTracker->SetA10(value);
602         }
603         
604         if (fCanLoadB[0])
605         {
606                 result = GetFloatFromTMap(map, "roi_paramB_chamber7", value, pathToEntry, "chamber 7 region of interest 'B'");
607                 if (result != 0) return result;
608                 fTracker->SetB7(value);
609         }
610         if (fCanLoadB[1])
611         {
612                 result = GetFloatFromTMap(map, "roi_paramB_chamber8", value, pathToEntry, "chamber 8 region of interest 'B'");
613                 if (result != 0) return result;
614                 fTracker->SetB8(value);
615         }
616         if (fCanLoadB[2])
617         {
618                 result = GetFloatFromTMap(map, "roi_paramB_chamber9", value, pathToEntry, "chamber 9 region of interest 'B'");
619                 if (result != 0) return result;
620                 fTracker->SetB9(value);
621         }
622         if (fCanLoadB[3])
623         {
624                 result = GetFloatFromTMap(map, "roi_paramB_chamber10", value, pathToEntry, "chamber 10 region of interest 'B'");
625                 if (result != 0) return result;
626                 fTracker->SetB10(value);
627         }
628         
629         if (fCanLoadZ[0])
630         {
631                 result = GetFloatFromTMap(map, "chamber7postion", value, pathToEntry, "nominal chamber 7 Z coordinate");
632                 if (result != 0) return result;
633                 fTracker->SetZ7(value);
634         }
635         if (fCanLoadZ[1])
636         {
637                 result = GetFloatFromTMap(map, "chamber8postion", value, pathToEntry, "nominal chamber 8 Z coordinate");
638                 if (result != 0) return result;
639                 fTracker->SetZ8(value);
640         }
641         if (fCanLoadZ[2])
642         {
643                 result = GetFloatFromTMap(map, "chamber9postion", value, pathToEntry, "nominal chamber 9 Z coordinate");
644                 if (result != 0) return result;
645                 fTracker->SetZ9(value);
646         }
647         if (fCanLoadZ[3])
648         {
649                 result = GetFloatFromTMap(map, "chamber10postion", value, pathToEntry, "nominal chamber 10 Z coordinate");
650                 if (result != 0) return result;
651                 fTracker->SetZ10(value);
652         }
653         if (fCanLoadZ[4])
654         {
655                 result = GetFloatFromTMap(map, "chamber11postion", value, pathToEntry, "nominal chamber 11 Z coordinate");
656                 if (result != 0) return result;
657                 fTracker->SetZ11(value);
658         }
659         if (fCanLoadZ[5])
660         {
661                 result = GetFloatFromTMap(map, "chamber13postion", value, pathToEntry, "nominal chamber 13 Z coordinate");
662                 if (result != 0) return result;
663                 fTracker->SetZ13(value);
664         }
665         
666         HLTDebug("Using the following configuration parameters:");
667         HLTDebug("                    Middle of dipole Z coordinate = %f cm", AliHLTMUONCalculations::Zf());
668         HLTDebug("                          Magnetic field integral = %f T.m", AliHLTMUONCalculations::QBL());
669         HLTDebug("   Region of interest parameter 'A' for chamber 7 = %f",    fTracker->GetA7());
670         HLTDebug("   Region of interest parameter 'B' for chamber 7 = %f cm", fTracker->GetB7());
671         HLTDebug("   Region of interest parameter 'A' for chamber 8 = %f",    fTracker->GetA8());
672         HLTDebug("   Region of interest parameter 'B' for chamber 8 = %f cm", fTracker->GetB8());
673         HLTDebug("   Region of interest parameter 'A' for chamber 9 = %f",    fTracker->GetA9());
674         HLTDebug("   Region of interest parameter 'B' for chamber 9 = %f cm", fTracker->GetB9());
675         HLTDebug("  Region of interest parameter 'A' for chamber 10 = %f",    fTracker->GetA10());
676         HLTDebug("  Region of interest parameter 'B' for chamber 10 = %f cm", fTracker->GetB10());
677         HLTDebug("               Nominal Z coordinate for chamber 7 = %f cm", fTracker->GetZ7());
678         HLTDebug("               Nominal Z coordinate for chamber 8 = %f cm", fTracker->GetZ8());
679         HLTDebug("               Nominal Z coordinate for chamber 9 = %f cm", fTracker->GetZ9());
680         HLTDebug("              Nominal Z coordinate for chamber 10 = %f cm", fTracker->GetZ10());
681         HLTDebug("              Nominal Z coordinate for chamber 11 = %f cm", fTracker->GetZ11());
682         HLTDebug("              Nominal Z coordinate for chamber 13 = %f cm", fTracker->GetZ13());
683         
684         return 0;
685 }
686
687
688 int AliHLTMUONMansoTrackerFSMComponent::DoDeinit()
689 {
690         ///
691         /// Inherited from AliHLTComponent. Performs a cleanup of the component.
692         ///
693         
694         HLTInfo("Deinitialising dHLT manso tracker FSM component.");
695         FreeMemory();
696         return 0;
697 }
698
699
700 int AliHLTMUONMansoTrackerFSMComponent::DoEvent(
701                 const AliHLTComponentEventData& evtData,
702                 const AliHLTComponentBlockData* blocks,
703                 AliHLTComponentTriggerData& trigData,
704                 AliHLTUInt8_t* outputPtr,
705                 AliHLTUInt32_t& size,
706                 AliHLTComponentBlockDataList& outputBlocks
707         )
708 {
709         ///
710         /// Inherited from AliHLTProcessor. Processes the new event data.
711         ///
712         
713         // Initialise the configuration parameters from CDB if we were
714         // requested to initialise only when the first event was received.
715         if (DelaySetup())
716         {
717                 // Load the configuration paramters from CDB if they have not
718                 // been given on the command line.
719                 if (AtLeastOneCanLoadFlagsIsSet())
720                 {
721                         HLTInfo("Loading configuration parameters from CDB.");
722                         int result = ReadConfigFromCDB();
723                         if (result != 0) return result;
724                 }
725                 
726                 DoneDelayedSetup();
727                 ResetCanLoadFlags();  // From this point read all parameters from CDB.
728         }
729         
730         Reset();
731         AliHLTUInt32_t specification = 0;  // Contains the output data block spec bits.
732         
733         // Resize the rec hit arrays if we possibly will need more space.
734         // To guarantee that they will not overflow we need to make sure each
735         // array is at least as big as the number of input data blocks.
736         if (fRecHitBlockArraySize < evtData.fBlockCnt)
737         {
738                 // Release the old memory block and allocate more memory.
739                 if (fRecHitBlock[0] != NULL)
740                 {
741                         delete [] fRecHitBlock[0];
742                 }
743                 
744                 // Reset the number of records actually stored in the arrays.
745                 for (Int_t i = 0; i < 4; i++)
746                 {
747                         fRecHitBlockCount[i] = 0;
748                 }
749                 
750                 try
751                 {
752                         fRecHitBlock[0] = new AliRecHitBlockInfo[evtData.fBlockCnt*4];
753                 }
754                 catch (const std::bad_alloc&)
755                 {
756                         HLTError("Could not allocate more memory for the reconstructed hit arrays.");
757                         // Ok so now we need to clear all the pointers because we actually
758                         // deleted the memory.
759                         fRecHitBlockArraySize = 0;
760                         for (Int_t i = 0; i < 4; i++)
761                         {
762                                 fRecHitBlock[i] = NULL;
763                         }
764                         if (DumpDataOnError()) DumpEvent(evtData, blocks, trigData, outputPtr, size, outputBlocks);
765                         return -ENOMEM;
766                 }
767                 // Only set the arrays' size once we have successfully allocated the memory for the arrays.
768                 fRecHitBlockArraySize = evtData.fBlockCnt;
769                 // Now we need to set the pointers fRecHitBlock[i] {i>0} relative to fRecHitBlock[0].
770                 for (Int_t i = 1; i < 4; i++)
771                 {
772                         fRecHitBlock[i] = fRecHitBlock[i-1] + fRecHitBlockArraySize;
773                 }
774         }
775         
776         AliHLTMUONMansoTracksBlockWriter block(outputPtr, size);
777         fBlock = &block;
778         
779         if (not block.InitCommonHeader())
780         {
781                 Logging(kHLTLogError,
782                         "AliHLTMUONMansoTrackerFSMComponent::DoEvent",
783                         "Buffer overflow",
784                         "The buffer is only %d bytes in size. We need a minimum of %d bytes.",
785                         size, sizeof(AliHLTMUONMansoTracksBlockWriter::HeaderType)
786                 );
787                 if (DumpDataOnError()) DumpEvent(evtData, blocks, trigData, outputPtr, size, outputBlocks);
788                 size = 0; // Important to tell framework that nothing was generated.
789                 return -ENOBUFS;
790         }
791
792         // Loop over all input blocks in the event and add the ones that contain
793         // reconstructed hits into the hit buffers. The blocks containing trigger
794         // records are ignored for now and will be processed later.
795         for (AliHLTUInt32_t n = 0; n < evtData.fBlockCnt; n++)
796         {
797                 HLTDebug("Handling block: %u, with fDataType = '%s', fPtr = %p and fSize = %u bytes.",
798                         n, DataType2Text(blocks[n].fDataType).c_str(), blocks[n].fPtr, blocks[n].fSize
799                 );
800                 
801                 if (blocks[n].fDataType == AliHLTMUONConstants::RecHitsBlockDataType())
802                 {
803                         specification |= blocks[n].fSpecification;
804                         
805                         AliHLTMUONRecHitsBlockReader inblock(blocks[n].fPtr, blocks[n].fSize);
806                         if (not BlockStructureOk(inblock))
807                         {
808                                 if (DumpDataOnError()) DumpEvent(evtData, blocks, trigData, outputPtr, size, outputBlocks);
809                                 continue;
810                         }
811                         
812                         if (inblock.Nentries() != 0)
813                                 AddRecHits(blocks[n].fSpecification, inblock.GetArray(), inblock.Nentries());
814                         else
815                         {
816                                 Logging(kHLTLogDebug,
817                                         "AliHLTMUONMansoTrackerFSMComponent::DoEvent",
818                                         "Block empty",
819                                         "Received a reconstructed hits data block which contains no entries."
820                                 );
821                         }
822                 }
823                 else if (blocks[n].fDataType != AliHLTMUONConstants::TriggerRecordsBlockDataType())
824                 {
825                         // Log a message indicating that we got a data block that we
826                         // do not know how to handle.
827                         if (fWarnForUnexpecedBlock)
828                                 HLTWarning("Received a data block of a type we cannot handle: '%s', spec: 0x%X",
829                                         DataType2Text(blocks[n].fDataType).c_str(), blocks[n].fSpecification
830                                 );
831 #ifdef __DEBUG
832                         else
833                                 HLTDebug("Received a data block of a type we cannot handle: '%s', spec: 0x%X",
834                                         DataType2Text(blocks[n].fDataType).c_str(), blocks[n].fSpecification
835                                 );
836 #endif
837                 }
838         }
839   
840         // Again loop over all input blocks in the event, but this time look for
841         // the trigger record blocks and process these.
842         for (AliHLTUInt32_t n = 0; n < evtData.fBlockCnt; n++)
843         {
844                 if (blocks[n].fDataType != AliHLTMUONConstants::TriggerRecordsBlockDataType())
845                         continue;
846                 
847                 AliHLTMUONTriggerRecordsBlockReader inblock(blocks[n].fPtr, blocks[n].fSize);
848                 if (not BlockStructureOk(inblock))
849                 {
850                         if (DumpDataOnError()) DumpEvent(evtData, blocks, trigData, outputPtr, size, outputBlocks);
851                         continue;
852                 }
853                 
854                 DebugTrace("Processing a trigger block with "
855                         << inblock.Nentries() << " entries."
856                 );
857                 
858                 specification |= blocks[n].fSpecification;
859                 
860                 for (AliHLTUInt32_t i = 0; i < inblock.Nentries(); i++)
861                 {
862                         fTracker->FindTrack(inblock[i]);
863                         
864                         // Reset the tracker so that we do not double count tracks.
865                         fTracker->Reset();
866                 }
867         }
868         
869         AliHLTComponentBlockData bd;
870         FillBlockData(bd);
871         bd.fPtr = outputPtr;
872         bd.fOffset = 0;
873         bd.fSize = block.BytesUsed();
874         bd.fDataType = AliHLTMUONConstants::MansoTracksBlockDataType();
875         bd.fSpecification = specification;
876         outputBlocks.push_back(bd);
877         AliHLTUInt32_t totalSize = block.BytesUsed();
878         
879         if (fTracker->MakeCandidates())
880         {
881                 AliHLTMUONMansoCandidatesBlockWriter candidatesBlock(outputPtr+totalSize, size-totalSize);
882                 if (not candidatesBlock.InitCommonHeader())
883                 {
884                         HLTError("Buffer overflowed. There are only %d bytes left in the buffer,"
885                                 " but we need a minimum of %d bytes.",
886                                 size, sizeof(AliHLTMUONMansoCandidatesBlockWriter::HeaderType)
887                         );
888                         if (DumpDataOnError()) DumpEvent(evtData, blocks, trigData, outputPtr, size, outputBlocks);
889                         size = 0; // Important to tell framework that nothing was generated.
890                         return -ENOBUFS;
891                 }
892                 
893                 // Fill in the output block buffer.
894                 candidatesBlock.SetNumberOfEntries(fTracker->TrackCandidatesCount());
895                 for (AliHLTUInt32_t i = 0; i < fTracker->TrackCandidatesCount(); ++i)
896                 {
897                         candidatesBlock[i] = fTracker->TrackCandidates()[i];
898                 }
899                 
900                 fTracker->ZeroTrackCandidatesList();
901                 
902                 AliHLTComponentBlockData bdc;
903                 FillBlockData(bdc);
904                 bdc.fPtr = outputPtr;
905                 bdc.fOffset = totalSize;
906                 bdc.fSize = candidatesBlock.BytesUsed();
907                 bdc.fDataType = AliHLTMUONConstants::MansoCandidatesBlockDataType();
908                 bdc.fSpecification = specification;
909                 outputBlocks.push_back(bdc);
910                 totalSize += candidatesBlock.BytesUsed();
911         }
912         
913         size = totalSize;
914         return 0;
915 }
916
917
918 void AliHLTMUONMansoTrackerFSMComponent::Reset()
919 {
920         ///
921         /// Reset the track count and reconstructed hit data block arrays.
922         ///
923         
924         DebugTrace("Resetting AliHLTMUONMansoTrackerFSMComponent.");
925
926         //fTracker->Reset();  // Not necessary here because it is done after every FindTrack call.
927         fTrackCount = 0;
928         fBlock = NULL;  // Do not delete. Already done implicitly at the end of DoEvent.
929         for (int i = 0; i < 4; i++)
930         {
931                 fRecHitBlockCount[i] = 0;
932         }
933 }
934
935
936 void AliHLTMUONMansoTrackerFSMComponent::FreeMemory()
937 {
938         /// Deletes any objects and arrays allocated by this component and releases
939         /// the memory used. This is called as a helper routine by the init and deinit
940         /// methods. If some or all of the object pointers are already NULL then
941         /// nothing is done for those. This method guarantees that all the relevant
942         /// pointers will be NULL after returning from this method.
943
944         if (fTracker != NULL)
945         {
946                 delete fTracker;
947                 fTracker = NULL;
948         }
949         
950         // Remember that only fRecHitBlock[0] stores the pointer to the allocated memory.
951         // The other pointers are just reletive to this.
952         if (fRecHitBlock[0] != NULL)
953                 delete [] fRecHitBlock[0];
954         
955         fRecHitBlockArraySize = 0;
956         for (Int_t i = 0; i < 4; i++)
957         {
958                 fRecHitBlockCount[i] = 0;
959                 fRecHitBlock[i] = NULL;
960         }
961 }
962
963
964 void AliHLTMUONMansoTrackerFSMComponent::AddRecHits(
965                 AliHLTUInt32_t specification,
966                 const AliHLTMUONRecHitStruct* recHits,
967                 AliHLTUInt32_t count
968         )
969 {
970         ///
971         /// Adds a new reconstructed hit data block to the internal list of blocks
972         /// for the tracker to process.
973         /// These lists will later be used when the tracker requests them through
974         /// the callback method 'RequestClusters'.
975         ///
976         
977         DebugTrace("AliHLTMUONMansoTrackerFSMComponent::AddRecHits called with specification = 0x"
978                  << std::hex << specification << std::dec << " and count = "
979                  << count << " rec hits."
980         );
981         
982         AliHLTUInt8_t chamberMap[20] = {
983                 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10
984         };
985         
986         // Identify the chamber the rec hits came from using the specifications field.
987         bool gotDataFromDDL[22];
988         AliHLTMUONUtils::UnpackSpecBits(specification, gotDataFromDDL);
989                 
990         AliHLTInt8_t chamber = -1;
991         for (int i = 0; i < 20; i++)
992         {
993                 if (not gotDataFromDDL[i]) continue;
994                 if (7 <= chamberMap[i] and chamberMap[i] <= 10)
995                 {
996                         if (chamber != -1 and chamber != chamberMap[i])
997                         {
998                                 Logging(kHLTLogError,
999                                         "AliHLTMUONMansoTrackerFSMComponent::AddRecHits",
1000                                         "Invalid block",
1001                                         "Received a data block with data from multiple chambers."
1002                                           " This component cannot handle such a case."
1003                                 );
1004                                 return;
1005                         }
1006                         else
1007                                 chamber = chamberMap[i];
1008                 }
1009                 else
1010                 {
1011                         Logging(kHLTLogError,
1012                                 "AliHLTMUONMansoTrackerFSMComponent::AddRecHits",
1013                                 "Invalid chamber",
1014                                 "Received a data block with data from chamber %d"
1015                                   " which is outside the expected range: [7..10].",
1016                                 chamberMap[i]
1017                         );
1018                         return;
1019                 }
1020         }
1021         
1022         // Make sure we got one chamber number.
1023         if (chamber < 7 or 10 < chamber)
1024         {
1025                 Logging(kHLTLogError,
1026                         "AliHLTMUONMansoTrackerFSMComponent::AddRecHits",
1027                         "Invalid block",
1028                         "Received a reconstructed hit data block with a null specification."
1029                          " Cannot know which chamber the data comes from."
1030                 );
1031                 return;
1032         }
1033         
1034         DebugTrace("Added " << count << " reconstructed hits from chamber "
1035                 << (int)chamber << " to the internal arrays."
1036         );
1037         
1038         assert( fRecHitBlockCount[chamber-7] < fRecHitBlockArraySize );
1039         AliRecHitBlockInfo info(count, recHits);
1040         fRecHitBlock[chamber-7][fRecHitBlockCount[chamber-7]] = info;
1041         fRecHitBlockCount[chamber-7]++;
1042 }
1043
1044
1045 void AliHLTMUONMansoTrackerFSMComponent::ResetCanLoadFlags()
1046 {
1047         /// Resets all the fCanLoad* flags to true. This enables loading of all
1048         /// those CDB entries in the method ReadConfigFromCDB.
1049         
1050         fCanLoadZmiddle = true;
1051         fCanLoadBL = true;
1052         for (int i = 0; i < 4; i++)
1053         {
1054                 fCanLoadA[i] = true;
1055                 fCanLoadB[i] = true;
1056         }
1057         for (int i = 0; i < 6; i++)
1058         {
1059                 fCanLoadZ[i] = true;
1060         }
1061 }
1062
1063
1064 bool AliHLTMUONMansoTrackerFSMComponent::AtLeastOneCanLoadFlagsIsSet() const
1065 {
1066         /// Returns true if at least one fCanLoad* flag was true and false otherwise.
1067
1068         if (fCanLoadZmiddle or fCanLoadBL) return true;
1069         for (int i = 0; i < 4; i++)
1070         {
1071                 if (fCanLoadA[i]) return true;
1072                 if (fCanLoadB[i]) return true;
1073         }
1074         for (int i = 0; i < 6; i++)
1075         {
1076                 if (fCanLoadZ[i]) return true;
1077         }
1078         return false;
1079 }
1080
1081
1082 void AliHLTMUONMansoTrackerFSMComponent::RequestClusters(
1083                 AliHLTMUONMansoTrackerFSM* tracker,
1084                 AliHLTFloat32_t left, AliHLTFloat32_t right,
1085                 AliHLTFloat32_t bottom, AliHLTFloat32_t top,
1086                 AliHLTMUONChamberName chamber, const void* tag
1087         )
1088 {
1089         ///
1090         /// Inherited from AliHLTMUONMansoTrackerFSMCallback.
1091         /// This is the call back method used by the tracker algorithm to request
1092         /// clusters on a certain chamber.
1093         ///
1094
1095         DebugTrace("AliHLTMUONMansoTracker::RequestClusters(chamber = " << chamber << ")");
1096         void* ctag = const_cast<void*>(tag);
1097         int chNo = -1;
1098         AliHLTUInt32_t recHitsCount = 0;
1099         AliRecHitBlockInfo* recHitsBlock = NULL;
1100         switch (chamber)
1101         {
1102         case kChamber7:
1103                 recHitsCount = fRecHitBlockCount[0];
1104                 recHitsBlock = fRecHitBlock[0];
1105                 chNo = 7;
1106                 break;
1107
1108         case kChamber8:
1109                 recHitsCount = fRecHitBlockCount[1];
1110                 recHitsBlock = fRecHitBlock[1];
1111                 chNo = 8;
1112                 break;
1113
1114         case kChamber9:
1115                 recHitsCount = fRecHitBlockCount[2];
1116                 recHitsBlock = fRecHitBlock[2];
1117                 chNo = 9;
1118                 break;
1119
1120         case kChamber10:
1121                 recHitsCount = fRecHitBlockCount[3];
1122                 recHitsBlock = fRecHitBlock[3];
1123                 chNo = 10;
1124                 break;
1125
1126         default: return;
1127         }
1128         
1129         DebugTrace("Returning requested hits for chamber " << chNo << ":");
1130         for (AliHLTUInt32_t i = 0; i < recHitsCount; i++)
1131         for (AliHLTUInt32_t j = 0; j < recHitsBlock[i].Count(); j++)
1132         {
1133                 const AliHLTMUONRecHitStruct* hit = &(recHitsBlock[i].Data()[j]);
1134                 if (left < hit->fX and hit->fX < right and bottom < hit->fY and hit->fY < top)
1135                         tracker->ReturnClusters(ctag, hit, 1);
1136         }
1137         DebugTrace("Done returning hits from chamber " << chNo << ".");
1138         tracker->EndOfClusters(ctag);
1139 }
1140
1141
1142 void AliHLTMUONMansoTrackerFSMComponent::EndOfClusterRequests(
1143                 AliHLTMUONMansoTrackerFSM* /*tracker*/
1144         )
1145 {
1146         ///
1147         /// Inherited from AliHLTMUONMansoTrackerFSMCallback.
1148         /// Nothing special to do here.
1149         ///
1150         
1151         DebugTrace("End of cluster requests.");
1152 }
1153
1154
1155 void AliHLTMUONMansoTrackerFSMComponent::FoundTrack(AliHLTMUONMansoTrackerFSM* tracker)
1156 {
1157         ///
1158         /// Inherited from AliHLTMUONMansoTrackerFSMCallback.
1159         /// This is the call back method used by the tracker algorithm to declare
1160         /// that a new track has been found.
1161         ///
1162         
1163         DebugTrace("AliHLTMUONMansoTrackerFSMComponent::FoundTrack()");
1164         
1165         AliHLTMUONMansoTracksBlockWriter* block =
1166                 reinterpret_cast<AliHLTMUONMansoTracksBlockWriter*>(fBlock);
1167         
1168         AliHLTMUONMansoTrackStruct newTrack;
1169         tracker->FillTrackData(newTrack);
1170         
1171         // The indicies of the duplicate tracks. If set to block->Nentries() then
1172         // this indicates the index is not used.
1173         AliHLTUInt32_t dup1 = block->Nentries();
1174         AliHLTUInt32_t dup2 = block->Nentries();
1175         
1176         // Check if there are any tracks that use the same hits as the one found.
1177         // If there are, then use the one that has the highest pT.
1178         // There will be at most 2 duplicate tracks.
1179         for (AliHLTUInt32_t i = 0; i < block->Nentries(); i++)
1180         {
1181                 AliHLTMUONMansoTrackStruct& track = (*block)[i];
1182                 bool hasNoDuplicates = true;
1183                 for (AliHLTUInt32_t j = 0; j < 4; j++)
1184                 {
1185                         if (track.fHit[j] == AliHLTMUONConstants::NilRecHitStruct()) continue;
1186                         if (newTrack.fHit[j] == AliHLTMUONConstants::NilRecHitStruct()) continue;
1187                         if (track.fHit[j] == newTrack.fHit[j])
1188                         {
1189                                 hasNoDuplicates = false;
1190                                 break;
1191                         }
1192                 }
1193                 if (hasNoDuplicates) continue;
1194                 
1195                 if (dup1 == block->Nentries())
1196                 {
1197                         dup1 = i;
1198                 }
1199                 else if (dup2 == block->Nentries())
1200                 {
1201                         dup2 = i;
1202                 }
1203                 else
1204                 {
1205                         HLTError("Found more than 2 tracks with duplicate hits. This is completely unexpected. Something is seriously wrong!");
1206                 }
1207         }
1208         
1209         if (dup1 != block->Nentries() and dup2 != block->Nentries())
1210         {
1211                 // In this case we found 2 duplicate entries.
1212                 // Figure out which one has the highest pT and keep only that one.
1213                 AliHLTMUONMansoTrackStruct& track1 = (*block)[dup1];
1214                 AliHLTMUONMansoTrackStruct& track2 = (*block)[dup2];
1215                 double newPt = sqrt(newTrack.fPx * newTrack.fPx + newTrack.fPy * newTrack.fPy);
1216                 double dupPt1 = sqrt(track1.fPx * track1.fPx + track1.fPy * track1.fPy);
1217                 double dupPt2 = sqrt(track2.fPx * track2.fPx + track2.fPy * track2.fPy);
1218                 
1219                 if (newPt >= dupPt1 and newPt >= dupPt2)
1220                 {
1221                         // The new track must replace both existing tracks.
1222                         track1 = newTrack;
1223                         track2 = (*block)[block->Nentries()-1];
1224                 }
1225                 else if (dupPt1 >= newPt and dupPt1 >= dupPt2)
1226                 {
1227                         // track1 has the highest pT so ignore the new track and delete track2.
1228                         track2 = (*block)[block->Nentries()-1];
1229                 }
1230                 else
1231                 {
1232                         // In this case track2 must have the highest pT so ignore the new
1233                         // track and delete track1.
1234                         track1 = (*block)[block->Nentries()-1];
1235                 }
1236                 
1237                 // Decrement the number of entries because we deleted a track.
1238                 assert(fTrackCount > 0);
1239                 assert(block->Nentries() > 0);
1240                 block->SetNumberOfEntries(block->Nentries()-1);
1241                 fTrackCount--;
1242         }
1243         else if (dup1 != block->Nentries())
1244         {
1245                 // Only one track with duplicate hits found.
1246                 // See if the new track has higher pT. If it does then replace the
1247                 // exisiting track, otherwise ignore the new track.
1248                 AliHLTMUONMansoTrackStruct& track1 = (*block)[dup1];
1249                 double newPt = sqrt(newTrack.fPx * newTrack.fPx + newTrack.fPy * newTrack.fPy);
1250                 double dupPt1 = sqrt(track1.fPx * track1.fPx + track1.fPy * track1.fPy);
1251                 if (newPt >= dupPt1)
1252                 {
1253                         track1 = newTrack;
1254                 }
1255         }
1256         else
1257         {
1258                 // No track found with duplicate hits so we can add the new track as it is.
1259                 AliHLTMUONMansoTrackStruct* track = block->AddEntry();
1260                 if (track == NULL)
1261                 {
1262                         Logging(kHLTLogError,
1263                                 "AliHLTMUONMansoTrackerFSMComponent::FoundTrack",
1264                                 "Buffer overflow",
1265                                 "We have overflowed the output buffer for Manso track data."
1266                                 " The output buffer size is only %d bytes.",
1267                                 block->BufferSize()
1268                         );
1269                         return;
1270                 }
1271         
1272                 fTrackCount++;
1273                 *track = newTrack;
1274                 DebugTrace("\tAdded new track: " << *track);
1275         }
1276 }
1277
1278
1279 void AliHLTMUONMansoTrackerFSMComponent::NoTrackFound(AliHLTMUONMansoTrackerFSM* /*tracker*/)
1280 {
1281         ///
1282         /// Inherited from AliHLTMUONMansoTrackerFSMCallback.
1283         /// Nothing special to do here.
1284         ///
1285         
1286         DebugTrace("No track found.");
1287 }
1288