Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:21:42

0001 // -*- C++ -*-
0002 //
0003 // Package:    RctTextToRctDigi
0004 // Class:      RctTextToRctDigi
0005 //
0006 /**\class RctTextToRctDigi RctTextToRctDigi.cc
0007 L1Trigger/TextToDigi/src/RctTextToRctDigi.cc
0008 
0009 Description: Makes RCT digis from the file format specified by Pam Klabbers
0010 
0011 */
0012 //
0013 // Original Author:  Alex Tapper
0014 //         Created:  Fri Mar  9 19:11:51 CET 2007
0015 
0016 // Rct Input File Format
0017 // Line 1: Crossing no as "Crossing x" (2)
0018 // Line 2: isoe0 isoe1 isoe2 isoe3 nonIsoe0 nonIsoe1 nonIso2 nonIso3 (8)
0019 // Line 3: RC0mip0 RC0mip1 RC1mip0 RC1mip1 RC2mip0 RC2mip1 RC3mip0 RC3mip1
0020 // RC4mip0 RC4mip1
0021 //         RC5mip0 RC5mip1 RC6mip0 RC6mip1 (14)
0022 // Line 4: RC0qt0 RCqt1 RC1qt0 RC1qt1 RC2qt0 RC2qt1 RC3qt0 RC3qt1 RC4qt0 RC4qt1
0023 //         RC5qt0 RC5qt1 RC6qt0 RC6qt1 (14)
0024 // Line 5: RC0reg0 RC0reg1 RC1reg0 RC1reg1 RC2reg0 RC2reg1 RC3reg0 RC3reg1
0025 // RC4reg0 RC4reg1
0026 //         RC5reg0 RC5reg1 RC6reg0 RC6reg1 (14)
0027 // Line 6: HF0eta0 HF0eta1 HF0eta2 HF0eta3 HF1eta0 HF1eta1 HF1eta2 HF1eta3 (8)
0028 //
0029 // NOTE:  CMS IN 2004/009 specifies that cable four provides 8 Quiet (fineGrain)
0030 // bits for the HF.  These are not
0031 //        detailed in the fileformat above, and are not currently dealt with in
0032 //        any way. Set to true.
0033 //
0034 
0035 #include "FWCore/MessageLogger/interface/MessageLogger.h"  // Logger
0036 #include "FWCore/ServiceRegistry/interface/Service.h"      // Framework services
0037 #include "RctTextToRctDigi.h"
0038 #include <iomanip>
0039 
0040 using namespace edm;
0041 using namespace std;
0042 
0043 // Set constant
0044 const static unsigned NUM_RCT_CRATES = 18;
0045 
0046 RctTextToRctDigi::RctTextToRctDigi(const edm::ParameterSet &iConfig)
0047     : m_textFileName(iConfig.getParameter<std::string>("TextFileName")),
0048       m_fileEventOffset(iConfig.getParameter<int>("FileEventOffset")),
0049       m_nevt(0) {
0050   // Produces collections
0051   produces<L1CaloEmCollection>();
0052   produces<L1CaloRegionCollection>();
0053 
0054   // Open the input files
0055   for (unsigned i = 0; i < NUM_RCT_CRATES; i++) {
0056     std::stringstream fileStream;
0057     fileStream << m_textFileName << std::setw(2) << std::setfill('0') << i << ".txt";
0058     std::string fileName(fileStream.str());
0059     m_file[i].open(fileName.c_str(), std::ios::in);
0060 
0061     if (!m_file[i].good()) {
0062       // throw cms::Exception("RctTextToRctDigiTextFileOpenError")
0063       LogDebug("RctTextToRctDigi") << "RctTextToRctDigi::RctTextToRctDigi : "
0064                                    << " couldn't open the file " << fileName << "...skipping!" << std::endl;
0065     }
0066   }
0067 }
0068 
0069 RctTextToRctDigi::~RctTextToRctDigi() {
0070   // Close the input files
0071   for (unsigned i = 0; i < NUM_RCT_CRATES; i++) {
0072     m_file[i].close();
0073   }
0074 }
0075 
0076 /// Append empty digi collection/n
0077 void RctTextToRctDigi::putEmptyDigi(edm::Event &iEvent) {
0078   std::unique_ptr<L1CaloEmCollection> em(new L1CaloEmCollection);
0079   std::unique_ptr<L1CaloRegionCollection> rgn(new L1CaloRegionCollection);
0080   for (unsigned i = 0; i < NUM_RCT_CRATES; i++) {
0081     for (unsigned j = 0; j < 4; j++) {
0082       em->push_back(L1CaloEmCand(0, i, true));
0083       em->push_back(L1CaloEmCand(0, i, false));
0084     }
0085     for (unsigned j = 0; j < 14; j++)
0086       rgn->push_back(L1CaloRegion(0, false, false, false, false, i, j / 2, j % 2));
0087     for (unsigned j = 0; j < 8; j++)
0088       rgn->push_back(L1CaloRegion(0, true, i, j));
0089   }
0090   iEvent.put(std::move(em));
0091   iEvent.put(std::move(rgn));
0092 }
0093 
0094 /// Syncronize bunch crossing number/n
0095 void RctTextToRctDigi::bxSynchro(int &bx, int crate) {
0096   std::string tmp;
0097   // bypass bx input until correct bx is reached
0098   while (bx < m_nevt + m_fileEventOffset) {
0099     for (int j = 0; j < 6; j++) {
0100       getline(m_file[crate], tmp);
0101     }
0102     m_file[crate] >> tmp >> bx;
0103     if (tmp != "Crossing")
0104       throw cms::Exception("RctTextToRctDigiTextFileReadError")
0105           << "RctTextToRctDigi::bxSynchro : "
0106           << " something screwy happened Crossing!=" << tmp << std::endl;
0107   }
0108 }
0109 
0110 // ------------ method called to produce the data  ------------
0111 void RctTextToRctDigi::produce(edm::Event &iEvent, const edm::EventSetup &iSetup) {
0112   // Skip event if required
0113   if (m_nevt < m_fileEventOffset) {
0114     // string tmp;
0115     // for (int i=0; i<6; i++){
0116     //  getline(m_file[i],tmp);
0117     //}
0118     putEmptyDigi(iEvent);
0119     m_nevt++;
0120     return;
0121   }
0122 
0123   // New collections
0124   std::unique_ptr<L1CaloEmCollection> em(new L1CaloEmCollection);
0125   std::unique_ptr<L1CaloRegionCollection> rgn(new L1CaloRegionCollection);
0126 
0127   // Loop over RCT crates
0128   for (unsigned i = 0; i < NUM_RCT_CRATES; i++) {
0129     if (!m_file[i].good()) {
0130       continue;
0131     }
0132 
0133     // Check we're not at the end of the file
0134     if (m_file[i].eof()) {
0135       // throw cms::Exception("RctTextToRctDigiTextFileReadError")
0136       LogDebug("RctTextToRctDigi") << "RctTextToRctDigi::produce : "
0137                                    << " unexpected end of file " << m_textFileName << i
0138                                    << " adding empty collection for event !" << std::endl;
0139       putEmptyDigi(iEvent);
0140       continue;
0141     }
0142 
0143     // Check we're at the start of an event
0144     std::string tmp;
0145     m_file[i] >> tmp;
0146     if (tmp != "Crossing") {
0147       throw cms::Exception("RctTextToRctDigiTextFileReadError")
0148           << "RctTextToRctDigi::produce : "
0149           << " something screwy happened Crossing!=" << tmp << std::endl;
0150     }
0151 
0152     // Read BX number
0153     dec(m_file[i]);
0154     int BXNum;
0155     m_file[i] >> BXNum;
0156 
0157     /// Synchronize bunch crossing
0158     bxSynchro(BXNum, i);
0159 
0160     if (BXNum != m_nevt + m_fileEventOffset)
0161       throw cms::Exception("RctTextToRctDigiTextSyncError")
0162           << "RctTextToRctDigi::produce : "
0163           << " something screwy happened "
0164           << "evt:" << m_nevt << " != bx:" << BXNum << " + " << m_fileEventOffset << std::endl;
0165 
0166     // Buffers
0167     unsigned long int uLongBuffer;
0168     bool mipBitBuffer[14], qBitBuffer[14];
0169 
0170     // All in hex from now on
0171     hex(m_file[i]);
0172 
0173     // Isolated electrons
0174     for (unsigned j = 0; j < 4; j++) {
0175       m_file[i] >> uLongBuffer;
0176       em->push_back(L1CaloEmCand(uLongBuffer, i, true, j, BXNum, false));
0177     }
0178 
0179     // Non-isolated electrons
0180     for (unsigned j = 0; j < 4; j++) {
0181       m_file[i] >> uLongBuffer;
0182       em->push_back(L1CaloEmCand(uLongBuffer, i, false, j, BXNum, false));
0183     }
0184 
0185     // MIP bits
0186     for (unsigned j = 0; j < 14; j++) {
0187       m_file[i] >> mipBitBuffer[j];
0188     }
0189 
0190     // Quiet bits
0191     for (unsigned j = 0; j < 14; j++) {
0192       m_file[i] >> qBitBuffer[j];
0193     }
0194 
0195     // Barrel and endcap regions
0196     for (unsigned j = 0; j < 14; j++) {
0197       m_file[i] >> uLongBuffer;
0198 
0199       unsigned et = uLongBuffer & 0x3ff;  // put the first 10 bits of rawData into the Et
0200       uLongBuffer >>= 10;                 // shift the remaining bits down to remove the 10 bits of Et
0201 
0202       bool overFlow = ((uLongBuffer & 0x1) != 0);        // LSB is now overflow bit
0203       bool tauVeto = (((uLongBuffer & 0x2) >> 1) != 0);  // 2nd bit is tauveto
0204 
0205       rgn->push_back(L1CaloRegion(et, overFlow, tauVeto, mipBitBuffer[j], qBitBuffer[j], i, j / 2, j % 2));
0206     }
0207 
0208     // HF
0209     for (unsigned j = 0; j < 8; j++) {
0210       m_file[i] >> uLongBuffer;
0211 
0212       unsigned et = uLongBuffer & 0xff;  // put the first 8 bits into the Et
0213 
0214       rgn->push_back(L1CaloRegion(et, true, i, j));
0215     }
0216 
0217     dec(m_file[i]);
0218   }
0219 
0220   iEvent.put(std::move(em));
0221   iEvent.put(std::move(rgn));
0222 
0223   m_nevt++;
0224 }