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 AliHLTTPCClusterAccessHLTOUT.h
20 /// @author Matthias Richter
22 /// @brief Interface to HLT TPC clusters
25 #include "AliHLTTPCClusterAccessHLTOUT.h"
26 #include "AliHLTTPCDataCompressionDecoder.h"
27 #include "AliHLTTPCDefinitions.h"
28 #include "AliHLTTPCClusterDataFormat.h"
29 #include "AliHLTTPCRawCluster.h"
30 #include "AliHLTTPCTransform.h"
31 #include "AliHLTOUT.h"
32 #include "AliHLTComponent.h"
33 #include "AliHLTErrorGuard.h"
34 #include "AliHLTDataInflater.h"
35 #include "AliHLTTPCDefinitions.h"
37 #include "AliHLTSystem.h"
38 #include "AliHLTPluginBase.h"
39 #include "AliTPCclusterMI.h"
40 #include "TClonesArray.h"
47 /** ROOT macro for the implementation of ROOT specific class methods */
48 ClassImp(AliHLTTPCClusterAccessHLTOUT)
50 AliHLTTPCClusterAccessHLTOUT::AliHLTTPCClusterAccessHLTOUT()
56 // see header file for class documentation
58 // refer to README to build package
60 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
63 AliHLTTPCClusterAccessHLTOUT::~AliHLTTPCClusterAccessHLTOUT()
73 void AliHLTTPCClusterAccessHLTOUT::Execute(const char *method, const char *params, Int_t *error)
75 /// inherited from TObject: abstract command interface
76 if (strcmp(method, "read")==0) {
77 int iResult=ProcessClusters(params);
78 if (error) *error=iResult;
81 if (strcmp(method, "verbosity")==0) {
85 int value=strtol(params, &dummy, 0);
89 AliError("invalid argument for command 'verbosity', expecting string with number");
94 if (error) *error=iResult;
99 TObject* AliHLTTPCClusterAccessHLTOUT::FindObject(const char *name) const
101 /// inherited from TObject: return the cluster array if name id "clusterarray"
102 if (strcmp(name, "clusterarray")==0) {
103 if (fCurrentSector<0) return NULL;
104 return fClusters->GetSectorArray(fCurrentSector);
106 return TObject::FindObject(name);
109 void AliHLTTPCClusterAccessHLTOUT::Clear(Option_t * option)
111 /// inherited from TObject: cleanup
112 if (strcmp(option, "event")==0) {
113 if (fClusters) fClusters->Clear();
118 void AliHLTTPCClusterAccessHLTOUT::Print(Option_t *option) const
120 /// inherited from TObject
121 if (fClusters) fClusters->Print(option);
124 int AliHLTTPCClusterAccessHLTOUT::ProcessClusters(const char* params)
126 /// process the cluster data from HLTOUT and fill array
127 /// the cluster data can be in many different formats, e.g.
128 /// raw or compressed
130 TString strparams(params);
132 std::auto_ptr<TObjArray> tokens(strparams.Tokenize(" "));
133 if (!tokens.get()) return -ENOMEM;
134 for (int i=0; i< tokens->GetEntriesFast(); i++) {
135 if (!tokens->At(i)) continue;
136 TString argument=tokens->At(i)->GetName();
137 // the offline code enumerates first the 36 inner (partitions 0+1) and then 36 outer
138 // sectors (partitions 2-5)
139 if (argument.BeginsWith("sector=")) {
140 argument.ReplaceAll("sector=", "");
141 sector=argument.Atoi();
145 AliError("invalid argument, please specify \"sector=sectorno\"");
149 AliError(Form("invalid sector number %d", sector));
154 fClusters=new AliTPCclusterMIContainer;
156 if (!fClusters) return -ENOMEM;
158 if (fCurrentSector>=0) {
159 // cluster container already filled
160 fCurrentSector=sector;
161 TObjArray* pArray=fClusters->GetSectorArray(fCurrentSector);
163 AliError(Form("can not get cluster array for sector %d", sector));
166 if (fVerbosity>0) AliInfo(Form("converted %d cluster(s) for sector %d", pArray->GetEntriesFast() ,sector));
167 return pArray->GetEntriesFast();
170 // fill the cluster container
171 AliHLTSystem* pSystem=AliHLTPluginBase::GetInstance();
173 AliError("can not access HLT system");
176 AliHLTOUT* pHLTOUT=pSystem->RequestHLTOUT();
178 AliError("can not access HLTOUT");
182 bool bNextBlock=false;
183 bool bHaveLabels=false;
185 // add cluster id and mc information data blocks
186 for (bNextBlock=(pHLTOUT->SelectFirstDataBlock()>=0);
187 bNextBlock; bNextBlock=(pHLTOUT->SelectNextDataBlock()>=0)) {
188 AliHLTComponentBlockData desc;
189 // FIXME: extend HLTOUT to get the full descriptor
190 const AliHLTUInt8_t* buffer=NULL;
191 if ((iResult=pHLTOUT->GetDataBuffer(buffer, desc.fSize))<0) {
194 desc.fPtr=(void*)buffer;
195 if (pHLTOUT->GetDataBlockDescription(desc.fDataType, desc.fSpecification)<0) {
198 if (desc.fDataType==AliHLTTPCDefinitions::AliHLTDataTypeClusterMCInfo()) {
199 // add mc information
200 if ((iResult=fClusters->AddClusterMCData(&desc))<0) {
205 if (desc.fDataType==AliHLTTPCDefinitions::RemainingClusterIdsDataType() ||
206 desc.fDataType==AliHLTTPCDefinitions::ClusterIdTracksDataType()) {
208 if ((iResult=fClusters->AddClusterIds(&desc))<0) {
215 bool bHavePartitionRawData=false;
216 bool bHavePartitionCompressedData=false;
217 vector<bool> bHavePartitionData(216, false);
221 AliHLTTPCDataCompressionDecoder decoder;
222 decoder.SetVerbosity(fVerbosity);
223 int nExtractedClusters=0;
224 for (bNextBlock=(pHLTOUT->SelectFirstDataBlock()>=0);
225 bNextBlock; bNextBlock=(pHLTOUT->SelectNextDataBlock()>=0)) {
226 decoder.SetPadShift(0.0);
227 AliHLTComponentBlockData desc;
228 // FIXME: extend HLTOUT to get the full descriptor with one call
229 const AliHLTUInt8_t* buffer=NULL;
230 if ((iResult=pHLTOUT->GetDataBuffer(buffer, desc.fSize))<0) {
233 desc.fPtr=(void*)buffer;
234 if (pHLTOUT->GetDataBlockDescription(desc.fDataType, desc.fSpecification)<0) {
237 if (!TestBit(kSkipPartitionClusters) &&
238 (desc.fDataType==AliHLTTPCDefinitions::RawClustersDataType())) {
239 // This is a special handling of data blocks produced with v5-01-Release
240 // The pad shift by 0.5 was not included in the data but was applied in the
241 // unpacking in this class. Changed in r51306, the next tag containing this
242 // change in the online system is v5-01-Rev-07. There are only very few runs
243 // of Sep 2011 with recorded clusters not containing the 0.5 shift
244 // There was also a chenge in the data type of the compressed partition
245 // cluster blocks which helps to identify the blocks which need the pad shift
247 if (desc.fSize<sizeof(AliHLTTPCRawClusterData)) continue;
248 const AliHLTTPCRawClusterData* clusterData = reinterpret_cast<const AliHLTTPCRawClusterData*>(buffer);
249 if (!clusterData) continue;
250 if (clusterData->fVersion==1) {
251 // compressed clusters without the pad shift
252 // no raw clusters (version==0) have ever been recorded
253 decoder.SetPadShift(0.5);
255 AliHLTUInt8_t slice = AliHLTTPCDefinitions::GetMinSliceNr(desc.fSpecification);
256 AliHLTUInt8_t partition = AliHLTTPCDefinitions::GetMinPatchNr(desc.fSpecification);
257 if (slice!=AliHLTTPCDefinitions::GetMaxSliceNr(desc.fSpecification) ||
258 partition!=AliHLTTPCDefinitions::GetMaxPatchNr(desc.fSpecification)) {
259 AliFatal(Form("inconsistent cluster data: can not handle blocks containing multiple partitions, "
260 "block specification 0x%08x", desc.fSpecification));
262 iResult=decoder.ReadClustersPartition(fClusters->BeginRemainingClusterBlock(0, desc.fSpecification),
263 reinterpret_cast<AliHLTUInt8_t*>(desc.fPtr),
265 desc.fSpecification);
266 if (iResult>0) nExtractedClusters+=iResult;
267 unsigned index=slice*AliHLTTPCTransform::GetNumberOfPatches()+partition;
268 if (index>=bHavePartitionData.size()) bHavePartitionData.resize(index, false);
269 if (bHavePartitionData[index]) {
270 AliFatal(Form("inconsistent cluster data: multiple data blocks of identical specification indicate a failure "
271 "in the production of the data. Probably an HLT emulation chain is executed in the reconstruction "
272 "and produces data in addition to HLTOUT. Option 'ignore-hltout' is required in that case; "
273 "block specification 0x%08x", desc.fSpecification));
275 bHavePartitionData[index]=true;
276 if (bHavePartitionCompressedData) {
277 AliFatal(Form("inconsistent cluster data: both compressed and raw cluster blocks present in HLTOUT, indicates a failure "
278 "in the production of the data. Probably an HLT emulation chain is executed in the reconstruction "
279 "and produces data in addition to HLTOUT. Option 'ignore-hltout' is required in that case; "
280 "block specification 0x%08x", desc.fSpecification));
282 bHavePartitionRawData=true;
284 } else if (!TestBit(kSkipPartitionClusters) &&
285 (desc.fDataType==AliHLTTPCDefinitions::RemainingClustersCompressedDataType())) {
286 AliHLTUInt8_t slice = AliHLTTPCDefinitions::GetMinSliceNr(desc.fSpecification);
287 AliHLTUInt8_t partition = AliHLTTPCDefinitions::GetMinPatchNr(desc.fSpecification);
288 if (slice!=AliHLTTPCDefinitions::GetMaxSliceNr(desc.fSpecification) ||
289 partition!=AliHLTTPCDefinitions::GetMaxPatchNr(desc.fSpecification)) {
290 AliFatal(Form("inconsistent cluster data: can not handle blocks containing multiple partitions, "
291 "block specification 0x%08x", desc.fSpecification));
293 iResult=decoder.ReadClustersPartition(fClusters->BeginRemainingClusterBlock(0, desc.fSpecification),
294 reinterpret_cast<AliHLTUInt8_t*>(desc.fPtr),
296 desc.fSpecification);
297 if (iResult>0) nExtractedClusters+=iResult;
298 unsigned index=slice*AliHLTTPCTransform::GetNumberOfPatches()+partition;
299 if (index>=bHavePartitionData.size()) bHavePartitionData.resize(index, false);
300 if (bHavePartitionData[index]) {
301 AliFatal(Form("inconsistent cluster data: multiple data blocks of identical specification indicate a failure "
302 "in the production of the data. Probably an HLT emulation chain is executed in the reconstruction "
303 "and produces data in addition to HLTOUT. Option 'ignore-hltout' is required in that case; "
304 "block specification 0x%08x", desc.fSpecification));
306 bHavePartitionData[index]=true;
307 bHavePartitionData[index]=true;
308 if (bHavePartitionRawData) {
309 AliFatal(Form("inconsistent cluster data: both compressed and raw cluster blocks present in HLTOUT, indicates a failure "
310 "in the production of the data. Probably an HLT emulation chain is executed in the reconstruction "
311 "and produces data in addition to HLTOUT. Option 'ignore-hltout' is required in that case; "
312 "block specification 0x%08x", desc.fSpecification));
314 bHavePartitionCompressedData=true;
316 } else if (!TestBit(kSkipTrackClusters) &&
317 desc.fDataType==AliHLTTPCDefinitions::ClusterTracksCompressedDataType()) {
318 iResult=decoder.ReadTrackModelClustersCompressed(fClusters->BeginTrackModelClusterBlock(0),
319 reinterpret_cast<AliHLTUInt8_t*>(desc.fPtr),
321 desc.fSpecification);
326 pSystem->ReleaseHLTOUT(pHLTOUT);
328 if (iResult<0) return iResult;
330 int nConvertedClusters=0;
331 for (int s=0; s<72; s++) {
332 TObjArray* pArray=fClusters->GetSectorArray(s);
333 if (!pArray) continue;
334 nConvertedClusters+=pArray->GetEntriesFast();
336 AliInfo(Form("extracted HLT clusters: %d, converted HLT clusters: %d", nExtractedClusters, nConvertedClusters));
339 fCurrentSector=sector;
340 TObjArray* pArray=fClusters->GetSectorArray(fCurrentSector);
342 AliError(Form("can not get cluster array for sector %d", sector));
345 if (fVerbosity>0) AliInfo(Form("converted %d cluster(s) for sector %d", pArray->GetEntriesFast() ,sector));
346 return pArray->GetEntriesFast();
349 int AliHLTTPCClusterAccessHLTOUT::ReadAliHLTTPCClusterMCData(AliHLTOUT* pHLTOUT, AliHLTTPCClusterMCDataList &tpcClusterLabels) const
351 // read cluster data from AliHLTTPCClusterData
353 if (!pHLTOUT) return -EINVAL;
355 const AliHLTUInt8_t* pBuffer=NULL;
356 AliHLTUInt32_t size=0;
357 if ((iResult=pHLTOUT->GetDataBuffer(pBuffer, size))<0) {
360 if (pBuffer==NULL || size<4) {
361 AliError("invalid cluster mc data block");
364 const AliHLTTPCClusterMCData* clusterMCData = reinterpret_cast<const AliHLTTPCClusterMCData*>(pBuffer);
365 Int_t nLabels = (Int_t) clusterMCData->fCount;
366 if (nLabels*sizeof(AliHLTTPCClusterMCLabel) + sizeof(AliHLTTPCClusterMCData) != size) {
367 AliError("inconsistent cluster mc data block size, skipping block");
370 // id of the cluster is
371 AliHLTComponentDataType dt=kAliHLTVoidDataType;
372 AliHLTUInt32_t specification=kAliHLTVoidDataSpec;
373 if (pHLTOUT->GetDataBlockDescription(dt, specification)<0) {
374 AliError("failed to retrieve data block description, skipping mc cluster data block ...");
377 AliHLTUInt8_t slice = AliHLTTPCDefinitions::GetMinSliceNr(specification);
378 AliHLTUInt8_t partition = AliHLTTPCDefinitions::GetMinPatchNr(specification);
379 if (slice!=AliHLTTPCDefinitions::GetMaxSliceNr(specification) ||
380 partition!=AliHLTTPCDefinitions::GetMaxPatchNr(specification)) {
381 AliError(Form("can not read cluster mc data block with data of multiple partitions, skipping block %s %08x",
382 AliHLTComponent::DataType2Text(dt).c_str(), specification));
385 const AliHLTTPCClusterMCLabel *labels = clusterMCData->fLabels;
386 for (int i=0; i<nLabels; i++) {
387 AliHLTUInt32_t id=AliHLTTPCSpacePointData::GetID(slice, partition, i);
388 if (tpcClusterLabels.find(id)==tpcClusterLabels.end()) {
390 tpcClusterLabels[id]=labels[i];
392 AliError(Form("cluster with ID 0x%08x already existing, skipping cluster %d of data block 0x%08x",
393 id, i, specification));
396 } while (pHLTOUT->SelectNextDataBlock()>=0);
400 int AliHLTTPCClusterAccessHLTOUT::ReadAliHLTTPCClusterData(AliHLTOUT* pHLTOUT, TClonesArray* pClusters, const AliHLTTPCClusterMCDataList *tpcClusterLabels) const
402 // read cluster data from AliHLTTPCClusterData
404 if (!pHLTOUT || !pClusters) return -EINVAL;
406 const AliHLTUInt8_t* pBuffer=NULL;
407 AliHLTUInt32_t size=0;
408 if ((iResult=pHLTOUT->GetDataBuffer(pBuffer, size))<0) {
411 if (pBuffer==NULL || size<4) {
412 AliError("invalid cluster data block");
415 AliHLTComponentDataType dt=kAliHLTVoidDataType;
416 AliHLTUInt32_t specification=kAliHLTVoidDataSpec;
417 if (pHLTOUT->GetDataBlockDescription(dt, specification)<0) {
418 AliError("failed to retrieve data block description, skipping mc cluster data block ...");
421 const AliHLTTPCClusterData* clusterData = reinterpret_cast<const AliHLTTPCClusterData*>(pBuffer);
422 Int_t nSpacepoints = (Int_t) clusterData->fSpacePointCnt;
423 if (nSpacepoints*sizeof(AliHLTTPCSpacePointData) + sizeof(AliHLTTPCClusterData) != size) {
424 AliError("inconsistent cluster data block size, skipping block");
427 const AliHLTTPCSpacePointData *clusters = clusterData->fSpacePoints;
428 int offset=pClusters->GetEntries();
429 pClusters->ExpandCreate(offset+nSpacepoints);
430 AliHLTUInt8_t slice = AliHLTTPCDefinitions::GetMinSliceNr(specification);
431 AliHLTUInt8_t partition = AliHLTTPCDefinitions::GetMinPatchNr(specification);
432 // FIXME: get first row number of outer sectors from a common definition instead using number
433 unsigned rowOffset=partition<2?0:63;
434 for (int i=0; i<nSpacepoints; i++) {
435 if (!pClusters->At(offset+i)) continue;
436 AliTPCclusterMI* pCluster=dynamic_cast<AliTPCclusterMI*>(pClusters->At(offset+i));
438 AliError("invalid object type, expecting AliTPCclusterMI");
439 break; // this is a problem of all objects
441 if (clusters[i].fPadRow<rowOffset) {
442 AliError(Form("invalid row number %d, expecting minimum row number %d for slice %d partition %d", clusters[i].fPadRow, rowOffset, slice, partition));
444 pCluster->SetRow(clusters[i].fPadRow-rowOffset);
446 pCluster->SetPad(clusters[i].fY);
447 pCluster->SetTimeBin(clusters[i].fZ);
448 pCluster->SetSigmaY2(clusters[i].fSigmaY2);
449 pCluster->SetSigmaZ2(clusters[i].fSigmaZ2);
450 pCluster->SetQ(clusters[i].fCharge);
451 pCluster->SetMax(clusters[i].fQMax);
452 if (tpcClusterLabels) {
453 if (tpcClusterLabels->find(clusters[i].fID)!=tpcClusterLabels->end()) {
454 const AliHLTTPCClusterMCWeight* mcWeights=tpcClusterLabels->find(clusters[i].fID)->second.fClusterID;
455 for (int k=0; k<3; k++) {
456 // TODO: sort the labels according to the weight in order to assign the most likely mc label
457 // to the first component
458 pCluster->SetLabel(mcWeights[k].fMCID, k);
461 AliError(Form("can not find mc label of cluster with id %0x08x", clusters[i].fID));
465 if (fVerbosity>0) AliInfo(Form("converted %d cluster(s) from block %s 0x%08x", nSpacepoints, AliHLTComponent::DataType2Text(dt).c_str(), specification));
466 } while (pHLTOUT->SelectNextDataBlock()>=0);
470 int AliHLTTPCClusterAccessHLTOUT::ReadAliHLTTPCRawClusterData(AliHLTOUT* pHLTOUT, TClonesArray* pClusters, const AliHLTTPCClusterMCDataList *tpcClusterLabels)
472 // read cluster data from AliHLTTPCClusterData
474 // FIXME: this is in large parts like ReadAliHLTTPCClusterData,
475 // make a common method
477 if (!pHLTOUT || !pClusters) return -EINVAL;
479 const AliHLTUInt8_t* pBuffer=NULL;
480 AliHLTUInt32_t size=0;
481 if ((iResult=pHLTOUT->GetDataBuffer(pBuffer, size))<0) {
484 if (pBuffer==NULL || size<4) {
485 AliError("invalid cluster data block");
488 AliHLTComponentDataType dt=kAliHLTVoidDataType;
489 AliHLTUInt32_t specification=kAliHLTVoidDataSpec;
490 if (pHLTOUT->GetDataBlockDescription(dt, specification)<0) {
491 AliError("failed to retrieve data block description, skipping mc cluster data block ...");
494 const AliHLTTPCRawClusterData* clusterData = reinterpret_cast<const AliHLTTPCRawClusterData*>(pBuffer);
495 Int_t nCount = (Int_t) clusterData->fCount;
496 if (clusterData->fVersion!=0) {
497 // this is encoded data of different formats
498 switch (clusterData->fVersion) {
500 iResult=ReadAliHLTTPCRawClusterDataDeflateSimple(reinterpret_cast<const AliHLTUInt8_t*>(clusterData->fClusters),
501 size-sizeof(AliHLTTPCRawClusterData), nCount, specification,
502 pClusters, tpcClusterLabels);
510 if (nCount*sizeof(AliHLTTPCRawCluster) + sizeof(AliHLTTPCRawClusterData) != size) {
511 AliError("inconsistent cluster data block size, skipping block");
514 const AliHLTTPCRawCluster *clusters = clusterData->fClusters;
515 int offset=pClusters->GetEntries();
516 pClusters->ExpandCreate(offset+nCount);
517 AliHLTUInt8_t slice = AliHLTTPCDefinitions::GetMinSliceNr(specification);
518 AliHLTUInt8_t partition = AliHLTTPCDefinitions::GetMinPatchNr(specification);
519 // FIXME: get first row number of outer sectors from a common definition instead using number
520 int rowOffset=partition<2?0:63;
521 for (int i=0; i<nCount; i++) {
522 if (!pClusters->At(offset+i)) continue;
523 AliTPCclusterMI* pCluster=dynamic_cast<AliTPCclusterMI*>(pClusters->At(offset+i));
525 AliError("invalid object type, expecting AliTPCclusterMI");
526 break; // this is a problem of all objects
528 if (fVerbosity>1) AliInfo(Form("cluster padrow %d (slice %d partition %d)", clusters[i].GetPadRow(), slice, partition));
529 if (clusters[i].GetPadRow()<rowOffset) {
530 AliError(Form("invalid row number %d, expecting minimum row number %d for slice %d partition %d", clusters[i].GetPadRow(), rowOffset, slice, partition));
532 pCluster->SetRow(clusters[i].GetPadRow()-rowOffset);
534 pCluster->SetPad(clusters[i].GetPad());
535 pCluster->SetTimeBin(clusters[i].GetTime());
536 pCluster->SetSigmaY2(clusters[i].GetSigmaY2());
537 pCluster->SetSigmaZ2(clusters[i].GetSigmaZ2());
538 pCluster->SetQ(clusters[i].GetCharge());
539 pCluster->SetMax(clusters[i].GetQMax());
540 if (tpcClusterLabels) {
541 UInt_t clusterID=AliHLTTPCSpacePointData::GetID(slice, partition, i);
542 if (tpcClusterLabels->find(clusterID)!=tpcClusterLabels->end()) {
543 const AliHLTTPCClusterMCWeight* mcWeights=tpcClusterLabels->find(clusterID)->second.fClusterID;
544 for (int k=0; k<3; k++) {
545 // TODO: sort the labels according to the weight in order to assign the most likely mc label
546 // to the first component
547 pCluster->SetLabel(mcWeights[k].fMCID, k);
550 AliError(Form("can not find mc label of cluster with id %0x08x", clusterID));
554 if (fVerbosity>0) AliInfo(Form("converted %d cluster(s) from block %s 0x%08x", nCount, AliHLTComponent::DataType2Text(dt).c_str(), specification));
555 } while (pHLTOUT->SelectNextDataBlock()>=0);
559 int AliHLTTPCClusterAccessHLTOUT::ReadRemainingClustersCompressed(AliHLTOUT* pHLTOUT, TClonesArray* pClusters, const AliHLTTPCClusterMCDataList *tpcClusterLabels)
561 // read cluster data from AliHLTTPCClusterData
563 if (!pHLTOUT || !pClusters) return -EINVAL;
565 const AliHLTUInt8_t* pBuffer=NULL;
566 AliHLTUInt32_t size=0;
567 if ((iResult=pHLTOUT->GetDataBuffer(pBuffer, size))<0) {
570 if (pBuffer==NULL || size<4) {
571 AliError("invalid cluster data block");
574 AliHLTComponentDataType dt=kAliHLTVoidDataType;
575 AliHLTUInt32_t specification=kAliHLTVoidDataSpec;
576 if (pHLTOUT->GetDataBlockDescription(dt, specification)<0) {
577 AliError("failed to retrieve data block description, skipping mc cluster data block ...");
580 const AliHLTTPCRawClusterData* clusterData = reinterpret_cast<const AliHLTTPCRawClusterData*>(pBuffer);
581 Int_t nCount = (Int_t) clusterData->fCount;
583 // this is encoded data of different formats
584 switch (clusterData->fVersion) {
586 iResult=ReadAliHLTTPCRawClusterDataDeflateSimple(reinterpret_cast<const AliHLTUInt8_t*>(clusterData->fClusters),
587 size-sizeof(AliHLTTPCRawClusterData), nCount, specification,
588 pClusters, tpcClusterLabels);
591 AliError(Form("invalid cluster format version %d", clusterData->fVersion));
595 if (fVerbosity>0) AliInfo(Form("converted %d cluster(s) from block %s 0x%08x", nCount, AliHLTComponent::DataType2Text(dt).c_str(), specification));
596 } while (pHLTOUT->SelectNextDataBlock()>=0 && iResult>=0);
601 int AliHLTTPCClusterAccessHLTOUT::ReadAliHLTTPCRawClusterDataDeflateSimple(const AliHLTUInt8_t* pData, int dataSize,
602 int nofClusters, AliHLTUInt32_t specification,
603 TClonesArray* pClusters,
604 const AliHLTTPCClusterMCDataList *tpcClusterLabels)
606 // read cluster data from AliHLTTPCClusterData
608 // FIXME: quick implementation to read the compressed cluster data from HLTOUT
609 // the data definition below is the same as in AliHLTTPCDataCompressionComponent
610 // but needs to be moved to a common class (AliHLTTPCDefinitions?)
611 // Think about a decoder class supporting iterator objects for various types
614 if (!pData || !pClusters) return -EINVAL;
615 AliHLTDataInflater inflater;
616 if ((iResult=inflater.InitBitDataInput(pData, dataSize))<0) {
620 int offset=pClusters->GetEntries();
621 pClusters->ExpandCreate(offset+nofClusters);
622 AliHLTUInt8_t slice = AliHLTTPCDefinitions::GetMinSliceNr(specification);
623 AliHLTUInt8_t partition = AliHLTTPCDefinitions::GetMinPatchNr(specification);
624 // the compressed format stores the difference of the local row number in
625 // the partition to the row of the last cluster
626 // add the first row in the partition to get global row number
627 // offline uses row number in physical sector, inner sector consists of
628 // partitions 0 and 1, outer sector of partition 2-5
629 int rowOffset=AliHLTTPCTransform::GetFirstRow(partition)-(partition<2?0:AliHLTTPCTransform::GetFirstRow(2));
633 AliHLTUInt8_t switchBit=0;
634 AliHLTUInt64_t value=0;
635 AliTPCclusterMI* pCluster=NULL;
636 AliHLTUInt32_t lastPadRow=0;
637 while (outClusterCnt<nofClusters && inflater.InputBit(switchBit)) {
638 const AliHLTTPCDefinitions::AliClusterParameter& parameter
639 =AliHLTTPCDefinitions::fgkClusterParameterDefinitions[parameterId];
640 // in mode DeflaterSimple, the optional parameter of the cluster parameter definition
641 // corresponds to the number bits of the reduced format
642 if (!inflater.InputBits(value, switchBit?parameter.fBitLength:parameter.fOptional)) {
647 if (!pClusters->At(offset+outClusterCnt)) {
648 // here we should not get anymore because of the condition outClusterCnt<nofClusters
651 pCluster=dynamic_cast<AliTPCclusterMI*>(pClusters->At(offset+outClusterCnt));
653 AliError("invalid object type, expecting AliTPCclusterMI");
654 iResult=-EBADF; // this is a problem of all objects
658 switch (parameterId) {
659 case AliHLTTPCDefinitions::kPadRow:
660 {pCluster->SetRow(value+lastPadRow+rowOffset); lastPadRow+=value;break;}
661 case AliHLTTPCDefinitions::kPad:
662 {float pad=value; pad/=parameter.fScale; pCluster->SetPad(pad); break;}
663 case AliHLTTPCDefinitions::kTime:
664 {float time=value; time/=parameter.fScale; pCluster->SetTimeBin(time); break;}
665 case AliHLTTPCDefinitions::kSigmaY2:
666 {float sigmaY2=value; sigmaY2/=parameter.fScale; pCluster->SetSigmaY2(sigmaY2); break;}
667 case AliHLTTPCDefinitions::kSigmaZ2:
668 {float sigmaZ2=value; sigmaZ2/=parameter.fScale; pCluster->SetSigmaZ2(sigmaZ2); break;}
669 case AliHLTTPCDefinitions::kCharge:
670 {pCluster->SetQ(value); break;}
671 case AliHLTTPCDefinitions::kQMax:
672 {pCluster->SetMax(value); break;}
674 if (parameterId>=AliHLTTPCDefinitions::kLast) {
675 // switch to next cluster
676 if (tpcClusterLabels) {
677 UInt_t clusterID=AliHLTTPCSpacePointData::GetID(slice, partition, outClusterCnt);
678 if (tpcClusterLabels->find(clusterID)!=tpcClusterLabels->end()) {
679 const AliHLTTPCClusterMCWeight* mcWeights=tpcClusterLabels->find(clusterID)->second.fClusterID;
680 for (int k=0; k<3; k++) {
681 // TODO: sort the labels according to the weight in order to assign the most likely mc label
682 // to the first component
683 pCluster->SetLabel(mcWeights[k].fMCID, k);
686 AliError(Form("can not find mc label of cluster with id 0x%08x", clusterID));
696 if (inflater.InputBit(switchBit)) {
697 AliWarning("format error of compressed clusters, there is more data than expected");
699 inflater.CloseBitDataInput();
700 if (iResult>=0 && nofClusters!=outClusterCnt) {
702 AliError(Form("error reading compressed cluster format: expected %d, read only %d cluster(s)", nofClusters, outClusterCnt));
708 AliHLTTPCClusterAccessHLTOUT::AliTPCclusterMIContainer::AliTPCclusterMIContainer()
710 , fRemainingClusterIds()
711 , fTrackModelClusterIds()
712 , fCurrentClusterIds(NULL)
718 for (int i=0; i<72; i++) {
719 fClusterArrays.push_back(new TClonesArray("AliTPCclusterMI"));
723 AliHLTTPCClusterAccessHLTOUT::AliTPCclusterMIContainer::~AliTPCclusterMIContainer()
726 for (vector<TClonesArray*>::iterator i=fClusterArrays.begin(); i!=fClusterArrays.end(); i++) {
734 AliHLTTPCClusterAccessHLTOUT::AliTPCclusterMIContainer::iterator& AliHLTTPCClusterAccessHLTOUT::AliTPCclusterMIContainer::BeginRemainingClusterBlock(int /*count*/, AliHLTUInt32_t specification)
736 /// iterator of remaining clusters block of specification
737 AliHLTUInt8_t slice=AliHLTTPCDefinitions::GetMinSliceNr(specification);
738 AliHLTUInt8_t partition=AliHLTTPCDefinitions::GetMinPatchNr(specification);
739 unsigned index=slice*AliHLTTPCTransform::GetNumberOfPatches()+partition;
740 if (index<fRemainingClusterIds.size())
741 fCurrentClusterIds=&fRemainingClusterIds[index];
743 fCurrentClusterIds=NULL;
744 fIterator=iterator(this);
748 AliHLTTPCClusterAccessHLTOUT::AliTPCclusterMIContainer::iterator& AliHLTTPCClusterAccessHLTOUT::AliTPCclusterMIContainer::BeginTrackModelClusterBlock(int /*count*/)
750 /// iterator of track model clusters
751 if (fTrackModelClusterIds.fIds && fTrackModelClusterIds.fSize>0)
752 fCurrentClusterIds=&fTrackModelClusterIds;
754 fCurrentClusterIds=NULL;
755 fIterator=iterator(this);
759 int AliHLTTPCClusterAccessHLTOUT::AliTPCclusterMIContainer::AddClusterMCData(const AliHLTComponentBlockData* pDesc)
761 /// add cluster mc data block
762 if (!pDesc) return -EINVAL;
763 if (pDesc->fDataType==AliHLTTPCDefinitions::AliHLTDataTypeClusterMCInfo()) {
764 AliHLTUInt8_t slice=AliHLTTPCDefinitions::GetMinSliceNr(pDesc->fSpecification);
765 AliHLTUInt8_t partition=AliHLTTPCDefinitions::GetMinPatchNr(pDesc->fSpecification);
766 unsigned index=slice*AliHLTTPCTransform::GetNumberOfPatches()+partition;
767 if (fClusterMCData.size()<=index) {
768 if ((int)fClusterMCData.size()<AliHLTTPCTransform::GetNSlice()*AliHLTTPCTransform::GetNumberOfPatches()) {
769 fClusterMCData.resize(AliHLTTPCTransform::GetNSlice()*AliHLTTPCTransform::GetNumberOfPatches(), NULL);
771 fClusterMCData.resize(index+1, NULL);
774 if (pDesc->fSize<sizeof(AliHLTTPCClusterMCData)) return -EINVAL;
775 const AliHLTTPCClusterMCData* pData=reinterpret_cast<const AliHLTTPCClusterMCData*>(pDesc->fPtr);
776 unsigned nLabels = pData->fCount;
777 if (nLabels*sizeof(AliHLTTPCClusterMCLabel) + sizeof(AliHLTTPCClusterMCData) != pDesc->fSize) {
780 fClusterMCData[index]=pData;
786 int AliHLTTPCClusterAccessHLTOUT::AliTPCclusterMIContainer::AddClusterIds(const AliHLTComponentBlockData* pDesc)
788 /// add cluster id block for remaining or track model clusters
789 if (!pDesc) return -EINVAL;
790 if (pDesc->fDataType==AliHLTTPCDefinitions::ClusterIdTracksDataType()) {
791 fTrackModelClusterIds.fIds=reinterpret_cast<AliHLTUInt32_t*>(pDesc->fPtr);
792 fTrackModelClusterIds.fSize=pDesc->fSize/sizeof(AliHLTUInt32_t);
795 if (pDesc->fDataType==AliHLTTPCDefinitions::RemainingClusterIdsDataType()) {
796 AliHLTUInt8_t slice=AliHLTTPCDefinitions::GetMinSliceNr(pDesc->fSpecification);
797 AliHLTUInt8_t partition=AliHLTTPCDefinitions::GetMinPatchNr(pDesc->fSpecification);
798 unsigned index=slice*AliHLTTPCTransform::GetNumberOfPatches()+partition;
799 if (fRemainingClusterIds.size()<=index) {
800 if ((int)fRemainingClusterIds.size()<AliHLTTPCTransform::GetNSlice()*AliHLTTPCTransform::GetNumberOfPatches()) {
801 fRemainingClusterIds.resize(AliHLTTPCTransform::GetNSlice()*AliHLTTPCTransform::GetNumberOfPatches());
803 fRemainingClusterIds.resize(index+1);
806 fRemainingClusterIds[index].fIds=reinterpret_cast<AliHLTUInt32_t*>(pDesc->fPtr);
807 fRemainingClusterIds[index].fSize=pDesc->fSize/sizeof(AliHLTUInt32_t);
813 AliHLTUInt32_t AliHLTTPCClusterAccessHLTOUT::AliTPCclusterMIContainer::GetClusterId(int clusterNo) const
815 /// get the cluster id from the current cluster id block (optional)
816 if (!fCurrentClusterIds ||
817 (int)fCurrentClusterIds->fSize<=clusterNo ||
819 return kAliHLTVoidDataSpec;
820 return fCurrentClusterIds->fIds[clusterNo];
823 AliTPCclusterMI* AliHLTTPCClusterAccessHLTOUT::AliTPCclusterMIContainer::NextCluster(int slice, int partition)
825 /// load next cluster from array of the sepcific sector
826 unsigned sector=partition<2?slice:slice+36;
827 if (fClusterArrays.size()<=sector ||
828 fClusterArrays[sector]==NULL) {
829 AliErrorClass(Form("no cluster array available for sector %d", sector));
832 TClonesArray& array=*(fClusterArrays[sector]);
833 int count=array.GetEntriesFast();
834 return new (array[count]) AliTPCclusterMI;
837 int AliHLTTPCClusterAccessHLTOUT::AliTPCclusterMIContainer::SetMC(AliTPCclusterMI* pCluster, AliHLTUInt32_t clusterId)
839 /// set MC data for the cluster
840 if (!pCluster) return -EINVAL;
841 if (clusterId==kAliHLTVoidDataSpec) return 0;
843 unsigned slice=AliHLTTPCSpacePointData::GetSlice(clusterId);
844 unsigned partition=AliHLTTPCSpacePointData::GetPatch(clusterId);
845 unsigned number=AliHLTTPCSpacePointData::GetNumber(clusterId);
846 if ((int)slice>=AliHLTTPCTransform::GetNSlice() ||
847 (int)partition>=AliHLTTPCTransform::GetNumberOfPatches()) return -EDOM;
848 unsigned index=slice*AliHLTTPCTransform::GetNumberOfPatches()+partition;
849 if (fClusterMCData.size()<=index ||
850 fClusterMCData[index]==NULL ||
851 fClusterMCData[index]->fCount<=number) return 0;
852 const AliHLTTPCClusterMCWeight* mcWeights=fClusterMCData[index]->fLabels[number].fClusterID;
853 for (int k=0; k<3; k++) {
854 // TODO: sort the labels according to the weight in order to assign the most likely mc label
855 // to the first component
856 pCluster->SetLabel(mcWeights[k].fMCID, k);
862 void AliHLTTPCClusterAccessHLTOUT::AliTPCclusterMIContainer::Clear(Option_t* /*option*/)
866 for (vector<TClonesArray*>::iterator i=fClusterArrays.begin(); i!=fClusterArrays.end(); i++)
867 if (*i) (*i)->Clear();
870 for (vector<AliClusterIdBlock>::iterator i=fRemainingClusterIds.begin(); i!=fRemainingClusterIds.end(); i++)
871 {i->fIds=NULL; i->fSize=0;}
873 fTrackModelClusterIds.fIds=NULL; fTrackModelClusterIds.fSize=0;
874 fCurrentClusterIds=NULL;
876 for (vector<const AliHLTTPCClusterMCData*>::iterator i=fClusterMCData.begin(); i!=fClusterMCData.end(); i++)
881 TObjArray* AliHLTTPCClusterAccessHLTOUT::AliTPCclusterMIContainer::GetSectorArray(unsigned sector) const
883 /// get the cluster array for a sector
884 if (fClusterArrays.size()<=sector) return NULL;
885 return fClusterArrays[sector];
888 void AliHLTTPCClusterAccessHLTOUT::AliTPCclusterMIContainer::Print(Option_t *option) const
890 /// inherited from TObject
891 cout << "AliHLTTPCClusterAccessHLTOUT::AliTPCclusterMIContainer" << endl;
892 ios::fmtflags coutflags=cout.flags(); // backup cout status flags
894 if ((bAll=(strcmp(option, "full")==0)) ||
895 strcmp(option, "short")==0) {
896 for (unsigned iArray=0; iArray<fClusterArrays.size(); iArray++) {
897 if (fClusterArrays[iArray]) {
898 TClonesArray* pArray=fClusterArrays[iArray];
899 cout << " sector " << setfill(' ') << setw(2) << iArray << ": " << pArray->GetEntriesFast() << endl;
901 for (int iCluster=0; iCluster<pArray->GetEntriesFast(); iCluster++) {
902 if (!pArray->At(iCluster)) continue;
903 AliTPCclusterMI* pCluster=dynamic_cast<AliTPCclusterMI*>(pArray->At(iCluster));
904 if (!pCluster) break;
905 cout << " AliTPCclusterMI:"
906 << " row=" << pCluster->GetRow()
907 << " pad=" << pCluster->GetPad()
908 << " time=" << pCluster->GetTimeBin()
909 << " charge=" << pCluster->GetQ()
910 << " maxq=" << pCluster->GetMax()
917 cout.flags(coutflags); // restore the original flags
920 AliHLTTPCClusterAccessHLTOUT::AliTPCclusterMIContainer::iterator& AliHLTTPCClusterAccessHLTOUT::AliTPCclusterMIContainer::iterator::Next(int slice, int partition)
922 // switch to next cluster
925 fClusterId=kAliHLTVoidDataSpec;
928 if (fClusterNo>=0 && !fCluster) {
929 // end was reached before
932 fCluster=fData->NextCluster(slice, partition);
933 fClusterId=fData->GetClusterId(++fClusterNo);
934 if (fCluster && fClusterId!=kAliHLTVoidDataSpec) {
935 fData->SetMC(fCluster, fClusterId);
937 // offline uses row number in physical sector, inner sector consists of
938 // partitions 0 and 1, outer sector of partition 2-5
939 fRowOffset=partition<2?0:AliHLTTPCTransform::GetFirstRow(2);