CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/L1Trigger/TextToDigi/plugins/RctTextToRctDigi.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:    RctTextToRctDigi
00004 // Class:      RctTextToRctDigi
00005 // 
00011 //
00012 // Original Author:  Alex Tapper
00013 //         Created:  Fri Mar  9 19:11:51 CET 2007
00014 // $Id: RctTextToRctDigi.cc,v 1.5 2010/08/06 20:24:40 wmtan Exp $
00015 
00016 // Rct Input File Format 
00017 // Line 1: Crossing no as "Crossing x" (2)     
00018 // Line 2: isoe0 isoe1 isoe2 isoe3 nonIsoe0 nonIsoe1 nonIso2 nonIso3 (8) 
00019 // Line 3: RC0mip0 RC0mip1 RC1mip0 RC1mip1 RC2mip0 RC2mip1 RC3mip0 RC3mip1 RC4mip0 RC4mip1 
00020 //         RC5mip0 RC5mip1 RC6mip0 RC6mip1 (14)
00021 // Line 4: RC0qt0 RCqt1 RC1qt0 RC1qt1 RC2qt0 RC2qt1 RC3qt0 RC3qt1 RC4qt0 RC4qt1 
00022 //         RC5qt0 RC5qt1 RC6qt0 RC6qt1 (14)
00023 // Line 5: RC0reg0 RC0reg1 RC1reg0 RC1reg1 RC2reg0 RC2reg1 RC3reg0 RC3reg1 RC4reg0 RC4reg1
00024 //         RC5reg0 RC5reg1 RC6reg0 RC6reg1 (14)
00025 // Line 6: HF0eta0 HF0eta1 HF0eta2 HF0eta3 HF1eta0 HF1eta1 HF1eta2 HF1eta3 (8)
00026 //
00027 // NOTE:  CMS IN 2004/009 specifies that cable four provides 8 Quiet (fineGrain) bits for the HF.  These are not
00028 //        detailed in the fileformat above, and are not currently dealt with in any way. Set to true.
00029 // 
00030 
00031 #include "RctTextToRctDigi.h"
00032 #include "FWCore/ServiceRegistry/interface/Service.h" // Framework services
00033 #include "FWCore/MessageLogger/interface/MessageLogger.h" // Logger
00034 #include <iomanip>
00035 
00036 using namespace edm;
00037 using namespace std;
00038 
00039 // Set constant
00040 const static unsigned NUM_RCT_CRATES = 18;
00041 
00042 RctTextToRctDigi::RctTextToRctDigi(const edm::ParameterSet& iConfig):
00043   m_textFileName(iConfig.getParameter<std::string>("TextFileName")),
00044   m_fileEventOffset(iConfig.getParameter<int>("FileEventOffset")),
00045   m_nevt(0)
00046 {
00047   // Produces collections
00048   produces<L1CaloEmCollection>();
00049   produces<L1CaloRegionCollection>();
00050 
00051   // Open the input files
00052   for (unsigned i=0; i<NUM_RCT_CRATES; i++){
00053     std::stringstream fileStream;
00054     fileStream << m_textFileName << std::setw(2) << std::setfill('0') << i << ".txt";
00055     std::string fileName(fileStream.str());
00056     m_file[i].open(fileName.c_str(),std::ios::in);
00057 
00058     if(!m_file[i].good())
00059       {
00060         //throw cms::Exception("RctTextToRctDigiTextFileOpenError")
00061         LogDebug("RctTextToRctDigi")
00062           << "RctTextToRctDigi::RctTextToRctDigi : "
00063           << " couldn't open the file " << fileName << "...skipping!" << std::endl;
00064       }
00065   }
00066 }
00067 
00068 RctTextToRctDigi::~RctTextToRctDigi()
00069 {
00070   // Close the input files
00071   for (unsigned i=0; i<NUM_RCT_CRATES; i++){  
00072     m_file[i].close();
00073   }
00074 }
00075 
00077 void RctTextToRctDigi::putEmptyDigi(edm::Event& iEvent) {
00078   std::auto_ptr<L1CaloEmCollection> em (new L1CaloEmCollection);
00079   std::auto_ptr<L1CaloRegionCollection> rgn (new L1CaloRegionCollection);
00080     for (unsigned i=0; i<NUM_RCT_CRATES; i++){  
00081       for (unsigned j=0; j<4; j++) {
00082         em->push_back(L1CaloEmCand(0, i, true));
00083         em->push_back(L1CaloEmCand(0, i, false));
00084       }
00085       for (unsigned j=0; j<14; j++)
00086         rgn->push_back(L1CaloRegion(0,false,false,false,false,i,j/2,j%2));
00087       for (unsigned j=0; j<8; j++)
00088         rgn->push_back(L1CaloRegion(0,true,i,j));
00089     }
00090     iEvent.put(em);
00091     iEvent.put(rgn);
00092 }
00093 
00095 void RctTextToRctDigi::bxSynchro(int &bx,int crate) {
00096   std::string tmp;
00097   // bypass bx input until correct bx is reached
00098   while(bx<m_nevt+m_fileEventOffset) {
00099     for (int j=0; j<6; j++){
00100       getline(m_file[crate],tmp);
00101     }
00102     m_file[crate] >> tmp >> bx;
00103     if(tmp!="Crossing")
00104       throw cms::Exception("RctTextToRctDigiTextFileReadError")
00105         << "RctTextToRctDigi::bxSynchro : "
00106         << " something screwy happened Crossing!=" << tmp << std::endl;
00107   }
00108 }
00109 
00110 // ------------ method called to produce the data  ------------
00111 void RctTextToRctDigi::produce(edm::Event& iEvent, const edm::EventSetup& iSetup)
00112 {
00113 
00114   // Skip event if required
00115   if (m_nevt < m_fileEventOffset){ 
00116     //string tmp;
00117     //for (int i=0; i<6; i++){
00118     //  getline(m_file[i],tmp);
00119     //} 
00120     putEmptyDigi(iEvent);
00121     m_nevt++;
00122     return;
00123   }
00124 
00125   // New collections
00126   std::auto_ptr<L1CaloEmCollection> em (new L1CaloEmCollection);
00127   std::auto_ptr<L1CaloRegionCollection> rgn (new L1CaloRegionCollection);
00128 
00129   // Loop over RCT crates
00130   for (unsigned i=0; i<NUM_RCT_CRATES; i++){  
00131 
00132     if(!m_file[i].good()) {
00133       continue;
00134     }
00135 
00136     // Check we're not at the end of the file
00137     if(m_file[i].eof())
00138       {
00139         //throw cms::Exception("RctTextToRctDigiTextFileReadError")
00140         LogDebug("RctTextToRctDigi")
00141           << "RctTextToRctDigi::produce : "
00142           << " unexpected end of file " << m_textFileName << i 
00143           << " adding empty collection for event !"
00144           << std::endl;
00145         putEmptyDigi(iEvent);
00146         continue;
00147       }      
00148     
00149     // Check we're at the start of an event
00150     std::string tmp;
00151     m_file[i]>> tmp;
00152     if(tmp!="Crossing")
00153       {
00154         throw cms::Exception("RctTextToRctDigiTextFileReadError")
00155           << "RctTextToRctDigi::produce : "
00156           << " something screwy happened Crossing!=" << tmp << std::endl;
00157       }      
00158 
00159     // Read BX number
00160     dec(m_file[i]);
00161     int BXNum;
00162     m_file[i]>>BXNum;
00163     
00165     bxSynchro(BXNum,i);
00166  
00167     if(BXNum!=m_nevt+m_fileEventOffset)
00168       throw cms::Exception("RctTextToRctDigiTextSyncError")
00169         << "RctTextToRctDigi::produce : "
00170         << " something screwy happened "
00171         << "evt:" << m_nevt << " != bx:" << BXNum << " + " << m_fileEventOffset 
00172         << std::endl;
00173     
00174     // Buffers
00175     unsigned long int uLongBuffer;
00176     bool mipBitBuffer[14],qBitBuffer[14];
00177 
00178     // All in hex from now on
00179     hex(m_file[i]); 
00180 
00181     // Isolated electrons
00182     for (unsigned j=0; j<4; j++){
00183       m_file[i] >> uLongBuffer;
00184       em->push_back(L1CaloEmCand(uLongBuffer, i, true, j,BXNum,0));
00185     }
00186 
00187     // Non-isolated electrons
00188     for (unsigned j=0; j<4; j++){
00189       m_file[i] >> uLongBuffer;
00190       em->push_back(L1CaloEmCand(uLongBuffer, i, false, j,BXNum,0));
00191     }      
00192     
00193     // MIP bits 
00194     for (unsigned j=0; j<14; j++){
00195       m_file[i] >> mipBitBuffer[j];
00196     }   
00197 
00198     // Quiet bits 
00199     for (unsigned j=0; j<14; j++){
00200       m_file[i] >> qBitBuffer[j];
00201     }     
00202 
00203     // Barrel and endcap regions
00204     for (unsigned j=0; j<14; j++){
00205       m_file[i] >> uLongBuffer;
00206 
00207       unsigned et = uLongBuffer & 0x3ff;  // put the first 10 bits of rawData into the Et
00208       uLongBuffer >>= 10;  // shift the remaining bits down to remove the 10 bits of Et
00209       
00210       bool overFlow = ((uLongBuffer & 0x1)        != 0); //LSB is now overflow bit
00211       bool tauVeto  = (((uLongBuffer & 0x2) >> 1) != 0); //2nd bit is tauveto      
00212 
00213       rgn->push_back(L1CaloRegion(et,overFlow,tauVeto,mipBitBuffer[j],qBitBuffer[j],i,j/2,j%2));
00214     }      
00215     
00216     // HF
00217     for (unsigned j=0; j<8; j++){
00218       m_file[i] >> uLongBuffer;
00219 
00220       unsigned et = uLongBuffer & 0xff;  // put the first 8 bits into the Et
00221 
00222       rgn->push_back(L1CaloRegion(et,true,i,j));
00223     }        
00224 
00225     dec(m_file[i]); 
00226   }
00227   
00228   iEvent.put(em);
00229   iEvent.put(rgn);
00230 
00231   m_nevt++;
00232 }
00233 
00234 
00235