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 "AliTRDRawStreamTB.h"
34 #include "AliTRDgeometry.h"
35 #include "AliTRDcalibDB.h"
36 #include "AliTRDfeeParam.h"
38 #include "AliTRDdigitsManager.h"
39 #include "AliTRDdataArrayI.h"
40 #include "AliTRDSignalIndex.h"
44 using namespace AliTRDrawDataUtilsTB;
46 ClassImp(AliTRDRawStreamTB)
48 Bool_t AliTRDRawStreamTB::fgStackIndexBug=kFALSE;
49 Int_t AliTRDRawStreamTB::fgForceRawVersion = -1;
50 Bool_t AliTRDRawStreamTB::fgSupressWarnings=kFALSE;
51 Bool_t AliTRDRawStreamTB::fgRawDataHack=kFALSE;
52 Bool_t AliTRDRawStreamTB::fgExtraDebug=kFALSE;
54 //_____________________________________________________________________________
55 AliTRDRawStreamTB::AliTRDRawStreamTB()
102 ,fMCMWordsExpected(0)
105 ,fRawDigitThreshold(0)
129 ,fEndOfDataFlag(kFALSE)
133 ,fSharedPadsOn(kFALSE)
134 ,fIsPadShared(kFALSE)
138 // Default constructor
141 for (Int_t i = 0; i < 540; i++) {
147 //_____________________________________________________________________________
148 AliTRDRawStreamTB::AliTRDRawStreamTB(AliRawReader *rawReader)
195 ,fMCMWordsExpected(0)
196 ,fRawReader(rawReader)
198 ,fRawDigitThreshold(0)
222 ,fEndOfDataFlag(kFALSE)
226 ,fSharedPadsOn(kFALSE)
227 ,fIsPadShared(kFALSE)
231 // Create an object to read TRD raw digits
234 fRawReader->Select("TRD");
236 for (Int_t i = 0; i < 540; i++) {
242 //_____________________________________________________________________________
243 AliTRDRawStreamTB::AliTRDRawStreamTB(const AliTRDRawStreamTB& stream)
290 ,fMCMWordsExpected(0)
293 ,fRawDigitThreshold(0)
317 ,fEndOfDataFlag(kFALSE)
321 ,fSharedPadsOn(kFALSE)
322 ,fIsPadShared(kFALSE)
329 AliFatal("Copy constructor not implemented");
333 //_____________________________________________________________________________
334 AliTRDRawStreamTB& AliTRDRawStreamTB::operator = (const AliTRDRawStreamTB&
338 // Assigment operator
341 Fatal("operator =", "assignment operator not implemented");
346 //_____________________________________________________________________________
347 AliTRDRawStreamTB::~AliTRDRawStreamTB()
360 //_____________________________________________________________________________
361 void AliTRDRawStreamTB::SetRawReader(AliRawReader *rawReader)
369 fRawReader = rawReader;
373 //_____________________________________________________________________________
374 Bool_t AliTRDRawStreamTB::SetRawVersion(Int_t rv)
377 // Set the raw data version
380 if ( rv >= 0 && rv <= 3 ) {
390 //____________________________________________________________________________
391 Int_t AliTRDRawStreamTB::Init()
397 if (!AliTRDcalibDB::Instance()) {
398 AliError("Could not get calibration object");
403 fGeo = new AliTRDgeometry();
406 fMaxADCgeom = (Int_t)fGeo->ADCmax();
408 fTimeBinsCalib = AliTRDcalibDB::Instance()->GetNumberOfTimeBins();
409 //AliDebug(2, Form("Number of Timebins read from CDB: %d", fTimeBinsCalib));
411 // The number of data words needed for this number of time bins (there
412 // are 3 time bins in one word)
413 fTimeWords = (fTimeBinsCalib-1)/3 + 1;
418 fHCHctr1 = fHCHctr2 = 0;
419 fGTUctr1 = fGTUctr2 = -1;
431 fLastStatus = fkStart;
432 fNextStatus = fkStart;
439 fkBufferSet = kFALSE;
442 fMCMWordsExpected = 0;
444 fEndOfDataFlag = kFALSE;
445 // set all ADC active
446 fNActiveADCs = ChannelsToRead(0x1fffff); // should be 1111 1111 1111 1111 1111 1 = 21 bits active (0-20)
450 //default value overwritten by HC header word h[2]
451 fCommonAdditive = 10;
452 //fSharedPadsOn = kFALSE;
453 //fSharedPadsOn = kTRUE;
454 fIsPadShared = kFALSE;
455 fZeroSuppressed = kFALSE;
460 //____________________________________________________________________________
461 void AliTRDRawStreamTB::SwapOnEndian()
464 // Check the endian and swap if needed
468 char* ptemp = (char*) &itemp;
472 // assume we are at the begining of the buffer!
473 //AliDebug(5, "Swapping.");
474 UInt_t *pbegin = (UInt_t*)fPos;
476 for (UInt_t i = 0; i < fBufSize / AliTRDrawDataUtilsTB::kSizeWord; i++)
478 fDataWord = pbegin + i;
479 iutmp = (((*fDataWord & 0x000000ffU) << 24) | ((*fDataWord & 0x0000ff00U) << 8) |
480 ((*fDataWord & 0x00ff0000U) >> 8) | ((*fDataWord & 0xff000000U) >> 24));
481 // here we override the value in the buffer!
488 //____________________________________________________________________________
489 Int_t AliTRDRawStreamTB::NextData()
492 // Updates the next data word pointer
495 if (fCountBytes + kSizeWord >= fBufSize)
497 fkBufferSet = fRawReader->ReadNextData(fPos);
498 if (fkBufferSet == kTRUE)
500 fBufSize = fRawReader->GetDataSize();
502 fDataWord = (UInt_t*)fPos;
504 if (fgRawDataHack == kTRUE)
507 fCountBytes += 24 * kSizeWord;
508 //fgRawDataHack = kFALSE;
510 ChangeStatus(fkNextSM);
512 AliDebug(3, "NextSM. Buffer is set.");
517 //AliDebug(3, "No more data!");
518 ChangeStatus(fkStop);
519 ChangeStatus(fkNoMoreData);
526 fCountBytes += kSizeWord;
527 fDataWord = (UInt_t*)fPos;
529 AliDebug(10, Form("Current word %d : 0x%x at 0x%x. Count bytes %d of %d",
530 fWordCtr, *fDataWord, fPos, fCountBytes, fBufSize));
531 // if (fCountBytes > 3080000)
532 // printf("Current word %d : 0x%x at 0x%x. Count bytes %d of %d\n",
533 // fWordCtr, *fDataWord, fPos, fCountBytes, fBufSize);
538 //____________________________________________________________________________
539 Int_t AliTRDRawStreamTB::RewindWord()
542 // Updates the next data word pointer
545 if (fkBufferSet == kFALSE)
551 fCountBytes -= kSizeWord;
552 fDataWord = (UInt_t*)fPos;
557 AliWarning("Already at the begining of the buffer");
563 //============================================================================
564 // Decoding functions
565 //============================================================================
567 //____________________________________________________________________________
568 void AliTRDRawStreamTB::DecodeHCheader(Int_t timeBins)
571 // Decode the HC header (fRawVersion == 2, 3, 4, ???)
573 //AliDebug(3, "Here");
576 if ( (*fDataWord & 0x3) == 1 )
579 fHCInfo.DecodeH0(fDataWord);
583 fHCHWords = fHCInfo.fNExtraWords;
585 fLAYER = fHCInfo.fLayer;
586 fSTACK = fHCInfo.fStack;
587 fSIDE = fHCInfo.fSide;
589 fRVmajor = fHCInfo.fRawVMajor;
590 fRVminor = fHCInfo.fRawVMinor;
592 //assuming valid raw version w/o ZS
596 if ( fRawVersion != fRVmajor )
598 if (fgSupressWarnings == kTRUE)
600 if (fgForceRawVersion > 0)
602 fRawVersion = fgForceRawVersion;
606 fRawVersion = fRVmajor;
611 AliWarning("===============================================================================");
612 AliWarning(Form("Mismatch between fRawVersion (%d) and fRVmajor from HC header (%d)"
613 ,fRawVersion,fRVmajor));
614 if (fgForceRawVersion > 0)
616 AliWarning(Form("Forcing fRawVersion to %d", fgForceRawVersion));
617 fRawVersion = fgForceRawVersion;
621 AliWarning(Form("Setting fRawVersion to %d", fRVmajor));
622 fRawVersion = fRVmajor;
624 AliWarning("===============================================================================");
626 }//if raw version missmatch
628 //AliInfo(Form("Raw Version %d", fRawVersion));
631 // check for zero suppression
632 if ( fRawVersion >= 3 || fRawVersion <= 4 ) fZeroSuppressed = kTRUE;
633 else fZeroSuppressed = kFALSE;
635 fROC = fGeo->GetDetectorSec(fLAYER, fSTACK);
637 AliDebug(3, Form("0x%08x: HC header: sm=%d; roc=%d; side=%x", *fDataWord, fSM, fROC, fSIDE+10));
638 AliDebug(5, Form("0x%08x: HC header: expecting %d HC words", *fDataWord, fHCHWords));
649 AliWarning(Form("0x%08x: Strange HC header: dcs=%d; sm=%d; layer=%d; stack=%d.",
650 *fDataWord, fDCS, fSM, fLAYER, fSTACK));
651 fRawReader->AddMajorErrorLog(kHCHeaderCorrupt,Form("0x%08x:dcs=%d; sm=%d; layer=%d; stack=%d.",
652 *fDataWord, fDCS, fSM, fLAYER, fSTACK));
662 AliWarning(Form("0x%08x: No HC header when it was expected.", *fDataWord));
663 fRawReader->AddMajorErrorLog(kHCHeaderMissing,Form("0x%08x", *fDataWord));
667 if ( fHCHWords >= 1 )
669 // read one more word
670 if (NextData() != fkWordOK)
672 AliWarning("Next HC ( H[1] ) word missing");
673 fRawReader->AddMajorErrorLog(kHCWordMissing,"Next HC ( H[1] )word missing");
674 ChangeStatus(fkNextHC);
678 if ( (*fDataWord & 0x3) == 1 )
681 fHCInfo.DecodeH1(fDataWord);
683 fBCctr = fHCInfo.fBunchCrossCounter;
684 fPTctr = fHCInfo.fPreTriggerCounter;
685 fPTphase = fHCInfo.fPreTriggerPhase;
686 fTBins = fHCInfo.fTimeBins;
688 fTimeWords = (fTBins - 1)/3 + 1;
690 AliDebug(3, Form("0x%08x: HC header [1]: BCctr=%d PTctr=%d PTph=%d TB=%d"
691 , *fDataWord, fBCctr, fPTctr, fPTphase, fTBins));
693 if( fTBins != timeBins )
695 if (fgSupressWarnings == kTRUE)
697 fTimeWords = (fTBins - 1)/3 + 1;
701 AliWarning("===============================================================================");
702 AliError(Form("Mismatch between nNTB from CDB (%d) and from HC header (%d)"
703 , timeBins, fTBins));
704 AliWarning(Form("We will use the value from the raw data (HC header): %d", fTBins));
705 AliWarning("===============================================================================");
706 fTimeWords = (fTBins - 1)/3 + 1;
713 if ( fHCHWords >= 2 )
715 // read one more word
716 if (NextData() != fkWordOK)
718 AliWarning("Next HC ( [2] )word missing");
719 fRawReader->AddMajorErrorLog(kHCWordMissing,"Next HC ( [2] ) word missing");
720 ChangeStatus(fkNextHC);
724 if ( (*fDataWord & 0x3) == 1 )
727 fTCon = (*fDataWord >> 29) & 0x1;
728 fPEDon = (*fDataWord >> 31) & 0x1;
729 fGAINon = (*fDataWord >> 30) & 0x1;
730 fXTon = (*fDataWord >> 28) & 0x1;
731 fNonLinOn = (*fDataWord >> 27) & 0x1;
732 fBypass = (*fDataWord >> 26) & 0x1;
734 fCommonAdditive = (*fDataWord >> 20) & 0x3f;
736 AliDebug(3, Form("0x%08x: HC header 3: TC=%d, PED=%d, GAIN=%d, XT=%d, NonLin=%d, Bypass=%d, Add=%d"
737 , fTCon, fPEDon, fGAINon, fXTon, fNonLinOn, fBypass, fCommonAdditive));
744 fTimeWords = (fTBins - 1)/3 + 1;
745 AliDebug(5, Form("Forced tbins %d timewords %d", fTBins, fTimeWords));
749 //____________________________________________________________________________
750 Int_t AliTRDRawStreamTB::ChannelsToRead(Int_t ADCmask)
752 //AliDebug(3, "Here");
753 memset(fADClookup, -1, 32 * sizeof(Int_t));
754 fADClookup[0] = 0; // count entries
755 fADClookup[1] = 2; // index - data start at 2
757 for (Int_t i = 0; i < 30; i++)
760 if ((ADCmask & mask))
762 //AliDebug(9, Form("fDataWord=0x%08x mask=0x%08x i=%d", *fDataWord, mask, i));
763 fADClookup[fADClookup[1]] = i;
769 // test the iteration - comment out for production
770 // begin of comment out section
771 // char schannels[512];
772 // sprintf(schannels, "ADC Channels to read: ");
773 // fADClookup[1] = 2;
774 // while(fADClookup[1] - 2 < fADClookup[0])
776 // AliDebug(9, Form("max=%d index=%d adc=%d", fADClookup[0], fADClookup[1], fADClookup[fADClookup[1]]));
777 // strcat(schannels, Form("%d ", fADClookup[fADClookup[1]]));
780 //AliDebug(9, Form("%s", schannels));
781 //AliDebug(9, Form("ADC channels = %d", fADClookup[0]));
782 // end of comment out section
785 return fADClookup[0];
788 //____________________________________________________________________________
789 void AliTRDRawStreamTB::DecodeTracklet()
793 // Decode the Tracklet
795 // this function is not tested yet on real tracklets
797 //AliDebug(3, "Here");
798 // if ( fRawVersion < 1 || fRawVersion > 3 )
800 // AliError(Form(" Unsupported raw version: %d", fRawVersion));
803 fTracklPID = (*fDataWord >> 24) & 0xff;
804 fTracklPadRow = (*fDataWord >> 20) & 0xf; // 0:15
805 fTracklDefL = (*fDataWord >> 13) & 0x7f;
806 fTracklPadPos = (*fDataWord) & 0x1fff;
808 fTracklPID /= (Float_t)((1<<8) - 1); // 0:1 (steps of 0.39%)
809 fTracklDefL = (fTracklDefL - ((1<< 7)-1)/2.) * 140.e-4; // -0.889:0.889cm
810 fTracklPadPos = (fTracklPadPos - ((1<<13)-1)/2.) * 160.e-4; // -65.528:65.528 cm
812 AliDebug(4, Form("0x%08x: Tracklet found: SM%d L%dS%d side %x: PadRow=%d PadPos=%f DefL=%f PID=%f"
813 , *fDataWord, fSM, fLAYER, fSTACK, fSIDE+10
814 , fTracklPadRow, fTracklPadPos, fTracklDefL, fTracklPID));
816 if( (fSTACK == 2) && (fTracklPadRow >= (Int_t) fGeo->RowmaxC0()) ||
817 (fSTACK != 2) && (fTracklPadRow >= (Int_t) fGeo->RowmaxC1()) ) {
818 AliWarning(Form("Strange Row read from Tracklet Word: %d", fTracklPadRow));
819 fRawReader->AddMajorErrorLog(kTrackletRowMismatch,Form("Word: %d", fTracklPadRow));
824 //____________________________________________________________________________
825 void AliTRDRawStreamTB::DecodeMCMheader()
829 // Decode the MCM header
831 //AliDebug(3, "Here");
833 if ( fRawVersion < 0 || fRawVersion > 3 )
835 AliError(Form(" Unsupported raw version: %d", fRawVersion));
838 fMCMInfo.Decode(fDataWord);
840 fMCM = fMCMInfo.fMCM;
841 fROB = fMCMInfo.fROB;
842 fEv = fMCMInfo.fEvCounter;
846 fROW = AliTRDfeeParam::Instance()->GetPadRowFromMCM(fROB, fMCM);
848 AliDebug(4, Form("0x%08x: SM%d L%dS%d. MCM Header: fROB=%d fMCM=%02d fEv=%02d"
849 , *fDataWord, fSM, fLAYER, fSTACK, fROB, fMCM, fEv));
851 if ( fROB % 2 == 0 && fSIDE == 1 )
853 AliWarning(Form("SM%d L%dS%d: Mismatch between fROB (%d) and fSIDE (%d): fMCM=%02d"
854 , fSM, fLAYER, fSTACK, fROB, fSIDE, fMCM ));
855 fRawReader->AddMajorErrorLog(kROBSideMismatch,Form("SM%d L%dS%d: fROB (%d) fSIDE (%d): fMCM=%02d"
856 , fSM, fLAYER, fSTACK, fROB, fSIDE, fMCM ));
859 if ( fROB % 2 != 0 && fSIDE == 0 )
861 AliWarning(Form("SM%d L%dS%d: Mismatch between fROB (%d) and fSIDE (%d): fMCM=%02d"
862 , fSM, fLAYER, fSTACK, fROB, fSIDE, fMCM ));
863 fRawReader->AddMajorErrorLog(kROBSideMismatch,Form("SM%d L%dS%d: fROB (%d) fSIDE (%d): fMCM=%02d"
864 , fSM, fLAYER, fSTACK, fROB, fSIDE, fMCM ));
867 if ( (fSTACK == 2 && fROW >= fGeo->RowmaxC0()) ||
868 (fSTACK != 2 && fROW >= fGeo->RowmaxC1()) || fROW < 0 )
870 AliWarning(Form("SM%d L%dS%d: Wrong Padrow (%d) fROB=%d, fSIDE=%d, fMCM=%02d"
871 , fSM, fLAYER, fSTACK, fROW, fROB, fSIDE, fMCM ));
872 fRawReader->AddMajorErrorLog(kWrongPadrow,Form("SM%d L%dS%d: Padrow (%d) fROB=%d, fSIDE=%d, fMCM=%02d"
873 , fSM, fLAYER, fSTACK, fROW, fROB, fSIDE, fMCM ));
879 fMCMWordCrt = 1; // MCM header
881 // AdcMask for Zero supressed data
882 if ( fRawVersion == 3 )
884 // read one more word
885 if (NextData() != fkWordOK)
887 AliWarning("MCM ADC mask missing");
888 fRawReader->AddMajorErrorLog(kMCMADCMaskMissing,"Missing");
889 fNextStatus = fkNextHC;
896 for ( Int_t ctr = 0; ctr < fMaxADCgeom; ctr++ ) {
897 if ( (*fDataWord >> (11+ctr)) == 0x1 ) fADCmask[ctr] = kTRUE;
898 else fADCmask[ctr] = kFALSE;
901 if (*fDataWord & 0xf != 0xc)
903 AliWarning(Form("ADC mask does not end with 0xc : 0x%x", *fDataWord));
906 //AliDebug(4, Form("0x%08x: ADC mask", *fDataWord));
907 // 7 MSbits are ignored!
908 UInt_t maskWord = (*fDataWord >> 4) & 0x1fffff;
909 fNActiveADCs = ChannelsToRead(maskWord);
913 if (fRawVersion <= 2)
915 fNActiveADCs = ChannelsToRead(0x1fffff); // should be 1111 1111 1111 1111 1111 1 = 21 bits active (0-20)
916 //directly get it like that:
917 fMCMWordsExpected = 1 + fNActiveADCs * fTBins / 3;
920 if (fRawVersion >= 3)
923 //directly get it like that:
924 fMCMWordsExpected = 1 + 1 + (fTBins * fNActiveADCs) / 3;
927 AliDebug(8, Form("TBins %d fNActiveADCs %d => We expect %d MCM words. We read %d so far.", fTBins, fNActiveADCs, fMCMWordsExpected, fMCMWordCrt));
930 //____________________________________________________________________________
931 Bool_t AliTRDRawStreamTB::DecodeADCWord()
933 // Decode ADC word for any pad
934 // Rearange this function!!!
935 //AliDebug(3, "Here");
936 Bool_t kIsDataOK = kFALSE;
939 // fCOL = fFee->GetPadColFromADC(fROB, fMCM, fADC);
940 if ( fADC >= fMaxADCgeom - 1)
942 // let us guess the Column
943 // take the one before last ADC and shift by one column
944 // later we check if we are inside the limits of the chamber
945 fCOL = AliTRDfeeParam::Instance()->GetPadColFromADC(fROB, fMCM, fADC - 1);
947 //AliDebug(8, Form("-x fADC %d fCOL %d", fADC, fCOL));
948 if (fCOL >= fColMax || fCOL < 0)
950 //AliDebug(8, Form("-xx fADC %d fCOL %d", fADC, fCOL));
958 //AliDebug(8, Form("-y fADC %d fCOL %d", fADC, fCOL));
959 fCOL = AliTRDfeeParam::Instance()->GetPadColFromADC(fROB, fMCM, fADC);
960 if (fCOL >= fColMax || fCOL < 0)
962 //AliDebug(8, Form("-xx fADC %d fCOL %d", fADC, fCOL));
969 // We have only 144 Pad Columns
970 //if ( fCOL > fColMax-1 || fCOL < 0 )
971 if ( fCOL >= 0 && fCOL < fColMax && fROW >= 0 && fROW < fRowMax )
973 // Decode 32 bit data words with information from 3 time bins and copy the data
974 fSig[0] = (*fDataWord & 0x00000ffc) >> 2;
975 fSig[1] = (*fDataWord & 0x003ff000) >> 12;
976 fSig[2] = (*fDataWord & 0xffc00000) >> 22;
978 // Print data to screen:
979 AliDebug(5, Form("DATA : 0x%x tTbin %d", *fDataWord, fTB));
980 AliDebug(5, Form("SM%d L%dS%d: ROB%d MCM=%d ADC=%d (ROW=%d COL=%d): Data %04d %04d %04d\n",
981 fSM, fLAYER, fSTACK, fROB, fMCM, fADC, fROW, fCOL, fSig[0], fSig[1], fSig[2]));
986 AliWarning(Form("SM%d L%dS%d: Wrong Pad column (%d) fROB=%d, fSIDE=%d, fMCM=%02d", fSM,
987 fLAYER, fSTACK, fCOL, fROB, fSIDE, fMCM ));
988 fRawReader->AddMajorErrorLog(kWrongPadcolumn,Form("SM%d L%dS%d: column (%d) fROB=%d, fSIDE=%d, fMCM=%02d", fSM,
989 fLAYER, fSTACK, fCOL, fROB, fSIDE, fMCM ));
996 //____________________________________________________________________________
997 Bool_t AliTRDRawStreamTB::DecodeNextRawWord()
999 //AliDebug(8, Form("-----------------------------------------"));
1000 //AliDebug(8, Form("DATA IS 0x%x", *fDataWord));
1002 //AliDebug(3, "Here");
1003 if ( *fDataWord == kEndOfRawDataMarker )
1004 { // End of half-chamber data, finished
1005 AliDebug(3, "We have reached the eof data. Next should be HC header.");
1007 ChangeStatus(fkNextHC);
1008 fEndOfDataFlag = kTRUE;
1012 if (fADClookup[1] - 2 > fADClookup[0])
1014 AliDebug(8, Form("Overflow Index ADC = %d Max Index = %d Value = %d. Done with ADCs in this MCM. Is this already MCM header 0x%x?",
1015 fADClookup[1] - 2, fADClookup[0], fADClookup[fADClookup[1]], *fDataWord));
1018 fMCMWordsExpected = 0;
1019 AliWarning("Trying to recover. Fall back to DecodeMCM.");
1021 ChangeStatus(fkNextMCM);
1025 if ( (*fDataWord & 0x00000003) != 0x2 && (*fDataWord & 0x00000003) != 0x3)
1027 AliWarning(Form("Data %08x : Data Word ends neither with b11 nor b10", (Int_t)*fDataWord));
1028 fRawReader->AddMinorErrorLog(kDataMaskError,Form("Data %08x", (Int_t)*fDataWord));
1029 fMCMWordsExpected = 0;
1032 AliWarning("Trying to recover. Fall back to DecodeMCM.");
1034 ChangeStatus(fkNextMCM);
1038 if ( (*fDataWord & 0x3) != fLastADCmask || fTbSwitchCtr >= fTimeWords)
1040 fADC = fADClookup[fADClookup[1]];
1041 AliDebug(8, Form("Next fADC = %d at index = %d MCM Word Number: %d Max MCM Words is %d",
1042 fADC, fADClookup[1] - 2, fMCMWordCrt, fMCMWordsExpected));
1043 AliDebug(10, Form("LastMask 0x%x this data 0x%x OR fTbSwitchCtr %d >= fTimeWords %d",
1044 fLastADCmask, (*fDataWord) & 0x3, fTbSwitchCtr, fTimeWords));
1045 if ((*fDataWord & 0x3) != fLastADCmask && fTbSwitchCtr < fTimeWords && fTbSwitchCtr > 0)
1047 AliWarning(Form("Change of mask! But not all words read!"));
1048 AliWarning(Form("Next fADC = %d at index = %d MCM Word Number: %d Max MCM Words is %d",
1049 fADC, fADClookup[1] - 2, fMCMWordCrt, fMCMWordsExpected));
1050 AliWarning(Form("LastMask 0x%x this data 0x%x OR fTbSwitchCtr %d >= fTimeWords %d",
1051 fLastADCmask, (*fDataWord) & 0x3, fTbSwitchCtr, fTimeWords));
1056 ChangeStatus(fkNextData);
1057 fLastADCmask = (*fDataWord) & 0x3;
1063 Bool_t kIsDataOK = kFALSE;
1065 // We have only 21 ADC channels.
1066 if ( fADC > fMaxADCgeom - 1 || fADC < 0)
1068 AliWarning(Form("Data 0x%08x : Data is strange. fADC is %d", (Int_t)*fDataWord, (Int_t)fADC));
1069 AliWarning(Form("fADClookup[0] %d fADClookup[1] %d", fADClookup[0], fADClookup[1]));
1070 fRawReader->AddMinorErrorLog(kADCChannelOverflow,Form("Data %08x : fADC=%d", (Int_t)*fDataWord, (Int_t)fADC));
1074 // There are 18 pads connected to each MCM ADC channels 2...19. The other channels cross to other
1075 // MCMs and are good for online tracking in the MCM.
1076 if (fSharedPadsOn == kTRUE)
1078 kIsDataOK = DecodeADCWord();
1079 if (fADC <= 1 || fADC == fMaxADCgeom - 1)
1081 AliDebug(9, Form("Shared Pad fADC == %d", fADC));
1082 fIsPadShared = kTRUE;
1086 fIsPadShared = kFALSE;
1091 if ( fADC > 1 && fADC < fMaxADCgeom - 1 )
1093 kIsDataOK = DecodeADCWord();
1097 AliDebug(9, Form("fADC not accepted - shared pad %d - DATA : 0x%x", fADC, *fDataWord));
1105 AliDebug(8, Form("We expect %d MCM words. We read %d so far.Current word is 0x%x", fMCMWordsExpected, fMCMWordCrt, *fDataWord));
1107 // all mcm data processed go to next one
1108 if ( fMCMWordCrt >= fMCMWordsExpected)
1110 AliDebug(8, Form("We expect %d MCM words. We read %d so far. Going to next MCM.", fMCMWordsExpected, fMCMWordCrt));
1111 ChangeStatus(fkNextMCM);
1117 //____________________________________________________________________________
1118 Bool_t AliTRDRawStreamTB::DecodeMCM()
1120 fTbSwitch = 3; // For first adc channel we expect: (*fDataWord & 3) = 3
1121 fTbSwitchCtr = 0; //
1122 fADC = fTB = 0; // Reset Counter
1123 fLastADCmask = 0; // Reset
1125 //AliDebug(3, "Here");
1127 //if( ((*fDataWord & 0x80000000) == 0x0) && ((*fDataWord & 0x0000000f) == 0xC) )
1128 //if( ((*fDataWord & 0xf0000000) == 0x80000000) && ((*fDataWord & 0x0000000f) == 0xC) )
1129 if( (*fDataWord & 0x0000000f) == 0xC )
1131 // this changed the status
1133 if ( fMCM < 0 || fMCM > 15 || fROB < 0 || fROB > 7 )
1135 AliWarning("Wrong fMCM or fROB. Skip this data");
1136 fRawReader->AddMajorErrorLog(kWrongMCMorROB,Form("MCM=%d, ROB=%d",fMCM,fROB));
1137 ChangeStatus(fkNextHC);
1141 if (fMCMWordCrt < fMCMWordsExpected)
1143 AliDebug(5, Form("Going to read data."));
1144 ChangeStatus(fkNextData);
1148 //AliDebug(5, Form("! fMCMWordCrt < fMCMWordsExpected"));
1149 ChangeStatus(fkNextMCM);
1151 //fEndOfDataFlag = kFALSE;
1155 if ( *fDataWord == kEndOfRawDataMarker )
1156 { // End of half-chamber data, finished
1157 AliDebug(10, "We have reached the eof data. Next should be HC header.");
1160 ChangeStatus(fkNextHC);
1161 fEndOfDataFlag = kTRUE;
1162 //AliDebug(5, "Expecting MCM header but got End-Of-Raw-Data Marker");
1163 if (fMCMWordsExpected == 0 || fMCMWordsExpected == fMCMWordCrt)
1165 AliDebug(5, Form("Got all mcm words. Returning true."));
1170 AliDebug(5, Form("MCM words missing? %d [expected=%d got=%d] ", fMCMWordsExpected - fMCMWordCrt, fMCMWordsExpected, fMCMWordCrt));
1171 //AliWarning(Form("MCM words missing? %d [expected=%d got=%d] ", fMCMWordsExpected - fMCMWordCrt, fMCMWordsExpected, fMCMWordCrt));
1176 //AliDebug(3, Form("Expecting MCM header but got 0x%x. Going to Next MCM header.", *fDataWord));
1177 AliWarning(Form("Expecting MCM header but got 0x%x. Fall back: Next MCM header.", *fDataWord));
1178 ChangeStatus(fkNextMCM);
1183 //____________________________________________________________________________
1184 Bool_t AliTRDRawStreamTB::DecodeHC()
1186 //AliDebug(3, "Here");
1188 // left overs from the last chamber?
1189 if (*fDataWord == kEndOfRawDataMarker)
1191 AliDebug(3, "We have reached the eof data. Next should be HC header or end of the event!");
1192 ChangeStatus(fkNextHC);
1196 // if (kFALSE == fSMinfo[fiSMx].fTrackletEnable)
1198 // // jump to data decoding - no traklets to expect
1199 // ChangeStatus(fkDecodeHC);
1203 // ChangeStatus(fkNextHC);
1206 if ( fNextStatus == fkNextHC && fSMinfo[fiSMx].fTrackletEnable == kTRUE)
1208 //AliDebug(5, "fkNextHC");
1210 // 1) Find end_of_tracklet_marker if tracklets present!
1214 // endoftrackletmarker?
1215 if ( *fDataWord == kEndOfTrackletMarker )
1217 AliDebug(3, "End-of-tracklet-marker found");
1218 AliDebug(5, Form("Data 0x%x", *fDataWord));
1219 ChangeStatus(fkSeekNonEoTracklet);
1225 //AliDebug(3, "Tracklet found");
1226 AliDebug(5, Form("Tracklet data 0x%x", *fDataWord));
1233 if (fSMinfo[fiSMx].fTrackletEnable == kFALSE)
1234 ChangeStatus(fkDecodeHC);
1237 if (fNextStatus == fkSeekNonEoTracklet)
1239 AliDebug(5, "fkSeekNonEoTracklet");
1242 // 2) Look for non-end_of_tracklet_marker
1244 //printf("Word %d: 0x%08x\n", fWordCtr, *fDataWord);
1246 if ( *fDataWord != kEndOfTrackletMarker )
1248 ChangeStatus(fkDecodeHC);
1249 AliDebug(3, "NON end-of-tracklet-marker found");
1250 AliDebug(5, Form("Data 0x%x", *fDataWord));
1251 //// no do not continue - this should be the hcheader
1255 //just go on and find the non-end_of_tracklet_marker
1260 if ( fNextStatus == fkDecodeHC )
1262 AliDebug(5, "fkDecodeHC");
1265 // 3) This Word must be Half Chamber Header
1267 if ( (*fDataWord & 0xf0000000) == 0x80000000 && (*fDataWord & 0x00000003) == 1 )
1269 AliDebug(5, Form("Is this the HC header? 0x%x", *fDataWord));
1270 DecodeHCheader(fTimeBinsCalib); // This is the new header!
1272 fDET = fGeo->GetDetector(fLAYER, fSTACK, fSM);
1273 fRowMax = fGeo->GetRowMax(fLAYER,fSTACK,fSM);
1274 fColMax = fGeo->GetColMax(fROC);
1278 fChamberDone[fDET]++;
1279 AliDebug(2, Form("-------------- DET %d fChamberDone[fDET]=%d", fDET, fChamberDone[fDET]));
1281 ChangeStatus(fkNextMCM);
1286 AliWarning(Form("Expecting HC header mask but got 0x%x. Fall back: Next HC.", *fDataWord));
1287 ChangeStatus(fkNextHC);
1288 // before we went to //ChangeStatus(fkNextSM);
1294 //____________________________________________________________________________
1295 Bool_t AliTRDRawStreamTB::DecodeGTUlinkMask()
1298 // Decode the link masks sent by the GTU. These marke the active optical links
1299 // between GTU and Super Module. Up to now only fully active links are found
1300 // (0xfff = 12 active links).
1303 if ( (*fDataWord & 0xfffff000) == 0xe0000000 )
1305 if ( fRawVersion < 1 || fRawVersion > 3 )
1307 AliError(Form(" Unsupported raw version: %d", fRawVersion));
1310 if ( fGTUctr1 == -1 ) fGTUctr2++;
1313 if ( (fGTUctr1 >= 0) && (fGTUctr1 < 5) && (fGTUctr2 >= 0) && (fGTUctr2 < 18) )
1315 fGTUlinkMask[fGTUctr2][fGTUctr1] = (*fDataWord & 0xfff);
1318 //AliDebug(5, Form("GTU link mask 0x%x decoded 0x%x", *fDataWord, fGTUlinkMask[fGTUctr2][fGTUctr1]));
1325 // //____________________________________________________________________________
1326 void AliTRDRawStreamTB::ChangeStatus(Int_t kstat)
1328 fLastStatus = fNextStatus;
1329 fNextStatus = kstat;
1332 //____________________________________________________________________________
1333 Bool_t AliTRDRawStreamTB::DecodeSM()
1342 Int_t status = DecodeHeadingInfo();
1343 if (status == fkWordOK)
1345 ChangeStatus(fkNextHC);
1350 AliWarning(Form("Decoding SM info failed. Fall back: None. Stop.", fEqID));
1351 fRawReader->AddMajorErrorLog(kGTULinkMaskMissing,Form("Equipment %d",fEqID));
1352 ChangeStatus(fkStop);
1358 //____________________________________________________________________________
1359 Bool_t AliTRDRawStreamTB::Next()
1362 // Updates the next data word pointer
1365 if (fNextStatus == fkStart)
1370 while (fNextStatus != fkStop && fNextStatus != fkNoMoreData)
1374 switch (fNextStatus)
1378 if (DecodeNextRawWord() == kTRUE)
1383 AliWarning(Form("Invalid time bin %d. sm %d det %d rob %d col %d row %d mcm=%d adc=%d ", fTB-3, fSM, fDET, fROB, fCOL, fROW, fMCM, fADC));
1384 AliWarning(Form("max=%d index=%d adc=%d", fADClookup[0], fADClookup[1], fADClookup[fADClookup[1]-1]));
1386 if (fSig[0] > fRawDigitThreshold || fSig[1] > fRawDigitThreshold || fSig[2] > fRawDigitThreshold)
1392 if (DecodeMCM() == kFALSE)
1393 AliWarning(Form("Decode MCM unsuccessfull. Current Word 0x%x at pos 0x%x", *fDataWord, fPos));
1396 case fkSeekNonEoTracklet:
1399 if (DecodeHC() == kFALSE)
1401 if (*fDataWord == kEndOfRawDataMarker)
1403 AliDebug(2, Form("End of data at 0x%x", fPos));
1407 AliWarning(Form("Decode HC unsuccessfull. Current Word 0x%x at pos 0x%x", *fDataWord, fPos));
1413 if (DecodeSM() == kFALSE)
1414 AliWarning(Form("Decode SM unsuccessfull. Current Word 0x%x at pos 0x%x", *fDataWord, fPos));
1420 AliWarning(Form("Unknown state %d. Last state %d. Current Word 0x%x at pos 0x%x", fNextStatus, fLastStatus, *fDataWord, fPos));
1421 ChangeStatus(fkStop);
1426 //AliDebug(1, Form("That's all folks! %d", fSM));
1430 //____________________________________________________________________________
1431 Int_t AliTRDRawStreamTB::NextChamber(AliTRDdigitsManager *man)
1434 // Fills single chamber digit array
1435 // Return value is the detector number
1438 AliTRDdataArrayI *digits = 0;
1439 AliTRDdataArrayI *track0 = 0;
1440 AliTRDdataArrayI *track1 = 0;
1441 AliTRDdataArrayI *track2 = 0;
1442 AliTRDSignalIndex *indexes = 0;
1444 if (fNextStatus == fkStart)
1449 // while (fNextStatus != fkStop)
1452 // // catch 3 things
1453 // // 1) if end of raw data - if chamber complete return
1454 // // 2) fill the data with signals if data decoded ok
1455 // // 3) initialize (destroy old) after the det has changed -> just after HC header decoding
1458 while (fNextStatus != fkStop)
1462 if (fNextStatus == fkStop)
1464 if (fLastStatus == fkStart)
1466 AliWarning("Stop just on the first word!");
1471 // if (fEndOfDataFlag == kTRUE)
1473 // if (fChamberDone[fDET] == 2)
1481 if ( *fDataWord == kEndOfRawDataMarker )
1482 { // End of half-chamber data, finished
1483 AliDebug(3, "We have reached the eof data. Next should be HC header or we are done.");
1486 if (fNextStatus != fkStop)
1487 ChangeStatus(fkNextHC);
1488 fEndOfDataFlag = kTRUE;
1490 if (fChamberDone[fDET] == 2)
1492 fChamberDone[fDET] = -1;
1493 AliDebug(5, Form("Return %d", fDET));
1499 switch (fNextStatus)
1503 if (DecodeNextRawWord() == kTRUE)
1505 for (Int_t it = 0; it < 3; it++)
1507 if ( fTB + it < fTBins )
1509 if ( fSig[it] > fRawDigitThreshold )
1511 digits->SetDataUnchecked(fROW, fCOL, fTB + it, fSig[it]);
1512 indexes->AddIndexTBin(fROW, fCOL, fTB + it);
1513 if (man->UsesDictionaries())
1515 track0->SetDataUnchecked(fROW, fCOL, fTB + it, 0);
1516 track1->SetDataUnchecked(fROW, fCOL, fTB + it, 0);
1517 track2->SetDataUnchecked(fROW, fCOL, fTB + it, 0);
1518 } // if dictionaries
1519 } // signal above zero
1520 } // check the tbins range
1521 } // for each tbin of current 3
1526 // can be here as a fall back from decode raw data calling decodemcm
1527 if (fEndOfDataFlag == kTRUE)
1529 if (fChamberDone[fDET] == 2)
1531 fChamberDone[fDET] = -1;
1532 AliDebug(5, Form("Return %d", fDET));
1535 fEndOfDataFlag = kFALSE;
1542 if (DecodeMCM() == kFALSE)
1544 AliWarning(Form("Decode MCM unsuccessfull. Current Word 0x%x at pos 0x%x", *fDataWord, fPos));
1546 // default place for end of raw data...
1547 if (fEndOfDataFlag == kTRUE)
1549 if (fChamberDone[fDET] == 2)
1551 fChamberDone[fDET] = -1;
1554 fEndOfDataFlag = kFALSE;
1559 case fkSeekNonEoTracklet:
1562 if (DecodeHC() == kFALSE)
1564 AliWarning(Form("Decode HC unsuccessfull. Current Word 0x%x at pos 0x%x", *fDataWord, fPos));
1568 AliWarning(Form("Decode HC successfull. Current Word 0x%x at pos 0x%x", *fDataWord, fPos));
1569 //the hc header should be decoded by now
1570 //if (fLastStatus == fkDecodeHC && fNextStatus != fkNextHC)
1571 AliDebug(3, Form("Status last %d %d det %d lastdet %d", fLastStatus,fkDecodeHC, fDET, fLastDET));
1572 if (fLastStatus == fkDecodeHC && fDET != fLastDET)
1574 AliDebug(4, Form("???? New DET ???? %d last %d", fDET, fLastDET));
1576 // allocate stuff for the new det
1577 //man->ResetArrays();
1578 digits = man->GetDigits(fDET);
1579 track0 = man->GetDictionary(fDET,0);
1580 track1 = man->GetDictionary(fDET,1);
1581 track2 = man->GetDictionary(fDET,2);
1583 // Allocate memory space for the digits buffer
1584 if (digits->GetNtime() == 0)
1586 AliDebug(5, Form("Alloc digits for det %d rows %d cols %d tbins %d", fDET, fRowMax, fColMax, fTBins));
1587 digits->Allocate(fRowMax, fColMax, fTBins);
1588 if (man->UsesDictionaries())
1590 track0->Allocate(fRowMax, fColMax, fTBins);
1591 track1->Allocate(fRowMax, fColMax, fTBins);
1592 track2->Allocate(fRowMax, fColMax, fTBins);
1596 indexes = man->GetIndexes(fDET);
1597 indexes->SetSM(fSM);
1598 indexes->SetStack(fSTACK);
1599 indexes->SetLayer(fLAYER);
1600 indexes->SetDetNumber(fDET);
1602 if (indexes->IsAllocated() == kFALSE)
1604 AliDebug(4, "Allocating indexes");
1605 indexes->Allocate(fRowMax, fColMax, fTBins);
1607 } // is the HC header already decoded?
1613 if (DecodeSM() == kFALSE)
1614 AliWarning(Form("Decode SM unsuccessfull. Current Word 0x%x at pos 0x%x", *fDataWord, fPos));
1621 AliWarning(Form("Unknown state %d. Last state %d. Current Word 0x%x at pos 0x%x", fNextStatus, fLastStatus, *fDataWord, fPos));
1622 ChangeStatus(fkStop);
1627 // we do not return chambers for which the end-of-data was not received twice (for each HC)
1629 //AliDebug(1, Form("That's all folks! %d", fSM));
1635 //____________________________________________________________________________
1636 Int_t AliTRDRawStreamTB::SkipWords(UInt_t iw)
1638 Int_t status = fkWordOK;
1639 for (UInt_t i = 0; i < iw; i++)
1641 status = NextData();
1642 AliDebug(5, Form("Skipping word %d of %d [0x%x]", i+1, iw, *fDataWord));
1643 if (status != fkWordOK)
1647 //status = NextData();
1651 //____________________________________________________________________________
1652 Int_t AliTRDRawStreamTB::DecodeHeadingInfo()
1655 fSMinfo[fiSMx].Decode(fDataWord);
1657 fSMinfo[fiSMx].Dump();
1659 Int_t status = SkipWords(fSMinfo[fiSMx].fHeaderSize);
1660 if (status != fkWordOK)
1663 status = NextData();
1665 //fSMinfo[fiSMx].fStackActive[0] = kTRUE;
1667 if (status == fkWordOK)
1669 for (Int_t i = 0; i < 5; i++)
1671 if (fSMinfo[fiSMx].fStackActive[i] == kFALSE)
1673 AliDebug(5, Form("Decode stack info %d 0x%x", i, *fDataWord));
1674 if (fgStackIndexBug == kFALSE)
1675 fStackInfo[fiSMx][i].Decode(fDataWord);
1677 fStackInfo[fiSMx][i].DecodeBug(fDataWord);
1680 fStackInfo[fiSMx][i].Dump();
1682 status = SkipWords(fStackInfo[fiSMx][i].fHeaderSize);
1683 if (status != fkWordOK)
1687 status = NextData();
1689 if (status != fkWordOK)