1 /**************************************************************************
2 * Copyright(c) 1998-2003, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
19 //This class contains all the necessary methods to create the Raw Data
20 //files (slides) for the ITS data challenges for:
26 #include <TClonesArray.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"
36 #include "AliFstream.h"
38 ClassImp(AliITSDDLRawData)
40 ////////////////////////////////////////////////////////////////////////////////////////
41 AliITSDDLRawData::AliITSDDLRawData():
49 ////////////////////////////////////////////////////////////////////////////////////////
51 AliITSDDLRawData::AliITSDDLRawData(const AliITSDDLRawData &source) :
53 fVerbose(source.fVerbose),
54 fIndex(source.fIndex),
55 fHalfStaveModule(source.fHalfStaveModule){
59 ////////////////////////////////////////////////////////////////////////////////////////
61 AliITSDDLRawData& AliITSDDLRawData::operator=(const AliITSDDLRawData &source){
63 this->fIndex=source.fIndex;
64 this->fHalfStaveModule=source.fHalfStaveModule;
65 this->fVerbose=source.fVerbose;
69 ////////////////////////////////////////////////////////////////////////////////////////
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
80 Int_t ndigits = ITSdigits->GetEntries();
85 ftxt.open("SSDdigits.txt",ios::app);
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;
94 ftxt<<"DDL:"<<ddl<<" Mod: "<<modR<<" N/P: "<<iz<<" Strip: "<<ix<<" Value: "<<is-1<<endl;
97 AliBitPacking::PackWord(word,baseWord,0,9);//ADC data
99 AliBitPacking::PackWord(word,baseWord,10,19);//Strip Number
101 AliBitPacking::PackWord(word,baseWord,20,20);//ADC Channel ID (N or P side)
103 AliBitPacking::PackWord(word,baseWord,21,31);//ADC module ID
105 buf[fIndex]=baseWord;
113 ////////////////////////////////////////////////////////////////////////////////////////
114 //Silicon Drift Detector
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
124 Int_t ndigits = ITSdigits->GetEntries();
127 Int_t digarr[512][256];
128 for(Int_t i=0;i<512;i++){
129 for(Int_t j=0;j<256;j++){
133 //word to select the 12 carlos for the 12 modules
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;
149 buf[fIndex]=carlosid;
155 Bool_t flag = kFALSE;
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
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
169 for anode info: 1 3 next 18 bits are for EOR
170 increments the anode value
172 EOR 18 error codes + other info
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
185 ftxt<<"DDL:"<<ddl<<" MID:"<<modR<<" An:"<<iz<<" T:"<<ix<<" A:"<<is<<endl;
186 if (is>255){Error("GetDigitsSDD", "bits words is needed)!!!");}
189 for(Int_t anode=0;anode<512;anode++){
192 AliBitPacking::PackWord(word2,baseWord,first,last);
208 for(Int_t tb=0;tb<256;tb++){
209 if(digarr[anode][tb]!=0){
212 AliBitPacking::PackWord(word2,baseWord,first,last);
218 //non lossy compression as it is done in Carlos
219 //(data are already 10to8bit compressed by AMBRA
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)
229 if(digarr[anode][tb]<8){
232 wordinfo1[3] = digarr[anode][tb]-(1 << bitinfo1[3]);
234 if(digarr[anode][tb]>=8 && digarr[anode][tb]<16){
237 wordinfo1[3] = digarr[anode][tb]-(1 << bitinfo1[3]);
239 if(digarr[anode][tb]>=16 && digarr[anode][tb]<32){
242 wordinfo1[3] = digarr[anode][tb]-(1 << bitinfo1[3]);
244 if(digarr[anode][tb]>=32 && digarr[anode][tb]<64){
247 wordinfo1[3] = digarr[anode][tb]-(1 << bitinfo1[3]);
249 if(digarr[anode][tb]>=64 && digarr[anode][tb]<128){
252 wordinfo1[3] = digarr[anode][tb]-(1 << bitinfo1[3]);
254 if(digarr[anode][tb]>=128){
257 wordinfo1[3] = digarr[anode][tb]-(1 << bitinfo1[3]);
260 for(Int_t ie=0;ie<4;ie++){
264 AliBitPacking::PackWord(word2,baseWord,first,last);
269 last = first+bitinfo1[ie]-1;
270 if(first < 30 && last < 30){
271 AliBitPacking::PackWord(wordinfo1[ie],baseWord,first,last);
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);
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);
286 buf[fIndex]=baseWord;
293 word2 = wordinfo1[ie];
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);
300 buf[fIndex]=baseWord;
313 for(Int_t i=0;i<2;i++){
316 AliBitPacking::PackWord(word2,baseWord,first,last);
325 if(first < 30 && last < 30){
326 AliBitPacking::PackWord(word,baseWord,first,last); //3 bit code =1 -> next 18 bits for EOR
332 UInt_t w = AliBitPacking::UnpackWord(word,0,29-first);
333 AliBitPacking::PackWord(w,baseWord,first,29);
334 Int_t lb = 29-first+1;
336 word2 = AliBitPacking::UnpackWord(word,lb,lb+diff-1);
338 if(anode<256) word = 2;
340 AliBitPacking::PackWord(word,baseWord,30,31);
342 buf[fIndex]=baseWord;
355 if(anode<256) word = 2;
357 AliBitPacking::PackWord(word,baseWord,30,31);
359 buf[fIndex]=baseWord;
378 ////////////////////////////////////////////////////////////////////////////////////////
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).
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;
398 fHalfStaveModule++; //It's a private variable used to distinguish between the firs
399 //and the second module of an Half Stave Module
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
416 ftxt<<"DDL:"<<ddl<<" Mod:"<<mod<<" Row:"<<ix<<" Col:"<<iz<<endl;
418 if(fHalfStaveModule){
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++){
428 WriteChipHeader(i+5,(mod/2),baseWord);
430 WriteChipHeader(i,(mod/2),baseWord);
431 WriteChipTrailer(buf,chipHitCount,baseWord);
434 WriteChipHeader(chipNo,(mod/2),baseWord);
436 WriteHit(buf,ix,hitRow,baseWord);
440 if(previousChip!=chipNo){
441 WriteChipTrailer(buf,chipHitCount,baseWord);
443 for(Int_t i=previousChip+1;i<chipNo;i++){
444 WriteChipHeader(i,(mod/2),baseWord);
445 WriteChipTrailer(buf,0,baseWord);
448 WriteChipHeader(chipNo,(mod/2),baseWord);
452 WriteHit(buf,ix,hitRow,baseWord);
455 //Even if there aren't digits for a given chip
456 //the chip header and the chip trailer are stored
459 WriteChipTrailer(buf,chipHitCount,baseWord);
461 for(Int_t i=chipNo+1;i<=end;i++){
462 WriteChipHeader(i,(mod/2),baseWord);
463 WriteChipTrailer(buf,0,baseWord);
468 //In this module there aren't digits but
469 //the chip header and chip trailer are store anyway
470 if(fHalfStaveModule){
474 for(Int_t i=0;i<5;i++){
475 WriteChipHeader(chipNo+i,(mod/2),baseWord);
476 WriteChipTrailer(buf,chipHitCount,baseWord);
485 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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
493 TClonesArray*& digits = * (TClonesArray**) branch->GetAddress();
495 AliFstream* outfile; // logical name of the output file
496 AliRawDataHeader header;
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);
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++){
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));
531 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
533 Int_t AliITSDDLRawData::RawDataSSD(TBranch* branch){
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)
540 TClonesArray*& digits = * (TClonesArray**) branch->GetAddress();
542 AliFstream* outfile; // logical name of the output file
543 AliRawDataHeader header;
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));
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){
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)));
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));
579 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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
587 TClonesArray*& digits = * (TClonesArray**) branch->GetAddress();
589 AliFstream* outfile; // logical name of the output file
590 AliRawDataHeader header;
591 UInt_t skippedword = AliBitPacking::PackWord(2,skippedword,0,31);
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));
601 //first 9 "dummy" words to be skipped
602 for(Int_t iw=0;iw<9;iw++){
603 outfile->WriteBuffer((char*)(&skippedword),sizeof(skippedword));
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){
611 branch->GetEvent(moduleNumber);
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)));
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));
634 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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;
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);
646 }//end WriteChipHeader
648 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
650 void AliITSDDLRawData::WriteChipTrailer(UInt_t *buf,Int_t ChipHitCount,UInt_t &BaseWord){
651 //This method writes a chip trailer
653 if((ChipHitCount%2)!=0){
654 AliBitPacking::PackWord(0xC000,BaseWord,16,31);
656 AliBitPacking::PackWord(ChipHitCount,BaseWord,0,13);
657 AliBitPacking::PackWord(0x0,BaseWord,14,15);
659 buf[fIndex]=BaseWord;
662 }//end WriteChipTrailer
664 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
666 void AliITSDDLRawData::WriteHit(UInt_t *buf,Int_t RowAddr,Int_t HitAddr,UInt_t &BaseWord){
667 //This method writs an hit
669 AliBitPacking::PackWord(HitAddr,BaseWord,16,20);
670 AliBitPacking::PackWord(RowAddr,BaseWord,21,28);
671 AliBitPacking::PackWord(2,BaseWord,30,31);
674 AliBitPacking::PackWord(HitAddr,BaseWord,0,4);
675 AliBitPacking::PackWord(RowAddr,BaseWord,5,12);
676 AliBitPacking::PackWord(2,BaseWord,14,15);
678 buf[fIndex]=BaseWord;