]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/CALO/AliHLTCaloClusterAnalyser.cxx
- stop clusterisation on no geometry
[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   fClusterType(AliESDCaloCluster::kPHOSCluster),
61   fRecoParamsPtr(0)
62 {
63   //See header file for documentation
64 }
65
66 AliHLTCaloClusterAnalyser::~AliHLTCaloClusterAnalyser() 
67 {
68   // See header file for class documentation
69 }
70
71 void 
72 AliHLTCaloClusterAnalyser::SetCaloClusterData(AliHLTCaloClusterDataStruct *caloClusterDataPtr)
73
74   //see header file for documentation
75   fCaloClusterDataPtr = caloClusterDataPtr; 
76 }
77
78 void
79 AliHLTCaloClusterAnalyser::SetRecPointArray(AliHLTCaloRecPointDataStruct **recPointDataPtr, Int_t nRecPoints)
80
81   fRecPointArray = recPointDataPtr; 
82   fNRecPoints = nRecPoints;
83 }
84
85 void 
86 AliHLTCaloClusterAnalyser::SetDigitDataArray(AliHLTCaloDigitDataStruct *digits) 
87
88 //   AliHLTCaloClusterizer cl("PHOS");
89   // cl.CheckDigits(fRecPointArray, digits, fNRecPoints);
90    fDigitDataArray = digits; 
91    //cl.CheckDigits(fRecPointArray, fDigitDataArray, fNRecPoints);
92 }
93
94 Int_t
95 AliHLTCaloClusterAnalyser::CalculateCenterOfGravity()
96 {
97   //see header file for documentation
98   Float_t wtot = 0.;
99   Float_t x = 0.;
100   Float_t z = 0.;
101   Float_t xi = 0.;
102   Float_t zi = 0.;
103
104   AliHLTCaloDigitDataStruct *digit = 0;
105
106   UInt_t iDigit = 0;
107
108   for(Int_t iRecPoint=0; iRecPoint < fNRecPoints; iRecPoint++) 
109     {
110       AliHLTCaloRecPointDataStruct *recPoint = fRecPointArray[iRecPoint];
111       //      digit = &(recPoint->fDigits);
112
113       Int_t *digitIndexPtr = &(recPoint->fDigits);
114        wtot = 0;
115        x = 0; 
116        z = 0;
117
118 /*       Float_t maxAmp = 0;
119        Int_t maxX = 0;
120        Int_t maxZ = 0;*/
121       for(iDigit = 0; iDigit < recPoint->fMultiplicity; iDigit++)
122         {
123
124           digit = &(fDigitDataArray[*digitIndexPtr]);
125
126           xi = digit->fX+0.5;
127           zi = digit->fZ+0.5;
128
129           if (recPoint->fAmp > 0 && digit->fEnergy > 0) 
130             {
131               Float_t w = TMath::Max( 0., fLogWeight + TMath::Log( digit->fEnergy / recPoint->fAmp ) ) ;
132               x    += xi * w ;
133               z    += zi * w ;
134               wtot += w ;
135 /*            if(digit->fEnergy > maxAmp)
136               {
137                  maxAmp = digit->fEnergy;
138                  maxX = digit->fX;// + 0.5;
139                  maxZ = digit->fZ;// + 0.5;
140               }*/
141             }
142           digitIndexPtr++;
143         }
144
145       if (wtot>0) 
146         {
147           recPoint->fX = x/wtot ;
148           recPoint->fZ = z/wtot ;
149         }
150       else
151         {
152           recPoint->fAmp = 0;
153         }
154 //      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);
155     }
156   return 0;
157 }
158
159
160 Int_t 
161 AliHLTCaloClusterAnalyser::CalculateRecPointMoments()
162 {       
163   //See header file for documentation
164   return 0;
165 }
166
167 Int_t 
168 AliHLTCaloClusterAnalyser::CalculateClusterMoments(AliHLTCaloRecPointDataStruct */*recPointPtr*/, AliHLTCaloClusterDataStruct* /*clusterPtr*/)
169 {
170   //See header file for documentation
171   return 0;
172 }
173
174
175 Int_t 
176 AliHLTCaloClusterAnalyser::DeconvoluteClusters()
177 {
178   //See header file for documentation
179   return 0;
180 }
181
182 Int_t 
183 AliHLTCaloClusterAnalyser::CreateClusters(Int_t nRecPoints, UInt_t availableSize, UInt_t& totSize)
184 {
185   //See header file for documentation
186
187    if(fRecoParamsPtr)
188    {
189       fLogWeight = fRecoParamsPtr->GetLogWeight();
190    }
191    
192   fNRecPoints = nRecPoints;
193
194   if(fGeometry == 0)
195   {
196      HLTError("No geometry object is initialised, creation of clusters stopped");
197      return  -1;
198   }
199
200   CalculateCenterOfGravity();
201
202   //  AliHLTCaloDigitDataStruct* digitPtr = &(recPointPtr->fDigits);  
203   AliHLTCaloDigitDataStruct* digitPtr = 0;
204
205   AliHLTCaloClusterDataStruct* caloClusterPtr = 0;
206
207   //  Int_t id = -1;
208   TVector3 globalPos;
209
210   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
211     {
212       if((availableSize - totSize)  < sizeof(AliHLTCaloClusterDataStruct))
213       {
214          HLTError("Out of buffer: available size is: %d, total size used: %d", availableSize, totSize);
215          return -ENOBUFS;
216       }
217       totSize += sizeof(AliHLTCaloClusterDataStruct);
218       
219       caloClusterPtr = fCaloClusterDataPtr;
220       caloClusterPtr->fChi2 = 0;
221       caloClusterPtr->fClusterType = kUndef;
222       caloClusterPtr->fDispersion = 0;
223       caloClusterPtr->fDistanceToBadChannel = 0;
224       caloClusterPtr->fDistToBadChannel = 0;
225       caloClusterPtr->fEmcCpvDistance = 0;
226       caloClusterPtr->fEnergy = 0;
227       caloClusterPtr->fFitQuality = 0;
228       caloClusterPtr->fID = 0;
229       caloClusterPtr->fM02 = 0;
230       caloClusterPtr->fM20 = 0;
231       caloClusterPtr->fNCells = 0;
232       caloClusterPtr->fNExMax = 0;
233       caloClusterPtr->fTOF = 0;
234       caloClusterPtr->fTrackDx = 0;
235       caloClusterPtr->fTrackDz = 0;
236      
237       AliHLTCaloRecPointDataStruct *recPointPtr = fRecPointArray[i];
238       
239       AliHLTCaloGlobalCoordinate globalCoord;
240       fGeometry->GetGlobalCoordinates(*recPointPtr, globalCoord);
241
242       caloClusterPtr->fModule = recPointPtr->fModule;
243       caloClusterPtr->fGlobalPos[0] =  globalCoord.fX;
244       caloClusterPtr->fGlobalPos[1] =  globalCoord.fY;
245       caloClusterPtr->fGlobalPos[2] =  globalCoord.fZ;
246
247       HLTDebug("Cluster local position: x = %f, z = %f, module = %d", recPointPtr->fX, recPointPtr->fZ, recPointPtr->fModule);
248       HLTDebug("Cluster global position: x = %f, y = %f, z = %f", globalCoord.fX, globalCoord.fY, globalCoord.fZ);
249       
250       //caloClusterPtr->fNCells = 0;//recPointPtr->fMultiplicity;
251       caloClusterPtr->fNCells = recPointPtr->fMultiplicity;
252
253       caloClusterPtr->fClusterType = fClusterType;
254 //      Int_t tmpSize = 0;//totSize + (caloClusterPtr->fNCells-1)*(sizeof(Short_t) + sizeof(Float_t));
255
256       //TODO remove hardcoded 10; 
257       memset(caloClusterPtr->fTracksMatched, 0xff, sizeof(Int_t)*10);
258
259       //Int_t tmpSize = totSize + (caloClusterPtr->fNCells-1)*(sizeof(Short_t) + sizeof(Float_t));
260       UInt_t tmpSize = (caloClusterPtr->fNCells-1)*sizeof(AliHLTCaloCellDataStruct);
261
262       if((availableSize - totSize)  < tmpSize)
263       {
264          HLTError("Out of buffer, available size is: %d, total size used: %d, extra size needed: %d", availableSize, totSize, tmpSize);
265          return -ENOBUFS;
266       }
267       
268       Int_t *digitIndexPtr = &(recPointPtr->fDigits);
269       Int_t id = 0;
270
271        AliHLTCaloCellDataStruct *cellPtr = &(caloClusterPtr->fCaloCells);
272        
273       for(UInt_t j = 0; j < caloClusterPtr->fNCells; j++)
274         {
275            digitPtr = &(fDigitDataArray[*digitIndexPtr]);
276            fGeometry->GetCellAbsId(recPointPtr->fModule, digitPtr->fX, digitPtr->fZ, id);
277            
278           cellPtr->fCellsAbsId= id;
279           cellPtr->fCellsAmpFraction = digitPtr->fEnergy/recPointPtr->fAmp;
280           //printf("Cell ID pointer: %x\n", cellIDPtr);
281          //printf("Cell Amp Pointer: %x\n", cellAmpFracPtr);
282          //printf("Cell pos: x = %d, z = %d\n", digitPtr->fX, digitPtr->fZ);
283 //       printf("Cell ID: %d\n", cellPtr->fCellsAbsId);
284          //printf("Cell Amp: %f, pointer: %x\n", *cellAmpFracPtr, cellAmpFracPtr);
285           cellPtr++;
286           digitIndexPtr++;
287           
288         }
289
290       totSize += tmpSize;
291
292       if(fRecoParamsPtr)
293       {
294          caloClusterPtr->fEnergy = fRecoParamsPtr->GetCorrectedEnergy(recPointPtr->fAmp);
295       }
296       else
297       {
298          caloClusterPtr->fEnergy = recPointPtr->fAmp;
299       }
300       
301       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);
302
303       if(fDoClusterFit)
304         {
305           FitCluster(recPointPtr);
306         }
307       else
308         {
309           caloClusterPtr->fDispersion = 0;
310           caloClusterPtr->fFitQuality = 0;
311           caloClusterPtr->fM20 = 0;
312           caloClusterPtr->fM02 = 0;
313
314         }
315       if(fHaveCPVInfo)
316         {
317           caloClusterPtr->fEmcCpvDistance = GetCPVDistance(recPointPtr);
318         }
319       else
320         {
321           caloClusterPtr->fEmcCpvDistance = -1;
322         }
323       if(fDoPID)
324         {
325           DoParticleIdentification(caloClusterPtr);
326         }
327       else
328         {
329           for(Int_t k = 0; k < AliPID::kSPECIESN; k++)
330             {
331               caloClusterPtr->fPID[k] = 0;
332             }
333         }
334       if(fHaveDistanceToBadChannel)
335         {
336           caloClusterPtr->fDistanceToBadChannel = GetDistanceToBadChannel(caloClusterPtr);
337         }
338       else
339         {
340           caloClusterPtr->fDistanceToBadChannel = -1;
341         }
342
343       caloClusterPtr->fClusterType = fClusterType;
344           //totSize += sizeof(AliHLTCaloClusterDataStruct) + (caloClusterPtr->fNCells)*(sizeof(Short_t) +sizeof(Float_t)-1);   
345       //totSize += sizeof(AliHLTCaloClusterDataStruct) + (caloClusterPtr->fNCells-1)*(sizeof(Short_t) + sizeof(Float_t));   
346       //printf("CaloClusterPtr: %x, energy: %f\n", caloClusterPtr, caloClusterPtr->fEnergy);
347       
348       //      caloClusterPtr = reinterpret_cast<AliHLTCaloClusterDataStruct*>(cellAmpFracPtr);
349       //caloClusterPtr = reinterpret_cast<AliHLTCaloClusterDataStruct*>(cellIDPtr);
350       fCaloClusterDataPtr = reinterpret_cast<AliHLTCaloClusterDataStruct*>(cellPtr);
351       recPointPtr = reinterpret_cast<AliHLTCaloRecPointDataStruct*>(digitPtr);
352       //digitPtr = &(recPointPtr->fDigits);  
353     }
354
355 return fNRecPoints;
356
357 }
358