CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch13/src/EventFilter/SiPixelRawToDigi/src/PixelDataFormatter.cc

Go to the documentation of this file.
00001 #include "EventFilter/SiPixelRawToDigi/interface/PixelDataFormatter.h"
00002 
00003 #include "CondFormats/SiPixelObjects/interface/SiPixelFedCablingTree.h"
00004 #include "CondFormats/SiPixelObjects/interface/SiPixelFedCablingMap.h"
00005 #include "CondFormats/SiPixelObjects/interface/SiPixelFrameConverter.h"
00006 
00007 #include "CondFormats/SiPixelObjects/interface/SiPixelQuality.h"
00008 
00009 #include "DataFormats/FEDRawData/interface/FEDRawData.h"
00010 #include "DataFormats/FEDRawData/interface/FEDHeader.h"
00011 #include "DataFormats/FEDRawData/interface/FEDTrailer.h"
00012 
00013 #include "CondFormats/SiPixelObjects/interface/PixelROC.h"
00014 
00015 #include "FWCore/Utilities/interface/Exception.h"
00016 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00017 
00018 #include <bitset>
00019 #include <sstream>
00020 #include <iostream>
00021 
00022 using namespace std;
00023 using namespace edm;
00024 using namespace sipixelobjects;
00025 
00026 const int LINK_bits = 6;
00027 const int ROC_bits  = 5;
00028 const int DCOL_bits = 5;
00029 const int PXID_bits = 8;
00030 const int ADC_bits  = 8;
00031 
00032 const int ADC_shift  = 0;
00033 const int PXID_shift = ADC_shift + ADC_bits;
00034 const int DCOL_shift = PXID_shift + PXID_bits;
00035 const int ROC_shift  = DCOL_shift + DCOL_bits;
00036 const int LINK_shift = ROC_shift + ROC_bits;
00037 
00038 const PixelDataFormatter::Word32 LINK_mask = ~(~PixelDataFormatter::Word32(0) << LINK_bits);
00039 const PixelDataFormatter::Word32 ROC_mask  = ~(~PixelDataFormatter::Word32(0) << ROC_bits);
00040 const PixelDataFormatter::Word32 DCOL_mask = ~(~PixelDataFormatter::Word32(0) << DCOL_bits);
00041 const PixelDataFormatter::Word32 PXID_mask = ~(~PixelDataFormatter::Word32(0) << PXID_bits);
00042 const PixelDataFormatter::Word32 ADC_mask  = ~(~PixelDataFormatter::Word32(0) << ADC_bits);
00043 
00044 const PixelDataFormatter::Word64 WORD32_mask = 0xffffffff;
00045 
00046 
00047 PixelDataFormatter::PixelDataFormatter( const SiPixelFedCabling* map)
00048   : theDigiCounter(0), theWordCounter(0), theCablingTree(map), badPixelInfo(0)
00049 {
00050   int s32 = sizeof(Word32);
00051   int s64 = sizeof(Word64);
00052   int s8  = sizeof(char);
00053   if ( s8 != 1 || s32 != 4*s8 || s64 != 2*s32) {
00054      LogError("**PixelDataFormatter**")
00055           <<" unexpected sizes: "
00056           <<"  size of char is: " << s8
00057           <<", size of Word32 is: " << s32
00058           <<", size of Word64 is: " << s64
00059           <<", send exception" ;
00060   }
00061   includeErrors = false;
00062   useQualityInfo = false;
00063   allDetDigis = 0;
00064   hasDetDigis = 0;
00065 }
00066 
00067 void PixelDataFormatter::setErrorStatus(bool ErrorStatus)
00068 {
00069   includeErrors = ErrorStatus;
00070   errorcheck.setErrorStatus(includeErrors);
00071 }
00072 
00073 void PixelDataFormatter::setQualityStatus(bool QualityStatus, const SiPixelQuality* QualityInfo)
00074 {
00075   useQualityInfo = QualityStatus;
00076   badPixelInfo = QualityInfo;
00077 }
00078 
00079 void PixelDataFormatter::interpretRawData(bool& errorsInEvent, int fedId, const FEDRawData& rawData, Digis& digis, Errors& errors)
00080 {
00081   debug = edm::MessageDrop::instance()->debugEnabled;
00082   int nWords = rawData.size()/sizeof(Word64);
00083   if (nWords==0) return;
00084 
00085   SiPixelFrameConverter * converter = (theCablingTree) ? 
00086       new SiPixelFrameConverter(theCablingTree, fedId) : 0;
00087 
00088   // check CRC bit
00089   const Word64* trailer = reinterpret_cast<const Word64* >(rawData.data())+(nWords-1);
00090   bool CRC_OK = errorcheck.checkCRC(errorsInEvent, fedId, trailer, errors);
00091   
00092   if(CRC_OK) {
00093     // check headers
00094     const Word64* header = reinterpret_cast<const Word64* >(rawData.data()); header--;
00095     bool moreHeaders = true;
00096     while (moreHeaders) {
00097       header++;
00098       if (debug) LogTrace("")<<"HEADER:  " <<  print(*header);
00099       bool headerStatus = errorcheck.checkHeader(errorsInEvent, fedId, header, errors);
00100       moreHeaders = headerStatus;
00101     }
00102 
00103     // check trailers
00104     bool moreTrailers = true;
00105     trailer++;
00106     while (moreTrailers) {
00107       trailer--;
00108       if (debug) LogTrace("")<<"TRAILER: " <<  print(*trailer);
00109       bool trailerStatus = errorcheck.checkTrailer(errorsInEvent, fedId, nWords, trailer, errors);
00110       moreTrailers = trailerStatus;
00111     }
00112 
00113     // data words
00114     theWordCounter += 2*(nWords-2);
00115     if (debug) LogTrace("")<<"data words: "<< (trailer-header-1);
00116     for (const Word64* word = header+1; word != trailer; word++) {
00117       if (debug) LogTrace("")<<"DATA:    " <<  print(*word);
00118       Word32 w1 =  *word       & WORD32_mask;
00119       Word32 w2 =  *word >> 32 & WORD32_mask;
00120       if (w1==0) theWordCounter--;
00121       if (w2==0) theWordCounter--;
00122 
00123       // check status of word...
00124       bool notErrorROC1 = errorcheck.checkROC(errorsInEvent, fedId, converter, w1, errors);
00125       if (notErrorROC1) {
00126         int status1 = word2digi(fedId, converter, includeErrors, useQualityInfo, w1, digis);
00127         if (status1) {
00128             LogDebug("PixelDataFormatter::interpretRawData") 
00129                     << "status #" <<status1<<" returned for word1";
00130             errorsInEvent = true;
00131             errorcheck.conversionError(fedId, converter, status1, w1, errors);
00132         }
00133       }
00134       bool notErrorROC2 = errorcheck.checkROC(errorsInEvent, fedId, converter, w2, errors);
00135       if (notErrorROC2) {
00136         int status2 = word2digi(fedId, converter, includeErrors, useQualityInfo, w2, digis);
00137         if (status2) {
00138             LogDebug("PixelDataFormatter::interpretRawData") 
00139                     << "status #" <<status2<<" returned for word2";
00140             errorsInEvent = true;
00141             errorcheck.conversionError(fedId, converter, status2, w2, errors);
00142         }
00143       }
00144     }
00145   }  // end if(CRC_OK)
00146   delete converter;
00147 }
00148 
00149 
00150 FEDRawData * PixelDataFormatter::formatData(unsigned int lvl1_ID, int fedId, const Digis & digis) 
00151 {
00152   vector<Word32> words;
00153 
00154   SiPixelFrameConverter converter(theCablingTree, fedId);
00155   for (Digis::const_iterator im = digis.begin(); im != digis.end(); im++) {
00156     allDetDigis++;
00157     uint32_t rawId = im->first;
00158     if ( !converter.hasDetUnit(rawId) ) continue;
00159     hasDetDigis++;
00160     const DetDigis & detDigis = im->second;
00161     for (DetDigis::const_iterator it = detDigis.begin(); it != detDigis.end(); it++) {
00162       theDigiCounter++;
00163       const PixelDigi & digi = (*it);
00164       int status = digi2word( &converter, rawId, digi, words); 
00165       if (status) {
00166          LogError("PixelDataFormatter::formatData exception") 
00167             <<" digi2word returns error #"<<status
00168             <<" Ndigis: "<<theDigiCounter << endl
00169             <<" detector: "<<rawId<< endl
00170             << print(digi) <<endl; 
00171       }
00172     }
00173   }
00174   LogTrace(" allDetDigis/hasDetDigis : ") << allDetDigis<<"/"<<hasDetDigis;
00175 
00176   //
00177   // since digis are written in the form of 64-bit packets
00178   // add extra 32-bit word to make number of digis even
00179   //
00180   if (words.size() %2 != 0) words.push_back( Word32(0) );
00181 
00182 
00183   //
00184   // size in Bytes; create output structure
00185   //
00186   int dataSize = words.size() * sizeof(Word32);
00187   int nHeaders = 1;
00188   int nTrailers = 1;
00189   dataSize += (nHeaders+nTrailers)*sizeof(Word64); 
00190   FEDRawData * rawData = new FEDRawData(dataSize);
00191 
00192   //
00193   // get begining of data;
00194   Word64 * word = reinterpret_cast<Word64* >(rawData->data());
00195 
00196   //
00197   // write one header
00198   FEDHeader::set(  reinterpret_cast<unsigned char*>(word), 0, lvl1_ID, 0, fedId); 
00199   word++;
00200 
00201   //
00202   // write data
00203   for (unsigned int i=0; i < words.size(); i+=2) {
00204     *word = (Word64(words[i]) << 32 ) | words[i+1];
00205     LogDebug("PixelDataFormatter")  << print(*word);
00206     word++;
00207   }
00208 
00209   // write one trailer
00210   FEDTrailer::set(  reinterpret_cast<unsigned char*>(word), dataSize/sizeof(Word64), 0,0,0);
00211   word++;
00212 
00213   //
00214   // check memory
00215   //
00216   if (word != reinterpret_cast<Word64* >(rawData->data()+dataSize)) {
00217     string s = "** PROBLEM in PixelDataFormatter !!!";
00218     throw cms::Exception(s);
00219   }
00220 
00221   return rawData;
00222 }
00223 
00224 int PixelDataFormatter::digi2word( const SiPixelFrameConverter* converter,
00225     uint32_t detId, const PixelDigi& digi, std::vector<Word32> & words) const
00226 {
00227   LogDebug("PixelDataFormatter")
00228 // <<" detId: " << detId 
00229   <<print(digi);
00230 
00231   DetectorIndex detector = {detId, digi.row(), digi.column()};
00232   ElectronicIndex  cabling;
00233   int status  = converter->toCabling(cabling, detector);
00234   if (status) return status;
00235 
00236   Word32 word =
00237              (cabling.link  << LINK_shift)
00238            | (cabling.roc << ROC_shift)
00239            | (cabling.dcol << DCOL_shift)
00240            | (cabling.pxid << PXID_shift)
00241            | (digi.adc() << ADC_shift);
00242   words.push_back(word);
00243   theWordCounter++;
00244   return 0;
00245 }
00246 
00247 
00248 int PixelDataFormatter::word2digi(const int fedId, const SiPixelFrameConverter* converter, 
00249     const bool includeErrors, const bool useQuality, const Word32 & word, Digis & digis) const
00250 {
00251   // do not interpret false digis
00252   if (word == 0 ) return 0;
00253 
00254   ElectronicIndex cabling;
00255   cabling.dcol = (word >> DCOL_shift) & DCOL_mask;
00256   cabling.pxid = (word >> PXID_shift) & PXID_mask;
00257   cabling.link = (word >> LINK_shift) & LINK_mask;
00258   cabling.roc  = (word >> ROC_shift) & ROC_mask;
00259   int adc      = (word >> ADC_shift) & ADC_mask;
00260 
00261   if (debug) {
00262     LocalPixel::DcolPxid pixel = {cabling.dcol,cabling.pxid};
00263     LocalPixel local(pixel);
00264     LogTrace("")<<" link: "<<cabling.link<<", roc: "<<cabling.roc
00265                 <<" rocRow: "<<local.rocRow()<<", rocCol:"<<local.rocCol()
00266                 <<" (dcol: "<<cabling.dcol<<", pxid:"<<cabling.pxid<<"), adc:"<<adc;
00267   }
00268 
00269   if (!converter) return 0;
00270 
00271   DetectorIndex detIdx;
00272   int status = converter->toDetector(cabling, detIdx);
00273   if (status) return status;
00274 
00275   // exclude ROC(raw) based on bad ROC list bad in SiPixelQuality
00276   // enable: process.siPixelDigis.UseQualityInfo = True
00277   // 20-10-2010 A.Y.
00278   if (useQuality&&badPixelInfo) {
00279     CablingPathToDetUnit path = {fedId, cabling.link, cabling.roc};
00280     const PixelROC * roc = theCablingTree->findItem(path);
00281     short rocInDet = (short) roc->idInDetUnit();
00282     bool badROC = badPixelInfo->IsRocBad(detIdx.rawId, rocInDet);
00283     if (badROC) return 0;
00284   }
00285 
00286   PixelDigi pd(detIdx.row, detIdx.col, adc);
00287   digis[detIdx.rawId].push_back(pd);
00288 
00289   theDigiCounter++;
00290 
00291   if (debug)  LogTrace("") << print(pd);
00292   return 0;
00293 }
00294 
00295 std::string PixelDataFormatter::print(const PixelDigi & digi) const
00296 {
00297   ostringstream str;
00298   str << " DIGI: row: " << digi.row() <<", col: " << digi.column() <<", adc: " << digi.adc();
00299   return str.str();
00300 }
00301 
00302 std::string PixelDataFormatter::print(const  Word64 & word) const
00303 {
00304   ostringstream str;
00305   //str  <<"word64:  " << *reinterpret_cast<const bitset<64>*> (&word);
00306   str  <<"word64:  " << reinterpret_cast<const bitset<64>&> (word);
00307   return str.str();
00308 }
00309