]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TPC/AliSimDigits.cxx
Now the full chain includes raw data.
[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 {
48   //  AliDigits::Invalite();
49   fTracks = 0;
50   fTrIndex = 0;  
51   InvalidateTrack();
52 }
53 AliSimDigits::~AliSimDigits()
54 {
55
56   if (fTracks != 0) {
57     delete fTracks;
58   }
59   if (fTrIndex != 0) { 
60     delete fTrIndex;
61   } 
62
63 }
64
65 void AliSimDigits::InvalidateTrack() 
66
67   //
68   //set default (invalid parameters)
69   if ( fTracks != 0) delete fTracks;
70   fTracks = new TArrayI;
71   if ( fTrIndex  != 0) delete fTrIndex;
72   fTrIndex = new TArrayI;
73
74   for (Int_t i = 0; i<3; i++){
75     fTracks->Set(0);
76     fTrIndex->Set(0);
77   }
78 }
79
80 void  AliSimDigits::AllocateTrack(Int_t length)
81 {
82   //
83   //construct empty buffer fElements and fTracks with size fNrows x fNcols x
84   //length 
85   InvalidateTrack();
86   fNlevel = length;
87   fTracks->Set(fNcols*fNrows*fNlevel);
88   fTrIndex->Set(0); 
89   fTrBufType =0;
90 }
91
92 Int_t AliSimDigits::GetTrackID(Int_t row, Int_t column, Int_t level) 
93 {
94   //
95   //Get track ID 
96   if (fTrBufType == 0) return  GetTrackIDFast(row, column,level);
97   if (fTrBufType == 1) return  GetTrackID1(row, column,level); 
98   if (fTrBufType == 2) return  GetTrackID2(row, column,level); 
99   return 0;
100 }
101
102 void AliSimDigits::ExpandTrackBuffer()
103 {  
104   //
105   //expand buffer to two dimensional array 
106   if (fTrBufType<0)  {
107     Error("ExpandBuffer", "buffer doesn't exist");
108     return;
109   }
110   if (fTrBufType==0)      return;  //buffer expanded
111   if (fTrBufType==1)  {ExpandTrackBuffer1(); return;}
112   if (fTrBufType==2)  ExpandTrackBuffer2();
113  
114 }
115
116 void AliSimDigits::CompresTrackBuffer(Int_t bufType)
117 {
118   //
119   //compres buffer according buffertype algorithm
120   //
121   if (fTrBufType<0)  {
122     Error("CompressBuffer", "buffer doesn't exist");
123     return;
124   }
125   if (fTrBufType == bufType) return;
126   //
127   if (fTrBufType>0) ExpandTrackBuffer();
128   if (fTrBufType !=0)  {
129     Error("CompressBuffer", "buffer doesn't exist");
130     return;
131   }
132   //compress buffer of type 1
133   
134   if (bufType==1)      {CompresTrackBuffer1();return;}
135   if (bufType==2)      CompresTrackBuffer2();
136    
137 }
138
139 Int_t  AliSimDigits::GetTrackID1(Int_t row, Int_t column, Int_t level)
140 {
141   //return  track ID of digits - for buffer compresion 2
142   Int_t i,n1,n2;
143   i = level*fNcols+column;
144   if ( (i+1)>=fTrIndex->fN) n2 = fTracks->fN;
145   else 
146     n2 = fTrIndex->At(i+1);
147   n1 = fTrIndex->At(i);
148   Int_t rownew = 0;
149   Int_t rowold=0;
150   Int_t id;
151   for (i = n1;(i<n2);i++){
152     id = 0;
153     Int_t num = fTracks->At(i);
154     if (num<0) {
155       rownew-=num;  
156       rowold = rownew;
157       i++;
158       if (i<n2){
159         num = fTracks->At(i);
160         rownew+=num;
161         i++;
162         id = fTracks->At(i);
163       }
164     }
165     else {
166       rowold = rownew;
167       rownew+=num;
168       i++;
169       id = fTracks->At(i);
170     }
171     id-=2;
172     if ( (row>=rowold) && (row<rownew) ) return id;
173     if (row < rownew ) return -2; //empty track
174   }
175   return -2;
176 }
177
178 void  AliSimDigits::ExpandTrackBuffer1()
179 {
180   //
181   //expand  track compressed according algorithm 1 (track id comression independent to the digit compression)
182   // !!in expanded tracks we don't use fTrIndex array
183   //  
184   fTrBufType = 0;
185   Int_t i,j;
186   Int_t all   = fNrows*fNcols;  //total number of digits
187   Int_t elems = all*fNlevel;  //length of the buffer
188
189   TArrayI * buf = new TArrayI;
190   buf->Set(elems);
191   fTrIndex->Set(0);
192   //
193   Int_t level = 0;
194   Int_t col=0;
195   Int_t row = 0;
196   Int_t n=fTracks->fN;
197   //
198   for (i=0;i<n;i++){
199     //oposite signa means how many unwrited (under threshold) values
200     Int_t num = fTracks->At(i);
201     if (num<0) row-=num;   //negative number mean number of zeroes (no tracks of gibven level no need to write to array) 
202     else {
203       num %= 10000000; //PH: take into account the case of underlying events
204       i++;
205       Int_t id =  fTracks->At(i);
206       for (j = 0; j<num; j++,row++) {
207         if (level*all+col*fNrows+row<elems) (*buf)[level*all+col*fNrows+row]=id;       
208       }
209     }
210     if (row>=fNrows) {
211       row=0;
212       col++;
213     }
214     if (col>=fNcols) {
215       col=0;
216       level++;
217     }    
218   }//end of loop over digits
219   delete fTracks;
220   fTracks = buf;
221 }
222
223 void  AliSimDigits::CompresTrackBuffer1()
224 {
225   //
226   //comress track according algorithm 1 (track id comression independent to the digit compression)
227   //
228   fTrBufType = 1;  
229
230   TArrayI *  buf = new TArrayI;   //create  new buffer 
231   buf->Set(fNrows*fNcols*fNlevel); //lets have the nearly the "worst case"
232   TArrayI *  index = new TArrayI;
233   index->Set(fNcols*fNlevel);
234   //  Int_t * pindex = 
235
236   
237   Int_t icurrent=-1;  //current index
238   Int_t izero;      //number of zero
239   Int_t inum;      //number of digits  with the same current track id  
240   Int_t lastID =0;  //last track id  
241   
242   Int_t *cbuff=fTracks->GetArray(); //MI change
243
244   for (Int_t lev =0; lev<fNlevel; lev++){    //loop over levels 
245     for (Int_t col = 0; col<fNcols; col++){    //loop over columns
246       izero = 0;
247       inum =  0;
248       lastID = 0;
249       (*index)[lev*fNcols+col]=icurrent+1;//set collumn pointer
250       Int_t id=0;  //current id
251       for (Int_t row = 0; row< fNrows;row++){ //loop over rows        
252         id = *cbuff;  //MI change
253         //      id = GetTrackIDFast(row,col,lev);
254         if (id <= 0) {
255           if ( inum> 0 ) { //if we have some tracks in buffer
256             icurrent++;
257             if ((icurrent+1)>=buf->fN) buf->Set(icurrent*2+1); //MI change - allocate +1
258             (*buf)[icurrent] = inum;
259             icurrent++;
260             (*buf)[icurrent] = lastID;  
261             inum = 0;      
262             lastID = 0;
263           }
264           izero++;
265         }
266         else
267           if (id != lastID) 
268             if ( izero > 0 ) { 
269               //if we have currently izero count of non tracks digits
270               icurrent++;         
271               if (icurrent>=buf->fN) buf->Set(icurrent*2+1);
272               (*buf)[icurrent]= -izero;  //write how many under zero
273               inum++;
274               izero = 0;             
275               lastID = id;
276             }
277             else{ 
278               //if we change track id from another track id         
279               icurrent++;         
280               if ((icurrent+1)>=buf->fN) buf->Set(icurrent*2+1);
281               (*buf)[icurrent] = inum;
282               icurrent++;
283               (*buf)[icurrent] = lastID;        
284               lastID = id;
285               inum = 1;      
286               izero = 0;
287             }   
288           else {          
289             inum++;
290           }
291         cbuff++;  //MI change
292       }//end of loop over row
293       if ( izero > 0 ) { 
294         //if we have currently izero count of non tracks digits
295         icurrent++;       
296         if (icurrent>=buf->fN) buf->Set(icurrent*2);
297         (*buf)[icurrent]= -izero;  //write how many under zero  
298       }
299       if ( inum> 0 ) { //if we have some tracks in buffer
300         icurrent++;
301         if ((icurrent+1)>=buf->fN) buf->Set(icurrent*2);
302         (*buf)[icurrent] = inum;
303         icurrent++;
304         (*buf)[icurrent] = id;  
305       }      
306     }//end of loop over columns
307   }//end of loop over differnet track level  
308   buf->Set(icurrent+1);
309   delete fTracks;
310   fTracks = buf;
311   delete fTrIndex;
312   fTrIndex = index;
313 }
314
315
316
317 void  AliSimDigits::ExpandTrackBuffer2()
318 {
319   //
320   //comress track according algorithm 2 (track id comression according  digit compression)
321   fTrBufType = 0;
322 }
323
324 void  AliSimDigits::CompresTrackBuffer2()
325 {
326   //
327   //comress track according algorithm 2 (track id comression according  digit compression)
328   fTrBufType = 2;
329 }
330
331
332 Int_t  AliSimDigits::GetTrackID2(Int_t /*row*/, Int_t /*column*/, Int_t /*level*/)
333 {
334   //returnb track id of digits - for buffer compresion 2
335   return -2;
336 }
337
338
339
340 AliH2F *  AliSimDigits::DrawTracks( const char *option,Int_t level, 
341                               Float_t x1, Float_t x2, Float_t y1, Float_t y2)
342 {
343   //
344   //draw digits in given array
345   //  
346   //make digits histo 
347   char ch[30];
348   sprintf(ch,"Track Segment_%d level %d ",GetID(),level );
349   if ( (fNrows<1)|| (fNcols<1)) {
350     return 0;
351   }
352   AliH2F * his  = new AliH2F("Track histo",ch,fNrows,0,fNrows,fNcols,0,fNcols);
353   ExpandTrackBuffer();
354   //set histogram  values
355   for (Int_t i = 0; i<fNrows;i++)    
356     for (Int_t j = 0; j<fNcols;j++)
357         his->Fill(i,j,GetTrackIDFast(i,j,level));
358   if (x1>=0) {
359       AliH2F *h2fsub = his->GetSubrange2d(x1,x2,y1,y2);
360       delete his;
361       his=h2fsub;
362   }
363   if (his==0) return 0;
364   if (option!=0) his->Draw(option);
365   else his->Draw();
366   return his;  
367 }
368
369 TClonesArray *  AliSimDigits::GenerTPCClonesArray(TClonesArray * arr)
370 {
371   //
372   //generate TClonnesArray of digits
373   //
374   TClonesArray * digits;
375   if (arr==0)  digits=new TClonesArray("AliTPCdigit",300);
376   else digits = arr; 
377   Int_t index = digits->GetEntriesFast();
378   for (Int_t row =0; row<fNrows; row++)
379     for (Int_t col =0; col<fNcols; col++){
380       Int_t amp = GetDigit(row,col);
381       if (amp>GetThreshold()){
382         AliTPCdigit dig;
383         dig.fPad = col;
384         dig.fTime = row;
385         dig.fSignal= amp;
386         dig.fPadRow =fSegmentID;
387         dig.fSector =fSegmentID;
388         dig.GetTracks()[0]= GetTrackID(row,col,0);
389         dig.GetTracks()[1]= GetTrackID(row,col,1);
390         dig.GetTracks()[2]= GetTrackID(row,col,2);
391         TClonesArray &ldigits = *digits;
392         new(ldigits[index++]) AliTPCdigit(dig);
393       }
394     }    
395   return digits;
396 }
397