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