CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_13_patch3/src/SimCalorimetry/EcalElectronicsEmulation/src/EcalSimRawData.cc

Go to the documentation of this file.
00001 #include "SimCalorimetry/EcalElectronicsEmulation/interface/EcalSimRawData.h"
00002 #include "FWCore/Framework/interface/Event.h"
00003 #include "FWCore/Framework/interface/EventSetup.h"
00004 #include "Geometry/Records/interface/IdealGeometryRecord.h"
00005 #include "FWCore/Framework/interface/ESHandle.h"
00006 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00007 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00008 
00009 #include <memory>
00010 
00011 #include <fstream> //used for debugging
00012 #include <iostream>
00013 #include <iomanip>
00014 #include <cmath>
00015 #include "DataFormats/EcalDigi/interface/EcalMGPASample.h"
00016 #include "FWCore/Utilities/interface/Exception.h"
00017 
00018 using namespace std;
00019 using namespace edm;
00020 
00021 const int EcalSimRawData::ttType[nEbTtEta] = {
00022   0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,1, //EE-
00023   0,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1//EE+
00024 };
00025 
00026 const int EcalSimRawData::stripCh2Phi[nTtTypes][ttEdge][ttEdge] = {
00027   //TT type 0:
00028   /*ch-->*/
00029   {{4,3,2,1,0}, /*strip*/               
00030    {0,1,2,3,4}, /*|*/
00031    {4,3,2,1,0}, /*|*/
00032    {0,1,2,3,4}, /*|*/
00033    {4,3,2,1,0}},/*V*/
00034   //TT type 1:
00035   {{0,1,2,3,4},
00036    {4,3,2,1,0},
00037    {0,1,2,3,4},
00038    {4,3,2,1,0},
00039    {0,1,2,3,4}}
00040 };
00041 
00042 const int EcalSimRawData::strip2Eta[nTtTypes][ttEdge] = {
00043   {4,3,2,1,0}, //TT type 0
00044   {0,1,2,3,4}  //TT type 1
00045 };
00046 
00047 EcalSimRawData::EcalSimRawData(const edm::ParameterSet& params){
00048   //sets up parameters:
00049   digiProducer_ = params.getParameter<string>("unsuppressedDigiProducer");
00050   ebDigiCollection_ = params.getParameter<std::string>("EBdigiCollection");
00051   eeDigiCollection_ = params.getParameter<std::string>("EEdigiCollection");
00052   srDigiProducer_ = params.getParameter<string>("srProducer");
00053   ebSrFlagCollection_ = params.getParameter<std::string>("EBSrFlagCollection");
00054   eeSrFlagCollection_ = params.getParameter<std::string>("EESrFlagCollection");
00055   tpDigiCollection_
00056     = params.getParameter<std::string>("trigPrimDigiCollection");
00057   tcpDigiCollection_ = params.getParameter<std::string>("tcpDigiCollection");
00058   tpProducer_ = params.getParameter<string>("trigPrimProducer");
00059   xtalVerbose_ = params.getUntrackedParameter<bool>("xtalVerbose", false);
00060   tpVerbose_ = params.getUntrackedParameter<bool>("tpVerbose", false);
00061   tcc2dcc_ = params.getUntrackedParameter<bool>("tcc2dccData", true);
00062   srp2dcc_ = params.getUntrackedParameter<bool>("srp2dccData", true);
00063   fe2dcc_ = params.getUntrackedParameter<bool>("fe2dccData", true);
00064   fe2tcc_ = params.getUntrackedParameter<bool>("fe2tccData", true);
00065   dccNum_ = params.getUntrackedParameter<int>("dccNum", -1);
00066   tccNum_ = params.getUntrackedParameter<int>("tccNum", -1);
00067   tccInDefaultVal_ = params.getUntrackedParameter<int>("tccInDefaultVal", 0xffff);
00068   basename_ = params.getUntrackedParameter<std::string>("outputBaseName");
00069 
00070 
00071   string writeMode = params.getParameter<string>("writeMode");
00072 
00073   if(writeMode==string("littleEndian")){
00074     writeMode_ = littleEndian;
00075   } else if(writeMode==string("bigEndian")){
00076     writeMode_ = bigEndian;
00077   } else{
00078     writeMode_ = ascii;
00079   }
00080 }
00081 
00082 void
00083 EcalSimRawData::analyze(const edm::Event& event,
00084                         const edm::EventSetup& es){
00085   //Event counter:
00086   static int iEvent = 0;
00087   ++iEvent; 
00088     
00089   if(xtalVerbose_ | tpVerbose_){
00090     cout << "======================================================================\n"
00091          << " Event " << iEvent << "\n"
00092          << "----------------------------------------------------------------------\n";
00093   }
00094 
00095   if(fe2dcc_){
00096     vector<uint16_t> adc[nEbEta][nEbPhi];
00097     getEbDigi(event, adc);
00098     genFeData(basename_, iEvent, adc);
00099   }
00100 
00101   if(fe2tcc_){
00102     int tcp[nTtEta][nTtPhi]={{0}};
00103     getTp(event, tcpDigiCollection_, tcp);
00104     genTccIn(basename_, iEvent, tcp);
00105   }
00106   
00107   if(tcc2dcc_){
00108     int tp[nTtEta][nTtPhi]={{0}};
00109     getTp(event, tpDigiCollection_, tp); 
00110     genTccOut(basename_, iEvent, tp);
00111   }
00112   
00113   //SR flags:
00114   int ebSrf[nTtEta][nTtPhi];
00115   int eeSrf[nEndcaps][nScX][nScY];
00116 
00117   if(srp2dcc_){
00118     getSrfs(event, ebSrf, eeSrf);
00119     genSrData(basename_, iEvent, ebSrf);
00120   }
00121   
00122 }
00123 
00124 void EcalSimRawData::elec2GeomNum(int ittEta0, int ittPhi0, int strip1,
00125                                   int ch1, int& iEta0, int& iPhi0) const{
00126   assert(0<=ittEta0 && ittEta0<nEbTtEta);
00127   assert(0<=ittPhi0 && ittPhi0<nTtPhi);
00128   assert(1<=strip1&& strip1<=ttEdge);
00129   assert(1<=ch1 && ch1<=ttEdge);
00130   const int type = ttType[ittEta0];
00131   iEta0 = ittEta0*ttEdge + strip2Eta[type][strip1-1];
00132   iPhi0 = ittPhi0*ttEdge + stripCh2Phi[type][strip1-1][ch1-1];
00133   assert(0<=iEta0 && iEta0<nEbEta);
00134   assert(0<=iPhi0 && iPhi0<nEbPhi);
00135 }
00136 
00137 void EcalSimRawData::fwrite(ofstream& f, uint16_t data,
00138                             int& iWord, bool hpar) const{
00139 
00140   if(hpar){
00141     //set horizontal odd parity bit:
00142     setHParity(data);
00143   }
00144   
00145   switch(writeMode_){
00146   case littleEndian:
00147     {
00148       char c = data & 0x00FF;
00149       f.write(&c, sizeof(c));
00150       c = (data >>8) & 0x00FF;
00151       f.write(&c, sizeof(c));
00152     }
00153     break;
00154   case bigEndian:
00155     {
00156       char c = (data >>8) & 0x00FF; 
00157       f.write(&c, sizeof(c));
00158       c = data & 0x00FF;
00159       f.write(&c, sizeof(c));
00160     }
00161     break;
00162   case ascii:
00163     f << ((iWord%8==0&&iWord!=0)?"\n":"")
00164       << "0x" << setfill('0') << setw(4) << hex << data << "\t"
00165       << dec << setfill(' ');
00166     break;
00167   }
00168   ++iWord;
00169 }
00170 
00171 string EcalSimRawData::getExt() const{
00172   switch(writeMode_){
00173   case littleEndian:
00174     return ".le";
00175   case bigEndian:
00176     return ".be";
00177   case ascii:
00178     return ".txt";
00179   default:
00180     return".?";
00181   }  
00182 }
00183 
00184 void EcalSimRawData::genFeData(string basename, int iEvent,
00185                                const vector<uint16_t> adcCount[nEbEta][nEbPhi]
00186                                ) const{
00187   int smf = 0;
00188   int gmf = 0;
00189   int nPendingEvt = 0;
00190   int monitorFlag = 0;
00191   int chFrameLen = adcCount[0][0].size() + 1;
00192   
00193   int iWord = 0;
00194   
00195   for(int iZ0 = 0; iZ0<2; ++iZ0){
00196     for(int iDccPhi0 = 0; iDccPhi0<nDccInPhi; ++iDccPhi0){
00197       int iDcc1 = iDccPhi0 + iZ0*nDccInPhi + nDccEndcap + 1;
00198 
00199       if(dccNum_!=-1  && dccNum_!=iDcc1) continue;
00200       
00201       stringstream s;
00202       s.str("");
00203       const string& ext = getExt();
00204       s << basename << "_fe2dcc" << setfill('0') << setw(2) << iDcc1
00205         << setfill(' ') << ext;
00206       ofstream f(s.str().c_str(), (iEvent==1?ios::ate:ios::app));
00207 
00208       if(!f) return;
00209 
00210 
00211       if(writeMode_==ascii){
00212         f << (iEvent==1?"":"\n") << "[Event:" << iEvent << "]\n";
00213       }
00214       
00215       for(int iTtEtaInSm0 = 0; iTtEtaInSm0 < nTtSmEta; ++iTtEtaInSm0){
00216         int iTtEta0 = iZ0*nTtSmEta + iTtEtaInSm0;
00217         for(int iTtPhiInSm0 = 0; iTtPhiInSm0 < nTtSmPhi; ++iTtPhiInSm0){
00218           //phi=0deg at middle of 1st barrel DCC:
00219           int iTtPhi0 = -nTtPhisPerEbDcc/2 + iDccPhi0*nTtPhisPerEbDcc
00220             + iTtPhiInSm0;
00221           if(iTtPhi0<0) iTtPhi0 += nTtPhi;
00222           for(int stripId1 = 1; stripId1 <= ttEdge; ++stripId1){
00223             uint16_t stripHeader =
00224               0xF << 11
00225               | (nPendingEvt & 0x3F) << 5
00226               | (gmf & 0x1) << 4
00227               | (smf & 0x1) << 3
00228               | (stripId1 & 0x7);
00230             fwrite(f,stripHeader, iWord);
00231 
00232             for(int xtalId1 = 1; xtalId1 <= ttEdge; ++xtalId1){
00233               
00234               uint16_t crystalHeader =
00235                 1 <<14
00236                 | (chFrameLen & 0xFF) <<4
00237                 | (monitorFlag & 0x1) <<3
00238                 | (xtalId1 & 0x7);
00239               //              crystalHeader |=parity(crystalHeader) << 15;
00240               fwrite(f, crystalHeader, iWord);
00241               
00242               int iEta0;
00243               int iPhi0;
00244               elec2GeomNum(iTtEta0, iTtPhi0, stripId1, xtalId1,
00245                            iEta0, iPhi0);
00246               if(xtalVerbose_){
00247                 cout << dec
00248                      << "iDcc1 = " << iDcc1 << "\t"
00249                      << "iEbTtEta0 = " << iTtEta0 << "\t"
00250                      << "iEbTtPhi0 = " << iTtPhi0 << "\t"
00251                      << "stripId1 = " << stripId1 << "\t"
00252                      << "xtalId1 = " << xtalId1 << "\t"
00253                      << "iEta0 = " << iEta0 << "\t"
00254                      << "iPhi0 = " << iPhi0 << "\t"
00255                      << "adc[5] = 0x" << hex << adcCount[iEta0][iPhi0][5]
00256                      << dec << "\n";
00257               }
00258               
00259 
00260               const vector<uint16_t>& adc = adcCount[iEta0][iPhi0];
00261               for(unsigned iSample=0; iSample  < adc.size(); ++iSample){
00262                 uint16_t data = adc[iSample] & 0x3FFF;
00263                 //              data |= parity(data);
00264                 fwrite(f, data, iWord);
00265               } //next time sample
00266             } //next crystal in strip
00267           } //next strip in TT
00268         } //next TT along phi
00269       } //next TT along eta
00270     } //next DCC
00271   } //next half-barrel
00272 }
00273 
00274 void EcalSimRawData::genSrData(string basename, int iEvent,
00275                                int srf[nEbTtEta][nTtPhi]) const{
00276   for(int iZ0 = 0; iZ0<2; ++iZ0){
00277     for(int iDccPhi0 = 0; iDccPhi0<nDccInPhi; ++iDccPhi0){
00278       int iDcc1 = iDccPhi0 + iZ0*nDccInPhi + nDccEndcap + 1;
00279       if(dccNum_!=-1  && dccNum_!=iDcc1) continue;
00280       stringstream s;
00281       s.str("");
00282       s << basename << "_ab2dcc" << setfill('0') << setw(2) << iDcc1
00283         << setfill(' ') << getExt();
00284       ofstream f(s.str().c_str(), (iEvent==1?ios::ate:ios::app));
00285       
00286       if(!f) throw cms::Exception(string("Cannot create/open file ")
00287                                   + s.str() + ".");
00288       
00289       int iWord = 0;
00290   
00291       if(writeMode_==ascii){
00292         f << (iEvent==1?"":"\n")<< "[Event:" << iEvent << "]\n";
00293       }
00294 
00295       const uint16_t le1 = 0;
00296       const uint16_t le0 = 0;
00297       const uint16_t h1 = 1;
00298       const uint16_t nFlags = 68;
00299       uint16_t data =  (h1 & 0x1)<< 14
00300         | (le1 & 0x1) << 12
00301         | (le0 & 0x1) << 11
00302         | (nFlags & 0x7F);
00303       
00304       fwrite(f, data, iWord, true);
00305       
00306       int iFlag = 0;
00307       data = 0;
00308 
00309       for(int iTtEtaInSm0 = 0; iTtEtaInSm0 < nTtSmEta; ++iTtEtaInSm0){
00310         //      int iTtEbEta0 = iZ0*nTtSmEta + iTtEtaInSm0;
00311         int iTtEta0 = nEeTtEta + iZ0*nTtSmEta + iTtEtaInSm0;
00312         for(int iTtPhiInSm0 = 0; iTtPhiInSm0 < nTtSmPhi; ++iTtPhiInSm0){
00313           //phi=0deg at middle of 1st barrel DCC:
00314           int iTtPhi0 = -nTtPhisPerEbDcc/2 + iDccPhi0*nTtPhisPerEbDcc
00315             + iTtPhiInSm0;
00316           if(iTtPhi0<0) iTtPhi0 += nTtPhi;
00317           //flags are packed by four:
00318           //|15 |14 |13-12 |11      9|8      6|5      3|2      0| 
00319           //| P | 0 | X  X |  srf i+3| srf i+2| srf i+1| srf i  |
00320           //|   |   |      | field 3 |field 2 | field 1| field 0|
00321           const int field = iFlag%4;
00322           //cout << "TtEta0: " << iTtEta0 << "\tTtPhi0: " << iTtPhi0 << "\n";
00323           //cout << "#" << oct << (int)srf[iTtEta0][iTtPhi0] << "o ****> #" << oct << (srf[iTtEta0][iTtPhi0] << (field*3)) << "o\n" << dec;
00324           
00325           data |= srf[iTtEta0][iTtPhi0] << (field*3);
00326 
00327           if(field==3){
00328             //cout <<  srf[iTtEta0][iTtPhi0] << "----> 0x" << hex << data << "\n";
00329             fwrite(f, data, iWord, true);
00330             data = 0;
00331           }
00332           ++iFlag;
00333         } //next TT along phi
00334       } //next TT along eta
00335     } //next DCC
00336   } //next half-barrel
00337 }
00338 
00339 
00340 void EcalSimRawData::genTccIn(string basename, int iEvent,
00341                               const int tcp[nTtEta][nTtPhi]) const{
00342   for(int iZ0 = 0; iZ0<2; ++iZ0){
00343     for(int iTccPhi0 = 0; iTccPhi0<nTccInPhi; ++iTccPhi0){
00344       int iTcc1 = iTccPhi0 + iZ0*nTccInPhi + nTccEndcap + 1;
00345       
00346       if(tccNum_!=-1  && tccNum_!=iTcc1) continue;
00347       
00348       stringstream s;
00349       s.str("");
00350       const char* ext = ".txt"; //only ascii mode supported for TCP
00351 
00352       s << basename << "_tcc" << setfill('0') << setw(2) << iTcc1
00353         << setfill(' ') << ext;
00354       ofstream fe2tcc(s.str().c_str(), (iEvent==1?ios::ate:ios::app));
00355 
00356       if(!fe2tcc) throw cms::Exception(string("Failed to create file ")
00357                                        + s.str() + ".");
00358       
00359       int memPos = iEvent-1;
00360       int iCh1 = 1;
00361       for(int iTtEtaInSm0 = 0; iTtEtaInSm0 < nTtSmEta; ++iTtEtaInSm0){
00362         int iTtEta0 = (iZ0==0) ? 27 - iTtEtaInSm0 : 28 + iTtEtaInSm0; 
00363         for(int iTtPhiInSm0 = 0; iTtPhiInSm0 < nTtSmPhi; ++iTtPhiInSm0){
00364           //phi=0deg at middle of 1st barrel DCC:
00365           int iTtPhi0 = -nTtPhisPerEbTcc/2 + iTccPhi0*nTtPhisPerEbTcc
00366             + iTtPhiInSm0;
00367           iTtPhi0 += nTtPhisPerEbTcc*iTccPhi0;
00368           if(iTtPhi0<0) iTtPhi0 += nTtPhi;
00369           uint16_t tp_fe2tcc = (tcp[iTtEta0][iTtPhi0] & 0x7ff) ; //keep only Et (9:0) and FineGrain (10)
00370           
00371           if(tpVerbose_ && tp_fe2tcc!=0){
00372             cout << dec
00373                  << "iTcc1 = " << iTcc1 << "\t"
00374                  << "iTtEta0 = " << iTtEta0 << "\t"
00375                  << "iTtPhi0 = " << iTtPhi0 << "\t"
00376                  << "iCh1 = " << iCh1 << "\t"
00377                  << "memPos = " << memPos << "\t" 
00378                  << "tp = 0x" << setfill('0') << hex << setw(3)
00379                  << tp_fe2tcc
00380                  << dec << setfill(' ') << "\n";
00381           }
00382           fe2tcc << iCh1 << "\t"
00383                  << memPos << "\t"
00384                  << setfill('0') << hex
00385                  << "0x" << setw(4) << tp_fe2tcc << "\t"
00386                  << "0"
00387                  << dec << setfill(' ') << "\n";
00388           ++iCh1;
00389         } //next TT along phi
00390       } //next TT along eta
00391       fe2tcc << std::flush;
00392       fe2tcc.close();
00393     } //next TCC
00394   } //next half-barrel
00395 }
00396 
00397 
00398 void EcalSimRawData::genTccOut(string basename, int iEvent,
00399                                const int tps[nTtEta][nTtPhi]) const{
00400   int iDccWord = 0;
00401 
00402   for(int iZ0 = 0; iZ0<2; ++iZ0){
00403     for(int iTccPhi0 = 0; iTccPhi0<nTccInPhi; ++iTccPhi0){
00404       int iTcc1 = iTccPhi0 + iZ0*nTccInPhi + nTccEndcap + 1;
00405 
00406       if(tccNum_!=-1  && tccNum_!=iTcc1) continue;
00407       
00408       stringstream s;
00409       s.str("");
00410       const char* ext = ".txt"; //only ascii mode supported for TCP
00411 
00412       s << basename << "_tcc" << setfill('0') << setw(2) << iTcc1
00413         << setfill(' ') << ext;
00414 
00415       s.str("");
00416       s << basename << "_tcc2dcc" << setfill('0') << setw(2) << iTcc1
00417         << setfill(' ') << getExt();
00418       ofstream dccF(s.str().c_str(), (iEvent==1?ios::ate:ios::app));
00419       
00420       if(!dccF){
00421         cout << "Warning: failed to create or open file " << s << ".\n";
00422         return;
00423       }
00424       
00425       const uint16_t h1 = 1;
00426       const uint16_t le1 = 0;
00427       const uint16_t le0 = 0;
00428       const uint16_t nSamples = 1;
00429       const uint16_t nTts = 68;
00430       const uint16_t data = (h1 & 0x1) << 14
00431         | (le1 & 0x1) << 12
00432         | (le0 & 0x1) << 11
00433         | (nSamples & 0xF) << 7
00434         | (nTts & 0x7F);        
00435       dccF << (iEvent==1?"":"\n") << "[Event:" << iEvent << "]\n";
00436       fwrite(dccF, data, iDccWord, false);
00437       
00438       int memPos = iEvent-1;
00439       int iCh1 = 1;
00440       for(int iTtEtaInSm0 = 0; iTtEtaInSm0 < nTtSmEta; ++iTtEtaInSm0){
00441         int iTtEta0 = nEeTtEta + iZ0*nTtSmEta + iTtEtaInSm0;
00442         for(int iTtPhiInSm0 = 0; iTtPhiInSm0 < nTtSmPhi; ++iTtPhiInSm0){
00443           //phi=0deg at middle of 1st barrel DCC:
00444           int iTtPhi0 = -nTtPhisPerEbTcc/2 + iTccPhi0*nTtPhisPerEbTcc
00445             + iTtPhiInSm0;
00446           if(iTtPhi0<0) iTtPhi0 += nTtPhi;
00447           
00448           if(tpVerbose_){
00449             cout << dec
00450                  << "iTcc1 = " << iTcc1 << "\t"
00451                  << "iTtEta0 = " << iTtEta0 << "\t"
00452                  << "iTtPhi0 = " << iTtPhi0 << "\t"
00453                  << "iCh1 = " << iCh1 << "\t"
00454                  << "memPos = " << memPos << "\t" 
00455                  << "tp = 0x" << hex << tps[iTtEta0][iTtPhi0]
00456                  << dec << "\n";
00457           }
00458           fwrite(dccF, tps[iTtEta0][iTtPhi0], iDccWord, false);
00459           ++iCh1;
00460         } //next TT along phi
00461       } //next TT along eta
00462     } //next TCC
00463   } //next half-barrel
00464 }
00465 
00466 
00467 void EcalSimRawData::setHParity(uint16_t& a) const{
00468   const int odd = 1 <<15;
00469   const int even = 0;
00470   //parity bit of numbers from 0x0 to 0xF:
00471   //                    0   1   2    3   4    5    6   7   8    9    A   B    C   D   E    F           
00472   const int p[16] = {even,odd,odd,even,odd,even,even,odd,odd,even,even,odd,even,odd,odd,even};
00473   //inverts parity bit (LSB) of 'a' in case of even parity:
00474   a ^= p[a&0xF] ^ p[(a>>4)&0xF] ^ p[(a>>8)&0xF] ^ p[a>>12&0xF] ^ odd;   
00475 }
00476 
00477 void EcalSimRawData::getSrfs(const edm::Event& event,
00478                              int ebSrf[nTtEta][nTtPhi],
00479                              int eeSrf[nEndcaps][nScX][nScY]) const{
00480 
00481   //EE
00482   edm::Handle<EESrFlagCollection> hEeSrFlags;
00483   event.getByLabel(srDigiProducer_, eeSrFlagCollection_, hEeSrFlags);
00484   for(size_t i=0; i < sizeof(eeSrf)/sizeof(int); ((int*)eeSrf)[i++] = -1){};
00485   if(hEeSrFlags.isValid()){
00486     for(EESrFlagCollection::const_iterator it = hEeSrFlags->begin();
00487         it != hEeSrFlags->end(); ++it){
00488       const EESrFlag& flag = *it;
00489       int iZ0 = flag.id().zside()>0?1:0;
00490       int iX0 = flag.id().ix()-1;
00491       int iY0 = flag.id().iy()-1;
00492       assert(iZ0>=0 && iZ0<nEndcaps);
00493       assert(iX0>=0 && iX0<nScX);
00494       assert(iY0>=0 && iY0<nScY);
00495       eeSrf[iZ0][iX0][iY0] = flag.value();
00496     }
00497   } else{
00498     LogWarning("EcalSimRawData") << "EE SR flag not found ("
00499                                  << "Product label: " << srDigiProducer_
00500                                  << "Producet instance: "
00501                                  << eeSrFlagCollection_ << ")";
00502   }
00503 
00504   //EB
00505   edm::Handle<EBSrFlagCollection> hEbSrFlags;
00506   event.getByLabel(srDigiProducer_, ebSrFlagCollection_, hEbSrFlags);
00507   for(size_t i=0; i<sizeof(ebSrf)/sizeof(int); ((int*)ebSrf)[i++] = -1){};
00508   if(hEbSrFlags.isValid()){
00509     for(EBSrFlagCollection::const_iterator it = hEbSrFlags->begin();
00510         it != hEbSrFlags->end(); ++it){
00511       
00512       const EBSrFlag& flag = *it;
00513       int iEta = flag.id().ieta();
00514       int iEta0 = iEta + nTtEta/2 - (iEta>=0?1:0); //0->55 from eta=-3 to eta=3
00515       int iEbEta0 = iEta0 - nEeTtEta;//0->33 from eta=-1.48 to eta=1.48
00516       int iPhi0 = flag.id().iphi() - 1;
00517 
00518       assert(iEbEta0>=0 && iEbEta0<nEbTtEta);
00519       assert(iPhi0>=0 && iPhi0<nTtPhi);
00520       
00521       ebSrf[iEbEta0][iPhi0] = flag.value();
00522     }
00523   } else{
00524     LogWarning("EcalSimRawData") << "EB SR flag not found ("
00525                                  << "Product label: " << srDigiProducer_
00526                                  << "Producet instance: "
00527                                  << ebSrFlagCollection_ << ")";
00528   }
00529 }
00530 
00531 void EcalSimRawData::getEbDigi(const edm::Event& event,
00532                                vector<uint16_t> adc[nEbEta][nEbPhi]) const{
00533 
00534   edm::Handle<EBDigiCollection> hEbDigis;
00535   event.getByLabel(digiProducer_, ebDigiCollection_, hEbDigis);
00536 
00537   int nSamples = 0;
00538   if(hEbDigis.isValid() && hEbDigis->size()>0){//there is at least one digi
00539     nSamples  = hEbDigis->begin()->size();//gets the sample count from 1st digi
00540   }
00541 
00542 
00543   const uint16_t suppressed = 0xFFFF;
00544 
00545   adc[0][0] = vector<uint16_t>(nSamples, suppressed);
00546   
00547   for(int iEbEta=0; iEbEta<nEbEta; ++iEbEta){
00548     for(int iEbPhi=0; iEbPhi<nEbPhi; ++iEbPhi){
00549       adc[iEbEta][iEbPhi] = adc[0][0];
00550     }
00551   }
00552   if(hEbDigis.isValid()){
00553     if(xtalVerbose_) cout << setfill('0');
00554     for(EBDigiCollection::const_iterator it = hEbDigis->begin();
00555         it != hEbDigis->end(); ++it){
00556       const EBDataFrame& frame = *it;
00557       
00558       int iEta0 = iEta2cIndex((frame.id()).ieta());
00559       int iPhi0 = iPhi2cIndex((frame.id()).iphi());
00560       
00561       //     cout << "xtl indices conv: (" << frame.id().ieta() << ","
00562       //         << frame.id().iphi() << ") -> ("
00563       //         << iEta0 << "," << iPhi0 << ")\n";
00564     
00565       if(iEta0<0 || iEta0>=nEbEta){
00566         cout << "iEta0 (= " << iEta0 << ") is out of range ("
00567              << "[0," << nEbEta -1 << "])\n";
00568       }
00569       if(iPhi0<0 || iPhi0>=nEbPhi){
00570         cout << "iPhi0 (= " << iPhi0 << ") is out of range ("
00571              << "[0," << nEbPhi -1 << "])\n";
00572       }
00573     
00574       if(xtalVerbose_){
00575         cout << iEta0 << "\t" << iPhi0 << ":\t";
00576         cout << hex;
00577       }
00578       
00579       if(nSamples!=frame.size()){
00580         throw cms::Exception("EcalSimRawData", "Found EB digis with different sample count! This is not supported by EcalSimRawData.");
00581       }
00582     
00583       for(int iSample=0; iSample<nSamples; ++iSample){
00584         const EcalMGPASample& sample = frame.sample(iSample);
00585         uint16_t encodedAdc = sample.raw();
00586         adc[iEta0][iPhi0][iSample] = encodedAdc;  
00587         if(xtalVerbose_){
00588           cout << (iSample>0?" ":"") << "0x" << setw(4) 
00589                << encodedAdc;
00590         }
00591       }
00592       if(xtalVerbose_) cout << "\n" << dec;
00593     }
00594     if(xtalVerbose_) cout << setfill(' ');
00595   }
00596 }
00597   
00598 
00599 void EcalSimRawData::getTp(const edm::Event& event,
00600                            const std::string& collName,
00601                            int tcp[nTtEta][nTtPhi]) const{
00602   edm::Handle<EcalTrigPrimDigiCollection> hTpDigis;
00603   event.getByLabel(tpProducer_, collName, hTpDigis);
00604   if(hTpDigis.isValid() && hTpDigis->size()>0){
00605     const EcalTrigPrimDigiCollection& tpDigis = *hTpDigis.product();
00606 
00607     //    EcalSelectiveReadout::ttFlag_t ttf[nTtEta][nTtPhi];
00608     for(int iTtEta0=0; iTtEta0 < nTtEta; ++iTtEta0){
00609       for(int iTtPhi0=0; iTtPhi0 < nTtPhi; ++iTtPhi0){
00610         tcp[iTtEta0][iTtPhi0] = tccInDefaultVal_ ;
00611       }
00612     }
00613     if(tpVerbose_){
00614       cout << setfill('0');
00615     }
00616     for(EcalTrigPrimDigiCollection::const_iterator it = tpDigis.begin();
00617         it != tpDigis.end(); ++it){
00618       const EcalTriggerPrimitiveDigi& tp = *it;
00619       int iTtEta0 = iTtEta2cIndex(tp.id().ieta());
00620       int iTtPhi0 = iTtPhi2cIndex(tp.id().iphi());
00621       if(iTtEta0<0 || iTtEta0>=nTtEta){
00622         cout << "iTtEta0 (= " << iTtEta0 << ") is out of range ("
00623              << "[0," << nEbTtEta -1 << "])\n";
00624       }
00625       if(iTtPhi0<0 || iTtPhi0>=nTtPhi){
00626         cout << "iTtPhi0 (= " << iTtPhi0 << ") is out of range ("
00627              << "[0," << nTtPhi -1 << "])\n";
00628       }
00629 
00630       tcp[iTtEta0][iTtPhi0] = tp[tp.sampleOfInterest()].raw();
00631 
00632       if(tpVerbose_){
00633         if(tcp[iTtEta0][iTtPhi0]!=0) //print non-zero values only
00634         cout << collName << (collName.size()==0?"":" ")
00635              << "TP(" << setw(2) << iTtEta0 << "," << iTtPhi0 << ") = "
00636              << "0x" << setw(4) 
00637              << tcp[iTtEta0][iTtPhi0]
00638              << "\tcmssw indices: "
00639              << tp.id().ieta() << " " << tp.id().iphi() << "\n";
00640       }
00641     }//next TP
00642     if(tpVerbose_) cout << setfill(' ');
00643   }
00644 }