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