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:      AMC13DumpToRaw
0005 //
0006 /**\class AMC13DumpToRaw AMC13DumpToRaw.cc L1Trigger/L1TCalorimeter/plugins/AMC13DumpToRaw.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/AMCSpec.h"
0046 
0047 namespace l1t {
0048 
0049   class AMC13DumpToRaw : public edm::EDProducer {
0050   public:
0051     explicit AMC13DumpToRaw(const edm::ParameterSet&);
0052     ~AMC13DumpToRaw() 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   AMC13DumpToRaw::AMC13DumpToRaw(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   AMC13DumpToRaw::~AMC13DumpToRaw() {
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 AMC13DumpToRaw::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     int size = load32.size() * 4;
0132 
0133     LogDebug("L1T") << "AMC13 size " << size << " bytes";
0134 
0135     // prepare the raw data collection
0136     std::unique_ptr<FEDRawDataCollection> raw_coll(new FEDRawDataCollection());
0137     FEDRawData& fed_data = raw_coll->FEDData(fedId_);
0138 
0139     fed_data.resize(size);
0140 
0141     // fill FEDRawData object
0142     for (unsigned i = 0; i < load32.size(); ++i) {
0143       for (unsigned j = 0; j < 4; ++j) {
0144         fed_data.data()[i * 4 + j] = (load32.at(i) >> (8 * j)) & 0xff;
0145       }
0146     }
0147 
0148     //  formatRaw(iEvent, amc13, fed_data);
0149 
0150     LogDebug("L1T") << "Packing FED ID " << fedId_ << " size " << fed_data.size();
0151 
0152     // put the collection in the event
0153     iEvent.put(std::move(raw_coll));
0154   }
0155 
0156   void AMC13DumpToRaw::readEvent(std::vector<uint32_t>& load32) {
0157     // read file
0158     std::string line;
0159 
0160     // while not encountering dumb errors
0161     while (getline(file_, line) && !line.empty()) {
0162       std::istringstream iss(line);
0163       unsigned long d;
0164       iss >> std::hex >> d;
0165 
0166       load32.push_back(d);
0167     }
0168   }
0169 
0170   // void
0171   // AMC13DumpToRaw::formatAMC(amc13::Packet& amc13, const std::vector<uint32_t>& load32) {
0172 
0173   //   // TODO this is an empty word to be replaced with a proper MP7
0174   //   // header containing at least the firmware version
0175 
0176   //   std::vector<uint64_t> load64;
0177   //   for (unsigned int i = 0; i < load32.size(); i += 2) {
0178   //     uint64_t word = load32[i];
0179   //     if (i + 1 < load32.size())
0180   //       word |= static_cast<uint64_t>(load32[i + 1]) << 32;
0181   //     load64.push_back(word);
0182   //   }
0183 
0184   //   LogDebug("L1T") << "Creating AMC packet " << iAmc_;
0185 
0186   //   amc13.add(iAmc_, boardId_, load64);
0187 
0188   // }
0189 
0190   // void
0191   // AMC13DumpToRaw::formatRaw(edm::Event& iEvent, amc13::Packet& amc13, FEDRawData& fed_data)
0192   // {
0193 
0194   //   unsigned int size = slinkHeaderSize_ + slinkTrailerSize_ + amc13.size() * 8;
0195   //   fed_data.resize(size);
0196   //   unsigned char * payload = fed_data.data();
0197   //   unsigned char * payload_start = payload;
0198 
0199   //   auto bxId = iEvent.bunchCrossing();
0200   //   auto evtId = iEvent.id().event();
0201 
0202   //   LogDebug("L1T") << "Creating FEDRawData ID " << fedId_ << ", size " << size;
0203 
0204   //   FEDHeader header(payload);
0205   //   header.set(payload, evType_, evtId, bxId, fedId_);
0206 
0207   //   payload += slinkHeaderSize_;
0208 
0209   //   amc13.write(iEvent, payload, size - slinkHeaderSize_ - slinkTrailerSize_);
0210 
0211   //   payload += amc13.size() * 8;
0212 
0213   //   FEDTrailer trailer(payload);
0214   //   trailer.set(payload, size / 8, evf::compute_crc(payload_start, size), 0, 0);
0215 
0216   // }
0217 
0218   // ------------ method called once each job just before starting event loop  ------------
0219   void AMC13DumpToRaw::beginJob() {
0220     // open VME file
0221     file_.open(filename_.c_str(), std::ios::in);
0222     if (!file_.good()) {
0223       edm::LogInfo("TextToDigi") << "Failed to open ASCII file " << filename_ << std::endl;
0224     }
0225   }
0226 
0227   // ------------ method called once each job just after ending the event loop  ------------
0228   void AMC13DumpToRaw::endJob() { file_.close(); }
0229 
0230   // ------------ method called when starting to processes a run  ------------
0231   /*
0232 void 
0233 AMC13DumpToRaw::beginRun(edm::Run const&, edm::EventSetup const&)
0234 {
0235 }
0236 */
0237 
0238   // ------------ method called when ending the processing of a run  ------------
0239   /*
0240 void 
0241 AMC13DumpToRaw::endRun(edm::Run const&, edm::EventSetup const&)
0242 {
0243 }
0244 */
0245 
0246   // ------------ method called when starting to processes a luminosity block  ------------
0247   /*
0248 vvoid 
0249 AMC13DumpToRaw::beginLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&)
0250 {
0251 }
0252 */
0253 
0254   // ------------ method called when ending the processing of a luminosity block  ------------
0255   /*
0256 void 
0257 AMC13DumpToRaw::endLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&)
0258 {
0259 }
0260 */
0261 
0262   // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
0263   void AMC13DumpToRaw::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0264     //The following says we do not know what parameters are allowed so do no validation
0265     // Please change this to state exactly what you do use, even if it is no parameters
0266     edm::ParameterSetDescription desc;
0267     desc.setUnknown();
0268     descriptions.addDefault(desc);
0269   }
0270 
0271 }  // namespace l1t
0272 
0273 using namespace l1t;
0274 //define this as a plug-in
0275 DEFINE_FWK_MODULE(AMC13DumpToRaw);