CMS 3D CMS Logo

RPCCPPFUnpacker.cc
Go to the documentation of this file.
2 
3 #include <algorithm>
4 
11 
17 
19  : RPCAMCUnpacker(config, producesCollector),
20  fill_counters_(config.getParameter<bool>("fillAMCCounters")),
21  bx_min_(config.getParameter<int>("bxMin")),
22  bx_max_(config.getParameter<int>("bxMax")) {
23  producesCollector.produces<RPCDigiCollection>();
24  producesCollector.produces<l1t::CPPFDigiCollection>();
25  if (fill_counters_) {
26  producesCollector.produces<RPCAMCLinkCounters>("RPCAMCUnpacker");
27  }
28 }
29 
31  if (es_cppf_link_map_watcher_.check(setup)) {
33  std::set<int> feds;
34  for (auto const& cppf_link : es_cppf_link_map_->getMap()) {
35  feds.insert(cppf_link.first.getFED());
36  }
37  feds_.assign(feds.begin(), feds.end());
38  }
39 }
40 
42  edm::EventSetup const& setup,
43  std::map<RPCAMCLink, rpcamc13::AMCPayload> const& amc_payload) {
44  // Get EventSetup Electronics Maps
46  setup.get<RPCLBLinkMapRcd>().get(es_lb_link_map_);
47 
48  std::set<std::pair<RPCDetId, RPCDigi> > rpc_digis;
49  std::unique_ptr<l1t::CPPFDigiCollection> rpc_cppf_digis(new l1t::CPPFDigiCollection());
50  std::unique_ptr<RPCAMCLinkCounters> counters(new RPCAMCLinkCounters());
51 
52  for (std::pair<RPCAMCLink const, rpcamc13::AMCPayload> const& payload : amc_payload) {
53  processCPPF(payload.first, payload.second, *counters, rpc_digis, *rpc_cppf_digis);
54  }
55 
56  putRPCDigis(event, rpc_digis);
57  std::sort(rpc_cppf_digis->begin(), rpc_cppf_digis->end());
58  event.put(std::move(rpc_cppf_digis));
59  if (fill_counters_) {
60  event.put(std::move(counters), "RPCAMCUnpacker");
61  }
62 }
63 
66  RPCAMCLinkCounters& counters,
67  std::set<std::pair<RPCDetId, RPCDigi> >& rpc_digis,
68  l1t::CPPFDigiCollection& rpc_cppf_digis) const {
69  LogDebug("RPCCPPFRawToDigi") << "RPCCPPF " << link << ", size " << payload.getData().size();
70 
71  if (!payload.isValid()) {
72  return false;
73  }
74 
75  std::vector<std::uint64_t>::const_iterator word(payload.getData().begin());
77  ++word;
78  ++word;
79  rpcmp7::SubHeader subheader(*word);
80  ++word;
81  std::vector<std::uint64_t>::const_iterator word_end(payload.getData().end());
82  --word_end; // rpcamc::Trailer
83  unsigned int bx_counter(header.getBXCounter());
84  unsigned int bx_counter_mod(bx_counter % 27);
85 
86  int bx_min(bx_min_), bx_max(bx_max_);
87  // no adjustable bx window implemented in readout yet
88 
89  unsigned int pos(0), length(0);
90  unsigned int caption_id(0);
91  bool zs_per_bx(false), have_bx_header(false);
92  unsigned int bx_words(0);
93  unsigned int block_id(0);
94  std::uint32_t records[2];
95  for (; word != word_end; ++word) {
96  records[0] = *word & 0xffffffff;
97  records[1] = (*word >> 32) & 0xffffffff;
98  for (std::uint32_t* record = &(records[0]); record < &(records[2]); ++record) {
99  if (pos >= length) {
100  rpcmp7::BlockHeader block_header(*record);
101  LogDebug("RPCCPPFUnpacker") << "block header " << std::hex << block_header.getRecord() << std::dec
102  << " caption id " << block_header.getCaptionId() << " zs per bx "
103  << block_header.hasZeroSuppressionPerBX() << " zs "
104  << block_header.isZeroSuppressed() << " length " << block_header.getLength() << " "
105  << word_end - word;
106  pos = 0;
107  length = block_header.getLength();
108  caption_id = block_header.getCaptionId();
109  zs_per_bx = block_header.hasZeroSuppressionPerBX();
110  have_bx_header = false;
111  bx_words = 6;
112  block_id = block_header.getId();
113  } else if (zs_per_bx && !have_bx_header) {
114  // rpcmp7::BXHeader bx_header(*record);
115  have_bx_header = true;
116  } else {
117  if (caption_id == 0x01) { // RX
118  processRXRecord(link, bx_counter_mod, rpccppf::RXRecord(*record), counters, rpc_digis, bx_min, bx_max);
119  } else if (caption_id == 0x02) { // TX
120  processTXRecord(link, block_id, 6 - bx_words, rpccppf::TXRecord(*record), rpc_cppf_digis);
121  }
122  ++pos;
123  --bx_words;
124  }
125  }
126  }
127 
128  return true;
129 }
130 
132  unsigned int bx_counter_mod,
133  rpccppf::RXRecord const& record,
134  RPCAMCLinkCounters& counters,
135  std::set<std::pair<RPCDetId, RPCDigi> >& rpc_digis,
136  int bx_min,
137  int bx_max) const {
138  LogDebug("RPCCPPFRawToDigi") << "RXRecord " << std::hex << record.getRecord() << std::dec << std::endl;
139  unsigned int fed(link.getFED());
140  unsigned int amc_number(link.getAMCNumber());
141  link.setAMCInput(record.getLink());
142 
143  int bx_offset = (int)(record.getBXCounterMod() + 31 - bx_counter_mod) % 27 - 4;
144 
145  if (record.isError()) {
146  if (fill_counters_ && bx_offset == 0) {
148  }
149  LogDebug("RPCCPPFRawToDigi") << "Link in error for " << link;
150  return;
151  } else if (!record.isAcknowledge()) {
152  if (fill_counters_ && bx_offset == 0) {
154  }
155  LogDebug("RPCCPPFRawToDigi") << "Link without acknowledge for " << link;
156  return;
157  }
158 
159  std::uint8_t data(record.getPartitionData());
160  if (!data) {
161  return;
162  }
163 
164  int bx(bx_offset - (int)(record.getDelay()));
165  LogDebug("RPCCPPFRawToDigi") << "RPC BX " << bx << " for offset " << bx_offset;
166 
167  if (fill_counters_ && bx == 0 && record.isEOD()) { // EOD comes at the last delay
168  counters.add(RPCAMCLinkEvents::input_eod_, link);
169  }
170 
171  RPCAMCLinkMap::map_type::const_iterator link_it = es_cppf_link_map_->getMap().find(link);
172  if (link_it == es_cppf_link_map_->getMap().end()) {
173  if (fill_counters_ && bx_offset == 0) {
174  counters.add(RPCAMCLinkEvents::amc_link_invalid_, RPCAMCLink(fed, amc_number));
175  }
176  LogDebug("RPCCPPFRawToDigi") << "Skipping unknown RPCCPPFLink " << link;
177  return;
178  }
179 
180  RPCLBLink lb_link = link_it->second;
181 
182  if (record.getLinkBoard() > (unsigned int)RPCLBLink::max_linkboard_) {
183  if (fill_counters_ && bx_offset == 0) {
185  }
186  LogDebug("RPCCPPFRawToDigi") << "Skipping invalid LinkBoard " << record.getLinkBoard() << " for record " << link
187  << " (" << std::hex << record.getRecord() << " in " << record.getRecord() << std::dec
188  << " from " << link;
189  return;
190  }
191 
192  if (record.getConnector() > (unsigned int)RPCLBLink::max_connector_) {
193  if (fill_counters_ && bx_offset == 0) {
195  }
196  LogDebug("RPCCPPFRawToDigi") << "Skipping invalid Connector " << record.getConnector() << " for record " << link
197  << " (" << std::hex << record.getRecord() << " in " << record.getRecord() << std::dec
198  << ") from " << link;
199  return;
200  }
201 
202  lb_link.setLinkBoard(record.getLinkBoard());
203  lb_link.setConnector(record.getConnector());
204 
205  RPCLBLinkMap::map_type::const_iterator lb_link_it = es_lb_link_map_->getMap().find(lb_link);
206  if (lb_link_it == es_lb_link_map_->getMap().end()) {
207  if (fill_counters_ && bx_offset == 0) {
209  }
210  LogDebug("RPCCPPFRawToDigi") << "Could not find " << lb_link << " for record " << link << " (" << std::hex
211  << record.getRecord() << " in " << record.getRecord() << std::dec << ") from " << link;
212  return;
213  }
214 
215  if (bx < bx_min || bx > bx_max) {
216  return;
217  }
218 
219  if (fill_counters_ && bx == 0) {
220  counters.add(RPCAMCLinkEvents::input_event_, link);
221  }
222 
223  RPCFebConnector const& feb_connector(lb_link_it->second);
224  RPCDetId det_id(feb_connector.getRPCDetId());
225  unsigned int channel_offset(record.getPartition() ? 9 : 1); // 1-16
226 
227  for (unsigned int channel = 0; channel < 8; ++channel) {
228  if (data & (0x1 << channel)) {
229  unsigned int strip(feb_connector.getStrip(channel + channel_offset));
230  if (strip) {
231  rpc_digis.insert(std::pair<RPCDetId, RPCDigi>(det_id, RPCDigi(strip, bx)));
232  LogDebug("RPCCPPFRawToDigi") << "RPCDigi " << det_id.rawId() << ", " << strip << ", " << bx;
233  }
234  }
235  }
236 }
237 
239  unsigned int block,
240  unsigned int word,
241  rpccppf::TXRecord const& record,
242  l1t::CPPFDigiCollection& rpc_cppf_digis) const {
243  if (!record.isValid(0) && !record.isValid(1)) {
244  return;
245  }
246 
247  // This translation should become part of a CondFormat
248  if (word > 5) {
249  return;
250  }
251  static int const ring[6] = {2, 2, 2, 3, 2, 3};
252  static int const station[6] = {1, 2, 3, 3, 4, 4};
253  int region(link.getAMCNumber() < 7 ? 1 : -1);
254  unsigned int endcap_sector((35 + (link.getAMCNumber() - (region > 0 ? 3 : 7)) * 9 + (block >> 1)) % 36 + 1);
255  RPCDetId rpc_id(region,
256  ring[word] // ring
257  ,
258  station[word] // station
259  ,
260  (endcap_sector / 6) + 1 // sector
261  ,
262  1 // layer
263  ,
264  (endcap_sector - 1) % 6 + 1 // subsector
265  ,
266  0); // roll
267 
268  if (record.isValid(0)) {
269  rpc_cppf_digis.push_back(l1t::CPPFDigi(rpc_id, 0, record.getTheta(0), record.getPhi(0)));
270  }
271  if (record.isValid(1)) {
272  rpc_cppf_digis.push_back(l1t::CPPFDigi(rpc_id, 0, record.getTheta(1), record.getPhi(1)));
273  }
274 }
275 
276 void RPCCPPFUnpacker::putRPCDigis(edm::Event& event, std::set<std::pair<RPCDetId, RPCDigi> > const& rpc_digis) const {
277  std::unique_ptr<RPCDigiCollection> rpc_digi_collection(new RPCDigiCollection());
278  RPCDetId rpc_det_id;
279  std::vector<RPCDigi> local_rpc_digis;
280  for (std::pair<RPCDetId, RPCDigi> const& rpc_digi : rpc_digis) {
281  LogDebug("RPCCPPFRawToDigi") << "RPCDigi " << rpc_digi.first.rawId() << ", " << rpc_digi.second.strip() << ", "
282  << rpc_digi.second.bx();
283  if (rpc_digi.first != rpc_det_id) {
284  if (!local_rpc_digis.empty()) {
285  rpc_digi_collection->put(RPCDigiCollection::Range(local_rpc_digis.begin(), local_rpc_digis.end()), rpc_det_id);
286  local_rpc_digis.clear();
287  }
288  rpc_det_id = rpc_digi.first;
289  }
290  local_rpc_digis.push_back(rpc_digi.second);
291  }
292  if (!local_rpc_digis.empty()) {
293  rpc_digi_collection->put(RPCDigiCollection::Range(local_rpc_digis.begin(), local_rpc_digis.end()), rpc_det_id);
294  }
295 
296  event.put(std::move(rpc_digi_collection));
297 }
298 
#define LogDebug(id)
void produce(edm::Event &event, edm::EventSetup const &setup, std::map< RPCAMCLink, rpcamc13::AMCPayload > const &amc_payload) override
unsigned int getLength() const
bool isZeroSuppressed() const
unsigned int getDelay() const
unsigned int getStrip(unsigned int channel) const
edm::ESHandle< RPCLBLinkMap > es_lb_link_map_
std::uint8_t getPartitionData() const
static unsigned int const input_connector_not_used_
std::vector< int > feds_
bool isError() const
bool isValid(unsigned int index) const
std::uint32_t const & getRecord() const
edm::ESHandle< RPCAMCLinkMap > es_cppf_link_map_
ProductRegistryHelper::BranchAliasSetterT< ProductType > produces()
JetCorrectorParameters::Record record
Definition: classes.h:7
unsigned int getBXCounterMod() const
bool processCPPF(RPCAMCLink const &link, rpcamc13::AMCPayload const &payload, RPCAMCLinkCounters &counters, std::set< std::pair< RPCDetId, RPCDigi > > &rpc_digis, l1t::CPPFDigiCollection &rpc_cppf_digis) const
static unsigned int const input_link_error_
std::uint32_t const & getRecord() const
static unsigned int const input_lb_invalid_
unsigned int getPartition() const
Definition: config.py:1
std::vector< std::uint64_t > const & getData() const
void beginRun(edm::Run const &run, edm::EventSetup const &setup) override
static unsigned int const amc_link_invalid_
void processRXRecord(RPCAMCLink link, unsigned int bx_counter_mod, rpccppf::RXRecord const &record, RPCAMCLinkCounters &counters, std::set< std::pair< RPCDetId, RPCDigi > > &rpc_digis, int bx_min, int bx_max) const
uint64_t word
unsigned int getConnector() const
bool isValid() const
MuonDigiCollection< RPCDetId, RPCDigi > RPCDigiCollection
map_type & getMap()
Definition: RPCAMCLinkMap.h:27
unsigned int getPhi(unsigned int index) const
bool isEOD() const
unsigned int getTheta(unsigned int index) const
map_type & getMap()
Definition: RPCLBLinkMap.h:27
edm::ESWatcher< RPCCPPFLinkMapRcd > es_cppf_link_map_watcher_
unsigned int getId() const
static unsigned int const input_event_
void add(unsigned int event, RPCAMCLink const &link, unsigned int count=1)
static unsigned int const input_link_ack_fail_
bool hasZeroSuppressionPerBX() const
void putRPCDigis(edm::Event &event, std::set< std::pair< RPCDetId, RPCDigi > > const &digis) const
unsigned int getCaptionId() const
std::pair< const_iterator, const_iterator > Range
bool check(const edm::EventSetup &iSetup)
Definition: ESWatcher.h:52
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
T get() const
Definition: EventSetup.h:73
RPCCPPFUnpacker(edm::ParameterSet const &, edm::ProducesCollector)
unsigned int getLink() const
#define DEFINE_EDM_PLUGIN(factory, type, name)
unsigned int getLinkBoard() const
bool isAcknowledge() const
std::vector< CPPFDigi > CPPFDigiCollection
Definition: CPPFDigi.h:83
def move(src, dest)
Definition: eostools.py:511
void processTXRecord(RPCAMCLink link, unsigned int block, unsigned int word, rpccppf::TXRecord const &record, l1t::CPPFDigiCollection &rpc_cppf_digis) const
RPCDetId getRPCDetId() const
Definition: event.py:1
Definition: Run.h:45
static unsigned int const input_eod_
static unsigned int const input_connector_invalid_