]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/CALO/AliHLTCaloClusterAnalyser.cxx
removed string operation and coverity fix.
[u/mrichter/AliRoot.git] / HLT / CALO / AliHLTCaloClusterAnalyser.cxx
1 // $Id: AliHLTCaloClusterAnalyser.cxx 35107 2009-09-30 01:45:06Z phille $
2
3 /**************************************************************************
4  * This file is property of and copyright by the ALICE HLT Project        * 
5  * All rights reserved.                                                   *
6  *                                                                        *
7  * Primary Authors: Oystein Djuvsland                                     *
8  *                                                                        *
9  * Permission to use, copy, modify and distribute this software and its   *
10  * documentation strictly for non-commercial purposes is hereby granted   *
11  * without fee, provided that the above copyright notice appears in all   *
12  * copies and that both the copyright notice and this permission notice   *
13  * appear in the supporting documentation. The authors make no claims     *
14  * about the suitability of this software for any purpose. It is          * 
15  * provided "as is" without express or implied warranty.                  *
16  **************************************************************************/
17
18 /** 
19  * @file   AliHLTCaloClusterAnalyser.cxx
20  * @author Oystein Djuvsland
21  * @date 
22  * @brief  Cluster analyser for Calo HLT 
23  */
24
25 // see header file for class documentation
26 // or
27 // refer to README to build package
28 // or
29 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
30
31 #include "AliHLTCaloClusterAnalyser.h"
32 #include "AliHLTCaloRecPointHeaderStruct.h"
33 #include "AliHLTCaloRecPointDataStruct.h"
34 #include "AliHLTCaloClusterDataStruct.h"
35 //#include "AliHLTCaloPhysicsAnalyzer.h"
36 #include "AliHLTCaloGeometry.h"
37 #include "AliESDCaloCluster.h"
38 #include "TMath.h"
39 #include "TVector3.h"
40 #include "TH1F.h"
41 #include "TFile.h"
42 #include "AliHLTCaloRecoParamHandler.h"
43
44 ClassImp(AliHLTCaloClusterAnalyser);
45
46 AliHLTCaloClusterAnalyser::AliHLTCaloClusterAnalyser() : 
47   //  AliHLTCaloBase(),
48   fLogWeight(4.5),
49   fRecPointArray(0),
50   fDigitDataArray(0),
51   fNRecPoints(0),
52   fCaloClusterDataPtr(0),
53   fCaloClusterHeaderPtr(0),
54   //fAnalyzerPtr(0),
55   fDoClusterFit(false),
56   fHaveCPVInfo(false),
57   fDoPID(false),
58   fHaveDistanceToBadChannel(false),
59   fGeometry(0),
60 #ifndef HAVE_NOT_ALIVCLUSTER // backward compatibility for r42844
61   fClusterType(AliVCluster::kPHOSNeutral),
62 #else
63   fClusterType(AliESDCaloCluster::kPHOSCluster),
64 #endif
65   fRecoParamsPtr(0),
66   fCutOnSingleCellClusters(false),
67   fSingleCellEnergyCut(0.5)
68 {
69   //See header file for documentation
70 }
71
72 AliHLTCaloClusterAnalyser::~AliHLTCaloClusterAnalyser() 
73 {
74   // See header file for class documentation
75 }
76
77 void 
78 AliHLTCaloClusterAnalyser::SetCaloClusterData(AliHLTCaloClusterDataStruct *caloClusterDataPtr)
79
80   //see header file for documentation
81   fCaloClusterDataPtr = caloClusterDataPtr; 
82 }
83
84 void
85 AliHLTCaloClusterAnalyser::SetRecPointArray(AliHLTCaloRecPointDataStruct **recPointDataPtr, Int_t nRecPoints)
86
87   fRecPointArray = recPointDataPtr; 
88   fNRecPoints = nRecPoints;
89 }
90
91 void 
92 AliHLTCaloClusterAnalyser::SetDigitDataArray(AliHLTCaloDigitDataStruct **digits) 
93
94 //   AliHLTCaloClusterizer cl("PHOS");
95   // cl.CheckDigits(fRecPointArray, digits, fNRecPoints);
96    fDigitDataArray = digits; 
97    //cl.CheckDigits(fRecPointArray, fDigitDataArray, fNRecPoints);
98 }
99
100 Int_t
101 AliHLTCaloClusterAnalyser::CalculateCenterOfGravity()
102 {
103   //see header file for documentation
104   Float_t wtot = 0.;
105   Float_t x = 0.;
106   Float_t z = 0.;
107   Float_t xi = 0.;
108   Float_t zi = 0.;
109
110   AliHLTCaloDigitDataStruct *digit = 0;
111
112   UInt_t iDigit = 0;
113
114   for(Int_t iRecPoint=0; iRecPoint < fNRecPoints; iRecPoint++) 
115     {
116       AliHLTCaloRecPointDataStruct *recPoint = fRecPointArray[iRecPoint];
117       //      digit = &(recPoint->fDigits);
118
119       Int_t *digitIndexPtr = &(recPoint->fDigits);
120        wtot = 0;
121        x = 0; 
122        z = 0;
123
124 /*       Float_t maxAmp = 0;
125        Int_t maxX = 0;
126        Int_t maxZ = 0;*/
127       for(iDigit = 0; iDigit < recPoint->fMultiplicity; iDigit++)
128         {
129
130           digit = fDigitDataArray[*digitIndexPtr];
131
132           xi = digit->fX+0.5;
133           zi = digit->fZ+0.5;
134
135           if (recPoint->fAmp > 0 && digit->fEnergy > 0) 
136             {
137               Float_t w = TMath::Max( 0., fLogWeight + TMath::Log( digit->fEnergy / recPoint->fAmp ) ) ;
138               x    += xi * w ;
139               z    += zi * w ;
140               wtot += w ;
141 /*            if(digit->fEnergy > maxAmp)
142               {
143                  maxAmp = digit->fEnergy;
144                  maxX = digit->fX;// + 0.5;
145                  maxZ = digit->fZ;// + 0.5;
146               }*/
147             }
148           digitIndexPtr++;
149         }
150
151       if (wtot>0) 
152         {
153           recPoint->fX = x/wtot ;
154           recPoint->fZ = z/wtot ;
155         }
156       else
157         {
158           recPoint->fAmp = 0;
159         }
160 //      printf("Max digit: E = %f, x = %d, z= %d, cluster: E = %f, x = %f, z = %f\n" , maxAmp, maxX, maxZ, recPoint->fAmp, recPoint->fX, recPoint->fZ);
161     }
162   return 0;
163 }
164
165
166 Int_t 
167 AliHLTCaloClusterAnalyser::CalculateRecPointMoments()
168 {       
169   //See header file for documentation
170   return 0;
171 }
172
173 Int_t 
174 AliHLTCaloClusterAnalyser::CalculateClusterMoments(AliHLTCaloRecPointDataStruct */*recPointPtr*/, AliHLTCaloClusterDataStruct* /*clusterPtr*/)
175 {
176   //See header file for documentation
177   return 0;
178 }
179
180
181 Int_t 
182 AliHLTCaloClusterAnalyser::DeconvoluteClusters()
183 {
184   //See header file for documentation
185   return 0;
186 }
187
188 Int_t 
189 AliHLTCaloClusterAnalyser::CreateClusters(Int_t nRecPoints, UInt_t availableSize, UInt_t& totSize)
190 {
191   //See header file for documentation
192
193    if(fRecoParamsPtr)
194    {
195       fLogWeight = fRecoParamsPtr->GetLogWeight();
196    }
197    
198   fNRecPoints = nRecPoints;
199
200   if(fGeometry == 0)
201   {
202      HLTError("No geometry object is initialised, creation of clusters stopped");
203      return  -1;
204   }
205
206   CalculateCenterOfGravity();
207
208   //  AliHLTCaloDigitDataStruct* digitPtr = &(recPointPtr->fDigits);  
209   AliHLTCaloDigitDataStruct* digitPtr = 0;
210
211   AliHLTCaloClusterDataStruct* caloClusterPtr = 0;
212
213   //  Int_t id = -1;
214   TVector3 globalPos;
215
216   for(Int_t i = 0; i < fNRecPoints; i++) //TODO needs fix when we start unfolding (number of clusters not necessarily same as number of recpoints gotten from the clusterizer
217     {
218       if((availableSize - totSize)  < sizeof(AliHLTCaloClusterDataStruct))
219       {
220          HLTError("Out of buffer: available size is: %d, total size used: %d", availableSize, totSize);
221          return -ENOBUFS;
222       }
223       
224       AliHLTCaloRecPointDataStruct *recPointPtr = fRecPointArray[i];
225
226       if(fCutOnSingleCellClusters && recPointPtr->fAmp > fSingleCellEnergyCut && recPointPtr->fMultiplicity == 1) continue;
227       
228       totSize += sizeof(AliHLTCaloClusterDataStruct);
229       
230       caloClusterPtr = fCaloClusterDataPtr;
231       caloClusterPtr->fChi2 = 0;
232       caloClusterPtr->fClusterType = kUndef;
233       caloClusterPtr->fDispersion = 0;
234       caloClusterPtr->fDistanceToBadChannel = 0;
235       caloClusterPtr->fDistToBadChannel = 0;
236       caloClusterPtr->fEmcCpvDistance = 0;
237       caloClusterPtr->fEnergy = 0;
238       caloClusterPtr->fFitQuality = 0;
239       caloClusterPtr->fID = 0;
240       caloClusterPtr->fM02 = 0;
241       caloClusterPtr->fM20 = 0;
242       caloClusterPtr->fNCells = 0;
243       caloClusterPtr->fNExMax = 0;
244       caloClusterPtr->fTOF = 0;
245       caloClusterPtr->fTrackDx = -999;
246       caloClusterPtr->fTrackDz = -999;
247       
248       AliHLTCaloGlobalCoordinate globalCoord;
249       fGeometry->GetGlobalCoordinates(*recPointPtr, globalCoord);
250
251       caloClusterPtr->fModule = recPointPtr->fModule;
252       caloClusterPtr->fGlobalPos[0] =  globalCoord.fX;
253       caloClusterPtr->fGlobalPos[1] =  globalCoord.fY;
254       caloClusterPtr->fGlobalPos[2] =  globalCoord.fZ;
255
256       HLTDebug("Cluster local position: x = %f, z = %f, module = %d", recPointPtr->fX, recPointPtr->fZ, recPointPtr->fModule);
257       HLTDebug("Cluster global position: x = %f, y = %f, z = %f", globalCoord.fX, globalCoord.fY, globalCoord.fZ);
258       
259       //caloClusterPtr->fNCells = 0;//recPointPtr->fMultiplicity;
260       caloClusterPtr->fNCells = recPointPtr->fMultiplicity;
261
262       caloClusterPtr->fClusterType = fClusterType;
263 //      Int_t tmpSize = 0;//totSize + (caloClusterPtr->fNCells-1)*(sizeof(Short_t) + sizeof(Float_t));
264
265       //TODO remove hardcoded 10; 
266       memset(caloClusterPtr->fTracksMatched, 0xff, sizeof(Int_t)*10);
267
268       //Int_t tmpSize = totSize + (caloClusterPtr->fNCells-1)*(sizeof(Short_t) + sizeof(Float_t));
269       UInt_t tmpSize = (caloClusterPtr->fNCells-1)*sizeof(AliHLTCaloCellDataStruct);
270
271       if((availableSize - totSize)  < tmpSize)
272       {
273          HLTError("Out of buffer, available size is: %d, total size used: %d, extra size needed: %d", availableSize, totSize, tmpSize);
274          return -ENOBUFS;
275       }
276       
277       Int_t *digitIndexPtr = &(recPointPtr->fDigits);
278       Int_t id = 0;
279
280        AliHLTCaloCellDataStruct *cellPtr = &(caloClusterPtr->fCaloCells);
281        
282       for(UInt_t j = 0; j < caloClusterPtr->fNCells; j++)
283         {
284            digitPtr = fDigitDataArray[*digitIndexPtr];
285            fGeometry->GetCellAbsId(recPointPtr->fModule, digitPtr->fX, digitPtr->fZ, id);
286            
287           cellPtr->fCellsAbsId= id;
288           cellPtr->fCellsAmpFraction = digitPtr->fEnergy/recPointPtr->fAmp;
289           //printf("Cell ID pointer: %x\n", cellIDPtr);
290          //printf("Cell Amp Pointer: %x\n", cellAmpFracPtr);
291          //printf("Cell pos: x = %d, z = %d\n", digitPtr->fX, digitPtr->fZ);
292 //       printf("Cell ID: %d\n", cellPtr->fCellsAbsId);
293          //printf("Cell Amp: %f, pointer: %x\n", *cellAmpFracPtr, cellAmpFracPtr);
294           cellPtr++;
295           digitIndexPtr++;
296           
297         }
298
299       totSize += tmpSize;
300
301       if(fRecoParamsPtr)
302       {
303          caloClusterPtr->fEnergy = fRecoParamsPtr->GetCorrectedEnergy(recPointPtr->fAmp);
304       }
305       else
306       {
307          caloClusterPtr->fEnergy = recPointPtr->fAmp;
308       }
309       
310       HLTDebug("Cluster global position: x = %f, y = %f, z = %f, energy: %f, number of cells: %d, cluster pointer: %x", globalCoord.fX, globalCoord.fY, globalCoord.fZ, caloClusterPtr->fEnergy, caloClusterPtr->fNCells,  caloClusterPtr);
311
312       if(fDoClusterFit)
313         {
314           FitCluster(recPointPtr);
315         }
316       else
317         {
318           caloClusterPtr->fDispersion = 0;
319           caloClusterPtr->fFitQuality = 0;
320           caloClusterPtr->fM20 = 0;
321           caloClusterPtr->fM02 = 0;
322
323         }
324       if(fHaveCPVInfo)
325         {
326           caloClusterPtr->fEmcCpvDistance = GetCPVDistance(recPointPtr);
327         }
328       else
329         {
330           caloClusterPtr->fEmcCpvDistance = -1;
331         }
332       if(fDoPID)
333         {
334           DoParticleIdentification(caloClusterPtr);
335         }
336       else
337         {
338           for(Int_t k = 0; k < AliPID::kSPECIESN; k++)
339             {
340               caloClusterPtr->fPID[k] = 0;
341             }
342         }
343       if(fHaveDistanceToBadChannel)
344         {
345           caloClusterPtr->fDistanceToBadChannel = GetDistanceToBadChannel(caloClusterPtr);
346         }
347       else
348         {
349           caloClusterPtr->fDistanceToBadChannel = -1;
350         }
351
352       caloClusterPtr->fClusterType = fClusterType;
353           //totSize += sizeof(AliHLTCaloClusterDataStruct) + (caloClusterPtr->fNCells)*(sizeof(Short_t) +sizeof(Float_t)-1);   
354       //totSize += sizeof(AliHLTCaloClusterDataStruct) + (caloClusterPtr->fNCells-1)*(sizeof(Short_t) + sizeof(Float_t));   
355       //printf("CaloClusterPtr: %x, energy: %f\n", caloClusterPtr, caloClusterPtr->fEnergy);
356       
357       //      caloClusterPtr = reinterpret_cast<AliHLTCaloClusterDataStruct*>(cellAmpFracPtr);
358       //caloClusterPtr = reinterpret_cast<AliHLTCaloClusterDataStruct*>(cellIDPtr);
359       fCaloClusterDataPtr = reinterpret_cast<AliHLTCaloClusterDataStruct*>(cellPtr);
360       recPointPtr = reinterpret_cast<AliHLTCaloRecPointDataStruct*>(digitPtr);
361       //digitPtr = &(recPointPtr->fDigits);  
362     }
363
364 return fNRecPoints;
365
366 }
367