]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ITS/AliITSDDLRawData.cxx
Commenting out unused variables
[u/mrichter/AliRoot.git] / ITS / AliITSDDLRawData.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-2003, 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 //This class contains all the necessary methods to create the Raw Data
20 //files (slides) for the ITS data challenges for:
21 //SPD 
22 //SDD
23 //SSD
24
25 #include <stdlib.h>
26 #include <Riostream.h>
27 #include <TClonesArray.h>
28 #include <TTree.h>
29 #include <TMath.h>
30 #include "AliITS.h"
31 #include "AliITSgeom.h"
32 #include "AliITSdigitSPD.h"
33 #include "AliITSdigitSDD.h"
34 #include "AliITSdigitSSD.h"
35 #include "AliITSDDLRawData.h"
36 #include "AliRawDataHeader.h"
37 #include "AliITSRawStreamSPD.h"
38 #include "AliITSRawStreamSDD.h"
39 #include "AliITSRawStreamSSD.h"
40
41 ClassImp(AliITSDDLRawData)
42
43 ////////////////////////////////////////////////////////////////////////////////////////
44 AliITSDDLRawData::AliITSDDLRawData(){
45   //Default constructor
46   fIndex=-1;
47   fHalfStaveModule=-1;
48   fVerbose=0;
49 }
50
51 ////////////////////////////////////////////////////////////////////////////////////////
52
53 AliITSDDLRawData::AliITSDDLRawData(const AliITSDDLRawData &source) : 
54     TObject(source){
55   //Copy Constructor
56   this->fIndex=source.fIndex;
57   this->fHalfStaveModule=source.fHalfStaveModule;
58   this->fVerbose=source.fVerbose;
59   return;
60 }
61
62 ////////////////////////////////////////////////////////////////////////////////////////
63
64 AliITSDDLRawData& AliITSDDLRawData::operator=(const AliITSDDLRawData &source){
65   //Assigment operator
66   this->fIndex=source.fIndex;
67   this->fHalfStaveModule=source.fHalfStaveModule;
68   this->fVerbose=source.fVerbose;
69   return *this;
70 }
71
72 ////////////////////////////////////////////////////////////////////////////////////////
73 //STRIP 
74 //
75
76 void AliITSDDLRawData::GetDigitsSSD(TClonesArray *ITSdigits,Int_t mod,Int_t modR,Int_t ddl,UInt_t *buf){
77   //This method packs the SSD digits in a proper 32 bits structure
78   Int_t ix;
79   Int_t iz;
80   Int_t is;
81   UInt_t word;
82   UInt_t baseWord;
83   Int_t ndigits = ITSdigits->GetEntries();
84   AliITSdigit *digs;
85   ofstream ftxt;
86   if(ndigits){
87     if (fVerbose==2){
88       ftxt.open("SSDdigits.txt",ios::app);
89     }
90     for (Int_t digit=0;digit<ndigits;digit++) {
91       digs = (AliITSdigit*)ITSdigits->UncheckedAt(digit);
92       iz=digs->GetCoord1();  // If iz==0, N side and if iz=1 P side
93       ix=digs->GetCoord2();  // Strip Numbar
94       is=digs->GetCompressedSignal();  // ADC Signal
95       // cout<<" Module:"<<mod-500<<" N/P side:"<<iz<<" Strip Number:"<<ix<<" Amplidute:"<<is-1<<endl;
96       if (fVerbose==2)
97         ftxt<<"DDL:"<<ddl<<" Mod: "<<modR<<" N/P: "<<iz<<" Strip: "<<ix<<" Value: "<<is-1<<endl;
98       baseWord=0;
99       word=is-1;
100       PackWord(baseWord,word,0,9);//ADC data
101       word=ix;
102       PackWord(baseWord,word,10,19);//Strip Number
103       word=iz;      
104       PackWord(baseWord,word,20,20);//ADC Channel ID (N or P side)
105       word=mod;
106       PackWord(baseWord,word,21,31);//ADC module ID
107       fIndex++;
108       buf[fIndex]=baseWord;
109     }//end for
110   }//end if
111   if (fVerbose==2)
112     ftxt.close();
113   return;
114 }//end GetDigitsSSD
115
116 ////////////////////////////////////////////////////////////////////////////////////////
117 //Silicon Drift Detector
118 //
119
120 void AliITSDDLRawData::GetDigitsSDD(TClonesArray *ITSdigits,Int_t mod,Int_t modR,Int_t ddl,UInt_t *buf){  
121   //This method packs the SSD digits in a proper 32 bits structure
122   Int_t ix;
123   Int_t iz;
124   Int_t is;
125   UInt_t word;
126   UInt_t baseWord;
127   Int_t ndigits = ITSdigits->GetEntries();
128   AliITSdigit *digs;
129   ofstream ftxt;
130   if(ndigits){
131     //cout<<"Mudule "<<mod<<" number of digits "<<ndigits<<endl;
132     if (fVerbose==2)
133       ftxt.open("SDDdigits.txt",ios::app);
134     for (Int_t digit=0;digit<ndigits;digit++) {
135       digs = (AliITSdigit*)ITSdigits->UncheckedAt(digit);
136       iz=digs->GetCoord1();  // Anode
137       ix=digs->GetCoord2();  // Time
138       is=digs->GetCompressedSignal();  // ADC Signal
139       if (fVerbose==2)
140         ftxt<<"DDL:"<<ddl<<" MID:"<<modR<<" An:"<<iz<<" T:"<<ix<<" A:"<<is<<endl;
141       //      cout<<"Amplitude value:"<<is<<" Time Bucket:"<<ix<<" Anode:"<<iz<<endl;
142       if (is>255){Error("GetDigitsSDD", "bits words is needed)!!!");}
143       baseWord=0;
144       /*
145       //10 bits words for amplitude value
146       word=is;
147       PackWord(baseWord,word,0,9);//ADC data
148       word=ix;
149       PackWord(baseWord,word,10,17);//Time bucket
150       word=iz;
151       PackWord(baseWord,word,18,26);//Anode Number
152       word=mod;
153       PackWord(baseWord,word,27,31);//Module number
154       */
155       
156       //8bits words for amplitude value
157       word=is;
158       PackWord(baseWord,word,0,7);//ADC data
159       word=ix;
160       PackWord(baseWord,word,8,15);//Time bucket
161       word=iz;
162       PackWord(baseWord,word,16,24);//Anode Number
163       word=mod;
164       PackWord(baseWord,word,25,31);//Module number
165      
166       fIndex++;
167       buf[fIndex]=baseWord;
168     }//end for
169   }//end if
170   if(fVerbose==2)
171     ftxt.close();
172   return;
173 }//end GetDigitsSDD
174
175 ////////////////////////////////////////////////////////////////////////////////////////
176 //PIXEL 
177 //
178
179 void AliITSDDLRawData::GetDigitsSPD(TClonesArray *ITSdigits,Int_t mod,Int_t ddl, UInt_t *buf){
180   //This method packs the SPD digits in a proper 32 structure
181   //Since data is zero suppressed,the coordinates for the chip having zero digits 
182   //doesn't get listed in the galice.root file. However the SPD format requires 
183   //the empty chip to be written with chip header and chip trailer.
184   Int_t ix;
185   Int_t iz;
186   Int_t chipNo=0;
187   UInt_t baseWord=0;
188   UInt_t hitRow=0;
189   Int_t chipHitCount=0;  //Number of Hit in the current chip
190   Int_t previousChip=-1; //Previuos chip respect to the actual aone
191   Int_t ndigits = ITSdigits->GetEntries(); //number of digits in the current module
192   //cout<<"      Number of digits in the current module:"<<ndigits<<" module:"<<mod<<endl;
193   AliITSdigit *digs;
194   fHalfStaveModule++;    //It's a private variable used to distinguish between the firs  
195                          //and the second module of an Half Stave Module
196   ofstream ftxt;
197   if(ndigits){
198     //loop over digits
199     if (fVerbose==2)
200       ftxt.open("SPDdigits.txt",ios::app);
201     for (Int_t digit=0;digit<ndigits;digit++){
202       digs = (AliITSdigit*)ITSdigits->UncheckedAt(digit);
203       /*---------------------------------------------------------------------------
204        *     Each module contains 5 read out chips of 256 rows and 32 columns.
205        *     So, the cell number in Z direction varies from 0 to 159.  Therefore,
206        *     to get the chip address (0 to 4), we need to divide column number by 32.
207        *     ---------------------------------------------------------------------*/
208       iz=digs->GetCoord1();  // Cell number in Z direction 
209       ix=digs->GetCoord2();  // Cell number in X direction
210       chipNo=iz/32;
211       if(fVerbose==2)
212         ftxt<<"DDL:"<<ddl<<" Mod:"<<mod<<" Row:"<<ix<<" Col:"<<iz<<endl;
213       hitRow=iz-chipNo*32;
214       if(fHalfStaveModule){
215         chipNo+=5;
216         fHalfStaveModule=-1;
217       }//end if
218       if(previousChip==-1){
219         //loop over chip without digits 
220         //Even if there aren't digits for a given chip 
221         //the chip header and the chip trailer are stored
222         for(Int_t i=0;i<(iz/32);i++){
223           if(chipNo>4)
224             WriteChipHeader(i+5,(mod/2),baseWord);
225           else
226             WriteChipHeader(i,(mod/2),baseWord);
227           WriteChipTrailer(buf,chipHitCount,baseWord);
228           chipHitCount=0;
229         }//end for
230         WriteChipHeader(chipNo,(mod/2),baseWord);
231         chipHitCount++;
232         WriteHit(buf,ix,hitRow,baseWord);
233         previousChip=chipNo;
234       }//end if
235       else{
236         if(previousChip!=chipNo){
237           WriteChipTrailer(buf,chipHitCount,baseWord);
238           chipHitCount=0;
239           for(Int_t i=previousChip+1;i<chipNo;i++){
240             WriteChipHeader(i,(mod/2),baseWord);
241             WriteChipTrailer(buf,0,baseWord);
242             chipHitCount=0;
243           }//end for
244           WriteChipHeader(chipNo,(mod/2),baseWord);
245           previousChip=chipNo;
246         }//end if
247         chipHitCount++;
248         WriteHit(buf,ix,hitRow,baseWord);
249       }//end else
250     }//end for
251     //Even if there aren't digits for a given chip 
252     //the chip header and the chip trailer are stored
253     Int_t end=4;
254     if(chipNo>4)end+=5;
255     WriteChipTrailer(buf,chipHitCount,baseWord);
256     chipHitCount=0;
257     for(Int_t i=chipNo+1;i<=end;i++){
258       WriteChipHeader(i,(mod/2),baseWord);
259       WriteChipTrailer(buf,0,baseWord);
260       chipHitCount=0;
261     }//end for
262   }//end if
263   else{
264     //In this module there aren't digits but
265     //the chip header and chip trailer are store anyway
266     if(fHalfStaveModule){
267       chipNo=5;
268       fHalfStaveModule=-1;
269     }//end if
270     for(Int_t i=0;i<5;i++){
271       WriteChipHeader(chipNo+i,(mod/2),baseWord);
272       WriteChipTrailer(buf,chipHitCount,baseWord);
273       chipHitCount=0;
274     }//end for
275   }//end else 
276   if(fVerbose==2)
277     ftxt.close();
278   return;
279 }//end GetDigitsSPD
280
281 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
282
283 void AliITSDDLRawData::PackWord(UInt_t &BaseWord, UInt_t Word, Int_t StartBit, Int_t StopBit){
284   //This method packs a word into the Baseword buffer starting form the "StartBit" 
285   //and tacking StopBit-StertBit+1 bits
286   UInt_t dummyWord,offSet;
287   Int_t   length;
288   UInt_t sum;
289   //The BaseWord is being filled with 1 from StartBit to StopBit
290   length=StopBit-StartBit+1;
291   sum=(UInt_t)TMath::Power(2,length)-1;
292   if(Word > sum){
293     Error("PackWord", "Word to be filled is not within desired length\n"
294           "Word:%d Start bit:%d Stop Bit:%d",Word,StartBit,StopBit);
295     return;
296   }
297   offSet=sum;
298   offSet<<=StartBit;
299   BaseWord=BaseWord|offSet;
300   //The Word to be filled is shifted to the position StartBit
301   //and the remaining  Left and Right bits are filled with 1
302   sum=(UInt_t)TMath::Power(2,StartBit)-1;
303   dummyWord=0xFFFFFFFF<<length;
304   dummyWord +=Word;
305   dummyWord<<=StartBit;
306   dummyWord+=sum;
307   BaseWord=BaseWord&dummyWord;
308   return;
309 }
310
311 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
312
313 void AliITSDDLRawData::UnpackWord(UInt_t PackedWord, Int_t StartBit, Int_t StopBit, UInt_t &Word){      
314   //This method unpacks a words of StopBit-StertBit+1 bits starting from "StopBits"  
315   UInt_t offSet;
316   Int_t length;
317   length=StopBit-StartBit+1;
318   offSet=(UInt_t)TMath::Power(2,length)-1;
319   offSet<<=StartBit;
320   Word=PackedWord&offSet;
321   Word>>=StartBit;
322   return;
323 }
324
325 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
326
327 Int_t AliITSDDLRawData::RawDataSPD(TBranch* branch){
328   //This method creates the Raw data files for SPD detectors
329   const Int_t kSize=21000; //256*32*5=40960 max number of digits per module
330   UInt_t buf[kSize];      //One buffer cell can contain 2 digits 
331   fIndex=-1;
332
333   TClonesArray*& digits = * (TClonesArray**) branch->GetAddress();
334   char fileName[15];
335   ofstream outfile;         // logical name of the output file 
336   AliRawDataHeader header;
337
338   //loop over DDLs
339   for(Int_t i=0;i<AliITSRawStreamSPD::kDDLsNumber;i++){
340     sprintf(fileName,"ITSSPD_%d.ddl",i+AliITSRawStreamSPD::kDDLOffset); //The name of the output file.
341 #ifndef __DECCXX
342     outfile.open(fileName,ios::binary);
343 #else
344     outfile.open(fileName);
345 #endif
346     //write Dummy DATA HEADER
347     UInt_t dataHeaderPosition=outfile.tellp();
348     outfile.write((char*)(&header),sizeof(header));
349     //Loops over Modules of a particular DDL
350     for (Int_t mod=0; mod<AliITSRawStreamSPD::kModulesPerDDL; mod++){
351       Int_t moduleNumber = AliITSRawStreamSPD::GetModuleNumber(i, mod);
352       digits->Clear();
353       branch->GetEvent(moduleNumber);
354       //For each Module, buf contains the array of data words in Binary format    
355       //fIndex gives the number of 32 bits words in the buffer for each module
356       GetDigitsSPD(digits,moduleNumber,i,buf);
357       outfile.write((char *)buf,((fIndex+1)*sizeof(UInt_t)));
358       for(Int_t i=0;i<(fIndex+1);i++){
359         buf[i]=0;
360       }//end for
361       fIndex=-1;
362     }//end for
363     
364     //Write REAL DATA HEADER
365     UInt_t currentFilePosition=outfile.tellp();
366     outfile.seekp(dataHeaderPosition);
367     header.fSize=currentFilePosition-dataHeaderPosition;
368     header.SetAttribute(0);  // valid data
369     outfile.write((char*)(&header),sizeof(header));
370     outfile.close();
371   }//end for
372
373   return 0;  
374 }
375
376 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
377
378 Int_t AliITSDDLRawData::RawDataSSD(TBranch* branch){
379   //This method creates the Raw data files for SSD detectors
380   const Int_t kSize=1536;//768*2 Number of stripe * number of sides(N and P)
381   UInt_t buf[kSize];      
382   fIndex=-1;
383
384   TClonesArray*& digits = * (TClonesArray**) branch->GetAddress();
385   char fileName[15];
386   ofstream outfile;         // logical name of the output file 
387   AliRawDataHeader header;
388
389   //loop over DDLs  
390   for(Int_t i=0;i<AliITSRawStreamSSD::kDDLsNumber;i++){
391     sprintf(fileName,"ITSSSD_%d.ddl",i+AliITSRawStreamSSD::kDDLOffset); //The name of the output file
392 #ifndef __DECCXX
393     outfile.open(fileName,ios::binary);
394 #else
395     outfile.open(fileName);
396 #endif
397     //write Dummy DATA HEADER
398     UInt_t dataHeaderPosition=outfile.tellp();
399     outfile.write((char*)(&header),sizeof(header));
400     
401     //Loops over Modules of a particular DDL
402     for (Int_t mod=0; mod<AliITSRawStreamSSD::kModulesPerDDL; mod++){
403       Int_t moduleNumber = AliITSRawStreamSSD::GetModuleNumber(i, mod);
404       if(moduleNumber!=-1){
405         digits->Clear();
406         branch->GetEvent(moduleNumber);
407         //For each Module, buf contains the array of data words in Binary format          
408         //fIndex gives the number of 32 bits words in the buffer for each module
409         GetDigitsSSD(digits,mod,moduleNumber,i,buf);
410         outfile.write((char *)buf,((fIndex+1)*sizeof(UInt_t)));
411         fIndex=-1;
412       }//end if
413     }//end for
414
415     //Write REAL DATA HEADER
416     UInt_t currentFilePosition=outfile.tellp();
417     outfile.seekp(dataHeaderPosition);
418     header.fSize=currentFilePosition-dataHeaderPosition;
419     header.SetAttribute(0);  // valid data
420     outfile.write((char*)(&header),sizeof(header));
421     outfile.close();
422   }//end for
423
424   return 0;  
425 }
426
427 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
428
429 Int_t AliITSDDLRawData::RawDataSDD(TBranch* branch){
430     //This method creates the Raw data files for SDD detectors
431   const Int_t kSize=131072; //256*512
432   UInt_t buf[kSize];      
433   fIndex=-1;
434
435   TClonesArray*& digits = * (TClonesArray**) branch->GetAddress();
436   char fileName[15];
437   ofstream outfile;             // logical name of the output file 
438   AliRawDataHeader header;
439
440   //loop over DDLs  
441   for(Int_t i=0;i<AliITSRawStreamSDD::kDDLsNumber;i++){
442     sprintf(fileName,"ITSSDD_%d.ddl",i+AliITSRawStreamSDD::kDDLOffset); //The name of the output file
443 #ifndef __DECCXX
444     outfile.open(fileName,ios::binary);
445 #else
446     outfile.open(fileName);
447 #endif
448     //write Dummy DATA HEADER
449     UInt_t dataHeaderPosition=outfile.tellp();
450     outfile.write((char*)(&header),sizeof(header));
451
452     //Loops over Modules of a particular DDL
453     for (Int_t mod=0; mod<AliITSRawStreamSDD::kModulesPerDDL; mod++){
454       Int_t moduleNumber = AliITSRawStreamSDD::GetModuleNumber(i, mod);
455       if(moduleNumber!=-1){
456         digits->Clear();
457         branch->GetEvent(moduleNumber);
458         //For each Module, buf contains the array of data words in Binary format          
459         //fIndex gives the number of 32 bits words in the buffer for each module
460         //      cout<<"MODULE NUMBER:"<<mapSDD[i][mod]<<endl;
461         GetDigitsSDD(digits,mod,moduleNumber,i,buf);
462         outfile.write((char *)buf,((fIndex+1)*sizeof(UInt_t)));
463         fIndex=-1;
464       }//end if
465     }//end for
466     
467     //Write REAL DATA HEADER
468     UInt_t currentFilePosition=outfile.tellp();
469     outfile.seekp(dataHeaderPosition);
470     header.fSize=currentFilePosition-dataHeaderPosition;
471     header.SetAttribute(0);  // valid data
472     outfile.write((char*)(&header),sizeof(header));
473     outfile.close();
474   }//end for
475
476   return 0;  
477 }
478
479 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
480
481 void AliITSDDLRawData::WriteChipHeader(Int_t ChipAddr,Int_t EventCnt,UInt_t &BaseWord){
482   //This method writes a chip header 
483   //cout<<"Chip: "<<ChipAddr<<" Half Stave module:"<<EventCnt<<endl;
484   BaseWord=0;
485   PackWord(BaseWord,ChipAddr,0,3);
486   PackWord(BaseWord,EventCnt,4,10);
487   PackWord(BaseWord,0x7,11,13);
488   PackWord(BaseWord,0x1,14,15);
489   return;
490 }//end WriteChipHeader
491
492 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
493
494 void AliITSDDLRawData::ReadChipHeader(Int_t &ChipAddr,Int_t &EventCnt,UInt_t BaseWord){
495   //This method reads a chip header
496   UInt_t temp=0;
497   UnpackWord(BaseWord,0,3,temp);
498   ChipAddr=(Int_t)temp;
499   UnpackWord(BaseWord,4,10,temp);
500   EventCnt=(Int_t)temp;
501   if(fVerbose)
502     Info("ReadChipHeader", "Chip:&d Half Stave module:%d",ChipAddr,EventCnt);
503   return;
504 }//end ReadChipHeader
505
506 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
507
508 void  AliITSDDLRawData::WriteChipTrailer(UInt_t *buf,Int_t ChipHitCount,UInt_t &BaseWord){
509   //This method writes a chip trailer
510   //pixel fill word
511   if((ChipHitCount%2)!=0){
512     PackWord(BaseWord,0xFEDC,0,15);
513   }
514   PackWord(BaseWord,ChipHitCount,16,28);
515   PackWord(BaseWord,0x0,30,31);
516   fIndex++;
517   buf[fIndex]=BaseWord;
518   BaseWord=0;
519   return;
520 }//end WriteChipTrailer
521
522 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
523
524 void  AliITSDDLRawData::ReadChipTrailer(Int_t &ChipHitCount,UInt_t BaseWord){
525   //This method reads a chip trailer
526   UInt_t temp=0;
527   UnpackWord(BaseWord,16,28,temp);
528   ChipHitCount=(Int_t)temp;
529   return;
530 }//end ReadChipTrailer
531
532 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
533
534 void  AliITSDDLRawData::WriteHit(UInt_t *buf,Int_t RowAddr,Int_t HitAddr,UInt_t &BaseWord){
535   //This method writs an hit
536   if(!BaseWord){
537     PackWord(BaseWord,HitAddr,0,4);
538     PackWord(BaseWord,RowAddr,5,12);
539     PackWord(BaseWord,2,14,15);
540   }//end if
541   else{
542     PackWord(BaseWord,HitAddr,16,20);
543     PackWord(BaseWord,RowAddr,21,28);
544     PackWord(BaseWord,2,30,31);
545     fIndex++;
546     buf[fIndex]=BaseWord;
547     BaseWord=0;
548   }//end else
549   return;
550 }//end WriteHit
551