]> git.uio.no Git - u/mrichter/AliRoot.git/blame - HLT/MUON/OnlineAnalysis/AliHLTMUONTriggerReconstructorComponent.cxx
fesfileid corrected.
[u/mrichter/AliRoot.git] / HLT / MUON / OnlineAnalysis / AliHLTMUONTriggerReconstructorComponent.cxx
CommitLineData
6efe69e7 1/**************************************************************************
b39b98c8 2 * This file is property of and copyright by the ALICE HLT Project *
6efe69e7 3 * All rights reserved. *
4 * *
5 * Primary Authors: *
6 * Indranil Das <indra.das@saha.ac.in> *
b39b98c8 7 * Artur Szostak <artursz@iafrica.com> *
6efe69e7 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 *
b39b98c8 14 * about the suitability of this software for any purpose. It is *
6efe69e7 15 * provided "as is" without express or implied warranty. *
16 **************************************************************************/
17
18/* $Id$ */
19
6253e09b 20///
21/// @file AliHLTMUONTriggerReconstructorComponent.cxx
22/// @author Indranil Das <indra.das@saha.ac.in>, Artur Szostak <artursz@iafrica.com>
61d982d3 23/// @date 18 Sep 2007
6253e09b 24/// @brief Implementation of the trigger DDL reconstructor component.
25///
6efe69e7 26
6efe69e7 27#include "AliHLTMUONTriggerReconstructorComponent.h"
960d54ad 28#include "AliHLTMUONTriggerReconstructor.h"
29#include "AliHLTMUONHitReconstructor.h"
30#include "AliHLTMUONConstants.h"
2b7af22a 31#include "AliHLTMUONCalculations.h"
b39b98c8 32#include "AliHLTMUONUtils.h"
227e7192 33#include "AliHLTMUONDataBlockWriter.h"
69c14678 34#include "AliRawDataHeader.h"
c95cf30c 35#include "AliCDBManager.h"
36#include "AliCDBStorage.h"
37#include "AliGeomManager.h"
38#include "AliMUONGeometryTransformer.h"
39#include "AliMUONGeometryDetElement.h"
40#include "AliMpCDB.h"
41#include "AliMpDDLStore.h"
42#include "AliMpPad.h"
43#include "AliMpSegmentation.h"
44#include "AliMpDEIterator.h"
45#include "AliMpVSegmentation.h"
46#include "AliMpDEManager.h"
47#include "AliMpLocalBoard.h"
48#include "AliMpTriggerCrate.h"
227e7192 49#include <cstdlib>
50#include <cerrno>
5d1682b9 51#include <cassert>
b39b98c8 52#include <fstream>
6efe69e7 53
c95cf30c 54
6efe69e7 55ClassImp(AliHLTMUONTriggerReconstructorComponent)
b39b98c8 56
57
227e7192 58AliHLTMUONTriggerReconstructorComponent::AliHLTMUONTriggerReconstructorComponent() :
154cba94 59 AliHLTMUONProcessor(),
227e7192 60 fTrigRec(NULL),
b39b98c8 61 fDDL(-1),
80590aa1 62 fWarnForUnexpecedBlock(false),
a3d4b6ba 63 fStopOnOverflow(false),
2b7af22a 64 fUseCrateId(true),
65 fDelaySetup(false),
66 fZmiddleSpecified(false),
67 fBLSpecified(false),
68 fLutInitialised(false)
960d54ad 69{
6253e09b 70 ///
71 /// Default constructor.
72 ///
960d54ad 73}
74
6efe69e7 75
76AliHLTMUONTriggerReconstructorComponent::~AliHLTMUONTriggerReconstructorComponent()
960d54ad 77{
6253e09b 78 ///
79 /// Default destructor.
80 ///
a6b16447 81
82 if (fTrigRec != NULL) delete fTrigRec;
960d54ad 83}
84
6efe69e7 85
86const char* AliHLTMUONTriggerReconstructorComponent::GetComponentID()
960d54ad 87{
6253e09b 88 ///
89 /// Inherited from AliHLTComponent. Returns the component ID.
90 ///
91
227e7192 92 return AliHLTMUONConstants::TriggerReconstructorId();
960d54ad 93}
94
95
96void AliHLTMUONTriggerReconstructorComponent::GetInputDataTypes( std::vector<AliHLTComponentDataType>& list)
97{
6253e09b 98 ///
99 /// Inherited from AliHLTProcessor. Returns the list of expected input data types.
100 ///
101
227e7192 102 list.clear();
668eee9f 103 list.push_back( AliHLTMUONConstants::DDLRawDataType() );
960d54ad 104}
6efe69e7 105
6efe69e7 106
107AliHLTComponentDataType AliHLTMUONTriggerReconstructorComponent::GetOutputDataType()
960d54ad 108{
6253e09b 109 ///
110 /// Inherited from AliHLTComponent. Returns the output data type.
111 ///
112
227e7192 113 return AliHLTMUONConstants::TriggerRecordsBlockDataType();
960d54ad 114}
6efe69e7 115
6efe69e7 116
227e7192 117void AliHLTMUONTriggerReconstructorComponent::GetOutputDataSize(
118 unsigned long& constBase, double& inputMultiplier
119 )
960d54ad 120{
6253e09b 121 ///
122 /// Inherited from AliHLTComponent. Returns an estimate of the expected output data size.
123 ///
124
227e7192 125 constBase = sizeof(AliHLTMUONTriggerRecordsBlockWriter::HeaderType);
c2e03d6e 126 inputMultiplier = 4;
960d54ad 127}
6efe69e7 128
129
6efe69e7 130AliHLTComponent* AliHLTMUONTriggerReconstructorComponent::Spawn()
960d54ad 131{
6253e09b 132 ///
133 /// Inherited from AliHLTComponent. Creates a new object instance.
134 ///
135
227e7192 136 return new AliHLTMUONTriggerReconstructorComponent;
960d54ad 137}
138
6efe69e7 139
960d54ad 140int AliHLTMUONTriggerReconstructorComponent::DoInit(int argc, const char** argv)
6efe69e7 141{
6253e09b 142 ///
143 /// Inherited from AliHLTComponent.
144 /// Parses the command line parameters and initialises the component.
145 ///
146
b39b98c8 147 // perform initialization.
148
149 HLTInfo("Initialising dHLT trigger reconstructor component.");
6efe69e7 150
a6b16447 151 // Make sure to cleanup fTrigRec if it is still there for some reason.
152 if (fTrigRec != NULL)
153 {
154 delete fTrigRec;
155 fTrigRec = NULL;
156 }
157
158 try
159 {
160 fTrigRec = new AliHLTMUONTriggerReconstructor();
161 }
162 catch (const std::bad_alloc&)
163 {
164 HLTError("Could not allocate more memory for the trigger reconstructor component.");
165 return -ENOMEM;
166 }
167
168 fDDL = -1;
b39b98c8 169 fWarnForUnexpecedBlock = false;
a3d4b6ba 170 fStopOnOverflow = false;
171 fUseCrateId = true;
2b7af22a 172 fDelaySetup = false;
173 fZmiddleSpecified = false;
174 fBLSpecified = false;
175 fLutInitialised = false;
960d54ad 176
b39b98c8 177 const char* lutFileName = NULL;
c95cf30c 178 const char* cdbPath = NULL;
179 Int_t run = -1;
180 bool useCDB = false;
a3d4b6ba 181 bool suppressPartialTrigs = true;
a5d4696f 182 bool tryRecover = false;
2b7af22a 183 bool useLocalId = true;
184 double zmiddle = 0;
185 double bfieldintegral = 0;
b39b98c8 186
187 for (int i = 0; i < argc; i++)
188 {
6ec6a7c1 189 if (strcmp( argv[i], "-lut" ) == 0)
b39b98c8 190 {
2b7af22a 191 if (lutFileName != NULL)
192 {
193 HLTWarning("LUT path was already specified."
194 " Will replace previous value given by -lut."
195 );
196 }
197
b39b98c8 198 if ( argc <= i+1 )
199 {
2b7af22a 200 HLTError("The lookup table filename was not specified." );
a6b16447 201 // Make sure to delete fTrigRec to avoid partial initialisation.
202 delete fTrigRec;
203 fTrigRec = NULL;
204 return -EINVAL;
b39b98c8 205 }
206
207 lutFileName = argv[i+1];
208
209 i++;
210 continue;
211 }
212
6ec6a7c1 213 if (strcmp( argv[i], "-ddl" ) == 0)
b39b98c8 214 {
2b7af22a 215 if (fDDL != -1)
216 {
217 HLTWarning("DDL number was already specified."
218 " Will replace previous value given by -ddl or -ddlid."
219 );
220 }
221
b39b98c8 222 if ( argc <= i+1 )
223 {
c95cf30c 224 HLTError("DDL number not specified. It must be in the range [21..22]" );
a6b16447 225 // Make sure to delete fTrigRec to avoid partial initialisation.
226 delete fTrigRec;
227 fTrigRec = NULL;
228 return -EINVAL;
b39b98c8 229 }
230
231 char* cpErr = NULL;
232 unsigned long num = strtoul(argv[i+1], &cpErr, 0);
233 if (cpErr == NULL or *cpErr != '\0')
234 {
5e67b742 235 HLTError("Cannot convert '%s' to a DDL Number.", argv[i+1]);
a6b16447 236 // Make sure to delete fTrigRec to avoid partial initialisation.
237 delete fTrigRec;
238 fTrigRec = NULL;
239 return -EINVAL;
b39b98c8 240 }
241 if (num < 21 or 22 < num)
242 {
a3d4b6ba 243 HLTError("The DDL number must be in the range [21..22].");
a6b16447 244 // Make sure to delete fTrigRec to avoid partial initialisation.
245 delete fTrigRec;
246 fTrigRec = NULL;
247 return -EINVAL;
b39b98c8 248 }
249 fDDL = num - 1; // Convert to DDL number in the range 0..21
250
251 i++;
252 continue;
253 }
c95cf30c 254
a3d4b6ba 255 if (strcmp( argv[i], "-ddlid" ) == 0)
256 {
2b7af22a 257 if (fDDL != -1)
258 {
259 HLTWarning("DDL number was already specified."
260 " Will replace previous value given by -ddl or -ddlid."
261 );
262 }
263
a3d4b6ba 264 if ( argc <= i+1 )
265 {
266 HLTError("DDL equipment ID number not specified. It must be in the range [2816..2817]" );
267 // Make sure to delete fTrigRec to avoid partial initialisation.
268 delete fTrigRec;
269 fTrigRec = NULL;
270 return -EINVAL;
271 }
272
273 char* cpErr = NULL;
274 unsigned long num = strtoul(argv[i+1], &cpErr, 0);
275 if (cpErr == NULL or *cpErr != '\0')
276 {
277 HLTError("Cannot convert '%s' to a DDL equipment ID Number.", argv[i+1]);
278 // Make sure to delete fTrigRec to avoid partial initialisation.
279 delete fTrigRec;
280 fTrigRec = NULL;
281 return -EINVAL;
282 }
283 fDDL = AliHLTMUONUtils::EquipIdToDDLNumber(num); // Convert to DDL number in the range 0..21
284 if (fDDL < 20 or 21 < fDDL)
285 {
286 HLTError("The DDL equipment ID number must be in the range [2816..2817].");
287 // Make sure to delete fTrigRec to avoid partial initialisation.
288 delete fTrigRec;
289 fTrigRec = NULL;
290 return -EINVAL;
291 }
292
293 i++;
294 continue;
295 }
296
c95cf30c 297 if (strcmp( argv[i], "-cdb" ) == 0)
298 {
299 useCDB = true;
300 continue;
301 }
302
303 if (strcmp( argv[i], "-cdbpath" ) == 0)
304 {
2b7af22a 305 if (cdbPath != NULL)
306 {
307 HLTWarning("CDB path was already specified."
308 " Will replace previous value given by -cdbpath."
309 );
310 }
311
c95cf30c 312 if ( argc <= i+1 )
313 {
314 HLTError("The CDB path was not specified." );
315 // Make sure to delete fTrigRec to avoid partial initialisation.
316 delete fTrigRec;
317 fTrigRec = NULL;
318 return -EINVAL;
319 }
320 cdbPath = argv[i+1];
321 useCDB = true;
322 i++;
323 continue;
324 }
325
326 if (strcmp( argv[i], "-run" ) == 0)
327 {
2b7af22a 328 if (run != -1)
329 {
330 HLTWarning("Run number was already specified."
331 " Will replace previous value given by -run."
332 );
333 }
334
c95cf30c 335 if ( argc <= i+1 )
336 {
ffc1a6f6 337 HLTError("The run number was not specified." );
c95cf30c 338 // Make sure to delete fTrigRec to avoid partial initialisation.
339 delete fTrigRec;
340 fTrigRec = NULL;
341 return -EINVAL;
342 }
343
344 char* cpErr = NULL;
345 run = Int_t( strtoul(argv[i+1], &cpErr, 0) );
346 if (cpErr == NULL or *cpErr != '\0')
347 {
348 HLTError("Cannot convert '%s' to a valid run number."
ffc1a6f6 349 " Expected a positive integer value.", argv[i+1]
c95cf30c 350 );
351 // Make sure to delete fTrigRec to avoid partial initialisation.
352 delete fTrigRec;
353 fTrigRec = NULL;
354 return -EINVAL;
355 }
b39b98c8 356
c95cf30c 357 i++;
358 continue;
359 }
2b7af22a 360
361 if (strcmp( argv[i], "-zmiddle" ) == 0)
362 {
363 if (fZmiddleSpecified)
364 {
365 HLTWarning("The Z coordinate for the middle of the dipole was already specified."
366 " Will replace previous value given by -zmiddle."
367 );
368 }
369
370 if ( argc <= i+1 )
371 {
372 HLTError("The Z coordinate for the middle of the dipole was not specified." );
373 // Make sure to delete fTrigRec to avoid partial initialisation.
374 delete fTrigRec;
375 fTrigRec = NULL;
376 return -EINVAL;
377 }
378
379 char* cpErr = NULL;
380 zmiddle = strtod(argv[i+1], &cpErr);
381 if (cpErr == NULL or *cpErr != '\0')
382 {
383 HLTError("Cannot convert '%s' to a valid floating point number.",
384 argv[i+1]
385 );
386 // Make sure to delete fTrigRec to avoid partial initialisation.
387 delete fTrigRec;
388 fTrigRec = NULL;
389 return -EINVAL;
390 }
391
392 fZmiddleSpecified = true;
393 i++;
394 continue;
395 }
396
397 if (strcmp( argv[i], "-bfieldintegral" ) == 0)
398 {
399 if (fBLSpecified)
400 {
401 HLTWarning("The magnetic field integral was already specified."
402 " Will replace previous value given by -bfieldintegral."
403 );
404 }
405
406 if ( argc <= i+1 )
407 {
408 HLTError("The magnetic field integral was not specified." );
409 // Make sure to delete fTrigRec to avoid partial initialisation.
410 delete fTrigRec;
411 fTrigRec = NULL;
412 return -EINVAL;
413 }
414
415 char* cpErr = NULL;
416 bfieldintegral = strtod(argv[i+1], &cpErr);
417 if (cpErr == NULL or *cpErr != '\0')
418 {
419 HLTError("Cannot convert '%s' to a valid floating point number.",
420 argv[i+1]
421 );
422 // Make sure to delete fTrigRec to avoid partial initialisation.
423 delete fTrigRec;
424 fTrigRec = NULL;
425 return -EINVAL;
426 }
427
428 fBLSpecified = true;
429 i++;
430 continue;
431 }
432
433 if (strcmp( argv[i], "-delaysetup" ) == 0)
434 {
435 fDelaySetup = true;
436 continue;
437 }
c95cf30c 438
6ec6a7c1 439 if (strcmp( argv[i], "-warn_on_unexpected_block" ) == 0)
b39b98c8 440 {
441 fWarnForUnexpecedBlock = true;
442 continue;
443 }
c95cf30c 444
6ec6a7c1 445 if (strcmp( argv[i], "-suppress_partial_triggers" ) == 0)
b39b98c8 446 {
a3d4b6ba 447 suppressPartialTrigs = true;
448 continue;
449 }
450
451 if (strcmp( argv[i], "-generate_partial_triggers" ) == 0)
452 {
453 suppressPartialTrigs = false;
454 continue;
455 }
456
457 if (strcmp( argv[i], "-stop_on_buffer_overflow" ) == 0)
458 {
459 fStopOnOverflow = true;
b39b98c8 460 continue;
461 }
462
a5d4696f 463 if (strcmp( argv[i], "-tryrecover" ) == 0)
464 {
465 tryRecover = true;
466 continue;
467 }
468
a3d4b6ba 469 if (strcmp( argv[i], "-dont_use_crateid" ) == 0)
470 {
471 fUseCrateId = false;
472 continue;
473 }
474
2b7af22a 475 if (strcmp( argv[i], "-dont_use_localid" ) == 0)
476 {
477 useLocalId = false;
478 continue;
479 }
480
b39b98c8 481 HLTError("Unknown option '%s'.", argv[i] );
a6b16447 482 // Make sure to delete fTrigRec to avoid partial initialisation.
483 delete fTrigRec;
484 fTrigRec = NULL;
485 return -EINVAL;
c95cf30c 486
a6b16447 487 } // for loop
b39b98c8 488
2b7af22a 489 if (fZmiddleSpecified and useCDB)
490 {
491 HLTWarning("The -cdb or -cdbpath parameter was specified, which indicates that"
492 " this component should read from the CDB, but then the -zmiddle argument"
493 " was also used. Will override the value from CDB with the command"
494 " line parameter given."
495 );
496 }
497 if (fBLSpecified and useCDB)
498 {
499 HLTWarning("The -cdb or -cdbpath parameter was specified, which indicates that"
500 " this component should read from the CDB, but then the -bfieldintegral"
501 " argument was also used. Will override the value from CDB with the"
502 " command line parameter given."
503 );
504 }
505
506 if (lutFileName != NULL and useCDB == true)
507 {
508 HLTWarning("The -cdb or -cdbpath parameter was specified, which indicates that"
509 " this component should read from the CDB, but then the -lut argument"
510 " was also used. Will ignore the -lut option and load from CDB anyway."
511 );
512 }
513
c95cf30c 514 if (lutFileName == NULL) useCDB = true;
515
2b7af22a 516 if (fDDL == -1 and not fDelaySetup)
b39b98c8 517 {
518 HLTWarning("DDL number not specified. Cannot check if incomming data is valid.");
960d54ad 519 }
960d54ad 520
ffc1a6f6 521 if (cdbPath != NULL or run != -1)
522 {
2b7af22a 523 int result = SetCDBPathAndRunNo(cdbPath, run);
524 if (result != 0)
525 {
526 // Error messages already generated in SetCDBPathAndRunNo.
527 delete fTrigRec; // Make sure to delete fTrigRec to avoid partial initialisation.
528 fTrigRec = NULL;
529 return result;
530 }
ffc1a6f6 531 }
532
2b7af22a 533 if (useCDB)
b39b98c8 534 {
2b7af22a 535 if (not fDelaySetup)
536 {
537 HLTInfo("Loading lookup table information from CDB for DDL %d (ID = %d).",
538 fDDL+1, AliHLTMUONUtils::DDLNumberToEquipId(fDDL)
539 );
540 int result = ReadLutFromCDB();
541 if (result != 0)
542 {
543 // Error messages already generated in ReadLutFromCDB.
544 delete fTrigRec; // Make sure to delete fTrigRec to avoid partial initialisation.
545 fTrigRec = NULL;
546 return result;
547 }
548 fLutInitialised = true;
549 }
960d54ad 550 }
2b7af22a 551 else
b39b98c8 552 {
c95cf30c 553 HLTInfo("Loading lookup table information from file %s.", lutFileName);
2b7af22a 554 int result = ReadLookUpTable(lutFileName);
555 if (result != 0)
556 {
557 // Error messages already generated in ReadLookUpTable.
558 delete fTrigRec; // Make sure to delete fTrigRec to avoid partial initialisation.
559 fTrigRec = NULL;
560 return result;
561 }
562 fLutInitialised = true;
c95cf30c 563 }
2b7af22a 564
565 if (fZmiddleSpecified) AliHLTMUONCalculations::Zf(zmiddle);
566 if (fBLSpecified) AliHLTMUONCalculations::QBL(bfieldintegral);
567
568 if (not fDelaySetup)
c95cf30c 569 {
2b7af22a 570 if (not fZmiddleSpecified or not fBLSpecified)
571 {
572 HLTInfo("Loading configuration parameters from CDB for DDL %d (ID = %d).",
573 fDDL+1, AliHLTMUONUtils::DDLNumberToEquipId(fDDL)
574 );
575
576 int result = ReadConfigFromCDB(not fZmiddleSpecified, not fBLSpecified);
577 if (result != 0)
578 {
579 // Error messages already generated in ReadConfigFromCDB.
580 delete fTrigRec; // Make sure to delete fTrigRec to avoid partial initialisation.
581 fTrigRec = NULL;
582 return result;
583 }
584 }
585 else
586 {
587 // Print the debug messages here since ReadConfigFromCDB does not get called,
588 // in-which the debug messages would have been printed.
589 HLTDebug("Using the following configuration parameters:");
590 HLTDebug(" Middle of dipole Z coordinate = %f cm", AliHLTMUONCalculations::Zf());
591 HLTDebug(" Magnetic field integral = %f T.m", AliHLTMUONCalculations::QBL());
592 }
960d54ad 593 }
b39b98c8 594
a3d4b6ba 595 fTrigRec->SuppressPartialTriggers(suppressPartialTrigs);
a5d4696f 596 fTrigRec->TryRecover(tryRecover);
a3d4b6ba 597 fTrigRec->UseCrateId(fUseCrateId);
2b7af22a 598 fTrigRec->UseLocalId(useLocalId);
a5d4696f 599
b39b98c8 600 return 0;
6efe69e7 601}
602
960d54ad 603
6efe69e7 604int AliHLTMUONTriggerReconstructorComponent::DoDeinit()
960d54ad 605{
6253e09b 606 ///
607 /// Inherited from AliHLTComponent. Performs a cleanup of the component.
608 ///
609
b39b98c8 610 HLTInfo("Deinitialising dHLT trigger reconstructor component.");
227e7192 611
b39b98c8 612 if (fTrigRec != NULL)
227e7192 613 {
614 delete fTrigRec;
615 fTrigRec = NULL;
616 }
617 return 0;
960d54ad 618}
6efe69e7 619
227e7192 620
960d54ad 621int AliHLTMUONTriggerReconstructorComponent::DoEvent(
622 const AliHLTComponentEventData& evtData,
b39b98c8 623 const AliHLTComponentBlockData* blocks,
624 AliHLTComponentTriggerData& /*trigData*/,
625 AliHLTUInt8_t* outputPtr,
960d54ad 626 AliHLTUInt32_t& size,
627 std::vector<AliHLTComponentBlockData>& outputBlocks
628 )
629{
6253e09b 630 ///
631 /// Inherited from AliHLTProcessor. Processes the new event data.
632 ///
633
2b7af22a 634 // Initialise the LUT and configuration parameters from CDB if we were
635 // requested to initialise only when the first event was received.
636 if (fDelaySetup)
637 {
638 // Use the specification given by the first data block if we
639 // have not been given a DDL number on the command line.
640 if (fDDL == -1)
641 {
642 if (evtData.fBlockCnt <= 0)
643 {
644 HLTError("The initialisation from CDB of the component has"
645 " been delayed to the first received event. However,"
646 " no data blocks have been found in the first event."
647 );
648 return -ENOENT;
649 }
650
651 fDDL = AliHLTMUONUtils::SpecToDDLNumber(blocks[0].fSpecification);
652
653 if (fDDL == -1)
654 {
655 HLTError("Received a data block with a specification (0x%8.8X)"
656 " indicating multiple DDL data sources, but we must only"
657 " receive data from one trigger station DDL.",
658 blocks[0].fSpecification
659 );
660 return -EPROTO;
661 }
662 }
663
664 // Check that the LUT was not already loaded in DoInit.
665 if (not fLutInitialised)
666 {
667 HLTInfo("Loading lookup table information from CDB for DDL %d (ID = %d).",
668 fDDL+1, AliHLTMUONUtils::DDLNumberToEquipId(fDDL)
669 );
670 int result = ReadLutFromCDB();
671 if (result != 0) return result;
672 fLutInitialised = true;
673 }
674
675 // Load the configuration paramters from CDB if they have not been given
676 // on the command line.
677 if (not fZmiddleSpecified or not fBLSpecified)
678 {
679 HLTInfo("Loading configuration parameters from CDB for DDL %d (ID = %d).",
680 fDDL+1, AliHLTMUONUtils::DDLNumberToEquipId(fDDL)
681 );
682 int result = ReadConfigFromCDB(not fZmiddleSpecified, not fBLSpecified);
683 if (result != 0) return result;
684 }
685
686 fDelaySetup = false;
687 }
688
227e7192 689 // Process an event
690 unsigned long totalSize = 0; // Amount of memory currently consumed in bytes.
6efe69e7 691
227e7192 692 HLTDebug("Processing event %llu with %u input data blocks.",
693 evtData.fEventID, evtData.fBlockCnt
694 );
6efe69e7 695
227e7192 696 // Loop over all input blocks in the event and run the trigger DDL
697 // reconstruction algorithm on the raw data.
698 for (AliHLTUInt32_t n = 0; n < evtData.fBlockCnt; n++)
699 {
450e0b36 700 HLTDebug("Handling block: %u, with fDataType = '%s', fPtr = %p and fSize = %u bytes.",
701 n, DataType2Text(blocks[n].fDataType).c_str(), blocks[n].fPtr, blocks[n].fSize
227e7192 702 );
703
668eee9f 704 if (blocks[n].fDataType != AliHLTMUONConstants::DDLRawDataType()
705 or not AliHLTMUONUtils::IsTriggerDDL(blocks[n].fSpecification)
706 )
227e7192 707 {
708 // Log a message indicating that we got a data block that we
709 // do not know how to handle.
5d1682b9 710 if (fWarnForUnexpecedBlock)
450e0b36 711 HLTWarning("Received a data block of a type we cannot handle: '%s', spec: 0x%X",
712 DataType2Text(blocks[n].fDataType).c_str(), blocks[n].fSpecification
5d1682b9 713 );
714 else
450e0b36 715 HLTDebug("Received a data block of a type we cannot handle: '%s', spec: 0x%X",
716 DataType2Text(blocks[n].fDataType).c_str(), blocks[n].fSpecification
5d1682b9 717 );
718
227e7192 719 continue;
720 }
721
406c5bc3 722 AliHLTInt32_t receivedDDL = AliHLTMUONUtils::SpecToDDLNumber(blocks[n].fSpecification);
a6b16447 723 if (fDDL != -1)
b39b98c8 724 {
406c5bc3 725 if (receivedDDL != fDDL)
a6b16447 726 {
a3d4b6ba 727 HLTWarning("Received raw data from DDL %d (ID = %d),"
728 " but expect data only from DDL %d (ID = %d).",
729 receivedDDL+1, AliHLTMUONUtils::DDLNumberToEquipId(receivedDDL),
730 fDDL+1, AliHLTMUONUtils::DDLNumberToEquipId(fDDL)
731 );
a6b16447 732 }
b39b98c8 733 }
734
227e7192 735 // Create a new output data block and initialise the header.
736 AliHLTMUONTriggerRecordsBlockWriter block(outputPtr+totalSize, size-totalSize);
737 if (not block.InitCommonHeader())
738 {
69c14678 739 HLTError("There is not enough space in the output buffer for the new data block."
b39b98c8 740 " We require at least %ufTrigRec->GetkDDLHeaderSize() bytes, but have %u bytes left.",
227e7192 741 sizeof(AliHLTMUONTriggerRecordsBlockWriter::HeaderType),
742 block.BufferSize()
743 );
744 break;
745 }
746
69c14678 747 AliHLTUInt32_t totalDDLSize = blocks[n].fSize;
748 if (totalDDLSize < sizeof(AliRawDataHeader))
749 {
750 HLTError("Raw data block %d is %d bytes in size and is too short to"
751 " possibly contain valid DDL raw data. We expect it to have"
752 " at least %d bytes for the commond data header.",
753 n, totalDDLSize, sizeof(AliRawDataHeader)
754 );
755 continue;
756 }
649ab027 757 AliRawDataHeader* header = reinterpret_cast<AliRawDataHeader*>(
758 reinterpret_cast<char*>(blocks[n].fPtr) + blocks[n].fOffset
759 );
69c14678 760 AliHLTUInt32_t payloadSize = totalDDLSize - sizeof(AliRawDataHeader);
a5d4696f 761 AliHLTUInt8_t* buffer = reinterpret_cast<AliHLTUInt8_t*>(header + 1);
227e7192 762 AliHLTUInt32_t nofTrigRec = block.MaxNumberOfEntries();
a5d4696f 763
764 // Decode if this is a scalar event or not.
765 bool scalarEvent = ((header->GetL1TriggerMessage() & 0x1) == 0x1);
766
406c5bc3 767 // Remember: the following does NOT change the mapping!
768 // It is just to generate unique trigger record IDs.
769 fTrigRec->SetDDL(receivedDDL);
770
80590aa1 771 bool runOk = fTrigRec->Run(
a5d4696f 772 buffer, payloadSize, scalarEvent,
a3d4b6ba 773 block.GetArray(), nofTrigRec
80590aa1 774 );
775 if (not runOk)
227e7192 776 {
154cba94 777 HLTError("Error while processing the trigger DDL reconstruction algorithm.");
a3d4b6ba 778 if (not fTrigRec->OverflowedOutputBuffer()
779 or (fTrigRec->OverflowedOutputBuffer() and fStopOnOverflow)
780 )
781 {
782 size = totalSize; // Must tell the framework how much buffer space was used.
783 return -EIO;
784 }
227e7192 785 }
786
787 // nofTrigRec should now contain the number of triggers actually found
788 // and filled into the output data block, so we can set this number.
789 assert( nofTrigRec <= block.MaxNumberOfEntries() );
790 block.SetNumberOfEntries(nofTrigRec);
791
792 HLTDebug("Number of trigger records found is %d", nofTrigRec);
793
794 // Fill a block data structure for our output block.
795 AliHLTComponentBlockData bd;
796 FillBlockData(bd);
797 bd.fPtr = outputPtr;
798 // This block's start (offset) is after all other blocks written so far.
799 bd.fOffset = totalSize;
800 bd.fSize = block.BytesUsed();
801 bd.fDataType = AliHLTMUONConstants::TriggerRecordsBlockDataType();
802 bd.fSpecification = blocks[n].fSpecification;
803 outputBlocks.push_back(bd);
804
805 HLTDebug("Created a new output data block at fPtr = %p,"
806 " with fOffset = %u (0x%.X) and fSize = %u bytes.",
807 bd.fPtr, bd.fOffset, bd.fOffset, bd.fSize
808 );
809
810 // Increase the total amount of data written so far to our output memory.
811 totalSize += block.BytesUsed();
812 }
960d54ad 813
227e7192 814 // Finally we set the total size of output memory we consumed.
815 size = totalSize;
816 return 0;
960d54ad 817}
818
6efe69e7 819
2b7af22a 820int AliHLTMUONTriggerReconstructorComponent::Reconfigure(
821 const char* cdbEntry, const char* componentId
822 )
823{
824 /// Inherited from AliHLTComponent. This method will reload CDB configuration
825 /// entries for this component from the CDB.
826 /// \param cdbEntry If this is NULL then it is assumed that all CDB entries should
827 /// be reloaded. Otherwise a particular value for 'cdbEntry' will trigger
828 /// reloading of the LUT if the path contains 'MUON/', but the other
829 /// configuration parameters will be loaded if 'cdbEntry' contains
830 /// "HLT/ConfigMUON/TriggerReconstructor".
831 /// \param componentId The name of the component in the current chain.
832
833 bool startsWithMUON = TString(cdbEntry).Index("MUON/", 5, 0, TString::kExact) == 0;
834 bool givenConfigPath = strcmp(cdbEntry, AliHLTMUONConstants::TriggerReconstructorCDBPath()) == 0;
835
836 if (cdbEntry == NULL or startsWithMUON or givenConfigPath)
837 {
838 HLTInfo("Reading new configuration entries from CDB for component '%s'.", componentId);
839 }
840
841 if (cdbEntry == NULL or startsWithMUON)
842 {
843 int result = ReadLutFromCDB();
844 if (result != 0) return result;
845 }
846
847 if (cdbEntry == NULL or givenConfigPath)
848 {
849 int result = ReadConfigFromCDB();
850 if (result != 0) return result;
851 }
852
853 return 0;
854}
855
856
857int AliHLTMUONTriggerReconstructorComponent::ReadPreprocessorValues(const char* modules)
858{
859 /// Inherited from AliHLTComponent.
860 /// Updates the configuration of this component if either HLT or MUON have
861 /// been specified in the 'modules' list.
862
863 TString mods = modules;
864 if (mods.Contains("ALL") or (mods.Contains("HLT") and mods.Contains("MUON")))
865 {
866 return Reconfigure(NULL, GetComponentID());
867 }
868 if (mods.Contains("HLT"))
869 {
870 return Reconfigure(AliHLTMUONConstants::TriggerReconstructorCDBPath(), GetComponentID());
871 }
872 if (mods.Contains("MUON"))
873 {
874 return Reconfigure("MUON/*", GetComponentID());
875 }
876 return 0;
877}
878
879
c95cf30c 880int AliHLTMUONTriggerReconstructorComponent::ReadLookUpTable(const char* lutpath)
6efe69e7 881{
6253e09b 882 ///
883 /// Read in the lookup table from file.
884 ///
885
b39b98c8 886 assert(fTrigRec != NULL);
6efe69e7 887
b39b98c8 888 fstream file;
889 file.open(lutpath, fstream::binary | fstream::in);
890 if (not file)
891 {
892 HLTError("Could not open file: %s", lutpath);
c95cf30c 893 return -ENOENT;
b39b98c8 894 }
895
896 file.read(reinterpret_cast<char*>(fTrigRec->LookupTableBuffer()), fTrigRec->LookupTableSize());
897 if (file.eof())
898 {
899 HLTError("The file %s was too short to contain a valid lookup table for this component.", lutpath);
900 file.close();
c95cf30c 901 return -EIO;
b39b98c8 902 }
c95cf30c 903 if (file.fail())
b39b98c8 904 {
905 HLTError("Could not read from file: %s", lutpath);
906 file.close();
c95cf30c 907 return -EIO;
908 }
909
910 file.close();
911 return 0;
912}
913
914
2b7af22a 915int AliHLTMUONTriggerReconstructorComponent::ReadConfigFromCDB(
916 bool setZmiddle, bool setBL
917 )
918{
919 /// Reads this component's configuration parameters from the CDB.
920 /// These include the middle of the dipole Z coordinate (zmiddle) and the
921 /// integrated magnetic field of the dipole.
922 /// \param setZmiddle Indicates if the zmiddle parameter should be set
923 /// (default true).
924 /// \param setBL Indicates if the integrated magnetic field parameter should
925 /// be set (default true).
926 /// \return 0 if no errors occured and negative error code compatible with
927 /// the HLT framework on errors.
928
929 const char* pathToEntry = AliHLTMUONConstants::TriggerReconstructorCDBPath();
930
931 TMap* map = NULL;
932 int result = FetchTMapFromCDB(pathToEntry, map);
933 if (result != 0) return result;
934
935 Double_t value = 0;
936 if (setZmiddle)
937 {
938 result = GetFloatFromTMap(map, "zmiddle", value, pathToEntry, "dipole middle Z coordinate");
939 if (result != 0) return result;
940 AliHLTMUONCalculations::Zf(value);
941 }
942
943 if (setBL)
944 {
945 result = GetFloatFromTMap(map, "bfieldintegral", value, pathToEntry, "integrated magnetic field");
946 if (result != 0) return result;
947 AliHLTMUONCalculations::QBL(value);
948 }
949
950 HLTDebug("Using the following configuration parameters:");
951 HLTDebug(" Middle of dipole Z coordinate = %f cm", AliHLTMUONCalculations::Zf());
952 HLTDebug(" Magnetic field integral = %f T.m", AliHLTMUONCalculations::QBL());
953
954 return 0;
955}
956
957
ffc1a6f6 958int AliHLTMUONTriggerReconstructorComponent::ReadLutFromCDB()
c95cf30c 959{
960 /// Loads the lookup table containing channel and geometrical position
961 /// information about trigger strips from CDB.
ffc1a6f6 962 ///
963 /// \note To override the default CDB path and / or run number the
964 /// SetCDBPathAndRunNo(cdbPath, run) method should be called before this
965 /// method.
966 ///
c95cf30c 967 /// \return 0 on success and non zero codes for errors.
968
969 if (fDDL == -1)
970 {
971 HLTError("No DDL number specified for which to load LUT data from CDB.");
972 return -EINVAL;
973 }
974
ffc1a6f6 975 int result = FetchMappingStores();
dba14d7d 976 // Error message already generated in FetchMappingStores.
977 if (result != 0) return result;
c95cf30c 978 AliMpDDLStore* ddlStore = AliMpDDLStore::Instance();
dba14d7d 979
c95cf30c 980 AliMpSegmentation* segmentation = AliMpSegmentation::Instance();
981 if (segmentation == NULL)
982 {
983 HLTError("Could not find segmentation mapping (AliMpSegmentation) instance.");
984 return -EIO;
985 }
986
61d982d3 987 // Only load geometry if not already loaded.
988 if (AliGeomManager::GetGeometry() == NULL)
989 {
990 AliGeomManager::LoadGeometry();
991 }
c95cf30c 992 AliMUONGeometryTransformer transformer;
993 if (not transformer.LoadGeometryData())
994 {
995 HLTError("Could not load geometry into transformer.");
996 return -ENOENT;
997 }
998
999 AliHLTMUONTriggerRecoLookupTable* lookupTable = fTrigRec->LookupTableBuffer();
1000
a3d4b6ba 1001 for (Int_t i = 0; i < 16; i++)
c95cf30c 1002 for (Int_t j = 0; j < 16; j++)
1003 for (Int_t k = 0; k < 4; k++)
1004 for (Int_t n = 0; n < 2; n++)
1005 for (Int_t m = 0; m < 16; m++)
1006 {
a090ff22 1007 lookupTable->fRow[i][j][k][n][m].fIdFlags = 0x0;
c95cf30c 1008 lookupTable->fRow[i][j][k][n][m].fX = 0;
1009 lookupTable->fRow[i][j][k][n][m].fY = 0;
1010 lookupTable->fRow[i][j][k][n][m].fZ = 0;
1011 }
1012
1013 AliMpDEIterator detElemIter;
1014 for (Int_t iReg = 0; iReg < 8; iReg++)
1015 {
1016 AliMpTriggerCrate* crate = ddlStore->GetTriggerCrate(fDDL, iReg);
1017 if (crate == NULL)
1018 {
a3d4b6ba 1019 HLTError("Could not get crate mapping for regional header = %d"
1020 " and DDL %d (ID = %d).",
1021 iReg, fDDL+1, AliHLTMUONUtils::DDLNumberToEquipId(fDDL)
1022 );
1023 continue;
1024 }
1025 // Depending on the value of fUseCrateId, use either the crate ID as would
1026 // be found in the regional header structures or the sequencial index number
1027 // of the structure.
1028 UInt_t crateId = (fUseCrateId ? crate->GetId() : iReg);
1029 if (crateId >= 16)
1030 {
1031 HLTError("The crate ID number (%d) for regional header = %d and"
1032 " DDL %d (ID = %d) is too big. It should be in the range [0..15]",
1033 crateId, iReg, fDDL+1, AliHLTMUONUtils::DDLNumberToEquipId(fDDL)
1034 );
c95cf30c 1035 continue;
1036 }
1037
1038 for (Int_t iLocBoard = 0; iLocBoard < 16; iLocBoard++)
1039 {
1040 Int_t boardId = crate->GetLocalBoardId(iLocBoard);
1041 if (boardId == 0) continue;
1042
1043 AliMpLocalBoard* localBoard = ddlStore->GetLocalBoard(boardId);
1044 if (localBoard == NULL)
1045 {
a3d4b6ba 1046 HLTError("Could not get local board: %d.", boardId);
c95cf30c 1047 continue;
1048 }
1049
1050 // skip copy cards
1051 if (! localBoard->IsNotified()) continue;
1052
1053 for (Int_t iChamber = 0; iChamber < 4; iChamber++)
1054 {
1055 Int_t detElemId = ddlStore->GetDEfromLocalBoard(boardId, iChamber);
1056
a090ff22 1057 AliHLTUInt32_t idflags = AliHLTMUONUtils::PackRecHitFlags(iChamber+10, detElemId);
1058
c95cf30c 1059 const AliMUONGeometryDetElement* detElemTransform = transformer.GetDetElement(detElemId);
1060 if (detElemTransform == NULL)
1061 {
a3d4b6ba 1062 HLTError("Got NULL pointer for geometry transformer"
1063 " for detection element ID = %d.",
1064 detElemId
1065 );
c95cf30c 1066 continue;
1067 }
1068
1069 for (Int_t iCathode = 0; iCathode <= 1; iCathode++)
1070 {
1071 const AliMpVSegmentation* seg = segmentation->GetMpSegmentation(
1072 detElemId, AliMp::GetCathodType(iCathode)
1073 );
1074
1075 for (Int_t bitxy = 0; bitxy < 16; bitxy++)
1076 {
1077 Int_t offset = 0;
1078 if (iCathode && localBoard->GetSwitch(6)) offset = -8;
1079
1080 AliMpPad pad = seg->PadByLocation(AliMpIntPair(boardId, bitxy+offset), kFALSE);
1081
1082 if (! pad.IsValid())
1083 {
1084 // There is no pad associated with the given local board and bit pattern.
1085 continue;
1086 }
1087
1088 // Get the global coodinates of the pad.
1089 Float_t lx = pad.Position().X();
1090 Float_t ly = pad.Position().Y();
1091 Float_t gx, gy, gz;
1092 detElemTransform->Local2Global(lx, ly, 0, gx, gy, gz);
1093
1094 // Fill the LUT
a3d4b6ba 1095 lookupTable->fRow[crateId][iLocBoard][iChamber][iCathode][bitxy].fIdFlags = idflags;
1096 lookupTable->fRow[crateId][iLocBoard][iChamber][iCathode][bitxy].fX = gx;
1097 lookupTable->fRow[crateId][iLocBoard][iChamber][iCathode][bitxy].fY = gy;
1098 lookupTable->fRow[crateId][iLocBoard][iChamber][iCathode][bitxy].fZ = gz;
c95cf30c 1099 }
1100 }
1101 }
1102 }
1103 }
61d982d3 1104
c95cf30c 1105 return 0;
1106}
1107
1108
1109bool AliHLTMUONTriggerReconstructorComponent::GenerateLookupTable(
1110 AliHLTInt32_t ddl, const char* filename,
2b7af22a 1111 const char* cdbPath, Int_t run, bool useCrateId
c95cf30c 1112 )
1113{
1114 /// Generates a binary file containing the lookup table (LUT) from the
1115 /// CDB, which can be used for the trigger reconstructor component later.
1116 /// @param ddl Must be the DDL for which to generate the DDL,
1117 /// in the range [20..21].
1118 /// @param filename The name of the LUT file to generate.
1119 /// @param cdbPath The CDB path to use.
1120 /// @param run The run number to use for the CDB.
2b7af22a 1121 /// @param useCrateId Indicates that the crate ID should be used rather
1122 /// than a sequencial number.
c95cf30c 1123 /// @return True if the generation of the LUT file succeeded.
1124
1125 AliHLTMUONTriggerReconstructorComponent comp;
1126
1127 if (ddl < 20 or 21 < ddl)
1128 {
1129 std::cerr << "ERROR: the DDL number must be in the range [20..21]." << std::endl;
1130 return false;
1131 }
1132
1133 char ddlNum[32];
1134 char runNum[32];
1135 sprintf(ddlNum, "%d", ddl+1);
1136 sprintf(runNum, "%d", run);
2b7af22a 1137 int argc = 7;
1138 const char* argv[8] = {"-ddl", ddlNum, "-cdbpath", cdbPath, "-run", runNum, "-dont_use_crateid", NULL};
1139 if (useCrateId)
1140 {
1141 argv[6] = NULL;
1142 argc--;
1143 }
1144 int result = comp.DoInit(argc, argv);
c95cf30c 1145 if (result != 0)
1146 {
1147 // Error message already generated in DoInit.
b39b98c8 1148 return false;
1149 }
1150
c95cf30c 1151 std::fstream file(filename, std::ios::out);
1152 if (not file)
1153 {
1154 std::cerr << "ERROR: could not open file: " << filename << std::endl;
1155 return false;
1156 }
1157
1158 file.write(
1159 reinterpret_cast<char*>(comp.fTrigRec->LookupTableBuffer()),
1160 comp.fTrigRec->LookupTableSize()
1161 );
1162 if (not file)
1163 {
a3d4b6ba 1164 std::cerr << "ERROR: There was a problem writing to the file: " << filename << std::endl;
c95cf30c 1165 return false;
1166 }
b39b98c8 1167 file.close();
c95cf30c 1168
1169 comp.DoDeinit();
1170
b39b98c8 1171 return true;
960d54ad 1172}