1 /**************************************************************************
2 * This file is property of and copyright by the ALICE HLT Project *
3 * All rights reserved. *
6 * Indranil Das <indra.das@saha.ac.in> *
7 * Artur Szostak <artursz@iafrica.com> *
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 **************************************************************************/
21 /// @file AliHLTMUONTriggerReconstructorComponent.cxx
22 /// @author Indranil Das <indra.das@saha.ac.in>, Artur Szostak <artursz@iafrica.com>
24 /// @brief Implementation of the trigger DDL reconstructor component.
27 #include "AliHLTMUONTriggerReconstructorComponent.h"
28 #include "AliHLTMUONTriggerReconstructor.h"
29 #include "AliHLTMUONHitReconstructor.h"
30 #include "AliHLTMUONConstants.h"
31 #include "AliHLTMUONUtils.h"
32 #include "AliHLTMUONDataBlockWriter.h"
33 #include "AliRawDataHeader.h"
34 #include "AliCDBManager.h"
35 #include "AliCDBStorage.h"
36 #include "AliGeomManager.h"
37 #include "AliMUONGeometryTransformer.h"
38 #include "AliMUONGeometryDetElement.h"
40 #include "AliMpDDLStore.h"
42 #include "AliMpSegmentation.h"
43 #include "AliMpDEIterator.h"
44 #include "AliMpVSegmentation.h"
45 #include "AliMpDEManager.h"
46 #include "AliMpLocalBoard.h"
47 #include "AliMpTriggerCrate.h"
54 ClassImp(AliHLTMUONTriggerReconstructorComponent)
57 AliHLTMUONTriggerReconstructorComponent::AliHLTMUONTriggerReconstructorComponent() :
58 AliHLTMUONProcessor(),
61 fWarnForUnexpecedBlock(false),
62 fSuppressPartialTrigs(false)
65 /// Default constructor.
70 AliHLTMUONTriggerReconstructorComponent::~AliHLTMUONTriggerReconstructorComponent()
73 /// Default destructor.
76 if (fTrigRec != NULL) delete fTrigRec;
80 const char* AliHLTMUONTriggerReconstructorComponent::GetComponentID()
83 /// Inherited from AliHLTComponent. Returns the component ID.
86 return AliHLTMUONConstants::TriggerReconstructorId();
90 void AliHLTMUONTriggerReconstructorComponent::GetInputDataTypes( std::vector<AliHLTComponentDataType>& list)
93 /// Inherited from AliHLTProcessor. Returns the list of expected input data types.
97 list.push_back( AliHLTMUONConstants::DDLRawDataType() );
101 AliHLTComponentDataType AliHLTMUONTriggerReconstructorComponent::GetOutputDataType()
104 /// Inherited from AliHLTComponent. Returns the output data type.
107 return AliHLTMUONConstants::TriggerRecordsBlockDataType();
111 void AliHLTMUONTriggerReconstructorComponent::GetOutputDataSize(
112 unsigned long& constBase, double& inputMultiplier
116 /// Inherited from AliHLTComponent. Returns an estimate of the expected output data size.
119 constBase = sizeof(AliHLTMUONTriggerRecordsBlockWriter::HeaderType);
124 AliHLTComponent* AliHLTMUONTriggerReconstructorComponent::Spawn()
127 /// Inherited from AliHLTComponent. Creates a new object instance.
130 return new AliHLTMUONTriggerReconstructorComponent;
134 int AliHLTMUONTriggerReconstructorComponent::DoInit(int argc, const char** argv)
137 /// Inherited from AliHLTComponent.
138 /// Parses the command line parameters and initialises the component.
141 // perform initialization.
143 HLTInfo("Initialising dHLT trigger reconstructor component.");
145 // Make sure to cleanup fTrigRec if it is still there for some reason.
146 if (fTrigRec != NULL)
154 fTrigRec = new AliHLTMUONTriggerReconstructor();
156 catch (const std::bad_alloc&)
158 HLTError("Could not allocate more memory for the trigger reconstructor component.");
163 fWarnForUnexpecedBlock = false;
164 fSuppressPartialTrigs = false;
166 const char* lutFileName = NULL;
167 const char* cdbPath = NULL;
170 bool tryRecover = false;
172 for (int i = 0; i < argc; i++)
174 if (strcmp( argv[i], "-lut" ) == 0)
178 HLTError("LookupTable filename not specified." );
179 // Make sure to delete fTrigRec to avoid partial initialisation.
185 lutFileName = argv[i+1];
191 if (strcmp( argv[i], "-ddl" ) == 0)
195 HLTError("DDL number not specified. It must be in the range [21..22]" );
196 // Make sure to delete fTrigRec to avoid partial initialisation.
203 unsigned long num = strtoul(argv[i+1], &cpErr, 0);
204 if (cpErr == NULL or *cpErr != '\0')
206 HLTError("Cannot convert '%s' to a DDL Number.", argv[i+1]);
207 // Make sure to delete fTrigRec to avoid partial initialisation.
212 if (num < 21 or 22 < num)
214 HLTError("The DDL ID number must be in the range [21..22].");
215 // Make sure to delete fTrigRec to avoid partial initialisation.
220 fDDL = num - 1; // Convert to DDL number in the range 0..21
226 if (strcmp( argv[i], "-cdb" ) == 0)
232 if (strcmp( argv[i], "-cdbpath" ) == 0)
236 HLTError("The CDB path was not specified." );
237 // Make sure to delete fTrigRec to avoid partial initialisation.
248 if (strcmp( argv[i], "-run" ) == 0)
252 HLTError("The RUN number was not specified." );
253 // Make sure to delete fTrigRec to avoid partial initialisation.
260 run = Int_t( strtoul(argv[i+1], &cpErr, 0) );
261 if (cpErr == NULL or *cpErr != '\0')
263 HLTError("Cannot convert '%s' to a valid run number."
264 " Expected an integer value.", argv[i+1]
266 // Make sure to delete fTrigRec to avoid partial initialisation.
276 if (strcmp( argv[i], "-warn_on_unexpected_block" ) == 0)
278 fWarnForUnexpecedBlock = true;
282 if (strcmp( argv[i], "-suppress_partial_triggers" ) == 0)
284 fSuppressPartialTrigs = true;
288 if (strcmp( argv[i], "-tryrecover" ) == 0)
294 HLTError("Unknown option '%s'.", argv[i] );
295 // Make sure to delete fTrigRec to avoid partial initialisation.
302 if (lutFileName == NULL) useCDB = true;
306 HLTWarning("DDL number not specified. Cannot check if incomming data is valid.");
312 HLTInfo("Loading lookup table information from CDB for DDL %d.", fDDL+1);
314 HLTWarning("DDL number not specified. The lookup table loaded from CDB will be empty!");
315 result = ReadCDB(cdbPath, run);
319 HLTInfo("Loading lookup table information from file %s.", lutFileName);
320 result = ReadLookUpTable(lutFileName);
324 // Error messages already generated in ReadCDB or ReadLookUpTable.
326 // Make sure to delete fTrigRec to avoid partial initialisation.
332 fTrigRec->TryRecover(tryRecover);
338 int AliHLTMUONTriggerReconstructorComponent::DoDeinit()
341 /// Inherited from AliHLTComponent. Performs a cleanup of the component.
344 HLTInfo("Deinitialising dHLT trigger reconstructor component.");
346 if (fTrigRec != NULL)
355 int AliHLTMUONTriggerReconstructorComponent::DoEvent(
356 const AliHLTComponentEventData& evtData,
357 const AliHLTComponentBlockData* blocks,
358 AliHLTComponentTriggerData& /*trigData*/,
359 AliHLTUInt8_t* outputPtr,
360 AliHLTUInt32_t& size,
361 std::vector<AliHLTComponentBlockData>& outputBlocks
365 /// Inherited from AliHLTProcessor. Processes the new event data.
369 unsigned long totalSize = 0; // Amount of memory currently consumed in bytes.
371 HLTDebug("Processing event %llu with %u input data blocks.",
372 evtData.fEventID, evtData.fBlockCnt
375 // Loop over all input blocks in the event and run the trigger DDL
376 // reconstruction algorithm on the raw data.
377 for (AliHLTUInt32_t n = 0; n < evtData.fBlockCnt; n++)
379 HLTDebug("Handling block: %u, with fDataType = '%s', fPtr = %p and fSize = %u bytes.",
380 n, DataType2Text(blocks[n].fDataType).c_str(), blocks[n].fPtr, blocks[n].fSize
383 if (blocks[n].fDataType != AliHLTMUONConstants::DDLRawDataType()
384 or not AliHLTMUONUtils::IsTriggerDDL(blocks[n].fSpecification)
387 // Log a message indicating that we got a data block that we
388 // do not know how to handle.
389 if (fWarnForUnexpecedBlock)
390 HLTWarning("Received a data block of a type we cannot handle: '%s', spec: 0x%X",
391 DataType2Text(blocks[n].fDataType).c_str(), blocks[n].fSpecification
394 HLTDebug("Received a data block of a type we cannot handle: '%s', spec: 0x%X",
395 DataType2Text(blocks[n].fDataType).c_str(), blocks[n].fSpecification
403 if (AliHLTMUONUtils::SpecToDDLNumber(blocks[n].fSpecification) != fDDL)
405 HLTWarning("Received raw data from an unexpected DDL.");
409 // Create a new output data block and initialise the header.
410 AliHLTMUONTriggerRecordsBlockWriter block(outputPtr+totalSize, size-totalSize);
411 if (not block.InitCommonHeader())
413 HLTError("There is not enough space in the output buffer for the new data block."
414 " We require at least %ufTrigRec->GetkDDLHeaderSize() bytes, but have %u bytes left.",
415 sizeof(AliHLTMUONTriggerRecordsBlockWriter::HeaderType),
421 AliHLTUInt32_t totalDDLSize = blocks[n].fSize;
422 if (totalDDLSize < sizeof(AliRawDataHeader))
424 HLTError("Raw data block %d is %d bytes in size and is too short to"
425 " possibly contain valid DDL raw data. We expect it to have"
426 " at least %d bytes for the commond data header.",
427 n, totalDDLSize, sizeof(AliRawDataHeader)
431 AliRawDataHeader* header = reinterpret_cast<AliRawDataHeader*>(blocks[n].fPtr);
432 AliHLTUInt32_t payloadSize = totalDDLSize - sizeof(AliRawDataHeader);
433 AliHLTUInt8_t* buffer = reinterpret_cast<AliHLTUInt8_t*>(header + 1);
434 AliHLTUInt32_t nofTrigRec = block.MaxNumberOfEntries();
436 // Decode if this is a scalar event or not.
437 bool scalarEvent = ((header->GetL1TriggerMessage() & 0x1) == 0x1);
439 bool runOk = fTrigRec->Run(
440 buffer, payloadSize, scalarEvent,
441 block.GetArray(), nofTrigRec,
442 fSuppressPartialTrigs
446 HLTError("Error while processing the trigger DDL reconstruction algorithm.");
447 size = totalSize; // Must tell the framework how much buffer space was used.
451 // nofTrigRec should now contain the number of triggers actually found
452 // and filled into the output data block, so we can set this number.
453 assert( nofTrigRec <= block.MaxNumberOfEntries() );
454 block.SetNumberOfEntries(nofTrigRec);
456 HLTDebug("Number of trigger records found is %d", nofTrigRec);
458 // Fill a block data structure for our output block.
459 AliHLTComponentBlockData bd;
462 // This block's start (offset) is after all other blocks written so far.
463 bd.fOffset = totalSize;
464 bd.fSize = block.BytesUsed();
465 bd.fDataType = AliHLTMUONConstants::TriggerRecordsBlockDataType();
466 bd.fSpecification = blocks[n].fSpecification;
467 outputBlocks.push_back(bd);
469 HLTDebug("Created a new output data block at fPtr = %p,"
470 " with fOffset = %u (0x%.X) and fSize = %u bytes.",
471 bd.fPtr, bd.fOffset, bd.fOffset, bd.fSize
474 // Increase the total amount of data written so far to our output memory.
475 totalSize += block.BytesUsed();
478 // Finally we set the total size of output memory we consumed.
484 int AliHLTMUONTriggerReconstructorComponent::ReadLookUpTable(const char* lutpath)
487 /// Read in the lookup table from file.
490 assert(fTrigRec != NULL);
493 file.open(lutpath, fstream::binary | fstream::in);
496 HLTError("Could not open file: %s", lutpath);
500 file.read(reinterpret_cast<char*>(fTrigRec->LookupTableBuffer()), fTrigRec->LookupTableSize());
503 HLTError("The file %s was too short to contain a valid lookup table for this component.", lutpath);
509 HLTError("Could not read from file: %s", lutpath);
519 int AliHLTMUONTriggerReconstructorComponent::ReadCDB(const char* cdbPath, Int_t run)
521 /// Loads the lookup table containing channel and geometrical position
522 /// information about trigger strips from CDB.
523 /// \param cdbPath This specifies the CDB path to use to load from.
524 /// Can be set to NULL meaning the default storage is used.
525 /// \param run Specifies the run number to use. If set to -1 then the
526 /// default / current run number set for the CDB is used.
527 /// \return 0 on success and non zero codes for errors.
531 HLTError("No DDL number specified for which to load LUT data from CDB.");
535 int result = FetchMappingStores(cdbPath, run);
536 // Error message already generated in FetchMappingStores.
537 if (result != 0) return result;
538 AliMpDDLStore* ddlStore = AliMpDDLStore::Instance();
540 AliMpSegmentation* segmentation = AliMpSegmentation::Instance();
541 if (segmentation == NULL)
543 HLTError("Could not find segmentation mapping (AliMpSegmentation) instance.");
547 // Only load geometry if not already loaded.
548 if (AliGeomManager::GetGeometry() == NULL)
550 AliGeomManager::LoadGeometry();
552 AliMUONGeometryTransformer transformer;
553 if (not transformer.LoadGeometryData())
555 HLTError("Could not load geometry into transformer.");
559 AliHLTMUONTriggerRecoLookupTable* lookupTable = fTrigRec->LookupTableBuffer();
561 for (Int_t i = 0; i < 8; i++)
562 for (Int_t j = 0; j < 16; j++)
563 for (Int_t k = 0; k < 4; k++)
564 for (Int_t n = 0; n < 2; n++)
565 for (Int_t m = 0; m < 16; m++)
567 lookupTable->fRow[i][j][k][n][m].fX = 0;
568 lookupTable->fRow[i][j][k][n][m].fY = 0;
569 lookupTable->fRow[i][j][k][n][m].fZ = 0;
572 AliMpDEIterator detElemIter;
573 for (Int_t iReg = 0; iReg < 8; iReg++)
575 AliMpTriggerCrate* crate = ddlStore->GetTriggerCrate(fDDL, iReg);
578 cerr << "ERROR: Could not get crate for regional header = " << iReg
579 << ", and DDL ID = " << fDDL << endl;
583 for (Int_t iLocBoard = 0; iLocBoard < 16; iLocBoard++)
585 Int_t boardId = crate->GetLocalBoardId(iLocBoard);
586 if (boardId == 0) continue;
588 AliMpLocalBoard* localBoard = ddlStore->GetLocalBoard(boardId);
589 if (localBoard == NULL)
591 cerr << "ERROR: Could not get loacl board: " << boardId << endl;
596 if (! localBoard->IsNotified()) continue;
598 for (Int_t iChamber = 0; iChamber < 4; iChamber++)
600 Int_t detElemId = ddlStore->GetDEfromLocalBoard(boardId, iChamber);
602 const AliMUONGeometryDetElement* detElemTransform = transformer.GetDetElement(detElemId);
603 if (detElemTransform == NULL)
605 cerr << "ERROR: Got NULL pointer for geometry transformer for detection element ID = "
606 << detElemId << endl;
610 for (Int_t iCathode = 0; iCathode <= 1; iCathode++)
612 const AliMpVSegmentation* seg = segmentation->GetMpSegmentation(
613 detElemId, AliMp::GetCathodType(iCathode)
616 for (Int_t bitxy = 0; bitxy < 16; bitxy++)
619 if (iCathode && localBoard->GetSwitch(6)) offset = -8;
621 AliMpPad pad = seg->PadByLocation(AliMpIntPair(boardId, bitxy+offset), kFALSE);
625 // There is no pad associated with the given local board and bit pattern.
629 // Get the global coodinates of the pad.
630 Float_t lx = pad.Position().X();
631 Float_t ly = pad.Position().Y();
633 detElemTransform->Local2Global(lx, ly, 0, gx, gy, gz);
636 lookupTable->fRow[iReg][iLocBoard][iChamber][iCathode][bitxy].fX = gx;
637 lookupTable->fRow[iReg][iLocBoard][iChamber][iCathode][bitxy].fY = gy;
638 lookupTable->fRow[iReg][iLocBoard][iChamber][iCathode][bitxy].fZ = gz;
649 bool AliHLTMUONTriggerReconstructorComponent::GenerateLookupTable(
650 AliHLTInt32_t ddl, const char* filename,
651 const char* cdbPath, Int_t run
654 /// Generates a binary file containing the lookup table (LUT) from the
655 /// CDB, which can be used for the trigger reconstructor component later.
656 /// @param ddl Must be the DDL for which to generate the DDL,
657 /// in the range [20..21].
658 /// @param filename The name of the LUT file to generate.
659 /// @param cdbPath The CDB path to use.
660 /// @param run The run number to use for the CDB.
661 /// @return True if the generation of the LUT file succeeded.
663 AliHLTMUONTriggerReconstructorComponent comp;
665 if (ddl < 20 or 21 < ddl)
667 std::cerr << "ERROR: the DDL number must be in the range [20..21]." << std::endl;
673 sprintf(ddlNum, "%d", ddl+1);
674 sprintf(runNum, "%d", run);
675 const char* argv[7] = {"-ddl", ddlNum, "-cdbpath", cdbPath, "-run", runNum, NULL};
676 int result = comp.DoInit(6, argv);
679 // Error message already generated in DoInit.
683 std::fstream file(filename, std::ios::out);
686 std::cerr << "ERROR: could not open file: " << filename << std::endl;
691 reinterpret_cast<char*>(comp.fTrigRec->LookupTableBuffer()),
692 comp.fTrigRec->LookupTableSize()
696 cerr << "ERROR: There was a problem writing to the file: " << filename << endl;