Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:30:37

0001 
0002 #include "GtPsbTextToDigi.h"
0003 // general
0004 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0005 #include "FWCore/ServiceRegistry/interface/Service.h"
0006 #include <iomanip>
0007 #include <iostream>
0008 // gct
0009 #include "DataFormats/L1GlobalCaloTrigger/interface/L1GctCollections.h"
0010 #include "DataFormats/L1GlobalCaloTrigger/interface/L1GctEmCand.h"
0011 #include "DataFormats/L1GlobalCaloTrigger/interface/L1GctEtSums.h"
0012 #include "DataFormats/L1GlobalCaloTrigger/interface/L1GctJetCand.h"
0013 //#include "DataFormats/L1GlobalCaloTrigger/interface/L1GctJetCounts.h"
0014 
0015 GtPsbTextToDigi::GtPsbTextToDigi(const edm::ParameterSet &iConfig)
0016     : m_fileEventOffset(iConfig.getUntrackedParameter<int>("FileEventOffset", 0)),
0017       m_textFileName(iConfig.getParameter<std::string>("TextFileName")),
0018       m_nevt(0) {
0019   // Produces collections
0020   produces<L1GctEmCandCollection>("isoEm");
0021   produces<L1GctEmCandCollection>("nonIsoEm");
0022   produces<L1GctJetCandCollection>("cenJets");
0023   produces<L1GctJetCandCollection>("forJets");
0024   produces<L1GctJetCandCollection>("tauJets");
0025   // produces<L1GctEtTotal>();
0026 
0027   // Open the input files
0028   for (int ifile = 0; ifile < 4; ifile++) {
0029     // gct em cand coll: (noiso) 0<->0, 1<->1, (iso) 6<->2, 7<->3
0030     int ii = (ifile < 2) ? ifile : ifile + 4;
0031     std::stringstream fileStream;
0032     fileStream << m_textFileName << ii << ".txt";
0033     std::string fileName(fileStream.str());
0034     m_file[ifile].open(fileName.c_str(), std::ios::in);
0035     if (!m_file[ifile].good()) {
0036       throw cms::Exception("GtPsbTextToDigiTextFileOpenError")
0037           // LogDebug("GtPsbTextToDigi")
0038           << "GtPsbTextToDigi::GtPsbTextToDigi : "
0039           << " couldn't open the file "
0040           << fileName
0041           //<< "...skipping!"
0042           << std::endl;
0043     }
0044     std::hex(m_file[ifile]);
0045   }
0046 
0047   // Initialize bc0 position holder
0048   for (int i = 0; i < 4; i++)
0049     m_bc0[i] = -1;
0050 }
0051 
0052 GtPsbTextToDigi::~GtPsbTextToDigi() {
0053   // Close the input files
0054   for (unsigned i = 0; i < 4; i++) {
0055     m_file[i].close();
0056   }
0057 }
0058 
0059 /// Append empty digi collection
0060 void GtPsbTextToDigi::putEmptyDigi(edm::Event &iEvent) {
0061   std::unique_ptr<L1GctEmCandCollection> gctIsolaEm(new L1GctEmCandCollection());
0062   std::unique_ptr<L1GctEmCandCollection> gctNoIsoEm(new L1GctEmCandCollection());
0063   std::unique_ptr<L1GctJetCandCollection> gctCenJets(new L1GctJetCandCollection());
0064   std::unique_ptr<L1GctJetCandCollection> gctForJets(new L1GctJetCandCollection());
0065   std::unique_ptr<L1GctJetCandCollection> gctTauJets(new L1GctJetCandCollection());
0066   // std::unique_ptr<L1GctEtTotal>           gctEtTotal( new L1GctEtTotal () );
0067   for (int i = 0; i < 4; i++) {
0068     gctIsolaEm->push_back(L1GctEmCand(0, true));
0069     gctNoIsoEm->push_back(L1GctEmCand(0, false));
0070     gctCenJets->push_back(L1GctJetCand(0, false, false));
0071     gctForJets->push_back(L1GctJetCand(0, false, true));
0072     gctTauJets->push_back(L1GctJetCand(0, true, false));
0073     // gctEtTotal->push_back(());
0074   }
0075   iEvent.put(std::move(gctIsolaEm), "isoEm");
0076   iEvent.put(std::move(gctNoIsoEm), "nonIsoEm");
0077   iEvent.put(std::move(gctCenJets), "cenJets");
0078   iEvent.put(std::move(gctForJets), "forJets");
0079   iEvent.put(std::move(gctTauJets), "tauJets");
0080   // iEvent.put(std::move(gctEtTotal));
0081 
0082   LogDebug("GtPsbTextToDigi") << "putting empty digi (evt:" << m_nevt << ")\n";
0083 }
0084 
0085 void GtPsbTextToDigi::produce(edm::Event &iEvent, const edm::EventSetup &iSetup) {
0086   // specify clock cycle bit sequence 1 0 1 0... or 0 1 0 1...
0087   unsigned short cbs[2] = {1, 0};
0088 
0089   // Skip event if required
0090   if (m_nevt < m_fileEventOffset) {
0091     putEmptyDigi(iEvent);
0092     LogDebug("GtPsbTextToDigi") << "[GtPsbTextToDigi::produce()] skipping event " << m_nevt << std::endl;
0093     m_nevt++;
0094     return;
0095   } else if (m_nevt == 0 && m_fileEventOffset < 0) {
0096     // skip first fileEventOffset input events
0097     unsigned long int buff;
0098     for (int ievt = 0; ievt < abs(m_fileEventOffset); ievt++) {
0099       for (int ifile = 0; ifile < 4; ifile++) {
0100         for (int cycle = 0; cycle < 2; cycle++) {
0101           std::hex(m_file[ifile]);
0102           m_file[ifile] >> buff;
0103           unsigned tmp = (buff >> 15) & 0x1;
0104           if (tmp != cbs[cycle]) {
0105             if (m_bc0[ifile] == -1 && cycle == 1 && tmp == 1)
0106               m_bc0[ifile] = ievt;
0107             else
0108               throw cms::Exception("GtPsbTextToDigiTextFileFormatError")
0109                   // std::cout << "GtPsbTextToDigiTextFileFormatError"
0110                   << "GtPsbTextToDigi::produce : "
0111                   << " found format inconsistency in file #" << ifile << "\n in skipped line:" << ievt * 2 + 1
0112                   << " cycle:" << tmp << " is different from " << cbs[cycle] << std::endl;
0113           }
0114         }
0115       }
0116       LogDebug("GtPsbTextToDigi") << "[GtPsbTextToDigi::produce()] skipping input " << ievt << std::endl;
0117     }
0118   }
0119   m_nevt++;
0120 
0121   // New collections
0122   std::unique_ptr<L1GctEmCandCollection> gctIsolaEm(new L1GctEmCandCollection());
0123   std::unique_ptr<L1GctEmCandCollection> gctNoIsoEm(new L1GctEmCandCollection());
0124   std::unique_ptr<L1GctJetCandCollection> gctCenJets(new L1GctJetCandCollection());
0125   std::unique_ptr<L1GctJetCandCollection> gctForJets(new L1GctJetCandCollection());
0126   std::unique_ptr<L1GctJetCandCollection> gctTauJets(new L1GctJetCandCollection());
0127   // std::unique_ptr<L1GctEtTotal>           gctEtTotal( new L1GctEtTotal () );
0128 
0129   /// buffer
0130   uint16_t data[4][2] = {{0}};
0131   for (int i = 0; i < 4; i++)
0132     for (int j = 0; j < 2; j++)
0133       data[i][j] = 0;
0134 
0135   // Loop over files
0136   for (int ifile = 0; ifile < 4; ifile++) {
0137     int ii = (ifile < 2) ? ifile : ifile + 4;
0138 
0139     // Check we're not at the end of the file
0140     if (m_file[ifile].eof()) {
0141       LogDebug("GtPsbTextToDigi") << "GtPsbTextToDigi::produce : "
0142                                   << " unexpected end of file " << m_textFileName << ii << ".txt" << std::endl;
0143       putEmptyDigi(iEvent);
0144       continue;
0145     }
0146 
0147     if (!m_file[ifile].good()) {
0148       LogDebug("GtPsbTextToDigi") << "GtPsbTextToDigi::produce : "
0149                                   << " problem reading file " << m_textFileName << ii << ".txt" << std::endl;
0150       putEmptyDigi(iEvent);
0151       continue;
0152     }
0153 
0154     /// Read in file
0155     unsigned long int uLongBuffer;
0156 
0157     for (unsigned cycle = 0; cycle < 2; cycle++) {
0158       m_file[ifile] >> uLongBuffer;
0159       unsigned tmp = (uLongBuffer >> 15) & 0x1;
0160 
0161       /// cycle debuging (temporary)
0162       if (false && tmp != cbs[cycle])
0163         std::cout << "[GtPsbTextToDigi::produce()] asserting "
0164                   << " evt:" << m_nevt << " ifile:" << ifile << " cycle:" << cbs[cycle] << std::hex
0165                   << " buffer:" << uLongBuffer << " tmp: " << tmp << std::dec << "\n\n"
0166                   << std::flush;
0167 
0168       if (tmp != cbs[cycle]) {
0169         if (m_bc0[ifile] == -1 && cycle == 1 && tmp == 1) {
0170           m_bc0[ifile] = (m_nevt - m_fileEventOffset);
0171         } else {
0172           throw cms::Exception("GtPsbTextToDigiTextFileFormatError")
0173               // std::cout << "GtPsbTextToDigiTextFileFormatError "
0174               << "GtPsbTextToDigi::produce : "
0175               << " found format inconsistency in file #" << ifile
0176               << "\n in line:" << (m_nevt - m_fileEventOffset) * 2 - 1 << " cycle:" << tmp << " is different from "
0177               << cbs[cycle] << std::endl;
0178         }
0179       }
0180       data[ifile][cycle] = (uLongBuffer & 0x7fff);
0181     }  // cycle loop
0182   }    // ifile loop
0183 
0184   /// Fill in digi collections
0185   unsigned iIsola, iNoIso;
0186   for (unsigned cycle = 0; cycle < 2; cycle++) {
0187     for (unsigned i = 0; i < 2; i++) {
0188       iIsola = i + 2;
0189       iNoIso = i;
0190       gctIsolaEm->push_back(L1GctEmCand(data[iIsola][cycle] & 0x7fff, true));
0191       gctNoIsoEm->push_back(L1GctEmCand(data[iNoIso][cycle] & 0x7fff, false));
0192       L1GctEmCand candI(data[iIsola][cycle], true);
0193       L1GctEmCand candN(data[iNoIso][cycle], false);
0194     }
0195   }
0196 
0197   /// Put collections
0198   iEvent.put(std::move(gctIsolaEm), "isoEm");
0199   iEvent.put(std::move(gctNoIsoEm), "nonIsoEm");
0200   iEvent.put(std::move(gctCenJets), "cenJets");
0201   iEvent.put(std::move(gctForJets), "forJets");
0202   iEvent.put(std::move(gctTauJets), "tauJets");
0203   // iEvent.put(std::move(gctEtTotal));
0204 }
0205 
0206 void GtPsbTextToDigi::endJob() {
0207   /// Check BC0 signals consistency
0208   int nmem = 4;
0209   bool match = true;
0210   for (int i = 0; i < nmem - 1; i++)
0211     match &= (m_bc0[i] == m_bc0[i + 1]);
0212   LogDebug("GtPsbTextToDigi") << "[GtPsbTextToDigi::endJob()] ";
0213   if (!match)
0214     LogDebug("GtPsbTextToDigi") << "did not find matching BC0 in all input files: ";
0215   else
0216     LogDebug("GtPsbTextToDigi") << "detected common BC0 in all input files: ";
0217   for (int i = 0; i < nmem; i++)
0218     LogDebug("GtPsbTextToDigi") << " " << m_bc0[i];
0219   LogDebug("GtPsbTextToDigi") << std::flush << std::endl;
0220 }