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():
52 ////////////////////////////////////////////////////////////////////////////////////////
54 AliITSDDLRawData::AliITSDDLRawData(const AliITSDDLRawData &source) :
56 fVerbose(source.fVerbose),
57 fIndex(source.fIndex),
58 fHalfStaveModule(source.fHalfStaveModule){
62 ////////////////////////////////////////////////////////////////////////////////////////
64 AliITSDDLRawData& AliITSDDLRawData::operator=(const AliITSDDLRawData &source){
66 this->fIndex=source.fIndex;
67 this->fHalfStaveModule=source.fHalfStaveModule;
68 this->fVerbose=source.fVerbose;
72 ////////////////////////////////////////////////////////////////////////////////////////
76 void AliITSDDLRawData::GetDigitsSSD(TClonesArray *ITSdigits,Int_t mod,Int_t modR,Int_t ddl,UInt_t *buf){
77 //This method packs the SSD digits in a proper 32 bits structure
78 // Revised by Enrico Fragiacomo
84 Int_t ndigits = ITSdigits->GetEntries();
89 ftxt.open("SSDdigits.txt",ios::app);
91 for (Int_t digit=0;digit<ndigits;digit++) {
92 digs = (AliITSdigit*)ITSdigits->UncheckedAt(digit);
93 iz=digs->GetCoord1(); // If iz==0, O side and if iz=1 N side
94 ix=digs->GetCoord2(); // Strip Number
95 is=digs->GetCompressedSignal(); // ADC Signal
96 // cout<<" Module:"<<mod-500<<" N/P side:"<<iz<<" Strip Number:"<<ix<<" Amplidute:"<<is-1<<endl;
97 if(is<0) is = 4096 + is;
99 ftxt<<"DDL:"<<ddl<<" Mod: "<<modR<<" N/P: "<<iz<<" Strip: "<<ix<<" Value: "<<is-1<<endl;
104 AliBitPacking::PackWord(word,baseWord,0,11);//ADC data
106 word = (iz==0) ? ix : 1535-ix ; // on N-side 1535-768 -> 0-767
107 AliBitPacking::PackWord(word,baseWord,12,22);//Strip Number
109 word = mod%12; // ADC-number (12 ADCs per AD module)
110 word += ( word<6 ) ? 0 : 2; // ADC range 0-5 and 8-13
111 AliBitPacking::PackWord(word,baseWord,24,27);//ADC Channel
113 word = mod/12+1; // AD-number (AD module index ranges 1-9)
114 AliBitPacking::PackWord(word,baseWord,28,31);//AD slot
116 buf[fIndex]=baseWord;
124 ////////////////////////////////////////////////////////////////////////////////////////
125 //Silicon Drift Detector
128 void AliITSDDLRawData::GetDigitsSDD(TClonesArray *ITSdigits,Int_t mod,Int_t modR,Int_t ddl,UInt_t *buf){
129 //This method packs the SDD digits in a proper 32 bits structure
135 Int_t ndigits = ITSdigits->GetEntries();
138 Int_t digarr[512][256];
139 for(Int_t i=0;i<512;i++){
140 for(Int_t j=0;j<256;j++){
144 //word to select the 12 carlos for the 12 modules
145 UInt_t carlosid=0x30000000+mod;
148 buf[fIndex]=carlosid;
154 Bool_t flag = kFALSE;
156 Int_t bitinfo1[4] = {3,8,3,7}; //vector with info on bit for timebin info
157 Int_t wordinfo1[4]= {0,0,0,0}; //vector with word info for timebin info
158 Int_t bitinfo2[2] = {3,18}; //vector with info on bit for EOR (end of row) info
159 Int_t wordinfo2[3]= {1,65593}; //vector with word info for anode info
161 /* for time bin info: word n bits meaning
162 0 3 next info is timebin
163 8 3 next word is 8 bit long
164 tb value 8 timebin value
165 n (2->7) 3 next info is n bit long
166 signal n signal value
168 for anode info: 1 3 next 18 bits are for EOR
169 increments the anode value
171 EOR 18 error codes + other info
176 ftxt.open("SDDdigits.txt",ios::app);
177 for (Int_t digit=0;digit<ndigits;digit++) {
178 digs = (AliITSdigit*)ITSdigits->UncheckedAt(digit);
179 iz=digs->GetCoord1(); // Anode
180 ix=digs->GetCoord2(); // Time
181 is=digs->GetCompressedSignal(); // ADC Signal
184 ftxt<<"DDL:"<<ddl<<" MID:"<<modR<<" An:"<<iz<<" T:"<<ix<<" A:"<<is<<endl;
185 if (is>255){Error("GetDigitsSDD", "bits words is needed)!!!");}
188 for(Int_t anode=0;anode<512;anode++){
191 AliBitPacking::PackWord(word2,baseWord,first,last);
204 for(Int_t tb=0;tb<256;tb++){
205 if(digarr[anode][tb]!=0){
208 AliBitPacking::PackWord(word2,baseWord,first,last);
214 //non lossy compression as it is done in Carlos
215 //(data are already 10to8bit compressed by AMBRA
217 /* if value < 8 value = value - (1 << 2) (word is 2 bit long)
218 if value < 16 value = value - (1 << 3) (word is 3 bit long)
219 if value < 32 value = value - (1 << 4) (word is 4 bit long)
220 if value < 64 value = value - (1 << 5) (word is 5 bit long)
221 if value <128 value = value - (1 << 6) (word is 6 bit long)
222 if value >=128value = value - (1 << 7) (word is 7 bit long)
225 if(digarr[anode][tb]<8){
228 wordinfo1[3] = digarr[anode][tb]-(1 << bitinfo1[3]);
230 if(digarr[anode][tb]>=8 && digarr[anode][tb]<16){
233 wordinfo1[3] = digarr[anode][tb]-(1 << bitinfo1[3]);
235 if(digarr[anode][tb]>=16 && digarr[anode][tb]<32){
238 wordinfo1[3] = digarr[anode][tb]-(1 << bitinfo1[3]);
240 if(digarr[anode][tb]>=32 && digarr[anode][tb]<64){
243 wordinfo1[3] = digarr[anode][tb]-(1 << bitinfo1[3]);
245 if(digarr[anode][tb]>=64 && digarr[anode][tb]<128){
248 wordinfo1[3] = digarr[anode][tb]-(1 << bitinfo1[3]);
250 if(digarr[anode][tb]>=128){
253 wordinfo1[3] = digarr[anode][tb]-(1 << bitinfo1[3]);
256 for(Int_t ie=0;ie<4;ie++){
260 AliBitPacking::PackWord(word2,baseWord,first,last);
265 last = first+bitinfo1[ie]-1;
266 if(first < 30 && last < 30){
267 AliBitPacking::PackWord(wordinfo1[ie],baseWord,first,last);
272 UInt_t w = AliBitPacking::UnpackWord(wordinfo1[ie],0,29-first);
273 AliBitPacking::PackWord(w,baseWord,first,29);
274 Int_t lb = 29-first+1;
275 diff = bitinfo1[ie]-lb;
276 word2 = AliBitPacking::UnpackWord(wordinfo1[ie],lb,lb+diff-1);
278 if(anode<256) word = 2;//channel 0 of carlos
279 else word = 3; //channel 1 of carlos
280 AliBitPacking::PackWord(word,baseWord,30,31);
282 buf[fIndex]=baseWord;
289 word2 = wordinfo1[ie];
292 if(anode<256) word = 2; //channel 0 of carlos
293 else word = 3; //channel 1 of carlos
294 AliBitPacking::PackWord(word,baseWord,30,31);
296 buf[fIndex]=baseWord;
309 for(Int_t i=0;i<2;i++){
312 AliBitPacking::PackWord(word2,baseWord,first,last);
321 if(first < 30 && last < 30){
322 AliBitPacking::PackWord(word,baseWord,first,last); //3 bit code =1 -> next 18 bits for EOR
328 UInt_t w = AliBitPacking::UnpackWord(word,0,29-first);
329 AliBitPacking::PackWord(w,baseWord,first,29);
330 Int_t lb = 29-first+1;
332 word2 = AliBitPacking::UnpackWord(word,lb,lb+diff-1);
334 if(anode<256) word = 2;
336 AliBitPacking::PackWord(word,baseWord,30,31);
338 buf[fIndex]=baseWord;
351 if(anode<256) word = 2;
353 AliBitPacking::PackWord(word,baseWord,30,31);
355 buf[fIndex]=baseWord;
374 ////////////////////////////////////////////////////////////////////////////////////////
378 void AliITSDDLRawData::GetDigitsSPD(TClonesArray *ITSdigits,Int_t mod,Int_t ddl, UInt_t *buf){
379 //This method packs the SPD digits in a proper 32 structure
380 //Since data is zero suppressed,the coordinates for the chip having zero digits
381 //doesn't get listed in the galice.root file. However the SPD format requires
382 //the empty chip to be written with chip header and chip trailer.
384 Int_t chipLow = AliITSRawStreamSPD::GetOnlineChipFromOffline(mod,0);
385 Int_t chipHigh = AliITSRawStreamSPD::GetOnlineChipFromOffline(mod,159);
387 if (chipLow>chipHigh) {chipLow -= 4; chipHigh += 4;}
388 UInt_t hs = AliITSRawStreamSPD::GetOnlineHSFromOffline(mod);
390 // create int map to later hold all digits sorted
391 AliITSIntMap* digMap = new AliITSIntMap();
394 Int_t chipHitCount=0; //Number of Hit in the current chip
395 Int_t previousChip=-1; //Previuos chip respect to the actual aone
396 Int_t ndigits = ITSdigits->GetEntries(); //number of digits in the current module
397 //cout<<" Number of digits in the current module:"<<ndigits<<" module:"<<mod<<endl;
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.
410 * ---------------------------------------------------------------------*/
411 Int_t iz=digs->GetCoord1(); // Cell number in Z direction
412 Int_t ix=digs->GetCoord2(); // Cell number in X direction
415 ftxt<<"DDL:"<<ddl<<" Mod:"<<mod<<" Row:"<<ix<<" Col:"<<iz<<endl;
416 UInt_t dummyDDL,dummyHS,chip,col,row;
417 AliITSRawStreamSPD::OfflineToOnline(mod,iz,ix,dummyDDL,dummyHS,chip,col,row);
419 // insert digit into map...
420 // (reverse order of cols and rows as in real raw data)
421 digMap->Insert(chip*256*32+(31-col)*256+(255-row),row);
425 UInt_t nrHits = digMap->GetNrEntries();
428 for (UInt_t nHit=0; nHit<nrHits; nHit++) {
429 Int_t key = digMap->GetKeyIndex(nHit);
431 Int_t col = 31 - (key%(256*32))/256;
432 Int_t row = digMap->GetValIndex(nHit);
434 if(previousChip==-1) { // first hit
435 //loop over chip without digits
436 //Even if there aren't digits for a given chip
437 //the chip header and the chip trailer are stored
438 for (Int_t i=chipLow; i<chip; i++) {
439 WriteChipHeader(i,hs,baseWord);
440 WriteChipTrailer(buf,0,baseWord);
442 WriteChipHeader(chip,hs,baseWord);
443 WriteHit(buf,row,col,baseWord);
448 if(previousChip!=(Int_t)chip) {
449 WriteChipTrailer(buf,chipHitCount,baseWord);
451 for(Int_t i=previousChip+1; i<chip; i++) {
452 WriteChipHeader(i,hs,baseWord);
453 WriteChipTrailer(buf,0,baseWord);
455 WriteChipHeader(chip,hs,baseWord);
459 WriteHit(buf,row,col,baseWord);
462 //Even if there aren't digits for a given chip
463 //the chip header and the chip trailer are stored
464 WriteChipTrailer(buf,chipHitCount,baseWord);
466 for(Int_t i=chip+1;i<=chipHigh;i++){
467 WriteChipHeader(i,hs,baseWord);
468 WriteChipTrailer(buf,0,baseWord);
472 //In this module there aren't digits but
473 //the chip header and chip trailer are stored anyway
474 for(Int_t i=chipLow; i<=chipHigh; i++){
475 WriteChipHeader(i,hs,baseWord);
476 WriteChipTrailer(buf,0,baseWord);
487 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
489 Int_t AliITSDDLRawData::RawDataSPD(TBranch* branch){
490 //This method creates the Raw data files for SPD detectors
491 const Int_t kSize=21000; //256*32*5=40960 max number of digits per module
492 UInt_t buf[kSize]; //One buffer cell can contain 2 digits
495 TClonesArray*& digits = * (TClonesArray**) branch->GetAddress();
497 AliFstream* outfile; // logical name of the output file
498 AliRawDataHeaderSim header;
501 for(Int_t ddl=0;ddl<AliDAQ::NumberOfDdls("ITSSPD");ddl++){
502 strcpy(fileName,AliDAQ::DdlFileName("ITSSPD",ddl)); //The name of the output file.
503 outfile = new AliFstream(fileName);
504 //write Dummy DATA HEADER
505 UInt_t dataHeaderPosition=outfile->Tellp();
506 outfile->WriteBuffer((char*)(&header),sizeof(header));
507 //Loops over Modules of a particular DDL
508 for (Int_t mod=0; mod<AliITSRawStreamSPD::kModulesPerDDL; mod++){
509 Int_t moduleNumber = AliITSRawStreamSPD::GetModuleNumber(ddl, mod);
511 branch->GetEvent(moduleNumber);
512 //For each Module, buf contains the array of data words in Binary format
513 //fIndex gives the number of 32 bits words in the buffer for each module
514 GetDigitsSPD(digits,moduleNumber,ddl,buf);
515 outfile->WriteBuffer((char *)buf,((fIndex+1)*sizeof(UInt_t)));
516 for(Int_t i=0;i<(fIndex+1);i++){
522 //Write REAL DATA HEADER
523 UInt_t currentFilePosition=outfile->Tellp();
524 outfile->Seekp(dataHeaderPosition);
525 header.fSize=currentFilePosition-dataHeaderPosition;
526 outfile->WriteBuffer((char*)(&header),sizeof(header));
533 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
535 Int_t AliITSDDLRawData::RawDataSSD(TBranch* branch){
537 //This method creates the Raw data files for SSD detectors
538 const Int_t kSize=1536;//768*2 Number of stripe * number of sides(N and P)
542 TClonesArray*& digits = * (TClonesArray**) branch->GetAddress();
544 AliFstream* outfile; // logical name of the output file
545 AliRawDataHeaderSim header;
548 for(Int_t i=0;i<AliDAQ::NumberOfDdls("ITSSSD");i++){
549 strcpy(fileName,AliDAQ::DdlFileName("ITSSSD",i)); //The name of the output file.
550 outfile = new AliFstream(fileName);
551 //write Dummy DATA HEADER
552 UInt_t dataHeaderPosition=outfile->Tellp();
553 outfile->WriteBuffer((char*)(&header),sizeof(header));
555 //Loops over Modules of a particular DDL
556 for (Int_t mod=0; mod<AliITSRawStreamSSD::kModulesPerDDL; mod++){
557 Int_t moduleNumber = AliITSRawStreamSSD::GetModuleNumber(i, mod);
558 if(moduleNumber!=-1){
560 branch->GetEvent(moduleNumber);
561 //For each Module, buf contains the array of data words in Binary format
562 //fIndex gives the number of 32 bits words in the buffer for each module
563 GetDigitsSSD(digits,mod,moduleNumber,i,buf);
564 outfile->WriteBuffer((char *)buf,((fIndex+1)*sizeof(UInt_t)));
569 //Write REAL DATA HEADER
570 UInt_t currentFilePosition=outfile->Tellp();
571 outfile->Seekp(dataHeaderPosition);
572 header.fSize=currentFilePosition-dataHeaderPosition;
573 header.SetAttribute(0); // valid data
574 outfile->WriteBuffer((char*)(&header),sizeof(header));
581 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
583 Int_t AliITSDDLRawData::RawDataSDD(TBranch* branch, AliITSDDLModuleMapSDD* ddlsdd){
584 //This method creates the Raw data files for SDD detectors
585 const Int_t kSize=131072; //256*512
589 TClonesArray*& digits = * (TClonesArray**) branch->GetAddress();
591 AliFstream* outfile; // logical name of the output file
592 AliRawDataHeaderSim header;
593 UInt_t skippedword, carlosFooterWord,fifoFooterWord,jitterWord;
595 retcode = AliBitPacking::PackWord(0x3FFFFFFF,carlosFooterWord,0,31);
596 retcode = AliBitPacking::PackWord(0x3F1F1F1F,fifoFooterWord,0,31);
597 retcode = AliBitPacking::PackWord(0x7F00000E,jitterWord,0,31);
600 for(Int_t i=0;i<AliDAQ::NumberOfDdls("ITSSDD");i++){
601 strcpy(fileName,AliDAQ::DdlFileName("ITSSDD",i)); //The name of the output file.
602 outfile = new AliFstream(fileName);
603 //write Dummy DATA HEADER
604 UInt_t dataHeaderPosition=outfile->Tellp();
605 outfile->WriteBuffer((char*)(&header),sizeof(header));
608 //first 1 "dummy" word to be skipped
609 retcode = AliBitPacking::PackWord(0xFFFFFFFF,skippedword,0,31);
610 outfile->WriteBuffer((char*)(&skippedword),sizeof(skippedword));
612 //Loops over Modules of a particular DDL
613 for (Int_t mod=0; mod<AliITSRawStreamSDD::kModulesPerDDL; mod++){
614 Int_t moduleNumber = ddlsdd->GetModuleNumber(i, mod);
615 if(moduleNumber!=-1){
617 branch->GetEvent(moduleNumber);
619 //For each Module, buf contains the array of data words in Binary format
620 //fIndex gives the number of 32 bits words in the buffer for each module
621 // cout<<"MODULE NUMBER:"<<mapSDD[i][mod]<<endl;
622 GetDigitsSDD(digits,mod,moduleNumber,i,buf);
623 outfile->WriteBuffer((char *)buf,((fIndex+1)*sizeof(UInt_t)));
624 for(Int_t iw=0;iw<3;iw++) outfile->WriteBuffer((char*)(&carlosFooterWord),sizeof(carlosFooterWord));
628 // 12 words with FIFO footers (=4 FIFO x 3 3F1F1F1F words per DDL)
629 for(Int_t iw=0;iw<12;iw++) outfile->WriteBuffer((char*)(&fifoFooterWord),sizeof(fifoFooterWord));
631 outfile->WriteBuffer((char*)(&jitterWord),sizeof(jitterWord));
633 //Write REAL DATA HEADER
634 UInt_t currentFilePosition=outfile->Tellp();
635 outfile->Seekp(dataHeaderPosition);
636 header.fSize=currentFilePosition-dataHeaderPosition;
637 header.SetAttribute(0); // valid data
638 outfile->WriteBuffer((char*)(&header),sizeof(header));
645 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
647 void AliITSDDLRawData::WriteChipHeader(Int_t ChipAddr,Int_t halfStave,UInt_t &BaseWord){
648 //This method writes a chip header
649 //cout<<"Chip: "<<ChipAddr<<" Half Stave module:"<<halfStave<<endl;
651 AliBitPacking::PackWord(ChipAddr,BaseWord,16,19);
652 // At the moment the event count is always 0 (bits 20-26)
653 AliBitPacking::PackWord(0,BaseWord,20,26);
654 AliBitPacking::PackWord(halfStave,BaseWord,27,29);
655 AliBitPacking::PackWord(0x1,BaseWord,30,31);
657 }//end WriteChipHeader
659 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
661 void AliITSDDLRawData::WriteChipTrailer(UInt_t *buf,Int_t ChipHitCount,UInt_t &BaseWord){
662 //This method writes a chip trailer
664 if((ChipHitCount%2)!=0){
665 AliBitPacking::PackWord(0xC000,BaseWord,16,31);
667 AliBitPacking::PackWord(ChipHitCount,BaseWord,0,13);
668 AliBitPacking::PackWord(0x0,BaseWord,14,15);
670 buf[fIndex]=BaseWord;
673 }//end WriteChipTrailer
675 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
677 void AliITSDDLRawData::WriteHit(UInt_t *buf,Int_t RowAddr,Int_t HitAddr,UInt_t &BaseWord){
678 //This method writs an hit
680 AliBitPacking::PackWord(HitAddr,BaseWord,16,20);
681 AliBitPacking::PackWord(RowAddr,BaseWord,21,28);
682 AliBitPacking::PackWord(2,BaseWord,30,31);
685 AliBitPacking::PackWord(HitAddr,BaseWord,0,4);
686 AliBitPacking::PackWord(RowAddr,BaseWord,5,12);
687 AliBitPacking::PackWord(2,BaseWord,14,15);
689 buf[fIndex]=BaseWord;