Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:29:24

0001 #include "FWCore/Framework/interface/Event.h"
0002 #include "FWCore/Framework/interface/EventSetup.h"
0003 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0004 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0005 #include "SimCalorimetry/EcalElectronicsEmulation/interface/EcalSimRawData.h"
0006 #include "DataFormats/EcalDigi/interface/EcalMGPASample.h"
0007 #include "FWCore/Utilities/interface/Exception.h"
0008 
0009 #include <cmath>
0010 #include <fstream>  //used for debugging
0011 #include <iomanip>
0012 #include <iostream>
0013 #include <memory>
0014 
0015 const int EcalSimRawData::ttType[nEbTtEta] = {
0016     0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1,  // EE-
0017     0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1   // EE+
0018 };
0019 
0020 const int EcalSimRawData::stripCh2Phi[nTtTypes][ttEdge][ttEdge] = {
0021     // TT type 0:
0022     /*ch-->*/
0023     {{4, 3, 2, 1, 0},  /*strip*/
0024      {0, 1, 2, 3, 4},  /*|*/
0025      {4, 3, 2, 1, 0},  /*|*/
0026      {0, 1, 2, 3, 4},  /*|*/
0027      {4, 3, 2, 1, 0}}, /*V*/
0028     // TT type 1:
0029     {{0, 1, 2, 3, 4}, {4, 3, 2, 1, 0}, {0, 1, 2, 3, 4}, {4, 3, 2, 1, 0}, {0, 1, 2, 3, 4}}};
0030 
0031 const int EcalSimRawData::strip2Eta[nTtTypes][ttEdge] = {
0032     {4, 3, 2, 1, 0},  // TT type 0
0033     {0, 1, 2, 3, 4}   // TT type 1
0034 };
0035 
0036 EcalSimRawData::EcalSimRawData(const edm::ParameterSet &params) {
0037   // sets up parameters:
0038   digiProducer_ = params.getParameter<std::string>("unsuppressedDigiProducer");
0039   ebDigiCollection_ = params.getParameter<std::string>("EBdigiCollection");
0040   eeDigiCollection_ = params.getParameter<std::string>("EEdigiCollection");
0041   srDigiProducer_ = params.getParameter<std::string>("srProducer");
0042   ebSrFlagCollection_ = params.getParameter<std::string>("EBSrFlagCollection");
0043   eeSrFlagCollection_ = params.getParameter<std::string>("EESrFlagCollection");
0044   tpDigiCollection_ = params.getParameter<std::string>("trigPrimDigiCollection");
0045   tcpDigiCollection_ = params.getParameter<std::string>("tcpDigiCollection");
0046   tpProducer_ = params.getParameter<std::string>("trigPrimProducer");
0047   xtalVerbose_ = params.getUntrackedParameter<bool>("xtalVerbose", false);
0048   tpVerbose_ = params.getUntrackedParameter<bool>("tpVerbose", false);
0049   tcc2dcc_ = params.getUntrackedParameter<bool>("tcc2dccData", true);
0050   srp2dcc_ = params.getUntrackedParameter<bool>("srp2dccData", true);
0051   fe2dcc_ = params.getUntrackedParameter<bool>("fe2dccData", true);
0052   fe2tcc_ = params.getUntrackedParameter<bool>("fe2tccData", true);
0053   dccNum_ = params.getUntrackedParameter<int>("dccNum", -1);
0054   tccNum_ = params.getUntrackedParameter<int>("tccNum", -1);
0055   tccInDefaultVal_ = params.getUntrackedParameter<int>("tccInDefaultVal", 0xffff);
0056   basename_ = params.getUntrackedParameter<std::string>("outputBaseName");
0057 
0058   iEvent = 0;
0059 
0060   std::string writeMode = params.getParameter<std::string>("writeMode");
0061 
0062   if (writeMode == std::string("littleEndian")) {
0063     writeMode_ = littleEndian;
0064   } else if (writeMode == std::string("bigEndian")) {
0065     writeMode_ = bigEndian;
0066   } else {
0067     writeMode_ = ascii;
0068   }
0069 
0070   eeSrFlagToken_ = consumes<EESrFlagCollection>(edm::InputTag(srDigiProducer_, eeSrFlagCollection_));
0071   ebSrFlagToken_ = consumes<EBSrFlagCollection>(edm::InputTag(srDigiProducer_, ebSrFlagCollection_));
0072   ebDigisToken_ = consumes<EBDigiCollection>(edm::InputTag(digiProducer_, ebDigiCollection_));
0073   trigPrimDigisToken_[EcalSimRawData::tcp] =
0074       consumes<EcalTrigPrimDigiCollection>(edm::InputTag(tpProducer_, tcpDigiCollection_));
0075   trigPrimDigisToken_[EcalSimRawData::tp] =
0076       consumes<EcalTrigPrimDigiCollection>(edm::InputTag(tpProducer_, tpDigiCollection_));
0077 }
0078 
0079 void EcalSimRawData::analyze(const edm::Event &event, const edm::EventSetup &es) {
0080   // Event counter:
0081   ++iEvent;
0082 
0083   if (xtalVerbose_ | tpVerbose_) {
0084     std::cout << "=================================================================="
0085                  "====\n"
0086               << " Event " << iEvent << "\n"
0087               << "------------------------------------------------------------------"
0088               << "----\n";
0089   }
0090 
0091   if (fe2dcc_) {
0092     std::vector<uint16_t> adc[nEbEta][nEbPhi];
0093     getEbDigi(event, adc);
0094     genFeData(basename_, iEvent, adc);
0095   }
0096 
0097   if (fe2tcc_) {
0098     int tcp[nTtEta][nTtPhi] = {{0}};
0099     getTp(event, EcalSimRawData::tcp, tcp);
0100     genTccIn(basename_, iEvent, tcp);
0101   }
0102 
0103   if (tcc2dcc_) {
0104     int tp[nTtEta][nTtPhi] = {{0}};
0105     getTp(event, EcalSimRawData::tp, tp);
0106     genTccOut(basename_, iEvent, tp);
0107   }
0108 
0109   // SR flags:
0110   int ebSrf[nTtEta][nTtPhi];
0111   int eeSrf[nEndcaps][nScX][nScY];
0112 
0113   if (srp2dcc_) {
0114     getSrfs(event, ebSrf, eeSrf);
0115     genSrData(basename_, iEvent, ebSrf);
0116   }
0117 }
0118 
0119 void EcalSimRawData::elec2GeomNum(int ittEta0, int ittPhi0, int strip1, int ch1, int &iEta0, int &iPhi0) const {
0120   assert(0 <= ittEta0 && ittEta0 < nEbTtEta);
0121   assert(0 <= ittPhi0 && ittPhi0 < nTtPhi);
0122   assert(1 <= strip1 && strip1 <= ttEdge);
0123   assert(1 <= ch1 && ch1 <= ttEdge);
0124   const int type = ttType[ittEta0];
0125   iEta0 = ittEta0 * ttEdge + strip2Eta[type][strip1 - 1];
0126   iPhi0 = ittPhi0 * ttEdge + stripCh2Phi[type][strip1 - 1][ch1 - 1];
0127   assert(0 <= iEta0 && iEta0 < nEbEta);
0128   assert(0 <= iPhi0 && iPhi0 < nEbPhi);
0129 }
0130 
0131 void EcalSimRawData::fwrite(std::ofstream &f, uint16_t data, int &iWord, bool hpar) const {
0132   if (hpar) {
0133     // set horizontal odd parity bit:
0134     setHParity(data);
0135   }
0136 
0137   switch (writeMode_) {
0138     case littleEndian: {
0139       char c = data & 0x00FF;
0140       f.write(&c, sizeof(c));
0141       c = (data >> 8) & 0x00FF;
0142       f.write(&c, sizeof(c));
0143     } break;
0144     case bigEndian: {
0145       char c = (data >> 8) & 0x00FF;
0146       f.write(&c, sizeof(c));
0147       c = data & 0x00FF;
0148       f.write(&c, sizeof(c));
0149     } break;
0150     case ascii:
0151       f << ((iWord % 8 == 0 && iWord != 0) ? "\n" : "") << "0x" << std::setfill('0') << std::setw(4) << std::hex << data
0152         << "\t" << std::dec << std::setfill(' ');
0153       break;
0154   }
0155   ++iWord;
0156 }
0157 
0158 std::string EcalSimRawData::getExt() const {
0159   switch (writeMode_) {
0160     case littleEndian:
0161       return ".le";
0162     case bigEndian:
0163       return ".be";
0164     case ascii:
0165       return ".txt";
0166     default:
0167       return ".?";
0168   }
0169 }
0170 
0171 void EcalSimRawData::genFeData(std::string &basename,
0172                                int iEvent,
0173                                const std::vector<uint16_t> adcCount[nEbEta][nEbPhi]) const {
0174   int smf = 0;
0175   int gmf = 0;
0176   int nPendingEvt = 0;
0177   int monitorFlag = 0;
0178   int chFrameLen = adcCount[0][0].size() + 1;
0179 
0180   int iWord = 0;
0181 
0182   for (int iZ0 = 0; iZ0 < 2; ++iZ0) {
0183     for (int iDccPhi0 = 0; iDccPhi0 < nDccInPhi; ++iDccPhi0) {
0184       int iDcc1 = iDccPhi0 + iZ0 * nDccInPhi + nDccEndcap + 1;
0185 
0186       if (dccNum_ != -1 && dccNum_ != iDcc1)
0187         continue;
0188 
0189       std::stringstream s;
0190       s.str("");
0191       const std::string &ext = getExt();
0192       s << basename << "_fe2dcc" << std::setfill('0') << std::setw(2) << iDcc1 << std::setfill(' ') << ext;
0193       std::ofstream f(s.str().c_str(), (iEvent == 1 ? std::ios::ate : std::ios::app));
0194 
0195       if (f.fail())
0196         return;
0197 
0198       if (writeMode_ == ascii) {
0199         f << (iEvent == 1 ? "" : "\n") << "[Event:" << iEvent << "]\n";
0200       }
0201 
0202       for (int iTtEtaInSm0 = 0; iTtEtaInSm0 < nTtSmEta; ++iTtEtaInSm0) {
0203         int iTtEta0 = iZ0 * nTtSmEta + iTtEtaInSm0;
0204         for (int iTtPhiInSm0 = 0; iTtPhiInSm0 < nTtSmPhi; ++iTtPhiInSm0) {
0205           // phi=0deg at middle of 1st barrel DCC:
0206           int iTtPhi0 = -nTtPhisPerEbDcc / 2 + iDccPhi0 * nTtPhisPerEbDcc + iTtPhiInSm0;
0207           if (iTtPhi0 < 0)
0208             iTtPhi0 += nTtPhi;
0209           for (int stripId1 = 1; stripId1 <= ttEdge; ++stripId1) {
0210             uint16_t stripHeader =
0211                 0xF << 11 | (nPendingEvt & 0x3F) << 5 | (gmf & 0x1) << 4 | (smf & 0x1) << 3 | (stripId1 & 0x7);
0212             ///     stripHeader |= parity(stripHeader) << 15;
0213             fwrite(f, stripHeader, iWord);
0214 
0215             for (int xtalId1 = 1; xtalId1 <= ttEdge; ++xtalId1) {
0216               uint16_t crystalHeader = 1 << 14 | (chFrameLen & 0xFF) << 4 | (monitorFlag & 0x1) << 3 | (xtalId1 & 0x7);
0217               //          crystalHeader |=parity(crystalHeader) << 15;
0218               fwrite(f, crystalHeader, iWord);
0219 
0220               int iEta0;
0221               int iPhi0;
0222               elec2GeomNum(iTtEta0, iTtPhi0, stripId1, xtalId1, iEta0, iPhi0);
0223               if (xtalVerbose_) {
0224                 std::cout << std::dec << "iDcc1 = " << iDcc1 << "\t"
0225                           << "iEbTtEta0 = " << iTtEta0 << "\t"
0226                           << "iEbTtPhi0 = " << iTtPhi0 << "\t"
0227                           << "stripId1 = " << stripId1 << "\t"
0228                           << "xtalId1 = " << xtalId1 << "\t"
0229                           << "iEta0 = " << iEta0 << "\t"
0230                           << "iPhi0 = " << iPhi0 << "\t"
0231                           << "adc[5] = 0x" << std::hex << adcCount[iEta0][iPhi0][5] << std::dec << "\n";
0232               }
0233 
0234               const std::vector<uint16_t> &adc = adcCount[iEta0][iPhi0];
0235               for (unsigned iSample = 0; iSample < adc.size(); ++iSample) {
0236                 uint16_t data = adc[iSample] & 0x3FFF;
0237                 //      data |= parity(data);
0238                 fwrite(f, data, iWord);
0239               }  // next time sample
0240             }    // next crystal in strip
0241           }      // next strip in TT
0242         }        // next TT along phi
0243       }          // next TT along eta
0244     }            // next DCC
0245   }              // next half-barrel
0246 }
0247 
0248 void EcalSimRawData::genSrData(std::string &basename, int iEvent, int srf[nEbTtEta][nTtPhi]) const {
0249   for (int iZ0 = 0; iZ0 < 2; ++iZ0) {
0250     for (int iDccPhi0 = 0; iDccPhi0 < nDccInPhi; ++iDccPhi0) {
0251       int iDcc1 = iDccPhi0 + iZ0 * nDccInPhi + nDccEndcap + 1;
0252       if (dccNum_ != -1 && dccNum_ != iDcc1)
0253         continue;
0254       std::stringstream s;
0255       s.str("");
0256       s << basename << "_ab2dcc" << std::setfill('0') << std::setw(2) << iDcc1 << std::setfill(' ') << getExt();
0257       std::ofstream f(s.str().c_str(), (iEvent == 1 ? std::ios::ate : std::ios::app));
0258 
0259       if (f.fail())
0260         throw cms::Exception(std::string("Cannot create/open file ") + s.str() + ".");
0261 
0262       int iWord = 0;
0263 
0264       if (writeMode_ == ascii) {
0265         f << (iEvent == 1 ? "" : "\n") << "[Event:" << iEvent << "]\n";
0266       }
0267 
0268       const uint16_t le1 = 0;
0269       const uint16_t le0 = 0;
0270       const uint16_t h1 = 1;
0271       const uint16_t nFlags = 68;
0272       uint16_t data = (h1 & 0x1) << 14 | (le1 & 0x1) << 12 | (le0 & 0x1) << 11 | (nFlags & 0x7F);
0273 
0274       fwrite(f, data, iWord, true);
0275 
0276       int iFlag = 0;
0277       data = 0;
0278 
0279       for (int iTtEtaInSm0 = 0; iTtEtaInSm0 < nTtSmEta; ++iTtEtaInSm0) {
0280         //  int iTtEbEta0 = iZ0*nTtSmEta + iTtEtaInSm0;
0281         int iTtEta0 = nEeTtEta + iZ0 * nTtSmEta + iTtEtaInSm0;
0282         for (int iTtPhiInSm0 = 0; iTtPhiInSm0 < nTtSmPhi; ++iTtPhiInSm0) {
0283           // phi=0deg at middle of 1st barrel DCC:
0284           int iTtPhi0 = -nTtPhisPerEbDcc / 2 + iDccPhi0 * nTtPhisPerEbDcc + iTtPhiInSm0;
0285           if (iTtPhi0 < 0)
0286             iTtPhi0 += nTtPhi;
0287           // flags are packed by four:
0288           //|15 |14 |13-12 |11      9|8      6|5      3|2      0|
0289           //| P | 0 | X  X |  srf i+3| srf i+2| srf i+1| srf i  |
0290           //|   |   |      | field 3 |field 2 | field 1| field 0|
0291           const int field = iFlag % 4;
0292           // std::cout << "TtEta0: " << iTtEta0 << "\tTtPhi0: " << iTtPhi0 << "\n";
0293           // std::cout << "#" << oct << (int)srf[iTtEta0][iTtPhi0] << "o ****> #" <<
0294           // oct << (srf[iTtEta0][iTtPhi0] << (field*3)) << "o\n" << std::dec;
0295 
0296           data |= srf[iTtEta0][iTtPhi0] << (field * 3);
0297 
0298           if (field == 3) {
0299             // std::cout <<  srf[iTtEta0][iTtPhi0] << "----> 0x" << std::hex << data <<
0300             // "\n";
0301             fwrite(f, data, iWord, true);
0302             data = 0;
0303           }
0304           ++iFlag;
0305         }  // next TT along phi
0306       }    // next TT along eta
0307     }      // next DCC
0308   }        // next half-barrel
0309 }
0310 
0311 void EcalSimRawData::genTccIn(std::string &basename, int iEvent, const int tcp[nTtEta][nTtPhi]) const {
0312   for (int iZ0 = 0; iZ0 < 2; ++iZ0) {
0313     for (int iTccPhi0 = 0; iTccPhi0 < nTccInPhi; ++iTccPhi0) {
0314       int iTcc1 = iTccPhi0 + iZ0 * nTccInPhi + nTccEndcap + 1;
0315 
0316       if (tccNum_ != -1 && tccNum_ != iTcc1)
0317         continue;
0318 
0319       std::stringstream s;
0320       s.str("");
0321       const char *ext = ".txt";  // only ascii mode supported for TCP
0322 
0323       s << basename << "_tcc" << std::setfill('0') << std::setw(2) << iTcc1 << std::setfill(' ') << ext;
0324       std::ofstream fe2tcc(s.str().c_str(), (iEvent == 1 ? std::ios::ate : std::ios::app));
0325 
0326       if (fe2tcc.fail())
0327         throw cms::Exception(std::string("Failed to create file ") + s.str() + ".");
0328 
0329       int memPos = iEvent - 1;
0330       int iCh1 = 1;
0331       for (int iTtEtaInSm0 = 0; iTtEtaInSm0 < nTtSmEta; ++iTtEtaInSm0) {
0332         int iTtEta0 = (iZ0 == 0) ? 27 - iTtEtaInSm0 : 28 + iTtEtaInSm0;
0333         for (int iTtPhiInSm0 = 0; iTtPhiInSm0 < nTtSmPhi; ++iTtPhiInSm0) {
0334           // phi=0deg at middle of 1st barrel DCC:
0335           int iTtPhi0 = -nTtPhisPerEbTcc / 2 + iTccPhi0 * nTtPhisPerEbTcc + iTtPhiInSm0;
0336           iTtPhi0 += nTtPhisPerEbTcc * iTccPhi0;
0337           if (iTtPhi0 < 0)
0338             iTtPhi0 += nTtPhi;
0339           uint16_t tp_fe2tcc = (tcp[iTtEta0][iTtPhi0] & 0x7ff);  // keep only Et (9:0) and FineGrain (10)
0340 
0341           if (tpVerbose_ && tp_fe2tcc != 0) {
0342             std::cout << std::dec << "iTcc1 = " << iTcc1 << "\t"
0343                       << "iTtEta0 = " << iTtEta0 << "\t"
0344                       << "iTtPhi0 = " << iTtPhi0 << "\t"
0345                       << "iCh1 = " << iCh1 << "\t"
0346                       << "memPos = " << memPos << "\t"
0347                       << "tp = 0x" << std::setfill('0') << std::hex << std::setw(3) << tp_fe2tcc << std::dec
0348                       << std::setfill(' ') << "\n";
0349           }
0350           fe2tcc << iCh1 << "\t" << memPos << "\t" << std::setfill('0') << std::hex << "0x" << std::setw(4) << tp_fe2tcc
0351                  << "\t"
0352                  << "0" << std::dec << std::setfill(' ') << "\n";
0353           ++iCh1;
0354         }  // next TT along phi
0355       }    // next TT along eta
0356       fe2tcc << std::flush;
0357       fe2tcc.close();
0358     }  // next TCC
0359   }    // next half-barrel
0360 }
0361 
0362 void EcalSimRawData::genTccOut(std::string &basename, int iEvent, const int tps[nTtEta][nTtPhi]) const {
0363   int iDccWord = 0;
0364 
0365   for (int iZ0 = 0; iZ0 < 2; ++iZ0) {
0366     for (int iTccPhi0 = 0; iTccPhi0 < nTccInPhi; ++iTccPhi0) {
0367       int iTcc1 = iTccPhi0 + iZ0 * nTccInPhi + nTccEndcap + 1;
0368 
0369       if (tccNum_ != -1 && tccNum_ != iTcc1)
0370         continue;
0371 
0372       std::stringstream s;
0373       s.str("");
0374       const char *ext = ".txt";  // only ascii mode supported for TCP
0375 
0376       s << basename << "_tcc" << std::setfill('0') << std::setw(2) << iTcc1 << std::setfill(' ') << ext;
0377 
0378       s.str("");
0379       s << basename << "_tcc2dcc" << std::setfill('0') << std::setw(2) << iTcc1 << std::setfill(' ') << getExt();
0380       std::ofstream dccF(s.str().c_str(), (iEvent == 1 ? std::ios::ate : std::ios::app));
0381 
0382       if (dccF.fail()) {
0383         std::cout << "Warning: failed to create or open file " << s.str() << ".\n";
0384         return;
0385       }
0386 
0387       const uint16_t h1 = 1;
0388       const uint16_t le1 = 0;
0389       const uint16_t le0 = 0;
0390       const uint16_t nSamples = 1;
0391       const uint16_t nTts = 68;
0392       const uint16_t data =
0393           (h1 & 0x1) << 14 | (le1 & 0x1) << 12 | (le0 & 0x1) << 11 | (nSamples & 0xF) << 7 | (nTts & 0x7F);
0394       dccF << (iEvent == 1 ? "" : "\n") << "[Event:" << iEvent << "]\n";
0395       fwrite(dccF, data, iDccWord, false);
0396 
0397       int memPos = iEvent - 1;
0398       int iCh1 = 1;
0399       for (int iTtEtaInSm0 = 0; iTtEtaInSm0 < nTtSmEta; ++iTtEtaInSm0) {
0400         int iTtEta0 = nEeTtEta + iZ0 * nTtSmEta + iTtEtaInSm0;
0401         for (int iTtPhiInSm0 = 0; iTtPhiInSm0 < nTtSmPhi; ++iTtPhiInSm0) {
0402           // phi=0deg at middle of 1st barrel DCC:
0403           int iTtPhi0 = -nTtPhisPerEbTcc / 2 + iTccPhi0 * nTtPhisPerEbTcc + iTtPhiInSm0;
0404           if (iTtPhi0 < 0)
0405             iTtPhi0 += nTtPhi;
0406 
0407           if (tpVerbose_) {
0408             std::cout << std::dec << "iTcc1 = " << iTcc1 << "\t"
0409                       << "iTtEta0 = " << iTtEta0 << "\t"
0410                       << "iTtPhi0 = " << iTtPhi0 << "\t"
0411                       << "iCh1 = " << iCh1 << "\t"
0412                       << "memPos = " << memPos << "\t"
0413                       << "tp = 0x" << std::hex << tps[iTtEta0][iTtPhi0] << std::dec << "\n";
0414           }
0415           fwrite(dccF, tps[iTtEta0][iTtPhi0], iDccWord, false);
0416           ++iCh1;
0417         }  // next TT along phi
0418       }    // next TT along eta
0419     }      // next TCC
0420   }        // next half-barrel
0421 }
0422 
0423 void EcalSimRawData::setHParity(uint16_t &a) const {
0424   const int odd = 1 << 15;
0425   const int even = 0;
0426   // parity bit of numbers from 0x0 to 0xF:
0427   //                    0   1   2    3   4    5    6   7   8    9    A   B    C
0428   //                    D   E    F
0429   const int p[16] = {even, odd, odd, even, odd, even, even, odd, odd, even, even, odd, even, odd, odd, even};
0430   // inverts parity bit (LSB) of 'a' in case of even parity:
0431   a ^= p[a & 0xF] ^ p[(a >> 4) & 0xF] ^ p[(a >> 8) & 0xF] ^ p[a >> 12 & 0xF] ^ odd;
0432 }
0433 
0434 void EcalSimRawData::getSrfs(const edm::Event &event,
0435                              int ebSrf[nTtEta][nTtPhi],
0436                              int eeSrf[nEndcaps][nScX][nScY]) const {
0437   // EE
0438   const auto &hEeSrFlags = event.getHandle(eeSrFlagToken_);
0439   for (size_t i = 0; i < (nEndcaps * nScX * nScY); ((int *)eeSrf)[i++] = -1) {
0440   };
0441   if (hEeSrFlags.isValid()) {
0442     for (EESrFlagCollection::const_iterator it = hEeSrFlags->begin(); it != hEeSrFlags->end(); ++it) {
0443       const EESrFlag &flag = *it;
0444       int iZ0 = flag.id().zside() > 0 ? 1 : 0;
0445       int iX0 = flag.id().ix() - 1;
0446       int iY0 = flag.id().iy() - 1;
0447       assert(iZ0 >= 0 && iZ0 < nEndcaps);
0448       assert(iX0 >= 0 && iX0 < nScX);
0449       assert(iY0 >= 0 && iY0 < nScY);
0450       eeSrf[iZ0][iX0][iY0] = flag.value();
0451     }
0452   } else {
0453     edm::LogWarning("EcalSimRawData") << "EE SR flag not found (Product label: " << srDigiProducer_
0454                                       << "Producet instance: " << eeSrFlagCollection_ << ")";
0455   }
0456 
0457   // EB
0458   const auto &hEbSrFlags = event.getHandle(ebSrFlagToken_);
0459   for (size_t i = 0; i < (nTtEta * nTtPhi); ((int *)ebSrf)[i++] = -1) {
0460   };
0461   if (hEbSrFlags.isValid()) {
0462     for (EBSrFlagCollection::const_iterator it = hEbSrFlags->begin(); it != hEbSrFlags->end(); ++it) {
0463       const EBSrFlag &flag = *it;
0464       int iEta = flag.id().ieta();
0465       int iEta0 = iEta + nTtEta / 2 - (iEta >= 0 ? 1 : 0);  // 0->55 from eta=-3 to eta=3
0466       int iEbEta0 = iEta0 - nEeTtEta;                       // 0->33 from eta=-1.48 to eta=1.48
0467       int iPhi0 = flag.id().iphi() - 1;
0468 
0469       assert(iEbEta0 >= 0 && iEbEta0 < nEbTtEta);
0470       assert(iPhi0 >= 0 && iPhi0 < nTtPhi);
0471 
0472       ebSrf[iEbEta0][iPhi0] = flag.value();
0473     }
0474   } else {
0475     edm::LogWarning("EcalSimRawData") << "EB SR flag not found (Product label: " << srDigiProducer_
0476                                       << "Producet instance: " << ebSrFlagCollection_ << ")";
0477   }
0478 }
0479 
0480 void EcalSimRawData::getEbDigi(const edm::Event &event, std::vector<uint16_t> adc[nEbEta][nEbPhi]) const {
0481   const auto &hEbDigis = event.getHandle(ebDigisToken_);
0482 
0483   int nSamples = 0;
0484   if (hEbDigis.isValid() && !hEbDigis->empty()) {  // there is at least one digi
0485     nSamples = hEbDigis->begin()->size();          // gets the sample count from 1st digi
0486   }
0487 
0488   const uint16_t suppressed = 0xFFFF;
0489 
0490   adc[0][0] = std::vector<uint16_t>(nSamples, suppressed);
0491 
0492   for (int iEbEta = 0; iEbEta < nEbEta; ++iEbEta) {
0493     for (int iEbPhi = 0; iEbPhi < nEbPhi; ++iEbPhi) {
0494       adc[iEbEta][iEbPhi] = adc[0][0];
0495     }
0496   }
0497   if (hEbDigis.isValid()) {
0498     if (xtalVerbose_)
0499       std::cout << std::setfill('0');
0500     for (EBDigiCollection::const_iterator it = hEbDigis->begin(); it != hEbDigis->end(); ++it) {
0501       const EBDataFrame &frame = *it;
0502 
0503       int iEta0 = iEta2cIndex((frame.id()).ieta());
0504       int iPhi0 = iPhi2cIndex((frame.id()).iphi());
0505 
0506       //     std::cout << "xtl indices conv: (" << frame.id().ieta() << ","
0507       //     << frame.id().iphi() << ") -> ("
0508       //     << iEta0 << "," << iPhi0 << ")\n";
0509 
0510       if (iEta0 < 0 || iEta0 >= nEbEta) {
0511         std::cout << "iEta0 (= " << iEta0 << ") is out of range ("
0512                   << "[0," << nEbEta - 1 << "])\n";
0513       }
0514       if (iPhi0 < 0 || iPhi0 >= nEbPhi) {
0515         std::cout << "iPhi0 (= " << iPhi0 << ") is out of range ("
0516                   << "[0," << nEbPhi - 1 << "])\n";
0517       }
0518 
0519       if (xtalVerbose_) {
0520         std::cout << iEta0 << "\t" << iPhi0 << ":\t";
0521         std::cout << std::hex;
0522       }
0523 
0524       if (nSamples != frame.size()) {
0525         throw cms::Exception("EcalSimRawData",
0526                              "Found EB digis with different sample count! This "
0527                              "is not supported by EcalSimRawData.");
0528       }
0529 
0530       for (int iSample = 0; iSample < nSamples; ++iSample) {
0531         const EcalMGPASample &sample = frame.sample(iSample);
0532         uint16_t encodedAdc = sample.raw();
0533         adc[iEta0][iPhi0][iSample] = encodedAdc;
0534         if (xtalVerbose_) {
0535           std::cout << (iSample > 0 ? " " : "") << "0x" << std::setw(4) << encodedAdc;
0536         }
0537       }
0538       if (xtalVerbose_)
0539         std::cout << "\n" << std::dec;
0540     }
0541     if (xtalVerbose_)
0542       std::cout << std::setfill(' ');
0543   }
0544 }
0545 
0546 void EcalSimRawData::getTp(const edm::Event &event, EcalSimRawData::tokenType type, int tcp[nTtEta][nTtPhi]) const {
0547   const auto &hTpDigis = event.getHandle(trigPrimDigisToken_[type]);
0548   if (hTpDigis.isValid() && !hTpDigis->empty()) {
0549     const EcalTrigPrimDigiCollection &tpDigis = *hTpDigis.product();
0550 
0551     //    EcalSelectiveReadout::ttFlag_t ttf[nTtEta][nTtPhi];
0552     for (int iTtEta0 = 0; iTtEta0 < nTtEta; ++iTtEta0) {
0553       for (int iTtPhi0 = 0; iTtPhi0 < nTtPhi; ++iTtPhi0) {
0554         tcp[iTtEta0][iTtPhi0] = tccInDefaultVal_;
0555       }
0556     }
0557     if (tpVerbose_) {
0558       std::cout << std::setfill('0');
0559     }
0560     for (EcalTrigPrimDigiCollection::const_iterator it = tpDigis.begin(); it != tpDigis.end(); ++it) {
0561       const EcalTriggerPrimitiveDigi &tp = *it;
0562       int iTtEta0 = iTtEta2cIndex(tp.id().ieta());
0563       int iTtPhi0 = iTtPhi2cIndex(tp.id().iphi());
0564       if (iTtEta0 < 0 || iTtEta0 >= nTtEta) {
0565         std::cout << "iTtEta0 (= " << iTtEta0 << ") is out of range ("
0566                   << "[0," << nEbTtEta - 1 << "])\n";
0567       }
0568       if (iTtPhi0 < 0 || iTtPhi0 >= nTtPhi) {
0569         std::cout << "iTtPhi0 (= " << iTtPhi0 << ") is out of range ("
0570                   << "[0," << nTtPhi - 1 << "])\n";
0571       }
0572 
0573       tcp[iTtEta0][iTtPhi0] = tp[tp.sampleOfInterest()].raw();
0574 
0575       if (tpVerbose_) {
0576         if (tcp[iTtEta0][iTtPhi0] != 0) {  // print non-zero values only
0577           std::string collName = (type == 0) ? tcpDigiCollection_ : tpDigiCollection_;
0578           std::cout << collName << (collName.empty() ? "" : " ") << "TP(" << std::setw(2) << iTtEta0 << "," << iTtPhi0
0579                     << ") = "
0580                     << "0x" << std::setw(4) << tcp[iTtEta0][iTtPhi0] << "\tcmssw indices: " << tp.id().ieta() << " "
0581                     << tp.id().iphi() << "\n";
0582         }
0583       }
0584     }  // next TP
0585     if (tpVerbose_)
0586       std::cout << std::setfill(' ');
0587   }
0588 }