]> git.uio.no Git - u/mrichter/AliRoot.git/blame - HLT/TPCLib/AliHLTTPCPad.cxx
Adding CreateIterator(void) and GetNeighbours() pure virtual methods,
[u/mrichter/AliRoot.git] / HLT / TPCLib / AliHLTTPCPad.cxx
CommitLineData
46b33a24 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
23using namespace std;
24#endif
25
26#include <cerrno>
27#include "AliHLTTPCPad.h"
84645eb0 28#include "AliHLTStdIncludes.h"
46b33a24 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 */
34ClassImp(AliHLTTPCPad)
35
36AliHLTTPCPad::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),
84645eb0 46 fBLMin(-1),
47 fBLMinBin(-1),
46b33a24 48 fCount(0),
49 fTotal(0),
50 fpRawData(NULL),
51 fFirstBLBin(0),
52 fNofBins(0),
53 fReadPos(0)
54{
55}
56
57AliHLTTPCPad::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),
84645eb0 67 fBLMin(-1),
68 fBLMinBin(-1),
46b33a24 69 fCount(0),
70 fTotal(0),
71 fpRawData(NULL),
72 fFirstBLBin(offset),
73 fNofBins(nofBins),
74 fReadPos(0)
75{
76}
77
78AliHLTTPCPad::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),
84645eb0 88 fBLMin(-1),
89 fBLMinBin(-1),
46b33a24 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
100AliHLTTPCPad& AliHLTTPCPad::operator=(const AliHLTTPCPad&)
101{
102 HLTFatal("assignment operator not implemented");
103 return (*this);
104}
105
106AliHLTTPCPad::~AliHLTTPCPad()
107{
108 if (fpRawData) {
109 HLTWarning("event data acquisition not stopped");
110 StopEvent();
111 }
112}
113
114Int_t AliHLTTPCPad::SetID(Int_t rowno, Int_t padno)
115{
116 fRowNo=rowno;
117 fPadNo=padno;
118}
119
120Int_t AliHLTTPCPad::StartEvent()
121{
122 Int_t iResult=0;
123 if (fpRawData==NULL) {
124 fBLMax=-1;
125 fBLMaxBin=-1;
84645eb0 126 fBLMin=-1;
127 fBLMinBin=-1;
46b33a24 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
147Int_t AliHLTTPCPad::CalculateBaseLine(Int_t reqMinCount)
148{
149 Int_t iResult=0;
150 AliHLTTPCSignal_t avBackup=fAverage;
84645eb0 151 //HLTDebug("reqMinCount=%d fCount=%d fTotal=%d fSum=%d fBLMax=%d fBLMin=%d", reqMinCount, fCount, fTotal, fSum, fBLMax, fBLMin);
46b33a24 152 if (fCount>=reqMinCount && fCount>=fTotal/2) {
153 fAverage=fCount>0?fSum/fCount:0;
154 if (fAverage>0) {
84645eb0 155 //HLTDebug("average for current event %d (%d - %d)", fAverage, fBLMax, fBLMin);
46b33a24 156 fCount=0;fSum=-1;
157 if (fBLMax>ALIHLTPAD_BASELINE_MARGIN) {
158 // calculate again
84645eb0 159 HLTDebug("maximum value %d exceeds margin for base line (%d) "
46b33a24 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;
84645eb0 166 HLTDebug("new average %d", fAverage);
46b33a24 167 } else {
db16520a 168 HLTDebug("baseline re-eveluation skipped because of to few "
46b33a24 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);
84645eb0 183 //HLTDebug("base line average for %d event(s): %d", fNofEvents+1, fAverage);
46b33a24 184 } else {
185 fAverage=avBackup;
186 }
187 } else {
188 fAverage=avBackup;
189 }
190 } else {
db16520a 191 HLTDebug("baseline calculation skipped because of to few contributing "
46b33a24 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
199Int_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
216Int_t AliHLTTPCPad::ResetHistory()
217{
218 Int_t iResult=0;
219 fAverage=-1;
220 fNofEvents=0;
221 return iResult;
222}
223
224Int_t AliHLTTPCPad::SetThreshold(AliHLTTPCSignal_t thresh)
225{
226 Int_t iResult=0;
227 fThreshold=thresh;
228 return iResult;
229}
230
231Int_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 }
84645eb0 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 }
46b33a24 251 } else {
84645eb0 252 HLTDebug("ignoring value %d (bin %d) for base line calculation "
253 "(current average is %d)",
46b33a24 254 value, bin, fAverage);
255 }
256 }
257 return iResult;
258}
259
260Int_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
84645eb0 271 HLTWarning("overriding content of bin %d (%d)", bin, fpRawData[bin]);
46b33a24 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
288Int_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
302Int_t AliHLTTPCPad::Rewind(Int_t bZeroSuppression)
303{
304 fReadPos=(bZeroSuppression>0?0:fFirstBLBin)-1;
305 return Next(bZeroSuppression);
306}
307
308AliHLTTPCSignal_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
323AliHLTTPCSignal_t AliHLTTPCPad::GetCorrectedData(Int_t bin) const
324{
325 AliHLTTPCSignal_t data=GetRawData(bin)-GetBaseLine(bin);
5235c3e9 326 AliHLTTPCSignal_t prev=0;
327 if (bin>1) prev=GetRawData(bin-1)-GetBaseLine(bin-1);
328 AliHLTTPCSignal_t succ=0;
329 if (bin+1<GetSize()) succ=GetRawData(bin+1)-GetBaseLine(bin+1);
330 if (fThreshold>0) {
331 data-=fThreshold;
332 prev-=fThreshold;
333 succ-=fThreshold;
334 }
335
336 // case 1:
337 // the signal is below the base-line and threshold
46b33a24 338 if (data<0) data=0;
5235c3e9 339
340 //case 2:
341 // the neighboring bins are both below base-line/threshold
342 // a real signal is always more than one bin wide because of the shaper
343 if (prev<=0 && succ<=0) data=0;
344
345 // case 3:
346 // the bin is inside the range of ignored bins
46b33a24 347 if (bin<fFirstBLBin) data=0;
84645eb0 348 //HLTDebug("fReadPos=%d data=%d threshold=%d raw data=%d base line=%d", fReadPos, data, fThreshold, GetRawData(bin), GetBaseLine(bin));
46b33a24 349 return data;
350}
351
352AliHLTTPCSignal_t AliHLTTPCPad::GetBaseLine(Int_t bin) const
353{
84645eb0 354 AliHLTTPCSignal_t val=0;
355 if (fAverage>0) {
356 // we take the minumum value as the base line if it doesn't differ from
357 // the average to much
358 const AliHLTTPCSignal_t kMaxDifference=15;
359 val=fAverage;
5235c3e9 360#ifdef KEEP_NOISE
84645eb0 361 if ((fAverage-fBLMin)<=kMaxDifference) val=fBLMin;
362 else val>kMaxDifference?val-=kMaxDifference:0;
5235c3e9 363#endif
84645eb0 364 }
365 if (val<0) {
366 // here we should never get
367 val=0;
368 HLTFatal("wrong base line value");
369 }
370 return val;
46b33a24 371}
372
373AliHLTTPCSignal_t AliHLTTPCPad::GetAverage() const
374{
375 return fAverage>0?fAverage:0;
376}
377
5235c3e9 378Float_t AliHLTTPCPad::GetOccupancy() const
379{
380 Float_t occupancy=0;
381 if (fpRawData && fNofBins>0) {
382 for (Int_t i=fFirstBLBin; i<fNofBins; i++) {
383 if (GetCorrectedData(i)>0) occupancy+=1;
384 }
385 if (fNofBins-fFirstBLBin>0)
386 occupancy/=fNofBins-fFirstBLBin;
387 }
388 return occupancy;
389}
390
391Float_t AliHLTTPCPad::GetAveragedOccupancy() const
392{
393 // history is not yet implemented
394 return GetOccupancy();
395}