Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-09-29 01:11:51

0001 // -*- C++ -*-
0002 //
0003 // Package:    EventFilter/L1TRawToDigi
0004 // Class:      AMCDumpToRaw
0005 //
0006 /**\class AMCDumpToRaw AMCDumpToRaw.cc L1Trigger/L1TCalorimeter/plugins/AMCDumpToRaw.cc
0007 
0008  Description: [one line class summary]
0009 
0010  Implementation:
0011      [Notes on implementation]
0012 */
0013 //
0014 // Original Author:  James Brooke
0015 //         Created:  Tue, 11 Mar 2014 14:55:45 GMT
0016 //
0017 //
0018 
0019 // system include files
0020 #include <memory>
0021 
0022 // user include files
0023 #include "FWCore/Utilities/interface/Exception.h"
0024 #include "FWCore/Framework/interface/Frameworkfwd.h"
0025 #include "FWCore/Framework/interface/one/EDProducer.h"
0026 #include "FWCore/Framework/interface/Event.h"
0027 #include "FWCore/Framework/interface/MakerMacros.h"
0028 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0029 #include "FWCore/ServiceRegistry/interface/Service.h"
0030 
0031 #include "DataFormats/FEDRawData/interface/FEDRawData.h"
0032 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
0033 #include "DataFormats/FEDRawData/interface/FEDHeader.h"
0034 #include "DataFormats/FEDRawData/interface/FEDTrailer.h"
0035 
0036 #include "FWCore/Utilities/interface/CRC16.h"
0037 
0038 #include <fstream>
0039 #include <iostream>
0040 #include <sstream>
0041 #include <string>
0042 #include <iomanip>
0043 #include <boost/algorithm/string.hpp>
0044 
0045 #include "EventFilter/L1TRawToDigi/interface/AMC13Spec.h"
0046 
0047 namespace l1t {
0048 
0049   class AMCDumpToRaw : public edm::one::EDProducer<> {
0050   public:
0051     explicit AMCDumpToRaw(const edm::ParameterSet&);
0052     ~AMCDumpToRaw() override;
0053 
0054     static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0055 
0056   private:
0057     void beginJob() override;
0058     void produce(edm::Event&, const edm::EventSetup&) override;
0059     void endJob() override;
0060 
0061     void readEvent(std::vector<uint32_t>& load32);
0062 
0063     void formatAMC(amc13::Packet& amc13, const std::vector<uint32_t>& load32);
0064 
0065     void formatRaw(edm::Event& iEvent, amc13::Packet& amc13, FEDRawData& fed_data);
0066 
0067     // ----------member data ---------------------------
0068     std::ifstream file_;
0069     std::string filename_;
0070 
0071     // DAQ params
0072     int fedId_;
0073     int iAmc_;
0074     int boardId_;
0075     int evType_;
0076     int fwVer_;
0077     int slinkHeaderSize_;  // in 8-bit words
0078     int slinkTrailerSize_;
0079   };
0080 
0081   //
0082   // constants, enums and typedefs
0083   //
0084 
0085   //
0086   // static data member definitions
0087   //
0088 
0089   //
0090   // constructors and destructor
0091   //
0092   AMCDumpToRaw::AMCDumpToRaw(const edm::ParameterSet& iConfig)
0093       : filename_(iConfig.getUntrackedParameter<std::string>("filename", "data.txt")),
0094         fedId_(iConfig.getUntrackedParameter<int>("fedId", 1)),
0095         iAmc_(iConfig.getUntrackedParameter<int>("iAmc", 1)),
0096         boardId_(iConfig.getUntrackedParameter<int>("boardId", 1)),
0097         evType_(iConfig.getUntrackedParameter<int>("eventType", 1)),
0098         fwVer_(iConfig.getUntrackedParameter<int>("fwVersion", 1)),
0099         slinkHeaderSize_(iConfig.getUntrackedParameter<int>("lenSlinkHeader", 8)),
0100         slinkTrailerSize_(iConfig.getUntrackedParameter<int>("lenSlinkTrailer", 8)) {
0101     produces<FEDRawDataCollection>();
0102   }
0103 
0104   AMCDumpToRaw::~AMCDumpToRaw() {
0105     // do anything here that needs to be done at desctruction time
0106     // (e.g. close files, deallocate resources etc.)
0107   }
0108 
0109   //
0110   // member functions
0111   //
0112 
0113   // ------------ method called for each event  ------------
0114   void AMCDumpToRaw::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0115     using namespace edm;
0116 
0117     // create AMC 13 packet
0118     amc13::Packet amc13;
0119 
0120     std::vector<uint32_t> load32;
0121 
0122     readEvent(load32);
0123 
0124     formatAMC(amc13, load32);
0125 
0126     LogDebug("L1T") << "AMC13 size " << amc13.size();
0127 
0128     // prepare the raw data collection
0129     std::unique_ptr<FEDRawDataCollection> raw_coll(new FEDRawDataCollection());
0130     FEDRawData& fed_data = raw_coll->FEDData(fedId_);
0131 
0132     formatRaw(iEvent, amc13, fed_data);
0133 
0134     LogDebug("L1T") << "Packing FED ID " << fedId_ << " size " << fed_data.size();
0135 
0136     // put the collection in the event
0137     iEvent.put(std::move(raw_coll));
0138   }
0139 
0140   void AMCDumpToRaw::readEvent(std::vector<uint32_t>& load32) {
0141     // read file
0142     std::string line;
0143 
0144     // while not encountering dumb errors
0145     while (getline(file_, line) && !line.empty()) {
0146       std::istringstream iss(line);
0147       unsigned long d;
0148       iss >> std::hex >> d;
0149 
0150       load32.push_back(d);
0151     }
0152   }
0153 
0154   void AMCDumpToRaw::formatAMC(amc13::Packet& amc13, const std::vector<uint32_t>& load32) {
0155     // TODO this is an empty word to be replaced with a proper MP7
0156     // header containing at least the firmware version
0157 
0158     std::vector<uint64_t> load64;
0159     for (unsigned int i = 0; i < load32.size(); i += 2) {
0160       uint64_t word = load32[i];
0161       if (i + 1 < load32.size())
0162         word |= static_cast<uint64_t>(load32[i + 1]) << 32;
0163       load64.push_back(word);
0164     }
0165 
0166     LogDebug("L1T") << "Creating AMC packet " << iAmc_;
0167 
0168     amc13.add(iAmc_, boardId_, 0, 0, 0, load64);
0169   }
0170 
0171   void AMCDumpToRaw::formatRaw(edm::Event& iEvent, amc13::Packet& amc13, FEDRawData& fed_data) {
0172     unsigned int size = slinkHeaderSize_ + slinkTrailerSize_ + amc13.size() * 8;
0173     fed_data.resize(size);
0174     unsigned char* payload = fed_data.data();
0175     unsigned char* payload_start = payload;
0176 
0177     auto bxId = iEvent.bunchCrossing();
0178     auto evtId = iEvent.id().event();
0179 
0180     LogDebug("L1T") << "Creating FEDRawData ID " << fedId_ << ", size " << size;
0181 
0182     FEDHeader header(payload);
0183     header.set(payload, evType_, evtId, bxId, fedId_);
0184 
0185     amc13.write(iEvent, payload, slinkHeaderSize_, size - slinkHeaderSize_ - slinkTrailerSize_);
0186 
0187     payload += slinkHeaderSize_;
0188     payload += amc13.size() * 8;
0189 
0190     FEDTrailer trailer(payload);
0191     trailer.set(payload, size / 8, evf::compute_crc(payload_start, size), 0, 0);
0192   }
0193 
0194   // ------------ method called once each job just before starting event loop  ------------
0195   void AMCDumpToRaw::beginJob() {
0196     // open VME file
0197     file_.open(filename_.c_str(), std::ios::in);
0198     if (!file_.good()) {
0199       edm::LogInfo("TextToDigi") << "Failed to open ASCII file " << filename_ << std::endl;
0200     }
0201   }
0202 
0203   // ------------ method called once each job just after ending the event loop  ------------
0204   void AMCDumpToRaw::endJob() { file_.close(); }
0205 
0206   // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
0207   void AMCDumpToRaw::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0208     //The following says we do not know what parameters are allowed so do no validation
0209     // Please change this to state exactly what you do use, even if it is no parameters
0210     edm::ParameterSetDescription desc;
0211     desc.setUnknown();
0212     descriptions.addDefault(desc);
0213   }
0214 
0215 }  // namespace l1t
0216 
0217 using namespace l1t;
0218 //define this as a plug-in
0219 DEFINE_FWK_MODULE(AMCDumpToRaw);