CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_2_9_HLT1_bphpatch4/src/L1Trigger/TextToDigi/plugins/GtPsbTextToDigi.cc

Go to the documentation of this file.
00001 
00002 #include "GtPsbTextToDigi.h"
00003 // general
00004 #include "FWCore/ServiceRegistry/interface/Service.h"
00005 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00006 #include <iomanip>
00007 #include <iostream>
00008 // gct
00009 #include "DataFormats/L1GlobalCaloTrigger/interface/L1GctCollections.h"
00010 #include "DataFormats/L1GlobalCaloTrigger/interface/L1GctEmCand.h"
00011 #include "DataFormats/L1GlobalCaloTrigger/interface/L1GctJetCand.h"
00012 #include "DataFormats/L1GlobalCaloTrigger/interface/L1GctEtSums.h"
00013 //#include "DataFormats/L1GlobalCaloTrigger/interface/L1GctJetCounts.h"
00014 
00015 GtPsbTextToDigi::GtPsbTextToDigi(const edm::ParameterSet& iConfig):
00016   m_fileEventOffset(iConfig.getUntrackedParameter<int>("FileEventOffset",0)),
00017   m_textFileName(iConfig.getParameter<std::string>("TextFileName")),
00018   m_nevt(0) {
00019   // Produces collections
00020   produces<L1GctEmCandCollection>("isoEm");
00021   produces<L1GctEmCandCollection>("nonIsoEm");
00022   produces<L1GctJetCandCollection>("cenJets");
00023   produces<L1GctJetCandCollection>("forJets");
00024   produces<L1GctJetCandCollection>("tauJets");
00025   //produces<L1GctEtTotal>();
00026 
00027   // Open the input files
00028   for (int ifile=0; ifile<4; ifile++){
00029     // gct em cand coll: (noiso) 0<->0, 1<->1, (iso) 6<->2, 7<->3
00030     int ii = (ifile<2)?ifile:ifile+4;
00031     std::stringstream fileStream;
00032     fileStream << m_textFileName << ii << ".txt";
00033     std::string fileName(fileStream.str());
00034     m_file[ifile].open(fileName.c_str(),std::ios::in);
00035     if(!m_file[ifile].good()) {
00036       throw cms::Exception("GtPsbTextToDigiTextFileOpenError")
00037         //LogDebug("GtPsbTextToDigi")
00038         << "GtPsbTextToDigi::GtPsbTextToDigi : "
00039         << " couldn't open the file " << fileName 
00040         //<< "...skipping!" 
00041         << std::endl;
00042     }
00043     std::hex(m_file[ifile]); 
00044   }
00045 
00046   // Initialize bc0 position holder
00047   for(int i=0; i<4; i++)
00048     m_bc0[i]=-1;  
00049 }
00050 
00051 GtPsbTextToDigi::~GtPsbTextToDigi() {
00052   // Close the input files
00053   for (unsigned i=0; i<4; i++){  
00054     m_file[i].close();
00055   }
00056 }
00057 
00059 void GtPsbTextToDigi::putEmptyDigi(edm::Event& iEvent) {
00060   std::auto_ptr<L1GctEmCandCollection>  gctIsolaEm( new L1GctEmCandCollection () );
00061   std::auto_ptr<L1GctEmCandCollection>  gctNoIsoEm( new L1GctEmCandCollection () );
00062   std::auto_ptr<L1GctJetCandCollection> gctCenJets( new L1GctJetCandCollection() );
00063   std::auto_ptr<L1GctJetCandCollection> gctForJets( new L1GctJetCandCollection() );
00064   std::auto_ptr<L1GctJetCandCollection> gctTauJets( new L1GctJetCandCollection() );
00065   //std::auto_ptr<L1GctEtTotal>           gctEtTotal( new L1GctEtTotal          () );
00066   for (int i=0; i<4; i++){  
00067     gctIsolaEm->push_back(L1GctEmCand (0,1));
00068     gctNoIsoEm->push_back(L1GctEmCand (0,0));
00069     gctCenJets->push_back(L1GctJetCand(0,0,0));
00070     gctForJets->push_back(L1GctJetCand(0,0,1));
00071     gctTauJets->push_back(L1GctJetCand(0,1,0));
00072     //gctEtTotal->push_back(());
00073   }
00074   iEvent.put(gctIsolaEm, "isoEm");
00075   iEvent.put(gctNoIsoEm, "nonIsoEm");
00076   iEvent.put(gctCenJets, "cenJets");
00077   iEvent.put(gctForJets, "forJets");
00078   iEvent.put(gctTauJets, "tauJets");
00079   //iEvent.put(gctEtTotal);
00080 
00081   LogDebug("GtPsbTextToDigi") << "putting empty digi (evt:" << m_nevt << ")\n"; 
00082 }
00083 
00084 void GtPsbTextToDigi::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
00085 
00086   // specify clock cycle bit sequence 1 0 1 0... or 0 1 0 1...
00087   unsigned short cbs[2] = {1,0};
00088 
00089   // Skip event if required
00090   if (m_nevt < m_fileEventOffset){ 
00091     putEmptyDigi(iEvent);
00092     LogDebug("GtPsbTextToDigi")
00093       << "[GtPsbTextToDigi::produce()] skipping event " << m_nevt 
00094       << std::endl;
00095     m_nevt++;
00096     return;
00097   } else if (m_nevt==0 && m_fileEventOffset<0) {
00098     // skip first fileEventOffset input events
00099     unsigned long int buff;
00100     for (int ievt=0; ievt<abs(m_fileEventOffset); ievt++){  
00101       for (int ifile=0; ifile<4; ifile++){  
00102         for (int cycle=0; cycle<2; cycle++){  
00103           std::hex(m_file[ifile]);
00104           m_file[ifile] >> buff;
00105           unsigned tmp = (buff>>15)&0x1;
00106           if(tmp!=cbs[cycle]) {
00107             if(m_bc0[ifile]==-1 && cycle==1 && tmp==1)
00108               m_bc0[ifile] = ievt;
00109             else 
00110               throw cms::Exception("GtPsbTextToDigiTextFileFormatError") 
00111                 //std::cout << "GtPsbTextToDigiTextFileFormatError" 
00112                 << "GtPsbTextToDigi::produce : "
00113                 << " found format inconsistency in file #" << ifile 
00114                 << "\n in skipped line:" << ievt*2+1
00115                 << " cycle:" << tmp << " is different from " << cbs[cycle]
00116                 << std::endl;
00117           }
00118         } 
00119       }
00120       LogDebug("GtPsbTextToDigi")
00121         << "[GtPsbTextToDigi::produce()] skipping input " << ievt 
00122         << std::endl;
00123     }
00124   }
00125   m_nevt++;
00126   
00127   // New collections
00128   std::auto_ptr<L1GctEmCandCollection>  gctIsolaEm( new L1GctEmCandCollection () );
00129   std::auto_ptr<L1GctEmCandCollection>  gctNoIsoEm( new L1GctEmCandCollection () );
00130   std::auto_ptr<L1GctJetCandCollection> gctCenJets( new L1GctJetCandCollection() ); 
00131   std::auto_ptr<L1GctJetCandCollection> gctForJets( new L1GctJetCandCollection() ); 
00132   std::auto_ptr<L1GctJetCandCollection> gctTauJets( new L1GctJetCandCollection() );
00133   //std::auto_ptr<L1GctEtTotal>           gctEtTotal( new L1GctEtTotal          () );
00134 
00136   uint16_t data[4][2]= {{0}};
00137   for (int i=0; i<4; i++)
00138     for (int j=0; j<2; j++)
00139       data[i][j]=0;
00140 
00141   // Loop over files
00142   for (int ifile=0; ifile<4; ifile++){  
00143     int ii = (ifile<2)?ifile:ifile+4;
00144     
00145     // Check we're not at the end of the file
00146     if(m_file[ifile].eof()) {
00147       LogDebug("GtPsbTextToDigi")
00148         << "GtPsbTextToDigi::produce : "
00149         << " unexpected end of file " << m_textFileName << ii << ".txt"
00150         << std::endl;
00151       putEmptyDigi(iEvent);
00152       continue;
00153     }      
00154     
00155     if(!m_file[ifile].good()) {
00156       LogDebug("GtPsbTextToDigi")
00157         << "GtPsbTextToDigi::produce : "
00158         << " problem reading file " << m_textFileName << ii << ".txt"
00159         << std::endl;
00160       putEmptyDigi(iEvent);
00161       continue;
00162     }
00163     
00165     unsigned long int uLongBuffer;
00166     
00167     for(unsigned cycle=0; cycle<2; cycle++) {
00168       m_file[ifile] >> uLongBuffer;
00169       unsigned tmp = (uLongBuffer>>15)&0x1;
00170       
00172       if(false && tmp!=cbs[cycle]) 
00173         std::cout << "[GtPsbTextToDigi::produce()] asserting " 
00174                   << " evt:"   << m_nevt
00175                   << " ifile:" << ifile
00176                   << " cycle:" << cbs[cycle]
00177                   << std::hex
00178                   << " buffer:"<< uLongBuffer
00179                   << " tmp: "  << tmp
00180                   << std::dec 
00181                   << "\n\n" << std::flush;
00182 
00183       if(tmp!=cbs[cycle]){
00184         if(m_bc0[ifile]==-1 && cycle==1 && tmp==1){
00185           m_bc0[ifile] = (m_nevt-m_fileEventOffset);
00186         }else{
00187           throw cms::Exception("GtPsbTextToDigiTextFileFormatError") 
00188             //std::cout << "GtPsbTextToDigiTextFileFormatError " 
00189             << "GtPsbTextToDigi::produce : "
00190             << " found format inconsistency in file #" << ifile 
00191             << "\n in line:" << (m_nevt-m_fileEventOffset)*2-1  
00192             << " cycle:" << tmp << " is different from " << cbs[cycle]
00193             << std::endl;
00194         }
00195       }
00196       data[ifile][cycle] = (uLongBuffer&0x7fff);
00197     } //cycle loop
00198   } //ifile loop
00199   
00201   unsigned iIsola, iNoIso;
00202   for (unsigned cycle=0; cycle<2; cycle++){  
00203     for (unsigned i=0; i<2; i++){  
00204       iIsola = i+2;
00205       iNoIso = i;
00206       gctIsolaEm->push_back(L1GctEmCand(data[iIsola][cycle]&0x7fff,1));
00207       gctNoIsoEm->push_back(L1GctEmCand(data[iNoIso][cycle]&0x7fff,0));
00208       L1GctEmCand candI(data[iIsola][cycle],1);
00209       L1GctEmCand candN(data[iNoIso][cycle],0);
00210     }
00211   }
00212 
00214   iEvent.put(gctIsolaEm, "isoEm");
00215   iEvent.put(gctNoIsoEm, "nonIsoEm");
00216   iEvent.put(gctCenJets, "cenJets");
00217   iEvent.put(gctForJets, "forJets");
00218   iEvent.put(gctTauJets, "tauJets");
00219   //iEvent.put(gctEtTotal);
00220 
00221 }
00222 
00223 void GtPsbTextToDigi::endJob() {
00225   int nmem = 4;
00226   bool match = true;
00227   for(int i=0; i<nmem-1; i++)
00228     match &= (m_bc0[i]==m_bc0[i+1]);  
00229   LogDebug("GtPsbTextToDigi") << "[GtPsbTextToDigi::endJob()] ";
00230   if(!match) 
00231     LogDebug("GtPsbTextToDigi") << "did not find matching BC0 in all input files: ";
00232   else
00233     LogDebug("GtPsbTextToDigi") << "detected common BC0 in all input files: ";
00234   for(int i=0; i<nmem; i++)
00235     LogDebug("GtPsbTextToDigi") << " " << m_bc0[i];  
00236   LogDebug("GtPsbTextToDigi") << std::flush << std::endl;
00237 
00238 }