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