Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:10:53

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/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/AMCSpec.h"
0046 
0047 namespace l1t {
0048 
0049   class AMC13DumpToRaw : public edm::one::EDProducer<> {
0050   public:
0051     explicit AMC13DumpToRaw(const edm::ParameterSet&);
0052 
0053     static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0054 
0055   private:
0056     void beginJob() override;
0057     void produce(edm::Event&, const edm::EventSetup&) override;
0058     void endJob() override;
0059 
0060     void readEvent(std::vector<uint32_t>& load32);
0061 
0062     //  void formatAMC(amc13::Packet& amc13, const std::vector<uint32_t>& load32);
0063 
0064     //  void formatRaw(edm::Event& iEvent, amc13::Packet& amc13, FEDRawData& fed_data);
0065 
0066     // ----------member data ---------------------------
0067     std::ifstream file_;
0068     std::string filename_;
0069 
0070     // DAQ params
0071     int fedId_;
0072     int iAmc_;
0073     int boardId_;
0074     int evType_;
0075     int fwVer_;
0076     int slinkHeaderSize_;  // in 8-bit words
0077     int slinkTrailerSize_;
0078   };
0079 
0080   //
0081   // constants, enums and typedefs
0082   //
0083 
0084   //
0085   // static data member definitions
0086   //
0087 
0088   //
0089   // constructors and destructor
0090   //
0091   AMC13DumpToRaw::AMC13DumpToRaw(const edm::ParameterSet& iConfig)
0092       : filename_(iConfig.getUntrackedParameter<std::string>("filename", "data.txt")),
0093         fedId_(iConfig.getUntrackedParameter<int>("fedId", 1)),
0094         iAmc_(iConfig.getUntrackedParameter<int>("iAmc", 1)),
0095         boardId_(iConfig.getUntrackedParameter<int>("boardId", 1)),
0096         evType_(iConfig.getUntrackedParameter<int>("eventType", 1)),
0097         fwVer_(iConfig.getUntrackedParameter<int>("fwVersion", 1)),
0098         slinkHeaderSize_(iConfig.getUntrackedParameter<int>("lenSlinkHeader", 8)),
0099         slinkTrailerSize_(iConfig.getUntrackedParameter<int>("lenSlinkTrailer", 8)) {
0100     produces<FEDRawDataCollection>();
0101   }
0102 
0103   //
0104   // member functions
0105   //
0106 
0107   // ------------ method called for each event  ------------
0108   void AMC13DumpToRaw::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0109     using namespace edm;
0110 
0111     // create AMC 13 packet
0112     // amc13::Packet amc13;
0113 
0114     std::vector<uint32_t> load32;
0115 
0116     readEvent(load32);
0117 
0118     //  formatAMC(amc13, load32);
0119 
0120     int size = load32.size() * 4;
0121 
0122     LogDebug("L1T") << "AMC13 size " << size << " bytes";
0123 
0124     // prepare the raw data collection
0125     std::unique_ptr<FEDRawDataCollection> raw_coll(new FEDRawDataCollection());
0126     FEDRawData& fed_data = raw_coll->FEDData(fedId_);
0127 
0128     fed_data.resize(size);
0129 
0130     // fill FEDRawData object
0131     for (unsigned i = 0; i < load32.size(); ++i) {
0132       for (unsigned j = 0; j < 4; ++j) {
0133         fed_data.data()[i * 4 + j] = (load32.at(i) >> (8 * j)) & 0xff;
0134       }
0135     }
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 AMC13DumpToRaw::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
0160   // AMC13DumpToRaw::formatAMC(amc13::Packet& amc13, const std::vector<uint32_t>& load32) {
0161 
0162   //   // TODO this is an empty word to be replaced with a proper MP7
0163   //   // header containing at least the firmware version
0164 
0165   //   std::vector<uint64_t> load64;
0166   //   for (unsigned int i = 0; i < load32.size(); i += 2) {
0167   //     uint64_t word = load32[i];
0168   //     if (i + 1 < load32.size())
0169   //       word |= static_cast<uint64_t>(load32[i + 1]) << 32;
0170   //     load64.push_back(word);
0171   //   }
0172 
0173   //   LogDebug("L1T") << "Creating AMC packet " << iAmc_;
0174 
0175   //   amc13.add(iAmc_, boardId_, load64);
0176 
0177   // }
0178 
0179   // void
0180   // AMC13DumpToRaw::formatRaw(edm::Event& iEvent, amc13::Packet& amc13, FEDRawData& fed_data)
0181   // {
0182 
0183   //   unsigned int size = slinkHeaderSize_ + slinkTrailerSize_ + amc13.size() * 8;
0184   //   fed_data.resize(size);
0185   //   unsigned char * payload = fed_data.data();
0186   //   unsigned char * payload_start = payload;
0187 
0188   //   auto bxId = iEvent.bunchCrossing();
0189   //   auto evtId = iEvent.id().event();
0190 
0191   //   LogDebug("L1T") << "Creating FEDRawData ID " << fedId_ << ", size " << size;
0192 
0193   //   FEDHeader header(payload);
0194   //   header.set(payload, evType_, evtId, bxId, fedId_);
0195 
0196   //   payload += slinkHeaderSize_;
0197 
0198   //   amc13.write(iEvent, payload, size - slinkHeaderSize_ - slinkTrailerSize_);
0199 
0200   //   payload += amc13.size() * 8;
0201 
0202   //   FEDTrailer trailer(payload);
0203   //   trailer.set(payload, size / 8, evf::compute_crc(payload_start, size), 0, 0);
0204 
0205   // }
0206 
0207   // ------------ method called once each job just before starting event loop  ------------
0208   void AMC13DumpToRaw::beginJob() {
0209     // open VME file
0210     file_.open(filename_.c_str(), std::ios::in);
0211     if (!file_.good()) {
0212       edm::LogInfo("TextToDigi") << "Failed to open ASCII file " << filename_ << std::endl;
0213     }
0214   }
0215 
0216   // ------------ method called once each job just after ending the event loop  ------------
0217   void AMC13DumpToRaw::endJob() { file_.close(); }
0218 
0219   // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
0220   void AMC13DumpToRaw::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0221     //The following says we do not know what parameters are allowed so do no validation
0222     // Please change this to state exactly what you do use, even if it is no parameters
0223     edm::ParameterSetDescription desc;
0224     desc.setUnknown();
0225     descriptions.addDefault(desc);
0226   }
0227 
0228 }  // namespace l1t
0229 
0230 using namespace l1t;
0231 //define this as a plug-in
0232 DEFINE_FWK_MODULE(AMC13DumpToRaw);