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