Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:26:07

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/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::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     //virtual void beginRun(edm::Run const&, edm::EventSetup const&) override;
0068     //virtual void endRun(edm::Run const&, edm::EventSetup const&) override;
0069     //virtual void beginLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override;
0070     //virtual void endLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override;
0071 
0072     // ----------member data ---------------------------
0073     std::ifstream file_;
0074     std::string filename_;
0075 
0076     // DAQ params
0077     int fedId_;
0078     int iAmc_;
0079     int boardId_;
0080     int evType_;
0081     int fwVer_;
0082     int slinkHeaderSize_;  // in 8-bit words
0083     int slinkTrailerSize_;
0084   };
0085 
0086   //
0087   // constants, enums and typedefs
0088   //
0089 
0090   //
0091   // static data member definitions
0092   //
0093 
0094   //
0095   // constructors and destructor
0096   //
0097   AMCDumpToRaw::AMCDumpToRaw(const edm::ParameterSet& iConfig)
0098       : filename_(iConfig.getUntrackedParameter<std::string>("filename", "data.txt")),
0099         fedId_(iConfig.getUntrackedParameter<int>("fedId", 1)),
0100         iAmc_(iConfig.getUntrackedParameter<int>("iAmc", 1)),
0101         boardId_(iConfig.getUntrackedParameter<int>("boardId", 1)),
0102         evType_(iConfig.getUntrackedParameter<int>("eventType", 1)),
0103         fwVer_(iConfig.getUntrackedParameter<int>("fwVersion", 1)),
0104         slinkHeaderSize_(iConfig.getUntrackedParameter<int>("lenSlinkHeader", 8)),
0105         slinkTrailerSize_(iConfig.getUntrackedParameter<int>("lenSlinkTrailer", 8)) {
0106     produces<FEDRawDataCollection>();
0107   }
0108 
0109   AMCDumpToRaw::~AMCDumpToRaw() {
0110     // do anything here that needs to be done at desctruction time
0111     // (e.g. close files, deallocate resources etc.)
0112   }
0113 
0114   //
0115   // member functions
0116   //
0117 
0118   // ------------ method called for each event  ------------
0119   void AMCDumpToRaw::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0120     using namespace edm;
0121 
0122     // create AMC 13 packet
0123     amc13::Packet amc13;
0124 
0125     std::vector<uint32_t> load32;
0126 
0127     readEvent(load32);
0128 
0129     formatAMC(amc13, load32);
0130 
0131     LogDebug("L1T") << "AMC13 size " << amc13.size();
0132 
0133     // prepare the raw data collection
0134     std::unique_ptr<FEDRawDataCollection> raw_coll(new FEDRawDataCollection());
0135     FEDRawData& fed_data = raw_coll->FEDData(fedId_);
0136 
0137     formatRaw(iEvent, amc13, fed_data);
0138 
0139     LogDebug("L1T") << "Packing FED ID " << fedId_ << " size " << fed_data.size();
0140 
0141     // put the collection in the event
0142     iEvent.put(std::move(raw_coll));
0143   }
0144 
0145   void AMCDumpToRaw::readEvent(std::vector<uint32_t>& load32) {
0146     // read file
0147     std::string line;
0148 
0149     // while not encountering dumb errors
0150     while (getline(file_, line) && !line.empty()) {
0151       std::istringstream iss(line);
0152       unsigned long d;
0153       iss >> std::hex >> d;
0154 
0155       load32.push_back(d);
0156     }
0157   }
0158 
0159   void AMCDumpToRaw::formatAMC(amc13::Packet& amc13, const std::vector<uint32_t>& load32) {
0160     // TODO this is an empty word to be replaced with a proper MP7
0161     // header containing at least the firmware version
0162 
0163     std::vector<uint64_t> load64;
0164     for (unsigned int i = 0; i < load32.size(); i += 2) {
0165       uint64_t word = load32[i];
0166       if (i + 1 < load32.size())
0167         word |= static_cast<uint64_t>(load32[i + 1]) << 32;
0168       load64.push_back(word);
0169     }
0170 
0171     LogDebug("L1T") << "Creating AMC packet " << iAmc_;
0172 
0173     amc13.add(iAmc_, boardId_, 0, 0, 0, load64);
0174   }
0175 
0176   void AMCDumpToRaw::formatRaw(edm::Event& iEvent, amc13::Packet& amc13, FEDRawData& fed_data) {
0177     unsigned int size = slinkHeaderSize_ + slinkTrailerSize_ + amc13.size() * 8;
0178     fed_data.resize(size);
0179     unsigned char* payload = fed_data.data();
0180     unsigned char* payload_start = payload;
0181 
0182     auto bxId = iEvent.bunchCrossing();
0183     auto evtId = iEvent.id().event();
0184 
0185     LogDebug("L1T") << "Creating FEDRawData ID " << fedId_ << ", size " << size;
0186 
0187     FEDHeader header(payload);
0188     header.set(payload, evType_, evtId, bxId, fedId_);
0189 
0190     amc13.write(iEvent, payload, slinkHeaderSize_, size - slinkHeaderSize_ - slinkTrailerSize_);
0191 
0192     payload += slinkHeaderSize_;
0193     payload += amc13.size() * 8;
0194 
0195     FEDTrailer trailer(payload);
0196     trailer.set(payload, size / 8, evf::compute_crc(payload_start, size), 0, 0);
0197   }
0198 
0199   // ------------ method called once each job just before starting event loop  ------------
0200   void AMCDumpToRaw::beginJob() {
0201     // open VME file
0202     file_.open(filename_.c_str(), std::ios::in);
0203     if (!file_.good()) {
0204       edm::LogInfo("TextToDigi") << "Failed to open ASCII file " << filename_ << std::endl;
0205     }
0206   }
0207 
0208   // ------------ method called once each job just after ending the event loop  ------------
0209   void AMCDumpToRaw::endJob() { file_.close(); }
0210 
0211   // ------------ method called when starting to processes a run  ------------
0212   /*
0213 void 
0214 AMCDumpToRaw::beginRun(edm::Run const&, edm::EventSetup const&)
0215 {
0216 }
0217 */
0218 
0219   // ------------ method called when ending the processing of a run  ------------
0220   /*
0221 void 
0222 AMCDumpToRaw::endRun(edm::Run const&, edm::EventSetup const&)
0223 {
0224 }
0225 */
0226 
0227   // ------------ method called when starting to processes a luminosity block  ------------
0228   /*
0229 vvoid 
0230 AMCDumpToRaw::beginLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&)
0231 {
0232 }
0233 */
0234 
0235   // ------------ method called when ending the processing of a luminosity block  ------------
0236   /*
0237 void 
0238 AMCDumpToRaw::endLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&)
0239 {
0240 }
0241 */
0242 
0243   // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
0244   void AMCDumpToRaw::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0245     //The following says we do not know what parameters are allowed so do no validation
0246     // Please change this to state exactly what you do use, even if it is no parameters
0247     edm::ParameterSetDescription desc;
0248     desc.setUnknown();
0249     descriptions.addDefault(desc);
0250   }
0251 
0252 }  // namespace l1t
0253 
0254 using namespace l1t;
0255 //define this as a plug-in
0256 DEFINE_FWK_MODULE(AMCDumpToRaw);