]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/PHOS/AliHLTPHOSClusterizerComponent.cxx
a41eef0aa76d5a811860ba6767c4575184cee763
[u/mrichter/AliRoot.git] / HLT / PHOS / AliHLTPHOSClusterizerComponent.cxx
1 // $Id$
2
3 /**************************************************************************
4  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
5  *                                                                        *
6  * Authors: Oystein Djuvsland <oysteind@ift.uib.no>                       *
7  *                                                                        *
8  * Permission to use, copy, modify and distribute this software and its   *
9  * documentation strictly for non-commercial purposes is hereby granted   *
10  * without fee, provided that the above copyright notice appears in all   *
11  * copies and that both the copyright notice and this permission notice   *
12  * appear in the supporting documentation. The authors make no claims     *
13  * about the suitability of this software for any purpose. It is          *
14  * provided "as is" without express or implied warranty.                  *
15  **************************************************************************/
16
17 #include <iostream>
18
19 #include "AliHLTPHOSClusterizerComponent.h"
20 #include "AliHLTPHOSClusterizer.h"
21 #include "AliHLTPHOSRecPointDataStruct.h"
22 #include "AliHLTPHOSRecPointHeaderStruct.h"
23 #include "AliHLTPHOSDigitDataStruct.h"
24 #include "AliHLTPHOSDigitContainerDataStruct.h"
25
26
27
28 /** @file   AliHLTPHOSClusterizerComponent.cxx
29     @author Oystein Djuvsland
30     @date   
31     @brief  A clusterizer component for PHOS HLT
32 */
33
34 // see header file for class documentation
35 // or
36 // refer to README to build package
37 // or
38 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
39
40 AliHLTPHOSClusterizerComponent gAliHLTPHOSClusterizerComponent;
41
42 AliHLTPHOSClusterizerComponent::AliHLTPHOSClusterizerComponent(): 
43   AliHLTPHOSProcessor(), 
44   fAllDigitsPtr(0),
45   fClusterizerPtr(0),
46   fDigitCount(0),
47   fNoCrazyness(0)
48 {
49   //See headerfile for documentation
50 }
51
52 AliHLTPHOSClusterizerComponent::~AliHLTPHOSClusterizerComponent()
53 {
54   //See headerfile for documentation
55
56   if(fClusterizerPtr)
57     {
58       delete fClusterizerPtr;
59       fClusterizerPtr = 0;
60     }
61   if(fAllDigitsPtr)
62     {
63       delete fAllDigitsPtr;
64       fAllDigitsPtr = 0;
65     }
66 }
67
68
69 int
70 AliHLTPHOSClusterizerComponent::Deinit()
71 {
72   //See headerfile for documentation
73
74   if (fClusterizerPtr)
75     {
76       delete fClusterizerPtr;
77       fClusterizerPtr = 0;
78     }
79
80   return 0;
81 }
82
83 const Char_t*
84 AliHLTPHOSClusterizerComponent::GetComponentID()
85 {
86   //See headerfile for documentation
87   return "PhosClusterizer";
88 }
89
90 void
91 AliHLTPHOSClusterizerComponent::GetInputDataTypes( vector<AliHLTComponentDataType>& list)
92 {
93   //See headerfile for documentation
94   list.clear();
95   list.push_back(AliHLTPHOSDefinitions::fgkDigitDataType);
96 }
97
98 AliHLTComponentDataType
99 AliHLTPHOSClusterizerComponent::GetOutputDataType()
100 {
101   //See headerfile for documentation
102   return AliHLTPHOSDefinitions::fgkRecPointDataType;
103 }
104
105 void
106 AliHLTPHOSClusterizerComponent::GetOutputDataSize(unsigned long& constBase, double& inputMultiplier )
107
108 {
109   //See headerfile for documentation
110   constBase = sizeof(AliHLTPHOSRecPointHeaderStruct) + sizeof(AliHLTPHOSRecPointDataStruct) + (sizeof(AliHLTPHOSDigitDataStruct) << 7); //Reasonable estimate... ;
111   inputMultiplier = 1.5;
112 }
113
114 int
115 AliHLTPHOSClusterizerComponent::DoEvent(const AliHLTComponentEventData& evtData, const AliHLTComponentBlockData* blocks,
116                                         AliHLTComponentTriggerData& /*trigData*/, AliHLTUInt8_t* outputPtr, AliHLTUInt32_t& size,
117                                         std::vector<AliHLTComponentBlockData>& outputBlocks)
118 {
119   //See headerfile for documentation
120
121   if(blocks == 0) return 0;
122   
123   UInt_t offset           = 0;
124   UInt_t mysize           = 0;
125   Int_t nRecPoints        = 0;
126   Int_t nDigits           = 0;
127
128   UInt_t availableSize = size;
129   AliHLTUInt8_t* outBPtr;
130   outBPtr = outputPtr;
131   const AliHLTComponentBlockData* iter = 0;
132   unsigned long ndx;
133   
134   UInt_t specification = 0;
135   
136   AliHLTPHOSDigitHeaderStruct *digitHeaderPtr = 0;
137   AliHLTPHOSDigitHeaderStruct *outputDigitHeaderPtr = reinterpret_cast<AliHLTPHOSDigitHeaderStruct*>(outBPtr);
138
139   //  HLTError("Header pointer before screwing around: 0x%x", outputDigitHeaderPtr);
140
141   AliHLTPHOSDigitDataStruct *firstDigitPtr = 0;
142   AliHLTPHOSDigitDataStruct *lastDigitPtr = 0;
143   
144   // Adding together all the digits, should be put in standalone method  
145   for ( ndx = 0; ndx < evtData.fBlockCnt; ndx++ )
146     {
147       iter = blocks+ndx;
148       if (iter->fDataType == AliHLTPHOSDefinitions::fgkDigitDataType)
149         {
150           // Get the digit header
151           digitHeaderPtr = reinterpret_cast<AliHLTPHOSDigitHeaderStruct*>(iter->fPtr);
152
153           // Update the number of digits
154           nDigits += digitHeaderPtr->fNDigits;
155
156           // Get the specification
157           specification = specification|iter->fSpecification;
158           
159           // Check if we have the first buffer in the event
160           if(!firstDigitPtr)
161             {
162               if(availableSize < digitHeaderPtr->fNDigits*sizeof(AliHLTPHOSDigitDataStruct) + sizeof(AliHLTPHOSDigitHeaderStruct))
163                 {
164                   // HLTError("Buffer overflow: Trying to write data of size: %d bytes. Output buffer available: %d bytes.", totSize, size);
165                   HLTError("Buffer overflow: Trying to write data of size: %d bytes. Output buffer available: %d bytes.", mysize, size);
166                   return -1;
167                 }
168               // If so, lets copy the header and the corresponding digits to the output
169               memcpy(outBPtr, iter->fPtr, digitHeaderPtr->fNDigits*sizeof(AliHLTPHOSDigitDataStruct) + sizeof(AliHLTPHOSDigitHeaderStruct));
170
171               // Set the pointer to the first digit in the list
172               //              firstDigitPtr = reinterpret_cast<AliHLTPHOSDigitDataStruct*>(outBPtr + sizeof(AliHLTPHOSDigitHeaderStruct) + digitHeaderPtr->fFirstDigitOffset);
173               firstDigitPtr = reinterpret_cast<AliHLTPHOSDigitDataStruct*>(outBPtr + digitHeaderPtr->fFirstDigitOffset);
174
175               //              lastDigitPtr = reinterpret_cast<AliHLTPHOSDigitDataStruct*>(outBPtr + sizeof(AliHLTPHOSDigitHeaderStruct) + digitHeaderPtr->fLastDigitOffset);
176               lastDigitPtr = reinterpret_cast<AliHLTPHOSDigitDataStruct*>(outBPtr + digitHeaderPtr->fLastDigitOffset);
177               
178               // Update the amount of the output buffer we have used
179               mysize += digitHeaderPtr->fNDigits*sizeof(AliHLTPHOSDigitDataStruct) + sizeof(AliHLTPHOSDigitHeaderStruct);
180             }
181           else
182             {
183               // Check if we have space for 
184               if(availableSize < digitHeaderPtr->fNDigits*sizeof(AliHLTPHOSDigitDataStruct))
185                 {
186                   HLTError("Buffer overflow: Trying to write data of size: %d bytes. Output buffer available: %d bytes.", mysize, size);
187                   return -1;
188                 }
189
190               // If we already have copied the first buffer to the output copy only the digits
191               memcpy(outBPtr, reinterpret_cast<const void*>(reinterpret_cast<UChar_t*>(iter->fPtr)+sizeof(AliHLTPHOSDigitHeaderStruct)), digitHeaderPtr->fNDigits*sizeof(AliHLTPHOSDigitDataStruct));
192
193               // Check if the first digit in this buffer has a ID less than the first digit in the previous
194               //              if(firstDigitPtr->fID > reinterpret_cast<AliHLTPHOSDigitDataStruct*>(reinterpret_cast<UChar_t*>(iter->fPtr) + sizeof(AliHLTPHOSDigitDataStruct) + digitHeaderPtr->fFirstDigitOffset)->fID)
195               AliHLTPHOSDigitDataStruct *thisFirst = 
196                 reinterpret_cast<AliHLTPHOSDigitDataStruct*>(reinterpret_cast<UChar_t*>(iter->fPtr) + digitHeaderPtr->fFirstDigitOffset);
197               if(firstDigitPtr->fID > thisFirst->fID)
198                 {
199                   // If that is the case we have to take care of the ordering
200                                   
201                   HLTError("Re-ordering digit blocks...");
202                   // The last digit in the current buffer has to link to the first digit in the previous buffer
203                   //              AliHLTPHOSDigitDataStruct *thisLast = reinterpret_cast<AliHLTPHOSDigitDataStruct*>(outBPtr + sizeof(AliHLTPHOSDigitHeaderStruct) + digitHeaderPtr->fLastDigitOffset);
204                   AliHLTPHOSDigitDataStruct *thisLast = reinterpret_cast<AliHLTPHOSDigitDataStruct*>(outBPtr - sizeof(AliHLTPHOSDigitHeaderStruct) + digitHeaderPtr->fLastDigitOffset);
205                   thisLast->fMemOffsetNext = reinterpret_cast<Long_t>(firstDigitPtr) - reinterpret_cast<Long_t>(thisLast);
206
207                   // Setting the pointer to the new first digit
208                   firstDigitPtr = reinterpret_cast<AliHLTPHOSDigitDataStruct*>(outBPtr + digitHeaderPtr->fFirstDigitOffset - sizeof(AliHLTPHOSDigitHeaderStruct));
209                 }
210               else
211                 {
212                   // Previous last digit need to link to the current first digit
213                   lastDigitPtr->fMemOffsetNext = reinterpret_cast<Long_t>(lastDigitPtr) - (reinterpret_cast<Long_t>(outBPtr) + digitHeaderPtr->fFirstDigitOffset - sizeof(AliHLTPHOSDigitHeaderStruct));                  
214                   
215                   // We need to change the last digit pointer
216                   lastDigitPtr = reinterpret_cast<AliHLTPHOSDigitDataStruct*>(reinterpret_cast<Long_t>(outBPtr) + digitHeaderPtr->fLastDigitOffset - sizeof(AliHLTPHOSDigitHeaderStruct));
217                 }
218               // Update the amount of the output buffer we have used
219               mysize += digitHeaderPtr->fNDigits*sizeof(AliHLTPHOSDigitDataStruct);
220             }
221
222           outBPtr += mysize;
223         }
224     }
225   
226   // The digit header in the output needs to know about the position of the new first digit
227   //  outputDigitHeaderPtr->fFirstDigitOffset = reinterpret_cast<Long_t>(firstDigitPtr) - reinterpret_cast<Long_t>(outputDigitHeaderPtr) + sizeof(AliHLTPHOSDigitHeaderStruct);
228   outputDigitHeaderPtr->fFirstDigitOffset = reinterpret_cast<Long_t>(firstDigitPtr) - reinterpret_cast<Long_t>(outputDigitHeaderPtr);
229   
230   // The digit header in the output needs to know about the position of the new last digit
231   //  outputDigitHeaderPtr->fLastDigitOffset = reinterpret_cast<Long_t>(lastDigitPtr) - reinterpret_cast<Long_t>(outputDigitHeaderPtr) + sizeof(AliHLTPHOSDigitHeaderStruct);
232   outputDigitHeaderPtr->fLastDigitOffset = reinterpret_cast<Long_t>(lastDigitPtr) - reinterpret_cast<Long_t>(outputDigitHeaderPtr);
233   if(firstDigitPtr)
234     {
235       //      HLTError("Header pointer after screwing around: 0x%x", outputDigitHeaderPtr);
236       //      HLTError("First/last offset: %d / %d, first digit ID: %d, energy: %f", outputDigitHeaderPtr->fFirstDigitOffset, outputDigitHeaderPtr->fLastDigitOffset, firstDigitPtr->fID, firstDigitPtr->fEnergy);
237     }
238
239   HLTDebug("Number of digits: %d", nDigits);
240
241   if(nDigits > 0)
242     {
243
244       AliHLTPHOSRecPointHeaderStruct* recPointHeaderPtr = reinterpret_cast<AliHLTPHOSRecPointHeaderStruct*>(outBPtr);
245
246       fClusterizerPtr->SetRecPointDataPtr(reinterpret_cast<AliHLTPHOSRecPointDataStruct*>(outBPtr+sizeof(AliHLTPHOSRecPointHeaderStruct)));
247
248       nRecPoints = fClusterizerPtr->ClusterizeEvent(outputDigitHeaderPtr, availableSize, mysize);
249       recPointHeaderPtr->fNRecPoints = nRecPoints;
250
251       mysize += sizeof(AliHLTPHOSRecPointHeaderStruct);
252   
253       HLTDebug("Number of clusters: %d", nRecPoints);
254     }
255   AliHLTComponentBlockData clusterBd;
256   FillBlockData( clusterBd );
257   clusterBd.fOffset = offset;
258   clusterBd.fSize = mysize;
259   clusterBd.fDataType = AliHLTPHOSDefinitions::fgkRecPointDataType;
260   clusterBd.fSpecification = specification;
261   outputBlocks.push_back( clusterBd );
262
263   if(false)
264     {
265       AliHLTComponentBlockData digitBd;
266       FillBlockData(digitBd);
267     }
268        
269   size = mysize;
270   
271   return 0;
272 }
273
274 int 
275 AliHLTPHOSClusterizerComponent::Reconfigure(const char* cdbEntry, const char* /*chainId*/)
276 {  
277   // see header file for class documentation
278
279   const char* path="HLT/ConfigPHOS/ClusterizerComponent";
280
281   if (cdbEntry) path = cdbEntry;
282
283   return ConfigureFromCDBTObjString(cdbEntry);
284 }
285
286 int 
287 AliHLTPHOSClusterizerComponent::ScanConfigurationArgument(int argc, const char **argv)
288 {
289   //See header file for documentation
290
291   if(argc <= 0) return 0;
292
293   int i=0;
294
295   TString argument=argv[i];
296
297   if (argument.CompareTo("-digitthreshold") == 0)
298     {
299       if (++i >= argc) return -EPROTO;
300       argument = argv[i];
301       fClusterizerPtr->SetEmcMinEnergyThreshold(argument.Atof());
302       return 1;
303     }
304
305   if (argument.CompareTo("-recpointthreshold") == 0)
306     {
307       if (++i >= argc) return -EPROTO;
308       argument = argv[i];
309       fClusterizerPtr->SetEmcClusteringThreshold(argument.Atof());
310       return 1;
311     }
312   return 0;
313 }
314
315 int
316 AliHLTPHOSClusterizerComponent::DoInit(int argc, const char** argv )
317 {
318   //See headerfile for documentation
319
320   fAllDigitsPtr = new AliHLTPHOSDigitContainerDataStruct();
321   fClusterizerPtr = new AliHLTPHOSClusterizer();
322   fClusterizerPtr->SetDigitContainer(fAllDigitsPtr);
323   fNoCrazyness = false;
324   //
325
326   //  const char *path = "HLT/ConfigPHOS/ClusterizerComponent";
327
328   //  ConfigureFromCDBTObjString(path);
329
330   for (int i = 0; i < argc; i++)
331     {
332       ScanConfigurationArgument(i, argv);
333     }
334
335   return 0;
336 }
337
338 AliHLTComponent*
339 AliHLTPHOSClusterizerComponent::Spawn()
340 {
341   //See headerfile for documentation
342
343   return new AliHLTPHOSClusterizerComponent();
344 }