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 cms_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
00160 if(errorType==30) {
00161 int StateMach_bits = 4;
00162 int StateMach_shift = 8;
00163 uint32_t StateMach_mask = ~(~uint32_t(0) << StateMach_bits);
00164 int StateMach = (errorWord >> StateMach_shift) & StateMach_mask;
00165 if( StateMach==4 || StateMach==9 ) errorType = 40;
00166 }
00167
00168
00169 SiPixelRawDataError error(errorWord, errorType, fedId);
00170 cms_uint32_t detId;
00171 detId = errorDetId(converter, errorType, errorWord);
00172 errors[detId].push_back(error);
00173 }
00174 return false;
00175 }
00176
00177 void ErrorChecker::conversionError(int fedId, const SiPixelFrameConverter* converter, int status, Word32& errorWord, Errors& errors)
00178 {
00179 switch (status) {
00180 case(1) : {
00181 LogDebug("ErrorChecker::conversionError") << " Fed: " << fedId << " invalid channel Id (errorType=35)";
00182 if(includeErrors) {
00183 int errorType = 35;
00184 SiPixelRawDataError error(errorWord, errorType, fedId);
00185 cms_uint32_t detId = errorDetId(converter, errorType, errorWord);
00186 errors[detId].push_back(error);
00187 }
00188 break;
00189 }
00190 case(2) : {
00191 LogDebug("ErrorChecker::conversionError")<< " Fed: " << fedId << " invalid ROC Id (errorType=36)";
00192 if(includeErrors) {
00193 int errorType = 36;
00194 SiPixelRawDataError error(errorWord, errorType, fedId);
00195 cms_uint32_t detId = errorDetId(converter, errorType, errorWord);
00196 errors[detId].push_back(error);
00197 }
00198 break;
00199 }
00200 case(3) : {
00201 LogDebug("ErrorChecker::conversionError")<< " Fed: " << fedId << " invalid dcol/pixel value (errorType=37)";
00202 if(includeErrors) {
00203 int errorType = 37;
00204 SiPixelRawDataError error(errorWord, errorType, fedId);
00205 cms_uint32_t detId = errorDetId(converter, errorType, errorWord);
00206 errors[detId].push_back(error);
00207 }
00208 break;
00209 }
00210 case(4) : {
00211 LogDebug("ErrorChecker::conversionError")<< " Fed: " << fedId << " dcol/pixel read out of order (errorType=38)";
00212 if(includeErrors) {
00213 int errorType = 38;
00214 SiPixelRawDataError error(errorWord, errorType, fedId);
00215 cms_uint32_t detId = errorDetId(converter, errorType, errorWord);
00216 errors[detId].push_back(error);
00217 }
00218 break;
00219 }
00220 default: LogDebug("ErrorChecker::conversionError")<<" cabling check returned unexpected result, status = "<< status;
00221 };
00222 }
00223
00224
00225 cms_uint32_t ErrorChecker::errorDetId(const SiPixelFrameConverter* converter,
00226 int errorType, const Word32 & word) const
00227 {
00228 if (!converter) return dummyDetId;
00229
00230 ElectronicIndex cabling;
00231
00232 switch (errorType) {
00233 case 25 : case 30 : case 31 : case 36 : case 40 : {
00234
00235 cabling.dcol = 0;
00236 cabling.pxid = 2;
00237 cabling.roc = 1;
00238 cabling.link = (word >> LINK_shift) & LINK_mask;
00239
00240 DetectorIndex detIdx;
00241 int status = converter->toDetector(cabling, detIdx);
00242 if (status) break;
00243 if(DetId(detIdx.rawId).subdetId() == static_cast<int>(PixelSubdetector::PixelBarrel)) return detIdx.rawId;
00244 break;
00245 }
00246 case 29 : {
00247 int chanNmbr = 0;
00248 const int DB0_shift = 0;
00249 const int DB1_shift = DB0_shift + 1;
00250 const int DB2_shift = DB1_shift + 1;
00251 const int DB3_shift = DB2_shift + 1;
00252 const int DB4_shift = DB3_shift + 1;
00253 const cms_uint32_t DataBit_mask = ~(~cms_uint32_t(0) << 1);
00254
00255 int CH1 = (word >> DB0_shift) & DataBit_mask;
00256 int CH2 = (word >> DB1_shift) & DataBit_mask;
00257 int CH3 = (word >> DB2_shift) & DataBit_mask;
00258 int CH4 = (word >> DB3_shift) & DataBit_mask;
00259 int CH5 = (word >> DB4_shift) & DataBit_mask;
00260 int BLOCK_bits = 3;
00261 int BLOCK_shift = 8;
00262 cms_uint32_t BLOCK_mask = ~(~cms_uint32_t(0) << BLOCK_bits);
00263 int BLOCK = (word >> BLOCK_shift) & BLOCK_mask;
00264 int localCH = 1*CH1+2*CH2+3*CH3+4*CH4+5*CH5;
00265 if (BLOCK%2==0) chanNmbr=(BLOCK/2)*9+localCH;
00266 else chanNmbr = ((BLOCK-1)/2)*9+4+localCH;
00267 if ((chanNmbr<1)||(chanNmbr>36)) break;
00268
00269
00270 cabling.dcol = 0;
00271 cabling.pxid = 2;
00272 cabling.roc = 1;
00273 cabling.link = chanNmbr;
00274 DetectorIndex detIdx;
00275 int status = converter->toDetector(cabling, detIdx);
00276 if (status) break;
00277 if(DetId(detIdx.rawId).subdetId() == static_cast<int>(PixelSubdetector::PixelBarrel)) return detIdx.rawId;
00278 break;
00279 }
00280 case 37 : case 38: {
00281 cabling.dcol = 0;
00282 cabling.pxid = 2;
00283 cabling.roc = (word >> ROC_shift) & ROC_mask;
00284 cabling.link = (word >> LINK_shift) & LINK_mask;
00285
00286 DetectorIndex detIdx;
00287 int status = converter->toDetector(cabling, detIdx);
00288 if (status) break;
00289
00290 return detIdx.rawId;
00291 break;
00292 }
00293 default : break;
00294 };
00295 return dummyDetId;
00296 }