1 /**************************************************************************
2 * This file is property of and copyright by the ALICE HLT Project *
3 * All rights reserved. *
6 * Artur Szostak <artursz@iafrica.com> *
8 * Permission to use, copy, modify and distribute this software and its *
9 * documentation strictly for non-commercial purposes is hereby granted *
10 * without fee, provided that the above copyright notice appears in all *
11 * copies and that both the copyright notice and this permission notice *
12 * appear in the supporting documentation. The authors make no claims *
13 * about the suitability of this software for any purpose. It is *
14 * provided "as is" without express or implied warranty. *
15 **************************************************************************/
20 /// @file AliHLTMUONRecHitsSource.cxx
21 /// @author Artur Szostak <artursz@iafrica.com>
23 /// @brief Implementation of the AliHLTMUONRecHitsSource component.
26 #include "AliHLTMUONRecHitsSource.h"
27 #include "AliHLTMUONConstants.h"
28 #include "AliHLTMUONUtils.h"
29 #include "AliHLTMUONDataBlockWriter.h"
30 #include "AliMUONMCDataInterface.h"
31 #include "AliMUONDataInterface.h"
32 #include "AliMUONHit.h"
33 #include "AliMUONVCluster.h"
34 #include "AliMUONConstants.h"
35 #include "AliMUONVClusterStore.h"
36 #include "AliMUONVHitStore.h"
37 #include "mapping/AliMpCDB.h"
38 #include "mapping/AliMpDEManager.h"
39 #include "mapping/AliMpDetElement.h"
46 ClassImp(AliHLTMUONRecHitsSource);
49 AliHLTMUONRecHitsSource::AliHLTMUONRecHitsSource() :
50 AliHLTOfflineDataSource(),
51 fMCDataInterface(NULL),
53 fSelection(kWholePlane),
57 /// Default constructor.
60 for (Int_t i = 0; i < AliMUONConstants::NTrackingCh(); i++)
61 fServeChamber[i] = false;
65 AliHLTMUONRecHitsSource::~AliHLTMUONRecHitsSource()
68 /// Default destructor.
71 assert( fMCDataInterface == NULL );
72 assert( fDataInterface == NULL );
76 int AliHLTMUONRecHitsSource::DoInit(int argc, const char** argv)
79 /// Inherited from AliHLTComponent.
80 /// Parses the command line parameters and initialises the component.
83 assert( fMCDataInterface == NULL );
84 assert( fDataInterface == NULL );
86 // Parse the command line arguments:
89 bool chamberWasSet = false;
90 fCurrentEventIndex = 0;
91 bool firstEventSet = false;
92 bool eventNumLitSet = false;
94 for (int i = 0; i < argc; i++)
96 if (strcmp(argv[i], "-simdata") == 0)
100 else if (strcmp(argv[i], "-recdata") == 0)
104 else if (strcmp(argv[i], "-plane") == 0)
109 Logging(kHLTLogError,
110 "AliHLTMUONRecHitsSource::DoInit",
112 "Expected one of 'left', 'right' or 'all' after '-plane'."
116 if (strcmp(argv[i], "left") == 0)
117 fSelection = kLeftPlane;
118 else if (strcmp(argv[i], "right") == 0)
119 fSelection = kRightPlane;
120 else if (strcmp(argv[i], "all") == 0)
121 fSelection = kWholePlane;
124 Logging(kHLTLogError,
125 "AliHLTMUONRecHitsSource::DoInit",
127 "The parameter '%s' is invalid and must be one of 'left',"
128 " 'right' or 'all'.",
134 else if (strcmp(argv[i], "-chamber") == 0)
139 Logging(kHLTLogError,
140 "AliHLTMUONRecHitsSource::DoInit",
142 "Expected a chamber number, range eg. '1-10' or list eg."
143 " '1,2,3' after '-chamber'."
147 int result = ParseChamberString(argv[i]);
148 if (result != 0) return result;
149 chamberWasSet = true;
151 else if (strcmp(argv[i], "-firstevent") == 0)
155 HLTWarning("The -firstevent flag is overridden by a"
156 " previous use of -event_number_literal."
162 HLTError("Expected a positive number after -firstevent.");
166 long num = strtol(argv[i], &end, 0);
167 if (*end != '\0' or num < 0) // Check if the conversion is OK.
170 "Expected a positive number after -firstevent"
171 " but got: %s", argv[i]
175 fCurrentEventIndex = Int_t(num);
176 firstEventSet = true;
178 else if (strcmp(argv[i], "-event_number_literal") == 0)
182 HLTWarning("The -event_number_literal option will"
183 " override -firstevent."
186 fCurrentEventIndex = -1;
187 eventNumLitSet = true;
191 Logging(kHLTLogError,
192 "AliHLTMUONRecHitsSource::DoInit",
194 "The argument '%s' is invalid.",
201 // Check the parameters we have parsed.
202 if (simdata and recdata)
204 Logging(kHLTLogError,
205 "AliHLTMUONRecHitsSource::DoInit",
207 "Cannot have both -simdata and -recdata set."
212 if (not simdata and not recdata)
214 Logging(kHLTLogError,
215 "AliHLTMUONRecHitsSource::DoInit",
217 "Must have either -simdata or -recdata specified."
222 if (not chamberWasSet)
225 "AliHLTMUONRecHitsSource::DoInit",
226 "Setting Parameters",
227 "No chambers were selected so we will publish for all chambers."
229 for (Int_t i = 0; i < AliMUONConstants::NTrackingCh(); i++)
230 fServeChamber[i] = true;
233 // Must load the mapping data for AliMpDetElement::GetDdlId()
234 // to return useful information later on.
235 AliMpCDB::LoadDDLStore();
237 // Now we can initialise the data interface objects and loaders.
240 Logging(kHLTLogDebug,
241 "AliHLTMUONRecHitsSource::DoInit",
243 "Loading simulated GEANT hits with AliMUONMCDataInterface."
248 fMCDataInterface = new AliMUONMCDataInterface("galice.root");
250 catch (const std::bad_alloc&)
252 Logging(kHLTLogError,
253 "AliHLTMUONRecHitsSource::DoInit",
255 "Not enough memory to allocate AliMUONMCDataInterface."
262 Logging(kHLTLogDebug,
263 "AliHLTMUONRecHitsSource::DoInit",
265 "Loading reconstructed clusters with AliMUONDataInterface."
270 fDataInterface = new AliMUONDataInterface("galice.root");
272 catch (const std::bad_alloc&)
274 Logging(kHLTLogError,
275 "AliHLTMUONRecHitsSource::DoInit",
277 "Not enough memory to allocate AliMUONDataInterface."
283 // Check that the fCurrentEventIndex number falls within the correct range.
285 if (fMCDataInterface != NULL)
286 maxevent = UInt_t(fMCDataInterface->NumberOfEvents());
287 else if (fDataInterface != NULL)
288 maxevent = UInt_t(fDataInterface->NumberOfEvents());
289 if (fCurrentEventIndex != -1 and UInt_t(fCurrentEventIndex) >= maxevent and maxevent != 0)
291 fCurrentEventIndex = 0;
292 HLTWarning(Form("The selected first event number (%d) was larger than"
293 " the available number of events (%d). Resetting the event"
294 " counter to zero.", fCurrentEventIndex, maxevent
302 int AliHLTMUONRecHitsSource::DoDeinit()
305 /// Inherited from AliHLTComponent. Performs a cleanup of the component.
308 if (fMCDataInterface != NULL)
310 delete fMCDataInterface;
311 fMCDataInterface = NULL;
313 if (fDataInterface != NULL)
315 delete fDataInterface;
316 fDataInterface = NULL;
322 const char* AliHLTMUONRecHitsSource::GetComponentID()
325 /// Inherited from AliHLTComponent. Returns the component ID.
328 return AliHLTMUONConstants::RecHitsSourceId();
332 AliHLTComponentDataType AliHLTMUONRecHitsSource::GetOutputDataType()
335 /// Inherited from AliHLTComponent. Returns the output data type.
338 return AliHLTMUONConstants::RecHitsBlockDataType();
342 void AliHLTMUONRecHitsSource::GetOutputDataSize(
343 unsigned long& constBase, double& inputMultiplier
347 /// Inherited from AliHLTComponent. Returns an estimate of the expected output data size.
350 constBase = sizeof(AliHLTMUONRecHitsBlockStruct)
351 + 256*16*sizeof(AliHLTMUONRecHitStruct);
356 AliHLTComponent* AliHLTMUONRecHitsSource::Spawn()
359 /// Inherited from AliHLTComponent. Creates a new object instance.
362 return new AliHLTMUONRecHitsSource();
366 int AliHLTMUONRecHitsSource::GetEvent(
367 const AliHLTComponentEventData& evtData,
368 AliHLTComponentTriggerData& /*trigData*/,
369 AliHLTUInt8_t* outputPtr,
370 AliHLTUInt32_t& size,
371 vector<AliHLTComponentBlockData>& outputBlocks
375 /// Inherited from AliHLTOfflineDataSource. Creates new event data blocks.
378 assert( fMCDataInterface != NULL or fDataInterface != NULL );
380 // Check the size of the event descriptor structure.
381 if (evtData.fStructSize < sizeof(AliHLTComponentEventData))
383 Logging(kHLTLogError,
384 "AliHLTMUONRecHitsSource::GetEvent",
385 "Invalid event descriptor",
386 "The event descriptor (AliHLTComponentEventData) size is"
387 " smaller than expected. It claims to be %d bytes, but"
388 " we expect it to be %d bytes.",
390 sizeof(AliHLTComponentEventData)
392 size = 0; // Important to tell framework that nothing was generated.
396 // Use the fEventID as the event number to load if fCurrentEventIndex == -1,
397 // check it and load that event with the runloader.
398 // If fCurrentEventIndex is a positive number then us it instead and
400 UInt_t eventnumber = UInt_t(evtData.fEventID);
401 UInt_t maxevent = fMCDataInterface != NULL ?
402 UInt_t(fMCDataInterface->NumberOfEvents())
403 : UInt_t(fDataInterface->NumberOfEvents());
404 if (fCurrentEventIndex != -1)
406 eventnumber = UInt_t(fCurrentEventIndex);
407 fCurrentEventIndex++;
408 if (UInt_t(fCurrentEventIndex) >= maxevent)
409 fCurrentEventIndex = 0;
411 if ( eventnumber >= maxevent )
413 Logging(kHLTLogError,
414 "AliHLTMUONRecHitsSource::GetEvent",
416 "The event number (%d) is larger than the available number"
417 " of events on file (%d).",
421 size = 0; // Important to tell framework that nothing was generated.
425 // Create and initialise a new data block.
426 AliHLTMUONRecHitsBlockWriter block(outputPtr, size);
427 if (not block.InitCommonHeader())
429 Logging(kHLTLogError,
430 "AliHLTMUONRecHitsSource::GetEvent",
432 "There is not enough buffer space to create a new data block."
433 " We require at least %d bytes but the buffer is only %d bytes.",
434 sizeof(AliHLTMUONRecHitsBlockWriter::HeaderType),
437 size = 0; // Important to tell framework that nothing was generated.
441 // Initialise the DDL list containing the DDLs which contributed to the
442 // data block. These are required to create the specification word later.
444 for (Int_t i = 0; i < 22; i++)
447 if (fMCDataInterface != NULL)
449 Logging(kHLTLogDebug,
450 "AliHLTMUONRecHitsSource::GetEvent",
452 "Filling data block with GEANT hits for event %d.",
456 // Loop over all tracks, extract the hits and write them to the
458 Int_t ntracks = fMCDataInterface->NumberOfTracks(eventnumber);
459 for (Int_t i = 0; i < ntracks; ++i)
461 AliMUONVHitStore* hitStore = fMCDataInterface->HitStore(eventnumber,i);
463 TIter next(hitStore->CreateIterator());
464 while ( ( hit = static_cast<AliMUONHit*>(next()) ) )
466 // Select only hits on selected chambers.
467 Int_t chamber = hit->Chamber() - 1;
468 if (chamber > AliMUONConstants::NTrackingCh()) continue;
469 if (not fServeChamber[chamber]) continue;
471 // Only select hits from the given part of the plane
472 if (fSelection == kLeftPlane and not (hit->Xref() < 0)) continue;
473 if (fSelection == kRightPlane and not (hit->Xref() >= 0)) continue;
475 AliHLTMUONRecHitStruct* rechit = block.AddEntry();
478 Logging(kHLTLogError,
479 "AliHLTMUONRecHitsSource::GetEvent",
481 "There is not enough buffer space to add more hits."
482 " We overflowed the buffer which is only %d bytes.",
485 size = 0; // Important to tell framework that nothing was generated.
489 rechit->fX = hit->Xref();
490 rechit->fY = hit->Yref();
491 rechit->fZ = hit->Zref();
493 // Workout which DDL this hit will be readout of.
494 AliMpDetElement* de = AliMpDEManager::GetDetElement(hit->DetElemId());
495 if (de != NULL and (0 <= de->GetDdlId() and de->GetDdlId() < 22))
496 ddlList[de->GetDdlId()] = true;
498 Logging(kHLTLogError,
499 "AliHLTMUONRecHitsSource::GetEvent",
501 "Could not find the DDL ID from which readout would take place."
506 else if (fDataInterface != NULL)
508 Logging(kHLTLogDebug,
509 "AliHLTMUONRecHitsSource::GetEvent",
511 "Filling data block with reconstructed raw clusters for event %d.",
515 AliMUONVClusterStore* clusterStore = fDataInterface->ClusterStore(eventnumber);
517 // Loop over selected chambers and extract the raw clusters.
518 for (Int_t chamber = 0; chamber < AliMUONConstants::NTrackingCh(); chamber++)
520 // Select only hits on selected chambers.
521 if (not fServeChamber[chamber]) continue;
523 TIter next(clusterStore->CreateChamberIterator(chamber,chamber));
524 AliMUONVCluster* cluster;
525 while ( ( cluster = static_cast<AliMUONVCluster*>(next()) ) )
527 // Only select hits from the given part of the plane
528 if (fSelection == kLeftPlane and not (cluster->GetX() < 0)) continue;
529 if (fSelection == kRightPlane and not (cluster->GetX() >= 0)) continue;
531 AliHLTMUONRecHitStruct* rechit = block.AddEntry();
534 Logging(kHLTLogError,
535 "AliHLTMUONRecHitsSource::GetEvent",
537 "There is not enough buffer space to add more hits."
538 " We overflowed the buffer which is only %d bytes.",
541 size = 0; // Important to tell framework that nothing was generated.
545 rechit->fX = cluster->GetX();
546 rechit->fY = cluster->GetY();
547 rechit->fZ = cluster->GetZ();
549 // Workout which DDL this hit will be readout of.
550 AliMpDetElement* de = AliMpDEManager::GetDetElement(cluster->GetDetElemId());
551 if (de != NULL and (0 <= de->GetDdlId() and de->GetDdlId() < 22))
552 ddlList[de->GetDdlId()] = true;
554 Logging(kHLTLogError,
555 "AliHLTMUONRecHitsSource::GetEvent",
557 "Could not find the DDL ID from which readout would take place."
564 Logging(kHLTLogError,
565 "AliHLTMUONRecHitsSource::GetEvent",
566 "Missing data interface",
567 "Neither AliMUONDataInterface nor AliMUONMCDataInterface were created."
569 size = 0; // Important to tell framework that nothing was generated.
573 AliHLTComponentBlockData bd;
577 bd.fSize = block.BytesUsed();
578 bd.fDataType = AliHLTMUONConstants::RecHitsBlockDataType();
579 bd.fSpecification = AliHLTMUONUtils::PackSpecBits(ddlList);
580 outputBlocks.push_back(bd);
581 size = block.BytesUsed();
587 int AliHLTMUONRecHitsSource::ParseChamberString(const char* str)
590 /// Parses a string with the following format:
591 /// <number>|<number>-<number>[,<number>|<number>-<number>]...
592 /// For example: 1 1,2,3 1-2 1,2-4,5 etc...
593 /// Flags in the fServeChamber will be set to 'true' for all appropriate
595 /// @param str The string to parse.
596 /// @return Zero on success and EINVAL if there is a parse error.
599 char* end = const_cast<char*>(str);
600 long lastChamber = -1;
603 // Parse the next number.
605 long chamber = strtol(current, &end, 0);
607 // Check for parse errors of the number.
610 Logging(kHLTLogError,
611 "AliHLTMUONRecHitsSource::GetEvent",
613 "Expected a number in the range [1..%d] but got '%s'.",
614 AliMUONConstants::NTrackingCh(), current
618 if (chamber < 1 or AliMUONConstants::NTrackingCh() < chamber)
620 Logging(kHLTLogError,
621 "AliHLTMUONRecHitsSource::GetEvent",
623 "Got the chamber number %d which is outside the valid range of [1..%d].",
624 chamber, AliMUONConstants::NTrackingCh()
629 // Skip any whitespace after the number
630 while (*end != '\0' and (*end == ' ' or *end == '\t' or *end == '\r' or *end == '\n')) end++;
632 // Check if we are dealing with a list or range, or if we are at
633 // the end of the string.
636 lastChamber = chamber;
640 else if (*end == ',')
642 assert( 1 <= chamber and chamber <= 10 );
643 fServeChamber[chamber-1] = true;
646 else if (*end == '\0')
648 assert( 1 <= chamber and chamber <= 10 );
649 fServeChamber[chamber-1] = true;
653 Logging(kHLTLogError,
654 "AliHLTMUONRecHitsSource::GetEvent",
656 "Could not understand parameter list '%s'. Expected '-', ','"
657 " or end of line but got '%c' at character %d.",
658 str, *end, (int)(end - str) +1
663 // Set the range of chambers to publish for.
667 if (lastChamber < chamber)
679 for (Int_t i = min; i <= max; i++)
680 fServeChamber[i-1] = true;
684 while (*end != '\0');