CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/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::passFrameReverter(const SiPixelFrameReverter* reverter)
00080 {
00081   theFrameReverter = reverter;
00082 }
00083 
00084 void PixelDataFormatter::interpretRawData(bool& errorsInEvent, int fedId, const FEDRawData& rawData, Digis& digis, Errors& errors)
00085 {
00086   debug = edm::MessageDrop::instance()->debugEnabled;
00087   int nWords = rawData.size()/sizeof(Word64);
00088   if (nWords==0) return;
00089 
00090   SiPixelFrameConverter * converter = (theCablingTree) ? 
00091       new SiPixelFrameConverter(theCablingTree, fedId) : 0;
00092 
00093   // check CRC bit
00094   const Word64* trailer = reinterpret_cast<const Word64* >(rawData.data())+(nWords-1);
00095   bool CRC_OK = errorcheck.checkCRC(errorsInEvent, fedId, trailer, errors);
00096   
00097   if(CRC_OK) {
00098     // check headers
00099     const Word64* header = reinterpret_cast<const Word64* >(rawData.data()); header--;
00100     bool moreHeaders = true;
00101     while (moreHeaders) {
00102       header++;
00103       if (debug) LogTrace("")<<"HEADER:  " <<  print(*header);
00104       bool headerStatus = errorcheck.checkHeader(errorsInEvent, fedId, header, errors);
00105       moreHeaders = headerStatus;
00106     }
00107 
00108     // check trailers
00109     bool moreTrailers = true;
00110     trailer++;
00111     while (moreTrailers) {
00112       trailer--;
00113       if (debug) LogTrace("")<<"TRAILER: " <<  print(*trailer);
00114       bool trailerStatus = errorcheck.checkTrailer(errorsInEvent, fedId, nWords, trailer, errors);
00115       moreTrailers = trailerStatus;
00116     }
00117 
00118     // data words
00119     theWordCounter += 2*(nWords-2);
00120     if (debug) LogTrace("")<<"data words: "<< (trailer-header-1);
00121     for (const Word64* word = header+1; word != trailer; word++) {
00122       if (debug) LogTrace("")<<"DATA:    " <<  print(*word);
00123       Word32 w1 =  *word       & WORD32_mask;
00124       Word32 w2 =  *word >> 32 & WORD32_mask;
00125       if (w1==0) theWordCounter--;
00126       if (w2==0) theWordCounter--;
00127 
00128       // check status of word...
00129       bool notErrorROC1 = errorcheck.checkROC(errorsInEvent, fedId, converter, w1, errors);
00130       if (notErrorROC1) {
00131         int status1 = word2digi(fedId, converter, includeErrors, useQualityInfo, w1, digis);
00132         if (status1) {
00133             LogDebug("PixelDataFormatter::interpretRawData") 
00134                     << "status #" <<status1<<" returned for word1";
00135             errorsInEvent = true;
00136             errorcheck.conversionError(fedId, converter, status1, w1, errors);
00137         }
00138       }
00139       bool notErrorROC2 = errorcheck.checkROC(errorsInEvent, fedId, converter, w2, errors);
00140       if (notErrorROC2) {
00141         int status2 = word2digi(fedId, converter, includeErrors, useQualityInfo, w2, digis);
00142         if (status2) {
00143             LogDebug("PixelDataFormatter::interpretRawData") 
00144                     << "status #" <<status2<<" returned for word2";
00145             errorsInEvent = true;
00146             errorcheck.conversionError(fedId, converter, status2, w2, errors);
00147         }
00148       }
00149     }
00150   }  // end if(CRC_OK)
00151   delete converter;
00152 }
00153 
00154 
00155 void PixelDataFormatter::formatRawData(unsigned int lvl1_ID, RawData & fedRawData, const Digis & digis) 
00156 {
00157   std::map<int, vector<Word32> > words;
00158 
00159   // translate digis into 32-bit raw words and store in map indexed by Fed
00160   for (Digis::const_iterator im = digis.begin(); im != digis.end(); im++) {
00161     allDetDigis++;
00162     uint32_t rawId = im->first;
00163     hasDetDigis++;
00164     const DetDigis & detDigis = im->second;
00165     for (DetDigis::const_iterator it = detDigis.begin(); it != detDigis.end(); it++) {
00166       theDigiCounter++;
00167       const PixelDigi & digi = (*it);
00168       int status = digi2word( rawId, digi, words);
00169       if (status) {
00170          LogError("PixelDataFormatter::formatData exception")
00171             <<" digi2word returns error #"<<status
00172             <<" Ndigis: "<<theDigiCounter << endl
00173             <<" detector: "<<rawId<< endl
00174             << print(digi) <<endl;
00175       } // if (status)
00176     } // for (DetDigis
00177   } // for (Digis
00178   LogTrace(" allDetDigis/hasDetDigis : ") << allDetDigis<<"/"<<hasDetDigis;
00179 
00180   typedef std::map<int, vector<Word32> >::const_iterator RI;
00181   for (RI feddata = words.begin(); feddata != words.end(); feddata++) {
00182     int fedId = feddata->first;
00183     // since raw words are written in the form of 64-bit packets
00184     // add extra 32-bit word to make number of words even if necessary
00185     if (words.find(fedId)->second.size() %2 != 0) words[fedId].push_back( Word32(0) );
00186 
00187     // size in Bytes; create output structure
00188     int dataSize = words.find(fedId)->second.size() * sizeof(Word32);
00189     int nHeaders = 1;
00190     int nTrailers = 1;
00191     dataSize += (nHeaders+nTrailers)*sizeof(Word64); 
00192     FEDRawData * rawData = new FEDRawData(dataSize);
00193 
00194     // get begining of data;
00195     Word64 * word = reinterpret_cast<Word64* >(rawData->data());
00196 
00197     // write one header
00198     FEDHeader::set(  reinterpret_cast<unsigned char*>(word), 0, lvl1_ID, 0, fedId); 
00199     word++;
00200 
00201     // write data
00202     unsigned int nWord32InFed = words.find(fedId)->second.size();
00203     for (unsigned int i=0; i < nWord32InFed; i+=2) {
00204       *word = (Word64(words.find(fedId)->second[i]) << 32 ) | words.find(fedId)->second[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     // check memory
00214     if (word != reinterpret_cast<Word64* >(rawData->data()+dataSize)) {
00215       string s = "** PROBLEM in PixelDataFormatter !!!";
00216       throw cms::Exception(s);
00217     } // if (word !=
00218     fedRawData[fedId] = *rawData;
00219     delete rawData;
00220   } // for (RI feddata 
00221 }
00222 
00223 int PixelDataFormatter::digi2word( uint32_t detId, const PixelDigi& digi, 
00224     std::map<int, vector<Word32> > & words) const
00225 {
00226   LogDebug("PixelDataFormatter")
00227 // <<" detId: " << detId 
00228   <<print(digi);
00229 
00230   DetectorIndex detector = {detId, digi.row(), digi.column()};
00231   ElectronicIndex cabling;
00232   int fedId  = theFrameReverter->toCabling(cabling, detector);
00233   if (fedId<0) return fedId;
00234 
00235   Word32 word =
00236              (cabling.link << LINK_shift)
00237            | (cabling.roc << ROC_shift)
00238            | (cabling.dcol << DCOL_shift)
00239            | (cabling.pxid << PXID_shift)
00240            | (digi.adc() << ADC_shift);
00241   words[fedId].push_back(word);
00242   theWordCounter++;
00243 
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   return str.str();
00307 }
00308