00001 #include "EventFilter/SiPixelRawToDigi/interface/ErrorChecker.h"
00002
00003 #include "CondFormats/SiPixelObjects/interface/SiPixelFrameConverter.h"
00004
00005 #include "DataFormats/DetId/interface/DetId.h"
00006 #include "DataFormats/SiPixelDetId/interface/PixelSubdetector.h"
00007 #include "DataFormats/FEDRawData/interface/FEDHeader.h"
00008 #include "DataFormats/FEDRawData/interface/FEDTrailer.h"
00009
00010 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00011
00012 #include <bitset>
00013 #include <sstream>
00014 #include <iostream>
00015
00016 using namespace std;
00017 using namespace edm;
00018 using namespace sipixelobjects;
00019
00020 const int CRC_bits = 1;
00021 const int LINK_bits = 6;
00022 const int ROC_bits = 5;
00023 const int DCOL_bits = 5;
00024 const int PXID_bits = 8;
00025 const int ADC_bits = 8;
00026 const int OMIT_ERR_bits = 1;
00027
00028 const int CRC_shift = 2;
00029 const int ADC_shift = 0;
00030 const int PXID_shift = ADC_shift + ADC_bits;
00031 const int DCOL_shift = PXID_shift + PXID_bits;
00032 const int ROC_shift = DCOL_shift + DCOL_bits;
00033 const int LINK_shift = ROC_shift + ROC_bits;
00034 const int OMIT_ERR_shift = 20;
00035
00036 const uint32_t dummyDetId = 0xffffffff;
00037
00038 const ErrorChecker::Word64 CRC_mask = ~(~ErrorChecker::Word64(0) << CRC_bits);
00039 const ErrorChecker::Word32 ERROR_mask = ~(~ErrorChecker::Word32(0) << ROC_bits);
00040 const ErrorChecker::Word32 LINK_mask = ~(~ErrorChecker::Word32(0) << LINK_bits);
00041 const ErrorChecker::Word32 ROC_mask = ~(~ErrorChecker::Word32(0) << ROC_bits);
00042 const ErrorChecker::Word32 DCOL_mask = ~(~ErrorChecker::Word32(0) << DCOL_bits);
00043 const ErrorChecker::Word32 PXID_mask = ~(~ErrorChecker::Word32(0) << PXID_bits);
00044 const ErrorChecker::Word32 OMIT_ERR_mask = ~(~ErrorChecker::Word32(0) << OMIT_ERR_bits);
00045
00046 ErrorChecker::ErrorChecker() {
00047
00048 includeErrors = false;
00049 }
00050
00051 void ErrorChecker::setErrorStatus(bool ErrorStatus)
00052 {
00053 includeErrors = ErrorStatus;
00054 }
00055
00056 bool ErrorChecker::checkCRC(bool& errorsInEvent, int fedId, const Word64* trailer, Errors& errors)
00057 {
00058 int CRC_BIT = (*trailer >> CRC_shift) & CRC_mask;
00059 if (CRC_BIT == 0) return true;
00060 errorsInEvent = true;
00061 if (includeErrors) {
00062 int errorType = 39;
00063 SiPixelRawDataError error(*trailer, errorType, fedId);
00064 errors[dummyDetId].push_back(error);
00065 }
00066 return false;
00067 }
00068
00069 bool ErrorChecker::checkHeader(bool& errorsInEvent, int fedId, const Word64* header, Errors& errors)
00070 {
00071 FEDHeader fedHeader( reinterpret_cast<const unsigned char*>(header));
00072 if ( !fedHeader.check() ) return false;
00073 if ( fedHeader.sourceID() != fedId) {
00074 LogDebug("PixelDataFormatter::interpretRawData, fedHeader.sourceID() != fedId")
00075 <<", sourceID = " <<fedHeader.sourceID()
00076 <<", fedId = "<<fedId<<", errorType = 32";
00077 errorsInEvent = true;
00078 if (includeErrors) {
00079 int errorType = 32;
00080 SiPixelRawDataError error(*header, errorType, fedId);
00081 errors[dummyDetId].push_back(error);
00082 }
00083 }
00084 return fedHeader.moreHeaders();
00085 }
00086
00087 bool ErrorChecker::checkTrailer(bool& errorsInEvent, int fedId, int nWords, const Word64* trailer, Errors& errors)
00088 {
00089 FEDTrailer fedTrailer(reinterpret_cast<const unsigned char*>(trailer));
00090 if ( !fedTrailer.check()) {
00091 if(includeErrors) {
00092 int errorType = 33;
00093 SiPixelRawDataError error(*trailer, errorType, fedId);
00094 errors[dummyDetId].push_back(error);
00095 }
00096 errorsInEvent = true;
00097 LogError("PixelDataFormatter::interpretRawData, fedTrailer.check: ")
00098 <<"fedTrailer.check failed, Fed: " << fedId << ", errorType = 33";
00099 return false;
00100 }
00101 if ( fedTrailer.lenght()!= nWords) {
00102 LogError("PROBLEM in PixelDataFormatter, fedTrailer.lenght()!= nWords !!")<< " Fed: " << fedId << ", errorType = 34";
00103 errorsInEvent = true;
00104 if(includeErrors) {
00105 int errorType = 34;
00106 SiPixelRawDataError error(*trailer, errorType, fedId);
00107 errors[dummyDetId].push_back(error);
00108 }
00109 }
00110 return fedTrailer.moreTrailers();
00111 }
00112
00113 bool ErrorChecker::checkROC(bool& errorsInEvent, int fedId, const SiPixelFrameConverter* converter, Word32& errorWord, Errors& errors)
00114 {
00115 int errorType = (errorWord >> ROC_shift) & ERROR_mask;
00116
00117 switch (errorType) {
00118 case(25) : {
00119 LogDebug("")<<" invalid ROC=25 found (errorType=25)";
00120 errorsInEvent = true;
00121 break;
00122 }
00123 case(26) : {
00124
00125 return false;
00126 }
00127 case(27) : {
00128
00129 return false;
00130 }
00131 case(28) : {
00132 LogDebug("")<<" error fifo nearly full (errorType=28)";
00133 errorsInEvent = true;
00134 break;
00135 }
00136 case(29) : {
00137 LogDebug("")<<" timeout on a channel (errorType=29)";
00138 errorsInEvent = true;
00139 if ((errorWord >> OMIT_ERR_shift) & OMIT_ERR_mask) {
00140 LogDebug("")<<" ...first errorType=29 error, this gets masked out";
00141 return false;
00142 }
00143 break;
00144 }
00145 case(30) : {
00146 LogDebug("")<<" TBM error trailer (errorType=30)";
00147 errorsInEvent = true;
00148 break;
00149 }
00150 case(31) : {
00151 LogDebug("")<<" event number error (errorType=31)";
00152 errorsInEvent = true;
00153 break;
00154 }
00155 default: return true;
00156 };
00157
00158 if(includeErrors) {
00159 SiPixelRawDataError error(errorWord, errorType, fedId);
00160 uint32_t detId;
00161 detId = errorDetId(converter, errorType, errorWord);
00162 errors[detId].push_back(error);
00163 }
00164 return false;
00165 }
00166
00167 void ErrorChecker::conversionError(int fedId, const SiPixelFrameConverter* converter, int status, Word32& errorWord, Errors& errors)
00168 {
00169 switch (status) {
00170 case(1) : {
00171 LogDebug("ErrorChecker::conversionError") << " Fed: " << fedId << " invalid channel Id (errorType=35)";
00172 if(includeErrors) {
00173 int errorType = 35;
00174 SiPixelRawDataError error(errorWord, errorType, fedId);
00175 uint32_t detId = errorDetId(converter, errorType, errorWord);
00176 errors[detId].push_back(error);
00177 }
00178 break;
00179 }
00180 case(2) : {
00181 LogDebug("ErrorChecker::conversionError")<< " Fed: " << fedId << " invalid ROC Id (errorType=36)";
00182 if(includeErrors) {
00183 int errorType = 36;
00184 SiPixelRawDataError error(errorWord, errorType, fedId);
00185 uint32_t detId = errorDetId(converter, errorType, errorWord);
00186 errors[detId].push_back(error);
00187 }
00188 break;
00189 }
00190 case(3) : {
00191 LogDebug("ErrorChecker::conversionError")<< " Fed: " << fedId << " invalid dcol/pixel value (errorType=37)";
00192 if(includeErrors) {
00193 int errorType = 37;
00194 SiPixelRawDataError error(errorWord, errorType, fedId);
00195 uint32_t detId = errorDetId(converter, errorType, errorWord);
00196 errors[detId].push_back(error);
00197 }
00198 break;
00199 }
00200 case(4) : {
00201 LogDebug("ErrorChecker::conversionError")<< " Fed: " << fedId << " dcol/pixel read out of order (errorType=38)";
00202 if(includeErrors) {
00203 int errorType = 38;
00204 SiPixelRawDataError error(errorWord, errorType, fedId);
00205 uint32_t detId = errorDetId(converter, errorType, errorWord);
00206 errors[detId].push_back(error);
00207 }
00208 break;
00209 }
00210 default: LogDebug("ErrorChecker::conversionError")<<" cabling check returned unexpected result, status = "<< status;
00211 };
00212 }
00213
00214
00215 uint32_t ErrorChecker::errorDetId(const SiPixelFrameConverter* converter,
00216 int errorType, const Word32 & word) const
00217 {
00218 if (!converter) return dummyDetId;
00219
00220 ElectronicIndex cabling;
00221
00222 switch (errorType) {
00223 case 25 : case 30 : case 31 : case 36 : {
00224
00225 cabling.dcol = 0;
00226 cabling.pxid = 2;
00227 cabling.roc = 1;
00228 cabling.link = (word >> LINK_shift) & LINK_mask;
00229
00230 DetectorIndex detIdx;
00231 int status = converter->toDetector(cabling, detIdx);
00232 if (status) break;
00233 if(DetId(detIdx.rawId).subdetId() == static_cast<int>(PixelSubdetector::PixelBarrel)) return detIdx.rawId;
00234 break;
00235 }
00236 case 29 : {
00237 int chanNmbr = 0;
00238 const int DB0_shift = 0;
00239 const int DB1_shift = DB0_shift + 1;
00240 const int DB2_shift = DB1_shift + 1;
00241 const int DB3_shift = DB2_shift + 1;
00242 const int DB4_shift = DB3_shift + 1;
00243 const uint32_t DataBit_mask = ~(~uint32_t(0) << 1);
00244
00245 int CH1 = (word >> DB0_shift) & DataBit_mask;
00246 int CH2 = (word >> DB1_shift) & DataBit_mask;
00247 int CH3 = (word >> DB2_shift) & DataBit_mask;
00248 int CH4 = (word >> DB3_shift) & DataBit_mask;
00249 int CH5 = (word >> DB4_shift) & DataBit_mask;
00250 int BLOCK_bits = 3;
00251 int BLOCK_shift = 8;
00252 uint32_t BLOCK_mask = ~(~uint32_t(0) << BLOCK_bits);
00253 int BLOCK = (word >> BLOCK_shift) & BLOCK_mask;
00254 int localCH = 1*CH1+2*CH2+3*CH3+4*CH4+5*CH5;
00255 if (BLOCK%2==0) chanNmbr=(BLOCK/2)*9+localCH;
00256 else chanNmbr = ((BLOCK-1)/2)*9+4+localCH;
00257 if ((chanNmbr<1)||(chanNmbr>36)) break;
00258
00259
00260 cabling.dcol = 0;
00261 cabling.pxid = 2;
00262 cabling.roc = 1;
00263 cabling.link = chanNmbr;
00264 DetectorIndex detIdx;
00265 int status = converter->toDetector(cabling, detIdx);
00266 if (status) break;
00267 if(DetId(detIdx.rawId).subdetId() == static_cast<int>(PixelSubdetector::PixelBarrel)) return detIdx.rawId;
00268 break;
00269 }
00270 case 37 : case 38: {
00271 cabling.dcol = 0;
00272 cabling.pxid = 2;
00273 cabling.roc = (word >> ROC_shift) & ROC_mask;
00274 cabling.link = (word >> LINK_shift) & LINK_mask;
00275
00276 DetectorIndex detIdx;
00277 int status = converter->toDetector(cabling, detIdx);
00278 if (status) break;
00279
00280 return detIdx.rawId;
00281 break;
00282 }
00283 default : break;
00284 };
00285 return dummyDetId;
00286 }