3 //**************************************************************************
4 //* This file is property of and copyright by the ALICE HLT Project *
5 //* ALICE Experiment at CERN, All rights reserved. *
7 //* Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no> *
8 //* for The ALICE HLT Project. *
10 //* Permission to use, copy, modify and distribute this software and its *
11 //* documentation strictly for non-commercial purposes is hereby granted *
12 //* without fee, provided that the above copyright notice appears in all *
13 //* copies and that both the copyright notice and this permission notice *
14 //* appear in the supporting documentation. The authors make no claims *
15 //* about the suitability of this software for any purpose. It is *
16 //* provided "as is" without express or implied warranty. *
17 //**************************************************************************
19 /** @file AliHLTBenchExternalTrackComponent.cxx
20 @author Matthias Richter
22 @brief Benchmark component for AliExternalTrackParam transportation.
25 // see header file for class documentation
27 // refer to README to build package
29 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
32 #include "AliHLTBenchExternalTrackComponent.h"
33 #include "AliExternalTrackParam.h"
34 #include "AliHLTExternalTrackParam.h"
39 #include "TObjArray.h"
40 #include "TClonesArray.h"
44 /** global object for component registration */
45 AliHLTBenchExternalTrackComponent gAliHLTBenchExternalTrackComponent;
47 /** ROOT macro for the implementation of ROOT specific class methods */
48 ClassImp(AliHLTBenchExternalTrackComponent)
50 AliHLTBenchExternalTrackComponent::AliHLTBenchExternalTrackComponent()
53 , fDisableRegistry(false)
54 , fMode(kPublishingOff)
59 , fRangeMultiplicator(1.0)
64 // see header file for class documentation
66 // refer to README to build package
68 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
71 AliHLTBenchExternalTrackComponent::~AliHLTBenchExternalTrackComponent()
73 // see header file for class documentation
76 const char* AliHLTBenchExternalTrackComponent::GetComponentID()
78 // see header file for class documentation
79 return "BenchmarkAliExternalTrackParam";
82 void AliHLTBenchExternalTrackComponent::GetInputDataTypes(AliHLTComponentDataTypeList& list)
84 // see header file for class documentation
86 list.push_back(kAliHLTAnyDataType);
89 AliHLTComponentDataType AliHLTBenchExternalTrackComponent::GetOutputDataType()
91 // see header file for class documentation
92 return kAliHLTMultipleDataType;
95 int AliHLTBenchExternalTrackComponent::GetOutputDataTypes(AliHLTComponentDataTypeList& tgtList)
97 // see header file for class documentation
99 tgtList.push_back(kAliHLTDataTypeTObject);
100 return tgtList.size();
103 void AliHLTBenchExternalTrackComponent::GetOutputDataSize(unsigned long& constBase, double& inputMultiplier)
105 // see header file for class documentation
106 constBase=sizeof(AliHLTExternalTrackParam)*fMaxSize;
110 AliHLTComponent* AliHLTBenchExternalTrackComponent::Spawn()
112 // see header file for class documentation
113 return new AliHLTBenchExternalTrackComponent;
116 int AliHLTBenchExternalTrackComponent::DoInit(int argc, const char** argv)
118 // see header file for class documentation
121 bool bMissingParam=0;
124 for (; i<argc && iResult>=0; i++) {
127 if (argument.IsNull()) continue;
130 if (argument.CompareTo("-tobjarray")==0) {
134 } else if (argument.CompareTo("-tclonesarray")==0) {
138 } else if (argument.CompareTo("-carray")==0) {
142 } else if (argument.CompareTo("-maxsize")==0) {
143 if ((bMissingParam=(++i>=argc))) break;
144 fMaxSize=strtoul( argv[i], &cpErr ,0);
148 } else if (argument.CompareTo("-minsize")==0) {
149 if ((bMissingParam=(++i>=argc))) break;
150 fMinSize=strtoul( argv[i], &cpErr ,0);
154 } else if (argument.CompareTo("-rangemodulo")==0) {
155 if ((bMissingParam=(++i>=argc))) break;
156 fEventModulo=strtoul( argv[i], &cpErr ,0);
160 } else if (argument.CompareTo("-rangeoffset")==0) {
161 if ((bMissingParam=(++i>=argc))) break;
162 fRangeOffset=strtoul( argv[i], &cpErr ,0);
166 } else if (argument.CompareTo("-rangefactor")==0) {
167 if ((bMissingParam=(++i>=argc))) break;
168 fRangeMultiplicator=strtof( argv[i], &cpErr);
172 } else if (argument.CompareTo("-verbosity")==0) {
173 if ((bMissingParam=(++i>=argc))) break;
174 fVerbosity=strtoul( argv[i], &cpErr ,0);
178 } else if (argument.CompareTo("-nocheck")==0) {
179 fDisableRegistry=true;
182 HLTError("unknown argument %s", argument.Data());
187 if (cpErr && *cpErr) {
188 HLTError("Cannot convert specifier '%s' for argument '%s'", argv[i], argument.Data());
190 } else if (bMissingParam) {
191 HLTError("missing parameter for argument %s", argument.Data());
195 // use a TClonesArray internally and for publishing in case of
196 // ktclonesarray. The maximum size and all members are allocated
197 fpTcArray=new TClonesArray("AliExternalTrackParam", fMaxSize);
199 fpTcArray->ExpandCreate(fMaxSize);
209 // use a TObjArray which owns the objects
210 fpTObjArray=new TObjArray;
216 HLTError("unknown publishing mode %d", fMode);
227 fpDice->SetSeed(dt.Get());
236 int AliHLTBenchExternalTrackComponent::DoDeinit()
238 // see header file for class documentation
239 if (fpTObjArray) delete fpTObjArray;
248 if (fpDice) delete fpDice;
254 int AliHLTBenchExternalTrackComponent::DoEvent(const AliHLTComponentEventData& /*evtData*/,
255 const AliHLTComponentBlockData* /*blocks*/,
256 AliHLTComponentTriggerData& /*trigData*/,
257 AliHLTUInt8_t* outputPtr,
258 AliHLTUInt32_t& size,
259 AliHLTComponentBlockDataList& outputBlocks)
261 // see header file for class documentation
263 // scan the input data blocks for types kAliHLTDataTypeTrack and
264 // kAliHLTDataTypeTObjArray with AliExternalTrackParam objects
266 // If data is available, the array is extracted into the internal
267 // TClonesArray fpTcArray. If no input data is available, the array
268 // is filled randomly, both in size and content.
270 // The array is published according to the fMode member either as
271 // TClonesArray, TObjArray or C-structure (AliHLTExternalTrackParam)
274 AliHLTUInt32_t capacity=size;
276 if (!IsDataEvent()) return 0;
277 if (!fpTcArray || !fpDice) return -ENODEV;
279 unsigned int arraySize=0;
280 const AliHLTComponentBlockData* pBlock=NULL;
281 const TObject* pObject=NULL;
282 const TObjArray* pObjArray=NULL;
283 AliHLTUInt32_t spec=0;
284 if ((pBlock=GetFirstInputBlock(kAliHLTDataTypeTrack))!=NULL) {
285 if (pBlock->fSize%sizeof(AliHLTExternalTrackParam)==0) {
286 arraySize=pBlock->fSize/sizeof(AliHLTExternalTrackParam);
288 spec=GetSpecification(pBlock);
289 fpTcArray->ExpandCreate(arraySize);
290 if ((iResult=ReadFromStruct(fpTcArray, reinterpret_cast<AliHLTExternalTrackParam*>(pBlock->fPtr), arraySize))<0) {
291 HLTError("can not convert struct to TObjArray, error %d", iResult);
294 HLTDebug("created TObjArray from C struct, specification 0x%08x", spec);
300 HLTWarning("data block does not match the AliHLTExternalTrackParam struct size");
302 } else if ((pObject=GetFirstInputObject(kAliHLTDataTypeTObjArray))!=NULL) {
303 pObjArray=dynamic_cast<const TObjArray*>(pObject);
305 spec=GetSpecification(pObject);
306 HLTDebug("extracted TObjArray, specification 0x%08x", spec);
308 HLTWarning("object of data type %s is not of type TObjArray, ignoring it",
309 DataType2Text(GetDataType()).c_str());
314 // we already have the object array, if publishing mode is >0 the
315 // block is forwarded according to the mode
317 // if specification is >0 the source object is searched in the list
318 // and compared with the extracted one.
320 TObject* pSrcObject=FindObject(spec);
321 TObjArray* pSrcArray=NULL;
323 HLTError("can not find source object of id 0x%08x", spec);
324 } else if ((pSrcArray=dynamic_cast<TObjArray*>(pSrcObject))==NULL) {
325 HLTError("type cast failed for object");
326 } else if (!Compare(pObjArray, pSrcArray)) {
327 HLTError("extracted array differs from source array");
329 for (int i=0; i<pObjArray->GetEntries(); i++) {
330 pObjArray->At(i)->Print();
332 HLTError("dump of original array");
333 for (int i=0; i<pSrcArray->GetEntries(); i++) {
334 pSrcArray->At(i)->Print();
339 HLTInfo("extracted array of size %d", pObjArray->GetEntries());
344 // no array to forward, create a new one
345 arraySize=fpDice->Integer(fMaxSize-fMinSize)+fMinSize;
346 fpTcArray->ExpandCreate(arraySize);
347 for (unsigned int track=0; track<arraySize; track++) {
348 FillRandom(reinterpret_cast<AliExternalTrackParam*>(fpTcArray->At(track)));
357 // register the object if it is not a forwarded array
358 if (spec==0 && pObjArray==fpTcArray) spec=Register(fpTcArray);
359 if ((iResult=PushBack(const_cast<TObjArray*>(pObjArray), kAliHLTDataTypeTObjArray, spec))>=0) {
361 HLTInfo("publishing TClonesarray, %d elements, specification 0x%08x", pObjArray->GetEntries(), spec);
366 iResult=SerializeToStruct(pObjArray, outputPtr, capacity);
369 AliHLTComponentBlockData bd;
374 bd.fDataType=kAliHLTDataTypeTrack;
375 // register the object if it is not a forwarded array
376 if (spec==0 && pObjArray==fpTcArray) spec=Register(fpTcArray);
377 bd.fSpecification=spec;
378 outputBlocks.push_back(bd);
380 HLTInfo("publishing C array, %d elements, specification 0x%08x", bd.fSize/sizeof(AliHLTExternalTrackParam), bd.fSpecification);
385 // use a TObjArray which owns the objects
387 fpTObjArray->Clear();
388 int entries=pObjArray->GetEntries();
389 for (int i=0; i<entries; i++) {
390 fpTObjArray->Add(pObjArray->At(i));
393 // register the object if it is not a forwarded array
394 if (spec==0 && pObjArray==fpTcArray) spec=Register(fpTObjArray);
395 if ((iResult=PushBack(fpTObjArray, kAliHLTDataTypeTObjArray, spec))>=0) {
397 HLTInfo("publishing TObjArray, %d elements, specification 0x%08x", pObjArray->GetEntries(), spec);
401 HLTError("object array not initialized");
406 HLTError("unknown publishing mode %d", fMode);
410 if (fEventModulo>0 &&
411 ((GetEventCount()+1)%fEventModulo)==0 &&
412 (fRangeMultiplicator!=1.0 || fRangeOffset!=0)) {
413 fMaxSize+=fRangeOffset;
414 fMaxSize=(int)(fMaxSize*fRangeMultiplicator);
415 fMinSize+=fRangeOffset;
416 fMinSize=(int)(fMinSize*fRangeMultiplicator);
417 if (fMaxSize<1) fMaxSize=1;
418 if (fMinSize<1) fMinSize=1;
423 int AliHLTBenchExternalTrackComponent::FillRandom(AliExternalTrackParam* track, int fillCov)
425 // see header file for class documentation
427 if (!track) return -EINVAL;
431 static TRandom* rand=NULL;
434 if (!rand) return -ENOMEM;
436 rand->SetSeed(dt.Get());
439 param[0]=(Double_t)rand->Integer(100);
440 param[1]=(Double_t)rand->Integer(100);
441 param[2]=(Double_t)rand->Integer(100)/100;
442 param[3]=(Double_t)rand->Integer(100)/100;
443 param[4]=(Double_t)rand->Integer(100);
444 for (int i=0; i<15 && i<fillCov; i++) {
445 covar[i]=(Double_t)rand->Integer(1000)/1000;
447 track->Set((Double_t)rand->Integer(1000),(Double_t)rand->Integer(100)/100, param, covar );
451 int AliHLTBenchExternalTrackComponent::SerializeToStruct(const TObjArray* pArray, AliHLTUInt8_t* buffer, unsigned int size)
453 // see header file for class documentation
455 if (!pArray || !buffer) return -EINVAL;
456 int entries=pArray->GetEntries();
457 if (entries*sizeof(AliHLTExternalTrackParam)>size) return -ENOSPC;
460 AliHLTExternalTrackParam* tgtParam=reinterpret_cast<AliHLTExternalTrackParam*>(buffer);
461 for (int track=0; track<entries; track++, tgtParam++) {
462 TObject* pObj=pArray->At(track);
464 log.LoggingVarargs(kHLTLogError, "AliHLTBenchExternalTrackComponent", "SerializeToStruct" , __FILE__ , __LINE__ ,
465 "internal mismatch: can not get object %d from array. aborting", track);
469 AliExternalTrackParam* srcParam=dynamic_cast<AliExternalTrackParam*>(pObj);
471 log.LoggingVarargs(kHLTLogError, "AliHLTBenchExternalTrackComponent", "SerializeToStruct" , __FILE__ , __LINE__ ,
472 "object %d has wrong type %s, expecting %s", track, pObj->Class()->GetName(), "AliExternalTrackParam");
477 //= srcParam->GetAlpha();
478 tgtParam->fX = srcParam->GetX();
479 tgtParam->fY = srcParam->GetY();
480 tgtParam->fZ = srcParam->GetZ();
484 tgtParam->fSinPsi = srcParam->GetSnp();
485 tgtParam->fTgl = srcParam->GetTgl();
486 tgtParam->fq1Pt = srcParam->GetSigned1Pt();
487 const Double_t *cov=srcParam->GetCovariance();
489 for (int i=0; i<15; i++) {
490 tgtParam->fC[i]=cov[i];
492 tgtParam->fNPoints=0;
496 iResult=entries*sizeof(AliHLTExternalTrackParam);
502 int AliHLTBenchExternalTrackComponent::ReadFromStruct(TObjArray* pTgtArray, AliHLTExternalTrackParam* pArray, unsigned int arraySize)
504 // see header file for class documentation
506 if ((unsigned)pTgtArray->GetEntries()<arraySize) {
507 log.LoggingVarargs(kHLTLogError, "AliHLTBenchExternalTrackComponent", "ReadFromStruct" , __FILE__ , __LINE__ ,
508 "not enough space in target array: %d, required %d", pTgtArray->GetEntries(), arraySize);
515 for (unsigned int track=0; track<arraySize; track++) {
516 AliExternalTrackParam* tp=dynamic_cast<AliExternalTrackParam*>(pTgtArray->At(track));
518 // enable if AliExternalTrackParam allows templates
519 //tp->Set(pArray[track].fX, (Float_t)0.0, &pArray[track].fY, pArray[track].fC);
520 param[0]=pArray[track].fY;
521 param[1]=pArray[track].fZ;
522 param[2]=pArray[track].fSinPsi;
523 param[3]=pArray[track].fTgl;
524 param[4]=pArray[track].fq1Pt;
525 for (int i=0; i<15; i++) {
526 covar[i]=pArray[track].fC[i];
528 tp->Set((Double_t)pArray[track].fX, (Double_t)0.0, param, covar);
530 log.LoggingVarargs(kHLTLogError, "AliHLTBenchExternalTrackComponent", "ReadFromStruct" , __FILE__ , __LINE__ ,
531 "invalid object type %s", pTgtArray->At(track)->Class()->GetName());
538 AliHLTUInt32_t AliHLTBenchExternalTrackComponent::CalcChecksum(const TObjArray* pArray)
540 // see header file for class documentation
541 AliHLTUInt32_t crc=0;
543 if (pArray && (entries=pArray->GetEntries())>0) {
545 unsigned int bufferSize=entries*sizeof(AliHLTExternalTrackParam);
546 AliHLTUInt8_t* buffer=new AliHLTUInt8_t[bufferSize];
547 if (buffer && SerializeToStruct(pArray, buffer, bufferSize)>0) {
548 crc=CalculateChecksum(buffer, bufferSize);
550 log.LoggingVarargs(kHLTLogError, "AliHLTBenchExternalTrackComponent", "SerializeToStruct" , __FILE__ , __LINE__ ,
551 "failed to serialize TObjArray");
557 bool AliHLTBenchExternalTrackComponent::Compare(const TObjArray* array1, const TObjArray* array2)
559 // see header file for class documentation
560 if (!array1 || !array2) return false;
561 int entries=array1->GetEntries();
562 if (entries!=array2->GetEntries()) {
566 for (int i=0; i<entries; i++) {
567 TObject* object1=array1->At(i);
568 TObject* object2=array2->At(i);
569 if (!object1 || !object2) return false;
571 AliExternalTrackParam* param1=dynamic_cast<AliExternalTrackParam*>(object1);
572 AliExternalTrackParam* param2=dynamic_cast<AliExternalTrackParam*>(object2);
573 if (!param1 || !param2) return false;
575 if (TMath::Abs(param1->GetX()-param2->GetX())>0.0001) return false;
576 if (TMath::Abs(param1->GetY()-param2->GetY())>0.0001) return false;
577 if (TMath::Abs(param1->GetZ()-param2->GetZ())>0.0001) return false;
578 if (TMath::Abs(param1->GetSnp()-param2->GetSnp())>0.0001) return false;
579 if (TMath::Abs(param1->GetTgl()-param2->GetTgl())>0.0001) return false;
580 if (TMath::Abs(param1->GetSigned1Pt()-param2->GetSigned1Pt())>0.0001) return false;
582 if (TMath::Abs(param1->GetSigmaY2()-param2->GetSigmaY2())>0.0001) return false;
583 if (TMath::Abs(param1->GetSigmaZY()-param2->GetSigmaZY())>0.0001) return false;
584 if (TMath::Abs(param1->GetSigmaZ2()-param2->GetSigmaZ2())>0.0001) return false;
585 if (TMath::Abs(param1->GetSigmaSnpY()-param2->GetSigmaSnpY())>0.0001) return false;
586 if (TMath::Abs(param1->GetSigmaSnpZ()-param2->GetSigmaSnpZ())>0.0001) return false;
587 if (TMath::Abs(param1->GetSigmaSnp2()-param2->GetSigmaSnp2())>0.0001) return false;
588 if (TMath::Abs(param1->GetSigmaTglY()-param2->GetSigmaTglY())>0.0001) return false;
589 if (TMath::Abs(param1->GetSigmaTglZ()-param2->GetSigmaTglZ())>0.0001) return false;
590 if (TMath::Abs(param1->GetSigmaTglSnp()-param2->GetSigmaTglSnp())>0.0001) return false;
591 if (TMath::Abs(param1->GetSigmaTgl2()-param2->GetSigmaTgl2())>0.0001) return false;
592 if (TMath::Abs(param1->GetSigma1PtY()-param2->GetSigma1PtY())>0.0001) return false;
593 if (TMath::Abs(param1->GetSigma1PtZ()-param2->GetSigma1PtZ())>0.0001) return false;
594 if (TMath::Abs(param1->GetSigma1PtSnp()-param2->GetSigma1PtSnp())>0.0001) return false;
595 if (TMath::Abs(param1->GetSigma1PtTgl()-param2->GetSigma1PtTgl())>0.0001) return false;
596 if (TMath::Abs(param1->GetSigma1Pt2()-param2->GetSigma1Pt2())>0.0001) return false;
602 TList* AliHLTBenchExternalTrackComponent::fgpRegistry=NULL;
604 TObject* AliHLTBenchExternalTrackComponent::FindObject(AliHLTUInt32_t id)
606 // see header file for class documentation
607 if (!fgpRegistry) return NULL;
609 TIter iter(fgpRegistry);
611 while ((pObj=iter.Next())) {
612 if (pObj->GetUniqueID()==id) break;
617 AliHLTUInt32_t AliHLTBenchExternalTrackComponent::Register(TObject* pObject)
619 // see header file for class documentation
620 if (fDisableRegistry) return 0;
623 fgpRegistry=new TList;
625 if (!fgpRegistry) return 0;
627 TObject* pExist=fgpRegistry->FindObject(pObject);
628 if (pExist) return pExist->GetUniqueID();
630 AliHLTUInt32_t id=AliHLTComponent::CalculateChecksum(reinterpret_cast<AliHLTUInt8_t*>(&pObject), sizeof(TObject*));
631 pExist=FindObject(id);
632 assert(pExist==NULL);
633 pObject->SetUniqueID(id);
634 fgpRegistry->Add(pObject);
635 HLTInfo("adding object %p with id 0x%08x to registry", pObject, id);
639 int AliHLTBenchExternalTrackComponent::Unregister(TObject* pObject)
641 // see header file for class documentation
642 if (!fgpRegistry) return 0;
644 if (fgpRegistry->Remove(pObject)==pObject) return 0;