]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/TPCLib/AliHLTTPCHWClusterTransformComponent.cxx
Bug fixes in HLT cluster transformation:
[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 #include "AliHLTTPCHWClusterTransformComponent.h"
26 #include "AliHLTTPCDefinitions.h"
27 #include "AliHLTTPCTransform.h"
28 #include "AliHLTTPCSpacePointData.h"
29 #include "AliHLTTPCClusterDataFormat.h"
30 #include "AliRawDataHeader.h"
31 #include "AliHLTTPCRawCluster.h"
32 #include "AliHLTTPCHWCFEmulator.h"
33 #include "AliHLTTPCHWCFData.h"
34 #include "AliHLTErrorGuard.h"
35
36 #include "AliCDBManager.h"
37 #include "AliCDBEntry.h"
38 #include "AliTPCcalibDB.h"
39
40 #include "TMath.h"
41 #include "TObjString.h" 
42 #include <cstdlib>
43 #include <cerrno>
44 #include <sys/time.h>
45
46 using namespace std;
47
48 ClassImp(AliHLTTPCHWClusterTransformComponent) //ROOT macro for the implementation of ROOT specific class methods
49
50 const char* AliHLTTPCHWClusterTransformComponent::fgkOCDBEntryHWTransform="HLT/ConfigTPC/TPCHWClusterTransform";
51
52 AliHLTTPCHWClusterTransformComponent::AliHLTTPCHWClusterTransformComponent()
53 :
54 fDataId(kFALSE),
55 fTransform(),
56 fPublishRawClusters(kFALSE),
57 fpDecoder(NULL),
58 fBenchmark("HWClusterTransform")
59 {
60   // see header file for class documentation
61   // or
62   // refer to README to build package
63   // or
64   // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt  
65
66   fBenchmark.Reset();
67   fBenchmark.SetTimer(0,"total");
68 }
69
70 AliHLTTPCHWClusterTransformComponent::~AliHLTTPCHWClusterTransformComponent()
71
72   // destructor
73   if (!fpDecoder) delete fpDecoder;
74   fpDecoder=NULL;
75 }
76
77 const char* AliHLTTPCHWClusterTransformComponent::GetComponentID() { 
78 // see header file for class documentation
79
80   return "TPCHWClusterTransform";
81 }
82
83 void AliHLTTPCHWClusterTransformComponent::GetInputDataTypes( vector<AliHLTComponentDataType>& list) { 
84   // see header file for class documentation
85
86   list.clear(); 
87   list.push_back( AliHLTTPCDefinitions::fgkHWClustersDataType );
88 }
89
90 AliHLTComponentDataType AliHLTTPCHWClusterTransformComponent::GetOutputDataType() { 
91   // see header file for class documentation
92
93   return AliHLTTPCDefinitions::fgkClustersDataType;
94 }
95
96 int AliHLTTPCHWClusterTransformComponent::GetOutputDataTypes(AliHLTComponentDataTypeList& tgtList) { 
97   // see header file for class documentation
98
99   tgtList.clear();
100   tgtList.push_back(AliHLTTPCDefinitions::fgkClustersDataType| kAliHLTDataOriginTPC);
101   tgtList.push_back(AliHLTTPCDefinitions::fgkAliHLTDataTypeClusterMCInfo | kAliHLTDataOriginTPC );
102   tgtList.push_back(AliHLTTPCDefinitions::fgkRawClustersDataType  | kAliHLTDataOriginTPC );
103   return tgtList.size();
104 }
105
106 void AliHLTTPCHWClusterTransformComponent::GetOutputDataSize( unsigned long& constBase, double& inputMultiplier ) { 
107   // see header file for class documentation
108   constBase = 0;
109   inputMultiplier = 3.0;
110 }
111
112 AliHLTComponent* AliHLTTPCHWClusterTransformComponent::Spawn() { 
113   // see header file for class documentation
114
115   return new AliHLTTPCHWClusterTransformComponent();
116 }
117         
118 int AliHLTTPCHWClusterTransformComponent::DoInit( int argc, const char** argv ) { 
119 // see header file for class documentation
120   
121   AliTPCcalibDB *calib=AliTPCcalibDB::Instance();  
122   if(!calib){
123     HLTError("AliTPCcalibDB does not exist");
124     return -ENOENT;
125   }
126   calib->SetRun(GetRunNo());
127   calib->UpdateRunInformations(GetRunNo());
128   
129   int err = fTransform.Init( GetBz(), GetTimeStamp() );
130
131   if( err!=0 ){
132     HLTError(Form("Cannot retrieve offline transform from AliTPCcalibDB, AliHLTTPCClusterTransformation returns %d",err));
133     return -ENOENT;
134   }
135
136   int iResult=0;
137   iResult = ConfigureFromCDBTObjString(fgkOCDBEntryHWTransform);
138
139   if (iResult>=0 && argc>0)
140     iResult=ConfigureFromArgumentString(argc, argv);
141
142   if (iResult>=0) {
143     fpDecoder=new AliHLTTPCHWCFData;
144     if (!fpDecoder) iResult=-ENOMEM;
145   }
146   
147   return iResult;
148 } // end DoInit()
149
150 int AliHLTTPCHWClusterTransformComponent::DoDeinit() { 
151   // see header file for class documentation   
152   if (!fpDecoder) delete fpDecoder;
153   fpDecoder=NULL;
154
155   return 0;
156 }
157
158 int AliHLTTPCHWClusterTransformComponent::DoEvent(const AliHLTComponentEventData& evtData, 
159                                                   const AliHLTComponentBlockData* blocks, 
160                                                   AliHLTComponentTriggerData& /*trigData*/, AliHLTUInt8_t* outputPtr, 
161                                                   AliHLTUInt32_t& size, 
162                                                   vector<AliHLTComponentBlockData>& outputBlocks ){
163   // see header file for class documentation
164  
165   UInt_t maxOutSize = size;
166   size = 0;
167   int iResult = 0;
168   if(!IsDataEvent()) return 0;
169
170   if (!fpDecoder) return -ENODEV;
171
172   fBenchmark.StartNewEvent();
173   fBenchmark.Start(0);
174
175   // fTransform.SetCurrentTimeStamp( GetTimeStamp() ); !!! for the future
176   
177   for( unsigned long ndx=0; ndx<evtData.fBlockCnt; ndx++ ){
178      
179     const AliHLTComponentBlockData *iter   = blocks+ndx;
180     
181     fBenchmark.AddInput(iter->fSize);
182     
183     HLTDebug("Event 0x%08LX (%Lu) received datatype: %s - required datatype: %s",
184              evtData.fEventID, evtData.fEventID, 
185              DataType2Text( iter->fDataType).c_str(), 
186              DataType2Text(AliHLTTPCDefinitions::fgkHWClustersDataType).c_str());                       
187  
188     if(iter->fDataType == (AliHLTTPCDefinitions::fgkAliHLTDataTypeClusterMCInfo | kAliHLTDataOriginTPC) ){
189       // simply forward MC labels
190       
191       if( size+iter->fSize > maxOutSize ){
192         HLTWarning( "Output buffer (%db) is too small, required %db", maxOutSize, size+iter->fSize);
193         iResult  = -ENOSPC;
194         break;
195       }
196
197       memcpy( outputPtr, iter->fPtr, iter->fSize );
198       
199       AliHLTComponentBlockData bd;
200       FillBlockData( bd );
201       bd.fOffset = size;
202       bd.fSize = iter->fSize;
203       bd.fSpecification = iter->fSpecification;     
204       bd.fDataType = iter->fDataType;
205       outputBlocks.push_back( bd );     
206       fBenchmark.AddOutput(bd.fSize);    
207       size   += bd.fSize;
208       outputPtr += bd.fSize;
209       continue;
210     }
211
212     if(iter->fDataType != (AliHLTTPCDefinitions::fgkHWClustersDataType | kAliHLTDataOriginTPC)) continue;                        
213         
214     UInt_t minSlice     = AliHLTTPCDefinitions::GetMinSliceNr(*iter); 
215     UInt_t minPartition = AliHLTTPCDefinitions::GetMinPatchNr(*iter);
216     //UInt_t maxSlice     = AliHLTTPCDefinitions::GetMaxSliceNr(*iter); 
217     //UInt_t maxPartition = AliHLTTPCDefinitions::GetMaxPatchNr(*iter);
218     float padpitch=1.0;
219     if ((int)minPartition<AliHLTTPCTransform::GetNRowLow())
220       padpitch=AliHLTTPCTransform::GetPadPitchWidthLow();
221     else
222       padpitch=AliHLTTPCTransform::GetPadPitchWidthUp();
223     float zwidth=AliHLTTPCTransform::GetZWidth();
224
225     fBenchmark.SetName(Form("HWClusterTransform slice %d patch %d",minSlice,minPartition));
226
227     HLTDebug("minSlice: %d, minPartition: %d", minSlice, minPartition);
228     
229     AliHLTTPCClusterData* outPtr  = (AliHLTTPCClusterData*)outputPtr;
230     outPtr->fSpacePointCnt=0;
231
232     long maxPoints = ((long)maxOutSize-size-sizeof(AliHLTTPCClusterData))/sizeof(AliHLTTPCSpacePointData);
233
234     AliHLTUInt32_t *buffer;     
235     buffer = (AliHLTUInt32_t*)iter->fPtr;  
236      
237      // skip the first 8 32-bit CDH words
238      buffer += 8;
239      UInt_t bufferSize32 = ((Int_t)iter->fSize - sizeof(AliRawDataHeader) )/sizeof(AliHLTUInt32_t);
240
241      if (fpDecoder->Init(reinterpret_cast<AliHLTUInt8_t*>(buffer), bufferSize32*sizeof(AliHLTUInt32_t))>=0 && fpDecoder->CheckVersion()>=0) {
242        for (AliHLTTPCHWCFData::iterator cl=fpDecoder->begin(); cl!=fpDecoder->end(); ++cl) {
243          if(outPtr->fSpacePointCnt>=maxPoints){
244            HLTWarning("No more space to add clusters, exiting!");
245            iResult  = -ENOSPC;
246            break;
247          }
248
249          AliHLTTPCSpacePointData& c=outPtr->fSpacePoints[outPtr->fSpacePointCnt];
250          int padrow=cl.GetPadRow();
251          if (padrow<0) {
252            // something wrong here, padrow is stored in the cluster header
253            // word which has bit pattern 0x3 in bits bit 30 and 31 which was
254            // not recognized
255            ALIHLTERRORGUARD(1, "can not read cluster header word");
256            break;
257          }
258          padrow+=AliHLTTPCTransform::GetFirstRow(minPartition);
259          AliHLTUInt32_t charge=cl.GetCharge();
260
261          float pad=cl.GetPad();
262          float time=cl.GetTime();
263          float sigmaY2=cl.GetSigmaY2();
264          float sigmaZ2=cl.GetSigmaZ2();
265          sigmaY2*=padpitch*padpitch;
266          sigmaZ2*=zwidth*zwidth;
267          c.SetPadRow(padrow);
268          c.SetCharge(charge);
269          c.SetSigmaY2(sigmaY2);
270          c.SetSigmaZ2(sigmaZ2);
271          c.SetQMax(cl.GetQMax());
272
273          Float_t xyz[3];
274          fTransform.Transform( minSlice, padrow, pad, time, xyz );       
275          c.SetX(xyz[0]);
276          c.SetY(xyz[1]);
277          c.SetZ(xyz[2]);
278
279          // set the cluster ID so that the cluster dump printout is the same for FCF and SCF
280          c.SetID( minSlice, minPartition, outPtr->fSpacePointCnt );
281          
282          HLTDebug("Cluster number %d: %f, Y: %f, Z: %f, charge: %d \n", outPtr->fSpacePointCnt, cluster.fX, cluster.fY, cluster.fZ, (UInt_t)cluster.fCharge);
283          
284          outPtr->fSpacePointCnt++; 
285        } // end of loop over clusters
286      }     
287      HLTDebug("Number of found clusters: %d", outPtr->fSpacePointCnt);
288      
289      UInt_t mysize = sizeof(AliHLTTPCClusterData) + sizeof(AliHLTTPCSpacePointData)*outPtr->fSpacePointCnt;
290  
291      AliHLTComponentBlockData bd;
292      FillBlockData( bd );
293      bd.fOffset = size;
294      bd.fSize = mysize;
295      bd.fSpecification = iter->fSpecification;     
296      if(fDataId==kFALSE) bd.fDataType = AliHLTTPCDefinitions::fgkClustersDataType;
297      else                bd.fDataType = AliHLTTPCDefinitions::fgkAlterClustersDataType;
298      
299      //HLTDebug("datatype: %s", DataType2Text(bd.fDataType).c_str());
300      
301      outputBlocks.push_back( bd );
302      
303      fBenchmark.AddOutput(bd.fSize);    
304      size   += mysize;
305      outputPtr += mysize;
306   
307      if (fPublishRawClusters) {
308
309        long maxRawClusters = ((long)maxOutSize-size-sizeof(AliHLTTPCRawClusterData))/sizeof(AliHLTTPCRawCluster);
310        
311        if( maxRawClusters<=0 ) {
312          HLTWarning("No more space to add raw clusters, exiting!");
313          iResult  = -ENOSPC;
314        } else {       
315
316          // copy raw cluster data from input
317          
318          AliHLTTPCRawClusterData* outputRaw= (AliHLTTPCRawClusterData*)(outputPtr);
319        
320          outputRaw->fVersion = 0;
321          outputRaw->fCount = 0;
322
323          // check if there are clusters available, if not the format might
324          // not even been decoded at that moment 
325          if (fpDecoder->GetNumberOfClusters()>0) {
326          for (AliHLTTPCHWCFData::iterator cl=fpDecoder->begin(); cl!=fpDecoder->end(); ++cl) {
327            if(outputRaw->fCount>=maxRawClusters){
328              HLTWarning("No more space to add clusters, exiting!");
329              iResult  = -ENOSPC;
330              break;
331            }
332            AliHLTTPCRawCluster &c = outputRaw->fClusters[outputRaw->fCount];
333            int padrow=cl.GetPadRow();
334            if (padrow<0) {
335              // something wrong here, padrow is stored in the cluster header
336              // word which has bit pattern 0x3 in bits bit 30 and 31 which was
337              // not recognized
338              break;
339            }
340            padrow+=AliHLTTPCTransform::GetFirstRow(minPartition);
341            AliHLTUInt32_t charge= cl.GetCharge();
342
343            float pad =cl.GetPad();
344            float time =cl.GetTime();
345            float sigmaP2=cl.GetSigmaY2();
346            float sigmaT2=cl.GetSigmaZ2();
347            c.SetPadRow(padrow);
348            c.SetCharge(charge);
349            c.SetPad(pad);  
350            c.SetTime(time);
351            c.SetSigmaY2(sigmaP2);
352            c.SetSigmaZ2(sigmaT2);
353            c.SetQMax(cl.GetQMax());
354
355            // store cluster and continue
356            outputRaw->fCount++;
357          }
358          }
359
360          // fill into HLT output data
361          AliHLTComponentBlockData bdRawClusters;
362          FillBlockData( bdRawClusters );
363          bdRawClusters.fOffset = size;
364          bdRawClusters.fSize = sizeof(AliHLTTPCRawClusterData)+outputRaw->fCount*sizeof(AliHLTTPCRawCluster);
365          bdRawClusters.fSpecification = iter->fSpecification;
366          bdRawClusters.fDataType = AliHLTTPCDefinitions::fgkRawClustersDataType | kAliHLTDataOriginTPC;
367          outputBlocks.push_back( bdRawClusters );
368          fBenchmark.AddOutput(bdRawClusters.fSize);
369          size   += bdRawClusters.fSize;
370          outputPtr += bdRawClusters.fSize;
371        }
372      }
373   } // end of loop over data blocks  
374   
375   fBenchmark.Stop(0);
376   HLTInfo(fBenchmark.GetStatistics());
377   
378   return iResult;
379 } // end DoEvent()
380
381 int AliHLTTPCHWClusterTransformComponent::ScanConfigurationArgument(int argc, const char** argv){
382
383   // see header file for class documentation
384
385   if (argc<=0) return 0;
386   int i=0;
387   TString argument=argv[i];
388
389   if (argument.CompareTo("-solenoidBz")==0){
390     if (++i>=argc) return -EPROTO;
391     argument=argv[i];
392     AliTPCcalibDB*  calib=AliTPCcalibDB::Instance();
393     if(!calib){
394       HLTError("CalibDB instance cannot be created.");
395       return 0;
396     }
397     Float_t magneticField = argument.Atof();
398     calib->SetExBField(magneticField);
399     HLTInfo("SolenoidBz is set to %f in the calibDB",magneticField);
400     return 2;
401   }
402
403   if (argument.CompareTo("-change-dataId")==0){
404     HLTDebug("Change data ID received.");
405     fDataId = kTRUE;
406     return 1;
407   }
408   
409   if (argument.CompareTo("-charge-threshold")==0) {
410     if (++i>=argc) return -EPROTO;
411     argument=argv[i];    
412     HLTInfo("The argument -charge-threshold is deprecated.");
413     return 2;
414   }    
415
416   if (argument.CompareTo("-publish-raw")==0) {
417     fPublishRawClusters=kTRUE;
418     return 1;
419   }  
420   
421   // unknown argument
422   return -EINVAL;
423 }
424
425 int AliHLTTPCHWClusterTransformComponent::Reconfigure(const char* /*cdbEntry*/, const char* /*chainId*/) { 
426   // see header file for class documentation
427   return ConfigureFromCDBTObjString(fgkOCDBEntryHWTransform);
428 }
429
430 void AliHLTTPCHWClusterTransformComponent::PrintDebug(AliHLTUInt32_t *buffer, Int_t size){
431 // see header file for class documentation 
432
433   HLTInfo("The size is: %d", size);
434   for(Int_t n32bit=0; n32bit<size; n32bit++){
435     
436     AliHLTUInt8_t *wordPtr = reinterpret_cast<AliHLTUInt8_t*>(&buffer[n32bit]);
437     //    cout << "word ptr initialized"<<endl;
438     for(Int_t w=3;w>=0;w--){
439       //     cout <<"accessing word"<<endl;
440       AliHLTUInt8_t word = wordPtr[w];
441       //     cout<< "word was accessed"<<endl; 
442       for(int n=7; n>=0; n--){
443         //print the byte values
444         if((((word>>n)<<7)&0x80) != 0){
445           printf("1");
446         }
447         else{
448           printf("0");
449         }
450       }
451       printf("  ");
452     }
453     printf("\n");
454   }
455 } // end of PrintDebug
456
457 void AliHLTTPCHWClusterTransformComponent::GetOCDBObjectDescription( TMap* const targetMap)
458 {
459   // Get a list of OCDB object description needed for the particular component
460   if (!targetMap) return;
461   
462   // OCDB entries for component arguments
463
464   targetMap->Add(new TObjString("HLT/ConfigTPC/TPCHWClusterTransform"), new TObjString("component argument for the charge threshold"));
465   
466   // OCDB entries to be fetched by the TAXI (access via the AliTPCcalibDB class)
467   targetMap->Add(new TObjString("TPC/Calib/Parameters"),    new TObjString("unknown content"));
468   targetMap->Add(new TObjString("TPC/Calib/TimeDrift"),     new TObjString("drift velocity calibration"));
469   targetMap->Add(new TObjString("TPC/Calib/TimeGain"),     new TObjString("time gain  calibration"));
470   targetMap->Add(new TObjString("TPC/Calib/Temperature"),   new TObjString("temperature map"));
471   targetMap->Add(new TObjString("TPC/Calib/PadGainFactor"), new TObjString("gain factor pad by pad"));
472   targetMap->Add(new TObjString("TPC/Calib/ClusterParam"),  new TObjString("cluster parameters"));
473   targetMap->Add(new TObjString("TPC/Calib/Correction"),  new TObjString("coreection"));
474   targetMap->Add(new TObjString("TPC/Calib/RecoParam"),  new TObjString("reconstruction parameters"));
475  
476   // OCDB entries needed to be fetched by the Pendolino
477   targetMap->Add(new TObjString("TPC/Calib/AltroConfig"), new TObjString("contains the altro config, e.g. info about the L0 trigger timing"));
478   targetMap->Add(new TObjString("GRP/CTP/CTPtiming"),     new TObjString("content used in the cluster coordinate transformation in relation to the L0 trigger timing"));
479
480   // OCDB entries necessary for replaying data on the HLT cluster
481   targetMap->Add(new TObjString("GRP/GRP/Data"), new TObjString("contains magnetic field info"));  
482  
483   // OCDB entries needed to suppress fatals/errors/warnings during reconstruction
484   targetMap->Add(new TObjString("TPC/Calib/Distortion"),  new TObjString("distortion map"));
485   targetMap->Add(new TObjString("TPC/Calib/GainFactorDedx"), new TObjString("gain factor dedx"));
486   targetMap->Add(new TObjString("TPC/Calib/PadTime0"),    new TObjString("time0 offset pad by pad"));
487   targetMap->Add(new TObjString("TPC/Calib/PadNoise"),    new TObjString("pad noise values"));
488   targetMap->Add(new TObjString("TPC/Calib/Pedestals"),   new TObjString("pedestal info"));
489   targetMap->Add(new TObjString("TPC/Calib/Pulser"),      new TObjString("pulser info"));
490   targetMap->Add(new TObjString("TPC/Calib/CE"),          new TObjString("CE laser calibration result"));
491   targetMap->Add(new TObjString("TPC/Calib/Raw"),         new TObjString("unknown content"));
492   targetMap->Add(new TObjString("TPC/Calib/QA"),          new TObjString("not important"));
493   targetMap->Add(new TObjString("TPC/Calib/Mapping"),     new TObjString("unknown content"));
494   targetMap->Add(new TObjString("TPC/Calib/Goofie"),      new TObjString("Goofie values, not used at the moment (05.03.2010)"));
495   targetMap->Add(new TObjString("TPC/Calib/HighVoltage"), new TObjString("high voltage values, not used"));
496   targetMap->Add(new TObjString("TPC/Calib/Ref"),         new TObjString("unknown content"));
497 }