1 /**************************************************************************
2 * Copyright(c) 1998-1999, 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 **************************************************************************/
18 ///////////////////////////////////////////////////////////////////////////////
20 // This class provides access to TRD digits in raw data. //
22 // It loops over all TRD digits in the raw data given by the AliRawReader. //
23 // The Next method goes to the next digit. If there are no digits left //
24 // it returns kFALSE. //
25 // Several getters provide information about the current digit. //
27 // Author: C. Lippmann (C.Lippmann@gsi.de) //
29 ///////////////////////////////////////////////////////////////////////////////
32 #include "AliRawReader.h"
33 #include "AliTRDdigitsManager.h"
34 #include "AliTRDdataArrayI.h"
36 #include "AliTRDRawStream.h"
37 #include "AliTRDgeometry.h"
38 #include "AliTRDCommonParam.h"
39 #include "AliTRDcalibDB.h"
41 ClassImp(AliTRDRawStream)
43 //_____________________________________________________________________________
44 AliTRDRawStream::AliTRDRawStream()
100 ,fDigitsManager(NULL)
107 // Default constructor
110 for (Int_t i = 0; i < 540; i++) {
116 //_____________________________________________________________________________
117 AliTRDRawStream::AliTRDRawStream(AliRawReader *rawReader
118 , AliTRDdigitsManager *man
119 , AliTRDdataArrayI *dig)
157 ,fRawReader(rawReader)
183 // Create an object to read TRD raw digits
186 fGeo = new AliTRDgeometry();
188 fRawReader->Select("TRD");
190 for (Int_t i = 0; i < 540; i++) {
196 //_____________________________________________________________________________
197 AliTRDRawStream::AliTRDRawStream(AliRawReader *rawReader)
235 ,fRawReader(rawReader)
253 ,fDigitsManager(NULL)
261 // Create an object to read TRD raw digits
264 fRawReader->Select("TRD");
266 for (Int_t i = 0; i < 540; i++) {
272 //_____________________________________________________________________________
273 AliTRDRawStream::AliTRDRawStream(const AliTRDRawStream& stream)
329 ,fDigitsManager(NULL)
340 AliFatal("Copy constructor not implemented");
344 //_____________________________________________________________________________
345 AliTRDRawStream& AliTRDRawStream::operator = (const AliTRDRawStream&
349 // Assigment operator
352 Fatal("operator =", "assignment operator not implemented");
357 //_____________________________________________________________________________
358 AliTRDRawStream::~AliTRDRawStream()
366 //delete fDigitsManager;
374 //_____________________________________________________________________________
375 Bool_t AliTRDRawStream::SetRawVersion(Int_t rv)
378 // Set the raw data version
381 if ( rv >= 0 && rv <= 2 ) {
390 //_____________________________________________________________________________
391 Bool_t AliTRDRawStream::Next()
394 // This is Bogdans code for reading raw data (offline use only).
395 // It is used for fRawVersion == 0. This funcyion read the next raw digit.
396 // Returns kFALSE if there is no digit left
399 fPrevDetector = fDetector;
401 fPrevColumn = fColumn;
404 AliTRDcalibDB *calibration = AliTRDcalibDB::Instance();
405 if (!calibration) return kFALSE;
407 Int_t timeBins = calibration->GetNumberOfTimeBins();
409 while (fCount >= 0) {
411 while (fCount == 0) { // next detector
414 if (!fRawReader->ReadNextChar(data)) {
418 AliError(Form("wrong flag: %x", data));
423 // read the detector number
424 if (!fRawReader->ReadNextChar(data)) {
425 AliError("Could not read detector number");
430 if (!fRawReader->ReadNextChar(data)) {
431 AliError("Could not read detector number");
435 fDetector += (UInt_t(data) << 8);
437 // read the number of byts
438 if (!fRawReader->ReadNextChar(data)) {
439 AliError("Could not read number of bytes");
444 if (!fRawReader->ReadNextChar(data)) {
445 AliError("Could not read number of bytes");
449 fCount += (UInt_t(data) << 8);
450 if (!fRawReader->ReadNextChar(data)) {
451 AliError("Could not read number of bytes");
455 fCount += (UInt_t(data) << 16);
457 // read the number of active pads
458 if (!fRawReader->ReadNextChar(data)) {
459 AliError("Could not read number of active pads");
464 if (!fRawReader->ReadNextChar(data)) {
465 AliError("Could not read number of active pads");
469 fNPads += (UInt_t(data) << 8);
475 // read the pad row and column number
476 if ((fTime >= timeBins) && (fCount > 2)) {
477 if (!fRawReader->ReadNextChar(data)) {
478 AliError("Could not read row number");
484 if (!fRawReader->ReadNextChar(data)) {
485 AliError("Could not read column number");
494 // read the next data byte
495 if (!fRawReader->ReadNextChar(data)) {
496 AliError("Could not read data");
502 if (data == 0) { // zeros
503 if (!fRawReader->ReadNextChar(data)) {
504 AliError("Could not read time value");
513 fSignal = (UInt_t(data & 0x7F) << 8);
514 if (!fRawReader->ReadNextChar(data)) {
515 AliError("Could not read ADC value");
530 //____________________________________________________________________________
531 Bool_t AliTRDRawStream::ReadAll()
535 // Read all TRD raw data word (32 bits). This is for all FrawVersion > 0.
536 // Return kFALSE if something is not cool
541 AliTRDCommonParam *commonParam = AliTRDCommonParam::Instance();
543 AliError("Could not get common parameters");
547 AliTRDcalibDB *calibration = AliTRDcalibDB::Instance();
549 AliError("Could not get calibration object");
553 UInt_t timeTotal = calibration->GetNumberOfTimeBins();
555 // The number of data words needed for this number of time bins (there
556 // are 3 time bins in one word)
557 UInt_t timeWords = ( timeTotal%3 == 0 ) ? timeTotal/3 : timeTotal/3 + 1;
559 AliDebug(2, Form("Number of Timebins read from CDB: %d", timeTotal));
562 UInt_t TBswitchCtr = 0;
568 fHCHctr1 = fHCHctr2 = 0;
569 fGTUctr1 = fGTUctr2 = -1;
571 AliInfo("Converting TRD raw data to digits ...");
573 while ( 1 ) { // loop over all supermodules
580 // 0) Find first GTU Link Mask and test if we can read data
584 if ( !fRawReader->ReadNextInt( fDataWord ) ) {
585 AliInfo(Form("Finished processing TRD raw data: Found %d Half-Chambers", fHCHctr2));
590 // After reading the first word check for size of this data and get Eq. ID
591 if ( WordCtr == 1 ) {
592 datasize = fRawReader->GetDataSize()/4; // Size of this payload is in 32bit words
593 EqID = fRawReader->GetEquipmentId(); // Get Equipment ID
597 if ( (fDataWord & 0xfffff000) == 0xe0000000 ) {
598 fStatus = 1; // GTU link mask found
603 AliError(Form("Equipment %d: First data word is not GTU Link Mask!", EqID));
608 while ( WordCtr < datasize );
611 // loop over all half chambers in one supermodule
613 while ( WordCtr < datasize ) {
616 // 1) Find end_of_tracklet_marker
618 while ( WordCtr < datasize ) {
620 if ( !fRawReader->ReadNextInt( fDataWord ) ) {
621 AliError("Could not read data");
627 if ( (fDataWord & 0xfffff000) == 0xe0000000 ) {
632 // end_of_tracklet_marker?
633 if ( fDataWord == end_of_tracklet_marker ) {
634 AliDebug(3, "end_of_tracklet_marker found");
640 AliDebug(3, "Tracklet found");
646 if ( fStatus == 0 ) break;
649 // 2) Look for non-end_of_tracklet_marker
652 while ( WordCtr < datasize ) {
654 if ( !fRawReader->ReadNextInt( fDataWord ) ) {
655 AliError("Could not read data");
659 //printf("Word %d: 0x%08x\n", WordCtr, fDataWord);
661 if ( fDataWord != end_of_tracklet_marker ) {
668 if ( fStatus == 0 ) break;
671 // 3) This Word must be Half Chamber Header
674 if ( (fDataWord & 0x00000003) == 1 ) { // HC header
676 // If both half chambers of chamber corresponding to previous header
677 // were already processed, we can compress these digits
678 iDET = fGeo->GetDetector(fLAYER, fSTACK, fSM);
679 if ( fChamberDone[iDET] == 2 ) {
680 fDigits->Compress(1,0);
681 fTrack0->Compress(1,0);
682 fTrack1->Compress(1,0);
683 fTrack2->Compress(1,0);
686 // Read from new HC header the chamber position (fLAYER, fSTACK, fSM)
687 DecodeHCheader(timeTotal);
688 WordCtr += fHCHWords;
689 iDET = fGeo->GetDetector(fLAYER, fSTACK, fSM);
690 fRowMax = commonParam->GetRowMax(fLAYER,fSTACK,fSM);
691 fColMax = commonParam->GetColMax(fROC);
693 // Add a container for the digits of this detector
694 fDigits = fDigitsManager->GetDigits(iDET);
695 fTrack0 = fDigitsManager->GetDictionary(iDET,0);
696 fTrack1 = fDigitsManager->GetDictionary(iDET,1);
697 fTrack2 = fDigitsManager->GetDictionary(iDET,2);
699 fChamberDone[iDET]++;
701 // Allocate memory if it was not already done
702 if (fDigits->GetNtime() == 0) {
703 fDigits->Allocate(fRowMax,fColMax,timeTotal);
704 fTrack0->Allocate(fRowMax,fColMax,timeTotal);
705 fTrack1->Allocate(fRowMax,fColMax,timeTotal);
706 fTrack2->Allocate(fRowMax,fColMax,timeTotal);
717 while ( WordCtr < datasize ) {
719 if ( !fRawReader->ReadNextInt( fDataWord ) ) {
720 AliError("Could not read data");
724 //printf("Word %d: 0x%08x\n", WordCtr, fDataWord);
726 //if ( WordCtr == 4*datasize ) AliInfo(Form("Achtung! WordCtr=%d (%d)", WordCtr, 4*datasize));
728 if( (fDataWord & 0x0000000f) == 0xC ) { // MCM Header
730 if ( fMCM < 0 || fMCM > 15 || fROB < 0 || fROB > 7 ) {
731 AliError("Wrong fMCM or fROB. Skip this data");
734 TBswitch = 3; // For first adc channel we expect: (fDataWord & 3) = 3
736 fADC = fTB = 0; // Reset Counter
737 fStatus = 1; // Now 1 means MCM header is found
741 // End of half-chamber data, finished:
742 if ( fDataWord == end_of_event_marker ) {
747 if ( fStatus == 1 ) { // MCM header is set, ADC data is valid.
750 // Found some data. Decode it now:
752 if ( (fDataWord & 0x00000003) != 0x2 && (fDataWord & 0x00000003) != 0x3) {
753 AliError(Form("Data %08x : Data Word ends neither with 11 nor 10", (Int_t)fDataWord));
757 if ( (fDataWord & 0x00000003) != TBswitch ) { // Next ADC channel found
758 //if ( fTB+1 != timeBins ) AliError(Form("Time bins in data (%d) != DB (%d)", fTB+1, timeBins));
759 TBswitch = (TBswitch & 2) | !(TBswitch & 1); // 0x3 <--> 0x2
765 TBswitchCtr++; // Just read one word
767 // We have only timeTotal time bins
768 if ( TBswitchCtr > timeWords ) {
769 AliError(Form("Data is strange. Already found %d words for this ADC channel", (Int_t)TBswitchCtr));
773 // We have only 21 ADC channels.
775 AliError(Form("Data %08x : Data is strange. fADC is already %d", (Int_t)fDataWord,
780 // There are 18 pads connected to each MCM ADC channels 2...19. The
781 // other channels cross to other MCMs and are good for online tracking
783 if ( fADC > 1 && fADC < (Int_t)fGeo->ADCmax()-1 ) {
785 fCOL = fGeo->GetPadCol(fROB, fMCM, fADC);
787 // We have only 144 Pad Columns
788 if ( fCOL > fColMax-1 || fCOL < 0 ) {
789 AliError(Form("SM%d L%dS%d: Wrong Pad column (%d) fROB=%d, fSIDE=%d, fMCM=%02d", fSM,
790 fLAYER, fSTACK, fCOL, fROB, fSIDE, fMCM ));
793 // Decode 32 bit data words with information from 3 time bins and copy the data
794 fSig[0] = (fDataWord & 0x00000ffc) >> 2;
795 fSig[1] = (fDataWord & 0x003ff000) >> 12;
796 fSig[2] = (fDataWord & 0xffc00000) >> 22;
798 // Print data to screen:
799 AliDebug(5, Form("SM%d L%dS%d: ROB%d MCM=%d ADC=%d (ROW=%d COL=%d): Data %04d %04d %04d\n",
800 fSM, fLAYER, fSTACK, fROB, fMCM, fADC, fROW, fCOL, fSig[0], fSig[1], fSig[2]));
803 if ( fCOL >= 0 && fCOL < fColMax && fROW >= 0 && fROW < fRowMax ) { // A real pad
804 for ( Int_t ctr = 0; ctr <3; ctr++ ) {
805 if ( fTB+ctr < (Int_t)timeTotal ) {
806 fDigits->SetDataUnchecked(fROW, fCOL, fTB+ctr, fSig[ctr]);
807 fTrack0->SetDataUnchecked(fROW, fCOL, fTB+ctr, 0);
808 fTrack1->SetDataUnchecked(fROW, fCOL, fTB+ctr, 0);
809 fTrack2->SetDataUnchecked(fROW, fCOL, fTB+ctr, 0);
827 AliDebug(2, Form("SM%d L%dS%d side %x: Processed %d MCMs.", fSM, fLAYER, fSTACK, fSIDE+10, fMCMHctr2));
829 } // End Half-Chamber loop
831 AliDebug(1, Form("SM%d (Eq %d): Processed %d HC (%d MCMs)", fSM, EqID, fHCHctr1, fMCMHctr1));
833 } // End Super Module loop
839 //============================================================================
840 // Decoding functions
841 //============================================================================
843 //____________________________________________________________________________
844 void AliTRDRawStream::DecodeHCheader(Int_t timeBins)
847 // Decode a half chamber header
850 if ( (fDataWord >> 31) != 1 ) {
852 if ( fRawVersion == 1 ) {
856 AliError(Form("Mismatch between fRawVersion (%d) and HC header signature", fRawVersion));
863 fRVmajor = (fDataWord >> 24) & 0x7f;
864 fRVminor = (fDataWord >> 17) & 0x7f;
865 if ( fRawVersion != fRVmajor ) {
866 AliError(Form("Mismatch between fRawVersion (%d) and fRVmajor from HC header(%d)"
867 ,fRawVersion,fRVmajor));
869 if (fRawVersion == 2 ) {
870 DecodeHCheaderV2(timeBins);
876 AliError(Form(" Unsupported raw version: %d", fRawVersion));
881 //____________________________________________________________________________
882 void AliTRDRawStream::DecodeHCheaderV1()
886 // Decode the HC header (fRawVersion == 1, SM I Commissioning 06)
889 if ( (fDataWord & 0x3) == 1 ) {
891 fDCS = (fDataWord >> 20);
892 fSM = (fDataWord >> 15) & 0x1f;
893 fLAYER = (fDataWord >> 12) & 0x7;
894 fSTACK = (fDataWord >> 9) & 0x7;
895 fSIDE = (fDataWord >> 8) & 0x1;
897 fROC = fGeo->GetDetectorSec(fLAYER, fSTACK);
899 //AliDebug(3, Form("0x%08x: HC header: dcs=%d; sm=%d; roc=%d; side=%x", fDataWord, fDCS, fSM, fROC, fSIDE+10));
908 AliError(Form("0x%08x: Strange HC header: dcs=%d; sm=%d; layer=%d; stack=%d.",
909 fDataWord, fDCS, fSM, fLAYER, fSTACK));
921 AliError(Form("0x%08x: No HC header when it was expected.", fDataWord));
928 //____________________________________________________________________________
929 void AliTRDRawStream::DecodeHCheaderV2(Int_t timeBins)
932 // Decode the HC header (fRawVersion == 2, Full raw production)
936 if ( (fDataWord & 0x3) == 1 ) {
938 fHCHWords = (fDataWord >> 14) & 0x7;
939 fSM = (fDataWord >> 9) & 0x1f;
940 fLAYER = (fDataWord >> 6) & 0x7;
941 fSTACK = (fDataWord >> 3) & 0x7;
942 fSIDE = (fDataWord >> 2) & 0x1;
944 fROC = fGeo->GetDetectorSec(fLAYER, fSTACK);
946 AliDebug(3, Form("0x%08x: HC header: sm=%d; roc=%d; side=%x", fDataWord, fSM, fROC, fSIDE+10));
956 AliError(Form("0x%08x: Strange HC header: dcs=%d; sm=%d; layer=%d; stack=%d.",
957 fDataWord, fDCS, fSM, fLAYER, fSTACK));
966 AliError(Form("0x%08x: No HC header when it was expected.", fDataWord));
970 if ( fHCHWords >= 1 ) {
971 // read one more word
972 if ( !fRawReader->ReadNextInt( fDataWord ) ) {
973 AliError("Could not read data");
976 if ( (fDataWord & 0x3) == 1 ) {
978 fBCctr = (fDataWord >> 16);
979 fPTctr = (fDataWord >> 12) & 0xf;
980 fPTphase = (fDataWord >> 8) & 0xf;
981 fTBins = ((fDataWord >> 2) & 0x3f) + 1;
983 AliDebug(3, Form("0x%08x: HC header 2: BCctr=%d PTctr=%d PTph=%d TB=%d"
984 , fDataWord, fBCctr, fPTctr, fPTphase, fTBins));
986 if( fTBins != timeBins ) {
987 AliError(Form("Mismatch between Number of Time Bins from CDB (%d) and from HC header (%d)"
988 , timeBins, fTBins));
996 if ( fHCHWords >= 2 ) {
997 // read one more word
998 if ( !fRawReader->ReadNextInt( fDataWord ) ) {
999 AliError("Could not read data");
1002 if ( (fDataWord & 0x3) == 1 ) {
1004 Not finished. Next to come:
1017 //____________________________________________________________________________
1018 void AliTRDRawStream::DecodeMCMheader()
1024 if ( fRawVersion >= 1 && fRawVersion <= 2 ) {
1025 DecodeMCMheaderV1();
1029 AliError(Form(" Unsupported raw version: %d", fRawVersion));
1034 //____________________________________________________________________________
1035 void AliTRDRawStream::DecodeMCMheaderV1()
1039 // Decode the MCM header
1042 fMCM = (fDataWord & 0xff000000) >> 24;
1043 fEv = (fDataWord & 0x00fffff0) >> 4;
1048 fROW = fGeo->GetPadRow(fROB, fMCM);
1050 AliDebug(4, Form("0x%08x: SM%d L%dS%d. MCM Header: fROB=%d fMCM=%02d fEv=%02d"
1051 , fDataWord, fSM, fLAYER, fSTACK, fROB, fMCM, fEv));
1053 if ( fROB % 2 == 0 && fSIDE == 1 ) {
1054 AliError(Form("SM%d L%dS%d: Mismatch between fROB (%d) and fSIDE (%d): fMCM=%02d"
1055 , fSM, fLAYER, fSTACK, fROB, fSIDE, fMCM ));
1057 if ( fROB % 2 != 0 && fSIDE == 0 ) {
1058 AliError(Form("SM%d L%dS%d: Mismatch between fROB (%d) and fSIDE (%d): fMCM=%02d"
1059 , fSM, fLAYER, fSTACK, fROB, fSIDE, fMCM ));
1061 if ( (fSTACK == 2 && fROW > 11) || (fSTACK != 2 && fROW > 15) || fROW < 0 ) {
1062 AliError(Form("SM%d L%dS%d: Wrong Padrow (%d) fROB=%d, fSIDE=%d, fMCM=%02d"
1063 , fSM, fLAYER, fSTACK, fROW, fROB, fSIDE, fMCM ));
1071 //____________________________________________________________________________
1072 void AliTRDRawStream::DecodeTracklet()
1078 if ( fRawVersion >= 1 && fRawVersion <= 2 ) {
1083 AliError(Form(" Unsupported raw version: %d", fRawVersion));
1088 //____________________________________________________________________________
1089 void AliTRDRawStream::DecodeTrackletV1()
1093 // Decode the Tracklet
1095 // this function is not tested yet on real tracklets
1098 fTracklPID = (fDataWord >> 24) & 0xff;
1099 fTracklPadRow = (fDataWord >> 20) & 0xf; // 0:15
1100 fTracklDefL = (fDataWord >> 13) & 0x7f;
1101 fTracklPadPos = (fDataWord) & 0x1fff;
1103 fTracklPID /= (Float_t)((1<<8) - 1); // 0:1 (steps of 0.39%)
1104 fTracklDefL = (fTracklDefL - ((1<< 7)-1)/2.) * 140.e-4; // -0.889:0.889cm
1105 fTracklPadPos = (fTracklPadPos - ((1<<13)-1)/2.) * 160.e-4; // -65.528:65.528 cm
1107 AliDebug(4, Form("0x%08x: Tracklet found: SM%d L%dS%d side %x: PadRow=%d PadPos=%f DefL=%f PID=%f"
1108 , fDataWord, fSM, fLAYER, fSTACK, fSIDE+10
1109 , fTracklPadRow, fTracklPadPos, fTracklDefL, fTracklPID));
1111 if( (fSTACK == 2) && (fTracklPadRow >= (Int_t) fGeo->RowmaxC0()) ||
1112 (fSTACK != 2) && (fTracklPadRow >= (Int_t) fGeo->RowmaxC1()) ) {
1113 AliError(Form("Strange Row read from Tracklet Word: %d", fTracklPadRow));
1118 //____________________________________________________________________________
1119 void AliTRDRawStream::DecodeGTUlinkMask()
1125 if ( fRawVersion >= 1 && fRawVersion <= 2 ) {
1126 DecodeGTUlinkMaskV1();
1130 AliError(Form(" Unsupported raw version: %d", fRawVersion));
1135 //____________________________________________________________________________
1136 void AliTRDRawStream::DecodeGTUlinkMaskV1()
1140 // Decode the link masks sent by the GTU. These marke the active optical links
1141 // between GTU and Super Module. Up to now only fully active links are found
1142 // (0xfff = 12 active links).
1145 if ( fGTUctr1 == -1 ) fGTUctr2++;
1148 //printf("fGTUctr=%d",fGTUctr);
1149 //printf("fGTUctr1=%d, fGTUctr2=%d",fGTUctr1, fGTUctr2);
1151 if ( (fGTUctr1 >= 0) && (fGTUctr1 < 5) && (fGTUctr2 >= 0) && (fGTUctr2 < 18) ) {
1152 fGTUlinkMask[fGTUctr2][fGTUctr1] = (fDataWord & 0xfff);
1155 for ( Int_t ctr = 0; ctr < 12; ctr++ ) {
1156 if ( IsGTULinkActive(fGTUctr2, ctr/2, fGTUctr1, ctr%2) ) {
1157 AliDebug(3, Form("SM %2d Stack %d: GTU Link %2d is active!", fGTUctr2, fGTUctr1, ctr));