Coverity 15169
[u/mrichter/AliRoot.git] / EVE / EveDet / AliEveTPCSectorData.cxx
CommitLineData
d810d0de 1// $Id$
2// Main authors: Matevz Tadel & Alja Mrak-Tadel: 2006, 2007
915dabe1 3
d810d0de 4/**************************************************************************
5 * Copyright(c) 1998-2008, ALICE Experiment at CERN, all rights reserved. *
6 * See http://aliceinfo.cern.ch/Offline/AliRoot/License.html for *
51346b82 7 * full copyright notice. *
d810d0de 8 **************************************************************************/
9
10#include "AliEveTPCSectorData.h"
915dabe1 11#include <AliTPCParamSR.h>
12
a15e6d7d 13#include <set>
14
15//==============================================================================
16//==============================================================================
17// AliEveTPCSectorData
18//==============================================================================
915dabe1 19
57ffa5fb 20//______________________________________________________________________________
915dabe1 21//
22// Stores data from a fiven TPC sector.
23//
24// Row addresses grow linearly by radius, there is no separation on
25// inner/outer segments. The SegmentInfo objects can be used to get
26// information about low-level segments.
27//
a97abca8 28// A lot of TPC-sector geometry information is stored as static data.
915dabe1 29//
d810d0de 30// For accessing data, see for example AliEveTPCSector2DGL::CreateTexture()
915dabe1 31// and LoadPadrow().
32//
915dabe1 33
d810d0de 34ClassImp(AliEveTPCSectorData)
35
36AliTPCParam* AliEveTPCSectorData::fgParam = 0;
37Float_t AliEveTPCSectorData::fgZLength = 0;
38Int_t AliEveTPCSectorData::fgNAllRows = 0;
39Int_t AliEveTPCSectorData::fgNAllPads = 0;
40Int_t* AliEveTPCSectorData::fgRowBegs = 0;
915dabe1 41
d810d0de 42AliEveTPCSectorData::SegmentInfo AliEveTPCSectorData::fgInnSeg;
43AliEveTPCSectorData::SegmentInfo AliEveTPCSectorData::fgOut1Seg;
44AliEveTPCSectorData::SegmentInfo AliEveTPCSectorData::fgOut2Seg;
915dabe1 45
d810d0de 46AliEveTPCSectorData::SegmentInfo* AliEveTPCSectorData::fgSegInfoPtrs[3] = {0};
915dabe1 47
57ffa5fb 48/******************************************************************************/
915dabe1 49
d810d0de 50void AliEveTPCSectorData::InitStatics()
915dabe1 51{
a97abca8 52 // Initialize static variables.
53
54 if (fgParam != 0) return;
915dabe1 55
56 fgParam = new AliTPCParamSR;
a97abca8 57 fgZLength = fgParam->GetZLength(0) + 0.275;
58 fgNAllRows = fgParam->GetNRowLow() + fgParam->GetNRowUp();
915dabe1 59 fgNAllPads = 0;
60 fgRowBegs = new Int_t[fgNAllRows + 1];
61
a15e6d7d 62 {
9af86b69 63 Int_t row = 0;
64 for (Int_t i=0; i<fgParam->GetNRowLow(); ++i, ++row)
65 {
66 fgRowBegs[row] = fgNAllPads;
67 fgNAllPads += fgParam->GetNPadsLow(i);
68 }
69 for (Int_t i=0; i<fgParam->GetNRowUp(); ++i, ++row)
70 {
71 fgRowBegs[row] = fgNAllPads;
72 fgNAllPads += fgParam->GetNPadsUp(i);
73 }
74 fgRowBegs[fgNAllRows] = fgNAllPads;
915dabe1 75 }
915dabe1 76
77 // Fill SegmentInfos, used by rendering classes.
78
79 // General paramameters
80 fgInnSeg.fPadWidth = fgParam->GetInnerPadPitchWidth();
81 fgInnSeg.fPadHeight = fgParam->GetInnerPadPitchLength();
82 fgInnSeg.fRLow = fgParam->GetPadRowRadiiLow(0);
83 fgInnSeg.fNRows = fgParam->GetNRowLow();
84 fgInnSeg.fFirstRow = 0;
85 fgInnSeg.fLastRow = fgInnSeg.fNRows - 1;
86 fgInnSeg.fNMaxPads = fgParam->GetNPadsLow(fgInnSeg.fNRows - 1);
87 fgSegInfoPtrs[0] = &fgInnSeg;
88
89 fgOut1Seg.fPadWidth = fgParam->GetOuterPadPitchWidth();
90 fgOut1Seg.fPadHeight = fgParam->GetOuter1PadPitchLength();
91 fgOut1Seg.fRLow = fgParam->GetPadRowRadiiUp(0);
92 fgOut1Seg.fNRows = fgParam->GetNRowUp1();
93 fgOut1Seg.fFirstRow = fgInnSeg.fNRows;
94 fgOut1Seg.fLastRow = fgOut1Seg.fFirstRow + fgOut1Seg.fNRows - 1;
95 fgOut1Seg.fNMaxPads = fgParam->GetNPadsUp(fgOut1Seg.fNRows - 1);
96 fgSegInfoPtrs[1] = &fgOut1Seg;
51346b82 97
915dabe1 98 fgOut2Seg.fPadWidth = fgParam->GetOuterPadPitchWidth();
99 fgOut2Seg.fPadHeight = fgParam->GetOuter2PadPitchLength();
100 fgOut2Seg.fRLow = fgParam->GetPadRowRadiiUp(fgOut1Seg.fNRows);
101 fgOut2Seg.fNRows = fgParam->GetNRowUp() - fgOut1Seg.fNRows;
102 fgOut2Seg.fFirstRow = fgOut1Seg.fLastRow + 1;
103 fgOut2Seg.fLastRow = fgOut2Seg.fFirstRow + fgOut2Seg.fNRows - 1;
104 fgOut2Seg.fNMaxPads = fgParam->GetNPadsUp(fgParam->GetNRowUp() - 1);
105 fgSegInfoPtrs[2] = &fgOut2Seg;
106
9af86b69 107 // Set stepsize arrays
108
109 { // Inner
110 Int_t k=0, npads = fgParam->GetNPadsLow(0);
111 for (Int_t row = 0; row < fgInnSeg.fNRows; ++row)
a15e6d7d 112 {
9af86b69 113 if (fgParam->GetNPadsLow(row) > npads)
114 {
115 npads = fgParam->GetNPadsLow(row);
116 fgInnSeg.fYStep[k] = row*fgInnSeg.fPadHeight + fgInnSeg.fRLow;
117 k++;
118 }
915dabe1 119 }
9af86b69 120 fgInnSeg.fNYSteps = k;
915dabe1 121 }
9af86b69 122
123 { // Outer 1 seg
124 Int_t k=0, npads = fgParam->GetNPadsUp(0);
125 for (Int_t row = 0; row < fgOut1Seg.fNRows; ++row)
a15e6d7d 126 {
9af86b69 127 if (fgParam->GetNPadsUp(row) > npads)
128 {
129 npads = fgParam->GetNPadsUp(row);
130 fgOut1Seg.fYStep[k] = row*fgOut1Seg.fPadHeight + fgOut1Seg.fRLow ;
131 k++;
132 }
915dabe1 133 }
9af86b69 134 fgOut1Seg.fNYSteps = k;
915dabe1 135 }
9af86b69 136
137 { // Outer 2 seg
138 Int_t k=0, npads = fgParam->GetNPadsUp(fgOut1Seg.fNRows);
139 for (Int_t row = fgOut1Seg.fNRows; row < fgParam->GetNRowUp(); ++row)
a15e6d7d 140 {
9af86b69 141 if (fgParam->GetNPadsUp(row) > npads)
142 {
143 npads = fgParam->GetNPadsUp(row);
144 fgOut2Seg.fYStep[k] = (row - fgOut1Seg.fNRows)*fgOut2Seg.fPadHeight + fgOut2Seg.fRLow ;
145 k++;
146 }
915dabe1 147 }
9af86b69 148 fgOut2Seg.fNYSteps = k;
915dabe1 149 }
915dabe1 150}
151
d810d0de 152Int_t AliEveTPCSectorData::GetNPadsInRow(Int_t row)
915dabe1 153{
a97abca8 154 // Return number of pads in given row.
155
a15e6d7d 156 if (row < 0 || row >= fgNAllRows) return 0;
915dabe1 157 return fgRowBegs[row + 1] - fgRowBegs[row];
158}
159
d810d0de 160const AliEveTPCSectorData::SegmentInfo& AliEveTPCSectorData::GetSeg(Int_t seg)
915dabe1 161{
a97abca8 162 // Return reference to segment geometry information.
163 // 0 ~ inner, 1 ~ middle, 2 ~ outer.
164
915dabe1 165 static const SegmentInfo null;
166
a15e6d7d 167 if (seg < 0 || seg > 2)
915dabe1 168 return null;
169 else
170 return *fgSegInfoPtrs[seg];
171}
172
57ffa5fb 173/******************************************************************************/
915dabe1 174// True member functions start here.
57ffa5fb 175/******************************************************************************/
915dabe1 176
d810d0de 177void AliEveTPCSectorData::NewBlock()
915dabe1 178{
a97abca8 179 // Create new data-block. Position is set to the beginning.
180
a15e6d7d 181 fBlocks.push_back(new Short_t[fkBlockSize]);
915dabe1 182 fBlockPos = 0;
183}
184
57ffa5fb 185/******************************************************************************/
915dabe1 186
d810d0de 187AliEveTPCSectorData::AliEveTPCSectorData(Int_t sector, Int_t bsize) :
a15e6d7d 188 fSectorID(sector), fNPadsFilled(0), fPads(),
189 fkBlockSize(bsize), fBlockPos(0), fBlocks(),
4b456ebb 190 fCurrentRow(0), fCurrentPad(0), fCurrentPos(0), fCurrentStep(0)
915dabe1 191{
a97abca8 192 // Constructor.
51f93891 193
194 memset(fPadBuffer,0,2048*sizeof(Short_t));
a97abca8 195
a15e6d7d 196 if (fgParam == 0) InitStatics();
915dabe1 197
198 fPads.assign(fgNAllPads, PadData());
199 fBlocks.reserve(16);
a15e6d7d 200 fBlockPos = fkBlockSize; // Enforce creation of a new block.
915dabe1 201}
202
203
d810d0de 204AliEveTPCSectorData::~AliEveTPCSectorData()
915dabe1 205{
a97abca8 206 // Destructor.
207
a15e6d7d 208 for (std::vector<Short_t*>::iterator b=fBlocks.begin(); b!=fBlocks.end(); ++b)
915dabe1 209 delete [] *b;
210}
211
d810d0de 212void AliEveTPCSectorData::DropData()
092578a7 213{
a97abca8 214 // Drop data, deallocate data-blocks.
215
092578a7 216 fPads.assign(fgNAllPads, PadData());
a15e6d7d 217 for (std::vector<Short_t*>::iterator b=fBlocks.begin(); b!=fBlocks.end(); ++b)
092578a7 218 delete [] *b;
219 fBlocks.clear();
a15e6d7d 220 fBlockPos = fkBlockSize; // Enforce creation of a new block.
092578a7 221}
222
57ffa5fb 223/******************************************************************************/
915dabe1 224
d810d0de 225void AliEveTPCSectorData::Print(Option_t* /*opt*/) const
915dabe1 226{
a97abca8 227 // Print summary information.
228
d810d0de 229 printf("AliEveTPCSectorData sector=%d, NPadsFilled=%d, NBlocks=%d, BlockPos=%d\n",
6242b962 230 fSectorID, fNPadsFilled, (Int_t) fBlocks.size(), fBlockPos);
915dabe1 231}
232
57ffa5fb 233/******************************************************************************/
915dabe1 234
d810d0de 235void AliEveTPCSectorData::BeginPad(Int_t row, Int_t pad, Bool_t reverseTime)
915dabe1 236{
a97abca8 237 // Begin filling of pad-data as specified with arguments.
238
915dabe1 239 fCurrentRow = row;
240 fCurrentPad = pad;
a15e6d7d 241 if (reverseTime) {
915dabe1 242 fCurrentPos = 2046;
243 fCurrentStep = -2;
244 } else {
245 fCurrentPos = 0;
246 fCurrentStep = 2;
247 }
092578a7 248 //printf("begpad for row=%d pad=%d\n buf=%p pos=%d step=%d\n",
249 // fCurrentRow, fCurrentPad,
250 // fPadBuffer, fCurrentPos, fCurrentStep);
915dabe1 251}
252
d810d0de 253void AliEveTPCSectorData::EndPad(Bool_t autoPedestal, Short_t threshold)
915dabe1 254{
a97abca8 255 // End filling of pad-data. At this point data is compressed and moved
256 // into the cuurent position in memory block.
257
915dabe1 258 Short_t *beg, *end;
a97abca8 259 if (fCurrentStep > 0) {
915dabe1 260 beg = fPadBuffer;
261 end = fPadBuffer + fCurrentPos;
262 } else {
263 beg = fPadBuffer + fCurrentPos + 2;
264 end = fPadBuffer + 2048;
265 }
d6433e5d 266
092578a7 267 //printf("endpad for row=%d pad=%d\n buf=%p beg=%p end=%p pos=%d step=%d\n",
268 // fCurrentRow, fCurrentPad,
269 // fPadBuffer, beg, end, fCurrentPos, fCurrentStep);
a97abca8 270 if (beg >= end)
092578a7 271 return;
272
a97abca8 273 if (autoPedestal) {
d6433e5d 274 Short_t array[1024];
275 Short_t* val;
51346b82 276 val = beg + 1;
a15e6d7d 277 while (val <= end) {
d6433e5d 278 array[(val-beg)/2] = *val;
279 val += 2;
51346b82 280 }
d6433e5d 281 Short_t pedestal = TMath::Nint(TMath::Median((end-beg)/2, array));
282 val = beg + 1;
a97abca8 283 while (val <= end) {
d6433e5d 284 *val -= pedestal;
285 val += 2;
286 }
287 Short_t* wpos = beg;
288 Short_t* rpos = beg;
a97abca8 289 while (rpos < end) {
290 if (rpos[1] >= threshold) {
d6433e5d 291 wpos[0] = rpos[0];
292 wpos[1] = rpos[1];
293 wpos += 2;
294 }
295 rpos += 2;
296 }
297 end = wpos;
298 }
299
915dabe1 300 Short_t* wpos = beg;
301 Short_t* rpos = beg;
302
303 // Compress pad buffer
a97abca8 304 while (rpos < end) {
915dabe1 305 Short_t* spos = rpos;
306 Short_t t = spos[0];
a97abca8 307 while (true) {
915dabe1 308 rpos += 2;
a15e6d7d 309 if (rpos >= end || *rpos > t + 1 || t == 0)
915dabe1 310 break;
311 ++t;
312 }
313 Short_t n = t - spos[0] + 1;
a97abca8 314 if (n == 1) {
915dabe1 315 wpos[0] = -spos[0];
316 wpos[1] = spos[1];
317 wpos += 2;
318 } else {
319 wpos[0] = spos[0];
320 wpos[2] = spos[1];
321 wpos[1] = n;
322 wpos += 3; spos += 3;
a97abca8 323 while (--n) {
915dabe1 324 *wpos = *spos;
325 ++wpos; spos += 2;
326 }
327 }
328 }
329
330 // Copy buffer to storage, set PadData
a97abca8 331 if (wpos > beg) {
915dabe1 332 Short_t len = wpos - beg;
a15e6d7d 333 if (len > fkBlockSize - fBlockPos)
915dabe1 334 NewBlock();
335 Short_t *dest = fBlocks.back() + fBlockPos;
336 memcpy(dest, beg, len*sizeof(Short_t));
337 fBlockPos += len;
338
339 PadData& pad = fPads[PadIndex(fCurrentRow, fCurrentPad)];
340 pad.SetDataLength(dest, len);
341 }
342
343 ++fNPadsFilled;
344}
345
57ffa5fb 346/******************************************************************************/
915dabe1 347
a15e6d7d 348const AliEveTPCSectorData::PadData& AliEveTPCSectorData::GetPadData(Int_t padAddr) const
915dabe1 349{
a97abca8 350 // Get pad-data reference by absolute index.
351
a15e6d7d 352 static const PadData kNull;
915dabe1 353
a15e6d7d 354 if (padAddr < 0 || padAddr >= fgNAllPads) return kNull;
915dabe1 355 return fPads[padAddr];
356}
357
a15e6d7d 358const AliEveTPCSectorData::PadData& AliEveTPCSectorData::GetPadData(Int_t row, Int_t pad) const
915dabe1 359{
a97abca8 360 // Get pad-data reference by row and pad number.
361
a15e6d7d 362 static const PadData kNull;
915dabe1 363
364 Int_t np = GetNPadsInRow(row);
a15e6d7d 365 if (np == 0 || pad < 0 || pad >= np) return kNull;
915dabe1 366 return GetPadData(fgRowBegs[row] + pad);
367}
368
d810d0de 369AliEveTPCSectorData::PadIterator AliEveTPCSectorData::MakePadIterator(Int_t padAddr, Short_t thr)
915dabe1 370{
a97abca8 371 // Get pad-data iterator by absolute index.
372
915dabe1 373 return PadIterator(GetPadData(padAddr), thr);
374}
375
d810d0de 376AliEveTPCSectorData::PadIterator AliEveTPCSectorData::MakePadIterator(Int_t row, Int_t pad, Short_t thr)
915dabe1 377{
a97abca8 378 // Get pad-data iterator by row and pad number.
379
915dabe1 380 return PadIterator(GetPadData(row, pad), thr);
381}
382
d810d0de 383AliEveTPCSectorData::RowIterator AliEveTPCSectorData::MakeRowIterator(Int_t row, Short_t thr)
915dabe1 384{
a97abca8 385 // Get row iterator.
386
915dabe1 387 Short_t npads = GetNPadsInRow(row);
a15e6d7d 388 if (npads > 0)
915dabe1 389 return RowIterator(&fPads[fgRowBegs[row]], npads, thr);
390 else
391 return RowIterator(0, 0);
392}
393
57ffa5fb 394/******************************************************************************/
d810d0de 395// AliEveTPCSectorData::PadData
57ffa5fb 396/******************************************************************************/
915dabe1 397
d810d0de 398void AliEveTPCSectorData::PadData::Print(Option_t* /*opt*/)
915dabe1 399{
a97abca8 400 // Print summary information.
401
915dabe1 402 printf("addr=%p, len=%hd>\n", (void*)fData, fLength);
a15e6d7d 403 for (Int_t i=0; i<fLength; ++i)
915dabe1 404 printf(" %3d %hd\n", i, fData[i]);
405}
406
57ffa5fb 407/******************************************************************************/
d810d0de 408// AliEveTPCSectorData::PadIterator
57ffa5fb 409/******************************************************************************/
915dabe1 410
d810d0de 411Bool_t AliEveTPCSectorData::PadIterator::Next()
915dabe1 412{
a97abca8 413 // Move iterator to next signal above the iteration threshold.
414 // Returns false when the end of data is reached.
415
416 if (fPos >= fEnd) return kFALSE;
417 if (fNChunk > 0) {
915dabe1 418 ++fTime;
419 --fNChunk;
420 fSignal = *fPos; ++fPos;
421 } else {
422 fTime = fPos[0];
a97abca8 423 if (fTime <= 0) {
915dabe1 424 fTime = -fTime;
425 fSignal = fPos[1];
426 fPos += 2;
427 } else {
428 fNChunk = fPos[1] - 1;
429 fSignal = fPos[2];
430 fPos += 3;
431 }
432 }
433 return (fSignal > fThreshold) ? kTRUE : Next();
434}
435
d810d0de 436void AliEveTPCSectorData::PadIterator::Reset()
915dabe1 437{
438 // Return to the beginning of the pad-data. Must call Next() to get to
439 // the first stored signal.
440
441 fPos = fBeg;
442 fTime = -1;
443 fSignal = -1;
444 fNChunk = 0;
445}
446
d810d0de 447void AliEveTPCSectorData::PadIterator::Reset(const PadData& pd)
915dabe1 448{
449 // Reinitialize to new pad-data. Must call Next() to get to the
450 // first stored signal.
451
452 fBeg = pd.Data();
453 fEnd = pd.Data() + pd.Length();
454 fPos = pd.Data();
455 Reset();
456}
457
d810d0de 458void AliEveTPCSectorData::PadIterator::Test()
915dabe1 459{
a15e6d7d 460 while (Next())
915dabe1 461 printf(" %3d %d\n", fTime, fSignal);
462}
463
57ffa5fb 464/******************************************************************************/
d810d0de 465// AliEveTPCSectorData::RowIterator
57ffa5fb 466/******************************************************************************/
915dabe1 467
d810d0de 468Bool_t AliEveTPCSectorData::RowIterator::NextPad()
915dabe1 469{
a97abca8 470 // Move iterator to next pad.
471
915dabe1 472 ++fPad;
473 if(fPad >= fNPads) return kFALSE;
474 Reset(fPadArray[fPad]);
475 return kTRUE;
476}
477
d810d0de 478void AliEveTPCSectorData::RowIterator::ResetRow()
915dabe1 479{
480 // Return to the beginning of the row. Must call NextPad() to get to
481 // the zeroth pad.
482
483 fPad = -1;
484}
485
d810d0de 486void AliEveTPCSectorData::RowIterator::ResetRow(const PadData* first, Short_t npads)
915dabe1 487{
488 // Reinitialize to another pad-data array. Must call NextPad() to
489 // get to the zeroth pad.
490
491 fPadArray = first;
492 fNPads = npads;
493 fPad = -1;
494}
495
57ffa5fb 496/******************************************************************************/
d810d0de 497// AliEveTPCSectorData::SegmentInfo
57ffa5fb 498/******************************************************************************/
915dabe1 499
a97abca8 500//______________________________________________________________________________
501//
502// Stores geometry data about a segment needed for fast data-access
503// and rendering
504
d810d0de 505ClassImp(AliEveTPCSectorData::SegmentInfo)
915dabe1 506
d810d0de 507AliEveTPCSectorData::SegmentInfo::SegmentInfo() :
265ecb21 508 TObject(),
509
510 fPadWidth(0), fPadHeight(0),
511 fRLow(0), fNRows(0), fFirstRow(0), fLastRow(0),
512 fNMaxPads(0),
513 fNYSteps(0)
915dabe1 514{
a97abca8 515 // Constructor.
516
d0ebdff6 517 memset(fYStep, 0, sizeof(fYStep));
915dabe1 518}