3 /**************************************************************************
4 * This file is property of and copyright by the ALICE HLT Project *
5 * All rights reserved. *
7 * Primary Authors: Oystein Djuvsland *
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 **************************************************************************/
19 * @file AliHLTCaloClusterizer.cxx
20 * @author Oystein Djuvsland
22 * @brief Clusterizer for PHOS HLT
25 // see header file for class documentation
27 // refer to README to build package
29 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
31 #include "AliHLTCaloClusterizer.h"
32 #include "AliHLTLogging.h"
34 #include "AliHLTCaloRecPointDataStruct.h"
35 #include "AliHLTCaloDigitDataStruct.h"
36 #include "AliHLTCaloDigitContainerDataStruct.h"
37 #include "AliHLTCaloConstantsHandler.h"
39 ClassImp(AliHLTCaloClusterizer);
41 AliHLTCaloClusterizer::AliHLTCaloClusterizer(TString det):
42 AliHLTCaloConstantsHandler(det),
43 fCompareFunction(CompareDigitsByPosition),
52 fEmcClusteringThreshold(0),
53 fEmcMinEnergyThreshold(0),
56 fDigitsPointerArray(0),
57 fDigitContainerPtr(0),
58 fMaxDigitIndexDiff(0),
60 fSortedByPosition(false),
61 fSortedByEnergy(false),
67 //See header file for documentation
68 //fEmcClusteringThreshold = 0.2;
69 //fEmcMinEnergyThreshold = 0.03;
71 fEmcClusteringThreshold = 0.1;
72 fEmcMinEnergyThreshold = 0.01;
73 fEmcTimeGate = 1.e-6 ;
75 fMaxDigitIndexDiff = 2*fCaloConstants->GetNZROWSMOD();
79 fRecPointArray = new AliHLTCaloRecPointDataStruct*[fArraySize];
81 fAvailableSize = sizeof(AliHLTCaloRecPointDataStruct) * 20;
82 fBuffer = new UChar_t[fAvailableSize]; //FR
84 fFirstRecPointPtr = reinterpret_cast<AliHLTCaloRecPointDataStruct*>(fBuffer);
85 fRecPointDataPtr = fFirstRecPointPtr;
89 AliHLTCaloClusterizer::~AliHLTCaloClusterizer()
91 //See header file for documentation
92 delete [] fBuffer; //FR
94 fAvailableSize = 0; //FR
96 delete [] fRecPointArray;
100 AliHLTCaloClusterizer::SetRecPointDataPtr(AliHLTCaloRecPointDataStruct* recPointDataPtr)
102 // See header file for documentation
103 fRecPointDataPtr = recPointDataPtr;
107 AliHLTCaloClusterizer::ClusterizeEvent(Int_t nDigits)
109 //see header file for documentation
110 Int_t nRecPoints = 0;
114 fRecPointDataPtr = fFirstRecPointPtr;
119 //Clusterization starts
120 for (Int_t i = 0; i < nDigits; i++)
122 fDigitsInCluster = 0;
124 HLTDebug("Digit with energy: %f", fDigitsPointerArray[i]->fEnergy);
126 if (fDigitsPointerArray[i]->fEnergy < fEmcClusteringThreshold && fSortedByEnergy)
128 // Since we have sorted by energy the next digit will have even lower energy, so we return
132 if(fDigitsPointerArray[i]->fAssociatedCluster != -1)
134 // The digit is added to a previous cluster, continue
141 // First digit is placed at the fDigits member variable in the recpoint
142 fDigitIndexPtr = &(fRecPointDataPtr->fDigits);
144 fRecPointDataPtr->fAmp = 0;
145 fRecPointDataPtr->fModule = fDigitsPointerArray[i]->fModule;
147 // Assigning the digit to this rec point
148 fRecPointDataPtr->fDigits = i;
149 fUsedSize += sizeof(AliHLTCaloRecPointDataStruct);
151 // Incrementing the pointer to be ready for new entry
154 fRecPointDataPtr->fAmp += fDigitsPointerArray[i]->fEnergy;
157 //fDigitsPointerArray[i]->fEnergy = 0;
158 fDigitsPointerArray[i]->fAssociatedCluster = fNRecPoints;
164 // Scanning for the neighbours
165 if (ScanForNeighbourDigits(i, fRecPointDataPtr) != 0)
170 //fUsedSize += sizeof(AliHLTCaloRecPointDataStruct) + (fDigitsInCluster-1)*sizeof(AliHLTCaloDigitDataStruct);
172 fRecPointDataPtr->fMultiplicity = fDigitsInCluster;
173 fRecPointArray[fNRecPoints] = fRecPointDataPtr;
175 fRecPointDataPtr = reinterpret_cast<AliHLTCaloRecPointDataStruct*>(fDigitIndexPtr);
179 }//end of clusterization
185 AliHLTCaloClusterizer::ScanForNeighbourDigits(Int_t index, AliHLTCaloRecPointDataStruct* recPoint)
187 //see header file for documentation
189 // The following cuts can be used if we sort by posisiton. Not tested, but it should be fine...
190 Int_t max = TMath::Min(fNDigits, (Int_t)fMaxDigitIndexDiff+index);
191 Int_t min = TMath::Max(0, (Int_t)(index - (Int_t)fMaxDigitIndexDiff));
193 // All digits for now
197 for (Int_t j = min; j < max; j++)
199 if (fDigitsPointerArray[j]->fAssociatedCluster == -1 && fDigitsPointerArray[j]->fEnergy > fEmcMinEnergyThreshold)
203 if (AreNeighbours(fDigitsPointerArray[index],
204 fDigitsPointerArray[j]))
206 // Check that the buffer is large enough for adding a digit (can be heavily improved wrt performance)
209 // Assigning index to digit
211 fUsedSize += sizeof(Int_t);
213 // Incrementing digit pointer to be ready for new entry
216 // Adding the digit energy to the rec point
217 fRecPointDataPtr->fAmp += fDigitsPointerArray[j]->fEnergy;
219 // Setting energy to 0
220 //fDigitsPointerArray[j]->fEnergy = 0;
222 // Setting the associated cluster
223 fDigitsPointerArray[j]->fAssociatedCluster = fNRecPoints;
225 HLTDebug("Added digit with index: %d, energy: %f, to associated cluster: %d", fDigitsPointerArray[j]->fID, fDigitsPointerArray[j]->fEnergy, fDigitsPointerArray[j]->fAssociatedCluster);
229 // Scan for neighbours of this digit
230 ScanForNeighbourDigits(j, recPoint);
239 AliHLTCaloClusterizer::AreNeighbours(AliHLTCaloDigitDataStruct* digit1,
240 AliHLTCaloDigitDataStruct* digit2)
242 //see header file for documentation
243 if ( (digit1->fModule == digit2->fModule) || AreEdgeCells(digit1, digit2))
245 Int_t rowdiff = TMath::Abs( digit1->fZ - digit2->fZ );
246 Int_t coldiff = TMath::Abs( digit1->fX - digit2->fX );
248 // Common edge defines neighbour
249 //if (( coldiff <= 1 && rowdiff == 0 ) || ( coldiff == 0 && rowdiff <= 1 ))
250 // Common edge and corner defines neighbour
251 if (( coldiff <= 1 && rowdiff <= 1 ))
253 // Check also for time
254 if (TMath::Abs(digit1->fTime - digit2->fTime ) < fEmcTimeGate)
265 Int_t AliHLTCaloClusterizer::CheckArray()
267 // See header file for class documentation
268 if (fArraySize == fNRecPoints)
271 AliHLTCaloRecPointDataStruct **tmp = new AliHLTCaloRecPointDataStruct*[fArraySize];
272 memcpy(tmp, fRecPointArray, fArraySize/2 * sizeof(AliHLTCaloRecPointDataStruct*));
273 delete [] fRecPointArray;
274 fRecPointArray = tmp;
279 Int_t AliHLTCaloClusterizer::CheckBuffer()
281 // See header file for class documentation
282 if ((fAvailableSize - fUsedSize) < (Int_t)sizeof(AliHLTCaloRecPointDataStruct))
284 Int_t recPointOffset = reinterpret_cast<UChar_t*>(fRecPointDataPtr) - reinterpret_cast<UChar_t*>(fFirstRecPointPtr);
285 Int_t digitIndexOffset = reinterpret_cast<UChar_t*>(fDigitIndexPtr) - reinterpret_cast<UChar_t*>(fRecPointDataPtr);
286 UChar_t *tmp = new UChar_t[fAvailableSize*2];
290 HLTError("Pointer error");
294 memcpy(tmp, fFirstRecPointPtr, fUsedSize);
296 for (Int_t n = 0; n < fNRecPoints; n++)
298 fRecPointArray[n] = reinterpret_cast<AliHLTCaloRecPointDataStruct*>(reinterpret_cast<UChar_t*>(fRecPointArray[n]) - reinterpret_cast<UChar_t*>(fFirstRecPointPtr) + reinterpret_cast<UChar_t*>(tmp));
300 delete [] fBuffer; //FR
303 fFirstRecPointPtr = reinterpret_cast<AliHLTCaloRecPointDataStruct*>(tmp);
304 fRecPointDataPtr = reinterpret_cast<AliHLTCaloRecPointDataStruct*>(tmp + recPointOffset);
305 fDigitIndexPtr = reinterpret_cast<Int_t*>(reinterpret_cast<UChar_t*>(fRecPointDataPtr) + digitIndexOffset);
311 void AliHLTCaloClusterizer::SetSortDigitsByPosition()
313 // Sort the digit pointers by position
314 fCompareFunction = &CompareDigitsByPosition;
316 fSortedByPosition = true;
319 void AliHLTCaloClusterizer::SetSortDigitsByEnergy()
321 // See header file for class documentation
322 fCompareFunction = &CompareDigitsByEnergy;
324 fSortedByEnergy = true;
327 void AliHLTCaloClusterizer::SortDigits()
329 // See header file for class documentation
330 if (fSortDigits) qsort(fDigitsPointerArray, fNDigits, sizeof(AliHLTCaloDigitDataStruct*), fCompareFunction);
334 AliHLTCaloClusterizer::CompareDigitsByPosition(const void *dig0, const void *dig1)
336 // See header file for documentation
337 return (*((AliHLTCaloDigitDataStruct**)(dig0)))->fID - (*((AliHLTCaloDigitDataStruct**)(dig1)))->fID;
341 AliHLTCaloClusterizer::CompareDigitsByEnergy(const void *dig0, const void *dig1)
343 // See header file for documentation
344 if ( ((*((AliHLTCaloDigitDataStruct**)(dig1)))->fEnergy - (*((AliHLTCaloDigitDataStruct**)(dig0)))->fEnergy) < 0) return -1;
348 void AliHLTCaloClusterizer::SetDetector(TString det)
350 if(det.CompareTo("EMCAL"))
360 Bool_t AliHLTCaloClusterizer::AreEdgeCells(AliHLTCaloDigitDataStruct *digit0, AliHLTCaloDigitDataStruct *digit1)
364 Int_t modDiff = digit0->fModule - digit1->fModule;
365 if(TMath::Abs(modDiff) > 1) return kFALSE;
366 if(digit0->fModule > digit1->fModule && digit1->fModule%2 == 0)
368 if(digit0->fZ == 0 && digit1->fZ == (fCaloConstants->GetNZROWSMOD()-1))
371 if(digit1->fModule > digit0->fModule && digit0->fModule%2 == 0)
373 if(digit1->fZ == 0 && digit0->fZ == (fCaloConstants->GetNZROWSMOD()-1))