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