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:      MP7BufferDumpToRaw
0005 //
0006 /**\class Stage2InputPatternWriter Stage2InputPatternWriter.cc L1Trigger/L1TCalorimeter/plugins/Stage2InputPatternWriter.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/MP7FileReader.h"
0046 #include "EventFilter/L1TRawToDigi/interface/MP7PacketReader.h"
0047 #include "EventFilter/L1TRawToDigi/interface/Block.h"
0048 #include "EventFilter/L1TRawToDigi/interface/AMC13Spec.h"
0049 //#include "EventFilter/L1TRawToDigi/interface/PackingSetup.h"
0050 //
0051 // class declaration
0052 //
0053 
0054 namespace l1t {
0055 
0056   class MP7BufferDumpToRaw : public edm::one::EDProducer<> {
0057   public:
0058     explicit MP7BufferDumpToRaw(const edm::ParameterSet&);
0059     ~MP7BufferDumpToRaw() override;
0060 
0061     static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0062 
0063   private:
0064     void produce(edm::Event&, const edm::EventSetup&) override;
0065 
0066     std::vector<Block> getBlocks(int iAmc);
0067 
0068     void formatAMC(amc13::Packet& amc13, const std::vector<Block>& blocks, int iAmc);
0069 
0070     void formatRaw(edm::Event& iEvent, amc13::Packet& amc13, FEDRawData& fed_data);
0071 
0072     // ----------member data ---------------------------
0073 
0074     // file readers
0075     MP7FileReader rxFileReader_;
0076     MP7FileReader txFileReader_;
0077     std::vector<unsigned> rxIndex_;
0078     std::vector<unsigned> txIndex_;
0079 
0080     // packet readers
0081     MP7PacketReader rxPacketReader_;
0082     MP7PacketReader txPacketReader_;
0083     //  MP7PacketReader::PacketData::const_iter rxItr_;
0084     //  MP7PacketReader::PacketData txItr_;
0085 
0086     // formatting parameters
0087     bool packetisedData_;
0088 
0089     // non packetised data parameters
0090     unsigned nFramesPerEvent_;
0091 
0092     // packetised data parameters
0093 
0094     // hardware params
0095     unsigned nBoard_;
0096     unsigned iBoard_;
0097     std::vector<int> boardId_;
0098 
0099     // board readout params
0100     std::vector<std::vector<int> > rxBlockLength_;
0101     std::vector<std::vector<int> > txBlockLength_;
0102     bool mux_;
0103     int muxOffset_;
0104 
0105     // DAQ params
0106     int fedId_;
0107     int evType_;
0108     int fwVer_;
0109     int slinkHeaderSize_;  // in 8-bit words
0110     int slinkTrailerSize_;
0111   };
0112 
0113   //
0114   // constants, enums and typedefs
0115   //
0116 
0117   //
0118   // static data member definitions
0119   //
0120 
0121   //
0122   // constructors and destructor
0123   //
0124   MP7BufferDumpToRaw::MP7BufferDumpToRaw(const edm::ParameterSet& iConfig)
0125       : rxFileReader_(iConfig.getUntrackedParameter<std::string>("rxFile", "rx_summary.txt")),
0126         txFileReader_(iConfig.getUntrackedParameter<std::string>("txFile", "tx_summary.txt")),
0127         rxPacketReader_(iConfig.getUntrackedParameter<std::string>("rxFile", "rx_summary.txt"),
0128                         iConfig.getUntrackedParameter<int>("rxHeaderFrames", 1),
0129                         0,
0130                         iConfig.getUntrackedParameter<int>("rxKeyLink", 0)),
0131         txPacketReader_(iConfig.getUntrackedParameter<std::string>("txFile", "tx_summary.txt"),
0132                         iConfig.getUntrackedParameter<int>("txHeaderFrames", 1),
0133                         0,
0134                         iConfig.getUntrackedParameter<int>("txKeyLink", 0)),
0135         packetisedData_(iConfig.getUntrackedParameter<bool>("packetisedData", true)),
0136         nFramesPerEvent_(iConfig.getUntrackedParameter<int>("nFramesPerEvent", 6)),
0137         iBoard_(iConfig.getUntrackedParameter<int>("boardOffset", 0)),
0138         boardId_(iConfig.getUntrackedParameter<std::vector<int> >("boardId")),
0139         mux_(iConfig.getUntrackedParameter<bool>("mux", false)),
0140         fedId_(iConfig.getUntrackedParameter<int>("fedId", 1)),
0141         evType_(iConfig.getUntrackedParameter<int>("eventType", 1)),
0142         fwVer_(iConfig.getUntrackedParameter<int>("fwVersion", 1)),
0143         slinkHeaderSize_(iConfig.getUntrackedParameter<int>("lenSlinkHeader", 8)),
0144         slinkTrailerSize_(iConfig.getUntrackedParameter<int>("lenSlinkTrailer", 8)) {
0145     produces<FEDRawDataCollection>();
0146 
0147     // check tx/rx file size consistency and number of boards
0148     if (rxFileReader_.size() != txFileReader_.size()) {
0149       edm::LogError("L1T") << "Different number of boards in Rx and Tx files";
0150     }
0151     nBoard_ = std::max(rxFileReader_.size(), txFileReader_.size());
0152     LogDebug("L1T") << "# boards : " << nBoard_;
0153 
0154     // advance pointers for non packetised data
0155     rxIndex_ = iConfig.getUntrackedParameter<std::vector<unsigned> >("nFramesOffset");
0156     if (rxIndex_.size() != nBoard_) {
0157       edm::LogError("L1T") << "Wrong number of boards in nFramesOffset " << rxIndex_.size();
0158     }
0159 
0160     txIndex_ = iConfig.getUntrackedParameter<std::vector<unsigned> >("nFramesLatency");
0161     if (txIndex_.size() != nBoard_) {
0162       edm::LogError("L1T") << "Wrong number of boards in nFramesLatency " << txIndex_.size();
0163     }
0164 
0165     // add latency to offset for Tx
0166     for (unsigned i = 0; i < rxIndex_.size(); ++i)
0167       txIndex_.at(i) += rxIndex_.at(i);
0168 
0169     // check board IDs
0170     if (nBoard_ != boardId_.size()) {
0171       edm::LogError("L1T") << "Found " << nBoard_ << " boards, but given " << boardId_.size() << " IDs";
0172     }
0173 
0174     // block length PSet
0175     std::vector<edm::ParameterSet> vpset = iConfig.getUntrackedParameter<std::vector<edm::ParameterSet> >("blocks");
0176 
0177     if (vpset.size() != nBoard_) {
0178       edm::LogError("L1T") << "Wrong number of block specs " << vpset.size();
0179     }
0180 
0181     rxBlockLength_.resize(nBoard_);
0182     txBlockLength_.resize(nBoard_);
0183 
0184     for (unsigned i = 0; i < nBoard_; ++i) {
0185       std::vector<int> rx = vpset.at(i).getUntrackedParameter<std::vector<int> >("rxBlockLength");
0186 
0187       rxBlockLength_.at(i).resize(rx.size());
0188 
0189       for (unsigned j = 0; j < rx.size(); ++j) {
0190         rxBlockLength_.at(i).at(j) = rx.at(j);
0191 
0192         if (rx.at(j) != 0) {
0193           //    LogDebug("L1T") << "Block readout : board " << i << " Rx link " << j << " size " << rx.at(j);
0194         }
0195       }
0196 
0197       std::vector<int> tx = vpset.at(i).getUntrackedParameter<std::vector<int> >("txBlockLength");
0198       txBlockLength_.at(i).resize(tx.size());
0199 
0200       for (unsigned j = 0; j < tx.size(); ++j) {
0201         txBlockLength_.at(i).at(j) = tx.at(j);
0202 
0203         if (tx.at(j) != 0) {
0204           //    LogDebug("L1T") << "Block readout : board " << i << " Tx link " << j << " size " << tx.at(j);
0205         }
0206       }
0207     }
0208 
0209     LogDebug("L1T") << "Board ID size " << boardId_.size();
0210 
0211     LogDebug("L1T") << "Frames per event " << nFramesPerEvent_;
0212   }
0213 
0214   MP7BufferDumpToRaw::~MP7BufferDumpToRaw() {
0215     // do anything here that needs to be done at desctruction time
0216     // (e.g. close files, deallocate resources etc.)
0217   }
0218 
0219   //
0220   // member functions
0221   //
0222 
0223   // ------------ method called for each event  ------------
0224   void MP7BufferDumpToRaw::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0225     using namespace edm;
0226 
0227     // AMC 13 packet
0228     amc13::Packet amc13;
0229 
0230     // create AMC formatted data
0231     if (mux_) {
0232       std::vector<Block> blocks = getBlocks(iBoard_);
0233       formatAMC(amc13, blocks, iBoard_);
0234     } else {
0235       for (unsigned iBoard = 0; iBoard < nBoard_; ++iBoard) {
0236         std::vector<Block> blocks = getBlocks(iBoard);
0237         formatAMC(amc13, blocks, iBoard);
0238       }
0239     }
0240 
0241     LogDebug("L1T") << "AMC13 size " << amc13.size();
0242 
0243     // prepare the raw data collection
0244     std::unique_ptr<FEDRawDataCollection> raw_coll(new FEDRawDataCollection());
0245     FEDRawData& fed_data = raw_coll->FEDData(fedId_);
0246 
0247     formatRaw(iEvent, amc13, fed_data);
0248 
0249     LogDebug("L1T") << "Packing FED ID " << fedId_ << " size " << fed_data.size();
0250 
0251     // put the collection in the event
0252     iEvent.put(std::move(raw_coll));
0253 
0254     //advance to next AMC for next event, if required...
0255     if (mux_) {
0256       iBoard_++;
0257       iBoard_ = iBoard_ % nBoard_;
0258     }
0259   }
0260 
0261   std::vector<Block> MP7BufferDumpToRaw::getBlocks(int iBoard) {
0262     LogDebug("L1T") << "Getting blocks from board " << iBoard << ", " << rxBlockLength_.at(iBoard).size()
0263                     << " Rx links, " << txBlockLength_.at(iBoard).size() << " Tx links";
0264 
0265     std::vector<Block> blocks;
0266 
0267     // Rx blocks first
0268     for (unsigned link = 0; link < rxBlockLength_.at(iBoard).size(); ++link) {
0269       unsigned id = link * 2;
0270       unsigned size = rxBlockLength_.at(iBoard).at(link);
0271 
0272       if (size == 0)
0273         continue;
0274 
0275       std::vector<uint32_t> data;
0276       if (packetisedData_) {
0277         const PacketData& p = rxPacketReader_.get(iBoard);
0278         PacketData::const_iterator itr = p.begin();
0279         for (unsigned i = 0; i < rxIndex_.at(iBoard); i++)
0280           itr++;
0281 
0282         LogDebug("L1T") << "Found packet [" << itr->first_ << ", " << itr->last_ << "]";
0283         LogDebug("L1T") << "Link " << link << " has " << itr->links_.find(link)->second.size() << " frames";
0284 
0285         for (unsigned iFrame = 0; iFrame < itr->links_.find(link)->second.size(); ++iFrame) {
0286           uint64_t d = itr->links_.find(link)->second.at(iFrame);
0287           data.push_back(d);
0288         }
0289       } else {
0290         for (unsigned iFrame = rxIndex_.at(iBoard); iFrame < rxIndex_.at(iBoard) + size; ++iFrame) {
0291           uint64_t d = rxFileReader_.get(iBoard).link(link).at(iFrame);
0292           LogDebug("L1T") << "Frame " << iFrame << " : " << std::hex << d;
0293           if ((d & 0x100000000) > 0)
0294             data.push_back(d & 0xffffffff);
0295         }
0296       }
0297 
0298       LogDebug("L1T") << "Board " << iBoard << " block " << id << ", size " << data.size();
0299 
0300       Block block(id, data);
0301       blocks.push_back(block);
0302     }
0303 
0304     // then Tx blocks
0305     for (unsigned link = 0; link < txBlockLength_.at(iBoard).size(); ++link) {
0306       unsigned id = (link * 2) + 1;
0307       unsigned size = txBlockLength_.at(iBoard).at(link);
0308 
0309       if (size == 0)
0310         continue;
0311 
0312       LogDebug("L1T") << "Block " << id << " expecting size " << size;
0313 
0314       std::vector<uint32_t> data;
0315       if (packetisedData_) {
0316         const PacketData& p = txPacketReader_.get(iBoard);
0317         PacketData::const_iterator itr = p.begin();
0318         for (unsigned i = 0; i < txIndex_.at(iBoard); i++)
0319           itr++;
0320 
0321         LogDebug("L1T") << "Found packet [" << itr->first_ << ", " << itr->last_ << "]";
0322         LogDebug("L1T") << "Link " << link << " has " << itr->links_.find(link)->second.size() << " frames";
0323 
0324         for (unsigned iFrame = 0; iFrame < itr->links_.find(link)->second.size(); ++iFrame) {
0325           uint64_t d = itr->links_.find(link)->second.at(iFrame);
0326           data.push_back(d);
0327         }
0328 
0329       } else {
0330         for (unsigned iFrame = txIndex_.at(iBoard); iFrame < txIndex_.at(iBoard) + size; ++iFrame) {
0331           uint64_t d = txFileReader_.get(iBoard).link(link).at(iFrame);
0332           LogDebug("L1T") << "Frame " << iFrame << " : " << std::hex << d;
0333           if ((d & 0x100000000) > 0)
0334             data.push_back(d & 0xffffffff);
0335         }
0336       }
0337 
0338       LogDebug("L1T") << "Board " << iBoard << " block " << id << ", size " << data.size();
0339 
0340       Block block(id, data);
0341 
0342       blocks.push_back(block);
0343     }
0344 
0345     if (packetisedData_) {
0346       rxIndex_.at(iBoard)++;
0347       txIndex_.at(iBoard)++;
0348     } else {
0349       rxIndex_.at(iBoard) += nFramesPerEvent_;
0350       txIndex_.at(iBoard) += nFramesPerEvent_;
0351     }
0352 
0353     LogDebug("L1T") << "Board " << iBoard << ", read " << blocks.size() << " blocks";
0354 
0355     return blocks;
0356   }
0357 
0358   void MP7BufferDumpToRaw::formatAMC(amc13::Packet& amc13, const std::vector<Block>& blocks, int iBoard) {
0359     LogDebug("L1T") << "Formatting Board " << iBoard;
0360 
0361     std::vector<uint32_t> load32;
0362     // TODO this is an empty word to be replaced with a proper MP7
0363     // header containing at least the firmware version
0364     load32.push_back(0);
0365     load32.push_back(fwVer_);
0366 
0367     for (const auto& block : blocks) {
0368       LogDebug("L1T") << "Adding block " << block.header().getID() << " with size " << block.payload().size();
0369       auto load = block.payload();
0370 
0371 #ifdef EDM_ML_DEBUG
0372       std::stringstream s("");
0373       s << "Block content:" << std::endl << std::hex << std::setfill('0');
0374       for (const auto& word : load)
0375         s << std::setw(8) << word << std::endl;
0376       LogDebug("L1T") << s.str();
0377 #endif
0378 
0379       load32.push_back(block.header().raw());
0380       load32.insert(load32.end(), load.begin(), load.end());
0381     }
0382 
0383     LogDebug("L1T") << "Converting payload " << iBoard;
0384 
0385     std::vector<uint64_t> load64;
0386     for (unsigned int i = 0; i < load32.size(); i += 2) {
0387       uint64_t word = load32[i];
0388       if (i + 1 < load32.size())
0389         word |= static_cast<uint64_t>(load32[i + 1]) << 32;
0390       load64.push_back(word);
0391     }
0392 
0393     LogDebug("L1T") << "Creating AMC packet " << iBoard;
0394     //  LogDebug("L1T") << iBoard << ", " << boardId_.at(iBoard) << ", " << load64.size();
0395 
0396     amc13.add(iBoard, boardId_.at(iBoard), 0, 0, 0, load64);
0397   }
0398 
0399   void MP7BufferDumpToRaw::formatRaw(edm::Event& iEvent, amc13::Packet& amc13, FEDRawData& fed_data) {
0400     unsigned int size = slinkHeaderSize_ + slinkTrailerSize_ + amc13.size() * 8;
0401     fed_data.resize(size);
0402     unsigned char* payload = fed_data.data();
0403     unsigned char* payload_start = payload;
0404 
0405     auto bxId = iEvent.bunchCrossing();
0406     auto evtId = iEvent.id().event();
0407 
0408     LogDebug("L1T") << "Creating FEDRawData ID " << fedId_ << ", size " << size;
0409 
0410     FEDHeader header(payload);
0411     header.set(payload, evType_, evtId, bxId, fedId_);
0412 
0413     amc13.write(iEvent, payload, slinkHeaderSize_, size - slinkHeaderSize_ - slinkTrailerSize_);
0414 
0415     payload += slinkHeaderSize_;
0416     payload += amc13.size() * 8;
0417 
0418     FEDTrailer trailer(payload);
0419     trailer.set(payload, size / 8, evf::compute_crc(payload_start, size), 0, 0);
0420   }
0421 
0422   // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
0423   void MP7BufferDumpToRaw::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0424     //The following says we do not know what parameters are allowed so do no validation
0425     // Please change this to state exactly what you do use, even if it is no parameters
0426     edm::ParameterSetDescription desc;
0427     desc.setUnknown();
0428     descriptions.addDefault(desc);
0429   }
0430 
0431 }  // namespace l1t
0432 
0433 using namespace l1t;
0434 //define this as a plug-in
0435 DEFINE_FWK_MODULE(MP7BufferDumpToRaw);