]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/TPCLib/AliHLTTPCPad.cxx
documentation
[u/mrichter/AliRoot.git] / HLT / TPCLib / AliHLTTPCPad.cxx
1 // @(#) $Id$
2
3 /**************************************************************************
4  * This file is property of and copyright by the ALICE HLT Project        * 
5  * ALICE Experiment at CERN, All rights reserved.                         *
6  *                                                                        *
7  * Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no>        *
8  *                  for The ALICE HLT Project.                            *
9  *                                                                        *
10  * Permission to use, copy, modify and distribute this software and its   *
11  * documentation strictly for non-commercial purposes is hereby granted   *
12  * without fee, provided that the above copyright notice appears in all   *
13  * copies and that both the copyright notice and this permission notice   *
14  * appear in the supporting documentation. The authors make no claims     *
15  * about the suitability of this software for any purpose. It is          *
16  * provided "as is" without express or implied warranty.                  *
17  **************************************************************************/
18
19 /** @file   AliHLTTPCPad.cxx
20     @author Matthias Richter
21     @date   
22     @brief  Container Class for TPC Pads.
23 */
24
25 #if __GNUC__>= 3
26 using namespace std;
27 #endif
28
29 #include <cerrno>
30 #include "AliHLTTPCPad.h"
31 #include "AliHLTStdIncludes.h"
32
33 /** margin for the base line be re-avaluated */
34 #define ALIHLTPAD_BASELINE_MARGIN (2*fAverage)
35
36 /** ROOT macro for the implementation of ROOT specific class methods */
37 ClassImp(AliHLTTPCPad)
38
39 AliHLTTPCPad::AliHLTTPCPad()
40   :
41   fRowNo(-1),
42   fPadNo(-1),
43   fThreshold(0),
44   fAverage(-1),
45   fNofEvents(0),
46   fSum(0),
47   fCount(0),
48   fTotal(0),
49   fBLMax(-1),
50   fBLMaxBin(-1),
51   fBLMin(-1),
52   fBLMinBin(-1),
53   fFirstBLBin(0),
54   fNofBins(0),
55   fReadPos(0),
56   fpRawData(NULL)
57 {
58   // see header file for class documentation
59   // or
60   // refer to README to build package
61   // or
62   // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
63 }
64
65 AliHLTTPCPad::AliHLTTPCPad(Int_t offset, Int_t nofBins)
66   :
67   fRowNo(-1),
68   fPadNo(-1),
69   fThreshold(0),
70   fAverage(-1),
71   fNofEvents(0),
72   fSum(0),
73   fCount(0),
74   fTotal(0),
75   fBLMax(-1),
76   fBLMaxBin(-1),
77   fBLMin(-1),
78   fBLMinBin(-1),
79   fFirstBLBin(offset),
80   fNofBins(nofBins),
81   fReadPos(0),
82   fpRawData(NULL)
83 {
84   // see header file for class documentation
85 }
86
87 AliHLTTPCPad::AliHLTTPCPad(const AliHLTTPCPad& srcPad)
88   :
89   fRowNo(srcPad.fRowNo),
90   fPadNo(srcPad.fPadNo),
91   fThreshold(0),
92   fAverage(-1),
93   fNofEvents(0),
94   fSum(0),
95   fCount(0),
96   fTotal(0),
97   fBLMax(-1),
98   fBLMaxBin(-1),
99   fBLMin(-1),
100   fBLMinBin(-1),
101   fFirstBLBin(0),
102   fNofBins(0),
103   fReadPos(0),
104   fpRawData(NULL)
105 {
106   // see header file for class documentation
107   HLTFatal("copy constructor not implemented");
108 }
109
110 AliHLTTPCPad& AliHLTTPCPad::operator=(const AliHLTTPCPad&)
111 {
112   // see header file for class documentation
113   HLTFatal("assignment operator not implemented");
114   return (*this);
115 }
116
117 AliHLTTPCPad::~AliHLTTPCPad()
118 {
119   // see header file for class documentation
120   if (fpRawData) {
121     HLTWarning("event data acquisition not stopped");
122     StopEvent();
123   }
124 }
125
126 Int_t AliHLTTPCPad::SetID(Int_t rowno, Int_t padno)
127 {
128   // see header file for class documentation
129   fRowNo=rowno;
130   fPadNo=padno;
131   return 0;
132 }
133
134 Int_t AliHLTTPCPad::StartEvent()
135 {
136   // see header file for class documentation
137   Int_t iResult=0;
138   if (fpRawData==NULL) {
139     fBLMax=-1;
140     fBLMaxBin=-1;
141     fBLMin=-1;
142     fBLMinBin=-1;
143     fSum=0;
144     fCount=0;
145     fTotal=0;
146     if (fNofBins>0) {
147       fpRawData=new AliHLTTPCSignal_t[fNofBins];
148       if (fpRawData) {
149         for (int i=0; i<fNofBins; i++) fpRawData[i]=-1;
150       } else {
151         HLTError("memory allocation failed");
152         iResult=-ENOMEM;
153       }
154     }
155   } else {
156     HLTWarning("event data acquisition already started");
157     iResult=-EALREADY;
158   }
159   return iResult;
160 }
161
162 Int_t AliHLTTPCPad::CalculateBaseLine(Int_t reqMinCount)
163 {
164   // see header file for class documentation
165   Int_t iResult=0;
166   AliHLTTPCSignal_t avBackup=fAverage;
167   //HLTDebug("reqMinCount=%d fCount=%d fTotal=%d fSum=%d fBLMax=%d fBLMin=%d", reqMinCount, fCount, fTotal, fSum, fBLMax, fBLMin);
168   if (fCount>=reqMinCount && fCount>=fTotal/2) {
169     fAverage=fCount>0?fSum/fCount:0;
170     if (fAverage>0) {
171       //HLTDebug("average for current event %d (%d - %d)", fAverage, fBLMax, fBLMin);
172       fCount=0;fSum=-1;
173       if (fBLMax>ALIHLTPAD_BASELINE_MARGIN) {
174         // calculate again
175         //HLTDebug("maximum value %d exceeds margin for base line (%d) "
176         //       "-> re-evaluate base line", fBLMax, ALIHLTPAD_BASELINE_MARGIN);
177         if (fpRawData) {
178           for (Int_t i=fFirstBLBin; i<fNofBins; i++)
179             if (fpRawData[i]>=0) AddBaseLineValue(i, fpRawData[i]);
180           if (fCount>0 && fCount>=reqMinCount && fCount>=fTotal/2) {
181             fAverage=fSum/fCount;
182             //HLTDebug("new average %d", fAverage);
183           } else {
184 //          HLTDebug("baseline re-eveluation skipped because of to few "
185 //                     "contributing bins: total=%d, contributing=%d, req=%d"
186 //                     "\ndata might be already zero suppressed"
187 //                     , fTotal, fCount, reqMinCount);
188             iResult=-ENODATA;
189           }
190           fCount=0;fSum=-1;
191         } else {
192           HLTError("missing raw data for base line calculation");
193           iResult=-ENOBUFS;
194         }
195       }
196       if (iResult>=0) {
197         // calculate average for all events
198         fAverage=((avBackup*fNofEvents)+fAverage)/(fNofEvents+1);
199         //HLTDebug("base line average for %d event(s): %d", fNofEvents+1, fAverage);
200       } else {
201         fAverage=avBackup;      
202       }
203     } else {
204       fAverage=avBackup;
205     }
206   } else {
207 //     HLTDebug("baseline calculation skipped because of to few contributing "
208 //             "bins: total=%d, contributing=%d, required=%d \ndata might be "
209 //             "already zero suppressed", fTotal, fCount, reqMinCount);
210   }
211
212   return iResult;
213 }
214
215 Int_t AliHLTTPCPad::StopEvent()
216 {
217   // see header file for class documentation
218   Int_t iResult=0;
219   if (fpRawData) {
220     AliHLTTPCSignal_t* pData=fpRawData;
221     fpRawData=NULL;
222     delete [] pData;
223     fTotal=0;
224     fNofEvents++;
225     Rewind();
226   } else if (fNofBins>0) {
227     HLTError("event data acquisition not started");
228     iResult=-EBADF;
229   }
230   return iResult;
231 }
232
233 Int_t AliHLTTPCPad::ResetHistory()
234 {
235   // see header file for class documentation
236   Int_t iResult=0;
237   fAverage=-1;
238   fNofEvents=0;
239   return iResult;
240 }
241
242 Int_t AliHLTTPCPad::SetThreshold(AliHLTTPCSignal_t thresh)
243 {
244   // see header file for class documentation
245   Int_t iResult=0;
246   fThreshold=thresh;
247   return iResult;
248 }
249
250 Int_t AliHLTTPCPad::AddBaseLineValue(Int_t bin, AliHLTTPCSignal_t value)
251 {
252   // see header file for class documentation
253   Int_t iResult=0;
254   if (bin>=fFirstBLBin) {
255     if (fAverage<0 || value<ALIHLTPAD_BASELINE_MARGIN) {
256       // add to the current sum and count
257       fSum+=value;
258       fCount++;
259       if (fBLMax<value) {
260         // keep the maximum value for later quality control of the base 
261         // line calculation
262         fBLMax=value;
263         fBLMaxBin=bin;
264       }
265       if (fBLMin<0 || fBLMin>value) {
266         // keep the minimum value for later quality control of the base 
267         // line calculation
268         fBLMin=value;
269         fBLMinBin=bin;
270       }
271     } else {
272 //       HLTDebug("ignoring value %d (bin %d) for base line calculation "
273 //             "(current average is %d)",
274 //             value, bin, fAverage);
275     }
276   }
277   return iResult;
278 }
279
280 Int_t AliHLTTPCPad::SetRawData(Int_t bin, AliHLTTPCSignal_t value)
281 {
282   // see header file for class documentation
283   Int_t iResult=0;
284   if (fpRawData) {
285     if (bin<fNofBins) {
286       if (value>=0) {
287         if (fpRawData[bin]<0) {
288           AddBaseLineValue(bin, value);
289           fTotal++;
290         } else {
291           // ignore value for average calculation
292           HLTWarning("overriding content of bin %d (%d)", bin, fpRawData[bin]);
293         }
294         fpRawData[bin]=value;
295       } else {
296         HLTWarning("ignoring neg. raw data");
297       }
298     } else {
299       HLTWarning("bin %d out of range (%d)", bin, fNofBins);
300       iResult=-ERANGE;
301     }
302   } else if (fNofBins>0) {
303     HLTError("event cycle not started");
304     iResult=-EBADF;
305   }
306   return iResult;
307 }
308
309 Int_t AliHLTTPCPad::Next(Int_t bZeroSuppression) 
310 {
311   // see header file for class documentation
312   if (fpRawData==NULL) return 0;
313   Int_t iResult=fReadPos<fNofBins;
314   if (iResult>0 && (iResult=(++fReadPos<fNofBins))>0) {
315     if (bZeroSuppression) {
316       while ((iResult=(fReadPos<fNofBins))>0 &&
317              GetCorrectedData(fReadPos)<=0)
318         fReadPos++;
319     }
320   }
321   return iResult;
322 }
323
324 Int_t AliHLTTPCPad::Rewind(Int_t bZeroSuppression)
325 {
326   // see header file for class documentation
327   fReadPos=(bZeroSuppression>0?0:fFirstBLBin)-1;
328   return Next(bZeroSuppression);
329 }
330
331 AliHLTTPCSignal_t AliHLTTPCPad::GetRawData(Int_t bin) const
332 {
333   // see header file for class documentation
334   AliHLTTPCSignal_t data=0;
335   if (fpRawData) {
336     if (bin<fNofBins) {
337       data=fpRawData[bin];
338     } else {
339       HLTWarning("requested bin %d out of range (%d)", bin, fNofBins);
340     }
341   } else if (fNofBins>0) {
342     HLTWarning("data only available within event cycle");
343   }
344   return data;
345 }
346
347 AliHLTTPCSignal_t AliHLTTPCPad::GetCorrectedData(Int_t bin) const
348 {
349   // see header file for class documentation
350   AliHLTTPCSignal_t data=GetRawData(bin)-GetBaseLine(bin);
351   AliHLTTPCSignal_t prev=0;
352   if (bin>1) prev=GetRawData(bin-1)-GetBaseLine(bin-1);
353   AliHLTTPCSignal_t succ=0;
354   if (bin+1<GetSize()) succ=GetRawData(bin+1)-GetBaseLine(bin+1);
355   if (fThreshold>0) {
356     data-=fThreshold;
357     prev-=fThreshold;
358     succ-=fThreshold;
359   }
360  
361   // case 1:
362   // the signal is below the base-line and threshold
363   if (data<0) data=0;
364
365   //case 2:
366   // the neighboring bins are both below base-line/threshold
367   // a real signal is always more than one bin wide because of the shaper 
368   if (prev<=0 && succ<=0) data=0;
369  
370   // case 3:
371   // the bin is inside the range of ignored bins
372   if (bin<fFirstBLBin) data=0;
373   //HLTDebug("fReadPos=%d data=%d threshold=%d raw data=%d base line=%d", fReadPos, data, fThreshold, GetRawData(bin), GetBaseLine(bin));
374   return data;
375 }
376
377 AliHLTTPCSignal_t AliHLTTPCPad::GetBaseLine(Int_t bin) const
378 {
379   // see header file for class documentation
380   AliHLTTPCSignal_t val=0;
381   if (fAverage>0) {
382     // we take the minumum value as the base line if it doesn't differ from
383     // the average to much
384     val=fAverage;
385 #ifdef KEEP_NOISE
386     const AliHLTTPCSignal_t kMaxDifference=15;
387     if ((fAverage-fBLMin)<=kMaxDifference) val=fBLMin;
388     else val>kMaxDifference?val-=kMaxDifference:0;
389 #endif
390   }
391   if (val<0) {
392     // here we should never get
393     val=0;
394     HLTFatal("wrong base line value");
395   }
396   return val;
397 }
398
399 AliHLTTPCSignal_t AliHLTTPCPad::GetAverage() const
400 {
401   // see header file for class documentation
402   return fAverage>0?fAverage:0;
403 }
404
405 Float_t AliHLTTPCPad::GetOccupancy() const
406 {
407   // see header file for class documentation
408   Float_t occupancy=0;
409   if (fpRawData && fNofBins>0) {
410     for (Int_t i=fFirstBLBin; i<fNofBins; i++) {
411       if (GetCorrectedData(i)>0) occupancy+=1;
412     }
413     if (fNofBins-fFirstBLBin>0)
414       occupancy/=fNofBins-fFirstBLBin;
415   }
416   return occupancy;
417 }
418
419 Float_t AliHLTTPCPad::GetAveragedOccupancy() const
420 {
421   // see header file for class documentation
422
423   // history is not yet implemented
424   return GetOccupancy();
425 }