]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/TPCLib/AliHLTTPCHWClusterTransformComponent.cxx
bug fix: 8 words were read outside the data buffer
[u/mrichter/AliRoot.git] / HLT / TPCLib / AliHLTTPCHWClusterTransformComponent.cxx
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: Kalliopi Kanaki <Kalliopi.Kanaki@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   AliHLTTPCHWClusterTransformComponent.cxx
20     @author Kalliopi Kanaki
21     @date   
22     @brief 
23 */
24
25 #if __GNUC__>= 3
26 using namespace std;
27 #endif
28 #include "AliHLTTPCHWClusterTransformComponent.h"
29 #include "AliHLTTPCDefinitions.h"
30 #include "AliHLTTPCTransform.h"
31 #include "AliHLTTPCSpacePointData.h"
32 #include "AliHLTTPCClusterDataFormat.h"
33 #include "AliRawDataHeader.h"
34
35 #include "AliCDBManager.h"
36 #include "AliCDBEntry.h"
37 #include "AliTPCcalibDB.h"
38
39 #include "TMath.h"
40 #include "TObjString.h" 
41 #include <cstdlib>
42 #include <cerrno>
43 #include <sys/time.h>
44
45 ClassImp(AliHLTTPCHWClusterTransformComponent) //ROOT macro for the implementation of ROOT specific class methods
46
47 const char* AliHLTTPCHWClusterTransformComponent::fgkOCDBEntryHWTransform="HLT/ConfigTPC/TPCHWClusterTransform";
48
49 AliHLTTPCHWClusterTransformComponent::AliHLTTPCHWClusterTransformComponent()
50 :
51 fDataId(kFALSE),
52 fChargeThreshold(10),
53 fTransform(),
54 fBenchmark("HWClusterTransform")
55 {
56   // see header file for class documentation
57   // or
58   // refer to README to build package
59   // or
60   // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt  
61
62   fBenchmark.Reset();
63   fBenchmark.SetTimer(0,"total");
64 }
65
66 AliHLTTPCHWClusterTransformComponent::~AliHLTTPCHWClusterTransformComponent() { 
67 // see header file for class documentation
68 }
69
70 const char* AliHLTTPCHWClusterTransformComponent::GetComponentID() { 
71 // see header file for class documentation
72
73   return "TPCHWClusterTransform";
74 }
75
76 void AliHLTTPCHWClusterTransformComponent::GetInputDataTypes( vector<AliHLTComponentDataType>& list) { 
77   // see header file for class documentation
78
79   list.clear(); 
80   list.push_back( AliHLTTPCDefinitions::fgkHWClustersDataType );
81 }
82
83 AliHLTComponentDataType AliHLTTPCHWClusterTransformComponent::GetOutputDataType() { 
84   // see header file for class documentation
85
86   return AliHLTTPCDefinitions::fgkClustersDataType;
87 }
88
89 int AliHLTTPCHWClusterTransformComponent::GetOutputDataTypes(AliHLTComponentDataTypeList& tgtList) { 
90   // see header file for class documentation
91
92   tgtList.clear();
93   tgtList.push_back(AliHLTTPCDefinitions::fgkClustersDataType);
94   return tgtList.size();
95 }
96
97 void AliHLTTPCHWClusterTransformComponent::GetOutputDataSize( unsigned long& constBase, double& inputMultiplier ) { 
98   // see header file for class documentation
99   constBase = 0;
100   inputMultiplier = 3.0;
101 }
102
103 AliHLTComponent* AliHLTTPCHWClusterTransformComponent::Spawn() { 
104   // see header file for class documentation
105
106   return new AliHLTTPCHWClusterTransformComponent();
107 }
108         
109 int AliHLTTPCHWClusterTransformComponent::DoInit( int argc, const char** argv ) { 
110 // see header file for class documentation
111   
112   AliTPCcalibDB *calib=AliTPCcalibDB::Instance();  
113   if(!calib){
114     HLTError("AliTPCcalibDB does not exist");
115     return -ENOENT;
116   }
117   calib->SetRun(GetRunNo());
118   calib->UpdateRunInformations(GetRunNo());
119
120   int err = fTransform.Init( GetBz(), GetTimeStamp() );
121
122   if( err!=0 ){
123     HLTError("Cannot retrieve offline transform from AliTPCcalibDB");
124     return -ENOENT;
125   }
126
127   int iResult=0;
128   iResult = ConfigureFromCDBTObjString(fgkOCDBEntryHWTransform);
129
130   if (iResult>=0 && argc>0)
131     iResult=ConfigureFromArgumentString(argc, argv);
132
133   return iResult;
134 } // end DoInit()
135
136 int AliHLTTPCHWClusterTransformComponent::DoDeinit() { 
137   // see header file for class documentation   
138   return 0;
139 }
140
141 int AliHLTTPCHWClusterTransformComponent::DoEvent(const AliHLTComponentEventData& evtData, 
142                                                   const AliHLTComponentBlockData* blocks, 
143                                                   AliHLTComponentTriggerData& /*trigData*/, AliHLTUInt8_t* outputPtr, 
144                                                   AliHLTUInt32_t& size, 
145                                                   vector<AliHLTComponentBlockData>& outputBlocks ){
146   // see header file for class documentation
147    
148   if(GetFirstInputBlock( kAliHLTDataTypeSOR ) || GetFirstInputBlock( kAliHLTDataTypeEOR )){
149      size = 0;
150      return 0;
151   }
152
153   fBenchmark.StartNewEvent();
154   fBenchmark.Start(0);
155
156   fTransform.SetCurrentTimeStamp( GetTimeStamp() );
157
158   const AliHLTComponentBlockData *iter = NULL;    
159   unsigned long ndx; 
160
161   AliHLTUInt8_t* outBPtr;
162   UInt_t offset, mysize, nSize, tSize = 0;
163
164   outBPtr = outputPtr;
165       
166   for(ndx=0; ndx<evtData.fBlockCnt; ndx++){
167      
168      iter   = blocks+ndx;
169      mysize = 0;
170      offset = tSize;
171      
172      fBenchmark.AddInput(iter->fSize);
173
174      HLTDebug("Event 0x%08LX (%Lu) received datatype: %s - required datatype: %s",
175                evtData.fEventID, evtData.fEventID, 
176                DataType2Text( iter->fDataType).c_str(), 
177                DataType2Text(AliHLTTPCDefinitions::fgkHWClustersDataType).c_str());                       
178  
179      if(iter->fDataType != AliHLTTPCDefinitions::fgkHWClustersDataType) continue;                        
180   
181      UInt_t minSlice     = AliHLTTPCDefinitions::GetMinSliceNr(*iter); 
182      UInt_t minPartition = AliHLTTPCDefinitions::GetMinPatchNr(*iter);
183      //UInt_t maxSlice     = AliHLTTPCDefinitions::GetMaxSliceNr(*iter); 
184      //UInt_t maxPartition = AliHLTTPCDefinitions::GetMaxPatchNr(*iter);
185
186      fBenchmark.SetName(Form("HWClusterTransform slice %d patch %d",minSlice,minPartition));
187
188      HLTDebug("minSlice: %d, minPartition: %d", minSlice, minPartition);
189     
190      AliHLTTPCClusterData* outPtr  = (AliHLTTPCClusterData*)outBPtr;
191
192      unsigned long maxPoints = (size-tSize-sizeof(AliHLTTPCClusterData))/sizeof(AliHLTTPCSpacePointData);
193      
194      AliHLTUInt32_t *buffer;     
195      buffer = (AliHLTUInt32_t*)iter->fPtr;  
196      
197      /*  
198      //cluster fabrication
199      buffer = new AliHLTUInt32_t[14];
200      //header
201      buffer[0]=0xffffffff;
202      buffer[1]=0xffffffff;
203      buffer[2]=0xffffffff;
204      buffer[3]=0xffffffff;
205      buffer[4]=0xffffffff;
206      buffer[5]=0xffffffff;
207      buffer[6]=0xffffffff;
208      buffer[7]=0xffffffff;
209      //cluster 1
210      buffer[8]=0xC60002EF;
211      buffer[9]=0x0;
212      buffer[10]=0x0;
213      buffer[11]=0x0;
214      buffer[12]=0x0;
215      //RCU trailer
216      buffer[13]=0x80000000;
217      */
218
219      // PrintDebug(buffer, 14);
220      
221      // skip the first 8 32-bit CDH words
222      buffer += 8;
223      UInt_t bufferSize32 = ((Int_t)iter->fSize - sizeof(AliRawDataHeader) )/sizeof(AliHLTUInt32_t);
224
225      //PrintDebug(buffer, (Int_t)iter->fSize/sizeof(AliHLTUInt32_t));
226
227      unsigned long nAddedClusters = 0;
228      
229      for(UInt_t nWords=0; nWords<bufferSize32; nWords+=5){
230      //     for(UInt_t nWords=0; nWords<5; nWords+=5){
231
232         // check if bit 31 and 30 of the 32-bit word is 11 -> cluster (10 is RCU trailer)
233         AliHLTUInt32_t bit3130 = (buffer[nWords]>>30); // shift 30 to the right
234        
235
236         if(bit3130 == 0x3){ //beginning of a cluster
237
238            //PrintDebug(&buffer[nWords], 5);
239           
240            if(nAddedClusters>=maxPoints){
241               HLTWarning("No more space to add clusters, exiting!");
242               break;
243            }
244
245            
246            AliHLTTPCSpacePointData cluster = { 0.,0.,0.,0,0,0.,0.,0,0,kFALSE,0 };
247               
248            //get the first word
249            AliHLTUInt32_t  rowCharge = buffer[nWords];
250            AliHLTUInt8_t  *rowPtr    = reinterpret_cast<AliHLTUInt8_t*>(&rowCharge);      
251            rowPtr+=3; // this is to run for little endian architecture, the word is read from right to left
252         
253            cluster.fPadRow  = (UChar_t)((*rowPtr)&0x3f);
254            cluster.fCharge  = ((UInt_t)rowCharge&0xFFFFFF)>>6; //24-bit mask to get out the charge and division with 64(>>6) for the gain correction
255
256            Float_t tmpPad   = *((Float_t*)&buffer[nWords+1]);
257            Float_t tmpTime  = *((Float_t*)&buffer[nWords+2]);
258            cluster.fSigmaY2 = *((Float_t*)&buffer[nWords+3]);
259            cluster.fSigmaZ2 = *((Float_t*)&buffer[nWords+4]);
260            
261            
262            if(cluster.fCharge<fChargeThreshold) continue;
263            
264            // correct expressions for the error calculation
265            // Kenneth: 12.11.2009 I'm not sure if this is a correct calculation. Leave it out for now since it is anyway not used later since it caused segfaults.
266            // cluster.fSigmaY2 = TMath::Sqrt( *((Float_t*)&buffer[nWords+3]) - *((Float_t*)&buffer[nWords+1])* (*((Float_t*)&buffer[nWords+1])) );
267            // cluster.fSigmaZ2 = TMath::Sqrt( *((Float_t*)&buffer[nWords+3]) - *((Float_t*)&buffer[nWords+1])* (*((Float_t*)&buffer[nWords+1])) );
268                    
269            HLTDebug("padrow: %d, charge: %d, pad: %f, time: %f, errY: %f, errZ: %f \n", cluster.fPadRow, (UInt_t)cluster.fCharge, tmpPad, tmpTime, cluster.fSigmaY2, cluster.fSigmaZ2);            
270                     
271            cluster.fPadRow += AliHLTTPCTransform::GetFirstRow(minPartition);                 
272               
273            Float_t xyz[3];
274            fTransform.Transform( minSlice, cluster.fPadRow, tmpPad, tmpTime, xyz );
275            cluster.fX = xyz[0];
276            cluster.fY = xyz[1];
277            cluster.fZ = xyz[2];                            
278
279            // set the cluster ID so that the cluster dump printout is the same for FCF and SCF
280            cluster.SetID( minSlice, minPartition, nAddedClusters );
281
282            HLTDebug("Cluster number %d: %f, Y: %f, Z: %f, charge: %d \n", nAddedClusters, cluster.fX, cluster.fY, cluster.fZ, (UInt_t)cluster.fCharge);
283            outPtr->fSpacePoints[nAddedClusters] = cluster;
284                    
285            nAddedClusters++; 
286         } // end of clusters starting with 11=0x3
287         else if(bit3130 == 0x2){ // we have reached the beginning of the RCU trailer - 10=0x2
288           break;
289         }
290      } // end of loop over clusters
291      
292      HLTDebug("Number of found clusters: %d", nAddedClusters);
293      
294      outPtr->fSpacePointCnt = nAddedClusters;
295      nSize = sizeof(AliHLTTPCSpacePointData)*outPtr->fSpacePointCnt;
296      mysize += nSize+sizeof(AliHLTTPCClusterData);
297  
298      AliHLTComponentBlockData bd;
299      FillBlockData( bd );
300      bd.fOffset = offset;
301      bd.fSize = mysize;
302      bd.fSpecification = iter->fSpecification;
303      
304      if(fDataId==kFALSE) bd.fDataType = AliHLTTPCDefinitions::fgkClustersDataType;
305      else                bd.fDataType = AliHLTTPCDefinitions::fgkAlterClustersDataType;
306      
307      //HLTDebug("datatype: %s", DataType2Text(bd.fDataType).c_str());
308      
309      outputBlocks.push_back( bd );
310      
311      fBenchmark.AddOutput(bd.fSize);
312     
313      tSize   += mysize;
314      outBPtr += mysize;
315   
316    } // end of loop over data blocks
317
318   size = tSize;
319   
320   fBenchmark.Stop(0);
321   HLTInfo(fBenchmark.GetStatistics());
322
323   return 0;
324 } // end DoEvent()
325
326 int AliHLTTPCHWClusterTransformComponent::ScanConfigurationArgument(int argc, const char** argv){
327
328   // see header file for class documentation
329
330   if (argc<=0) return 0;
331   int i=0;
332   TString argument=argv[i];
333
334   if (argument.CompareTo("-solenoidBz")==0){
335     if (++i>=argc) return -EPROTO;
336     argument=argv[i];
337     AliTPCcalibDB*  calib=AliTPCcalibDB::Instance();
338     if(!calib){
339       HLTError("CalibDB instance cannot be created.");
340       return 0;
341     }
342     Float_t magneticField = argument.Atof();
343     calib->SetExBField(magneticField);
344     HLTInfo("SolenoidBz is set to %f in the calibDB",magneticField);
345     return 2;
346   }
347
348   if (argument.CompareTo("-change-dataId")==0){
349     HLTDebug("Change data ID received.");
350     fDataId = kTRUE;
351     return 1;
352   }
353   
354   if (argument.CompareTo("-charge-threshold")==0) {
355     if (++i>=argc) return -EPROTO;
356     argument=argv[i];
357     fChargeThreshold=(UInt_t)argument.Atoi();
358     HLTInfo("The charge threshold has been set to %d.", fChargeThreshold);
359     return 2;
360   }    
361
362   // unknown argument
363   return -EINVAL;
364 }
365
366 int AliHLTTPCHWClusterTransformComponent::Reconfigure(const char* /*cdbEntry*/, const char* /*chainId*/) { 
367   // see header file for class documentation
368   return ConfigureFromCDBTObjString(fgkOCDBEntryHWTransform);
369 }
370
371 void AliHLTTPCHWClusterTransformComponent::PrintDebug(AliHLTUInt32_t *buffer, Int_t size){
372 // see header file for class documentation 
373
374   HLTInfo("The size is: %d", size);
375   for(Int_t n32bit=0; n32bit<size; n32bit++){
376     
377     AliHLTUInt8_t *wordPtr = reinterpret_cast<AliHLTUInt8_t*>(&buffer[n32bit]);
378     //    cout << "word ptr initialized"<<endl;
379     for(Int_t w=3;w>=0;w--){
380       //     cout <<"accessing word"<<endl;
381       AliHLTUInt8_t word = wordPtr[w];
382       //     cout<< "word was accessed"<<endl; 
383       for(int n=7; n>=0; n--){
384         //print the byte values
385         if((((word>>n)<<7)&0x80) != 0){
386           printf("1");
387         }
388         else{
389           printf("0");
390         }
391       }
392       printf("  ");
393     }
394     printf("\n");
395   }
396 } // end of PrintDebug
397
398 void AliHLTTPCHWClusterTransformComponent::GetOCDBObjectDescription( TMap* const targetMap){
399 // Get a list of OCDB object description needed for the particular component
400   if (!targetMap) return;
401   targetMap->Add(new TObjString("HLT/ConfigTPC/TPCHWClusterTransform"), new TObjString("component argument for the charge threshold"));
402 }