]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ITS/AliITSDDLRawData.cxx
4eaae88e72543db6bc0527b501db0b8ed5b0389e
[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]<4) continue; // not write <4 cnts above tL
264           if(digarr[anode][tb]<8){
265             bitinfo1[3] = 2;
266             wordinfo1[2] = 2;
267             wordinfo1[3] = digarr[anode][tb]-(1 << bitinfo1[3]);
268           }               
269           if(digarr[anode][tb]>=8 && digarr[anode][tb]<16){
270             bitinfo1[3] = 3;
271             wordinfo1[2] = 3;
272             wordinfo1[3] = digarr[anode][tb]-(1 << bitinfo1[3]);
273           }
274           if(digarr[anode][tb]>=16 && digarr[anode][tb]<32){
275             bitinfo1[3] = 4;
276             wordinfo1[2] = 4;
277             wordinfo1[3] = digarr[anode][tb]-(1 << bitinfo1[3]);
278           }
279           if(digarr[anode][tb]>=32 && digarr[anode][tb]<64){
280             bitinfo1[3] = 5;
281             wordinfo1[2] = 5;
282             wordinfo1[3] = digarr[anode][tb]-(1 << bitinfo1[3]);
283           }
284           if(digarr[anode][tb]>=64 && digarr[anode][tb]<128){
285             bitinfo1[3] = 6;
286             wordinfo1[2] = 6;
287             wordinfo1[3] = digarr[anode][tb]-(1 << bitinfo1[3]);
288           }
289           if(digarr[anode][tb]>=128){
290             bitinfo1[3] = 7;
291             wordinfo1[2] = 7;
292             wordinfo1[3] = digarr[anode][tb]-(1 << bitinfo1[3]);
293           }
294           
295           for(Int_t ie=0;ie<4;ie++){
296             
297             if(flag){      
298               last = first+diff-1;
299               AliBitPacking::PackWord(word2,baseWord,first,last);
300               flag = kFALSE;
301               first = last+1;
302               diff=0;
303             }
304             last = first+bitinfo1[ie]-1;
305             if(first < 30 && last < 30){                  
306               AliBitPacking::PackWord(wordinfo1[ie],baseWord,first,last); 
307               first = last+1;
308             }
309             else{
310               if(first<=29){
311                 UInt_t w = AliBitPacking::UnpackWord(wordinfo1[ie],0,29-first);
312                 AliBitPacking::PackWord(w,baseWord,first,29);
313                 Int_t lb = 29-first+1;
314                 diff = bitinfo1[ie]-lb;
315                 word2 = AliBitPacking::UnpackWord(wordinfo1[ie],lb,lb+diff-1);
316                 flag = kTRUE;
317                 if(anode<256) word = 2;//channel 0 of carlos
318                 else word = 3; //channel 1 of carlos
319                 AliBitPacking::PackWord(word,baseWord,30,31);
320                 fIndex++;
321                 buf[fIndex]=baseWord;
322                 first=0;
323                 last = 0;
324                 baseWord=0;
325                 word = 0;
326               }
327               else{
328                 word2 = wordinfo1[ie];
329                 diff = bitinfo1[ie];
330                 flag = kTRUE;
331                 if(anode<256) word = 2; //channel 0 of carlos
332                 else word = 3; //channel 1 of carlos
333                 AliBitPacking::PackWord(word,baseWord,30,31);
334                 fIndex++;
335                 buf[fIndex]=baseWord;
336                 first=0;
337                 last=0;
338                 baseWord=0;
339                 word = 0;
340               }
341             }
342           }
343           
344         }//END IF
345         
346       }//end loop on tb
347     
348       for(Int_t i=0;i<2;i++){
349         if(flag){      
350           last = first+diff-1;
351           AliBitPacking::PackWord(word2,baseWord,first,last);
352           flag = kFALSE;
353           first = last+1;
354           diff=0;
355         }
356         
357         word = wordinfo2[i];
358         nbit = bitinfo2[i];
359         last = first+nbit-1;
360         if(first < 30 && last < 30){              
361           AliBitPacking::PackWord(word,baseWord,first,last); //3 bit code =1 -> next 18 bits for EOR
362           first = last+1;
363         }
364         
365         else{
366           if(first<=29){
367             UInt_t w = AliBitPacking::UnpackWord(word,0,29-first);
368             AliBitPacking::PackWord(w,baseWord,first,29);
369             Int_t lb = 29-first+1;
370             diff = nbit-lb;        
371             word2 = AliBitPacking::UnpackWord(word,lb,lb+diff-1);
372             flag = kTRUE;
373             if(anode<256) word = 2;
374             else word = 3;
375             AliBitPacking::PackWord(word,baseWord,30,31);
376             fIndex++;
377             buf[fIndex]=baseWord;
378             first=0;
379             last = 0;
380             baseWord=0;
381             if(anode==255){
382               flag=kFALSE;
383               word2=0;
384             }
385           }
386           else{
387             word2 = word;
388             diff = nbit;
389             flag = kTRUE;
390             if(anode<256) word = 2;
391             else word = 3;
392             AliBitPacking::PackWord(word,baseWord,30,31);
393             fIndex++;
394             buf[fIndex]=baseWord;
395             first=0;
396             last=0;
397             baseWord=0;
398             if(anode==255){
399               flag=kFALSE;
400               word2=0;
401             }
402           }
403         }
404       }
405     } //end for
406     
407   }
408   if(fVerbose==2)
409     ftxt.close();
410   return;
411 }//end GetDigitsSDD
412
413 ////////////////////////////////////////////////////////////////////////////////////////
414 //PIXEL 
415 //
416
417 void AliITSDDLRawData::GetDigitsSPD(TClonesArray *ITSdigits,Int_t mod,Int_t ddl, UInt_t *buf){
418   //This method packs the SPD digits in a proper 32 structure
419   //Since data is zero suppressed,the coordinates for the chip having zero digits 
420   //doesn't get listed in the galice.root file. However the SPD format requires 
421   //the empty chip to be written with chip header and chip trailer.
422
423   Int_t chipLow  = AliITSRawStreamSPD::GetOnlineChipFromOffline(mod,0);
424   Int_t chipHigh = AliITSRawStreamSPD::GetOnlineChipFromOffline(mod,159);
425
426   if (chipLow>chipHigh) {chipLow  -= 4; chipHigh += 4;}
427   UInt_t hs = AliITSRawStreamSPD::GetOnlineHSFromOffline(mod);
428
429   // create int map to later hold all digits sorted
430   AliITSIntMap* digMap = new AliITSIntMap();
431
432   UInt_t baseWord=0;
433   Int_t chipHitCount=0;  //Number of Hit in the current chip
434   Int_t previousChip=-1; //Previuos chip respect to the actual aone
435   Int_t ndigits = ITSdigits->GetEntries(); //number of digits in the current module
436   //cout<<"      Number of digits in the current module:"<<ndigits<<" module:"<<mod<<endl;
437
438   AliITSdigit *digs;
439   ofstream ftxt;
440   if(ndigits){
441     //loop over digits
442     if (fVerbose==2)
443       ftxt.open("SPDdigits.txt",ios::app);
444     for (Int_t digit=0;digit<ndigits;digit++){
445       digs = (AliITSdigit*)ITSdigits->UncheckedAt(digit);
446       /*---------------------------------------------------------------------------
447        *     Each module contains 5 read out chips of 256 rows and 32 columns.
448        *     So, the cell number in Z direction varies from 0 to 159.
449        *     ---------------------------------------------------------------------*/
450       Int_t iz=digs->GetCoord1();  // Cell number in Z direction 
451       Int_t ix=digs->GetCoord2();  // Cell number in X direction
452
453       if(fVerbose==2)
454         ftxt<<"DDL:"<<ddl<<" Mod:"<<mod<<" Row:"<<ix<<" Col:"<<iz<<endl;
455       UInt_t dummyDDL,dummyHS,chip,col,row;
456       AliITSRawStreamSPD::OfflineToOnline(mod,iz,ix,dummyDDL,dummyHS,chip,col,row);
457
458       //  insert digit into map...
459       // (reverse order of cols and rows as in real raw data)
460       digMap->Insert(chip*256*32+(31-col)*256+(255-row),row);
461     }
462   }
463
464   UInt_t nrHits = digMap->GetNrEntries();
465   if (nrHits>0) {
466     Int_t chip = 0;
467     for (UInt_t nHit=0; nHit<nrHits; nHit++) {
468       Int_t key = digMap->GetKeyIndex(nHit);
469       chip = key/(256*32);
470       Int_t col = 31 - (key%(256*32))/256;
471       Int_t row = digMap->GetValIndex(nHit);
472
473       if(previousChip==-1) { // first hit
474         //loop over chip without digits 
475         //Even if there aren't digits for a given chip 
476         //the chip header and the chip trailer are stored
477         for (Int_t i=chipLow; i<chip; i++) {
478           WriteChipHeader(i,hs,baseWord);
479           WriteChipTrailer(buf,0,baseWord);
480         }
481         WriteChipHeader(chip,hs,baseWord);
482         WriteHit(buf,row,col,baseWord);
483         chipHitCount++;
484         previousChip=chip;
485       }//end if
486       else{
487         if(previousChip!=(Int_t)chip) {
488           WriteChipTrailer(buf,chipHitCount,baseWord);
489           chipHitCount=0;
490           for(Int_t i=previousChip+1; i<chip; i++) {
491             WriteChipHeader(i,hs,baseWord);
492             WriteChipTrailer(buf,0,baseWord);
493           }//end for
494           WriteChipHeader(chip,hs,baseWord);
495           previousChip=chip;
496         }//end if
497         chipHitCount++;
498         WriteHit(buf,row,col,baseWord);
499       }//end else
500     }//end for
501     //Even if there aren't digits for a given chip 
502     //the chip header and the chip trailer are stored
503     WriteChipTrailer(buf,chipHitCount,baseWord);
504     chipHitCount=0;
505     for(Int_t i=chip+1;i<=chipHigh;i++){
506       WriteChipHeader(i,hs,baseWord);
507       WriteChipTrailer(buf,0,baseWord);
508     }//end for
509   }//end if
510   else{
511     //In this module there aren't digits but
512     //the chip header and chip trailer are stored anyway
513     for(Int_t i=chipLow; i<=chipHigh; i++){
514       WriteChipHeader(i,hs,baseWord);
515       WriteChipTrailer(buf,0,baseWord);
516     }//end for
517   }//end else 
518
519   delete digMap;
520
521   if(fVerbose==2)
522     ftxt.close();
523   return;
524 }//end GetDigitsSPD
525
526 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
527
528 Int_t AliITSDDLRawData::RawDataSPD(TBranch* branch){
529   //This method creates the Raw data files for SPD detectors
530   const Int_t kSize=21000; //256*32*5=40960 max number of digits per module
531   UInt_t buf[kSize];      //One buffer cell can contain 2 digits 
532   fIndex=-1;
533
534   TClonesArray*& digits = * (TClonesArray**) branch->GetAddress();
535   char fileName[15];
536   AliFstream* outfile;         // logical name of the output file 
537   AliRawDataHeaderSim header;
538
539   //loop over DDLs
540   for(Int_t ddl=0;ddl<AliDAQ::NumberOfDdls("ITSSPD");ddl++){
541     strcpy(fileName,AliDAQ::DdlFileName("ITSSPD",ddl)); //The name of the output file.
542     outfile = new AliFstream(fileName);
543     //write Dummy DATA HEADER
544     UInt_t dataHeaderPosition=outfile->Tellp();
545     outfile->WriteBuffer((char*)(&header),sizeof(header));
546     //Loops over Modules of a particular DDL
547     for (Int_t mod=0; mod<AliITSRawStreamSPD::kModulesPerDDL; mod++){
548       Int_t moduleNumber = AliITSRawStreamSPD::GetModuleNumber(ddl, mod);
549       digits->Clear();
550       branch->GetEvent(moduleNumber);
551       //For each Module, buf contains the array of data words in Binary format    
552       //fIndex gives the number of 32 bits words in the buffer for each module
553       GetDigitsSPD(digits,moduleNumber,ddl,buf);
554       outfile->WriteBuffer((char *)buf,((fIndex+1)*sizeof(UInt_t)));
555       for(Int_t i=0;i<(fIndex+1);i++){
556         buf[i]=0;
557       }//end for
558       fIndex=-1;
559     }//end for
560     
561     //Write REAL DATA HEADER
562     UInt_t currentFilePosition=outfile->Tellp();
563     outfile->Seekp(dataHeaderPosition);
564     header.fSize=currentFilePosition-dataHeaderPosition;
565     outfile->WriteBuffer((char*)(&header),sizeof(header));
566     delete outfile;
567   }//end for
568
569   return 0;  
570 }
571
572 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
573
574 Int_t AliITSDDLRawData::RawDataSSD(TBranch* branch){
575
576     //This method creates the Raw data files for SSD detectors
577   const Int_t kSize=1536;//768*2 Number of stripe * number of sides(N and P)
578   UInt_t buf[kSize];      
579   fIndex=-1;
580
581   TClonesArray*& digits = * (TClonesArray**) branch->GetAddress();
582   char fileName[15];
583   AliFstream* outfile;         // logical name of the output file 
584   AliRawDataHeaderSim header;
585
586   //loop over DDLs  
587   for(Int_t i=0;i<AliDAQ::NumberOfDdls("ITSSSD");i++){
588     strcpy(fileName,AliDAQ::DdlFileName("ITSSSD",i)); //The name of the output file.
589     outfile = new AliFstream(fileName);
590     //write Dummy DATA HEADER
591     UInt_t dataHeaderPosition=outfile->Tellp();
592     outfile->WriteBuffer((char*)(&header),sizeof(header));
593     
594     //Loops over Modules of a particular DDL
595     for (Int_t mod=0; mod<AliITSRawStreamSSD::kModulesPerDDL; mod++){
596       Int_t moduleNumber = AliITSRawStreamSSD::GetModuleNumber(i, mod);
597       if(moduleNumber!=-1){
598         digits->Clear();
599         branch->GetEvent(moduleNumber);
600         //For each Module, buf contains the array of data words in Binary format          
601         //fIndex gives the number of 32 bits words in the buffer for each module
602         GetDigitsSSD(digits,mod,moduleNumber,i,buf);
603         outfile->WriteBuffer((char *)buf,((fIndex+1)*sizeof(UInt_t)));
604         fIndex=-1;
605       }//end if
606     }//end for
607
608     //Write REAL DATA HEADER
609     UInt_t currentFilePosition=outfile->Tellp();
610     outfile->Seekp(dataHeaderPosition);
611     header.fSize=currentFilePosition-dataHeaderPosition;
612     header.SetAttribute(0);  // valid data
613     outfile->WriteBuffer((char*)(&header),sizeof(header));
614     delete outfile;
615   }//end for
616
617   return 0;  
618 }
619
620 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
621
622 Int_t AliITSDDLRawData::RawDataSDD(TBranch* branch, AliITSDDLModuleMapSDD* ddlsdd){
623     //This method creates the Raw data files for SDD detectors
624   const Int_t kSize=131072; //256*512
625   UInt_t buf[kSize];      
626   fIndex=-1;
627
628   TClonesArray*& digits = * (TClonesArray**) branch->GetAddress();
629   char fileName[15];
630   AliFstream* outfile;             // logical name of the output file 
631   AliRawDataHeaderSim header;
632   UInt_t skippedword, carlosFooterWord,fifoFooterWord,jitterWord;
633   Bool_t retcode;
634   retcode = AliBitPacking::PackWord(0x3FFFFFFF,carlosFooterWord,0,31);
635   retcode = AliBitPacking::PackWord(0x3F1F1F1F,fifoFooterWord,0,31);
636   if(!fUseCompressedSDDFormat) retcode = AliBitPacking::PackWord(0x7F000000,jitterWord,0,31);
637   else retcode = AliBitPacking::PackWord(0x80000000,jitterWord,0,31);
638  
639   //loop over DDLs  
640   for(Int_t i=0;i<AliDAQ::NumberOfDdls("ITSSDD");i++){
641     strcpy(fileName,AliDAQ::DdlFileName("ITSSDD",i)); //The name of the output file.
642     outfile = new AliFstream(fileName);
643     //write Dummy DATA HEADER
644     UInt_t dataHeaderPosition=outfile->Tellp();
645     outfile->WriteBuffer((char*)(&header),sizeof(header));
646
647
648     //first 1 "dummy" word to be skipped
649     if(!fUseCompressedSDDFormat){
650       retcode = AliBitPacking::PackWord(0xFFFFFFFF,skippedword,0,31);
651       outfile->WriteBuffer((char*)(&skippedword),sizeof(skippedword));
652     }
653
654     //Loops over Modules of a particular DDL
655     for (Int_t mod=0; mod<AliITSRawStreamSDD::kModulesPerDDL; mod++){
656       Int_t moduleNumber = ddlsdd->GetModuleNumber(i, mod);
657       if(moduleNumber!=-1){
658         digits->Clear();
659         branch->GetEvent(moduleNumber);
660
661         //For each Module, buf contains the array of data words in Binary format          
662         //fIndex gives the number of 32 bits words in the buffer for each module
663         //      cout<<"MODULE NUMBER:"<<mapSDD[i][mod]<<endl;
664         if(fUseCompressedSDDFormat){
665           GetDigitsSDDCompressed(digits,mod,buf);
666           outfile->WriteBuffer((char *)buf,((fIndex+1)*sizeof(UInt_t)));
667         }else{
668           GetDigitsSDD(digits,mod,moduleNumber,i,buf);
669           outfile->WriteBuffer((char *)buf,((fIndex+1)*sizeof(UInt_t)));
670           for(Int_t iw=0;iw<3;iw++) outfile->WriteBuffer((char*)(&carlosFooterWord),sizeof(carlosFooterWord));
671         }
672         fIndex=-1;
673       }//end if
674     }//end for
675     // 12 words with FIFO footers (=4 FIFO x 3 3F1F1F1F words per DDL)
676     if(!fUseCompressedSDDFormat){
677       for(Int_t iw=0;iw<12;iw++) outfile->WriteBuffer((char*)(&fifoFooterWord),sizeof(fifoFooterWord));
678     }
679     outfile->WriteBuffer((char*)(&jitterWord),sizeof(jitterWord));      
680     //Write REAL DATA HEADER
681     UInt_t currentFilePosition=outfile->Tellp();
682     outfile->Seekp(dataHeaderPosition);
683     header.fSize=currentFilePosition-dataHeaderPosition;
684     header.SetAttribute(0);  // valid data
685     outfile->WriteBuffer((char*)(&header),sizeof(header));
686     delete outfile;
687   }//end for
688
689   return 0;  
690 }
691
692 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
693
694 void AliITSDDLRawData::WriteChipHeader(Int_t ChipAddr,Int_t halfStave,UInt_t &BaseWord){
695   //This method writes a chip header 
696   //cout<<"Chip: "<<ChipAddr<<" Half Stave module:"<<halfStave<<endl;
697   BaseWord=0;
698   AliBitPacking::PackWord(ChipAddr,BaseWord,16,19);
699   //  At the moment the event count is always 0 (bits 20-26)
700   AliBitPacking::PackWord(0,BaseWord,20,26);
701   AliBitPacking::PackWord(halfStave,BaseWord,27,29);
702   AliBitPacking::PackWord(0x1,BaseWord,30,31);
703   return;
704 }//end WriteChipHeader
705
706 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
707
708 void  AliITSDDLRawData::WriteChipTrailer(UInt_t *buf,Int_t ChipHitCount,UInt_t &BaseWord){
709   //This method writes a chip trailer
710   //pixel fill word
711   if((ChipHitCount%2)!=0){
712     AliBitPacking::PackWord(0xC000,BaseWord,16,31);
713   }
714   AliBitPacking::PackWord(ChipHitCount,BaseWord,0,13);
715   AliBitPacking::PackWord(0x0,BaseWord,14,15);
716   fIndex++;
717   buf[fIndex]=BaseWord;
718   BaseWord=0;
719   return;
720 }//end WriteChipTrailer
721
722 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
723
724 void  AliITSDDLRawData::WriteHit(UInt_t *buf,Int_t RowAddr,Int_t HitAddr,UInt_t &BaseWord){
725   //This method writs an hit
726   if(!BaseWord){
727     AliBitPacking::PackWord(HitAddr,BaseWord,16,20);
728     AliBitPacking::PackWord(RowAddr,BaseWord,21,28);
729     AliBitPacking::PackWord(2,BaseWord,30,31);
730   }//end if
731   else{
732     AliBitPacking::PackWord(HitAddr,BaseWord,0,4);
733     AliBitPacking::PackWord(RowAddr,BaseWord,5,12);
734     AliBitPacking::PackWord(2,BaseWord,14,15);
735     fIndex++;
736     buf[fIndex]=BaseWord;
737     BaseWord=0;
738   }//end else
739   return;
740 }//end WriteHit