3 //**************************************************************************
4 //* This file is property of and copyright by the ALICE 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"
36 #include "AliHLTSystem.h"
37 #include "AliHLTPluginBase.h"
38 #include "AliTPCclusterMI.h"
39 #include "AliTPCClustersRow.h"
40 #include "AliTPCParam.h"
41 #include "TClonesArray.h"
49 /** ROOT macro for the implementation of ROOT specific class methods */
50 ClassImp(AliHLTTPCClusterAccessHLTOUT)
52 AliHLTTPCClusterAccessHLTOUT::AliHLTTPCClusterAccessHLTOUT()
60 // see header file for class documentation
62 // refer to README to build package
64 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
67 AliHLTTPCClusterAccessHLTOUT::~AliHLTTPCClusterAccessHLTOUT()
81 // FIXME: a copy of the TPCParam object is not possible because there is
82 // no appropriate copy constructor or assignment operator, using as
89 void AliHLTTPCClusterAccessHLTOUT::Execute(const char *method, const char *params, Int_t *error)
91 /// inherited from TObject: abstract command interface
92 if (strcmp(method, "read")==0) {
93 int iResult=ProcessClusters(params);
94 if (error) *error=iResult;
97 if (strcmp(method, "verbosity")==0) {
101 int value=strtol(params, &dummy, 0);
105 AliError("invalid argument for command 'verbosity', expecting string with number");
110 if (error) *error=iResult;
115 TObject* AliHLTTPCClusterAccessHLTOUT::FindObject(const char *name) const
117 /// inherited from TObject: return the cluster array if name id "clusterarray"
118 if (strcmp(name, "clusterarray")==0) {
119 if (fCurrentSector<0) return NULL;
120 return fClusters->GetSectorArray(fCurrentSector);
122 return TObject::FindObject(name);
125 void AliHLTTPCClusterAccessHLTOUT::Copy(TObject &object) const
127 /// inherited from TObject: supports writing of data to AliTPCClustersRow
128 AliTPCClustersRow* rowcl=dynamic_cast<AliTPCClustersRow*>(&object);
130 int index=rowcl->GetID();
132 AliFatal("TPCParam object not initialized, use 'Copy()' funtion to initialize");
137 if (!fTPCParam->AdjustSectorRow(index, sector, row)) {
138 AliFatal(Form("failed to get sector and row for index %d", index));
141 fClusters->FillSectorArray(rowcl->GetArray(), sector, row);
144 AliTPCParam* tpcparam=dynamic_cast<AliTPCParam*>(&object);
146 // FIXME: can nor make a copy of the TPCparam object because
147 // there is no appropriate copy constructor or assignment operator
148 const_cast<AliHLTTPCClusterAccessHLTOUT*>(this)->fTPCParam=tpcparam;
151 return TObject::Copy(object);
155 void AliHLTTPCClusterAccessHLTOUT::Clear(Option_t * option)
157 /// inherited from TObject: cleanup
158 if (strcmp(option, "event")==0) {
159 if (fClusters) fClusters->Clear();
164 void AliHLTTPCClusterAccessHLTOUT::Print(Option_t *option) const
166 /// inherited from TObject
167 if (fClusters) fClusters->Print(option);
170 int AliHLTTPCClusterAccessHLTOUT::ProcessClusters(const char* params)
172 /// process the cluster data from HLTOUT and fill array
173 /// the cluster data can be in many different formats, e.g.
174 /// raw or compressed
176 TString strparams(params);
178 std::auto_ptr<TObjArray> tokens(strparams.Tokenize(" "));
179 if (!tokens.get()) return -ENOMEM;
180 for (int i=0; i< tokens->GetEntriesFast(); i++) {
181 if (!tokens->At(i)) continue;
182 TString argument=tokens->At(i)->GetName();
183 // the offline code enumerates first the 36 inner (partitions 0+1) and then 36 outer
184 // sectors (partitions 2-5)
185 if (argument.BeginsWith("sector=")) {
186 argument.ReplaceAll("sector=", "");
187 sector=argument.Atoi();
191 AliError("invalid argument, please specify \"sector=sectorno\"");
195 AliError(Form("invalid sector number %d", sector));
200 fClusters=new AliRawClusterContainer;
202 if (!fClusters) return -ENOMEM;
204 if (fCurrentSector>=0) {
205 // cluster container already filled
206 fCurrentSector=sector;
207 // TObjArray* pArray=fClusters->GetSectorArray(fCurrentSector);
209 // AliError(Form("can not get cluster array for sector %d", sector));
212 // if (fVerbosity>0) AliInfo(Form("converted %d cluster(s) for sector %d", pArray->GetEntriesFast() ,sector));
213 return 0; //pArray->GetEntriesFast();
216 // fill the cluster container
217 AliHLTSystem* pSystem=AliHLTPluginBase::GetInstance();
219 AliError("can not access HLT system");
222 AliHLTOUT* pHLTOUT=pSystem->RequestHLTOUT();
224 AliError("can not access HLTOUT");
229 fpDecoder=new AliHLTTPCDataCompressionDecoder;
233 AliError("failed to create decoder instance");
237 AliHLTTPCDataCompressionDecoder& decoder=*fpDecoder;
239 decoder.SetVerbosity(fVerbosity);
240 //decoder.EnableClusterMerger();
242 bool bNextBlock=false;
243 // add cluster id and mc information data blocks
244 for (bNextBlock=(pHLTOUT->SelectFirstDataBlock()>=0);
245 bNextBlock; bNextBlock=(pHLTOUT->SelectNextDataBlock()>=0)) {
246 AliHLTComponentBlockData desc;
247 // FIXME: extend HLTOUT to get the full descriptor
248 const AliHLTUInt8_t* buffer=NULL;
249 if ((iResult=pHLTOUT->GetDataBuffer(buffer, desc.fSize))<0) {
252 desc.fPtr=(void*)buffer;
253 if (pHLTOUT->GetDataBlockDescription(desc.fDataType, desc.fSpecification)<0) {
256 if (desc.fDataType==AliHLTTPCDefinitions::AliHLTDataTypeClusterMCInfo()) {
257 // add mc information
258 if ((iResult=decoder.AddClusterMCData(&desc))<0) {
262 if (desc.fDataType==AliHLTTPCDefinitions::RemainingClusterIdsDataType() ||
263 desc.fDataType==AliHLTTPCDefinitions::ClusterIdTracksDataType()) {
265 if ((iResult=decoder.AddClusterIds(&desc))<0) {
271 bool bHavePartitionRawData=false;
272 bool bHavePartitionCompressedData=false;
273 vector<bool> bHavePartitionData(216, false);
277 int nExtractedClusters=0;
278 for (bNextBlock=(pHLTOUT->SelectFirstDataBlock()>=0);
279 bNextBlock; bNextBlock=(pHLTOUT->SelectNextDataBlock()>=0)) {
280 decoder.SetPadShift(0.0);
281 AliHLTComponentBlockData desc;
282 // FIXME: extend HLTOUT to get the full descriptor with one call
283 const AliHLTUInt8_t* buffer=NULL;
284 if ((iResult=pHLTOUT->GetDataBuffer(buffer, desc.fSize))<0) {
287 desc.fPtr=(void*)buffer;
288 if (pHLTOUT->GetDataBlockDescription(desc.fDataType, desc.fSpecification)<0) {
291 if (!TestBit(kSkipPartitionClusters) &&
292 (desc.fDataType==AliHLTTPCDefinitions::RawClustersDataType())) {
293 // This is a special handling of data blocks produced with v5-01-Release
294 // The pad shift by 0.5 was not included in the data but was applied in the
295 // unpacking in this class. Changed in r51306, the next tag containing this
296 // change in the online system is v5-01-Rev-07. There are only very few runs
297 // of Sep 2011 with recorded clusters not containing the 0.5 shift
298 // There was also a chenge in the data type of the compressed partition
299 // cluster blocks which helps to identify the blocks which need the pad shift
301 if (desc.fSize<sizeof(AliHLTTPCRawClusterData)) continue;
302 const AliHLTTPCRawClusterData* clusterData = reinterpret_cast<const AliHLTTPCRawClusterData*>(buffer);
303 if (!clusterData) continue;
304 if (clusterData->fVersion==1) {
305 // compressed clusters without the pad shift
306 // no raw clusters (version==0) have ever been recorded
307 decoder.SetPadShift(0.5);
309 AliHLTUInt8_t slice = AliHLTTPCDefinitions::GetMinSliceNr(desc.fSpecification);
310 AliHLTUInt8_t partition = AliHLTTPCDefinitions::GetMinPatchNr(desc.fSpecification);
311 if (slice!=AliHLTTPCDefinitions::GetMaxSliceNr(desc.fSpecification) ||
312 partition!=AliHLTTPCDefinitions::GetMaxPatchNr(desc.fSpecification)) {
313 AliFatal(Form("inconsistent cluster data: can not handle blocks containing multiple partitions, "
314 "block specification 0x%08x", desc.fSpecification));
316 iResult=decoder.ReadClustersPartition(fClusters->BeginRemainingClusterBlock(0, desc.fSpecification),
317 reinterpret_cast<AliHLTUInt8_t*>(desc.fPtr),
319 desc.fSpecification);
320 if (iResult>=0) nExtractedClusters+=iResult;
322 AliFatal(Form("processing of cluster block 0x%08x failed with error code %d", desc.fSpecification, iResult));
324 unsigned index=slice*AliHLTTPCTransform::GetNumberOfPatches()+partition;
325 if (index>=bHavePartitionData.size()) bHavePartitionData.resize(index, false);
326 if (bHavePartitionData[index]) {
327 AliFatal(Form("inconsistent cluster data: multiple data blocks of identical specification indicate a failure "
328 "in the production of the data. Probably an HLT emulation chain is executed in the reconstruction "
329 "and produces data in addition to HLTOUT. Option 'ignore-hltout' is required in that case; "
330 "block specification 0x%08x", desc.fSpecification));
332 bHavePartitionData[index]=true;
333 if (bHavePartitionCompressedData) {
334 AliFatal(Form("inconsistent cluster data: both compressed and raw cluster blocks present in HLTOUT, indicates a failure "
335 "in the production of the data. Probably an HLT emulation chain is executed in the reconstruction "
336 "and produces data in addition to HLTOUT. Option 'ignore-hltout' is required in that case; "
337 "block specification 0x%08x", desc.fSpecification));
339 bHavePartitionRawData=true;
341 } else if (!TestBit(kSkipPartitionClusters) &&
342 (desc.fDataType==AliHLTTPCDefinitions::RemainingClustersCompressedDataType())) {
343 AliHLTUInt8_t slice = AliHLTTPCDefinitions::GetMinSliceNr(desc.fSpecification);
344 AliHLTUInt8_t partition = AliHLTTPCDefinitions::GetMinPatchNr(desc.fSpecification);
345 if (slice!=AliHLTTPCDefinitions::GetMaxSliceNr(desc.fSpecification) ||
346 partition!=AliHLTTPCDefinitions::GetMaxPatchNr(desc.fSpecification)) {
347 AliFatal(Form("inconsistent cluster data: can not handle blocks containing multiple partitions, "
348 "block specification 0x%08x", desc.fSpecification));
350 iResult=decoder.ReadClustersPartition(fClusters->BeginRemainingClusterBlock(0, desc.fSpecification),
351 reinterpret_cast<AliHLTUInt8_t*>(desc.fPtr),
353 desc.fSpecification);
354 if (iResult>0) nExtractedClusters+=iResult;
355 unsigned index=slice*AliHLTTPCTransform::GetNumberOfPatches()+partition;
356 if (index>=bHavePartitionData.size()) bHavePartitionData.resize(index, false);
357 if (bHavePartitionData[index]) {
358 AliFatal(Form("inconsistent cluster data: multiple data blocks of identical specification indicate a failure "
359 "in the production of the data. Probably an HLT emulation chain is executed in the reconstruction "
360 "and produces data in addition to HLTOUT. Option 'ignore-hltout' is required in that case; "
361 "block specification 0x%08x", desc.fSpecification));
363 bHavePartitionData[index]=true;
364 bHavePartitionData[index]=true;
365 if (bHavePartitionRawData) {
366 AliFatal(Form("inconsistent cluster data: both compressed and raw cluster blocks present in HLTOUT, indicates a failure "
367 "in the production of the data. Probably an HLT emulation chain is executed in the reconstruction "
368 "and produces data in addition to HLTOUT. Option 'ignore-hltout' is required in that case; "
369 "block specification 0x%08x", desc.fSpecification));
371 bHavePartitionCompressedData=true;
373 } else if (!TestBit(kSkipTrackClusters) &&
374 desc.fDataType==AliHLTTPCDefinitions::ClusterTracksCompressedDataType()) {
375 iResult=decoder.ReadTrackModelClustersCompressed(fClusters->BeginTrackModelClusterBlock(0),
376 reinterpret_cast<AliHLTUInt8_t*>(desc.fPtr),
378 desc.fSpecification);
383 pSystem->ReleaseHLTOUT(pHLTOUT);
385 if (iResult<0) return iResult;
386 // if (fVerbosity>0) {
387 // int nConvertedClusters=0;
388 // for (int s=0; s<72; s++) {
389 // TObjArray* pArray=fClusters->GetSectorArray(s);
390 // if (!pArray) continue;
391 // nConvertedClusters+=pArray->GetEntriesFast();
393 // AliInfo(Form("extracted HLT clusters: %d, converted HLT clusters: %d", nExtractedClusters, nConvertedClusters));
396 fCurrentSector=sector;
397 // TObjArray* pArray=fClusters->GetSectorArray(fCurrentSector);
399 // AliError(Form("can not get cluster array for sector %d", sector));
402 // if (fVerbosity>0) AliInfo(Form("converted %d cluster(s) for sector %d", pArray->GetEntriesFast() ,sector));
403 return 0; //pArray->GetEntriesFast();
406 AliHLTTPCClusterAccessHLTOUT::AliRawClusterContainer::AliRawClusterContainer()
408 , fSectorArray(new TClonesArray(AliTPCclusterMI::Class()))
413 for (int i=0; i<72; i++) {
414 fClusterMaps.push_back(new AliRawClusterEntryVector);
415 fClusterMaps.back()->reserve(30000);
419 AliHLTTPCClusterAccessHLTOUT::AliRawClusterContainer::~AliRawClusterContainer()
423 for (vector<AliRawClusterEntryVector*>::iterator i=fClusterMaps.begin(); i!=fClusterMaps.end(); i++) {
430 fSectorArray->Clear();
436 AliHLTTPCClusterAccessHLTOUT::AliRawClusterContainer::iterator& AliHLTTPCClusterAccessHLTOUT::AliRawClusterContainer::BeginPartitionClusterBlock(int count, AliHLTUInt32_t specification)
438 /// iterator of remaining clusters block of specification
440 // reserve space in the array of all clusters
441 // reserve space in the map of the partition
442 unsigned index=AliHLTTPCDefinitions::GetMinSliceNr(specification);
443 AliHLTUInt8_t partition=AliHLTTPCDefinitions::GetMinPatchNr(specification);
444 if (partition>=2) index+=36;
445 if (index<fClusterMaps.size() &&
446 fClusterMaps[index]!=NULL &&
447 fClusterMaps[index]->size()+count>fClusterMaps[index]->capacity()) {
448 fClusterMaps[index]->reserve(fClusterMaps[index]->size()+count);
451 fIterator=iterator(this);
455 AliHLTTPCClusterAccessHLTOUT::AliRawClusterContainer::iterator& AliHLTTPCClusterAccessHLTOUT::AliRawClusterContainer::BeginTrackModelClusterBlock(int /*count*/)
457 /// iterator of track model clusters
458 fIterator=iterator(this);
462 AliHLTTPCClusterAccessHLTOUT::AliRawClusterEntry* AliHLTTPCClusterAccessHLTOUT::AliRawClusterContainer::NextCluster(int slice, int partition)
464 /// load next cluster from array of the sepcific sector
465 unsigned sector=partition<2?slice:slice+36;
466 if (fClusterMaps.size()<=sector ||
467 fClusterMaps[sector]==NULL) {
468 AliErrorClass(Form("no cluster array available for sector %d", sector));
471 AliRawClusterEntryVector& map=*(fClusterMaps[sector]);
472 map.push_back(AliRawClusterEntry());
476 void AliHLTTPCClusterAccessHLTOUT::AliRawClusterContainer::Clear(Option_t* /*option*/)
480 for (vector<AliRawClusterEntryVector*>::iterator i=fClusterMaps.begin(); i!=fClusterMaps.end(); i++)
481 if (*i) (*i)->clear();
483 if (fSectorArray) fSectorArray->Clear();
486 TObjArray* AliHLTTPCClusterAccessHLTOUT::AliRawClusterContainer::GetSectorArray(unsigned sector) const
488 /// get the cluster array for a sector
489 if (fClusterMaps.size()<=sector) return NULL;
491 FillSectorArray(fSectorArray, sector)<0) {
492 fSectorArray->Clear();
497 int AliHLTTPCClusterAccessHLTOUT::AliRawClusterContainer::FillSectorArray(TClonesArray* pSectorArray, unsigned sector, int row) const
499 /// fill the cluster array for a sector and specific row if specified
500 if (!pSectorArray) return -EINVAL;
501 if (fClusterMaps.size()<=sector) return -ERANGE;
502 pSectorArray->Clear();
504 AliRawClusterEntryVector& map=*fClusterMaps[sector];
506 for (unsigned i=0; i<map.size(); i++) {
507 if (row>=0 && map[i].fCluster.GetPadRow()!=row) continue;
508 AliTPCclusterMI* pCluster=new ((*pSectorArray)[nFilled]) AliTPCclusterMI;
509 if (!pCluster) break;
511 pCluster->SetRow(map[i].fCluster.GetPadRow());
512 pCluster->SetPad(map[i].fCluster.GetPad());
513 pCluster->SetTimeBin(map[i].fCluster.GetTime());
514 pCluster->SetSigmaY2(map[i].fCluster.GetSigmaY2());
515 pCluster->SetSigmaZ2(map[i].fCluster.GetSigmaZ2());
516 pCluster->SetQ(map[i].fCluster.GetCharge());
517 pCluster->SetMax(map[i].fCluster.GetQMax());
519 for (int k=0; k<3; k++) {
520 // TODO: sort the labels according to the weight in order to assign the most likely mc label
521 // to the first component
522 pCluster->SetLabel(map[i].fMC.fClusterID[k].fMCID, k);
530 void AliHLTTPCClusterAccessHLTOUT::AliRawClusterContainer::Print(Option_t *option) const
532 /// inherited from TObject
533 cout << "AliHLTTPCClusterAccessHLTOUT::AliRawClusterContainer" << endl;
534 ios::fmtflags coutflags=cout.flags(); // backup cout status flags
536 if ((bAll=(strcmp(option, "full")==0)) ||
537 strcmp(option, "short")==0) {
538 for (unsigned iArray=0; iArray<fClusterMaps.size(); iArray++) {
539 if (fClusterMaps[iArray]) {
540 AliRawClusterEntryVector& map=*fClusterMaps[iArray];
541 cout << " sector " << setfill(' ') << setw(2) << iArray << ": " << map.size() << endl;
543 for (unsigned iCluster=0; iCluster<map.size(); iCluster++) {
544 AliHLTTPCRawCluster &cluster = map[iCluster].fCluster;
545 cout << " AliTPCclusterMI:"
546 << " row=" << cluster.GetPadRow()
547 << " pad=" << cluster.GetPad()
548 << " time=" << cluster.GetTime()
549 << " charge=" << cluster.GetCharge()
550 << " maxq=" << cluster.GetQMax()
557 cout.flags(coutflags); // restore the original flags
560 AliHLTTPCClusterAccessHLTOUT::AliRawClusterContainer::iterator& AliHLTTPCClusterAccessHLTOUT::AliRawClusterContainer::iterator::Next(int slice, int partition)
562 // switch to next cluster
567 if (fClusterNo>=0 && !fEntry) {
568 // end was reached before
571 fEntry=fData->NextCluster(slice, partition);
573 // offline uses row number in physical sector, inner sector consists of
574 // partitions 0 and 1, outer sector of partition 2-5
575 fRowOffset=partition<2?0:AliHLTTPCTransform::GetFirstRow(2);