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 fStopOnOverflow(false),
66 /// Default constructor.
71 AliHLTMUONTriggerReconstructorComponent::~AliHLTMUONTriggerReconstructorComponent()
74 /// Default destructor.
77 if (fTrigRec != NULL) delete fTrigRec;
81 const char* AliHLTMUONTriggerReconstructorComponent::GetComponentID()
84 /// Inherited from AliHLTComponent. Returns the component ID.
87 return AliHLTMUONConstants::TriggerReconstructorId();
91 void AliHLTMUONTriggerReconstructorComponent::GetInputDataTypes( std::vector<AliHLTComponentDataType>& list)
94 /// Inherited from AliHLTProcessor. Returns the list of expected input data types.
98 list.push_back( AliHLTMUONConstants::DDLRawDataType() );
102 AliHLTComponentDataType AliHLTMUONTriggerReconstructorComponent::GetOutputDataType()
105 /// Inherited from AliHLTComponent. Returns the output data type.
108 return AliHLTMUONConstants::TriggerRecordsBlockDataType();
112 void AliHLTMUONTriggerReconstructorComponent::GetOutputDataSize(
113 unsigned long& constBase, double& inputMultiplier
117 /// Inherited from AliHLTComponent. Returns an estimate of the expected output data size.
120 constBase = sizeof(AliHLTMUONTriggerRecordsBlockWriter::HeaderType);
125 AliHLTComponent* AliHLTMUONTriggerReconstructorComponent::Spawn()
128 /// Inherited from AliHLTComponent. Creates a new object instance.
131 return new AliHLTMUONTriggerReconstructorComponent;
135 int AliHLTMUONTriggerReconstructorComponent::DoInit(int argc, const char** argv)
138 /// Inherited from AliHLTComponent.
139 /// Parses the command line parameters and initialises the component.
142 // perform initialization.
144 HLTInfo("Initialising dHLT trigger reconstructor component.");
146 // Make sure to cleanup fTrigRec if it is still there for some reason.
147 if (fTrigRec != NULL)
155 fTrigRec = new AliHLTMUONTriggerReconstructor();
157 catch (const std::bad_alloc&)
159 HLTError("Could not allocate more memory for the trigger reconstructor component.");
164 fWarnForUnexpecedBlock = false;
165 fStopOnOverflow = false;
168 const char* lutFileName = NULL;
169 const char* cdbPath = NULL;
172 bool suppressPartialTrigs = true;
173 bool tryRecover = false;
175 for (int i = 0; i < argc; i++)
177 if (strcmp( argv[i], "-lut" ) == 0)
181 HLTError("LookupTable filename not specified." );
182 // Make sure to delete fTrigRec to avoid partial initialisation.
188 lutFileName = argv[i+1];
194 if (strcmp( argv[i], "-ddl" ) == 0)
198 HLTError("DDL number not specified. It must be in the range [21..22]" );
199 // Make sure to delete fTrigRec to avoid partial initialisation.
206 unsigned long num = strtoul(argv[i+1], &cpErr, 0);
207 if (cpErr == NULL or *cpErr != '\0')
209 HLTError("Cannot convert '%s' to a DDL Number.", argv[i+1]);
210 // Make sure to delete fTrigRec to avoid partial initialisation.
215 if (num < 21 or 22 < num)
217 HLTError("The DDL number must be in the range [21..22].");
218 // Make sure to delete fTrigRec to avoid partial initialisation.
223 fDDL = num - 1; // Convert to DDL number in the range 0..21
229 if (strcmp( argv[i], "-ddlid" ) == 0)
233 HLTError("DDL equipment ID number not specified. It must be in the range [2816..2817]" );
234 // Make sure to delete fTrigRec to avoid partial initialisation.
241 unsigned long num = strtoul(argv[i+1], &cpErr, 0);
242 if (cpErr == NULL or *cpErr != '\0')
244 HLTError("Cannot convert '%s' to a DDL equipment ID Number.", argv[i+1]);
245 // Make sure to delete fTrigRec to avoid partial initialisation.
250 fDDL = AliHLTMUONUtils::EquipIdToDDLNumber(num); // Convert to DDL number in the range 0..21
251 if (fDDL < 20 or 21 < fDDL)
253 HLTError("The DDL equipment ID number must be in the range [2816..2817].");
254 // Make sure to delete fTrigRec to avoid partial initialisation.
264 if (strcmp( argv[i], "-cdb" ) == 0)
270 if (strcmp( argv[i], "-cdbpath" ) == 0)
274 HLTError("The CDB path was not specified." );
275 // Make sure to delete fTrigRec to avoid partial initialisation.
286 if (strcmp( argv[i], "-run" ) == 0)
290 HLTError("The RUN number was not specified." );
291 // Make sure to delete fTrigRec to avoid partial initialisation.
298 run = Int_t( strtoul(argv[i+1], &cpErr, 0) );
299 if (cpErr == NULL or *cpErr != '\0')
301 HLTError("Cannot convert '%s' to a valid run number."
302 " Expected an integer value.", argv[i+1]
304 // Make sure to delete fTrigRec to avoid partial initialisation.
314 if (strcmp( argv[i], "-warn_on_unexpected_block" ) == 0)
316 fWarnForUnexpecedBlock = true;
320 if (strcmp( argv[i], "-suppress_partial_triggers" ) == 0)
322 suppressPartialTrigs = true;
326 if (strcmp( argv[i], "-generate_partial_triggers" ) == 0)
328 suppressPartialTrigs = false;
332 if (strcmp( argv[i], "-stop_on_buffer_overflow" ) == 0)
334 fStopOnOverflow = true;
338 if (strcmp( argv[i], "-tryrecover" ) == 0)
344 if (strcmp( argv[i], "-dont_use_crateid" ) == 0)
350 HLTError("Unknown option '%s'.", argv[i] );
351 // Make sure to delete fTrigRec to avoid partial initialisation.
358 if (lutFileName == NULL) useCDB = true;
362 HLTWarning("DDL number not specified. Cannot check if incomming data is valid.");
368 HLTInfo("Loading lookup table information from CDB for DDL %d (ID = %d).",
369 fDDL+1, AliHLTMUONUtils::DDLNumberToEquipId(fDDL)
372 HLTWarning("DDL number not specified. The lookup table loaded from CDB will be empty!");
373 result = ReadCDB(cdbPath, run);
377 HLTInfo("Loading lookup table information from file %s.", lutFileName);
378 result = ReadLookUpTable(lutFileName);
382 // Error messages already generated in ReadCDB or ReadLookUpTable.
384 // Make sure to delete fTrigRec to avoid partial initialisation.
390 fTrigRec->SuppressPartialTriggers(suppressPartialTrigs);
391 fTrigRec->TryRecover(tryRecover);
392 fTrigRec->UseCrateId(fUseCrateId);
398 int AliHLTMUONTriggerReconstructorComponent::DoDeinit()
401 /// Inherited from AliHLTComponent. Performs a cleanup of the component.
404 HLTInfo("Deinitialising dHLT trigger reconstructor component.");
406 if (fTrigRec != NULL)
415 int AliHLTMUONTriggerReconstructorComponent::DoEvent(
416 const AliHLTComponentEventData& evtData,
417 const AliHLTComponentBlockData* blocks,
418 AliHLTComponentTriggerData& /*trigData*/,
419 AliHLTUInt8_t* outputPtr,
420 AliHLTUInt32_t& size,
421 std::vector<AliHLTComponentBlockData>& outputBlocks
425 /// Inherited from AliHLTProcessor. Processes the new event data.
429 unsigned long totalSize = 0; // Amount of memory currently consumed in bytes.
431 HLTDebug("Processing event %llu with %u input data blocks.",
432 evtData.fEventID, evtData.fBlockCnt
435 // Loop over all input blocks in the event and run the trigger DDL
436 // reconstruction algorithm on the raw data.
437 for (AliHLTUInt32_t n = 0; n < evtData.fBlockCnt; n++)
439 HLTDebug("Handling block: %u, with fDataType = '%s', fPtr = %p and fSize = %u bytes.",
440 n, DataType2Text(blocks[n].fDataType).c_str(), blocks[n].fPtr, blocks[n].fSize
443 if (blocks[n].fDataType != AliHLTMUONConstants::DDLRawDataType()
444 or not AliHLTMUONUtils::IsTriggerDDL(blocks[n].fSpecification)
447 // Log a message indicating that we got a data block that we
448 // do not know how to handle.
449 if (fWarnForUnexpecedBlock)
450 HLTWarning("Received a data block of a type we cannot handle: '%s', spec: 0x%X",
451 DataType2Text(blocks[n].fDataType).c_str(), blocks[n].fSpecification
454 HLTDebug("Received a data block of a type we cannot handle: '%s', spec: 0x%X",
455 DataType2Text(blocks[n].fDataType).c_str(), blocks[n].fSpecification
461 AliHLTInt32_t receivedDDL = AliHLTMUONUtils::SpecToDDLNumber(blocks[n].fSpecification);
464 if (receivedDDL != fDDL)
466 HLTWarning("Received raw data from DDL %d (ID = %d),"
467 " but expect data only from DDL %d (ID = %d).",
468 receivedDDL+1, AliHLTMUONUtils::DDLNumberToEquipId(receivedDDL),
469 fDDL+1, AliHLTMUONUtils::DDLNumberToEquipId(fDDL)
474 // Create a new output data block and initialise the header.
475 AliHLTMUONTriggerRecordsBlockWriter block(outputPtr+totalSize, size-totalSize);
476 if (not block.InitCommonHeader())
478 HLTError("There is not enough space in the output buffer for the new data block."
479 " We require at least %ufTrigRec->GetkDDLHeaderSize() bytes, but have %u bytes left.",
480 sizeof(AliHLTMUONTriggerRecordsBlockWriter::HeaderType),
486 AliHLTUInt32_t totalDDLSize = blocks[n].fSize;
487 if (totalDDLSize < sizeof(AliRawDataHeader))
489 HLTError("Raw data block %d is %d bytes in size and is too short to"
490 " possibly contain valid DDL raw data. We expect it to have"
491 " at least %d bytes for the commond data header.",
492 n, totalDDLSize, sizeof(AliRawDataHeader)
496 AliRawDataHeader* header = reinterpret_cast<AliRawDataHeader*>(blocks[n].fPtr);
497 AliHLTUInt32_t payloadSize = totalDDLSize - sizeof(AliRawDataHeader);
498 AliHLTUInt8_t* buffer = reinterpret_cast<AliHLTUInt8_t*>(header + 1);
499 AliHLTUInt32_t nofTrigRec = block.MaxNumberOfEntries();
501 // Decode if this is a scalar event or not.
502 bool scalarEvent = ((header->GetL1TriggerMessage() & 0x1) == 0x1);
504 // Remember: the following does NOT change the mapping!
505 // It is just to generate unique trigger record IDs.
506 fTrigRec->SetDDL(receivedDDL);
508 bool runOk = fTrigRec->Run(
509 buffer, payloadSize, scalarEvent,
510 block.GetArray(), nofTrigRec
514 HLTError("Error while processing the trigger DDL reconstruction algorithm.");
515 if (not fTrigRec->OverflowedOutputBuffer()
516 or (fTrigRec->OverflowedOutputBuffer() and fStopOnOverflow)
519 size = totalSize; // Must tell the framework how much buffer space was used.
524 // nofTrigRec should now contain the number of triggers actually found
525 // and filled into the output data block, so we can set this number.
526 assert( nofTrigRec <= block.MaxNumberOfEntries() );
527 block.SetNumberOfEntries(nofTrigRec);
529 HLTDebug("Number of trigger records found is %d", nofTrigRec);
531 // Fill a block data structure for our output block.
532 AliHLTComponentBlockData bd;
535 // This block's start (offset) is after all other blocks written so far.
536 bd.fOffset = totalSize;
537 bd.fSize = block.BytesUsed();
538 bd.fDataType = AliHLTMUONConstants::TriggerRecordsBlockDataType();
539 bd.fSpecification = blocks[n].fSpecification;
540 outputBlocks.push_back(bd);
542 HLTDebug("Created a new output data block at fPtr = %p,"
543 " with fOffset = %u (0x%.X) and fSize = %u bytes.",
544 bd.fPtr, bd.fOffset, bd.fOffset, bd.fSize
547 // Increase the total amount of data written so far to our output memory.
548 totalSize += block.BytesUsed();
551 // Finally we set the total size of output memory we consumed.
557 int AliHLTMUONTriggerReconstructorComponent::ReadLookUpTable(const char* lutpath)
560 /// Read in the lookup table from file.
563 assert(fTrigRec != NULL);
566 file.open(lutpath, fstream::binary | fstream::in);
569 HLTError("Could not open file: %s", lutpath);
573 file.read(reinterpret_cast<char*>(fTrigRec->LookupTableBuffer()), fTrigRec->LookupTableSize());
576 HLTError("The file %s was too short to contain a valid lookup table for this component.", lutpath);
582 HLTError("Could not read from file: %s", lutpath);
592 int AliHLTMUONTriggerReconstructorComponent::ReadCDB(const char* cdbPath, Int_t run)
594 /// Loads the lookup table containing channel and geometrical position
595 /// information about trigger strips from CDB.
596 /// \param cdbPath This specifies the CDB path to use to load from.
597 /// Can be set to NULL meaning the default storage is used.
598 /// \param run Specifies the run number to use. If set to -1 then the
599 /// default / current run number set for the CDB is used.
600 /// \return 0 on success and non zero codes for errors.
604 HLTError("No DDL number specified for which to load LUT data from CDB.");
608 int result = FetchMappingStores(cdbPath, run);
609 // Error message already generated in FetchMappingStores.
610 if (result != 0) return result;
611 AliMpDDLStore* ddlStore = AliMpDDLStore::Instance();
613 AliMpSegmentation* segmentation = AliMpSegmentation::Instance();
614 if (segmentation == NULL)
616 HLTError("Could not find segmentation mapping (AliMpSegmentation) instance.");
620 // Only load geometry if not already loaded.
621 if (AliGeomManager::GetGeometry() == NULL)
623 AliGeomManager::LoadGeometry();
625 AliMUONGeometryTransformer transformer;
626 if (not transformer.LoadGeometryData())
628 HLTError("Could not load geometry into transformer.");
632 AliHLTMUONTriggerRecoLookupTable* lookupTable = fTrigRec->LookupTableBuffer();
634 for (Int_t i = 0; i < 16; i++)
635 for (Int_t j = 0; j < 16; j++)
636 for (Int_t k = 0; k < 4; k++)
637 for (Int_t n = 0; n < 2; n++)
638 for (Int_t m = 0; m < 16; m++)
640 lookupTable->fRow[i][j][k][n][m].fIdFlags = 0x0;
641 lookupTable->fRow[i][j][k][n][m].fX = 0;
642 lookupTable->fRow[i][j][k][n][m].fY = 0;
643 lookupTable->fRow[i][j][k][n][m].fZ = 0;
646 AliMpDEIterator detElemIter;
647 for (Int_t iReg = 0; iReg < 8; iReg++)
649 AliMpTriggerCrate* crate = ddlStore->GetTriggerCrate(fDDL, iReg);
652 HLTError("Could not get crate mapping for regional header = %d"
653 " and DDL %d (ID = %d).",
654 iReg, fDDL+1, AliHLTMUONUtils::DDLNumberToEquipId(fDDL)
658 // Depending on the value of fUseCrateId, use either the crate ID as would
659 // be found in the regional header structures or the sequencial index number
661 UInt_t crateId = (fUseCrateId ? crate->GetId() : iReg);
664 HLTError("The crate ID number (%d) for regional header = %d and"
665 " DDL %d (ID = %d) is too big. It should be in the range [0..15]",
666 crateId, iReg, fDDL+1, AliHLTMUONUtils::DDLNumberToEquipId(fDDL)
671 for (Int_t iLocBoard = 0; iLocBoard < 16; iLocBoard++)
673 Int_t boardId = crate->GetLocalBoardId(iLocBoard);
674 if (boardId == 0) continue;
676 AliMpLocalBoard* localBoard = ddlStore->GetLocalBoard(boardId);
677 if (localBoard == NULL)
679 HLTError("Could not get local board: %d.", boardId);
684 if (! localBoard->IsNotified()) continue;
686 for (Int_t iChamber = 0; iChamber < 4; iChamber++)
688 Int_t detElemId = ddlStore->GetDEfromLocalBoard(boardId, iChamber);
690 AliHLTUInt32_t idflags = AliHLTMUONUtils::PackRecHitFlags(iChamber+10, detElemId);
692 const AliMUONGeometryDetElement* detElemTransform = transformer.GetDetElement(detElemId);
693 if (detElemTransform == NULL)
695 HLTError("Got NULL pointer for geometry transformer"
696 " for detection element ID = %d.",
702 for (Int_t iCathode = 0; iCathode <= 1; iCathode++)
704 const AliMpVSegmentation* seg = segmentation->GetMpSegmentation(
705 detElemId, AliMp::GetCathodType(iCathode)
708 for (Int_t bitxy = 0; bitxy < 16; bitxy++)
711 if (iCathode && localBoard->GetSwitch(6)) offset = -8;
713 AliMpPad pad = seg->PadByLocation(AliMpIntPair(boardId, bitxy+offset), kFALSE);
717 // There is no pad associated with the given local board and bit pattern.
721 // Get the global coodinates of the pad.
722 Float_t lx = pad.Position().X();
723 Float_t ly = pad.Position().Y();
725 detElemTransform->Local2Global(lx, ly, 0, gx, gy, gz);
728 lookupTable->fRow[crateId][iLocBoard][iChamber][iCathode][bitxy].fIdFlags = idflags;
729 lookupTable->fRow[crateId][iLocBoard][iChamber][iCathode][bitxy].fX = gx;
730 lookupTable->fRow[crateId][iLocBoard][iChamber][iCathode][bitxy].fY = gy;
731 lookupTable->fRow[crateId][iLocBoard][iChamber][iCathode][bitxy].fZ = gz;
742 bool AliHLTMUONTriggerReconstructorComponent::GenerateLookupTable(
743 AliHLTInt32_t ddl, const char* filename,
744 const char* cdbPath, Int_t run
745 //TODO add option fCrateId
748 /// Generates a binary file containing the lookup table (LUT) from the
749 /// CDB, which can be used for the trigger reconstructor component later.
750 /// @param ddl Must be the DDL for which to generate the DDL,
751 /// in the range [20..21].
752 /// @param filename The name of the LUT file to generate.
753 /// @param cdbPath The CDB path to use.
754 /// @param run The run number to use for the CDB.
755 /// @return True if the generation of the LUT file succeeded.
757 AliHLTMUONTriggerReconstructorComponent comp;
759 if (ddl < 20 or 21 < ddl)
761 std::cerr << "ERROR: the DDL number must be in the range [20..21]." << std::endl;
767 sprintf(ddlNum, "%d", ddl+1);
768 sprintf(runNum, "%d", run);
769 const char* argv[7] = {"-ddl", ddlNum, "-cdbpath", cdbPath, "-run", runNum, NULL};
770 int result = comp.DoInit(6, argv);
773 // Error message already generated in DoInit.
777 std::fstream file(filename, std::ios::out);
780 std::cerr << "ERROR: could not open file: " << filename << std::endl;
785 reinterpret_cast<char*>(comp.fTrigRec->LookupTableBuffer()),
786 comp.fTrigRec->LookupTableSize()
790 std::cerr << "ERROR: There was a problem writing to the file: " << filename << std::endl;