Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include "EventFilter/RPCRawToDigi/plugins/RPCCPPFUnpacker.h"
0002 
0003 #include <algorithm>
0004 
0005 #include "DataFormats/Common/interface/Handle.h"
0006 #include "FWCore/Framework/interface/Event.h"
0007 #include "FWCore/Framework/interface/EventSetup.h"
0008 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0009 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0010 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0011 
0012 #include "DataFormats/RPCDigi/interface/RPCAMCLinkCounters.h"
0013 #include "DataFormats/RPCDigi/interface/RPCDigiCollection.h"
0014 #include "EventFilter/RPCRawToDigi/interface/RPCAMCLinkEvents.h"
0015 #include "EventFilter/RPCRawToDigi/interface/RPCMP7Record.h"
0016 
0017 RPCCPPFUnpacker::RPCCPPFUnpacker(edm::ParameterSet const& config,
0018                                  edm::ConsumesCollector consumesCollector,
0019                                  edm::ProducesCollector producesCollector)
0020     : RPCAMCUnpacker(config, consumesCollector, producesCollector),
0021       fill_counters_(config.getParameter<bool>("fillAMCCounters")),
0022       bx_min_(config.getParameter<int>("bxMin")),
0023       bx_max_(config.getParameter<int>("bxMax")),
0024       es_cppf_link_map_br_token_(
0025           consumesCollector.esConsumes<RPCAMCLinkMap, RPCCPPFLinkMapRcd, edm::Transition::BeginRun>()),
0026       es_cppf_link_map_token_(consumesCollector.esConsumes<RPCAMCLinkMap, RPCCPPFLinkMapRcd>()),
0027       es_lb_link_map_token_(consumesCollector.esConsumes<RPCLBLinkMap, RPCLBLinkMapRcd>()) {
0028   producesCollector.produces<RPCDigiCollection>();
0029   producesCollector.produces<l1t::CPPFDigiCollection>();
0030   if (fill_counters_) {
0031     producesCollector.produces<RPCAMCLinkCounters>("RPCAMCUnpacker");
0032   }
0033 }
0034 
0035 void RPCCPPFUnpacker::beginRun(edm::Run const& run, edm::EventSetup const& setup) {
0036   if (es_cppf_link_map_watcher_.check(setup)) {
0037     auto link_map = setup.getHandle(es_cppf_link_map_br_token_);
0038     std::set<int> feds;
0039     for (auto const& cppf_link : link_map->getMap()) {
0040       feds.insert(cppf_link.first.getFED());
0041     }
0042     feds_.assign(feds.begin(), feds.end());
0043   }
0044 }
0045 
0046 void RPCCPPFUnpacker::produce(edm::Event& event,
0047                               edm::EventSetup const& setup,
0048                               std::map<RPCAMCLink, rpcamc13::AMCPayload> const& amc_payload) {
0049   // Get EventSetup Electronics Maps
0050   es_cppf_link_map_ = setup.getHandle(es_cppf_link_map_token_);
0051   es_lb_link_map_ = setup.getHandle(es_lb_link_map_token_);
0052 
0053   std::set<std::pair<RPCDetId, RPCDigi> > rpc_digis;
0054   std::unique_ptr<l1t::CPPFDigiCollection> rpc_cppf_digis(new l1t::CPPFDigiCollection());
0055   std::unique_ptr<RPCAMCLinkCounters> counters(new RPCAMCLinkCounters());
0056 
0057   for (std::pair<RPCAMCLink const, rpcamc13::AMCPayload> const& payload : amc_payload) {
0058     processCPPF(payload.first, payload.second, *counters, rpc_digis, *rpc_cppf_digis);
0059   }
0060 
0061   putRPCDigis(event, rpc_digis);
0062   std::sort(rpc_cppf_digis->begin(), rpc_cppf_digis->end());
0063   event.put(std::move(rpc_cppf_digis));
0064   if (fill_counters_) {
0065     event.put(std::move(counters), "RPCAMCUnpacker");
0066   }
0067 }
0068 
0069 bool RPCCPPFUnpacker::processCPPF(RPCAMCLink const& link,
0070                                   rpcamc13::AMCPayload const& payload,
0071                                   RPCAMCLinkCounters& counters,
0072                                   std::set<std::pair<RPCDetId, RPCDigi> >& rpc_digis,
0073                                   l1t::CPPFDigiCollection& rpc_cppf_digis) const {
0074   LogDebug("RPCCPPFRawToDigi") << "RPCCPPF " << link << ", size " << payload.getData().size();
0075 
0076   if (!payload.isValid()) {
0077     return false;
0078   }
0079 
0080   std::vector<std::uint64_t>::const_iterator word(payload.getData().begin());
0081   rpcmp7::Header header(&(*word));
0082   ++word;
0083   ++word;
0084   rpcmp7::SubHeader subheader(*word);
0085   ++word;
0086   std::vector<std::uint64_t>::const_iterator word_end(payload.getData().end());
0087   --word_end;  // rpcamc::Trailer
0088   unsigned int bx_counter(header.getBXCounter());
0089   unsigned int bx_counter_mod(bx_counter % 27);
0090 
0091   int bx_min(bx_min_), bx_max(bx_max_);
0092   // no adjustable bx window implemented in readout yet
0093 
0094   unsigned int pos(0), length(0);
0095   unsigned int caption_id(0);
0096   bool zs_per_bx(false), have_bx_header(false);
0097   unsigned int bx_words(0);
0098   unsigned int block_id(0);
0099   std::uint32_t records[2];
0100   for (; word != word_end; ++word) {
0101     records[0] = *word & 0xffffffff;
0102     records[1] = (*word >> 32) & 0xffffffff;
0103     for (std::uint32_t* record = &(records[0]); record < &(records[2]); ++record) {
0104       if (pos >= length) {
0105         rpcmp7::BlockHeader block_header(*record);
0106         LogDebug("RPCCPPFUnpacker") << "block header " << std::hex << block_header.getRecord() << std::dec
0107                                     << " caption id " << block_header.getCaptionId() << " zs per bx "
0108                                     << block_header.hasZeroSuppressionPerBX() << " zs "
0109                                     << block_header.isZeroSuppressed() << " length " << block_header.getLength() << " "
0110                                     << word_end - word;
0111         pos = 0;
0112         length = block_header.getLength();
0113         caption_id = block_header.getCaptionId();
0114         zs_per_bx = block_header.hasZeroSuppressionPerBX();
0115         have_bx_header = false;
0116         bx_words = 6;
0117         block_id = block_header.getId();
0118       } else if (zs_per_bx && !have_bx_header) {
0119         // rpcmp7::BXHeader bx_header(*record);
0120         have_bx_header = true;
0121       } else {
0122         if (caption_id == 0x01) {  // RX
0123           processRXRecord(link, bx_counter_mod, rpccppf::RXRecord(*record), counters, rpc_digis, bx_min, bx_max);
0124         } else if (caption_id == 0x02) {  // TX
0125           processTXRecord(link, block_id, 6 - bx_words, rpccppf::TXRecord(*record), rpc_cppf_digis);
0126         }
0127         ++pos;
0128         --bx_words;
0129       }
0130     }
0131   }
0132 
0133   return true;
0134 }
0135 
0136 void RPCCPPFUnpacker::processRXRecord(RPCAMCLink link,
0137                                       unsigned int bx_counter_mod,
0138                                       rpccppf::RXRecord const& record,
0139                                       RPCAMCLinkCounters& counters,
0140                                       std::set<std::pair<RPCDetId, RPCDigi> >& rpc_digis,
0141                                       int bx_min,
0142                                       int bx_max) const {
0143   LogDebug("RPCCPPFRawToDigi") << "RXRecord " << std::hex << record.getRecord() << std::dec << std::endl;
0144   unsigned int fed(link.getFED());
0145   unsigned int amc_number(link.getAMCNumber());
0146   link.setAMCInput(record.getLink());
0147 
0148   int bx_offset = (int)(record.getBXCounterMod() + 31 - bx_counter_mod) % 27 - 4;
0149 
0150   if (record.isError()) {
0151     if (fill_counters_ && bx_offset == 0) {
0152       counters.add(RPCAMCLinkEvents::input_link_error_, link);
0153     }
0154     LogDebug("RPCCPPFRawToDigi") << "Link in error for " << link;
0155     return;
0156   } else if (!record.isAcknowledge()) {
0157     if (fill_counters_ && bx_offset == 0) {
0158       counters.add(RPCAMCLinkEvents::input_link_ack_fail_, link);
0159     }
0160     LogDebug("RPCCPPFRawToDigi") << "Link without acknowledge for " << link;
0161     return;
0162   }
0163 
0164   std::uint8_t data(record.getPartitionData());
0165   if (!data) {
0166     return;
0167   }
0168 
0169   int bx(bx_offset - (int)(record.getDelay()));
0170   LogDebug("RPCCPPFRawToDigi") << "RPC BX " << bx << " for offset " << bx_offset;
0171 
0172   if (fill_counters_ && bx == 0 && record.isEOD()) {  // EOD comes at the last delay
0173     counters.add(RPCAMCLinkEvents::input_eod_, link);
0174   }
0175 
0176   RPCAMCLinkMap::map_type::const_iterator link_it = es_cppf_link_map_->getMap().find(link);
0177   if (link_it == es_cppf_link_map_->getMap().end()) {
0178     if (fill_counters_ && bx_offset == 0) {
0179       counters.add(RPCAMCLinkEvents::amc_link_invalid_, RPCAMCLink(fed, amc_number));
0180     }
0181     LogDebug("RPCCPPFRawToDigi") << "Skipping unknown RPCCPPFLink " << link;
0182     return;
0183   }
0184 
0185   RPCLBLink lb_link = link_it->second;
0186 
0187   if (record.getLinkBoard() > (unsigned int)RPCLBLink::max_linkboard_) {
0188     if (fill_counters_ && bx_offset == 0) {
0189       counters.add(RPCAMCLinkEvents::input_lb_invalid_, link);
0190     }
0191     LogDebug("RPCCPPFRawToDigi") << "Skipping invalid LinkBoard " << record.getLinkBoard() << " for record " << link
0192                                  << " (" << std::hex << record.getRecord() << " in " << record.getRecord() << std::dec
0193                                  << " from " << link;
0194     return;
0195   }
0196 
0197   if (record.getConnector() > (unsigned int)RPCLBLink::max_connector_) {
0198     if (fill_counters_ && bx_offset == 0) {
0199       counters.add(RPCAMCLinkEvents::input_connector_invalid_, link);
0200     }
0201     LogDebug("RPCCPPFRawToDigi") << "Skipping invalid Connector " << record.getConnector() << " for record " << link
0202                                  << " (" << std::hex << record.getRecord() << " in " << record.getRecord() << std::dec
0203                                  << ") from " << link;
0204     return;
0205   }
0206 
0207   lb_link.setLinkBoard(record.getLinkBoard());
0208   lb_link.setConnector(record.getConnector());
0209 
0210   RPCLBLinkMap::map_type::const_iterator lb_link_it = es_lb_link_map_->getMap().find(lb_link);
0211   if (lb_link_it == es_lb_link_map_->getMap().end()) {
0212     if (fill_counters_ && bx_offset == 0) {
0213       counters.add(RPCAMCLinkEvents::input_connector_not_used_, link);
0214     }
0215     LogDebug("RPCCPPFRawToDigi") << "Could not find " << lb_link << " for record " << link << " (" << std::hex
0216                                  << record.getRecord() << " in " << record.getRecord() << std::dec << ") from " << link;
0217     return;
0218   }
0219 
0220   if (bx < bx_min || bx > bx_max) {
0221     return;
0222   }
0223 
0224   if (fill_counters_ && bx == 0) {
0225     counters.add(RPCAMCLinkEvents::input_event_, link);
0226   }
0227 
0228   RPCFebConnector const& feb_connector(lb_link_it->second);
0229   RPCDetId det_id(feb_connector.getRPCDetId());
0230   unsigned int channel_offset(record.getPartition() ? 9 : 1);  // 1-16
0231 
0232   for (unsigned int channel = 0; channel < 8; ++channel) {
0233     if (data & (0x1 << channel)) {
0234       unsigned int strip(feb_connector.getStrip(channel + channel_offset));
0235       if (strip) {
0236         rpc_digis.insert(std::pair<RPCDetId, RPCDigi>(det_id, RPCDigi(strip, bx)));
0237         LogDebug("RPCCPPFRawToDigi") << "RPCDigi " << det_id.rawId() << ", " << strip << ", " << bx;
0238       }
0239     }
0240   }
0241 }
0242 
0243 void RPCCPPFUnpacker::processTXRecord(RPCAMCLink link,
0244                                       unsigned int block,
0245                                       unsigned int word,
0246                                       rpccppf::TXRecord const& record,
0247                                       l1t::CPPFDigiCollection& rpc_cppf_digis) const {
0248   if (!record.isValid(0) && !record.isValid(1)) {
0249     return;
0250   }
0251 
0252   // This translation should become part of a CondFormat
0253   if (word > 5) {
0254     return;
0255   }
0256   static int const ring[6] = {2, 2, 2, 3, 2, 3};
0257   static int const station[6] = {1, 2, 3, 3, 4, 4};
0258   int region(link.getAMCNumber() < 7 ? 1 : -1);
0259   unsigned int endcap_sector((35 + (link.getAMCNumber() - (region > 0 ? 3 : 7)) * 9 + (block >> 1)) % 36 + 1);
0260   RPCDetId rpc_id(region,
0261                   ring[word]  // ring
0262                   ,
0263                   station[word]  // station
0264                   ,
0265                   (endcap_sector / 6) + 1  // sector
0266                   ,
0267                   1  // layer
0268                   ,
0269                   (endcap_sector - 1) % 6 + 1  // subsector
0270                   ,
0271                   0);  // roll
0272 
0273   if (record.isValid(0)) {
0274     rpc_cppf_digis.push_back(l1t::CPPFDigi(rpc_id, 0, record.getTheta(0), record.getPhi(0)));
0275   }
0276   if (record.isValid(1)) {
0277     rpc_cppf_digis.push_back(l1t::CPPFDigi(rpc_id, 0, record.getTheta(1), record.getPhi(1)));
0278   }
0279 }
0280 
0281 void RPCCPPFUnpacker::putRPCDigis(edm::Event& event, std::set<std::pair<RPCDetId, RPCDigi> > const& rpc_digis) const {
0282   std::unique_ptr<RPCDigiCollection> rpc_digi_collection(new RPCDigiCollection());
0283   RPCDetId rpc_det_id;
0284   std::vector<RPCDigi> local_rpc_digis;
0285   for (std::pair<RPCDetId, RPCDigi> const& rpc_digi : rpc_digis) {
0286     LogDebug("RPCCPPFRawToDigi") << "RPCDigi " << rpc_digi.first.rawId() << ", " << rpc_digi.second.strip() << ", "
0287                                  << rpc_digi.second.bx();
0288     if (rpc_digi.first != rpc_det_id) {
0289       if (!local_rpc_digis.empty()) {
0290         rpc_digi_collection->put(RPCDigiCollection::Range(local_rpc_digis.begin(), local_rpc_digis.end()), rpc_det_id);
0291         local_rpc_digis.clear();
0292       }
0293       rpc_det_id = rpc_digi.first;
0294     }
0295     local_rpc_digis.push_back(rpc_digi.second);
0296   }
0297   if (!local_rpc_digis.empty()) {
0298     rpc_digi_collection->put(RPCDigiCollection::Range(local_rpc_digis.begin(), local_rpc_digis.end()), rpc_det_id);
0299   }
0300 
0301   event.put(std::move(rpc_digi_collection));
0302 }
0303 
0304 #include "FWCore/Framework/interface/MakerMacros.h"
0305 #include "EventFilter/RPCRawToDigi/plugins/RPCAMCUnpackerFactory.h"
0306 DEFINE_EDM_PLUGIN(RPCAMCUnpackerFactory, RPCCPPFUnpacker, "RPCCPPFUnpacker");