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 "AliITSRawStreamSSD.h"
35 #include "AliITSIntMap.h"
36 #include "AliBitPacking.h"
38 #include "AliFstream.h"
40 ClassImp(AliITSDDLRawData)
42 ////////////////////////////////////////////////////////////////////////////////////////
43 AliITSDDLRawData::AliITSDDLRawData():
51 ////////////////////////////////////////////////////////////////////////////////////////
53 AliITSDDLRawData::AliITSDDLRawData(const AliITSDDLRawData &source) :
55 fVerbose(source.fVerbose),
56 fIndex(source.fIndex),
57 fHalfStaveModule(source.fHalfStaveModule){
61 ////////////////////////////////////////////////////////////////////////////////////////
63 AliITSDDLRawData& AliITSDDLRawData::operator=(const AliITSDDLRawData &source){
65 this->fIndex=source.fIndex;
66 this->fHalfStaveModule=source.fHalfStaveModule;
67 this->fVerbose=source.fVerbose;
71 ////////////////////////////////////////////////////////////////////////////////////////
75 void AliITSDDLRawData::GetDigitsSSD(TClonesArray *ITSdigits,Int_t mod,Int_t modR,Int_t ddl,UInt_t *buf){
76 //This method packs the SSD digits in a proper 32 bits structure
77 // Revised by Enrico Fragiacomo
83 Int_t ndigits = ITSdigits->GetEntries();
88 ftxt.open("SSDdigits.txt",ios::app);
90 for (Int_t digit=0;digit<ndigits;digit++) {
91 digs = (AliITSdigit*)ITSdigits->UncheckedAt(digit);
92 iz=digs->GetCoord1(); // If iz==0, O side and if iz=1 N side
93 ix=digs->GetCoord2(); // Strip Number
94 is=digs->GetCompressedSignal(); // ADC Signal
95 // cout<<" Module:"<<mod-500<<" N/P side:"<<iz<<" Strip Number:"<<ix<<" Amplidute:"<<is-1<<endl;
96 if(is<0) is = 4096 + is;
98 ftxt<<"DDL:"<<ddl<<" Mod: "<<modR<<" N/P: "<<iz<<" Strip: "<<ix<<" Value: "<<is-1<<endl;
103 AliBitPacking::PackWord(word,baseWord,0,11);//ADC data
105 word = (iz==0) ? ix : 1535-ix ; // on N-side 1535-768 -> 0-767
106 AliBitPacking::PackWord(word,baseWord,12,22);//Strip Number
108 word = mod%12; // ADC-number (12 ADCs per AD module)
109 word += ( word<6 ) ? 0 : 2; // ADC range 0-5 and 8-13
110 AliBitPacking::PackWord(word,baseWord,24,27);//ADC Channel
112 word = mod/12+1; // AD-number (AD module index ranges 1-9)
113 AliBitPacking::PackWord(word,baseWord,28,31);//AD slot
115 buf[fIndex]=baseWord;
123 ////////////////////////////////////////////////////////////////////////////////////////
124 //Silicon Drift Detector
127 void AliITSDDLRawData::GetDigitsSDD(TClonesArray *ITSdigits,Int_t mod,Int_t modR,Int_t ddl,UInt_t *buf){
128 //This method packs the SDD digits in a proper 32 bits structure
134 Int_t ndigits = ITSdigits->GetEntries();
137 Int_t digarr[512][256];
138 for(Int_t i=0;i<512;i++){
139 for(Int_t j=0;j<256;j++){
143 //word to select the 12 carlos for the 12 modules
144 UInt_t carlosid=0x30000000+mod;
147 buf[fIndex]=carlosid;
153 Bool_t flag = kFALSE;
155 Int_t bitinfo1[4] = {3,8,3,7}; //vector with info on bit for timebin info
156 Int_t wordinfo1[4]= {0,0,0,0}; //vector with word info for timebin info
157 Int_t bitinfo2[2] = {3,18}; //vector with info on bit for EOR (end of row) info
158 Int_t wordinfo2[3]= {1,65593}; //vector with word info for anode info
160 /* for time bin info: word n bits meaning
161 0 3 next info is timebin
162 8 3 next word is 8 bit long
163 tb value 8 timebin value
164 n (2->7) 3 next info is n bit long
165 signal n signal value
167 for anode info: 1 3 next 18 bits are for EOR
168 increments the anode value
170 EOR 18 error codes + other info
175 ftxt.open("SDDdigits.txt",ios::app);
176 for (Int_t digit=0;digit<ndigits;digit++) {
177 digs = (AliITSdigit*)ITSdigits->UncheckedAt(digit);
178 iz=digs->GetCoord1(); // Anode
179 ix=digs->GetCoord2(); // Time
180 is=digs->GetCompressedSignal(); // ADC Signal
183 ftxt<<"DDL:"<<ddl<<" MID:"<<modR<<" An:"<<iz<<" T:"<<ix<<" A:"<<is<<endl;
184 if (is>255){Error("GetDigitsSDD", "bits words is needed)!!!");}
187 for(Int_t anode=0;anode<512;anode++){
190 AliBitPacking::PackWord(word2,baseWord,first,last);
203 for(Int_t tb=0;tb<256;tb++){
204 if(digarr[anode][tb]!=0){
207 AliBitPacking::PackWord(word2,baseWord,first,last);
213 //non lossy compression as it is done in Carlos
214 //(data are already 10to8bit compressed by AMBRA
216 /* if value < 8 value = value - (1 << 2) (word is 2 bit long)
217 if value < 16 value = value - (1 << 3) (word is 3 bit long)
218 if value < 32 value = value - (1 << 4) (word is 4 bit long)
219 if value < 64 value = value - (1 << 5) (word is 5 bit long)
220 if value <128 value = value - (1 << 6) (word is 6 bit long)
221 if value >=128value = value - (1 << 7) (word is 7 bit long)
224 if(digarr[anode][tb]<8){
227 wordinfo1[3] = digarr[anode][tb]-(1 << bitinfo1[3]);
229 if(digarr[anode][tb]>=8 && digarr[anode][tb]<16){
232 wordinfo1[3] = digarr[anode][tb]-(1 << bitinfo1[3]);
234 if(digarr[anode][tb]>=16 && digarr[anode][tb]<32){
237 wordinfo1[3] = digarr[anode][tb]-(1 << bitinfo1[3]);
239 if(digarr[anode][tb]>=32 && digarr[anode][tb]<64){
242 wordinfo1[3] = digarr[anode][tb]-(1 << bitinfo1[3]);
244 if(digarr[anode][tb]>=64 && digarr[anode][tb]<128){
247 wordinfo1[3] = digarr[anode][tb]-(1 << bitinfo1[3]);
249 if(digarr[anode][tb]>=128){
252 wordinfo1[3] = digarr[anode][tb]-(1 << bitinfo1[3]);
255 for(Int_t ie=0;ie<4;ie++){
259 AliBitPacking::PackWord(word2,baseWord,first,last);
264 last = first+bitinfo1[ie]-1;
265 if(first < 30 && last < 30){
266 AliBitPacking::PackWord(wordinfo1[ie],baseWord,first,last);
271 UInt_t w = AliBitPacking::UnpackWord(wordinfo1[ie],0,29-first);
272 AliBitPacking::PackWord(w,baseWord,first,29);
273 Int_t lb = 29-first+1;
274 diff = bitinfo1[ie]-lb;
275 word2 = AliBitPacking::UnpackWord(wordinfo1[ie],lb,lb+diff-1);
277 if(anode<256) word = 2;//channel 0 of carlos
278 else word = 3; //channel 1 of carlos
279 AliBitPacking::PackWord(word,baseWord,30,31);
281 buf[fIndex]=baseWord;
288 word2 = wordinfo1[ie];
291 if(anode<256) word = 2; //channel 0 of carlos
292 else word = 3; //channel 1 of carlos
293 AliBitPacking::PackWord(word,baseWord,30,31);
295 buf[fIndex]=baseWord;
308 for(Int_t i=0;i<2;i++){
311 AliBitPacking::PackWord(word2,baseWord,first,last);
320 if(first < 30 && last < 30){
321 AliBitPacking::PackWord(word,baseWord,first,last); //3 bit code =1 -> next 18 bits for EOR
327 UInt_t w = AliBitPacking::UnpackWord(word,0,29-first);
328 AliBitPacking::PackWord(w,baseWord,first,29);
329 Int_t lb = 29-first+1;
331 word2 = AliBitPacking::UnpackWord(word,lb,lb+diff-1);
333 if(anode<256) word = 2;
335 AliBitPacking::PackWord(word,baseWord,30,31);
337 buf[fIndex]=baseWord;
350 if(anode<256) word = 2;
352 AliBitPacking::PackWord(word,baseWord,30,31);
354 buf[fIndex]=baseWord;
373 ////////////////////////////////////////////////////////////////////////////////////////
377 void AliITSDDLRawData::GetDigitsSPD(TClonesArray *ITSdigits,Int_t mod,Int_t ddl, UInt_t *buf){
378 //This method packs the SPD digits in a proper 32 structure
379 //Since data is zero suppressed,the coordinates for the chip having zero digits
380 //doesn't get listed in the galice.root file. However the SPD format requires
381 //the empty chip to be written with chip header and chip trailer.
383 Int_t chipLow = AliITSRawStreamSPD::GetOnlineChipFromOffline(mod,0);
384 Int_t chipHigh = AliITSRawStreamSPD::GetOnlineChipFromOffline(mod,159);
385 if (chipLow>chipHigh) {chipLow -= 4; chipHigh += 4;}
386 UInt_t hs = AliITSRawStreamSPD::GetOnlineHSFromOffline(mod);
388 // create int map to later hold all digits sorted
389 AliITSIntMap* digMap = new AliITSIntMap();
392 Int_t chipHitCount=0; //Number of Hit in the current chip
393 Int_t previousChip=-1; //Previuos chip respect to the actual aone
394 Int_t ndigits = ITSdigits->GetEntries(); //number of digits in the current module
395 //cout<<" Number of digits in the current module:"<<ndigits<<" module:"<<mod<<endl;
402 ftxt.open("SPDdigits.txt",ios::app);
403 for (Int_t digit=0;digit<ndigits;digit++){
404 digs = (AliITSdigit*)ITSdigits->UncheckedAt(digit);
405 /*---------------------------------------------------------------------------
406 * Each module contains 5 read out chips of 256 rows and 32 columns.
407 * So, the cell number in Z direction varies from 0 to 159.
408 * ---------------------------------------------------------------------*/
409 Int_t iz=digs->GetCoord1(); // Cell number in Z direction
410 Int_t ix=digs->GetCoord2(); // Cell number in X direction
413 ftxt<<"DDL:"<<ddl<<" Mod:"<<mod<<" Row:"<<ix<<" Col:"<<iz<<endl;
414 UInt_t dummyDDL,dummyHS,chip,col,row;
415 AliITSRawStreamSPD::OfflineToOnline(mod,iz,ix,dummyDDL,dummyHS,chip,col,row);
417 // insert digit into map...
418 // (reverse order of cols and rows as in real raw data)
419 digMap->Insert(chip*256*32+(32-col)*256+(256-row),row);
423 UInt_t nrHits = digMap->GetNrEntries();
426 for (UInt_t nHit=0; nHit<nrHits; nHit++) {
427 Int_t key = digMap->GetKeyIndex(nHit);
429 Int_t col = 32 - (key%(256*32))/256;
430 Int_t row = digMap->GetValIndex(nHit);
432 if(previousChip==-1) { // first hit
433 //loop over chip without digits
434 //Even if there aren't digits for a given chip
435 //the chip header and the chip trailer are stored
436 for (Int_t i=chipLow; i<chip; i++) {
437 WriteChipHeader(i,hs,baseWord);
438 WriteChipTrailer(buf,0,baseWord);
440 WriteChipHeader(chip,hs,baseWord);
441 WriteHit(buf,row,col,baseWord);
446 if(previousChip!=(Int_t)chip) {
447 WriteChipTrailer(buf,chipHitCount,baseWord);
449 for(Int_t i=previousChip+1; i<chip; i++) {
450 WriteChipHeader(i,hs,baseWord);
451 WriteChipTrailer(buf,0,baseWord);
453 WriteChipHeader(chip,hs,baseWord);
457 WriteHit(buf,row,col,baseWord);
460 //Even if there aren't digits for a given chip
461 //the chip header and the chip trailer are stored
462 WriteChipTrailer(buf,chipHitCount,baseWord);
464 for(Int_t i=chip+1;i<=chipHigh;i++){
465 WriteChipHeader(i,hs,baseWord);
466 WriteChipTrailer(buf,0,baseWord);
470 //In this module there aren't digits but
471 //the chip header and chip trailer are stored anyway
472 for(Int_t i=chipLow; i<=chipHigh; i++){
473 WriteChipHeader(i,hs,baseWord);
474 WriteChipTrailer(buf,0,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 AliRawDataHeaderSim 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 AliRawDataHeaderSim 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 AliRawDataHeaderSim header;
591 UInt_t skippedword, carlosFooterWord,fifoFooterWord,jitterWord;
593 retcode = AliBitPacking::PackWord(0x3FFFFFFF,carlosFooterWord,0,31);
594 retcode = AliBitPacking::PackWord(0x3F1F1F1F,fifoFooterWord,0,31);
595 retcode = AliBitPacking::PackWord(0x7F00000E,jitterWord,0,31);
598 for(Int_t i=0;i<AliDAQ::NumberOfDdls("ITSSDD");i++){
599 strcpy(fileName,AliDAQ::DdlFileName("ITSSDD",i)); //The name of the output file.
600 outfile = new AliFstream(fileName);
601 //write Dummy DATA HEADER
602 UInt_t dataHeaderPosition=outfile->Tellp();
603 outfile->WriteBuffer((char*)(&header),sizeof(header));
606 //first 1 "dummy" word to be skipped
607 retcode = AliBitPacking::PackWord(0xFFFFFFFF,skippedword,0,31);
608 outfile->WriteBuffer((char*)(&skippedword),sizeof(skippedword));
610 //Loops over Modules of a particular DDL
611 for (Int_t mod=0; mod<AliITSRawStreamSDD::kModulesPerDDL; mod++){
612 Int_t moduleNumber = AliITSRawStreamSDD::GetModuleNumber(i, mod);
613 if(moduleNumber!=-1){
615 branch->GetEvent(moduleNumber);
617 //For each Module, buf contains the array of data words in Binary format
618 //fIndex gives the number of 32 bits words in the buffer for each module
619 // cout<<"MODULE NUMBER:"<<mapSDD[i][mod]<<endl;
620 GetDigitsSDD(digits,mod,moduleNumber,i,buf);
621 outfile->WriteBuffer((char *)buf,((fIndex+1)*sizeof(UInt_t)));
622 for(Int_t iw=0;iw<3;iw++) outfile->WriteBuffer((char*)(&carlosFooterWord),sizeof(carlosFooterWord));
626 // 12 words with FIFO footers (=4 FIFO x 3 3F1F1F1F words per DDL)
627 for(Int_t iw=0;iw<12;iw++) outfile->WriteBuffer((char*)(&fifoFooterWord),sizeof(fifoFooterWord));
629 outfile->WriteBuffer((char*)(&jitterWord),sizeof(jitterWord));
631 //Write REAL DATA HEADER
632 UInt_t currentFilePosition=outfile->Tellp();
633 outfile->Seekp(dataHeaderPosition);
634 header.fSize=currentFilePosition-dataHeaderPosition;
635 header.SetAttribute(0); // valid data
636 outfile->WriteBuffer((char*)(&header),sizeof(header));
643 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
645 void AliITSDDLRawData::WriteChipHeader(Int_t ChipAddr,Int_t halfStave,UInt_t &BaseWord){
646 //This method writes a chip header
647 //cout<<"Chip: "<<ChipAddr<<" Half Stave module:"<<halfStave<<endl;
649 AliBitPacking::PackWord(ChipAddr,BaseWord,16,19);
650 // At the moment the event count is always 0 (bits 20-26)
651 AliBitPacking::PackWord(0,BaseWord,20,26);
652 AliBitPacking::PackWord(halfStave,BaseWord,27,29);
653 AliBitPacking::PackWord(0x1,BaseWord,30,31);
655 }//end WriteChipHeader
657 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
659 void AliITSDDLRawData::WriteChipTrailer(UInt_t *buf,Int_t ChipHitCount,UInt_t &BaseWord){
660 //This method writes a chip trailer
662 if((ChipHitCount%2)!=0){
663 AliBitPacking::PackWord(0xC000,BaseWord,16,31);
665 AliBitPacking::PackWord(ChipHitCount,BaseWord,0,13);
666 AliBitPacking::PackWord(0x0,BaseWord,14,15);
668 buf[fIndex]=BaseWord;
671 }//end WriteChipTrailer
673 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
675 void AliITSDDLRawData::WriteHit(UInt_t *buf,Int_t RowAddr,Int_t HitAddr,UInt_t &BaseWord){
676 //This method writs an hit
678 AliBitPacking::PackWord(HitAddr,BaseWord,16,20);
679 AliBitPacking::PackWord(RowAddr,BaseWord,21,28);
680 AliBitPacking::PackWord(2,BaseWord,30,31);
683 AliBitPacking::PackWord(HitAddr,BaseWord,0,4);
684 AliBitPacking::PackWord(RowAddr,BaseWord,5,12);
685 AliBitPacking::PackWord(2,BaseWord,14,15);
687 buf[fIndex]=BaseWord;