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"
37 ClassImp(AliITSDDLRawData)
39 ////////////////////////////////////////////////////////////////////////////////////////
40 AliITSDDLRawData::AliITSDDLRawData():
48 ////////////////////////////////////////////////////////////////////////////////////////
50 AliITSDDLRawData::AliITSDDLRawData(const AliITSDDLRawData &source) :
52 fVerbose(source.fVerbose),
53 fIndex(source.fIndex),
54 fHalfStaveModule(source.fHalfStaveModule){
58 ////////////////////////////////////////////////////////////////////////////////////////
60 AliITSDDLRawData& AliITSDDLRawData::operator=(const AliITSDDLRawData &source){
62 this->fIndex=source.fIndex;
63 this->fHalfStaveModule=source.fHalfStaveModule;
64 this->fVerbose=source.fVerbose;
68 ////////////////////////////////////////////////////////////////////////////////////////
72 void AliITSDDLRawData::GetDigitsSSD(TClonesArray *ITSdigits,Int_t mod,Int_t modR,Int_t ddl,UInt_t *buf){
73 //This method packs the SSD digits in a proper 32 bits structure
79 Int_t ndigits = ITSdigits->GetEntries();
84 ftxt.open("SSDdigits.txt",ios::app);
86 for (Int_t digit=0;digit<ndigits;digit++) {
87 digs = (AliITSdigit*)ITSdigits->UncheckedAt(digit);
88 iz=digs->GetCoord1(); // If iz==0, N side and if iz=1 P side
89 ix=digs->GetCoord2(); // Strip Numbar
90 is=digs->GetCompressedSignal(); // ADC Signal
91 // cout<<" Module:"<<mod-500<<" N/P side:"<<iz<<" Strip Number:"<<ix<<" Amplidute:"<<is-1<<endl;
93 ftxt<<"DDL:"<<ddl<<" Mod: "<<modR<<" N/P: "<<iz<<" Strip: "<<ix<<" Value: "<<is-1<<endl;
96 AliBitPacking::PackWord(word,baseWord,0,9);//ADC data
98 AliBitPacking::PackWord(word,baseWord,10,19);//Strip Number
100 AliBitPacking::PackWord(word,baseWord,20,20);//ADC Channel ID (N or P side)
102 AliBitPacking::PackWord(word,baseWord,21,31);//ADC module ID
104 buf[fIndex]=baseWord;
112 ////////////////////////////////////////////////////////////////////////////////////////
113 //Silicon Drift Detector
116 void AliITSDDLRawData::GetDigitsSDD(TClonesArray *ITSdigits,Int_t mod,Int_t modR,Int_t ddl,UInt_t *buf){
117 //This method packs the SDD digits in a proper 32 bits structure
123 Int_t ndigits = ITSdigits->GetEntries();
126 Int_t digarr[512][256];
127 for(Int_t i=0;i<512;i++){
128 for(Int_t j=0;j<256;j++){
132 //word to select the 12 carlos for the 12 modules
134 if(mod==0) carlosid=805306368;
135 if(mod==1) carlosid=805306369;
136 if(mod==2) carlosid=805306370;
137 if(mod==3) carlosid=805306371;
138 if(mod==4) carlosid=805306372;
139 if(mod==5) carlosid=805306373;
140 if(mod==6) carlosid=805306374;
141 if(mod==7) carlosid=805306375;
142 if(mod==8) carlosid=805306376;
143 if(mod==9) carlosid=805306377;
144 if(mod==10) carlosid=805306378;
145 if(mod==11) carlosid=805306379;
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);
207 for(Int_t tb=0;tb<256;tb++){
208 if(digarr[anode][tb]!=0){
211 AliBitPacking::PackWord(word2,baseWord,first,last);
217 //non lossy compression as it is done in Carlos
218 //(data are already 10to8bit compressed by AMBRA
220 /* if value < 8 value = value - (1 << 2) (word is 2 bit long)
221 if value < 16 value = value - (1 << 3) (word is 3 bit long)
222 if value < 32 value = value - (1 << 4) (word is 4 bit long)
223 if value < 64 value = value - (1 << 5) (word is 5 bit long)
224 if value <128 value = value - (1 << 6) (word is 6 bit long)
225 if value >=128value = value - (1 << 7) (word is 7 bit long)
228 if(digarr[anode][tb]<8){
231 wordinfo1[3] = digarr[anode][tb]-(1 << bitinfo1[3]);
233 if(digarr[anode][tb]>=8 && digarr[anode][tb]<16){
236 wordinfo1[3] = digarr[anode][tb]-(1 << bitinfo1[3]);
238 if(digarr[anode][tb]>=16 && digarr[anode][tb]<32){
241 wordinfo1[3] = digarr[anode][tb]-(1 << bitinfo1[3]);
243 if(digarr[anode][tb]>=32 && digarr[anode][tb]<64){
246 wordinfo1[3] = digarr[anode][tb]-(1 << bitinfo1[3]);
248 if(digarr[anode][tb]>=64 && digarr[anode][tb]<128){
251 wordinfo1[3] = digarr[anode][tb]-(1 << bitinfo1[3]);
253 if(digarr[anode][tb]>=128){
256 wordinfo1[3] = digarr[anode][tb]-(1 << bitinfo1[3]);
259 for(Int_t ie=0;ie<4;ie++){
263 AliBitPacking::PackWord(word2,baseWord,first,last);
268 last = first+bitinfo1[ie]-1;
269 if(first < 30 && last < 30){
270 AliBitPacking::PackWord(wordinfo1[ie],baseWord,first,last);
275 UInt_t w = AliBitPacking::UnpackWord(wordinfo1[ie],0,29-first);
276 AliBitPacking::PackWord(w,baseWord,first,29);
277 Int_t lb = 29-first+1;
278 diff = bitinfo1[ie]-lb;
279 word2 = AliBitPacking::UnpackWord(wordinfo1[ie],lb,lb+diff-1);
281 if(anode<256) word = 2;//channel 0 of carlos
282 else word = 3; //channel 1 of carlos
283 AliBitPacking::PackWord(word,baseWord,30,31);
285 buf[fIndex]=baseWord;
292 word2 = wordinfo1[ie];
295 if(anode<256) word = 2; //channel 0 of carlos
296 else word = 3; //channel 1 of carlos
297 AliBitPacking::PackWord(word,baseWord,30,31);
299 buf[fIndex]=baseWord;
312 for(Int_t i=0;i<2;i++){
315 AliBitPacking::PackWord(word2,baseWord,first,last);
324 if(first < 30 && last < 30){
325 AliBitPacking::PackWord(word,baseWord,first,last); //3 bit code =1 -> next 18 bits for EOR
331 UInt_t w = AliBitPacking::UnpackWord(word,0,29-first);
332 AliBitPacking::PackWord(w,baseWord,first,29);
333 Int_t lb = 29-first+1;
335 word2 = AliBitPacking::UnpackWord(word,lb,lb+diff-1);
337 if(anode<256) word = 2;
339 AliBitPacking::PackWord(word,baseWord,30,31);
341 buf[fIndex]=baseWord;
354 if(anode<256) word = 2;
356 AliBitPacking::PackWord(word,baseWord,30,31);
358 buf[fIndex]=baseWord;
377 ////////////////////////////////////////////////////////////////////////////////////////
381 void AliITSDDLRawData::GetDigitsSPD(TClonesArray *ITSdigits,Int_t mod,Int_t ddl, UInt_t *buf){
382 //This method packs the SPD digits in a proper 32 structure
383 //Since data is zero suppressed,the coordinates for the chip having zero digits
384 //doesn't get listed in the galice.root file. However the SPD format requires
385 //the empty chip to be written with chip header and chip trailer.
386 //The index of the half stave is calculated as (mod/2).
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;
397 fHalfStaveModule++; //It's a private variable used to distinguish between the firs
398 //and the second module of an Half Stave Module
403 ftxt.open("SPDdigits.txt",ios::app);
404 for (Int_t digit=0;digit<ndigits;digit++){
405 digs = (AliITSdigit*)ITSdigits->UncheckedAt(digit);
406 /*---------------------------------------------------------------------------
407 * Each module contains 5 read out chips of 256 rows and 32 columns.
408 * So, the cell number in Z direction varies from 0 to 159. Therefore,
409 * to get the chip address (0 to 4), we need to divide column number by 32.
410 * ---------------------------------------------------------------------*/
411 iz=digs->GetCoord1(); // Cell number in Z direction
412 ix=digs->GetCoord2(); // Cell number in X direction
415 ftxt<<"DDL:"<<ddl<<" Mod:"<<mod<<" Row:"<<ix<<" Col:"<<iz<<endl;
417 if(fHalfStaveModule){
421 if(previousChip==-1){
422 //loop over chip without digits
423 //Even if there aren't digits for a given chip
424 //the chip header and the chip trailer are stored
425 for(Int_t i=0;i<(iz/32);i++){
427 WriteChipHeader(i+5,(mod/2),baseWord);
429 WriteChipHeader(i,(mod/2),baseWord);
430 WriteChipTrailer(buf,chipHitCount,baseWord);
433 WriteChipHeader(chipNo,(mod/2),baseWord);
435 WriteHit(buf,ix,hitRow,baseWord);
439 if(previousChip!=chipNo){
440 WriteChipTrailer(buf,chipHitCount,baseWord);
442 for(Int_t i=previousChip+1;i<chipNo;i++){
443 WriteChipHeader(i,(mod/2),baseWord);
444 WriteChipTrailer(buf,0,baseWord);
447 WriteChipHeader(chipNo,(mod/2),baseWord);
451 WriteHit(buf,ix,hitRow,baseWord);
454 //Even if there aren't digits for a given chip
455 //the chip header and the chip trailer are stored
458 WriteChipTrailer(buf,chipHitCount,baseWord);
460 for(Int_t i=chipNo+1;i<=end;i++){
461 WriteChipHeader(i,(mod/2),baseWord);
462 WriteChipTrailer(buf,0,baseWord);
467 //In this module there aren't digits but
468 //the chip header and chip trailer are store anyway
469 if(fHalfStaveModule){
473 for(Int_t i=0;i<5;i++){
474 WriteChipHeader(chipNo+i,(mod/2),baseWord);
475 WriteChipTrailer(buf,chipHitCount,baseWord);
484 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
486 Int_t AliITSDDLRawData::RawDataSPD(TBranch* branch){
487 //This method creates the Raw data files for SPD detectors
488 const Int_t kSize=21000; //256*32*5=40960 max number of digits per module
489 UInt_t buf[kSize]; //One buffer cell can contain 2 digits
492 TClonesArray*& digits = * (TClonesArray**) branch->GetAddress();
494 ofstream outfile; // logical name of the output file
495 AliRawDataHeader header;
498 for(Int_t i=0;i<AliDAQ::NumberOfDdls("ITSSPD");i++){
499 strcpy(fileName,AliDAQ::DdlFileName("ITSSPD",i)); //The name of the output file.
501 outfile.open(fileName,ios::binary);
503 outfile.open(fileName);
505 //write Dummy DATA HEADER
506 UInt_t dataHeaderPosition=outfile.tellp();
507 outfile.write((char*)(&header),sizeof(header));
508 //Loops over Modules of a particular DDL
509 for (Int_t mod=0; mod<AliITSRawStreamSPD::kModulesPerDDL; mod++){
510 Int_t moduleNumber = AliITSRawStreamSPD::GetModuleNumber(i, mod);
512 branch->GetEvent(moduleNumber);
513 //For each Module, buf contains the array of data words in Binary format
514 //fIndex gives the number of 32 bits words in the buffer for each module
515 GetDigitsSPD(digits,mod,i,buf);
516 outfile.write((char *)buf,((fIndex+1)*sizeof(UInt_t)));
517 for(Int_t i=0;i<(fIndex+1);i++){
523 //Write REAL DATA HEADER
524 UInt_t currentFilePosition=outfile.tellp();
525 outfile.seekp(dataHeaderPosition);
526 header.fSize=currentFilePosition-dataHeaderPosition;
527 outfile.write((char*)(&header),sizeof(header));
534 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
536 Int_t AliITSDDLRawData::RawDataSSD(TBranch* branch){
538 //This method creates the Raw data files for SSD detectors
539 const Int_t kSize=1536;//768*2 Number of stripe * number of sides(N and P)
543 TClonesArray*& digits = * (TClonesArray**) branch->GetAddress();
545 ofstream outfile; // logical name of the output file
546 AliRawDataHeader header;
549 for(Int_t i=0;i<AliDAQ::NumberOfDdls("ITSSSD");i++){
550 strcpy(fileName,AliDAQ::DdlFileName("ITSSSD",i)); //The name of the output file.
552 outfile.open(fileName,ios::binary);
554 outfile.open(fileName);
556 //write Dummy DATA HEADER
557 UInt_t dataHeaderPosition=outfile.tellp();
558 outfile.write((char*)(&header),sizeof(header));
560 //Loops over Modules of a particular DDL
561 for (Int_t mod=0; mod<AliITSRawStreamSSD::kModulesPerDDL; mod++){
562 Int_t moduleNumber = AliITSRawStreamSSD::GetModuleNumber(i, mod);
563 if(moduleNumber!=-1){
565 branch->GetEvent(moduleNumber);
566 //For each Module, buf contains the array of data words in Binary format
567 //fIndex gives the number of 32 bits words in the buffer for each module
568 GetDigitsSSD(digits,mod,moduleNumber,i,buf);
569 outfile.write((char *)buf,((fIndex+1)*sizeof(UInt_t)));
574 //Write REAL DATA HEADER
575 UInt_t currentFilePosition=outfile.tellp();
576 outfile.seekp(dataHeaderPosition);
577 header.fSize=currentFilePosition-dataHeaderPosition;
578 header.SetAttribute(0); // valid data
579 outfile.write((char*)(&header),sizeof(header));
586 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
588 Int_t AliITSDDLRawData::RawDataSDD(TBranch* branch){
589 //This method creates the Raw data files for SDD detectors
590 const Int_t kSize=131072; //256*512
594 TClonesArray*& digits = * (TClonesArray**) branch->GetAddress();
596 ofstream outfile; // logical name of the output file
597 AliRawDataHeader header;
598 UInt_t skippedword = AliBitPacking::PackWord(2,skippedword,0,31);
601 for(Int_t i=0;i<AliDAQ::NumberOfDdls("ITSSDD");i++){
602 strcpy(fileName,AliDAQ::DdlFileName("ITSSDD",i)); //The name of the output file.
604 outfile.open(fileName,ios::binary);
606 outfile.open(fileName);
608 //write Dummy DATA HEADER
609 UInt_t dataHeaderPosition=outfile.tellp();
610 outfile.write((char*)(&header),sizeof(header));
612 //first 9 "dummy" words to be skipped
613 for(Int_t iw=0;iw<9;iw++){
614 outfile.write((char*)&skippedword,sizeof(skippedword));
617 //Loops over Modules of a particular DDL
618 for (Int_t mod=0; mod<AliITSRawStreamSDD::kModulesPerDDL; mod++){
619 Int_t moduleNumber = AliITSRawStreamSDD::GetModuleNumber(i, mod);
620 if(moduleNumber!=-1){
622 branch->GetEvent(moduleNumber);
624 //For each Module, buf contains the array of data words in Binary format
625 //fIndex gives the number of 32 bits words in the buffer for each module
626 // cout<<"MODULE NUMBER:"<<mapSDD[i][mod]<<endl;
627 GetDigitsSDD(digits,mod,moduleNumber,i,buf);
628 outfile.write((char *)buf,((fIndex+1)*sizeof(UInt_t)));
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.write((char*)(&header),sizeof(header));
646 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
648 void AliITSDDLRawData::WriteChipHeader(Int_t ChipAddr,Int_t halfStave,UInt_t &BaseWord){
649 //This method writes a chip header
650 //cout<<"Chip: "<<ChipAddr<<" Half Stave module:"<<halfStave<<endl;
652 AliBitPacking::PackWord(ChipAddr,BaseWord,16,19);
653 // At the moment the event count is always 0 (bits 20-26)
654 AliBitPacking::PackWord(0,BaseWord,20,26);
655 AliBitPacking::PackWord(halfStave,BaseWord,27,29);
656 AliBitPacking::PackWord(0x1,BaseWord,30,31);
658 }//end WriteChipHeader
660 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
662 void AliITSDDLRawData::WriteChipTrailer(UInt_t *buf,Int_t ChipHitCount,UInt_t &BaseWord){
663 //This method writes a chip trailer
665 if((ChipHitCount%2)!=0){
666 AliBitPacking::PackWord(0xC000,BaseWord,16,31);
668 AliBitPacking::PackWord(ChipHitCount,BaseWord,0,13);
669 AliBitPacking::PackWord(0x0,BaseWord,14,15);
671 buf[fIndex]=BaseWord;
674 }//end WriteChipTrailer
676 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
678 void AliITSDDLRawData::WriteHit(UInt_t *buf,Int_t RowAddr,Int_t HitAddr,UInt_t &BaseWord){
679 //This method writs an hit
681 AliBitPacking::PackWord(HitAddr,BaseWord,16,20);
682 AliBitPacking::PackWord(RowAddr,BaseWord,21,28);
683 AliBitPacking::PackWord(2,BaseWord,30,31);
686 AliBitPacking::PackWord(HitAddr,BaseWord,0,4);
687 AliBitPacking::PackWord(RowAddr,BaseWord,5,12);
688 AliBitPacking::PackWord(2,BaseWord,14,15);
690 buf[fIndex]=BaseWord;