Changes to compile with Root6
[u/mrichter/AliRoot.git] / ITS / UPGRADE / AliITSUSuze02.cxx
1 #include <Riostream.h>
2 #include "TMath.h"
3 #include "AliITSUSuze02.h"
4
5 //*******************************************************************
6 //
7 //  Simulation of the SUZE02 readout
8 //  Origin: Serhiy.Senuykov@cern.ch
9 //  
10 //*******************************************************************
11
12 using std::cout;
13 using std::endl;
14
15 ClassImp(AliITSUSuze02)
16
17 AliITSUSuze02::AliITSUSuze02(Int_t Nrows, Int_t Ncols):
18   fNRowsChip(Nrows),
19   fNColsChip(Ncols),
20   fChip(new TMatrixF(Nrows,Ncols)), 
21   fTestColumnSize(2),
22   fTestRowSize(2),
23   fNWindowsPer32colsMax(0),
24   fNWindowsPerHalfFSBBMax(0),
25   fNWindowsPerFSBBMax(0),
26   fNDigitsEncoded(0), 
27   fNEncodedWindows(0),
28   fNDigitsLost(0),
29   fNLostWindows(0),
30   fDataSizePerChip(0),
31   fNWindowsPer32colsMin(0),
32   fNWindowsPerHalfFSBBMin(0),
33   fNWindowsPerFSBBMin(0)
34 {
35   if (Ncols%(kNumberOfFSBB*kNumberOfHalfFSBB) != 0) {
36     printf("Number of columns should be multiple of %d. SUZE matrix wasn't created\n",kNumberOfFSBB*kNumberOfHalfFSBB);
37   }
38 }  
39   
40 AliITSUSuze02::AliITSUSuze02(const AliITSUSuze02& suze): 
41   fNRowsChip(suze.fNRowsChip),
42   fNColsChip(suze.fNColsChip),
43   fChip(new TMatrixF(*suze.fChip)), 
44   fTestColumnSize(suze.fTestColumnSize),
45   fTestRowSize(suze.fTestRowSize),
46   fNWindowsPer32colsMax(suze.fNWindowsPer32colsMax),
47   fNWindowsPerHalfFSBBMax(suze.fNWindowsPerHalfFSBBMax),
48   fNWindowsPerFSBBMax(suze.fNWindowsPerFSBBMax),
49   fNDigitsEncoded(suze.fNDigitsEncoded), 
50   fNEncodedWindows(suze.fNEncodedWindows),
51   fNDigitsLost(suze.fNDigitsLost),
52   fNLostWindows(suze.fNLostWindows),
53   fDataSizePerChip(suze.fDataSizePerChip),
54   fNWindowsPer32colsMin(suze.fNWindowsPer32colsMin),
55   fNWindowsPerHalfFSBBMin(suze.fNWindowsPerHalfFSBBMin),
56   fNWindowsPerFSBBMin(suze.fNWindowsPerFSBBMin)
57 {
58 }
59
60 AliITSUSuze02 &AliITSUSuze02::operator=(const AliITSUSuze02& suze) {
61   if (&suze == this) return *this;
62
63   fNRowsChip = suze.fNRowsChip;
64   fNColsChip = suze.fNColsChip;
65   fChip = new TMatrixF(*suze.fChip);  
66   fTestColumnSize = suze.fTestColumnSize;
67   fTestRowSize = suze.fTestRowSize;
68   fNWindowsPer32colsMax = suze.fNWindowsPer32colsMax;
69   fNWindowsPerHalfFSBBMax = suze.fNWindowsPerHalfFSBBMax;
70   fNWindowsPerFSBBMax = suze.fNWindowsPerFSBBMax;
71   fNDigitsEncoded = suze.fNDigitsEncoded;
72   fNEncodedWindows = suze.fNEncodedWindows;
73   fNDigitsLost = suze.fNDigitsLost;
74   fNLostWindows = suze.fNLostWindows;
75   fDataSizePerChip = suze.fDataSizePerChip;
76   fNWindowsPer32colsMin = suze.fNWindowsPer32colsMin;
77   fNWindowsPerHalfFSBBMin = suze.fNWindowsPerHalfFSBBMin;
78   fNWindowsPerFSBBMin = suze.fNWindowsPerFSBBMin;
79
80   return *this;
81 }
82
83 AliITSUSuze02::~AliITSUSuze02() {
84   if(fChip) delete fChip;
85 }
86
87 void AliITSUSuze02::SetEncodingWindowSize(Int_t Wrows, Int_t Wcols){
88   fTestColumnSize=Wrows;
89   fTestRowSize=Wcols;
90 }
91
92 void AliITSUSuze02::SetQuotas(Int_t q32, Int_t qHalfFSBB, Int_t qFSBB){
93   fNWindowsPer32colsMax=q32;
94   fNWindowsPerHalfFSBBMax=qHalfFSBB;
95   fNWindowsPerFSBBMax=qFSBB;
96 }
97
98 void AliITSUSuze02::AddDigit(Int_t row, Int_t col){
99   (*fChip)(row,col)++;
100 }
101
102 //void AliITSUSuze02::Process(Bool_t Verbose){  
103 void AliITSUSuze02::Process(TH1F* OverflowCodes, TH1F* NDigitsPerEncodingWindowDist, Bool_t Verbose) {
104
105   //cout<<"Processing"<<endl;
106   //fChip->Print(); 
107   
108   Int_t NRowsFSBB=fNRowsChip;
109   Int_t NColsFSBB=fNColsChip/kNumberOfFSBB;
110   TMatrixF FSBB(NRowsFSBB,NColsFSBB);
111   
112   Int_t NRowsSuperLine=fTestColumnSize;
113   Int_t NColsSuperLine=NColsFSBB;   
114   TMatrixF SuperLineUp(NRowsSuperLine,NColsSuperLine);
115   TMatrixF SuperLineDown(NRowsSuperLine,NColsSuperLine);
116   
117   Int_t NRowsSuperLineX2=NRowsSuperLine*2; 
118   Int_t NColsSuperLineX2=NColsSuperLine; //SuperLineX2 and SuperLine size in columns is equal to FSBB.
119   TMatrixF SuperLineX2(NRowsSuperLineX2,NColsSuperLineX2); 
120   
121   TMatrixF TestRow(1,fTestRowSize);
122   TMatrixF TestColumn(fTestColumnSize,1);      
123   Int_t TestRowSum=0;
124   TMatrixF EncodingWindow(fTestColumnSize,fTestRowSize);
125   
126   Int_t EncodingWindowStartRow=0;
127   Int_t EncodingWindowStopRow=0;
128   Int_t EncodingWindowStartCol=0;
129   Int_t EncodingWindowStopCol=0;
130   
131   Int_t nMasks=fTestRowSize-1;
132   Int_t MaskSize=NRowsSuperLineX2-1;
133   TMatrixF Masks(MaskSize,nMasks);
134
135    //parameters for internal data size calculation
136   Int_t DataSizePerWindowInRow=8+fTestColumnSize*fTestRowSize+TMath::Ceil(TMath::Log2(fTestColumnSize));
137   Int_t DataSizePerSuperLineX2Header=30;   
138   
139   Int_t DataSizePerSuperLineX2=0;
140   
141   Bool_t Overflow32=kFALSE;
142   Bool_t OverflowHalfFSBB=kFALSE;
143   Bool_t OverflowFSBB=kFALSE;
144   
145   Int_t nWindowsPer32cols=fNWindowsPer32colsMax;
146   Int_t nWindowsPerHalfFSBB=fNWindowsPerHalfFSBBMax;
147   Int_t nWindowsPerFSBB=fNWindowsPerFSBBMax; 
148   
149   fNWindowsPer32colsMin=fNWindowsPer32colsMax;
150   fNWindowsPerHalfFSBBMin=fNWindowsPerHalfFSBBMax;
151   fNWindowsPerFSBBMin=fNWindowsPerFSBBMax;  
152   
153   fNEncodedWindows=0;
154   fNDigitsEncoded=0;
155   fNLostWindows=0;
156   fNDigitsLost=0;
157   fDataSizePerChip=0;    
158   
159   for(Int_t FSBBindex=0; FSBBindex<kNumberOfFSBB; FSBBindex++){
160     FSBB=fChip->GetSub(0,NRowsFSBB-1,FSBBindex*NColsFSBB,(FSBBindex+1)*NColsFSBB-1);
161     SuperLineDown=FSBB.GetSub(0,NRowsSuperLine-1,0,NColsSuperLine-1);
162     for(Int_t SuperLineX2StartRow=0; SuperLineX2StartRow<NRowsFSBB; SuperLineX2StartRow+=NRowsSuperLine){
163       if(nWindowsPerFSBB<fNWindowsPerFSBBMin) {fNWindowsPerFSBBMin=nWindowsPerFSBB;} //saving the lowest number of remaining windows
164       nWindowsPerFSBB=fNWindowsPerFSBBMax; //Reset number of available encoding windows for the new double SuperLine
165       OverflowFSBB=kFALSE;
166       SuperLineUp=SuperLineDown;
167       if(SuperLineX2StartRow+NRowsSuperLineX2<=NRowsFSBB){
168         SuperLineDown=FSBB.GetSub(SuperLineX2StartRow+NRowsSuperLine,SuperLineX2StartRow+NRowsSuperLineX2-1,0,NColsSuperLine-1);
169       }
170       else if(SuperLineX2StartRow+NRowsSuperLine<NRowsFSBB){
171         SuperLineDown.Zero();
172         SuperLineDown.SetSub(0,0,FSBB.GetSub(SuperLineX2StartRow+NRowsSuperLine,NRowsFSBB-1,0,NColsSuperLine-1));
173       } 
174       else{
175         SuperLineDown.Zero();
176       }
177       if(SuperLineUp.Sum()>0){
178         DataSizePerSuperLineX2=0; 
179         SuperLineX2.SetSub(0,0,SuperLineUp);
180         SuperLineX2.SetSub(NRowsSuperLine,0,SuperLineDown);
181         for(Int_t HalfFSBBindex=0; HalfFSBBindex<kNumberOfHalfFSBB; HalfFSBBindex++){
182           if(nWindowsPerHalfFSBB<fNWindowsPerHalfFSBBMin) {fNWindowsPerHalfFSBBMin=nWindowsPerHalfFSBB;}  
183           nWindowsPerHalfFSBB=fNWindowsPerHalfFSBBMax; //reset counter per HalfFSBB
184           OverflowHalfFSBB=kFALSE;
185           for(Int_t i=0; i<MaskSize; i++){ //reset masks to 1111111
186             for(Int_t j=0; j<nMasks; j++){
187               Masks(i,j)=1;
188             }
189           }
190           
191           for(Int_t TestRowStartCol=HalfFSBBindex*NColsSuperLineX2/kNumberOfHalfFSBB; TestRowStartCol<(HalfFSBBindex+1)*NColsSuperLineX2/kNumberOfHalfFSBB; TestRowStartCol++){
192             if(TestRowStartCol%32==0){
193                     if(nWindowsPer32cols<fNWindowsPer32colsMin) fNWindowsPer32colsMin=nWindowsPer32cols;
194               nWindowsPer32cols=fNWindowsPer32colsMax; //reset nWindowsPer32cols counter every 32 columns
195               Overflow32=kFALSE;
196             }
197             //apply masks
198             for(Int_t RowIndex=0; RowIndex<MaskSize; RowIndex++){
199               for(Int_t MaskIndex=0; MaskIndex<nMasks; MaskIndex++){
200                 if(Masks(RowIndex,MaskIndex)==0){
201                   // cout<<"Mask has zero bit at pos:"<<RowIndex<<":"<<MaskIndex<<endl;
202                   // cout<<"will clean the pixels at row:"<<RowIndex<<" from pos.:"<<TestRowStartCol<<" till "<< TestRowStartCol+(nMasks-MaskIndex)<<endl;
203                   for(Int_t ColIndex=TestRowStartCol; ColIndex<TestRowStartCol+(nMasks-MaskIndex); ColIndex++){
204                     if(ColIndex<(HalfFSBBindex+1)*NColsSuperLineX2/kNumberOfHalfFSBB){
205                       SuperLineX2(RowIndex,ColIndex)=0;
206                       // cout<<"Mask has zero bit. Cleaning at pos:"<<RowIndex<<":"<<ColIndex<<endl;
207                       if(RowIndex>=fTestColumnSize){
208                         SuperLineDown(RowIndex-fTestColumnSize,ColIndex)=0;
209                       }
210                     }
211                   }
212                   break;
213                 }
214               }
215             }
216             //shift masks
217             for(Int_t RowIndex=0; RowIndex<MaskSize; RowIndex++){
218               for(Int_t MaskIndex=nMasks-1; MaskIndex>0; MaskIndex--){
219                 Masks(RowIndex,MaskIndex)=Masks(RowIndex,MaskIndex-1);
220               }
221               Masks(RowIndex,0)=1;
222             }  
223             
224             for(Int_t TestRowStartRow=0; TestRowStartRow<fTestColumnSize; TestRowStartRow++){
225               TestRowSum=0;
226               for(Int_t TestRowIndex=0; TestRowIndex<fTestRowSize; TestRowIndex++){
227                 if(TestRowStartCol+TestRowIndex<(HalfFSBBindex+1)*NColsSuperLineX2/kNumberOfHalfFSBB){
228                   TestRowSum+=SuperLineX2(TestRowStartRow,TestRowStartCol+TestRowIndex);
229                 }
230                 if(TestRowSum>0){
231                   //cout<<"TestR at col n."<<TestRowStartCol<<" and row n."<<TestRowStartRow<<" has a hit"<<endl;
232                   break;
233                 }
234               }
235               if(TestRowSum>0){
236                 TestColumn=SuperLineX2.GetSub(TestRowStartRow,TestRowStartRow+fTestColumnSize-1,TestRowStartCol,TestRowStartCol);   
237                 if(TestColumn.Sum()>0){
238                   EncodingWindowStartRow=TestRowStartRow;
239                   EncodingWindowStopRow=EncodingWindowStartRow+fTestColumnSize-1;
240                   EncodingWindowStartCol=TestRowStartCol; 
241                   
242                   if(TestRowStartCol+fTestRowSize>(HalfFSBBindex+1)*NColsSuperLineX2/kNumberOfHalfFSBB){
243                     EncodingWindowStopCol=((HalfFSBBindex+1)*NColsSuperLineX2/kNumberOfHalfFSBB)-1;
244                   }
245                   else{
246                     EncodingWindowStopCol=EncodingWindowStartCol+fTestRowSize-1;
247                   }
248
249                   EncodingWindow.Zero();
250                   EncodingWindow.SetSub(0,0,SuperLineX2.GetSub(EncodingWindowStartRow,EncodingWindowStopRow,EncodingWindowStartCol,EncodingWindowStopCol));
251                   
252                   if(nWindowsPer32cols && nWindowsPerHalfFSBB && nWindowsPerFSBB){
253                     //cout<<"Will encode window starting at "<<TestRowStartRow<<":"<<TestRowStartCol<<endl;
254                     fNDigitsEncoded+=EncodingWindow.Sum();
255                     fNEncodedWindows++;
256                     OverflowCodes->Fill(0);
257                                 NDigitsPerEncodingWindowDist->Fill(EncodingWindow.Sum());
258                     nWindowsPerFSBB--;
259                     nWindowsPerHalfFSBB--;
260                     nWindowsPer32cols--;
261                     DataSizePerSuperLineX2+=DataSizePerWindowInRow;
262                     //cout<<"Windows left:"<<nWindowsPerFSBB<<":"<<nWindowsPerHalfFSBB<<":"<<nWindowsPer32cols<<endl;
263                   }
264                   else{
265                     fNDigitsLost+=EncodingWindow.Sum();
266                     fNLostWindows++;
267                     //cout<<"------ No encoding at col.:"<<TestRowStartCol<<" at SuperLineX2 that starts at row:"<<SuperLineX2StartRow<<endl;
268                     if(!nWindowsPer32cols) Overflow32=kTRUE;
269                     if(!nWindowsPerHalfFSBB) OverflowHalfFSBB=kTRUE;
270                     if(!nWindowsPerFSBB) OverflowFSBB=kTRUE;
271                     OverflowCodes->Fill(Overflow32+2*OverflowHalfFSBB+4*OverflowFSBB);
272                   } 
273                   for(Int_t k=TestRowStartRow;k<TestRowStartRow+fTestColumnSize; k++){
274                     Masks(k,0)=0;
275                     //cout<<"Setting Mask to 0 at "<<k<<endl;
276                     //Setting to 0 the first column of the encoding window. This part if probably present in the real SUZE.
277                     SuperLineX2(k,TestRowStartCol)=0;
278                     if(k>=fTestColumnSize){
279                       SuperLineDown(k-fTestColumnSize,TestRowStartCol)=0;
280                     }
281                   }
282                 }  
283                 break;
284               }
285             }
286           }
287         }
288         fDataSizePerChip+=(DataSizePerSuperLineX2+DataSizePerSuperLineX2Header);
289       }
290     }
291   }
292   if(Verbose){
293     cout<<fNDigitsEncoded<<" digits encoded in "<<fNEncodedWindows<<" windows"<<endl;
294     cout<<fNDigitsLost<<" digits lost in "<<fNLostWindows<<" windows"<<endl;
295   }
296 }
297
298 void AliITSUSuze02::GetResults(){
299   
300 }
301 /*
302 void AliITSUSuze02::InitHistos(){
303   fOverflowCodes = new TH1F("OverflowCodes","Overflow codes",8,0,8);
304   if(fNRowsChip*fNColsChip){
305     fNDigitsPerEncodingWindowDist = new TH1F("nDigitsPerEncodingWindowPerChip","nDigitsPerEncodingWindowPerChip",fTestColumnSize*fTestRowSize,1,fTestColumnSize*fTestRowSize+1);
306   }
307   else{
308     printf("Run AliITSUSuze02::SetEncodingWindowSize first\n");
309   }
310 }
311 */
312 void AliITSUSuze02::ResetChip(){
313   fChip->Zero();
314 }