o add missing ClassImp
[u/mrichter/AliRoot.git] / HLT / TPCLib / AliHLTTPCClusterAccessHLTOUT.cxx
CommitLineData
c54aa300 1// $Id$
2
3//**************************************************************************
4//* This file is property of and copyright by the ALICE HLT Project *
5//* ALICE Experiment at CERN, All rights reserved. *
6//* *
7//* Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no> *
8//* for The ALICE HLT Project. *
9//* *
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//**************************************************************************
18
19/// @file AliHLTTPCClusterAccessHLTOUT.h
20/// @author Matthias Richter
21/// @date 2011-06-06
22/// @brief Interface to HLT TPC clusters
23///
24
25#include "AliHLTTPCClusterAccessHLTOUT.h"
26#include "AliHLTTPCDefinitions.h"
27#include "AliHLTTPCClusterDataFormat.h"
28#include "AliHLTOUT.h"
81e7f739 29#include "AliHLTComponent.h"
c54aa300 30#include "AliLog.h"
31#include "AliHLTSystem.h"
32#include "AliHLTPluginBase.h"
33#include "AliTPCclusterMI.h"
34#include "TClonesArray.h"
81e7f739 35#include <cstdlib>
36#include <string>
37#include <memory>
c54aa300 38
39/** ROOT macro for the implementation of ROOT specific class methods */
40ClassImp(AliHLTTPCClusterAccessHLTOUT)
41
42AliHLTTPCClusterAccessHLTOUT::AliHLTTPCClusterAccessHLTOUT()
43 : TObject()
81e7f739 44 , fVerbosity(0)
c54aa300 45 , fClusters(NULL)
46{
47 // see header file for class documentation
48 // or
49 // refer to README to build package
50 // or
51 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
52}
53
54AliHLTTPCClusterAccessHLTOUT::~AliHLTTPCClusterAccessHLTOUT()
55{
56 // destructor
57 if (fClusters) {
58 fClusters->Clear();
59 delete fClusters;
60 fClusters=NULL;
61 }
62}
63
81e7f739 64void AliHLTTPCClusterAccessHLTOUT::Execute(const char *method, const char *params, Int_t *error)
c54aa300 65{
66 /// inherited from TObject: abstract command interface
67 if (strcmp(method, "read")==0) {
81e7f739 68 int iResult=ProcessClusters(params);
69 if (error) *error=iResult;
70 return;
71 }
72 if (strcmp(method, "verbosity")==0) {
73 int iResult=0;
74 if (params) {
75 char* dummy;
76 int value=strtol(params, &dummy, 0);
77 if (dummy==NULL) {
78 fVerbosity=value;
79 } else {
80 AliError("invalid argument for command 'verbosity', expecting string with number");
81 }
82 } else {
83 iResult=-EINVAL;
84 }
c54aa300 85 if (error) *error=iResult;
86 return;
87 }
88}
89
90TObject* AliHLTTPCClusterAccessHLTOUT::FindObject(const char *name) const
91{
92 /// inherited from TObject: return the cluster array if name id "clusterarray"
93 if (strcmp(name, "clusterarray")==0) return fClusters;
94 return TObject::FindObject(name);
95}
96
97void AliHLTTPCClusterAccessHLTOUT::Clear(Option_t * /*option*/)
98{
99 /// inherited from TObject: cleanup
100 if (fClusters) fClusters->Clear();
101}
102
103void AliHLTTPCClusterAccessHLTOUT::Print(Option_t */*option*/) const
104{
105 /// inherited from TObject
106 if (!fClusters) return;
107 for (int i=0; i<fClusters->GetEntriesFast(); i++) {
108 if (!fClusters->At(i)) continue;
109 AliTPCclusterMI* pCluster=dynamic_cast<AliTPCclusterMI*>(fClusters->At(i));
110 if (!pCluster) break;
111 cout << "AliTPCclusterMI:"
112 << " row=" << pCluster->GetRow()
113 << " pad=" << pCluster->GetPad()
114 << " time=" << pCluster->GetTimeBin()
115 << " charge=" << pCluster->GetQ()
116 << " maxq=" << pCluster->GetMax()
117 << endl;
118 }
119}
120
81e7f739 121int AliHLTTPCClusterAccessHLTOUT::ProcessClusters(const char* params)
c54aa300 122{
123 /// process the cluster data from HLTOUT and fill array
124 /// the cluster data can be in many different formats, e.g.
125 /// raw or compressed
126 int iResult=0;
81e7f739 127 TString strparams(params);
128 int minSlice=0, maxSlice=35, minPart=0, maxPart=5;
129 std::auto_ptr<TObjArray> tokens(strparams.Tokenize(" "));
130 if (!tokens.get()) return -ENOMEM;
131 for (int i=0; i< tokens->GetEntriesFast(); i++) {
132 if (!tokens->At(i)) continue;
133 TString argument=tokens->At(i)->GetName();
134 if (argument.BeginsWith("sector=")) {
135 argument.ReplaceAll("sector=", "");
136 int sector=argument.Atoi();
137 // the offline code enumerates first the 36 inner (partitions 0+1) and then 36 outer
138 // sectors (partitions 2-5)
139 if (fVerbosity>0) AliInfo(Form("processing HLT clusters for sector %d", sector));
140 if (sector<36) { // inner sectors
141 minSlice=maxSlice=sector;
142 minPart=0; maxPart=1;
143 } else { // outer sectors
144 minSlice=maxSlice=sector-36;
145 minPart=2; maxPart=5;
146 }
147 }
148 }
149
c54aa300 150 if (!fClusters) {
151 fClusters=new TClonesArray("AliTPCclusterMI");
152 }
153 if (!fClusters) return -ENOMEM;
154
155 AliHLTSystem* pSystem=AliHLTPluginBase::GetInstance();
156 if (!pSystem) {
157 AliError("can not access HLT system");
158 return -ENODEV;
159 }
160 AliHLTOUT* pHLTOUT=pSystem->RequestHLTOUT();
161 if (!pHLTOUT) {
162 AliError("can not access HLTOUT");
163 return -ENODEV;
164 }
165
81e7f739 166 for (int slice=minSlice; slice<=maxSlice; slice++) {
167 for (int part=minPart; part<=maxPart; part++) {
168 if (fVerbosity>0) AliInfo(Form("processing HLT clusters for slice %d partitions %d", slice, part));
169 AliHLTUInt32_t spec=slice<<24 | slice<<16 | part<<8 | part;
170 AliHLTTPCClusterMCDataList tpcClusterLabels;
171 if (pHLTOUT->SelectFirstDataBlock(AliHLTTPCDefinitions::fgkAliHLTDataTypeClusterMCInfo, spec)>=0) {
172 iResult=ReadAliHLTTPCClusterMCData(pHLTOUT, tpcClusterLabels);
173 }
174
175 if (pHLTOUT->SelectFirstDataBlock(AliHLTTPCDefinitions::fgkClustersDataType, spec)>=0) {
176 iResult=ReadAliHLTTPCClusterData(pHLTOUT, fClusters);
177 }
178 }
c54aa300 179 }
180
181 pSystem->ReleaseHLTOUT(pHLTOUT);
182 return iResult;
183}
184
81e7f739 185int AliHLTTPCClusterAccessHLTOUT::ReadAliHLTTPCClusterMCData(AliHLTOUT* pHLTOUT, AliHLTTPCClusterMCDataList &tpcClusterLabels) const
186{
187 // read cluster data from AliHLTTPCClusterData
188 int iResult=0;
189 if (!pHLTOUT) return -EINVAL;
190 do {
191 const AliHLTUInt8_t* pBuffer=NULL;
192 AliHLTUInt32_t size=0;
193 if ((iResult=pHLTOUT->GetDataBuffer(pBuffer, size))<0) {
194 continue;
195 }
196 if (pBuffer==NULL || size<4) {
197 AliError("invalid cluster mc data block");
198 continue;
199 }
200 const AliHLTTPCClusterMCData* clusterMCData = reinterpret_cast<const AliHLTTPCClusterMCData*>(pBuffer);
201 Int_t nLabels = (Int_t) clusterMCData->fCount;
202 if (nLabels*sizeof(AliHLTTPCClusterMCLabel) + sizeof(AliHLTTPCClusterMCData) != size) {
203 AliError("inconsistent cluster mc data block size, skipping block");
204 continue;
205 }
206 // id of the cluster is
207 AliHLTComponentDataType dt=kAliHLTVoidDataType;
208 AliHLTUInt32_t specification=kAliHLTVoidDataSpec;
209 if (pHLTOUT->GetDataBlockDescription(dt, specification)<0) {
210 AliError("failed to retrieve data block description, skipping mc cluster data block ...");
211 continue;
212 }
213 AliHLTUInt8_t slice = AliHLTTPCDefinitions::GetMinSliceNr(specification);
214 AliHLTUInt8_t partition = AliHLTTPCDefinitions::GetMinPatchNr(specification);
215 if (slice!=AliHLTTPCDefinitions::GetMaxSliceNr(specification) ||
216 partition!=AliHLTTPCDefinitions::GetMaxPatchNr(specification)) {
217 AliError(Form("can not read cluster mc data block with data of multiple partitions, skipping block %s %08x",
218 AliHLTComponent::DataType2Text(dt).c_str(), specification));
219 continue;
220 }
221 const AliHLTTPCClusterMCLabel *labels = clusterMCData->fLabels;
222 for (int i=0; i<nLabels; i++) {
223 AliHLTUInt32_t id=AliHLTTPCSpacePointData::GetID(slice, partition, i);
224 if (tpcClusterLabels.find(id)==tpcClusterLabels.end()) {
225 // new cluster
226 tpcClusterLabels[id]=labels[i];
227 } else {
228 AliError(Form("cluster with ID 0x%08x already existing, skipping cluster %d of data block 0x%08x",
229 id, i, specification));
230 }
231 }
232 } while (pHLTOUT->SelectNextDataBlock()>=0);
233 return iResult;
234}
235
236int AliHLTTPCClusterAccessHLTOUT::ReadAliHLTTPCClusterData(AliHLTOUT* pHLTOUT, TClonesArray* pClusters, const AliHLTTPCClusterMCDataList *tpcClusterLabels) const
c54aa300 237{
238 // read cluster data from AliHLTTPCClusterData
239 int iResult=0;
240 if (!pHLTOUT || !pClusters) return -EINVAL;
241 do {
242 const AliHLTUInt8_t* pBuffer=NULL;
243 AliHLTUInt32_t size=0;
244 if ((iResult=pHLTOUT->GetDataBuffer(pBuffer, size))<0) {
245 continue;
246 }
247 if (pBuffer==NULL || size<4) {
248 AliError("invalid cluster data block");
249 continue;
250 }
81e7f739 251 AliHLTComponentDataType dt=kAliHLTVoidDataType;
252 AliHLTUInt32_t specification=kAliHLTVoidDataSpec;
253 if (pHLTOUT->GetDataBlockDescription(dt, specification)<0) {
254 AliError("failed to retrieve data block description, skipping mc cluster data block ...");
255 continue;
256 }
c54aa300 257 const AliHLTTPCClusterData* clusterData = reinterpret_cast<const AliHLTTPCClusterData*>(pBuffer);
258 Int_t nSpacepoints = (Int_t) clusterData->fSpacePointCnt;
259 if (nSpacepoints*sizeof(AliHLTTPCSpacePointData) + sizeof(AliHLTTPCClusterData) != size) {
260 AliError("inconsistent cluster data block size, skipping block");
261 continue;
262 }
263 const AliHLTTPCSpacePointData *clusters = clusterData->fSpacePoints;
81e7f739 264 int offset=pClusters->GetEntries();
265 pClusters->ExpandCreate(offset+nSpacepoints);
c54aa300 266 for (int i=0; i<nSpacepoints; i++) {
81e7f739 267 if (!pClusters->At(offset+i)) continue;
268 AliTPCclusterMI* pCluster=dynamic_cast<AliTPCclusterMI*>(pClusters->At(offset+i));
c54aa300 269 if (!pCluster) {
270 AliError("invalid object type, expecting AliTPCclusterMI");
271 break; // this is a problem of all objects
272 }
273 pCluster->SetRow(clusters[i].fPadRow);
274 pCluster->SetTimeBin(clusters[i].fZ);
81e7f739 275 pCluster->SetPad(clusters[i].fY);
276 pCluster->SetSigmaY2(clusters[i].fSigmaY2);
277 pCluster->SetSigmaZ2(clusters[i].fSigmaZ2);
c54aa300 278 pCluster->SetQ(clusters[i].fCharge);
279 pCluster->SetMax(clusters[i].fQMax);
81e7f739 280 if (tpcClusterLabels) {
281 if (tpcClusterLabels->find(clusters[i].fID)!=tpcClusterLabels->end()) {
282 const AliHLTTPCClusterMCWeight* mcWeights=tpcClusterLabels->find(clusters[i].fID)->second.fClusterID;
283 for (int k=0; k<3; k++) {
284 // TODO: sort the labels according to the weight in order to assign the most likely mc label
285 // to the first component
286 pCluster->SetLabel(mcWeights[k].fMCID, k);
287 }
288 } else {
289 AliError(Form("can not find mc label of cluster with id %0x08x", clusters[i].fID));
290 }
291 }
c54aa300 292 }
81e7f739 293 if (fVerbosity>0) AliInfo(Form("converted %d cluster(s) from block 0x%08x", nSpacepoints, specification));
c54aa300 294 } while (pHLTOUT->SelectNextDataBlock()>=0);
295 return iResult;
296}