]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ITS/AliITSDDLRawData.cxx
New version of the hybrid geometry: fixes to SDD simulation (L.Gaudichet)
[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 SDD digits in a proper 32 bits structure
118   Int_t ix;
119   Int_t iz;
120   Int_t is;
121   UInt_t word=0;
122   UInt_t baseWord=0;
123   Int_t ndigits = ITSdigits->GetEntries();
124   AliITSdigit *digs;
125   ofstream ftxt;
126   Int_t digarr[512][256];
127   for(Int_t i=0;i<512;i++){
128     for(Int_t j=0;j<256;j++){
129       digarr[i][j]=0;
130     }
131   }
132   //word to select the 12 carlos for the 12 modules
133   UInt_t carlosid=0;
134   if(mod==0) carlosid=805306368;
135   if(mod==1) carlosid=805306369;
136   if(mod==2) carlosid=805306370;
137   if(mod==3) carlosid=805306371;
138   if(mod==4) carlosid=805306372;
139   if(mod==5) carlosid=805306373;
140   if(mod==6) carlosid=805306374;
141   if(mod==7) carlosid=805306375;
142   if(mod==8) carlosid=805306376;
143   if(mod==9) carlosid=805306377;
144   if(mod==10) carlosid=805306378;
145   if(mod==11) carlosid=805306379;
146   
147   fIndex++;
148   buf[fIndex]=carlosid;
149   Int_t first=0;
150   Int_t last=0;
151   Int_t diff=0;
152   Int_t nbit=0;
153   UInt_t word2=0;
154   Bool_t flag = kFALSE;
155   baseWord=0;
156   Int_t bitinfo1[4] = {3,8,3,7}; //vector with info on bit for timebin info 
157   Int_t wordinfo1[4]= {0,0,0,0}; //vector with word info for timebin info 
158   Int_t bitinfo2[2] = {3,18};    //vector with info on bit for EOR (end of row) info
159   Int_t wordinfo2[3]= {1,65593}; //vector with word info for anode info
160
161   /* for time bin info: word          n bits   meaning
162                          0               3      next info is timebin 
163                          8               3      next word is 8 bit long
164                        tb value          8      timebin value
165                        n (2->7)          3      next info is n bit long
166                         signal           n      signal value
167
168      for anode info:     1               3      next 18 bits are for EOR 
169                                                 increments the anode value
170
171                          EOR             18     error codes + other info
172   */
173              
174   if(ndigits){
175     if (fVerbose==2)
176       ftxt.open("SDDdigits.txt",ios::app);
177     for (Int_t digit=0;digit<ndigits;digit++) {
178       digs = (AliITSdigit*)ITSdigits->UncheckedAt(digit);
179       iz=digs->GetCoord1();  // Anode
180       ix=digs->GetCoord2();  // Time
181       is=digs->GetCompressedSignal();  // ADC Signal
182       digarr[iz][ix]=is;
183         if (fVerbose==2)
184         ftxt<<"DDL:"<<ddl<<" MID:"<<modR<<" An:"<<iz<<" T:"<<ix<<" A:"<<is<<endl;
185       if (is>255){Error("GetDigitsSDD", "bits words is needed)!!!");}
186     }
187       
188     for(Int_t anode=0;anode<512;anode++){
189       if(flag){            
190         last = first+diff-1;
191         AliBitPacking::PackWord(word2,baseWord,first,last);
192         flag = kFALSE;
193         first = last+1;
194         diff=0;
195       }
196       
197       
198       if(anode == 256){
199         last = 0;
200         first = 0;
201         flag = kFALSE;
202         diff = 0;
203         word2=0;
204         
205       }
206       
207       for(Int_t tb=0;tb<256;tb++){
208         if(digarr[anode][tb]!=0){
209           if(flag){      
210             last = first+diff-1;
211             AliBitPacking::PackWord(word2,baseWord,first,last);
212             flag = kFALSE;
213             first = last+1;
214             diff=0;
215           }
216           wordinfo1[1] = tb;
217           //non lossy compression as it is done in Carlos 
218           //(data are already 10to8bit compressed by AMBRA
219
220           /* if value < 8  value = value - (1 << 2) (word is 2 bit long) 
221              if value < 16 value = value - (1 << 3) (word is 3 bit long)
222              if value < 32 value = value - (1 << 4) (word is 4 bit long)
223              if value < 64 value = value - (1 << 5) (word is 5 bit long)
224              if value <128 value = value - (1 << 6) (word is 6 bit long)
225              if value >=128value = value - (1 << 7) (word is 7 bit long)
226
227           */
228           if(digarr[anode][tb]<8){
229             bitinfo1[3] = 2;
230             wordinfo1[2] = 2;
231             wordinfo1[3] = digarr[anode][tb]-(1 << bitinfo1[3]);
232           }               
233           if(digarr[anode][tb]>=8 && digarr[anode][tb]<16){
234             bitinfo1[3] = 3;
235             wordinfo1[2] = 3;
236             wordinfo1[3] = digarr[anode][tb]-(1 << bitinfo1[3]);
237           }
238           if(digarr[anode][tb]>=16 && digarr[anode][tb]<32){
239             bitinfo1[3] = 4;
240             wordinfo1[2] = 4;
241             wordinfo1[3] = digarr[anode][tb]-(1 << bitinfo1[3]);
242           }
243           if(digarr[anode][tb]>=32 && digarr[anode][tb]<64){
244             bitinfo1[3] = 5;
245             wordinfo1[2] = 5;
246             wordinfo1[3] = digarr[anode][tb]-(1 << bitinfo1[3]);
247           }
248           if(digarr[anode][tb]>=64 && digarr[anode][tb]<128){
249             bitinfo1[3] = 6;
250             wordinfo1[2] = 6;
251             wordinfo1[3] = digarr[anode][tb]-(1 << bitinfo1[3]);
252           }
253           if(digarr[anode][tb]>=128){
254             bitinfo1[3] = 7;
255             wordinfo1[2] = 7;
256             wordinfo1[3] = digarr[anode][tb]-(1 << bitinfo1[3]);
257           }
258           
259           for(Int_t ie=0;ie<4;ie++){
260             
261             if(flag){      
262               last = first+diff-1;
263               AliBitPacking::PackWord(word2,baseWord,first,last);
264               flag = kFALSE;
265               first = last+1;
266               diff=0;
267             }
268             last = first+bitinfo1[ie]-1;
269             if(first < 30 && last < 30){                  
270               AliBitPacking::PackWord(wordinfo1[ie],baseWord,first,last); 
271               first = last+1;
272             }
273             else{
274               if(first<=29){
275                 UInt_t w = AliBitPacking::UnpackWord(wordinfo1[ie],0,29-first);
276                 AliBitPacking::PackWord(w,baseWord,first,29);
277                 Int_t lb = 29-first+1;
278                 diff = bitinfo1[ie]-lb;
279                 word2 = AliBitPacking::UnpackWord(wordinfo1[ie],lb,lb+diff-1);
280                 flag = kTRUE;
281                 if(anode<256) word = 2;//channel 0 of carlos
282                 else word = 3; //channel 1 of carlos
283                 AliBitPacking::PackWord(word,baseWord,30,31);
284                 fIndex++;
285                 buf[fIndex]=baseWord;
286                 first=0;
287                 last = 0;
288                 baseWord=0;
289                 word = 0;
290               }
291               else{
292                 word2 = wordinfo1[ie];
293                 diff = bitinfo1[ie];
294                 flag = kTRUE;
295                 if(anode<256) word = 2; //channel 0 of carlos
296                 else word = 3; //channel 1 of carlos
297                 AliBitPacking::PackWord(word,baseWord,30,31);
298                 fIndex++;
299                 buf[fIndex]=baseWord;
300                 first=0;
301                 last=0;
302                 baseWord=0;
303                 word = 0;
304               }
305             }
306           }
307           
308         }//END IF
309         
310       }//end loop on tb
311     
312       for(Int_t i=0;i<2;i++){
313         if(flag){      
314           last = first+diff-1;
315           AliBitPacking::PackWord(word2,baseWord,first,last);
316           flag = kFALSE;
317           first = last+1;
318           diff=0;
319         }
320         
321         word = wordinfo2[i];
322         nbit = bitinfo2[i];
323         last = first+nbit-1;
324         if(first < 30 && last < 30){              
325           AliBitPacking::PackWord(word,baseWord,first,last); //3 bit code =1 -> next 18 bits for EOR
326           first = last+1;
327         }
328         
329         else{
330           if(first<=29){
331             UInt_t w = AliBitPacking::UnpackWord(word,0,29-first);
332             AliBitPacking::PackWord(w,baseWord,first,29);
333             Int_t lb = 29-first+1;
334             diff = nbit-lb;        
335             word2 = AliBitPacking::UnpackWord(word,lb,lb+diff-1);
336             flag = kTRUE;
337             if(anode<256) word = 2;
338             else word = 3;
339             AliBitPacking::PackWord(word,baseWord,30,31);
340             fIndex++;
341             buf[fIndex]=baseWord;
342             first=0;
343             last = 0;
344             baseWord=0;
345             if(anode==255){
346               flag=kFALSE;
347               word2=0;
348             }
349           }
350           else{
351             word2 = word;
352             diff = nbit;
353             flag = kTRUE;
354             if(anode<256) word = 2;
355             else word = 3;
356             AliBitPacking::PackWord(word,baseWord,30,31);
357             fIndex++;
358             buf[fIndex]=baseWord;
359             first=0;
360             last=0;
361             baseWord=0;
362             if(anode==255){
363               flag=kFALSE;
364               word2=0;
365             }
366           }
367         }
368       }
369     } //end for
370     
371   }
372   if(fVerbose==2)
373     ftxt.close();
374   return;
375 }//end GetDigitsSDD
376
377 ////////////////////////////////////////////////////////////////////////////////////////
378 //PIXEL 
379 //
380
381 void AliITSDDLRawData::GetDigitsSPD(TClonesArray *ITSdigits,Int_t mod,Int_t ddl, UInt_t *buf){
382   //This method packs the SPD digits in a proper 32 structure
383   //Since data is zero suppressed,the coordinates for the chip having zero digits 
384   //doesn't get listed in the galice.root file. However the SPD format requires 
385   //the empty chip to be written with chip header and chip trailer.
386   //The index of the half stave is calculated as (mod/2).
387   Int_t ix;
388   Int_t iz;
389   Int_t chipNo=0;
390   UInt_t baseWord=0;
391   UInt_t hitRow=0;
392   Int_t chipHitCount=0;  //Number of Hit in the current chip
393   Int_t previousChip=-1; //Previuos chip respect to the actual aone
394   Int_t ndigits = ITSdigits->GetEntries(); //number of digits in the current module
395   //cout<<"      Number of digits in the current module:"<<ndigits<<" module:"<<mod<<endl;
396   AliITSdigit *digs;
397   fHalfStaveModule++;    //It's a private variable used to distinguish between the firs  
398                          //and the second module of an Half Stave Module
399   ofstream ftxt;
400   if(ndigits){
401     //loop over digits
402     if (fVerbose==2)
403       ftxt.open("SPDdigits.txt",ios::app);
404     for (Int_t digit=0;digit<ndigits;digit++){
405       digs = (AliITSdigit*)ITSdigits->UncheckedAt(digit);
406       /*---------------------------------------------------------------------------
407        *     Each module contains 5 read out chips of 256 rows and 32 columns.
408        *     So, the cell number in Z direction varies from 0 to 159.  Therefore,
409        *     to get the chip address (0 to 4), we need to divide column number by 32.
410        *     ---------------------------------------------------------------------*/
411       iz=digs->GetCoord1();  // Cell number in Z direction 
412       ix=digs->GetCoord2();  // Cell number in X direction
413       chipNo=iz/32;
414       if(fVerbose==2)
415         ftxt<<"DDL:"<<ddl<<" Mod:"<<mod<<" Row:"<<ix<<" Col:"<<iz<<endl;
416       hitRow=iz-chipNo*32;
417       if(fHalfStaveModule){
418         chipNo+=5;
419         fHalfStaveModule=-1;
420       }//end if
421       if(previousChip==-1){
422         //loop over chip without digits 
423         //Even if there aren't digits for a given chip 
424         //the chip header and the chip trailer are stored
425         for(Int_t i=0;i<(iz/32);i++){
426           if(chipNo>4)
427             WriteChipHeader(i+5,(mod/2),baseWord);
428           else
429             WriteChipHeader(i,(mod/2),baseWord);
430           WriteChipTrailer(buf,chipHitCount,baseWord);
431           chipHitCount=0;
432         }//end for
433         WriteChipHeader(chipNo,(mod/2),baseWord);
434         chipHitCount++;
435         WriteHit(buf,ix,hitRow,baseWord);
436         previousChip=chipNo;
437       }//end if
438       else{
439         if(previousChip!=chipNo){
440           WriteChipTrailer(buf,chipHitCount,baseWord);
441           chipHitCount=0;
442           for(Int_t i=previousChip+1;i<chipNo;i++){
443             WriteChipHeader(i,(mod/2),baseWord);
444             WriteChipTrailer(buf,0,baseWord);
445             chipHitCount=0;
446           }//end for
447           WriteChipHeader(chipNo,(mod/2),baseWord);
448           previousChip=chipNo;
449         }//end if
450         chipHitCount++;
451         WriteHit(buf,ix,hitRow,baseWord);
452       }//end else
453     }//end for
454     //Even if there aren't digits for a given chip 
455     //the chip header and the chip trailer are stored
456     Int_t end=4;
457     if(chipNo>4)end+=5;
458     WriteChipTrailer(buf,chipHitCount,baseWord);
459     chipHitCount=0;
460     for(Int_t i=chipNo+1;i<=end;i++){
461       WriteChipHeader(i,(mod/2),baseWord);
462       WriteChipTrailer(buf,0,baseWord);
463       chipHitCount=0;
464     }//end for
465   }//end if
466   else{
467     //In this module there aren't digits but
468     //the chip header and chip trailer are store anyway
469     if(fHalfStaveModule){
470       chipNo=5;
471       fHalfStaveModule=-1;
472     }//end if
473     for(Int_t i=0;i<5;i++){
474       WriteChipHeader(chipNo+i,(mod/2),baseWord);
475       WriteChipTrailer(buf,chipHitCount,baseWord);
476       chipHitCount=0;
477     }//end for
478   }//end else 
479   if(fVerbose==2)
480     ftxt.close();
481   return;
482 }//end GetDigitsSPD
483
484 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
485
486 Int_t AliITSDDLRawData::RawDataSPD(TBranch* branch){
487   //This method creates the Raw data files for SPD detectors
488   const Int_t kSize=21000; //256*32*5=40960 max number of digits per module
489   UInt_t buf[kSize];      //One buffer cell can contain 2 digits 
490   fIndex=-1;
491
492   TClonesArray*& digits = * (TClonesArray**) branch->GetAddress();
493   char fileName[15];
494   ofstream outfile;         // logical name of the output file 
495   AliRawDataHeader header;
496
497   //loop over DDLs
498   for(Int_t i=0;i<AliDAQ::NumberOfDdls("ITSSPD");i++){
499     strcpy(fileName,AliDAQ::DdlFileName("ITSSPD",i)); //The name of the output file.
500 #ifndef __DECCXX
501     outfile.open(fileName,ios::binary);
502 #else
503     outfile.open(fileName);
504 #endif
505     //write Dummy DATA HEADER
506     UInt_t dataHeaderPosition=outfile.tellp();
507     outfile.write((char*)(&header),sizeof(header));
508     //Loops over Modules of a particular DDL
509     for (Int_t mod=0; mod<AliITSRawStreamSPD::kModulesPerDDL; mod++){
510       Int_t moduleNumber = AliITSRawStreamSPD::GetModuleNumber(i, mod);
511       digits->Clear();
512       branch->GetEvent(moduleNumber);
513       //For each Module, buf contains the array of data words in Binary format    
514       //fIndex gives the number of 32 bits words in the buffer for each module
515       GetDigitsSPD(digits,mod,i,buf);
516       outfile.write((char *)buf,((fIndex+1)*sizeof(UInt_t)));
517       for(Int_t i=0;i<(fIndex+1);i++){
518         buf[i]=0;
519       }//end for
520       fIndex=-1;
521     }//end for
522     
523     //Write REAL DATA HEADER
524     UInt_t currentFilePosition=outfile.tellp();
525     outfile.seekp(dataHeaderPosition);
526     header.fSize=currentFilePosition-dataHeaderPosition;
527     outfile.write((char*)(&header),sizeof(header));
528     outfile.close();
529   }//end for
530
531   return 0;  
532 }
533
534 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
535
536 Int_t AliITSDDLRawData::RawDataSSD(TBranch* branch){
537
538   //This method creates the Raw data files for SSD detectors
539   const Int_t kSize=1536;//768*2 Number of stripe * number of sides(N and P)
540   UInt_t buf[kSize];      
541   fIndex=-1;
542
543   TClonesArray*& digits = * (TClonesArray**) branch->GetAddress();
544   char fileName[15];
545   ofstream outfile;         // logical name of the output file 
546   AliRawDataHeader header;
547
548   //loop over DDLs  
549   for(Int_t i=0;i<AliDAQ::NumberOfDdls("ITSSSD");i++){
550     strcpy(fileName,AliDAQ::DdlFileName("ITSSSD",i)); //The name of the output file.
551 #ifndef __DECCXX
552     outfile.open(fileName,ios::binary);
553 #else
554     outfile.open(fileName);
555 #endif
556     //write Dummy DATA HEADER
557     UInt_t dataHeaderPosition=outfile.tellp();
558     outfile.write((char*)(&header),sizeof(header));
559     
560     //Loops over Modules of a particular DDL
561     for (Int_t mod=0; mod<AliITSRawStreamSSD::kModulesPerDDL; mod++){
562       Int_t moduleNumber = AliITSRawStreamSSD::GetModuleNumber(i, mod);
563       if(moduleNumber!=-1){
564         digits->Clear();
565         branch->GetEvent(moduleNumber);
566         //For each Module, buf contains the array of data words in Binary format          
567         //fIndex gives the number of 32 bits words in the buffer for each module
568         GetDigitsSSD(digits,mod,moduleNumber,i,buf);
569         outfile.write((char *)buf,((fIndex+1)*sizeof(UInt_t)));
570         fIndex=-1;
571       }//end if
572     }//end for
573
574     //Write REAL DATA HEADER
575     UInt_t currentFilePosition=outfile.tellp();
576     outfile.seekp(dataHeaderPosition);
577     header.fSize=currentFilePosition-dataHeaderPosition;
578     header.SetAttribute(0);  // valid data
579     outfile.write((char*)(&header),sizeof(header));
580     outfile.close();
581   }//end for
582
583   return 0;  
584 }
585
586 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
587
588 Int_t AliITSDDLRawData::RawDataSDD(TBranch* branch){
589     //This method creates the Raw data files for SDD detectors
590   const Int_t kSize=131072; //256*512
591   UInt_t buf[kSize];      
592   fIndex=-1;
593
594   TClonesArray*& digits = * (TClonesArray**) branch->GetAddress();
595   char fileName[15];
596   ofstream outfile;             // logical name of the output file 
597   AliRawDataHeader header;
598   UInt_t skippedword = AliBitPacking::PackWord(2,skippedword,0,31);
599
600   //loop over DDLs  
601   for(Int_t i=0;i<AliDAQ::NumberOfDdls("ITSSDD");i++){
602     strcpy(fileName,AliDAQ::DdlFileName("ITSSDD",i)); //The name of the output file.
603 #ifndef __DECCXX
604     outfile.open(fileName,ios::binary);
605 #else
606     outfile.open(fileName);
607 #endif
608     //write Dummy DATA HEADER
609     UInt_t dataHeaderPosition=outfile.tellp();
610     outfile.write((char*)(&header),sizeof(header));
611
612     //first 9 "dummy" words to be skipped
613     for(Int_t iw=0;iw<9;iw++){
614         outfile.write((char*)&skippedword,sizeof(skippedword));
615     }
616    
617     //Loops over Modules of a particular DDL
618     for (Int_t mod=0; mod<AliITSRawStreamSDD::kModulesPerDDL; mod++){
619       Int_t moduleNumber = AliITSRawStreamSDD::GetModuleNumber(i, mod);
620       if(moduleNumber!=-1){
621         digits->Clear();
622         branch->GetEvent(moduleNumber);
623
624         //For each Module, buf contains the array of data words in Binary format          
625         //fIndex gives the number of 32 bits words in the buffer for each module
626         //      cout<<"MODULE NUMBER:"<<mapSDD[i][mod]<<endl;
627         GetDigitsSDD(digits,mod,moduleNumber,i,buf);
628         outfile.write((char *)buf,((fIndex+1)*sizeof(UInt_t)));
629         fIndex=-1;
630       }//end if
631     }//end for
632     
633     //Write REAL DATA HEADER
634     UInt_t currentFilePosition=outfile.tellp();
635     outfile.seekp(dataHeaderPosition);
636     header.fSize=currentFilePosition-dataHeaderPosition;
637     header.SetAttribute(0);  // valid data
638     outfile.write((char*)(&header),sizeof(header));
639
640     outfile.close();
641   }//end for
642
643   return 0;  
644 }
645
646 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
647
648 void AliITSDDLRawData::WriteChipHeader(Int_t ChipAddr,Int_t halfStave,UInt_t &BaseWord){
649   //This method writes a chip header 
650   //cout<<"Chip: "<<ChipAddr<<" Half Stave module:"<<halfStave<<endl;
651   BaseWord=0;
652   AliBitPacking::PackWord(ChipAddr,BaseWord,16,19);
653   //  At the moment the event count is always 0 (bits 20-26)
654   AliBitPacking::PackWord(0,BaseWord,20,26);
655   AliBitPacking::PackWord(halfStave,BaseWord,27,29);
656   AliBitPacking::PackWord(0x1,BaseWord,30,31);
657   return;
658 }//end WriteChipHeader
659
660 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
661
662 void  AliITSDDLRawData::WriteChipTrailer(UInt_t *buf,Int_t ChipHitCount,UInt_t &BaseWord){
663   //This method writes a chip trailer
664   //pixel fill word
665   if((ChipHitCount%2)!=0){
666     AliBitPacking::PackWord(0xC000,BaseWord,16,31);
667   }
668   AliBitPacking::PackWord(ChipHitCount,BaseWord,0,13);
669   AliBitPacking::PackWord(0x0,BaseWord,14,15);
670   fIndex++;
671   buf[fIndex]=BaseWord;
672   BaseWord=0;
673   return;
674 }//end WriteChipTrailer
675
676 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
677
678 void  AliITSDDLRawData::WriteHit(UInt_t *buf,Int_t RowAddr,Int_t HitAddr,UInt_t &BaseWord){
679   //This method writs an hit
680   if(!BaseWord){
681     AliBitPacking::PackWord(HitAddr,BaseWord,16,20);
682     AliBitPacking::PackWord(RowAddr,BaseWord,21,28);
683     AliBitPacking::PackWord(2,BaseWord,30,31);
684   }//end if
685   else{
686     AliBitPacking::PackWord(HitAddr,BaseWord,0,4);
687     AliBitPacking::PackWord(RowAddr,BaseWord,5,12);
688     AliBitPacking::PackWord(2,BaseWord,14,15);
689     fIndex++;
690     buf[fIndex]=BaseWord;
691     BaseWord=0;
692   }//end else
693   return;
694 }//end WriteHit
695