]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/TPCLib/AliHLTTPCHWClusterTransformComponent.cxx
Updated the code to use new argument/ocdb entry reading schema. Added the corrections...
[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
34 #include "AliTPCcalibDB.h"
35 #include "AliTPCTransform.h"
36 #include "AliTPCCalPad.h"
37
38 #include "AliCDBManager.h"
39 #include "AliCDBEntry.h"
40
41 #include "TMath.h"
42 #include "TObjString.h" 
43 #include <cstdlib>
44 #include <cerrno>
45 #include <sys/time.h>
46
47 ClassImp(AliHLTTPCHWClusterTransformComponent) //ROOT macro for the implementation of ROOT specific class methods
48
49 const char* AliHLTTPCHWClusterTransformComponent::fgkOCDBEntryHWTransform="HLT/ConfigTPC/TPCHWClusterTransform";
50
51 AliHLTTPCHWClusterTransformComponent::AliHLTTPCHWClusterTransformComponent()
52 :
53 fOfflineTransform(NULL),
54 fDataId(kFALSE),
55 fOfflineTPCRecoParam()
56 {
57   // see header file for class documentation
58   // or
59   // refer to README to build package
60   // or
61   // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt  
62 }
63
64 AliHLTTPCHWClusterTransformComponent::~AliHLTTPCHWClusterTransformComponent() { 
65 // see header file for class documentation
66 }
67
68 const char* AliHLTTPCHWClusterTransformComponent::GetComponentID() { 
69 // see header file for class documentation
70
71   return "TPCHWClusterTransform";
72 }
73
74 void AliHLTTPCHWClusterTransformComponent::GetInputDataTypes( vector<AliHLTComponentDataType>& list) { 
75   // see header file for class documentation
76
77   list.clear(); 
78   list.push_back( AliHLTTPCDefinitions::fgkHWClustersDataType );
79 }
80
81 AliHLTComponentDataType AliHLTTPCHWClusterTransformComponent::GetOutputDataType() { 
82   // see header file for class documentation
83
84   return AliHLTTPCDefinitions::fgkClustersDataType;
85 }
86
87 int AliHLTTPCHWClusterTransformComponent::GetOutputDataTypes(AliHLTComponentDataTypeList& tgtList) { 
88   // see header file for class documentation
89
90   tgtList.clear();
91   tgtList.push_back(AliHLTTPCDefinitions::fgkClustersDataType);
92   return tgtList.size();
93 }
94
95 void AliHLTTPCHWClusterTransformComponent::GetOutputDataSize( unsigned long& constBase, double& inputMultiplier ) { 
96   // see header file for class documentation
97   constBase = 0;
98   inputMultiplier = 3.0;
99 }
100
101 AliHLTComponent* AliHLTTPCHWClusterTransformComponent::Spawn() { 
102   // see header file for class documentation
103
104   return new AliHLTTPCHWClusterTransformComponent();
105 }
106         
107 int AliHLTTPCHWClusterTransformComponent::DoInit( int argc, const char** argv ) { 
108 // see header file for class documentation
109
110   AliTPCcalibDB* pCalib=AliTPCcalibDB::Instance();
111   if(!pCalib ||
112      !(fOfflineTransform = AliTPCcalibDB::Instance()->GetTransform())){
113     HLTError("Cannot retrieve offline transform from AliTPCcalibDB (%p)", pCalib);
114     return -ENOENT;
115   }
116   // set the flags in the reco param
117   fOfflineTPCRecoParam.SetUseExBCorrection(1);
118   fOfflineTPCRecoParam.SetUseTOFCorrection(1);
119   fOfflineTransform->SetCurrentRecoParam(&fOfflineTPCRecoParam);
120   
121   pCalib->SetExBField(GetBz());
122   
123   //Int_t i = 0;
124   //Char_t* cpErr;
125
126   int iResult=0;
127
128   iResult = ConfigureFromCDBTObjString(fgkOCDBEntryHWTransform);
129
130   if (iResult>=0 && argc>0)
131     iResult=ConfigureFromArgumentString(argc, argv);
132
133   return iResult;
134
135   /*
136   TString configuration="";
137   TString argument="";
138   for(int j=0; j<argc && iResult>=0; j++){
139       argument=argv[j];
140       if (!configuration.IsNull()) configuration+=" ";
141       configuration+=argument;  
142   }
143   
144   if (!configuration.IsNull()) {
145     iResult=Configure(configuration.Data());
146   } 
147   else {
148     //iResult=Reconfigure(NULL, NULL);
149   }
150   
151   return 0;
152   */
153 } // end DoInit()
154
155 int AliHLTTPCHWClusterTransformComponent::DoDeinit() { 
156   // see header file for class documentation 
157    
158   return 0;
159 }
160
161 int AliHLTTPCHWClusterTransformComponent::DoEvent(const AliHLTComponentEventData& evtData, 
162                                                   const AliHLTComponentBlockData* blocks, 
163                                                   AliHLTComponentTriggerData& /*trigData*/, AliHLTUInt8_t* outputPtr, 
164                                                   AliHLTUInt32_t& size, 
165                                                   vector<AliHLTComponentBlockData>& outputBlocks ){
166   // see header file for class documentation
167   if(GetFirstInputBlock( kAliHLTDataTypeSOR ) || GetFirstInputBlock( kAliHLTDataTypeEOR )){
168      size = 0;
169      return 0;
170   }
171   const AliHLTComponentBlockData *iter = NULL;    
172   unsigned long ndx;
173
174   AliHLTTPCClusterData* outPtr;
175
176   AliHLTUInt8_t* outBPtr;
177   UInt_t offset, mysize, nSize, tSize = 0;
178
179   outBPtr = outputPtr;
180   outPtr  = (AliHLTTPCClusterData*)outBPtr;
181   
182   AliHLTTPCSpacePointData *spacePoints = outPtr->fSpacePoints;
183
184   unsigned long maxPoints = 0;
185
186   for(ndx=0; ndx<evtData.fBlockCnt; ndx++){
187      
188      iter   = blocks+ndx;
189      mysize = 0;
190      offset = tSize;
191  
192      HLTDebug("Event 0x%08LX (%Lu) received datatype: %s - required datatype: %s",
193                evtData.fEventID, evtData.fEventID, 
194                DataType2Text( iter->fDataType).c_str(), 
195                DataType2Text(AliHLTTPCDefinitions::fgkHWClustersDataType).c_str());                       
196  
197      if(iter->fDataType != AliHLTTPCDefinitions::fgkHWClustersDataType) continue;                        
198   
199      UInt_t minSlice     = AliHLTTPCDefinitions::GetMinSliceNr(*iter); 
200      UInt_t minPartition = AliHLTTPCDefinitions::GetMinPatchNr(*iter);
201      //UInt_t maxSlice     = AliHLTTPCDefinitions::GetMaxSliceNr(*iter); 
202      //UInt_t maxPartition = AliHLTTPCDefinitions::GetMaxPatchNr(*iter);
203      
204      HLTDebug("minSlice: %d, minPartition: %d", minSlice, minPartition);
205     
206      outPtr = (AliHLTTPCClusterData*)outBPtr;
207      maxPoints = (size-tSize-sizeof(AliHLTTPCClusterData))/sizeof(AliHLTTPCSpacePointData);
208      
209      AliHLTUInt32_t *buffer;     
210      buffer = (AliHLTUInt32_t*)iter->fPtr;  
211      
212      /*  
213      //cluster fabrication
214      buffer = new AliHLTUInt32_t[14];
215      //header
216      buffer[0]=0xffffffff;
217      buffer[1]=0xffffffff;
218      buffer[2]=0xffffffff;
219      buffer[3]=0xffffffff;
220      buffer[4]=0xffffffff;
221      buffer[5]=0xffffffff;
222      buffer[6]=0xffffffff;
223      buffer[7]=0xffffffff;
224      //cluster 1
225      buffer[8]=0xC60002EF;
226      buffer[9]=0x0;
227      buffer[10]=0x0;
228      buffer[11]=0x0;
229      buffer[12]=0x0;
230      //RCU trailer
231      buffer[13]=0x80000000;
232      */
233
234      Int_t sector=-99, thisrow=-99;
235
236      // PrintDebug(buffer, 14);
237      
238      // skip the first 8 32-bit CDH words
239      buffer += 8;
240
241      //PrintDebug(buffer, (Int_t)iter->fSize/sizeof(AliHLTUInt32_t));
242
243      unsigned long nAddedClusters = 0;
244      
245      for(UInt_t nWords=0; nWords<(iter->fSize/sizeof(AliHLTUInt32_t)); nWords+=5){
246      //     for(UInt_t nWords=0; nWords<5; nWords+=5){
247          
248         // check if bit 31 and 30 of the 32-bit word is 11 -> cluster (10 is RCU trailer)
249         AliHLTUInt32_t bit3130 = (buffer[nWords]>>30); // shift 30 to the right
250        
251
252         if(bit3130 == 0x3){ //beginning of a cluster
253
254            //PrintDebug(&buffer[nWords], 5);
255           
256            if(nAddedClusters>=maxPoints){
257               HLTWarning("No more space to add clusters, exiting!");
258               break;
259            }
260            
261            AliHLTTPCSpacePointData cluster = { 0.,0.,0.,0,0,0.,0.,0,0,kFALSE,0 };
262               
263
264            //get the first word
265            AliHLTUInt32_t  rowCharge = buffer[nWords];
266            AliHLTUInt8_t  *rowPtr    = reinterpret_cast<AliHLTUInt8_t*>(&rowCharge);      
267            rowPtr+=3; // this is to run for little endian architecture, the word is read from right to left
268
269         
270            cluster.fPadRow  = (UChar_t)((*rowPtr)&0x3f);
271            cluster.fCharge  = ((UInt_t)rowCharge&0xFFFFFF)>>6; //24-bit mask to get out the charge and division with 64(>>6) for the gain correction
272
273
274            Float_t tmpPad   = *((Float_t*)&buffer[nWords+1]);
275            Float_t tmpTime  = *((Float_t*)&buffer[nWords+2]);
276            //cluster.fSigmaY2 = *((Float_t*)&buffer[nWords+3]);
277            //cluster.fSigmaZ2 = *((Float_t*)&buffer[nWords+4]);
278            
279            // correct expressions for the error calculation
280            // 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.
281            // cluster.fSigmaY2 = TMath::Sqrt( *((Float_t*)&buffer[nWords+3]) - *((Float_t*)&buffer[nWords+1])* (*((Float_t*)&buffer[nWords+1])) );
282            // cluster.fSigmaZ2 = TMath::Sqrt( *((Float_t*)&buffer[nWords+3]) - *((Float_t*)&buffer[nWords+1])* (*((Float_t*)&buffer[nWords+1])) );
283
284            Float_t xyz[3]; xyz[0] = xyz[1] = xyz[2] = -99.;
285                    
286            HLTDebug("padrow: %d, charge: %f, pad: %f, time: %f, errY: %f, errZ: %f \n", cluster.fPadRow, (Float_t)cluster.fCharge, tmpPad, tmpTime, cluster.fSigmaY2, cluster.fSigmaZ2);                   
287            
288
289            if(fOfflineTransform == NULL){                  
290
291               cluster.fPadRow += AliHLTTPCTransform::GetFirstRow(minPartition);                    
292               AliHLTTPCTransform::Slice2Sector(minSlice, cluster.fPadRow, sector, thisrow);           
293               AliHLTTPCTransform::Raw2Local(xyz, sector, thisrow, tmpPad, tmpTime); 
294               if(minSlice>17) xyz[1]=(-1)*xyz[1];          
295               cluster.fX = xyz[0];
296               cluster.fY = xyz[1];
297               cluster.fZ = xyz[2]; 
298               
299
300            } else {
301              cluster.fPadRow += AliHLTTPCTransform::GetFirstRow(minPartition);                     
302              AliHLTTPCTransform::Slice2Sector(minSlice, (UInt_t)cluster.fPadRow, sector, thisrow);            
303              Double_t x[3] = {(Double_t)cluster.fPadRow,tmpPad+.5,tmpTime}; 
304              Int_t iSector[1]= {sector};
305              fOfflineTransform->Transform(x,iSector,0,1);
306              cluster.fX = x[0];
307              cluster.fY = x[1];
308              cluster.fZ = x[2];       
309            }
310            
311
312            HLTDebug("cluster X: %f, Y: %f, Z: %f \n", cluster.fX, cluster.fY, cluster.fZ);
313            spacePoints[nAddedClusters] = cluster;
314                    
315            nAddedClusters++; 
316         } // end of clusters starting with 11=0x3
317         else if(bit3130 == 0x2){ // we have reached the beginning of the RCU trailer - 10=0x2
318           break;
319         }
320      } // end of loop over clusters
321      
322      HLTDebug("Number of found clusters: %d", nAddedClusters);
323      
324      outPtr->fSpacePointCnt = nAddedClusters;
325      nSize = sizeof(AliHLTTPCSpacePointData)*outPtr->fSpacePointCnt;
326      mysize += nSize+sizeof(AliHLTTPCClusterData);
327  
328      AliHLTComponentBlockData bd;
329      FillBlockData( bd );
330      bd.fOffset = offset;
331      bd.fSize = mysize;
332      bd.fSpecification = iter->fSpecification;
333      
334      if(fDataId==kFALSE) bd.fDataType = AliHLTTPCDefinitions::fgkClustersDataType;
335      else                bd.fDataType = AliHLTTPCDefinitions::fgkAlterClustersDataType;
336      
337      //HLTDebug("datatype: %s", DataType2Text(bd.fDataType).c_str());
338      
339      outputBlocks.push_back( bd );
340      
341      tSize   += mysize;
342      outBPtr += mysize;
343      outPtr   = (AliHLTTPCClusterData*)outBPtr;
344   
345    } // end of loop over data blocks
346    size = tSize;
347    return 0;
348 } // end DoEvent()
349
350 int AliHLTTPCHWClusterTransformComponent::ScanConfigurationArgument(int argc, const char** argv){
351
352   // see header file for class documentation
353
354   if (argc<=0) return 0;
355   int i=0;
356   TString argument=argv[i];
357
358   if (argument.CompareTo("-solenoidBz")==0){
359     if (++i>=argc) return -EPROTO;
360     argument=argv[i];
361     AliTPCcalibDB*  calib=AliTPCcalibDB::Instance();
362     if(!calib){
363       HLTError("CalibDB not availible");
364     }
365     Float_t magneticField = argument.Atof();
366     calib->SetExBField(magneticField);
367     HLTInfo("SolenoidBz is set to %f in the calibDB",magneticField);
368     return 2;
369   }
370
371   if (argument.CompareTo("-change-dataId")==0){
372     HLTDebug("Change data ID received.");
373     fDataId = kTRUE;
374     return 1;
375   }
376
377   // unknown argument
378   return -EINVAL;
379 }
380
381
382   
383 int AliHLTTPCHWClusterTransformComponent::Configure(const char* arguments) { 
384   // see header file for class documentation
385   
386   int iResult=0;
387   if (!arguments) return iResult;
388   HLTInfo("parsing configuration string \'%s\'", arguments);
389
390   TString allArgs=arguments;
391   TString argument;
392   int bMissingParam=0;
393
394   TObjArray* pTokens=allArgs.Tokenize(" ");
395   if (pTokens) {
396     for (int i=0; i<pTokens->GetEntries() && iResult>=0; i++) {
397       argument=((TObjString*)pTokens->At(i))->GetString();
398       if (argument.IsNull()) continue;
399      
400       if (argument.CompareTo("-change-dataId")==0) {
401         fDataId = kTRUE;
402         HLTInfo("got \'-change-dataId\': %s", ((TObjString*)pTokens->At(i))->GetString().Data());       
403       } 
404       else {
405         HLTError("unknown argument %s", argument.Data());
406         iResult=-EINVAL;
407         break;
408       }
409     } // end for
410   
411     delete pTokens;
412   
413   } // end if pTokens
414   
415   if (bMissingParam) {
416     HLTError("missing parameter for argument %s", argument.Data());
417     iResult=-EINVAL;
418   }
419   return iResult;
420 }
421
422 int AliHLTTPCHWClusterTransformComponent::Reconfigure(const char* /*cdbEntry*/, const char* /*chainId*/) { 
423   // see header file for class documentation
424   return ConfigureFromCDBTObjString(fgkOCDBEntryHWTransform);
425 }
426
427 void AliHLTTPCHWClusterTransformComponent::PrintDebug(AliHLTUInt32_t *buffer, Int_t size){
428 // see header file for class documentation 
429
430   HLTInfo("The size is: %d", size);
431   for(Int_t n32bit=0; n32bit<size; n32bit++){
432     
433     AliHLTUInt8_t *wordPtr = reinterpret_cast<AliHLTUInt8_t*>(&buffer[n32bit]);
434     //    cout << "word ptr initialized"<<endl;
435     for(Int_t w=3;w>=0;w--){
436       //     cout <<"accessing word"<<endl;
437       AliHLTUInt8_t word = wordPtr[w];
438       //     cout<< "word was accessed"<<endl; 
439       for(int n=7; n>=0; n--){
440         //print the byte values
441         if((((word>>n)<<7)&0x80) != 0){
442           printf("1");
443         }
444         else{
445           printf("0");
446         }
447       }
448       printf("  ");
449     }
450     printf("\n");
451   }
452 } // end of PrintDebug