CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_4_5_patch3/src/L1Trigger/TextToDigi/plugins/TextToRaw.cc

Go to the documentation of this file.
00001 
00002 
00003 #include "L1Trigger/TextToDigi/plugins/TextToRaw.h"
00004 
00005 // system
00006 #include <vector>
00007 #include <string>
00008 #include <fstream>
00009 #include <iostream>
00010 
00011 // framework
00012 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00013 
00014 #include "FWCore/Utilities/interface/Exception.h"
00015 
00016 // Raw data collection
00017 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
00018 
00019 using std::vector;
00020 using std::cerr;
00021 using std::cout;
00022 using std::endl;
00023 using std::string;
00024 using std::ios;
00025 
00026 const unsigned TextToRaw::EVT_MAX_SIZE;
00027 
00028 TextToRaw::TextToRaw(const edm::ParameterSet& iConfig) :
00029   fedId_(iConfig.getUntrackedParameter<int>("fedId", 745)),
00030   filename_(iConfig.getUntrackedParameter<std::string>("filename", "slinkOutput.txt")),
00031   fileEventOffset_(iConfig.getUntrackedParameter<int>("FileEventOffset", 0)),
00032   nevt_(0)
00033 {
00034   edm::LogInfo("TextToDigi") << "Reading ASCII dump from " << filename_ << std::endl;
00035 
00036   //register the products
00037   produces<FEDRawDataCollection>();
00038 
00039 }
00040 
00041 
00042 TextToRaw::~TextToRaw()
00043 {
00044  
00045    // do anything here that needs to be done at desctruction time
00046    // (e.g. close files, deallocate resources etc.)
00047 
00048 }
00049 
00050 
00051 
00053 void TextToRaw::putEmptyDigi(edm::Event& iEvent) {
00054   std::auto_ptr<FEDRawDataCollection> rawColl(new FEDRawDataCollection()); 
00055   //FEDRawData& feddata=rawColl->FEDData(fedId_);
00056   //feddata.data()[0] = 0;
00057   iEvent.put(rawColl);
00058 }
00059 
00060 
00061 // ------------ method called to produce the data  ------------
00062 void
00063 TextToRaw::produce(edm::Event& iEvent, const edm::EventSetup& iSetup)
00064 {
00065   using namespace edm;
00066    
00067   // Skip event if required
00068   if (nevt_ < fileEventOffset_){
00069     putEmptyDigi(iEvent);
00070     nevt_++;
00071     return;
00072   } else if (nevt_==0 && fileEventOffset_<0) {
00073     std::string line;
00074     //skip first fileEventOffset input crossings 
00075     for(unsigned i=0; i<(unsigned)abs(fileEventOffset_); i++) {
00076       unsigned iline=0;
00077       while (getline(file_, line) && !line.empty()) {
00078         iline++;
00079         if(iline*4>=EVT_MAX_SIZE)       
00080           throw cms::Exception("TextToRawEventSizeOverflow")
00081             << "TextToRaw::produce() : "
00082             << " read too many lines (" << iline << ": " << line << ")" 
00083             << ", maximum event size is " << EVT_MAX_SIZE
00084             << std::endl;
00085       }
00086     }
00087   }
00088   
00089   nevt_++;
00090   
00091    // read file
00092    std::string line;
00093    unsigned i=0; // count 32-bit words
00094 
00095    // while not encountering dumb errors
00096    while (getline(file_, line) && !line.empty() ) {
00097 
00098      // bail if we reached the EVT_MAX_SIZE
00099      if (i*4>=EVT_MAX_SIZE) {
00100        throw cms::Exception("TextToRaw")
00101          << "Read too many lines from file. Maximum event size is " << EVT_MAX_SIZE << " lines" << std::endl;
00102      }
00103 
00104      // convert string to int
00105      std::istringstream iss(line);
00106      unsigned long d;
00107      iss >> std::hex >> d;
00108 
00109      // copy data
00110      for (int j=0; j<4; j++) {
00111        if ( (i*4+j) < EVT_MAX_SIZE ) { 
00112          char c = (d>>(8*j))&0xff;
00113          data_[i*4+j] = c;
00114        }
00115      }
00116 
00117      ++i;
00118 
00119      // bail if we reached the EVT_MAX_SIZE
00120      if (i>=EVT_MAX_SIZE) {
00121        throw cms::Exception("TextToRaw")
00122          << "Read too many lines from file. Maximum event size is " << EVT_MAX_SIZE << " lines" << std::endl;
00123      }
00124 
00125    }
00126 
00127    unsigned evtSize = i * 4;
00128 
00129    // create the collection
00130    std::auto_ptr<FEDRawDataCollection> rawColl(new FEDRawDataCollection()); 
00131    // retrieve the target buffer
00132    FEDRawData& feddata=rawColl->FEDData(fedId_);
00133    // Allocate space for header+trailer+payload
00134    feddata.resize(evtSize);
00135 
00136    // fill FEDRawData object
00137    for (unsigned i=0; i<evtSize; ++i) {
00138      feddata.data()[i] = data_[i];
00139    }
00140 
00141    // put the collection in the event
00142    iEvent.put(rawColl);
00143      
00144 }
00145 
00146 
00147 // ------------ method called once each job just before starting event loop  ------------
00148 void 
00149 TextToRaw::beginJob() 
00150 {
00151   // open VME file
00152   file_.open(filename_.c_str(), std::ios::in);
00153   if(!file_.good()) { edm::LogInfo("TextToDigi") << "Failed to open ASCII file " << filename_ << std::endl; }
00154 }
00155 
00156 
00157 // ------------ method called once each job just after ending the event loop  ------------
00158 void 
00159 TextToRaw::endJob() {
00160   file_.close();
00161 }
00162