]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/PHOS/AliHLTPHOSDigitMaker.cxx
- added handlers for reconstruction parameters
[u/mrichter/AliRoot.git] / HLT / PHOS / AliHLTPHOSDigitMaker.cxx
1 // $Id$
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  * @file   AliHLTPHOSDigitMaker.cxx
19  * @author Oystein Djuvsland
20  * @date 
21  * @brief  Digit maker for PHOS HLT  
22  */
23   
24
25     
26
27 // see header file for class documentation
28 // or
29 // refer to README to build package
30 // or
31 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
32
33 #include "AliHLTPHOSDigitMaker.h"
34 #include "AliHLTLogging.h"
35
36 #include "AliHLTPHOSConstant.h"
37 #include "AliHLTPHOSMapper.h"
38
39 #include "AliHLTPHOSChannelDataStruct.h"
40 #include "AliHLTPHOSChannelDataHeaderStruct.h"
41 #include "AliHLTPHOSDigitDataStruct.h"
42 #include "AliHLTPHOSSharedMemoryInterfacev2.h" // added by PTH
43 #include "TH2F.h"
44 #include <cstdlib>
45
46 ClassImp(AliHLTPHOSDigitMaker);
47
48 using namespace PhosHLTConst;
49
50 AliHLTPHOSDigitMaker::AliHLTPHOSDigitMaker() :
51   AliHLTLogging(),
52   fShmPtr(0),
53   fDigitStructPtr(0),
54   fDigitHeaderPtr(0),
55   fDigitCount(0),
56   fOrdered(true),
57   fMapperPtr(0),
58   fDigitPtrArray(0),
59   fAvailableSize(0)
60 {
61   // See header file for documentation
62
63   fShmPtr = new AliHLTPHOSSharedMemoryInterfacev2();
64
65   for(int x = 0; x < NXCOLUMNSMOD; x++)
66     {
67       for(int z = 0; z < NZROWSMOD; z++)
68         {
69           fHighGainFactors[x][z] = 0.005;
70           fLowGainFactors[x][z] = 0.08;
71           fBadChannelMask[x][z][HIGHGAIN] = 1;
72           fBadChannelMask[x][z][LOWGAIN] = 1; 
73         }
74     }     
75   fMapperPtr = new AliHLTPHOSMapper();
76
77   fDigitPtrArray = new AliHLTPHOSDigitDataStruct*[NZROWSRCU*NXCOLUMNSMOD];
78 }
79    
80 AliHLTPHOSDigitMaker::~AliHLTPHOSDigitMaker() 
81 {
82   //See header file for documentation
83 }
84
85 Int_t
86 AliHLTPHOSDigitMaker::MakeDigits(AliHLTPHOSChannelDataHeaderStruct* channelDataHeader, AliHLTUInt32_t availableSize)
87 {
88   //See header file for documentation
89   
90   fAvailableSize = availableSize;
91   Int_t j = 0;
92   UInt_t totSize = sizeof(AliHLTPHOSDigitDataStruct);
93   
94   
95
96 //   Int_t xMod = -1;
97 //   Int_t zMod = -1;
98   
99   UShort_t coord1[4];
100   UShort_t coord2[4];
101   Float_t locCoord[3];
102   
103   
104   AliHLTPHOSChannelDataStruct* currentchannel = 0;
105   AliHLTPHOSChannelDataStruct* currentchannelLG = 0;  
106   AliHLTPHOSChannelDataStruct* tmpchannel = 0;
107
108   fShmPtr->SetMemory(channelDataHeader);
109   currentchannel = fShmPtr->NextChannel();
110   
111   while(currentchannel != 0)
112     {
113       if(availableSize < totSize) return -1;
114
115       AliHLTPHOSMapper::GetChannelCoord(currentchannel->fChannelID, coord1);
116       
117       if(fOrdered) // High gain comes before low gain
118         {
119           tmpchannel = currentchannel;
120           
121           if(coord1[2] == HIGHGAIN) // We got a completely new crystal
122             {
123               if(currentchannel->fEnergy < MAXBINVALUE) // Make sure we don't have signal overflow
124                 {
125                   //AliHLTPHOSMapper::GetLocalCoord(currentchannel->fChannelID, locCoord);
126                   if(!AddDigit(currentchannel, coord1, locCoord)) return -1;
127                   j++;        
128                   totSize += sizeof(AliHLTPHOSDigitDataStruct);
129
130                   currentchannel = fShmPtr->NextChannel(); // Get the next channel
131
132                   if(currentchannel != 0) // There was a next channel!
133                     {
134                       AliHLTPHOSMapper::GetChannelCoord(currentchannel->fChannelID, coord2);
135                       if(coord1[0] == coord2[0] && coord1[1] == coord2[1]) // Did we get the low gain channel for this crystal?
136                         {
137                           currentchannel = fShmPtr->NextChannel(); // In that case, jump to next channel
138                         }
139                     }
140                 }
141
142               else // Ooops, overflow, we try the next channel... 
143                 {
144                   currentchannel = fShmPtr->NextChannel();
145                   if(currentchannel != 0) // There was a next channel
146                     {
147                       AliHLTPHOSMapper::GetChannelCoord(currentchannel->fChannelID, coord2);
148                       if(coord2[0] == coord1[0] && coord2[1] == coord1[1]) // It is a low gain channel with the same coordinates, we may use it
149                         {
150                         //  AliHLTPHOSMapper::GetLocalCoord(currentchannel->fChannelID, locCoord);
151                           if(!AddDigit(currentchannel, coord2, locCoord)) return -1;
152                           j++;
153                           totSize += sizeof(AliHLTPHOSDigitDataStruct);
154                           currentchannel = fShmPtr->NextChannel();                    
155                         }
156                       
157                       else // No low gain channel with information about the overflow channel so we just use the overflowed one...
158                         {
159                           //AliHLTPHOSMapper::GetLocalCoord(currentchannel->fChannelID, locCoord);
160                           if(!AddDigit(tmpchannel, coord1, locCoord)) return -1;
161                           j++;        
162                           totSize += sizeof(AliHLTPHOSDigitDataStruct);
163                           // no need to get the next channel here, we already did...
164                         }
165                       
166                     }
167                 }
168             }
169           else // Well, there seem to be missing a high gain channel for this crystal, let's use the low gain one
170             {    
171               //AliHLTPHOSMapper::GetLocalCoord(currentchannel->fChannelID, locCoord);  
172               if(!AddDigit(tmpchannel, coord1, locCoord)) return -1;
173               j++;            
174               totSize += sizeof(AliHLTPHOSDigitDataStruct);
175               currentchannel = fShmPtr->NextChannel(); 
176             }
177
178         }
179       else  //Reversed ordered (low gain before high gain)
180         {
181           if(coord1[2] == LOWGAIN) // We got a new channel!
182             {
183               currentchannelLG = currentchannel; // Ok, let's back up the low gain channel and look for the fancy high gain one
184               currentchannel = fShmPtr->NextChannel();
185
186               if(currentchannel != 0) //There was another channel in the event
187                 {
188                   AliHLTPHOSMapper::GetChannelCoord(currentchannel->fChannelID, coord2);
189                   
190                   if(coord1[0] == coord2[0] && coord1[1] == coord2[1]) // Aha! Found the high gain channel
191                     {
192                       if(currentchannel->fEnergy < MAXBINVALUE)  // To overflow or not to overflow?
193                         {
194                 //        AliHLTPHOSMapper::GetLocalCoord(currentchannel->fChannelID, locCoord);
195                           if(!AddDigit(currentchannel, coord2, locCoord)) return -1;
196                           j++;
197                           totSize += sizeof(AliHLTPHOSDigitDataStruct);
198                           currentchannel = fShmPtr->NextChannel();
199                         }
200                       else // Oh well, better use the low gain channel then
201                         {
202                 //        AliHLTPHOSMapper::GetLocalCoord(currentchannel->fChannelID, locCoord);
203                           if(!AddDigit(currentchannelLG, coord1, locCoord)) return -1;
204                           j++;
205                           totSize += sizeof(AliHLTPHOSDigitDataStruct);
206                           currentchannel = fShmPtr->NextChannel();
207                         }
208                     }
209                   else // No available high gain channel for this crystal, adding the low gain one
210                     {
211                   //    AliHLTPHOSMapper::GetLocalCoord(currentchannel->fChannelID, locCoord);
212                       if(!AddDigit(currentchannelLG, coord1, locCoord)) return -1;
213                       j++;
214                       totSize += sizeof(AliHLTPHOSDigitDataStruct);
215                     }
216                 }
217               else //Fine, no more channels, better add this one...
218                 {
219                   //AliHLTPHOSMapper::GetLocalCoord(currentchannelLG->fChannelID, locCoord);
220                   if(!AddDigit(currentchannelLG, coord1, locCoord)) return -1;
221                   j++;
222                   totSize += sizeof(AliHLTPHOSDigitDataStruct);
223                 }
224             } 
225           else // Cool, no annoying low gain channel for this channel
226             {
227               //AliHLTPHOSMapper::GetLocalCoord(currentchannel->fChannelID, locCoord);
228               if(!AddDigit(currentchannel, coord1, locCoord)) return -1;
229               j++;
230               currentchannel = fShmPtr->NextChannel();
231             }
232         }
233     }
234   if(fDigitCount > 1) SortDigits();
235   return fDigitCount; 
236 }
237
238 void 
239 AliHLTPHOSDigitMaker::SetGlobalHighGainFactor(Float_t factor)
240 {
241   //See header file for documentation
242   for(int x = 0; x < NXCOLUMNSMOD; x++)
243     {
244       for(int z = 0; z < NZROWSMOD; z++)
245         {
246           fHighGainFactors[x][z] = factor;
247         }
248     }
249 }
250
251 void
252 AliHLTPHOSDigitMaker::SetGlobalLowGainFactor(Float_t factor)
253 {
254   //See header file for documentation
255   for(int x = 0; x < NXCOLUMNSMOD; x++)
256     {
257       for(int z = 0; z < NZROWSMOD; z++)
258         {
259           fLowGainFactors[x][z] = factor;
260         }
261     }
262 }
263
264 void
265 AliHLTPHOSDigitMaker::SetBadChannelMask(TH2F* badChannelHGHist, TH2F* badChannelLGHist, Float_t qCut)
266 {
267   // See header file for documentation
268  for(int x = 0; x < NXCOLUMNSMOD; x++)
269     {
270       for(int z = 0; z < NZROWSMOD; z++)
271         {
272           if(badChannelHGHist->GetBinContent(x, z) < qCut && badChannelHGHist->GetBinContent(x, z) > 0)
273             {
274               fBadChannelMask[x][z][HIGHGAIN] = 1;
275             }
276           else
277             {
278               fBadChannelMask[x][z][HIGHGAIN] = 0;
279             }
280           if(badChannelLGHist->GetBinContent(x, z) < qCut && badChannelLGHist->GetBinContent(x, z) > 0)
281             {
282               fBadChannelMask[x][z][LOWGAIN] = 0;
283             }
284           else
285             {
286               fBadChannelMask[x][z][LOWGAIN] = 0;
287             }
288         }
289     }
290 }
291
292 void
293 AliHLTPHOSDigitMaker::SortDigits()
294 {
295
296   // See header file for documentation
297
298   //  Int_t (*funcPtr)(const void*, const void*)  = &AliHLTPHOSDigitMaker::CompareDigits;
299   //  HLTError("fDigitPtrArray[0]: %lu, fDigitHeaderPtr: %lu, sizeof(AliHLTPHOSDigitHeaderStruct): %d", fDigitPtrArray[0], fDigitHeaderPtr, sizeof(AliHLTPHOSDigitHeaderStruct));
300   //  HLTError("First digit offset: %d, first digit ptr: %ld, sizeof(AliHLTPHOSDigitDataStruct) = %d", fDigitHeaderPtr->fFirstDigitOffset, fDigitPtrArray[0], sizeof(AliHLTPHOSDigitDataStruct));
301   qsort(fDigitPtrArray, fDigitCount, sizeof(AliHLTPHOSDigitDataStruct*), CompareDigits);
302
303   //  HLTError("fDigitPtrArray[0]: %lu, fDigitHeaderPtr: %lu, sizeof(AliHLTPHOSDigitHeaderStruct): %d", fDigitPtrArray[0], fDigitHeaderPtr, sizeof(AliHLTPHOSDigitHeaderStruct));
304   //  fDigitHeaderPtr->fFirstDigitOffset = reinterpret_cast<Long_t>(fDigitPtrArray[0]) - reinterpret_cast<Long_t>(fDigitHeaderPtr) + sizeof(AliHLTPHOSDigitHeaderStruct);
305   fDigitHeaderPtr->fFirstDigitOffset = reinterpret_cast<Long_t>(fDigitPtrArray[0]) - reinterpret_cast<Long_t>(fDigitHeaderPtr);
306   //  HLTError("First digit offset: %d, first digit ptr: %ld, sizeof(AliHLTPHOSDigitDataStruct) = %d", fDigitHeaderPtr->fFirstDigitOffset, fDigitPtrArray[0], sizeof(AliHLTPHOSDigitDataStruct));
307   for(Int_t i = 0; i < fDigitCount-1; i++)
308     {
309       fDigitPtrArray[i]->fMemOffsetNext = reinterpret_cast<Long_t>(fDigitPtrArray[i+1]) - reinterpret_cast<Long_t>(fDigitPtrArray[i]);
310       //      HLTError("Adding digit with energy: %f, ID: %d, offset: %d and pointer %lu", fDigitPtrArray[i]->fEnergy, fDigitPtrArray[i]->fID, fDigitPtrArray[i]->fMemOffsetNext, fDigitPtrArray[i]);
311     }
312   //  fDigitHeaderPtr->fLastDigitOffset = reinterpret_cast<Long_t>(fDigitPtrArray[fDigitCount-1]) - (reinterpret_cast<Long_t>(fDigitHeaderPtr) + sizeof(AliHLTPHOSDigitHeaderStruct));
313   fDigitHeaderPtr->fLastDigitOffset = reinterpret_cast<Long_t>(fDigitPtrArray[fDigitCount-1]) - reinterpret_cast<Long_t>(fDigitHeaderPtr);
314   //  HLTError("Last digit offset: %d, last digit ptr: %ld", fDigitHeaderPtr->fLastDigitOffset, fDigitPtrArray[fDigitCount-1]);
315   fDigitPtrArray[fDigitCount-1]->fMemOffsetNext = 0;
316   //  HLTError("Number of digits: %d", fDigitCount);
317   fDigitHeaderPtr->fNDigits = fDigitCount;
318
319 }
320
321 Int_t
322 AliHLTPHOSDigitMaker::CompareDigits(const void *dig0, const void *dig1)
323 {
324   // See header file for documentation
325   return (*((AliHLTPHOSDigitDataStruct**)(dig0)))->fID - (*((AliHLTPHOSDigitDataStruct**)(dig1)))->fID;
326 }