]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/TPCLib/comp/AliHLTTPCDataCompressionDecoder.cxx
7205659bd5b83f65da51d1e6ed08258832369268
[u/mrichter/AliRoot.git] / HLT / TPCLib / comp / AliHLTTPCDataCompressionDecoder.cxx
1 // $Id$
2 //**************************************************************************
3 //* This file is property of and copyright by the                          * 
4 //* ALICE Experiment at CERN, All rights reserved.                         *
5 //*                                                                        *
6 //* Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no>        *
7 //*                                                                        *
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 //**************************************************************************
16
17 /// @file   AliHLTTPCDataCompressionDecoder.cxx
18 /// @author Matthias Richter
19 /// @date   2011-10-04
20 /// @brief  Generic decoder class for compressed TPC data, works on a container
21 ///         class implementation which fills the actual target data struct
22
23 #include "AliHLTTPCDataCompressionDecoder.h"
24 #include "AliHLTTPCDataCompressionDescriptor.h"
25 #include "AliHLTDataInflaterSimple.h"
26 #include "AliHLTDataInflaterHuffman.h"
27 #include "TList.h"
28 #include <memory>
29
30 ClassImp(AliHLTTPCDataCompressionDecoder)
31
32 AliHLTTPCDataCompressionDecoder::AliHLTTPCDataCompressionDecoder()
33   : fPadShift(0.)
34   , fVerbosity(0)
35   , fUseClusterMerger(kTRUE)
36   , fpDataInflaterPartition(NULL)
37   , fpDataInflaterTrack(NULL)
38   , fpClusterMerger(NULL)
39   , fPartitionClusterIds()
40   , fTrackModelClusterIds()
41   , fCurrentClusterIds(NULL)
42   , fClusterMCData()
43 {
44   /// constructor
45 }
46
47 AliHLTTPCDataCompressionDecoder::~AliHLTTPCDataCompressionDecoder()
48 {
49   ///destructor
50   if (fpDataInflaterPartition) delete fpDataInflaterPartition;
51   fpDataInflaterPartition=NULL;
52   if (fpDataInflaterTrack) delete fpDataInflaterTrack;
53   fpDataInflaterTrack=NULL;
54   if (fpClusterMerger) delete fpClusterMerger;
55   fpClusterMerger=NULL;
56 }
57
58 AliHLTDataInflater* AliHLTTPCDataCompressionDecoder::CreateInflater(int deflater, int mode) const
59 {
60   // create the inflater for the specified mode
61   vector<AliHLTTPCDefinitions::AliClusterParameterId_t> parameterids;
62   switch (mode) {
63   case 1:
64     parameterids.push_back(AliHLTTPCDefinitions::kPadRow );
65     parameterids.push_back(AliHLTTPCDefinitions::kPad    );
66     parameterids.push_back(AliHLTTPCDefinitions::kTime   );
67     parameterids.push_back(AliHLTTPCDefinitions::kSigmaY2);
68     parameterids.push_back(AliHLTTPCDefinitions::kSigmaZ2);
69     parameterids.push_back(AliHLTTPCDefinitions::kCharge );
70     parameterids.push_back(AliHLTTPCDefinitions::kQMax   );
71     break;
72   case 2:
73     parameterids.push_back(AliHLTTPCDefinitions::kResidualPad );
74     parameterids.push_back(AliHLTTPCDefinitions::kResidualTime);
75     parameterids.push_back(AliHLTTPCDefinitions::kSigmaY2);
76     parameterids.push_back(AliHLTTPCDefinitions::kSigmaZ2);
77     parameterids.push_back(AliHLTTPCDefinitions::kCharge );
78     parameterids.push_back(AliHLTTPCDefinitions::kQMax   );
79     break;
80   default:
81     HLTError("invalid mode %d for inflater initialization", mode);
82   }
83
84   switch (deflater) {
85   case 1:
86     {
87       std::auto_ptr<AliHLTDataInflaterSimple> inflatersimple(new AliHLTDataInflaterSimple);
88       if (!inflatersimple.get()) return NULL;
89       for (vector<AliHLTTPCDefinitions::AliClusterParameterId_t>::const_iterator id=parameterids.begin();
90            id!=parameterids.end(); id++) {
91         const AliHLTTPCDefinitions::AliClusterParameter& parameter=AliHLTTPCDefinitions::fgkClusterParameterDefinitions[*id];
92         if (inflatersimple->AddParameterDefinition(parameter.fName,
93                                                    parameter.fBitLength,
94                                                    parameter.fOptional)<0) {
95           HLTError("error adding parameter definition %s to inflater", parameter.fName);
96           return NULL;
97         }
98       }
99       return inflatersimple.release();
100     }
101     break;
102   case 2:
103     {
104       std::auto_ptr<AliHLTDataInflaterHuffman> inflaterhuffman(new AliHLTDataInflaterHuffman);
105       if (!inflaterhuffman.get()) return NULL;
106       TString cdbPath("HLT/ConfigTPC/TPCDataCompressorHuffmanTables");
107       TObject* pConf=AliHLTMisc::Instance().ExtractObject(AliHLTMisc::Instance().LoadOCDBEntry(cdbPath));
108       if (!pConf) {
109         HLTError("can not load configuration object %s", cdbPath.Data());
110         return NULL;
111       }
112       if (dynamic_cast<TList*>(pConf)==NULL) {
113         HLTError("huffman table configuration object of inconsistent type");
114         return NULL;
115       }
116       inflaterhuffman->InitDecoders(dynamic_cast<TList*>(pConf));
117       for (vector<AliHLTTPCDefinitions::AliClusterParameterId_t>::const_iterator id=parameterids.begin();
118            id!=parameterids.end(); id++) {
119         const AliHLTTPCDefinitions::AliClusterParameter& parameter=AliHLTTPCDefinitions::fgkClusterParameterDefinitions[*id];
120         if (inflaterhuffman->AddParameterDefinition(parameter.fName,
121                                                     parameter.fBitLength)<0) {
122           HLTError("error adding parameter definition %s to inflater", parameter.fName);
123           return NULL;
124         }
125       }
126       return inflaterhuffman.release();
127     }
128     break;
129   default:
130     HLTError("unknown inflater requested %d", deflater);
131   }
132   return NULL;
133 }
134
135 int AliHLTTPCDataCompressionDecoder::InitPartitionClusterDecoding(AliHLTUInt32_t specification)
136 {
137   /// init the decoding of partition cluster block
138   AliHLTUInt8_t slice=AliHLTTPCDefinitions::GetMinSliceNr(specification);
139   AliHLTUInt8_t partition=AliHLTTPCDefinitions::GetMinPatchNr(specification);
140   unsigned index=slice*AliHLTTPCTransform::GetNumberOfPatches()+partition;
141   if (index<fPartitionClusterIds.size())
142     fCurrentClusterIds=&fPartitionClusterIds[index];
143   else
144     fCurrentClusterIds=NULL;
145   return 0;
146 }
147
148 int AliHLTTPCDataCompressionDecoder::InitTrackModelClusterClusterDecoding()
149 {
150   /// init the decoding of track model cluster block
151   if (fTrackModelClusterIds.fIds && fTrackModelClusterIds.fSize>0)
152     fCurrentClusterIds=&fTrackModelClusterIds;
153   else
154     fCurrentClusterIds=NULL;
155   return 0;
156 }
157
158 int AliHLTTPCDataCompressionDecoder::AddCompressionDescriptor(const AliHLTComponentBlockData* pDesc)
159 {
160   /// read descriptor
161   if (!pDesc) return -EINVAL;
162   if (pDesc->fDataType!=AliHLTTPCDefinitions::DataCompressionDescriptorDataType()) return -ENODATA;
163   const AliHLTTPCDataCompressionDescriptor* pHeader=reinterpret_cast<const AliHLTTPCDataCompressionDescriptor*>(pDesc->fPtr);
164   if (! pHeader->CheckSize( pDesc->fSize ) ) return -EINVAL;
165   if( pHeader->GetMergedClustersFlag() == 0 ){
166     fUseClusterMerger = kTRUE;
167   } else if( pHeader->GetMergedClustersFlag() == 1 ){
168     fUseClusterMerger = kFALSE;
169   } else return -EINVAL;
170   return 0;
171 }
172
173 int AliHLTTPCDataCompressionDecoder::AddClusterMCData(const AliHLTComponentBlockData* pDesc)
174 {
175   /// add cluster mc data block
176   if (!pDesc) return -EINVAL;
177   if (pDesc->fDataType==AliHLTTPCDefinitions::AliHLTDataTypeClusterMCInfo()) {
178     AliHLTUInt8_t slice=AliHLTTPCDefinitions::GetMinSliceNr(pDesc->fSpecification);
179     AliHLTUInt8_t partition=AliHLTTPCDefinitions::GetMinPatchNr(pDesc->fSpecification);
180     unsigned index=slice*AliHLTTPCTransform::GetNumberOfPatches()+partition;
181     if (fClusterMCData.size()<=index) {
182       if ((int)fClusterMCData.size()<AliHLTTPCTransform::GetNSlice()*AliHLTTPCTransform::GetNumberOfPatches()) {
183         fClusterMCData.resize(AliHLTTPCTransform::GetNSlice()*AliHLTTPCTransform::GetNumberOfPatches(), NULL);
184       } else {
185         fClusterMCData.resize(index+1, NULL);
186       }
187     }
188     if (pDesc->fSize<sizeof(AliHLTTPCClusterMCData)) return -EINVAL;
189     const AliHLTTPCClusterMCData* pData=reinterpret_cast<const AliHLTTPCClusterMCData*>(pDesc->fPtr);
190     unsigned nLabels = pData->fCount;
191     if (nLabels*sizeof(AliHLTTPCClusterMCLabel) + sizeof(AliHLTTPCClusterMCData) != pDesc->fSize) {
192       return -EINVAL;
193     }
194     fClusterMCData[index]=pData;
195     return 0;
196   }
197   return -ENODATA;
198 }
199
200 int AliHLTTPCDataCompressionDecoder::AddClusterIds(const AliHLTComponentBlockData* pDesc)
201 {
202   /// add cluster id block for partition or track model clusters
203   if (!pDesc) return -EINVAL;
204   if (pDesc->fDataType==AliHLTTPCDefinitions::ClusterIdTracksDataType()) {
205     fTrackModelClusterIds.fIds=reinterpret_cast<AliHLTUInt32_t*>(pDesc->fPtr);
206     fTrackModelClusterIds.fSize=pDesc->fSize/sizeof(AliHLTUInt32_t);
207     return 0;
208   }
209   if (pDesc->fDataType==AliHLTTPCDefinitions::RemainingClusterIdsDataType()) {
210     AliHLTUInt8_t slice=AliHLTTPCDefinitions::GetMinSliceNr(pDesc->fSpecification);
211     AliHLTUInt8_t partition=AliHLTTPCDefinitions::GetMinPatchNr(pDesc->fSpecification);
212     unsigned index=slice*AliHLTTPCTransform::GetNumberOfPatches()+partition;
213     if (fPartitionClusterIds.size()<=index) {
214       if ((int)fPartitionClusterIds.size()<AliHLTTPCTransform::GetNSlice()*AliHLTTPCTransform::GetNumberOfPatches()) {
215         fPartitionClusterIds.resize(AliHLTTPCTransform::GetNSlice()*AliHLTTPCTransform::GetNumberOfPatches());
216       } else {
217         fPartitionClusterIds.resize(index+1);
218       }
219     }
220     fPartitionClusterIds[index].fIds=reinterpret_cast<AliHLTUInt32_t*>(pDesc->fPtr);
221     fPartitionClusterIds[index].fSize=pDesc->fSize/sizeof(AliHLTUInt32_t);
222     return 0;
223   }
224   return -ENODATA;
225 }
226
227 AliHLTUInt32_t AliHLTTPCDataCompressionDecoder::GetClusterId(int clusterNo) const
228 {
229   /// get the cluster id from the current cluster id block
230   /// clusters ids correctly link the MC label from the separate MC data block
231   /// to the cluster. The option is enabled by default in the simulation.
232   if (!fCurrentClusterIds ||
233       (int)fCurrentClusterIds->fSize<=clusterNo ||
234       clusterNo<0)
235     return kAliHLTVoidDataSpec;
236   return fCurrentClusterIds->fIds[clusterNo];
237 }
238
239 const AliHLTTPCClusterMCLabel* AliHLTTPCDataCompressionDecoder::GetMCLabel(AliHLTUInt32_t clusterId) const
240 {
241   /// get MC data for a cluster Id
242   /// MC data is sent in a separate data block to keep the raw compressed
243   /// format free from any overhead
244   if (clusterId==kAliHLTVoidDataSpec) return NULL;
245
246   unsigned slice=AliHLTTPCSpacePointData::GetSlice(clusterId);
247   unsigned partition=AliHLTTPCSpacePointData::GetPatch(clusterId);
248   unsigned number=AliHLTTPCSpacePointData::GetNumber(clusterId);
249   if ((int)slice>=AliHLTTPCTransform::GetNSlice() ||
250       (int)partition>=AliHLTTPCTransform::GetNumberOfPatches()) return NULL;
251   unsigned index=slice*AliHLTTPCTransform::GetNumberOfPatches()+partition;
252   if (fClusterMCData.size()<=index ||
253       fClusterMCData[index]==NULL ||
254       fClusterMCData[index]->fCount<=number) return NULL;
255
256   return &(fClusterMCData[index]->fLabels[number]);
257 }
258
259 void AliHLTTPCDataCompressionDecoder::Clear(const char* option)
260 {
261   /// cleanup, tabula rase for next event
262   if (fpDataInflaterPartition) fpDataInflaterPartition->Clear(option);
263   if (fpDataInflaterTrack) fpDataInflaterTrack->Clear(option);
264   if (fpClusterMerger) fpClusterMerger->Clear();
265   fCurrentClusterIds=NULL;
266   fPartitionClusterIds.clear();
267   fTrackModelClusterIds.Clear();
268   fClusterMCData.clear();
269   fUseClusterMerger = kTRUE;
270 }