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 AliHLTMUONClusterFinderComponent.cxx
21 /// @author Artur Szostak <artursz@iafrica.com>
23 /// @brief Implementation of the offline algorithm cluster finding processing component.
25 /// The cluster finder component interfaces the offline MUON reconstruction
26 /// algorithms for cluster finding with the online HLT system.
29 #include "AliHLTMUONClusterFinderComponent.h"
30 #include "AliHLTMUONConstants.h"
31 #include "AliHLTMUONUtils.h"
32 #include "AliHLTMUONDataBlockWriter.h"
33 #include "AliHLTLogging.h"
34 #include "AliHLTSystem.h"
35 #include "AliHLTDefinitions.h"
36 #include "AliMUONGeometryTransformer.h"
37 #include "AliMUONCalibrationData.h"
38 #include "AliMUONVCalibParam.h"
39 #include "AliMUONVDigitStore.h"
40 #include "AliMUONVClusterStore.h"
41 #include "AliMUONClusterStoreV2.h"
42 #include "AliMUONDigitMaker.h"
43 #include "AliMUONDigitCalibrator.h"
44 #include "AliMUONCalibrationData.h"
45 #include "AliMUONGeometryTransformer.h"
46 #include "AliMUONVClusterFinder.h"
47 #include "AliMUONSimpleClusterServer.h"
48 #include "AliMUONRecoParam.h"
49 #include "AliMUONReconstructor.h"
50 #include "AliMUONVCluster.h"
51 #include "AliMUONRawStreamTrackerHP.h"
52 #include "AliMpConstants.h"
54 #include "AliMpArea.h"
55 #include "AliRawReader.h"
56 #include "AliRawReaderMemory.h"
57 #include "AliCDBManager.h"
58 #include "AliGeomManager.h"
65 ClassImp(AliHLTMUONClusterFinderComponent)
68 AliHLTMUONClusterFinderComponent::AliHLTMUONClusterFinderComponent() :
69 AliHLTMUONProcessor(),
73 fCalibrationData(NULL),
74 fDigitCalibrator(NULL),
78 fMakeClusterStore(true),
81 /// Default constructor.
85 AliHLTMUONClusterFinderComponent::~AliHLTMUONClusterFinderComponent()
87 /// Default destructor.
92 const char* AliHLTMUONClusterFinderComponent::GetComponentID()
94 /// Inherited from AliHLTComponent. Returns the component ID.
96 return AliHLTMUONConstants::ClusterFinderId();
100 void AliHLTMUONClusterFinderComponent::GetInputDataTypes(AliHLTComponentDataTypeList& list)
102 /// Inherited from AliHLTProcessor. Returns the list of expected input data types.
105 list.push_back( AliHLTMUONConstants::DDLRawDataType() );
109 AliHLTComponentDataType AliHLTMUONClusterFinderComponent::GetOutputDataType()
111 /// Inherited from AliHLTComponent. Returns kAliHLTMultipleDataType
112 /// refer to GetOutputDataTypes for all returned data types.
114 return kAliHLTMultipleDataType;
118 int AliHLTMUONClusterFinderComponent::GetOutputDataTypes(AliHLTComponentDataTypeList& list)
120 /// Inherited from AliHLTComponent. Returns the output data types.
122 assert( list.empty() );
123 list.push_back( AliHLTMUONConstants::ClusterStoreDataType() );
124 list.push_back( AliHLTMUONConstants::RecHitsBlockDataType() );
129 void AliHLTMUONClusterFinderComponent::GetOutputDataSize(unsigned long& constBase, double& inputMultiplier)
131 /// Inherited from AliHLTComponent. Returns an estimate of the expected output data size.
133 constBase = sizeof(AliMUONVClusterStore) + 1024*1024;
138 AliHLTComponent* AliHLTMUONClusterFinderComponent::Spawn()
140 /// Inherited from AliHLTComponent. Creates a new object instance.
142 return new AliHLTMUONClusterFinderComponent;
146 int AliHLTMUONClusterFinderComponent::DoInit(int argc, const char** argv)
148 /// Inherited from AliHLTComponent.
149 /// Parses the command line parameters and initialises the component.
151 HLTInfo("Initialising dHLT cluster finder component.");
153 // Inherit the parents functionality.
154 int result = AliHLTMUONProcessor::DoInit(argc, argv);
155 if (result != 0) return result;
157 // Must make sure that all the offline reconstruction objects are released in case
158 // they are still allocated for whatever reason.
161 // Initialise fields with default values then parse the command line.
162 fMakeClusterStore = true;
163 fMakeRecHits = false;
164 bool tryRecover = false;
166 for (int i = 0; i < argc; i++)
168 if (ArgumentAlreadyHandled(i, argv[i])) continue;
170 if (strcmp( argv[i], "-tryrecover" ) == 0)
176 if (strcmp( argv[i], "-nostore" ) == 0)
178 fMakeClusterStore = false;
182 if (strcmp( argv[i], "-makehits" ) == 0)
188 HLTError("Unknown option '%s'", argv[i]);
195 fRawReader = new AliRawReaderMemory();
196 fDigitMaker = new AliMUONDigitMaker(true, true, true);
197 fTransformer = new AliMUONGeometryTransformer();
199 catch (const std::bad_alloc&)
201 HLTError("Could not allocate more memory for the MUON offline reconstruction objects.");
206 if (not DelaySetup())
208 result = ReadConfigFromCDB();
211 // Error messages already generated in ReadConfigFromCDB.
212 FreeObjects(); // Make sure we cleanup to avoid partial initialisation.
217 #ifndef HAVE_NOT_MUON_DIGITMAKER_GETRAWSTREAM
218 AliMUONRawStreamTrackerHP* stream = static_cast<AliMUONRawStreamTrackerHP*>(
219 fDigitMaker->GetRawStreamTracker()
221 stream->TryRecover(tryRecover);
222 #endif //HAVE_NOT_MUON_DIGITMAKER_GETRAWSTREAM
228 int AliHLTMUONClusterFinderComponent::DoDeinit()
230 /// Inherited from AliHLTComponent. Performs a cleanup of the component.
232 HLTInfo("Deinitialising dHLT cluster finder component.");
238 int AliHLTMUONClusterFinderComponent::Reconfigure(
239 const char* cdbEntry, const char* componentId
242 /// Inherited from AliHLTComponent. This method will reload CDB configuration
243 /// entries for this component from the CDB.
244 /// \param cdbEntry If set to NULL or starts with "MUON/" then the DDL store
245 /// for MUON will be loaded which contains the mapping and also the
246 /// calibration data will be updated.
247 /// \param componentId The name of the component in the current chain.
249 bool startsWithMUON = TString(cdbEntry).Index("MUON/", 5, 0, TString::kExact) == 0;
251 if (cdbEntry == NULL or startsWithMUON)
253 HLTInfo("Reading new configuration entries from CDB for component '%s'.", componentId);
255 //TODO: add more granularity to the loading.
256 return ReadConfigFromCDB();
263 int AliHLTMUONClusterFinderComponent::ReadPreprocessorValues(const char* modules)
265 /// Inherited from AliHLTComponent.
266 /// Updates the configuration of this component if either HLT or MUON have
267 /// been specified in the 'modules' list.
269 TString mods = modules;
270 if (mods.Contains("ALL") or mods.Contains("MUON"))
272 return Reconfigure(NULL, GetComponentID());
274 if (mods.Contains("MUON"))
276 return Reconfigure("MUON/*", GetComponentID());
282 int AliHLTMUONClusterFinderComponent::DoEvent(
283 const AliHLTComponentEventData& evtData,
284 const AliHLTComponentBlockData* blocks,
285 AliHLTComponentTriggerData& trigData,
286 AliHLTUInt8_t* outputPtr,
287 AliHLTUInt32_t& size,
288 AliHLTComponentBlockDataList& outputBlocks
291 /// Inherited from AliHLTProcessor. Processes the new event data by
292 /// applying the offline cluster finding algorithms.
294 // Initialise the mapping and calibration from CDB if we were requested
295 // to initialise only when the first event was received.
298 int result = ReadConfigFromCDB();
299 if (result != 0) return result;
303 assert(fRawReader != NULL);
304 assert(fDigitMaker != NULL);
305 assert(fTransformer != NULL);
306 assert(fCalibrationData != NULL);
307 assert(fDigitCalibrator != NULL);
308 assert(fClusterFinder != NULL);
309 assert(fClusterServer != NULL);
311 AliHLTUInt32_t specification = 0x0; // Accumulated specification to use for output blocks.
313 HLTDebug("Processing event %llu with %u input data blocks.",
314 evtData.fEventID, evtData.fBlockCnt
317 AliMUONVDigitStore* digitStore = NULL;
318 AliMUONVClusterStore* clusterStore = NULL;
321 digitStore = AliMUONVDigitStore::Create("AliMUONDigitStoreV2R");
322 clusterStore = new AliMUONClusterStoreV2();
324 catch (const std::bad_alloc&)
326 HLTError("Could not allocate more memory for the digit or cluster store object.");
327 if (digitStore != NULL) delete digitStore;
328 if (clusterStore != NULL) delete clusterStore;
333 const AliHLTComponentBlockData* block = NULL;
334 const AliHLTComponentDataType& rawType = AliHLTMUONConstants::DDLRawDataType();
335 for (block = GetFirstInputBlock(rawType); block != NULL; block = GetNextInputBlock())
337 HLTDebug("Handling block with fDataType = '%s', fSpecification = 0x%8.8X, fPtr = %p and fSize = %u bytes.",
338 DataType2Text(block->fDataType).c_str(), block->fSpecification,
339 block->fPtr, block->fSize
342 if (not AliHLTMUONUtils::IsTrackerDDL(block->fSpecification))
344 HLTError("Received raw data from a DDL that was not a tracker DDL."
345 " The data block specification was: 0x%8.8X."
346 " Will skip the data block.",
347 block->fSpecification
352 specification |= block->fSpecification;
354 fRawReader->SetMemory(reinterpret_cast<UChar_t*>(block->fPtr), ULong_t(block->fSize));
355 fRawReader->SetEquipmentID(AliHLTMUONUtils::SpecToEquipId(block->fSpecification));
357 fRawReader->NextEvent();
358 fDigitMaker->Raw2Digits(fRawReader, digitStore, NULL);
359 #ifndef HAVE_NOT_MUON_DIGITMAKER_GETRAWSTREAM
360 if (fDigitMaker->GetRawStreamTracker()->IsErrorMessage() and DumpDataOnError())
362 DumpEvent(evtData, blocks, trigData, outputPtr, size, outputBlocks);
364 #endif //HAVE_NOT_MUON_DIGITMAKER_GETRAWSTREAM
367 fDigitCalibrator->Calibrate(*digitStore);
368 TIter next(digitStore->CreateIterator());
369 fClusterServer->UseDigits(next);
371 for (Int_t i = 0; i < AliMpConstants::NofTrackingChambers(); ++i)
373 if (fRecoParam->UseChamber(i))
375 if ( ( i == 6 or i == 7 ) and fRecoParam->BypassSt4() ) continue;
376 if ( ( i == 8 or i == 9 ) and fRecoParam->BypassSt5() ) continue;
377 fClusterServer->Clusterize(i, *clusterStore, area);
381 // Now write the clusters to output blocks.
382 AliHLTUInt8_t* buffer = outputPtr;
383 AliHLTUInt32_t bufferSize = size;
385 if (fMakeClusterStore)
387 PushBack(clusterStore, AliHLTMUONConstants::ClusterStoreDataType(), specification);
393 bufferSize = sizeof(AliHLTMUONRecHitsBlockWriter::HeaderType)
394 + sizeof(AliHLTMUONRecHitsBlockWriter::ElementType) * clusterStore->GetSize();
395 buffer = new AliHLTUInt8_t[size];
397 catch (const std::bad_alloc&)
399 HLTError("Could not allocate more memory for the reconstructed hit buffer.");
400 if (digitStore != NULL) delete digitStore;
401 if (clusterStore != NULL) delete clusterStore;
409 AliHLTMUONRecHitsBlockWriter outBlock(buffer, bufferSize);
410 outBlock.InitCommonHeader();
411 AliHLTUInt32_t i = 0;
412 TIter next2(clusterStore->CreateIterator());
413 AliMUONVCluster* cluster = NULL;
414 while ( (cluster = static_cast<AliMUONVCluster*>(next2())) != NULL )
416 AliHLTMUONRecHitStruct& hit = outBlock[i++];
417 hit.fFlags = AliHLTMUONUtils::PackRecHitFlags(
418 AliHLTUInt8_t(cluster->GetChamberId()),
419 AliHLTUInt16_t(cluster->GetDetElemId())
421 hit.fX = cluster->GetX();
422 hit.fY = cluster->GetY();
423 hit.fZ = cluster->GetZ();
425 outBlock.SetNumberOfEntries(i);
428 buffer, outBlock.BytesUsed(),
429 AliHLTMUONConstants::RecHitsBlockDataType(), specification
432 if (fMakeClusterStore)
445 void AliHLTMUONClusterFinderComponent::FreeObjects()
447 /// Deletes any allocated objects, if they are allocated, else nothing is
448 /// done for objects not yet allocated.
449 /// This is used as a helper method to make sure the corresponding pointers
450 /// are NULL and we get back to a well defined state.
452 if (fRawReader != NULL)
457 if (fDigitMaker != NULL)
462 if (fTransformer != NULL)
467 if (fCalibrationData != NULL)
469 delete fCalibrationData;
470 fCalibrationData = NULL;
472 if (fDigitCalibrator != NULL)
474 delete fDigitCalibrator;
475 fDigitCalibrator = NULL;
477 if (fClusterServer != NULL)
479 delete fClusterServer;
480 fClusterServer = NULL;
481 fClusterFinder = NULL; // The cluster server takes ownership.
484 // The following is just in case we created the cluster finder, but could
485 // not yet create the cluster server.
486 if (fClusterFinder != NULL)
488 delete fClusterFinder;
489 fClusterFinder = NULL;
492 if (fRecoParam != NULL)
500 int AliHLTMUONClusterFinderComponent::ReadConfigFromCDB(
501 bool loadParams, bool loadMapping, bool loadGeom, bool loadCalib
504 /// Loads the various configuration, calibration, mapping and geometry
509 HLTInfo("Loading mapping information from CDB.");
510 int result = FetchMappingStores();
511 if (result != 0) return result;
516 HLTInfo("Loading reconstruction parameters from CDB.");
517 AliMUONRecoParam* recoParam = NULL;
521 HLTWarning("Have not yet implemented loading reco params from CDB! Using AliMUONRecoParam::GetLowFluxParam().");
522 //result = LoadRecoParamsFromCDB(recoParam);
523 //if (result != 0) return result;
524 recoParam = AliMUONRecoParam::GetLowFluxParam();
526 catch (const std::bad_alloc&)
528 HLTError("Could not allocate more memory for the reconstruction parameter object.");
531 if (fRecoParam != NULL) delete fRecoParam;
532 fRecoParam = recoParam;
537 HLTInfo("Loading geometry data from CDB.");
538 // Only load geometry if not already loaded.
539 if (AliGeomManager::GetGeometry() == NULL)
541 AliGeomManager::LoadGeometry();
543 assert(fTransformer != NULL);
544 if (not fTransformer->LoadGeometryData())
546 HLTError("Could not load geometry data for transformation.");
553 assert(fRecoParam != NULL);
555 HLTInfo("Loading calibration information from CDB.");
556 AliMUONCalibrationData* calibData = NULL;
559 assert(AliCDBManager::Instance() != NULL);
560 Int_t runNumber = AliCDBManager::Instance()->GetRun();
561 calibData = new AliMUONCalibrationData(runNumber);
563 catch (const std::bad_alloc&)
565 HLTError("Could not allocate more memory for the calibration data object.");
569 if (not calibData->IsValid())
571 HLTError("Could not retrieve calibrations!");
576 // Check that we get all the calibrations we'll need.
577 if (not calibData->Pedestals() or
578 not calibData->Gains() or
579 not calibData->HV() )
581 HLTError("Could not access all required calibration data.");
586 if (fCalibrationData != NULL) delete fCalibrationData;
587 fCalibrationData = calibData;
590 AliMUONDigitCalibrator* calibrator = NULL;
591 AliMUONVClusterFinder* clusterFinder = NULL;
592 AliMUONSimpleClusterServer* clusterServer = NULL;
595 calibrator = new AliMUONDigitCalibrator(
596 *fCalibrationData, fRecoParam->GetCalibrationMode()
599 clusterFinder = AliMUONReconstructor::CreateClusterFinder(
600 fRecoParam->GetClusteringMode()
603 clusterServer = new AliMUONSimpleClusterServer(clusterFinder, *fTransformer);
605 catch (const std::bad_alloc&)
607 HLTError("Could not allocate more memory for a offline reconstruction object.");
608 if (calibrator != NULL) delete calibrator;
609 if (clusterFinder != NULL) delete clusterFinder;
610 if (clusterServer != NULL) delete clusterServer;
614 if (fDigitCalibrator != NULL) delete fDigitCalibrator;
615 fDigitCalibrator = calibrator;
616 if (fClusterFinder != NULL) delete fClusterFinder;
617 fClusterFinder = clusterFinder;
618 if (fClusterServer != NULL) delete fClusterServer;
619 fClusterServer = clusterServer;