]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TPC/AliSimDigits.cxx
Coverity fixes
[u/mrichter/AliRoot.git] / TPC / AliSimDigits.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
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 /* $Id$ */
17
18 ///////////////////////////////////////////////////////////////////////////////
19 //                                                                           //
20 //  Alice segment manager object                                             //
21 //  AliSimDigits object   (derived from AliDigits)                           //
22 //  provide additional track information to digit                            //
23 //   Origin: Marian Ivanov  GSI Darmstadt                                    //
24 //                                                                           //
25 //                                                                           //
26 ///////////////////////////////////////////////////////////////////////////////
27
28 #include "TClass.h"
29 #include <Riostream.h>
30 #include "TError.h"
31 #include "AliSegmentID.h"
32 #include "AliH2F.h"
33 #include "TArrayI.h"
34 #include "AliDigits.h"
35 #include "AliSimDigits.h"
36 #include "AliTPCdigit.h"
37 #include <TClonesArray.h>
38
39
40
41 //_____________________________________________________________________________
42 //_____________________________________________________________________________
43 //_____________________________________________________________________________
44 ClassImp(AliSimDigits)
45
46 AliSimDigits::AliSimDigits()
47              :AliDigits(),
48               fTracks(0),
49               fTrIndex(0),
50               fNlevel(0),
51               fTrBufType(0)  
52 {
53   //  AliDigits::Invalite();
54  
55   InvalidateTrack();
56 }
57 //
58 AliSimDigits::AliSimDigits(const AliSimDigits &param)
59              :AliDigits(),
60               fTracks(0),
61               fTrIndex(0),
62               fNlevel(0),
63               fTrBufType(0) 
64 {
65   //
66   // dummy
67   //
68   fTrIndex = param.fTrIndex;
69 }
70 //
71 AliSimDigits::~AliSimDigits()
72 {
73
74   if (fTracks != 0) {
75     delete fTracks;
76   }
77   if (fTrIndex != 0) { 
78     delete fTrIndex;
79   } 
80
81 }
82 AliSimDigits & AliSimDigits::operator =(const AliSimDigits & param)
83 {
84   //
85   // assignment operator - dummy
86   //
87   if(this!=&param){
88     fTrIndex=param.fTrIndex;
89   }
90   return (*this);
91 }
92
93 //__________________________________________________________________
94 void AliSimDigits::InvalidateTrack() 
95
96   //
97   //set default (invalid parameters)
98   if ( fTracks != 0) delete fTracks;
99   fTracks = new TArrayI;
100   if ( fTrIndex  != 0) delete fTrIndex;
101   fTrIndex = new TArrayI;
102
103   for (Int_t i = 0; i<3; i++){
104     fTracks->Set(0);
105     fTrIndex->Set(0);
106   }
107 }
108
109 void  AliSimDigits::AllocateTrack(Int_t length)
110 {
111   //
112   //construct empty buffer fElements and fTracks with size fNrows x fNcols x
113   //length 
114   InvalidateTrack();
115   fNlevel = length;
116   fTracks->Set(fNcols*fNrows*fNlevel);
117   fTrIndex->Set(0); 
118   fTrBufType =0;
119 }
120
121 Int_t AliSimDigits::GetTrackID(Int_t row, Int_t column, Int_t level) 
122 {
123   //
124   //Get track ID 
125   if (fTrBufType == 0) return  GetTrackIDFast(row, column,level);
126   if (fTrBufType == 1) return  GetTrackID1(row, column,level); 
127   if (fTrBufType == 2) return  GetTrackID2(row, column,level); 
128   return 0;
129 }
130
131 void AliSimDigits::ExpandTrackBuffer()
132 {  
133   //
134   //expand buffer to two dimensional array 
135   if (fTrBufType<0)  {
136     Error("ExpandBuffer", "buffer doesn't exist");
137     return;
138   }
139   if (fTrBufType==0)      return;  //buffer expanded
140   if (fTrBufType==1)  {ExpandTrackBuffer1(); return;}
141   if (fTrBufType==2)  ExpandTrackBuffer2();
142  
143 }
144
145 void AliSimDigits::CompresTrackBuffer(Int_t bufType)
146 {
147   //
148   //compres buffer according buffertype algorithm
149   //
150   if (fTrBufType<0)  {
151     Error("CompressBuffer", "buffer doesn't exist");
152     return;
153   }
154   if (fTrBufType == bufType) return;
155   //
156   if (fTrBufType>0) ExpandTrackBuffer();
157   if (fTrBufType !=0)  {
158     Error("CompressBuffer", "buffer doesn't exist");
159     return;
160   }
161   //compress buffer of type 1
162   
163   if (bufType==1)      {CompresTrackBuffer1();return;}
164   if (bufType==2)      CompresTrackBuffer2();
165    
166 }
167
168 Int_t  AliSimDigits::GetTrackID1(Int_t row, Int_t column, Int_t level)
169 {
170   //return  track ID of digits - for buffer compresion 2
171   Int_t i,n1,n2;
172   i = level*fNcols+column;
173   if ( (i+1)>=fTrIndex->fN) n2 = fTracks->fN;
174   else 
175     n2 = fTrIndex->At(i+1);
176   n1 = fTrIndex->At(i);
177   Int_t rownew = 0;
178   Int_t rowold=0;
179   Int_t id;
180   for (i = n1;(i<n2);i++){
181     id = 0;
182     Int_t num = fTracks->At(i);
183     if (num<0) {
184       rownew-=num;  
185       rowold = rownew;
186       i++;
187       if (i<n2){
188         num = fTracks->At(i);
189         rownew+=num;
190         i++;
191         id = fTracks->At(i);
192       }
193     }
194     else {
195       rowold = rownew;
196       rownew+=num;
197       i++;
198       id = fTracks->At(i);
199     }
200     id-=2;
201     if ( (row>=rowold) && (row<rownew) ) return id;
202     if (row < rownew ) return -2; //empty track
203   }
204   return -2;
205 }
206
207 void  AliSimDigits::ExpandTrackBuffer1()
208 {
209   //
210   //expand  track compressed according algorithm 1 (track id comression independent to the digit compression)
211   // !!in expanded tracks we don't use fTrIndex array
212   //  
213   fTrBufType = 0;
214   Int_t i,j;
215   Int_t all   = fNrows*fNcols;  //total number of digits
216   Int_t elems = all*fNlevel;  //length of the buffer
217
218   TArrayI * buf = new TArrayI;
219   buf->Set(elems);
220   fTrIndex->Set(0);
221   //
222   Int_t level = 0;
223   Int_t col=0;
224   Int_t row = 0;
225   Int_t n=fTracks->fN;
226   //
227   for (i=0;i<n;i++){
228     //oposite signa means how many unwrited (under threshold) values
229     Int_t num = fTracks->At(i);
230     if (num<0) row-=num;   //negative number mean number of zeroes (no tracks of gibven level no need to write to array) 
231     else {
232       num %= 10000000; //PH: take into account the case of underlying events
233       i++;
234       Int_t id =  fTracks->At(i);
235       for (j = 0; j<num; j++,row++) {
236         if (level*all+col*fNrows+row<elems) (*buf)[level*all+col*fNrows+row]=id;       
237       }
238     }
239     if (row>=fNrows) {
240       row=0;
241       col++;
242     }
243     if (col>=fNcols) {
244       col=0;
245       level++;
246     }    
247   }//end of loop over digits
248   delete fTracks;
249   fTracks = buf;
250 }
251
252 void  AliSimDigits::CompresTrackBuffer1()
253 {
254   //
255   //comress track according algorithm 1 (track id comression independent to the digit compression)
256   //
257   fTrBufType = 1;  
258
259   TArrayI *  buf = new TArrayI;   //create  new buffer 
260   buf->Set(fNrows*fNcols*fNlevel); //lets have the nearly the "worst case"
261   TArrayI *  index = new TArrayI;
262   index->Set(fNcols*fNlevel);
263   //  Int_t * pindex = 
264
265   
266   Int_t icurrent=-1;  //current index
267   Int_t izero;      //number of zero
268   Int_t inum;      //number of digits  with the same current track id  
269   Int_t lastID =0;  //last track id  
270   
271   Int_t *cbuff=fTracks->GetArray(); //MI change
272
273   for (Int_t lev =0; lev<fNlevel; lev++){    //loop over levels 
274     for (Int_t col = 0; col<fNcols; col++){    //loop over columns
275       izero = 0;
276       inum =  0;
277       lastID = 0;
278       (*index)[lev*fNcols+col]=icurrent+1;//set collumn pointer
279       Int_t id=0;  //current id
280       for (Int_t row = 0; row< fNrows;row++){ //loop over rows        
281         id = *cbuff;  //MI change
282         //      id = GetTrackIDFast(row,col,lev);
283         if (id <= 0) {
284           if ( inum> 0 ) { //if we have some tracks in buffer
285             icurrent++;
286             if ((icurrent+1)>=buf->fN) buf->Set(icurrent*2+1); //MI change - allocate +1
287             (*buf)[icurrent] = inum;
288             icurrent++;
289             (*buf)[icurrent] = lastID;  
290             inum = 0;      
291             lastID = 0;
292           }
293           izero++;
294         }
295         else
296           if (id != lastID) 
297             if ( izero > 0 ) { 
298               //if we have currently izero count of non tracks digits
299               icurrent++;         
300               if (icurrent>=buf->fN) buf->Set(icurrent*2+1);
301               (*buf)[icurrent]= -izero;  //write how many under zero
302               inum++;
303               izero = 0;             
304               lastID = id;
305             }
306             else{ 
307               //if we change track id from another track id         
308               icurrent++;         
309               if ((icurrent+1)>=buf->fN) buf->Set(icurrent*2+1);
310               (*buf)[icurrent] = inum;
311               icurrent++;
312               (*buf)[icurrent] = lastID;        
313               lastID = id;
314               inum = 1;      
315               izero = 0;
316             }   
317           else {          
318             inum++;
319           }
320         cbuff++;  //MI change
321       }//end of loop over row
322       if ( izero > 0 ) { 
323         //if we have currently izero count of non tracks digits
324         icurrent++;       
325         if (icurrent>=buf->fN) buf->Set(icurrent*2);
326         (*buf)[icurrent]= -izero;  //write how many under zero  
327       }
328       if ( inum> 0 ) { //if we have some tracks in buffer
329         icurrent++;
330         if ((icurrent+1)>=buf->fN) buf->Set(icurrent*2);
331         (*buf)[icurrent] = inum;
332         icurrent++;
333         (*buf)[icurrent] = id;  
334       }      
335     }//end of loop over columns
336   }//end of loop over differnet track level  
337   buf->Set(icurrent+1);
338   delete fTracks;
339   fTracks = buf;
340   delete fTrIndex;
341   fTrIndex = index;
342 }
343
344
345
346 void  AliSimDigits::ExpandTrackBuffer2()
347 {
348   //
349   //comress track according algorithm 2 (track id comression according  digit compression)
350   fTrBufType = 0;
351 }
352
353 void  AliSimDigits::CompresTrackBuffer2()
354 {
355   //
356   //comress track according algorithm 2 (track id comression according  digit compression)
357   fTrBufType = 2;
358 }
359
360
361 Int_t  AliSimDigits::GetTrackID2(Int_t /*row*/, Int_t /*column*/, Int_t /*level*/)
362 {
363   //returnb track id of digits - for buffer compresion 2
364   return -2;
365 }
366
367
368
369 AliH2F *  AliSimDigits::DrawTracks( const char *option,Int_t level, 
370                               Float_t x1, Float_t x2, Float_t y1, Float_t y2)
371 {
372   //
373   //draw digits in given array
374   //  
375   //make digits histo 
376   char ch[30];
377   //sprintf(ch,"Track Segment_%d level %d ",GetID(),level );
378   snprintf(ch,30,"Track Segment_%d level %d ",GetID(),level );
379   if ( (fNrows<1)|| (fNcols<1)) {
380     return 0;
381   }
382   AliH2F * his  = new AliH2F("Track histo",ch,fNrows,0,fNrows,fNcols,0,fNcols);
383   ExpandTrackBuffer();
384   //set histogram  values
385   for (Int_t i = 0; i<fNrows;i++)    
386     for (Int_t j = 0; j<fNcols;j++)
387         his->Fill(i,j,GetTrackIDFast(i,j,level));
388   if (x1>=0) {
389       AliH2F *h2fsub = his->GetSubrange2d(x1,x2,y1,y2);
390       delete his;
391       his=h2fsub;
392   }
393   if (his==0) return 0;
394   if (option!=0) his->Draw(option);
395   else his->Draw();
396   return his;  
397 }
398
399 TClonesArray *  AliSimDigits::GenerTPCClonesArray(TClonesArray * arr)
400 {
401   //
402   //generate TClonnesArray of digits
403   //
404   TClonesArray * digits;
405   if (arr==0)  digits=new TClonesArray("AliTPCdigit",300);
406   else digits = arr; 
407   Int_t index = digits->GetEntriesFast();
408   for (Int_t row =0; row<fNrows; row++)
409     for (Int_t col =0; col<fNcols; col++){
410       Int_t amp = GetDigit(row,col);
411       if (amp>GetThreshold()){
412         AliTPCdigit dig;
413         dig.fPad = col;
414         dig.fTime = row;
415         dig.fSignal= amp;
416         dig.fPadRow =fSegmentID;
417         dig.fSector =fSegmentID;
418         dig.GetTracks()[0]= GetTrackID(row,col,0);
419         dig.GetTracks()[1]= GetTrackID(row,col,1);
420         dig.GetTracks()[2]= GetTrackID(row,col,2);
421         TClonesArray &ldigits = *digits;
422         new(ldigits[index++]) AliTPCdigit(dig);
423       }
424     }    
425   return digits;
426 }
427 void AliSimDigits::GlitchFilter(){
428   //
429   //  glitch filter, optionally
430   //
431   
432   for (Int_t i=0;i<fNcols;i++){ //pads
433     for(Int_t j=1;j<fNrows-1;j++){ //time bins
434       // first and last time bins are checked separately
435       if(GetDigitFast(j,i)){// nonzero digit
436         if (!GetDigitFast(j-1,i) && !GetDigitFast(j+1,i)) {
437           SetDigitFast(0,j,i);
438           SetTrackIDFast(-2,j,i,0);
439           SetTrackIDFast(-2,j,i,1);
440           SetTrackIDFast(-2,j,i,2);
441         }
442       }
443     }//time
444    
445     if(GetDigitFast(0,i) && !GetDigitFast(1,i)) {
446         SetDigitFast(0,0,i);
447         SetTrackIDFast(-2,0,i,0);
448         SetTrackIDFast(-2,0,i,1);
449         SetTrackIDFast(-2,0,i,2);
450     }
451     if(GetDigitFast(fNrows-1,i) && !GetDigitFast(fNrows-2,i)){ 
452        SetDigitFast(0,fNrows-1,i);
453        SetTrackIDFast(-2,fNrows-1,i,0);
454        SetTrackIDFast(-2,fNrows-1,i,1);
455        SetTrackIDFast(-2,fNrows-1,i,2);    
456     }
457   }//pads
458  
459 }
460