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 <Riostream.h>
27 #include <TClonesArray.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"
39 #include "AliFstream.h"
41 ClassImp(AliITSDDLRawData)
43 ////////////////////////////////////////////////////////////////////////////////////////
44 AliITSDDLRawData::AliITSDDLRawData():
48 fUseCompressedSDDFormat(0){
53 ////////////////////////////////////////////////////////////////////////////////////////
55 AliITSDDLRawData::AliITSDDLRawData(const AliITSDDLRawData &source) :
57 fVerbose(source.fVerbose),
58 fIndex(source.fIndex),
59 fHalfStaveModule(source.fHalfStaveModule),
60 fUseCompressedSDDFormat(source.fUseCompressedSDDFormat){
64 ////////////////////////////////////////////////////////////////////////////////////////
66 AliITSDDLRawData& AliITSDDLRawData::operator=(const AliITSDDLRawData &source){
68 this->fIndex=source.fIndex;
69 this->fHalfStaveModule=source.fHalfStaveModule;
70 this->fVerbose=source.fVerbose;
71 this->fUseCompressedSDDFormat=source.fUseCompressedSDDFormat;
75 ////////////////////////////////////////////////////////////////////////////////////////
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
87 Int_t ndigits = ITSdigits->GetEntries();
92 ftxt.open("SSDdigits.txt",ios::app);
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;
102 ftxt<<"DDL:"<<ddl<<" Mod: "<<modR<<" N/P: "<<iz<<" Strip: "<<ix<<" Value: "<<is-1<<endl;
107 AliBitPacking::PackWord(word,baseWord,0,11);//ADC data
109 word = (iz==0) ? ix : 1535-ix ; // on N-side 1535-768 -> 0-767
110 AliBitPacking::PackWord(word,baseWord,12,22);//Strip Number
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
116 word = mod/12+1; // AD-number (AD module index ranges 1-9)
117 AliBitPacking::PackWord(word,baseWord,28,31);//AD slot
119 buf[fIndex]=baseWord;
127 ////////////////////////////////////////////////////////////////////////////////////////
128 //Silicon Drift Detector
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
136 Int_t ndigits = ITSdigits->GetEntries();
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
155 buf[fIndex]=dataWord;
158 UInt_t finalWord=15<<28;
161 buf[fIndex]=finalWord;
164 //______________________________________________________________________
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
173 Int_t ndigits = ITSdigits->GetEntries();
176 Int_t digarr[512][256];
177 for(Int_t i=0;i<512;i++){
178 for(Int_t j=0;j<256;j++){
182 //word to select the 12 carlos for the 12 modules
183 UInt_t carlosid=0x30000000+mod;
186 buf[fIndex]=carlosid;
192 Bool_t flag = kFALSE;
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
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
206 for anode info: 1 3 next 18 bits are for EOR
207 increments the anode value
209 EOR 18 error codes + other info
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
222 ftxt<<"DDL:"<<ddl<<" MID:"<<modR<<" An:"<<iz<<" T:"<<ix<<" A:"<<is<<endl;
223 if (is>255){Error("GetDigitsSDD", "bits words is needed)!!!");}
226 for(Int_t anode=0;anode<512;anode++){
229 AliBitPacking::PackWord(word2,baseWord,first,last);
242 for(Int_t tb=0;tb<256;tb++){
243 if(digarr[anode][tb]!=0){
246 AliBitPacking::PackWord(word2,baseWord,first,last);
252 //non lossy compression as it is done in Carlos
253 //(data are already 10to8bit compressed by AMBRA
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)
263 if(digarr[anode][tb]<8){
266 wordinfo1[3] = digarr[anode][tb]-(1 << bitinfo1[3]);
268 if(digarr[anode][tb]>=8 && digarr[anode][tb]<16){
271 wordinfo1[3] = digarr[anode][tb]-(1 << bitinfo1[3]);
273 if(digarr[anode][tb]>=16 && digarr[anode][tb]<32){
276 wordinfo1[3] = digarr[anode][tb]-(1 << bitinfo1[3]);
278 if(digarr[anode][tb]>=32 && digarr[anode][tb]<64){
281 wordinfo1[3] = digarr[anode][tb]-(1 << bitinfo1[3]);
283 if(digarr[anode][tb]>=64 && digarr[anode][tb]<128){
286 wordinfo1[3] = digarr[anode][tb]-(1 << bitinfo1[3]);
288 if(digarr[anode][tb]>=128){
291 wordinfo1[3] = digarr[anode][tb]-(1 << bitinfo1[3]);
294 for(Int_t ie=0;ie<4;ie++){
298 AliBitPacking::PackWord(word2,baseWord,first,last);
303 last = first+bitinfo1[ie]-1;
304 if(first < 30 && last < 30){
305 AliBitPacking::PackWord(wordinfo1[ie],baseWord,first,last);
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);
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);
320 buf[fIndex]=baseWord;
327 word2 = wordinfo1[ie];
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);
334 buf[fIndex]=baseWord;
347 for(Int_t i=0;i<2;i++){
350 AliBitPacking::PackWord(word2,baseWord,first,last);
359 if(first < 30 && last < 30){
360 AliBitPacking::PackWord(word,baseWord,first,last); //3 bit code =1 -> next 18 bits for EOR
366 UInt_t w = AliBitPacking::UnpackWord(word,0,29-first);
367 AliBitPacking::PackWord(w,baseWord,first,29);
368 Int_t lb = 29-first+1;
370 word2 = AliBitPacking::UnpackWord(word,lb,lb+diff-1);
372 if(anode<256) word = 2;
374 AliBitPacking::PackWord(word,baseWord,30,31);
376 buf[fIndex]=baseWord;
389 if(anode<256) word = 2;
391 AliBitPacking::PackWord(word,baseWord,30,31);
393 buf[fIndex]=baseWord;
412 ////////////////////////////////////////////////////////////////////////////////////////
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.
422 Int_t chipLow = AliITSRawStreamSPD::GetOnlineChipFromOffline(mod,0);
423 Int_t chipHigh = AliITSRawStreamSPD::GetOnlineChipFromOffline(mod,159);
425 if (chipLow>chipHigh) {chipLow -= 4; chipHigh += 4;}
426 UInt_t hs = AliITSRawStreamSPD::GetOnlineHSFromOffline(mod);
428 // create int map to later hold all digits sorted
429 AliITSIntMap* digMap = new AliITSIntMap();
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;
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
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);
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);
463 UInt_t nrHits = digMap->GetNrEntries();
466 for (UInt_t nHit=0; nHit<nrHits; nHit++) {
467 Int_t key = digMap->GetKeyIndex(nHit);
469 Int_t col = 31 - (key%(256*32))/256;
470 Int_t row = digMap->GetValIndex(nHit);
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);
480 WriteChipHeader(chip,hs,baseWord);
481 WriteHit(buf,row,col,baseWord);
486 if(previousChip!=(Int_t)chip) {
487 WriteChipTrailer(buf,chipHitCount,baseWord);
489 for(Int_t i=previousChip+1; i<chip; i++) {
490 WriteChipHeader(i,hs,baseWord);
491 WriteChipTrailer(buf,0,baseWord);
493 WriteChipHeader(chip,hs,baseWord);
497 WriteHit(buf,row,col,baseWord);
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);
504 for(Int_t i=chip+1;i<=chipHigh;i++){
505 WriteChipHeader(i,hs,baseWord);
506 WriteChipTrailer(buf,0,baseWord);
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);
525 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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
533 TClonesArray*& digits = * (TClonesArray**) branch->GetAddress();
535 AliFstream* outfile; // logical name of the output file
536 AliRawDataHeaderSim header;
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);
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++){
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));
571 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
573 Int_t AliITSDDLRawData::RawDataSSD(TBranch* branch){
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)
580 TClonesArray*& digits = * (TClonesArray**) branch->GetAddress();
582 AliFstream* outfile; // logical name of the output file
583 AliRawDataHeaderSim header;
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));
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){
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)));
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));
619 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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
627 TClonesArray*& digits = * (TClonesArray**) branch->GetAddress();
629 AliFstream* outfile; // logical name of the output file
630 AliRawDataHeaderSim header;
631 UInt_t skippedword, carlosFooterWord,fifoFooterWord,jitterWord;
633 retcode = AliBitPacking::PackWord(0x3FFFFFFF,carlosFooterWord,0,31);
634 retcode = AliBitPacking::PackWord(0x3F1F1F1F,fifoFooterWord,0,31);
635 retcode = AliBitPacking::PackWord(0x7F00000E,jitterWord,0,31);
638 for(Int_t i=0;i<AliDAQ::NumberOfDdls("ITSSDD");i++){
639 strcpy(fileName,AliDAQ::DdlFileName("ITSSDD",i)); //The name of the output file.
640 outfile = new AliFstream(fileName);
641 //write Dummy DATA HEADER
642 UInt_t dataHeaderPosition=outfile->Tellp();
643 outfile->WriteBuffer((char*)(&header),sizeof(header));
646 //first 1 "dummy" word to be skipped
647 if(!fUseCompressedSDDFormat){
648 retcode = AliBitPacking::PackWord(0xFFFFFFFF,skippedword,0,31);
649 outfile->WriteBuffer((char*)(&skippedword),sizeof(skippedword));
652 //Loops over Modules of a particular DDL
653 for (Int_t mod=0; mod<AliITSRawStreamSDD::kModulesPerDDL; mod++){
654 Int_t moduleNumber = ddlsdd->GetModuleNumber(i, mod);
655 if(moduleNumber!=-1){
657 branch->GetEvent(moduleNumber);
659 //For each Module, buf contains the array of data words in Binary format
660 //fIndex gives the number of 32 bits words in the buffer for each module
661 // cout<<"MODULE NUMBER:"<<mapSDD[i][mod]<<endl;
662 if(fUseCompressedSDDFormat){
663 GetDigitsSDDCompressed(digits,mod,buf);
664 outfile->WriteBuffer((char *)buf,((fIndex+1)*sizeof(UInt_t)));
666 GetDigitsSDD(digits,mod,moduleNumber,i,buf);
667 outfile->WriteBuffer((char *)buf,((fIndex+1)*sizeof(UInt_t)));
668 for(Int_t iw=0;iw<3;iw++) outfile->WriteBuffer((char*)(&carlosFooterWord),sizeof(carlosFooterWord));
673 // 12 words with FIFO footers (=4 FIFO x 3 3F1F1F1F words per DDL)
674 if(!fUseCompressedSDDFormat){
675 for(Int_t iw=0;iw<12;iw++) outfile->WriteBuffer((char*)(&fifoFooterWord),sizeof(fifoFooterWord));
676 outfile->WriteBuffer((char*)(&jitterWord),sizeof(jitterWord));
678 //Write REAL DATA HEADER
679 UInt_t currentFilePosition=outfile->Tellp();
680 outfile->Seekp(dataHeaderPosition);
681 header.fSize=currentFilePosition-dataHeaderPosition;
682 header.SetAttribute(0); // valid data
683 outfile->WriteBuffer((char*)(&header),sizeof(header));
690 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
692 void AliITSDDLRawData::WriteChipHeader(Int_t ChipAddr,Int_t halfStave,UInt_t &BaseWord){
693 //This method writes a chip header
694 //cout<<"Chip: "<<ChipAddr<<" Half Stave module:"<<halfStave<<endl;
696 AliBitPacking::PackWord(ChipAddr,BaseWord,16,19);
697 // At the moment the event count is always 0 (bits 20-26)
698 AliBitPacking::PackWord(0,BaseWord,20,26);
699 AliBitPacking::PackWord(halfStave,BaseWord,27,29);
700 AliBitPacking::PackWord(0x1,BaseWord,30,31);
702 }//end WriteChipHeader
704 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
706 void AliITSDDLRawData::WriteChipTrailer(UInt_t *buf,Int_t ChipHitCount,UInt_t &BaseWord){
707 //This method writes a chip trailer
709 if((ChipHitCount%2)!=0){
710 AliBitPacking::PackWord(0xC000,BaseWord,16,31);
712 AliBitPacking::PackWord(ChipHitCount,BaseWord,0,13);
713 AliBitPacking::PackWord(0x0,BaseWord,14,15);
715 buf[fIndex]=BaseWord;
718 }//end WriteChipTrailer
720 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
722 void AliITSDDLRawData::WriteHit(UInt_t *buf,Int_t RowAddr,Int_t HitAddr,UInt_t &BaseWord){
723 //This method writs an hit
725 AliBitPacking::PackWord(HitAddr,BaseWord,16,20);
726 AliBitPacking::PackWord(RowAddr,BaseWord,21,28);
727 AliBitPacking::PackWord(2,BaseWord,30,31);
730 AliBitPacking::PackWord(HitAddr,BaseWord,0,4);
731 AliBitPacking::PackWord(RowAddr,BaseWord,5,12);
732 AliBitPacking::PackWord(2,BaseWord,14,15);
734 buf[fIndex]=BaseWord;