CMS 3D CMS Logo

CSCTFUnpacker.cc
Go to the documentation of this file.
5 
6 //CSC Track Finder Raw Data Format
8 
9 //FEDRawData
13 
14 #include <vector>
15 #include <string>
16 
17 class CSCTriggerMapping;
18 
20 private:
23 
24  CSCTriggerMapping* mapping; // redundant, but needed
25 
26  CSCTFEvent tfEvent; // TF data container
27 
28  // geometry may not be properly set in CSC TF data
29  // make an artificial assignment of each of 12 SPs (slots 6-11 and 16-21) to 12 sectors (1-12, 0-not assigned)
30  std::vector<int> slot2sector;
31 
32  // label of the module which produced raw data
34 
36 
37 public:
38  void produce(edm::Event& e, const edm::EventSetup& c) override;
39 
41  ~CSCTFUnpacker(void) override;
42 };
43 
44 //Framework stuff
48 
49 //Digi
54 
55 //Digi collections
60 
61 //Unique key
64 
65 //Don't know what
68 
70 //#include <DataFormats/MuonDetId/interface/CSCTriggerNumbering.h>
71 
72 //#include <iostream>
73 #include <sstream>
74 
75 CSCTFUnpacker::CSCTFUnpacker(const edm::ParameterSet& pset) : edm::stream::EDProducer<>(), mapping(nullptr) {
76  LogDebug("CSCTFUnpacker|ctor") << "Started ...";
77 
78  // Edges of the time window, which LCTs are put into (unlike tracks, which are always centred around 0):
79  m_minBX = pset.getParameter<int>("MinBX"); //3
80  m_maxBX = pset.getParameter<int>("MaxBX"); //9
81 
82  // Swap: if(swapME1strips && me1b && !zplus) strip = 65 - strip; // 1-64 -> 64-1 :
83  swapME1strips = pset.getParameter<bool>("swapME1strips");
84 
85  // Initialize slot<->sector assignment
86  slot2sector = pset.getParameter<std::vector<int>>("slot2sector");
87  LogDebug("CSCTFUnpacker|ctor") << "Verifying slot<->sector map from 'vint32 slot2sector'";
88  for (int slot = 0; slot < 22; slot++)
89  if (slot2sector[slot] < 0 || slot2sector[slot] > 12)
90  throw cms::Exception("Invalid configuration") << "CSCTFUnpacker: sector index is set out of range (slot2sector["
91  << slot << "]=" << slot2sector[slot] << ", should be [0-12])";
92  // Just for safety (in case of bad data):
93  slot2sector.resize(32);
94 
95  // As we use standard CSC digi containers, we have to initialize mapping:
96  std::string mappingFile = pset.getParameter<std::string>("mappingFile");
97  if (mappingFile.length()) {
98  LogDebug("CSCTFUnpacker|ctor") << "Define ``mapping'' only if you want to screw up real geometry";
100  } else {
101  LogDebug("CSCTFUnpacker|ctor") << "Generating default hw<->geometry mapping";
102  class M : public CSCTriggerSimpleMapping {
103  void fill(void) override {}
104  };
105  mapping = new M();
106  for (int endcap = 1; endcap <= 2; endcap++)
107  for (int station = 1; station <= 4; station++)
108  for (int sector = 1; sector <= 6; sector++)
109  for (int csc = 1; csc <= 9; csc++) {
110  if (station == 1) {
113  } else
115  }
116  }
117 
118  producer = pset.getParameter<edm::InputTag>("producer");
119 
120  produces<CSCCorrelatedLCTDigiCollection>();
121  produces<L1CSCTrackCollection>();
122  produces<L1CSCStatusDigiCollection>();
123  produces<CSCTriggerContainer<csctf::TrackStub>>("DT");
124 
125  Raw_token = consumes<FEDRawDataCollection>(producer);
126 }
127 
129  if (mapping)
130  delete mapping;
131 }
132 
134  // Get a handle to the FED data collection
136  e.getByToken(Raw_token, rawdata);
137 
138  // create the collection of CSC wire and strip digis as well as of DT stubs, which we receive from DTTF
139  auto LCTProduct = std::make_unique<CSCCorrelatedLCTDigiCollection>();
140  auto trackProduct = std::make_unique<L1CSCTrackCollection>();
141  auto statusProduct = std::make_unique<L1CSCStatusDigiCollection>();
142  auto dtProduct = std::make_unique<CSCTriggerContainer<csctf::TrackStub>>();
143 
145  const FEDRawData& fedData = rawdata->FEDData(fedid);
146  if (fedData.size() == 0)
147  continue;
148  //LogDebug("CSCTFUnpacker|produce");
149  //if( monitor ) monitor->process((unsigned short*)fedData.data());
150  unsigned int unpacking_status = tfEvent.unpack((unsigned short*)fedData.data(), fedData.size() / 2);
151  if (unpacking_status == 0) {
152  // There may be several SPs in event
153  std::vector<const CSCSPEvent*> SPs = tfEvent.SPs_fast();
154  // Cycle over all of them
155  for (std::vector<const CSCSPEvent*>::const_iterator spItr = SPs.begin(); spItr != SPs.end(); spItr++) {
156  const CSCSPEvent* sp = *spItr;
157 
159  status.sp_slot = sp->header().slot();
160  status.l1a_bxn = sp->header().BXN();
161  status.fmm_status = sp->header().status();
162  status.track_cnt = sp->counters().track_counter();
163  status.orbit_cnt = sp->counters().orbit_counter();
164 
165  // Finds central LCT BX
166  // assumes window is odd number of bins
167  int central_lct_bx = (m_maxBX + m_minBX) / 2;
168 
169  // Find central SP BX
170  // assumes window is odd number of bins
171  int central_sp_bx = int(sp->header().nTBINs() / 2);
172 
173  for (unsigned int tbin = 0; tbin < sp->header().nTBINs(); tbin++) {
174  status.se |= sp->record(tbin).SEs();
175  status.sm |= sp->record(tbin).SMs();
176  status.bx |= sp->record(tbin).BXs();
177  status.af |= sp->record(tbin).AFs();
178  status.vp |= sp->record(tbin).VPs();
179 
180  for (unsigned int FPGA = 0; FPGA < 5; FPGA++)
181  for (unsigned int MPClink = 0; MPClink < 3; ++MPClink) {
182  std::vector<CSCSP_MEblock> lct = sp->record(tbin).LCT(FPGA, MPClink);
183  if (lct.empty())
184  continue;
185 
186  status.link_status[lct[0].spInput() - 1] |= (1 << lct[0].receiver_status_frame1()) |
187  (1 << lct[0].receiver_status_frame2()) |
188  ((lct[0].aligment_fifo() ? 1 : 0) << 4);
189  status.mpc_link_id |= (lct[0].link() << 2) | lct[0].mpc();
190 
191  int station = (FPGA ? FPGA : 1);
192  int endcap = 0, sector = 0;
193  if (slot2sector[sp->header().slot()]) {
194  endcap = slot2sector[sp->header().slot()] / 7 + 1;
195  sector = slot2sector[sp->header().slot()];
196  if (sector > 6)
197  sector -= 6;
198  } else {
199  endcap = (sp->header().endcap() ? 1 : 2);
200  sector = sp->header().sector();
201  }
202  int subsector = (FPGA > 1 ? 0 : FPGA + 1);
203  int cscid = lct[0].csc();
204 
205  try {
206  CSCDetId id = mapping->detId(endcap, station, sector, subsector, cscid, 0);
207  // corrlcts now have no layer associated with them
208  LCTProduct->insertDigi(id,
210  lct[0].vp(),
211  lct[0].quality(),
212  lct[0].wireGroup(),
213  (swapME1strips && cscid <= 3 && station == 1 &&
214  endcap == 2 && lct[0].strip() < 65
215  ? 65 - lct[0].strip()
216  : lct[0].strip()),
217  lct[0].pattern(),
218  lct[0].l_r(),
219  (lct[0].tbin() + (central_lct_bx - central_sp_bx)),
220  lct[0].link(),
221  lct[0].BXN(),
222  0,
223  cscid));
224  } catch (cms::Exception& e) {
225  edm::LogInfo("CSCTFUnpacker|produce")
226  << e.what() << "Not adding digi to collection in event " << sp->header().L1A()
227  << " (endcap=" << endcap << ",station=" << station << ",sector=" << sector
228  << ",subsector=" << subsector << ",cscid=" << cscid << ",spSlot=" << sp->header().slot() << ")";
229  }
230  }
231 
232  std::vector<CSCSP_MBblock> mbStubs = sp->record(tbin).mbStubs();
233  for (std::vector<CSCSP_MBblock>::const_iterator iter = mbStubs.begin(); iter != mbStubs.end(); iter++) {
234  int endcap, sector;
235  if (slot2sector[sp->header().slot()]) {
236  endcap = slot2sector[sp->header().slot()] / 7 + 1;
237  sector = slot2sector[sp->header().slot()];
238  if (sector > 6)
239  sector -= 6;
240  } else {
241  endcap = (sp->header().endcap() ? 1 : 2);
242  sector = sp->header().sector();
243  }
244  const unsigned int csc2dt[6][2] = {{2, 3}, {4, 5}, {6, 7}, {8, 9}, {10, 11}, {12, 1}};
245  DTChamberId id((endcap == 1 ? 2 : -2), 1, csc2dt[sector - 1][iter->id() - 1]);
247  iter->vq(),
248  iter->quality(),
249  iter->cal(),
250  iter->flag(),
251  iter->bc0(),
252  iter->phi_bend(),
253  tbin + (central_lct_bx - central_sp_bx),
254  iter->id(),
255  iter->bxn(),
256  iter->timingError(),
257  iter->BXN());
258  csctf::TrackStub dtStub(base, id, iter->phi(), 0);
259  dtProduct->push_back(dtStub);
260  }
261 
262  std::vector<CSCSP_SPblock> tracks = sp->record(tbin).tracks();
263  unsigned int trkNumber = 0;
264  for (std::vector<CSCSP_SPblock>::const_iterator iter = tracks.begin(); iter != tracks.end();
265  iter++, trkNumber++) {
267  if (slot2sector[sp->header().slot()]) {
268  track.first.m_endcap = slot2sector[sp->header().slot()] / 7 + 1;
269  track.first.m_sector = slot2sector[sp->header().slot()];
270  if (track.first.m_sector > 6)
271  track.first.m_sector -= 6;
272  } else {
273  track.first.m_endcap = (sp->header().endcap() ? 1 : 2);
274  track.first.m_sector = sp->header().sector();
275  }
276 
277  track.first.m_lphi = iter->phi();
278  track.first.m_ptAddress = iter->ptLUTaddress();
279  track.first.m_fr = iter->f_r();
280  track.first.m_ptAddress |= (iter->f_r() << 21);
281 
282  track.first.setStationIds(iter->ME1_id(), iter->ME2_id(), iter->ME3_id(), iter->ME4_id(), iter->MB_id());
283  track.first.setTbins(
284  iter->ME1_tbin(), iter->ME2_tbin(), iter->ME3_tbin(), iter->ME4_tbin(), iter->MB_tbin());
285  track.first.setBx(iter->tbin() - central_sp_bx);
286  track.first.setBits(iter->syncErr(), iter->bx0(), iter->bc0());
287 
288  track.first.setLocalPhi(iter->phi());
289  track.first.setEtaPacked(iter->eta());
290  track.first.setChargePacked(iter->charge());
291 
292  track.first.m_output_link = iter->id();
293  if (track.first.m_output_link) {
294  track.first.m_rank =
295  (iter->f_r() ? sp->record(tbin).ptSpy() & 0x7F : (sp->record(tbin).ptSpy() & 0x7F00) >> 8);
296  track.first.setChargeValidPacked(
297  (iter->f_r() ? (sp->record(tbin).ptSpy() & 0x80) >> 8 : (sp->record(tbin).ptSpy() & 0x8000) >> 15));
298  } else {
299  track.first.m_rank = 0;
300  track.first.setChargeValidPacked(0);
301  }
302  track.first.setFineHaloPacked(iter->halo());
303 
304  track.first.m_winner = iter->MS_id() & (1 << trkNumber);
305 
306  std::vector<CSCSP_MEblock> lcts = iter->LCTs();
307  for (std::vector<CSCSP_MEblock>::const_iterator lct = lcts.begin(); lct != lcts.end(); lct++) {
308  int station = (lct->spInput() > 6 ? (lct->spInput() - 1) / 3 : 1);
309  int subsector = (lct->spInput() > 6 ? 0 : (lct->spInput() - 1) / 3 + 1);
310  try {
311  CSCDetId id =
312  mapping->detId(track.first.m_endcap, station, track.first.m_sector, subsector, lct->csc(), 0);
313  track.second.insertDigi(id,
315  lct->vp(),
316  lct->quality(),
317  lct->wireGroup(),
318  (swapME1strips && lct->csc() <= 3 && station == 1 &&
319  track.first.m_endcap == 2 && lct[0].strip() < 65
320  ? 65 - lct[0].strip()
321  : lct[0].strip()),
322  lct->pattern(),
323  lct->l_r(),
324  (lct->tbin() + (central_lct_bx - central_sp_bx)),
325  lct->link(),
326  lct->BXN(),
327  0,
328  lct->csc()));
329  } catch (cms::Exception& e) {
330  edm::LogInfo("CSCTFUnpacker|produce")
331  << e.what() << "Not adding track digi to collection in event" << sp->header().L1A()
332  << " (endcap=" << track.first.m_endcap << ",station=" << station
333  << ",sector=" << track.first.m_sector << ",subsector=" << subsector << ",cscid=" << lct->csc()
334  << ",spSlot=" << sp->header().slot() << ")";
335  }
336  }
337 
338  std::vector<CSCSP_MBblock> mbStubs = iter->dtStub();
339  for (std::vector<CSCSP_MBblock>::const_iterator iter = mbStubs.begin(); iter != mbStubs.end(); iter++) {
340  CSCDetId id = mapping->detId(track.first.m_endcap, 1, track.first.m_sector, iter->id(), 1, 0);
341  track.second.insertDigi(id,
342  CSCCorrelatedLCTDigi(iter->phi(),
343  iter->vq(),
344  iter->quality() + 100,
345  iter->cal(),
346  iter->flag(),
347  iter->bc0(),
348  iter->phi_bend(),
349  tbin + (central_lct_bx - central_sp_bx),
350  iter->id(),
351  iter->bxn(),
352  iter->timingError(),
353  iter->BXN()));
354  }
355 
356  trackProduct->push_back(track);
357  }
358  }
359  statusProduct->second.push_back(status);
360  }
361  } else {
362  edm::LogError("CSCTFUnpacker|produce")
363  << " problem of unpacking TF event: 0x" << std::hex << unpacking_status << std::dec << " code";
364  }
365 
366  statusProduct->first = unpacking_status;
367 
368  } //end of fed cycle
369  e.put(std::move(dtProduct), "DT");
370  e.put(std::move(LCTProduct)); // put processed lcts into the event.
371  e.put(std::move(trackProduct));
372  e.put(std::move(statusProduct));
373 }
374 
unsigned int SEs(void) const
Definition: CSCSPRecord.h:84
unsigned int unpack(const unsigned short *buf, unsigned int length)
Definition: CSCTFEvent.cc:6
const CSCSPCounters & counters(void) const
Definition: CSCSPEvent.h:18
CSCTFUnpacker(const edm::ParameterSet &pset)
std::vector< const CSCSPEvent * > SPs_fast(void) const
Definition: CSCTFEvent.h:32
unsigned int BXs(void) const
Definition: CSCSPRecord.h:87
std::pair< csc::L1Track, CSCCorrelatedLCTDigiCollection > L1CSCTrack
base
Main Program
Definition: newFWLiteAna.py:92
unsigned int status(void) const
Definition: CSCSPHeader.h:95
std::vector< CSCSP_SPblock > tracks(void) const
Definition: CSCSPRecord.cc:104
unsigned int AFs(void) const
Definition: CSCSPRecord.h:86
unsigned int L1A(void) const
Definition: CSCSPHeader.h:76
edm::InputTag producer
size_t size() const
Lenght of the data buffer in bytes.
Definition: FEDRawData.h:45
int track_counter(void) const
Definition: CSCSPCounters.h:33
edm::EDGetTokenT< FEDRawDataCollection > Raw_token
Log< level::Error, false > LogError
uint32_t T const *__restrict__ uint32_t const *__restrict__ int32_t int Histo::index_type cudaStream_t stream
void addRecord(int rendcap, int rstation, int rsector, int rsubsector, int rcscid, int cendcap, int cstation, int csector, int csubsector, int ccscid)
string quality
~CSCTFUnpacker(void) override
unsigned int endcap(void) const
Definition: CSCSPHeader.h:87
void produce(edm::Event &e, const edm::EventSetup &c) override
unsigned int BXN(void) const
Definition: CSCSPHeader.h:75
unsigned int nTBINs(void) const
Definition: CSCSPHeader.h:97
int orbit_counter(void) const
Definition: CSCSPCounters.h:34
std::vector< int > slot2sector
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
unsigned int VPs(void) const
Definition: CSCSPRecord.h:83
Definition: L1Track.h:19
unsigned int sector(void) const
Definition: CSCSPHeader.h:81
const FEDRawData & FEDData(int fedid) const
retrieve data for fed
const CSCSPRecord & record(unsigned int tbin) const
Definition: CSCSPEvent.h:21
const CSCSPHeader & header(void) const
Definition: CSCSPEvent.h:17
Log< level::Info, false > LogInfo
CSCTriggerMapping * mapping
std::vector< CSCSP_MBblock > mbStubs(void) const
Definition: CSCSPRecord.cc:115
HLT enums.
const unsigned char * data() const
Return a const pointer to the beginning of the data buffer.
Definition: FEDRawData.cc:24
CSCDetId detId(int endcap, int station, int sector, int subsector, int cscid, int layer=0) const
CSCTFEvent tfEvent
def move(src, dest)
Definition: eostools.py:511
unsigned int slot(void) const
Definition: CSCSPHeader.h:78
std::vector< CSCSP_MEblock > LCT(unsigned int mpc, unsigned int link) const
Definition: CSCSPRecord.cc:96
unsigned int SMs(void) const
Definition: CSCSPRecord.h:85
unsigned int ptSpy(void) const
Definition: CSCSPRecord.h:89
#define LogDebug(id)