Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 
0002 
0003 #include "L1Trigger/TextToDigi/plugins/TextToRaw.h"
0004 
0005 // system
0006 #include <fstream>
0007 #include <iostream>
0008 #include <string>
0009 #include <vector>
0010 
0011 // framework
0012 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0013 
0014 #include "FWCore/Utilities/interface/Exception.h"
0015 
0016 // Raw data collection
0017 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
0018 
0019 using std::cerr;
0020 using std::cout;
0021 using std::endl;
0022 using std::ios;
0023 using std::string;
0024 using std::vector;
0025 
0026 const unsigned TextToRaw::EVT_MAX_SIZE;
0027 
0028 TextToRaw::TextToRaw(const edm::ParameterSet &iConfig)
0029     : fedId_(iConfig.getUntrackedParameter<int>("fedId", 745)),
0030       filename_(iConfig.getUntrackedParameter<std::string>("filename", "slinkOutput.txt")),
0031       fileEventOffset_(iConfig.getUntrackedParameter<int>("FileEventOffset", 0)),
0032       nevt_(0) {
0033   edm::LogInfo("TextToDigi") << "Reading ASCII dump from " << filename_ << std::endl;
0034 
0035   // register the products
0036   produces<FEDRawDataCollection>();
0037 }
0038 
0039 TextToRaw::~TextToRaw() {
0040   // do anything here that needs to be done at desctruction time
0041   // (e.g. close files, deallocate resources etc.)
0042 }
0043 
0044 /// Append empty digi collection
0045 void TextToRaw::putEmptyDigi(edm::Event &iEvent) {
0046   std::unique_ptr<FEDRawDataCollection> rawColl(new FEDRawDataCollection());
0047   // FEDRawData& feddata=rawColl->FEDData(fedId_);
0048   // feddata.data()[0] = 0;
0049   iEvent.put(std::move(rawColl));
0050 }
0051 
0052 // ------------ method called to produce the data  ------------
0053 void TextToRaw::produce(edm::Event &iEvent, const edm::EventSetup &iSetup) {
0054   using namespace edm;
0055 
0056   // Skip event if required
0057   if (nevt_ < fileEventOffset_) {
0058     putEmptyDigi(iEvent);
0059     nevt_++;
0060     return;
0061   } else if (nevt_ == 0 && fileEventOffset_ < 0) {
0062     std::string line;
0063     // skip first fileEventOffset input crossings
0064     for (unsigned i = 0; i < (unsigned)abs(fileEventOffset_); i++) {
0065       unsigned iline = 0;
0066       while (getline(file_, line) && !line.empty()) {
0067         iline++;
0068         if (iline * 4 >= EVT_MAX_SIZE)
0069           throw cms::Exception("TextToRawEventSizeOverflow") << "TextToRaw::produce() : "
0070                                                              << " read too many lines (" << iline << ": " << line << ")"
0071                                                              << ", maximum event size is " << EVT_MAX_SIZE << std::endl;
0072       }
0073     }
0074   }
0075 
0076   nevt_++;
0077 
0078   // read file
0079   std::string line;
0080   unsigned i = 0;  // count 32-bit words
0081 
0082   // while not encountering dumb errors
0083   while (getline(file_, line) && !line.empty()) {
0084     // bail if we reached the EVT_MAX_SIZE
0085     if (i * 4 >= EVT_MAX_SIZE) {
0086       throw cms::Exception("TextToRaw") << "Read too many lines from file. Maximum event size is " << EVT_MAX_SIZE
0087                                         << " lines" << std::endl;
0088     }
0089 
0090     // convert string to int
0091     std::istringstream iss(line);
0092     unsigned long d;
0093     iss >> std::hex >> d;
0094 
0095     // copy data
0096     for (int j = 0; j < 4; j++) {
0097       if ((i * 4 + j) < EVT_MAX_SIZE) {
0098         char c = (d >> (8 * j)) & 0xff;
0099         data_[i * 4 + j] = c;
0100       }
0101     }
0102 
0103     ++i;
0104 
0105     // bail if we reached the EVT_MAX_SIZE
0106     if (i >= EVT_MAX_SIZE) {
0107       throw cms::Exception("TextToRaw") << "Read too many lines from file. Maximum event size is " << EVT_MAX_SIZE
0108                                         << " lines" << std::endl;
0109     }
0110   }
0111 
0112   unsigned evtSize = i * 4;
0113 
0114   // create the collection
0115   std::unique_ptr<FEDRawDataCollection> rawColl(new FEDRawDataCollection());
0116   // retrieve the target buffer
0117   FEDRawData &feddata = rawColl->FEDData(fedId_);
0118   // Allocate space for header+trailer+payload
0119   feddata.resize(evtSize);
0120 
0121   // fill FEDRawData object
0122   for (unsigned i = 0; i < evtSize; ++i) {
0123     feddata.data()[i] = data_[i];
0124   }
0125 
0126   // put the collection in the event
0127   iEvent.put(std::move(rawColl));
0128 }
0129 
0130 // ------------ method called once each job just before starting event loop
0131 // ------------
0132 void TextToRaw::beginJob() {
0133   // open VME file
0134   file_.open(filename_.c_str(), std::ios::in);
0135   if (!file_.good()) {
0136     edm::LogInfo("TextToDigi") << "Failed to open ASCII file " << filename_ << std::endl;
0137   }
0138 }
0139 
0140 // ------------ method called once each job just after ending the event loop
0141 // ------------
0142 void TextToRaw::endJob() { file_.close(); }