CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_3/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), modulesToUnpack(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::setModulesToUnpack(const std::set<unsigned int> * moduleIds)
00080 {
00081   modulesToUnpack = moduleIds;
00082 }
00083 
00084 void PixelDataFormatter::passFrameReverter(const SiPixelFrameReverter* reverter)
00085 {
00086   theFrameReverter = reverter;
00087 }
00088 
00089 void PixelDataFormatter::interpretRawData(bool& errorsInEvent, int fedId, const FEDRawData& rawData, Digis& digis, Errors& errors)
00090 {
00091   debug = edm::MessageDrop::instance()->debugEnabled;
00092   int nWords = rawData.size()/sizeof(Word64);
00093   if (nWords==0) return;
00094 
00095   SiPixelFrameConverter * converter = (theCablingTree) ? 
00096       new SiPixelFrameConverter(theCablingTree, fedId) : 0;
00097 
00098   // check CRC bit
00099   const Word64* trailer = reinterpret_cast<const Word64* >(rawData.data())+(nWords-1);
00100   bool CRC_OK = errorcheck.checkCRC(errorsInEvent, fedId, trailer, errors);
00101   
00102   if(CRC_OK) {
00103     // check headers
00104     const Word64* header = reinterpret_cast<const Word64* >(rawData.data()); header--;
00105     bool moreHeaders = true;
00106     while (moreHeaders) {
00107       header++;
00108       if (debug) LogTrace("")<<"HEADER:  " <<  print(*header);
00109       bool headerStatus = errorcheck.checkHeader(errorsInEvent, fedId, header, errors);
00110       moreHeaders = headerStatus;
00111     }
00112 
00113     // check trailers
00114     bool moreTrailers = true;
00115     trailer++;
00116     while (moreTrailers) {
00117       trailer--;
00118       if (debug) LogTrace("")<<"TRAILER: " <<  print(*trailer);
00119       bool trailerStatus = errorcheck.checkTrailer(errorsInEvent, fedId, nWords, trailer, errors);
00120       moreTrailers = trailerStatus;
00121     }
00122 
00123     // data words
00124     theWordCounter += 2*(nWords-2);
00125     if (debug) LogTrace("")<<"data words: "<< (trailer-header-1);
00126     for (const Word64* word = header+1; word != trailer; word++) {
00127       if (debug) LogTrace("")<<"DATA:    " <<  print(*word);
00128       Word32 w1 =  *word       & WORD32_mask;
00129       Word32 w2 =  *word >> 32 & WORD32_mask;
00130       if (w1==0) theWordCounter--;
00131       if (w2==0) theWordCounter--;
00132 
00133       // check status of word...
00134       bool notErrorROC1 = errorcheck.checkROC(errorsInEvent, fedId, converter, w1, errors);
00135       if (notErrorROC1) {
00136         int status1 = word2digi(fedId, converter, includeErrors, useQualityInfo, w1, digis);
00137         if (status1) {
00138             LogDebug("PixelDataFormatter::interpretRawData") 
00139                     << "status #" <<status1<<" returned for word1";
00140             errorsInEvent = true;
00141             errorcheck.conversionError(fedId, converter, status1, w1, errors);
00142         }
00143       }
00144       bool notErrorROC2 = errorcheck.checkROC(errorsInEvent, fedId, converter, w2, errors);
00145       if (notErrorROC2) {
00146         int status2 = word2digi(fedId, converter, includeErrors, useQualityInfo, w2, digis);
00147         if (status2) {
00148             LogDebug("PixelDataFormatter::interpretRawData") 
00149                     << "status #" <<status2<<" returned for word2";
00150             errorsInEvent = true;
00151             errorcheck.conversionError(fedId, converter, status2, w2, errors);
00152         }
00153       }
00154     }
00155   }  // end if(CRC_OK)
00156   delete converter;
00157 }
00158 
00159 
00160 void PixelDataFormatter::formatRawData(unsigned int lvl1_ID, RawData & fedRawData, const Digis & digis) 
00161 {
00162   std::map<int, vector<Word32> > words;
00163 
00164   // translate digis into 32-bit raw words and store in map indexed by Fed
00165   for (Digis::const_iterator im = digis.begin(); im != digis.end(); im++) {
00166     allDetDigis++;
00167     cms_uint32_t rawId = im->first;
00168     hasDetDigis++;
00169     const DetDigis & detDigis = im->second;
00170     for (DetDigis::const_iterator it = detDigis.begin(); it != detDigis.end(); it++) {
00171       theDigiCounter++;
00172       const PixelDigi & digi = (*it);
00173       int status = digi2word( rawId, digi, words);
00174       if (status) {
00175          LogError("PixelDataFormatter::formatData exception")
00176             <<" digi2word returns error #"<<status
00177             <<" Ndigis: "<<theDigiCounter << endl
00178             <<" detector: "<<rawId<< endl
00179             << print(digi) <<endl;
00180       } // if (status)
00181     } // for (DetDigis
00182   } // for (Digis
00183   LogTrace(" allDetDigis/hasDetDigis : ") << allDetDigis<<"/"<<hasDetDigis;
00184 
00185   typedef std::map<int, vector<Word32> >::const_iterator RI;
00186   for (RI feddata = words.begin(); feddata != words.end(); feddata++) {
00187     int fedId = feddata->first;
00188     // since raw words are written in the form of 64-bit packets
00189     // add extra 32-bit word to make number of words even if necessary
00190     if (words.find(fedId)->second.size() %2 != 0) words[fedId].push_back( Word32(0) );
00191 
00192     // size in Bytes; create output structure
00193     int dataSize = words.find(fedId)->second.size() * sizeof(Word32);
00194     int nHeaders = 1;
00195     int nTrailers = 1;
00196     dataSize += (nHeaders+nTrailers)*sizeof(Word64); 
00197     FEDRawData * rawData = new FEDRawData(dataSize);
00198 
00199     // get begining of data;
00200     Word64 * word = reinterpret_cast<Word64* >(rawData->data());
00201 
00202     // write one header
00203     FEDHeader::set(  reinterpret_cast<unsigned char*>(word), 0, lvl1_ID, 0, fedId); 
00204     word++;
00205 
00206     // write data
00207     unsigned int nWord32InFed = words.find(fedId)->second.size();
00208     for (unsigned int i=0; i < nWord32InFed; i+=2) {
00209       *word = (Word64(words.find(fedId)->second[i]) << 32 ) | words.find(fedId)->second[i+1];
00210       LogDebug("PixelDataFormatter")  << print(*word);
00211       word++;
00212     }
00213 
00214     // write one trailer
00215     FEDTrailer::set(  reinterpret_cast<unsigned char*>(word), dataSize/sizeof(Word64), 0,0,0);
00216     word++;
00217 
00218     // check memory
00219     if (word != reinterpret_cast<Word64* >(rawData->data()+dataSize)) {
00220       string s = "** PROBLEM in PixelDataFormatter !!!";
00221       throw cms::Exception(s);
00222     } // if (word !=
00223     fedRawData[fedId] = *rawData;
00224     delete rawData;
00225   } // for (RI feddata 
00226 }
00227 
00228 int PixelDataFormatter::digi2word( cms_uint32_t detId, const PixelDigi& digi, 
00229     std::map<int, vector<Word32> > & words) const
00230 {
00231   LogDebug("PixelDataFormatter")
00232 // <<" detId: " << detId 
00233   <<print(digi);
00234 
00235   DetectorIndex detector = {detId, digi.row(), digi.column()};
00236   ElectronicIndex cabling;
00237   int fedId  = theFrameReverter->toCabling(cabling, detector);
00238   if (fedId<0) return fedId;
00239 
00240   Word32 word =
00241              (cabling.link << LINK_shift)
00242            | (cabling.roc << ROC_shift)
00243            | (cabling.dcol << DCOL_shift)
00244            | (cabling.pxid << PXID_shift)
00245            | (digi.adc() << ADC_shift);
00246   words[fedId].push_back(word);
00247   theWordCounter++;
00248 
00249   return 0;
00250 }
00251 
00252 
00253 int PixelDataFormatter::word2digi(const int fedId, const SiPixelFrameConverter* converter, 
00254     const bool includeErrors, const bool useQuality, const Word32 & word, Digis & digis) const
00255 {
00256   // do not interpret false digis
00257   if (word == 0 ) return 0;
00258 
00259   ElectronicIndex cabling;
00260   cabling.dcol = (word >> DCOL_shift) & DCOL_mask;
00261   cabling.pxid = (word >> PXID_shift) & PXID_mask;
00262   cabling.link = (word >> LINK_shift) & LINK_mask;
00263   cabling.roc  = (word >> ROC_shift) & ROC_mask;
00264   int adc      = (word >> ADC_shift) & ADC_mask;
00265 
00266   if (debug) {
00267     LocalPixel::DcolPxid pixel = {cabling.dcol,cabling.pxid};
00268     LocalPixel local(pixel);
00269     LogTrace("")<<" link: "<<cabling.link<<", roc: "<<cabling.roc
00270                 <<" rocRow: "<<local.rocRow()<<", rocCol:"<<local.rocCol()
00271                 <<" (dcol: "<<cabling.dcol<<", pxid:"<<cabling.pxid<<"), adc:"<<adc;
00272   }
00273 
00274   if (!converter) return 0;
00275 
00276   DetectorIndex detIdx;
00277   int status = converter->toDetector(cabling, detIdx);
00278   if (status) return status;
00279 
00280   // exclude ROC(raw) based on bad ROC list bad in SiPixelQuality
00281   // enable: process.siPixelDigis.UseQualityInfo = True
00282   // 20-10-2010 A.Y.
00283   if (useQuality&&badPixelInfo) {
00284     CablingPathToDetUnit path = {fedId, cabling.link, cabling.roc};
00285     const PixelROC * roc = theCablingTree->findItem(path);
00286     short rocInDet = (short) roc->idInDetUnit();
00287     bool badROC = badPixelInfo->IsRocBad(detIdx.rawId, rocInDet);
00288     if (badROC) return 0;
00289   }
00290 
00291   if (modulesToUnpack && modulesToUnpack->find(detIdx.rawId) == modulesToUnpack->end()) return 0;
00292 
00293   PixelDigi pd(detIdx.row, detIdx.col, adc);
00294   digis[detIdx.rawId].push_back(pd);
00295 
00296   theDigiCounter++;
00297 
00298   if (debug)  LogTrace("") << print(pd);
00299   return 0;
00300 }
00301 
00302 std::string PixelDataFormatter::print(const PixelDigi & digi) const
00303 {
00304   ostringstream str;
00305   str << " DIGI: row: " << digi.row() <<", col: " << digi.column() <<", adc: " << digi.adc();
00306   return str.str();
00307 }
00308 
00309 std::string PixelDataFormatter::print(const  Word64 & word) const
00310 {
00311   ostringstream str;
00312   str  <<"word64:  " << reinterpret_cast<const bitset<64>&> (word);
00313   return str.str();
00314 }
00315