CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
CSCDigiToRaw.cc
Go to the documentation of this file.
1 
13 #include "boost/dynamic_bitset.hpp"
14 #include "boost/foreach.hpp"
23 #include <algorithm>
24 
25 using namespace edm;
26 using namespace std;
27 
28 namespace cscd2r
29 {
31 CSCDetId chamberID(const CSCDetId & cscDetId)
32 {
33  CSCDetId chamberId = cscDetId.chamberId();
34  if (chamberId.ring() ==4)
35  {
36  chamberId = CSCDetId(chamberId.endcap(), chamberId.station(), 1, chamberId.chamber(), 0);
37  }
38  return chamberId;
39 }
40 
41 template<typename LCTCollection>
42 bool accept(const CSCDetId & cscId, const LCTCollection & lcts,
43  int bxMin, int bxMax)
44 {
45  if (bxMin == -999) return true;
46  int nominalBX = 6;
47  CSCDetId chamberId = chamberID(cscId);
48  typename LCTCollection::Range lctRange = lcts.get(chamberId);
49  bool result = false;
50  for (typename LCTCollection::const_iterator lctItr = lctRange.first;
51  lctItr != lctRange.second; ++lctItr)
52  {
53  int bx = lctItr->getBX() - nominalBX;
54  if (bx >= bxMin && bx <= bxMax)
55  {
56  result = true;
57  break;
58  }
59  }
60  return result;
61 }
62 
63 // need to specialize for pretriggers, since they don't have a getBX()
64 template<>
65 bool accept(const CSCDetId & cscId, const CSCCLCTPreTriggerCollection & lcts,
66  int bxMin, int bxMax)
67 {
68  if (bxMin == -999) return true;
69  int nominalBX = 6;
70  CSCDetId chamberId = chamberID(cscId);
71  CSCCLCTPreTriggerCollection::Range lctRange = lcts.get(chamberId);
72  bool result = false;
73  for (CSCCLCTPreTriggerCollection::const_iterator lctItr = lctRange.first;
74  lctItr != lctRange.second; ++lctItr)
75  {
76  int bx = *lctItr - nominalBX;
77  if (bx >= bxMin && bx <= bxMax)
78  {
79  result = true;
80  break;
81  }
82  }
83  return result;
84 }
85 
86 }
87 
88 
90  : alctWindowMin_(pset.getParameter<int>("alctWindowMin")),
91  alctWindowMax_(pset.getParameter<int>("alctWindowMax")),
92  clctWindowMin_(pset.getParameter<int>("clctWindowMin")),
93  clctWindowMax_(pset.getParameter<int>("clctWindowMax")),
94  preTriggerWindowMin_(pset.getParameter<int>("preTriggerWindowMin")),
95  preTriggerWindowMax_(pset.getParameter<int>("preTriggerWindowMax")),
96  formatVersion_(2005),
97  usePreTriggers_(true),
98  packEverything_(false)
99 {}
100 
102 {
103  theChamberDataMap.clear();
105 }
106 
107 
109 {
110  CSCDetId chamberId = cscd2r::chamberID(cscDetId);
111  // find the entry into the map
112  map<CSCDetId, CSCEventData>::iterator chamberMapItr = theChamberDataMap.find(chamberId);
113  if (chamberMapItr == theChamberDataMap.end())
114  {
115  // make an entry, telling it the correct chamberType
116  int chamberType = chamberId.iChamberType();
117  chamberMapItr = theChamberDataMap.insert(pair<CSCDetId, CSCEventData>(chamberId, CSCEventData(chamberType, formatVersion_))).first;
118  }
119  CSCEventData & cscData = chamberMapItr->second;
120  cscData.dmbHeader()->setCrateAddress(theElectronicsMap->crate(cscDetId), theElectronicsMap->dmb(cscDetId));
121 
122  if (formatVersion_ == 2013)
123  {
124  // Set DMB version field to distinguish between ME11s and other chambers (ME11 - 2, others - 1)
125  bool me11 = ((chamberId.station()==1) && (chamberId.ring()==4)) || ((chamberId.station()==1) && (chamberId.ring()==1));
126  if (me11)
127  {
128  cscData.dmbHeader()->setdmbVersion(2);
129  }
130  else
131  {
132  cscData.dmbHeader()->setdmbVersion(1);
133  }
134 
135  }
136  return cscData;
137 }
138 
139 
140 
142  const CSCCLCTPreTriggerCollection & preTriggers)
143 { //iterate over chambers with strip digis in them
144  for (CSCStripDigiCollection::DigiRangeIterator j=stripDigis.begin(); j!=stripDigis.end(); ++j)
145  {
146  CSCDetId cscDetId=(*j).first;
147  // only digitize if there are pre-triggers
148 
149  /* !!! Testing. Uncomment for production */
152  {
153  bool me1a = (cscDetId.station()==1) && (cscDetId.ring()==4);
154  bool zplus = (cscDetId.endcap() == 1);
155  bool me1b = (cscDetId.station()==1) && (cscDetId.ring()==1);
156 
157  CSCEventData & cscData = findEventData(cscDetId);
158 
159  std::vector<CSCStripDigi>::const_iterator digiItr = (*j).second.first;
160  std::vector<CSCStripDigi>::const_iterator last = (*j).second.second;
161  for ( ; digiItr != last; ++digiItr)
162  {
163  CSCStripDigi digi = *digiItr;
164  int strip = digi.getStrip();
165  if (formatVersion_ == 2013)
166  {
167  if ( me1a && zplus )
168  {
169  digi.setStrip(49-strip); // 1-48 -> 48-1
170  }
171  if ( me1b && !zplus)
172  {
173  digi.setStrip(65-strip); // 1-64 -> 64-1
174  }
175  if ( me1a )
176  {
177  strip = digi.getStrip(); // reset back 1-16 to 65-80 digi
178  digi.setStrip(strip+64);
179  }
180 
181  }
182  else
183  {
184  if ( me1a && zplus )
185  {
186  digi.setStrip(17-strip); // 1-16 -> 16-1
187  }
188  if ( me1b && !zplus)
189  {
190  digi.setStrip(65-strip); // 1-64 -> 64-1
191  }
192  if ( me1a )
193  {
194  strip = digi.getStrip(); // reset back 1-16 to 65-80 digi
195  digi.setStrip(strip+64);
196  }
197  }
198  cscData.add(digi, cscDetId.layer() );
199  }
200  }
201  }
202 }
203 
204 
206  const CSCALCTDigiCollection & alctDigis)
207 {
208  add(alctDigis);
209  for (CSCWireDigiCollection::DigiRangeIterator j=wireDigis.begin(); j!=wireDigis.end(); ++j)
210  {
211  CSCDetId cscDetId=(*j).first;
212  if (packEverything_ || cscd2r::accept(cscDetId, alctDigis, alctWindowMin_, alctWindowMax_))
213  {
214  CSCEventData & cscData = findEventData(cscDetId);
215  std::vector<CSCWireDigi>::const_iterator digiItr = (*j).second.first;
216  std::vector<CSCWireDigi>::const_iterator last = (*j).second.second;
217  for ( ; digiItr != last; ++digiItr)
218  {
219  cscData.add(*digiItr, cscDetId.layer() );
220  }
221  }
222  }
223 
224 }
225 
226 void CSCDigiToRaw::add(const CSCComparatorDigiCollection & comparatorDigis,
227  const CSCCLCTDigiCollection & clctDigis)
228 {
229  add(clctDigis);
230  for (CSCComparatorDigiCollection::DigiRangeIterator j=comparatorDigis.begin(); j!=comparatorDigis.end(); ++j)
231  {
232  CSCDetId cscDetId=(*j).first;
233  CSCEventData & cscData = findEventData(cscDetId);
234  if (packEverything_ || cscd2r::accept(cscDetId, clctDigis, clctWindowMin_, clctWindowMax_))
235  {
236  bool me1a = (cscDetId.station()==1) && (cscDetId.ring()==4);
237 
238 
239  BOOST_FOREACH(CSCComparatorDigi digi, (*j).second)
240  {
241  if (formatVersion_ == 2013)
242  {
243  // Move ME1/A comparators from CFEB=0 to CFEB=4 if this has not
244  // been done already.
245  if (me1a && digi.getStrip() <= 48)
246  {
247  CSCComparatorDigi digi_corr(64+digi.getStrip(),
248  digi.getComparator(),
249  digi.getTimeBinWord());
250  cscData.add(digi_corr, cscDetId); // This version does ME11 strips swapping
251  // cscData.add(digi_corr, cscDetId.layer()); // This one doesn't
252  }
253  else
254  {
255  cscData.add(digi, cscDetId); // This version does ME11 strips swapping
256  // cscData.add(digi, cscDetId.layer()); // This one doesn't
257  }
258  }
259  else
260  {
261  // Move ME1/A comparators from CFEB=0 to CFEB=4 if this has not
262  // been done already.
263  if (me1a && digi.getStrip() <= 16)
264  {
265  CSCComparatorDigi digi_corr(64+digi.getStrip(),
266  digi.getComparator(),
267  digi.getTimeBinWord());
268  cscData.add(digi_corr, cscDetId.layer());
269  }
270  else
271  {
272  cscData.add(digi, cscDetId.layer());
273  }
274  }
275  }
276  }
277  }
278 }
279 
281 {
282  for (CSCALCTDigiCollection::DigiRangeIterator j=alctDigis.begin(); j!=alctDigis.end(); ++j)
283  {
284  CSCDetId cscDetId=(*j).first;
285  CSCEventData & cscData = findEventData(cscDetId);
286 
287  cscData.add(std::vector<CSCALCTDigi>((*j).second.first, (*j).second.second));
288  }
289 }
290 
292 {
293  for (CSCCLCTDigiCollection::DigiRangeIterator j=clctDigis.begin(); j!=clctDigis.end(); ++j)
294  {
295  CSCDetId cscDetId=(*j).first;
296  CSCEventData & cscData = findEventData(cscDetId);
297 
298  cscData.add(std::vector<CSCCLCTDigi>((*j).second.first, (*j).second.second));
299  }
300 }
301 
303 {
304  for (CSCCorrelatedLCTDigiCollection::DigiRangeIterator j=corrLCTDigis.begin(); j!=corrLCTDigis.end(); ++j)
305  {
306  CSCDetId cscDetId=(*j).first;
307  CSCEventData & cscData = findEventData(cscDetId);
308 
309  cscData.add(std::vector<CSCCorrelatedLCTDigi>((*j).second.first, (*j).second.second));
310  }
311 
312 }
313 
314 
316  const CSCWireDigiCollection& wireDigis,
317  const CSCComparatorDigiCollection& comparatorDigis,
318  const CSCALCTDigiCollection& alctDigis,
319  const CSCCLCTDigiCollection& clctDigis,
320  const CSCCLCTPreTriggerCollection & preTriggers,
321  const CSCCorrelatedLCTDigiCollection& correlatedLCTDigis,
322  FEDRawDataCollection& fed_buffers,
323  const CSCChamberMap* mapping,
324  Event & e, uint16_t format_version, bool use_pre_triggers,
325  bool packEverything)
326 {
327 
328  formatVersion_ = format_version;
329  usePreTriggers_ = use_pre_triggers;
330  packEverything_ = packEverything;
331  //bits of code from ORCA/Muon/METBFormatter - thanks, Rick:)!
332 
333  //get fed object from fed_buffers
334  // make a map from the index of a chamber to the event data from it
335  beginEvent(mapping);
336  add(stripDigis, preTriggers);
337  add(wireDigis, alctDigis);
338  add(comparatorDigis, clctDigis);
339  add(correlatedLCTDigis);
340 
341  int l1a=e.id().event(); //need to add increments or get it from lct digis
342  int bx = l1a;//same as above
343  //int startingFED = FEDNumbering::MINCSCFEDID;
344 
345  if (formatVersion_ == 2005)
346  {
347  std::map<int, CSCDCCEventData> dccMap;
348  for (int idcc=FEDNumbering::MINCSCFEDID;
349  idcc<=FEDNumbering::MAXCSCFEDID; ++idcc)
350  {
351  //idcc goes from startingFed to startingFED+7
352  // @@ if ReadoutMapping changes, this'll have to change
353  // DCCs 1,2,4,5 have 5 DDUs. Otherwise, 4
354  //int nDDUs = (idcc < 2) || (idcc ==4) || (idcc ==5)
355  // ? 5 : 4;
356  //@@ WARNING some DCCs only have 4 DDUs, but I'm giving them all 5, for now
357  int nDDUs = 5;
358  dccMap.insert(std::pair<int, CSCDCCEventData>(idcc, CSCDCCEventData(idcc, nDDUs, bx, l1a) ) );
359  }
360 
361  for (int idcc=FEDNumbering::MINCSCFEDID;
362  idcc<=FEDNumbering::MAXCSCFEDID; ++idcc)
363  {
364 
365  // for every chamber with data, add to a DDU in this DCC Event
366  for (map<CSCDetId, CSCEventData>::iterator chamberItr = theChamberDataMap.begin();
367  chamberItr != theChamberDataMap.end(); ++chamberItr)
368  {
369  int indexDCC = mapping->slink(chamberItr->first);
370  if (indexDCC == idcc)
371  {
372  //FIXME (What does this mean? Is something wrong?)
373  std::map<int, CSCDCCEventData>::iterator dccMapItr = dccMap.find(indexDCC);
374  if (dccMapItr == dccMap.end())
375  {
376  throw cms::Exception("CSCDigiToRaw") << "Bad DCC number:" << indexDCC;
377  }
378  // get id's based on ChamberId from mapping
379 
380  int dduId = mapping->ddu(chamberItr->first);
381  int dduSlot = mapping->dduSlot(chamberItr->first);
382  int dduInput = mapping->dduInput(chamberItr->first);
383  int dmbId = mapping->dmb(chamberItr->first);
384  dccMapItr->second.addChamber(chamberItr->second, dduId, dduSlot, dduInput, dmbId, formatVersion_);
385  }
386  }
387  }
388 
389  // FIXME: FEDRawData size set to 2*64 to add FED header and trailer
390  for (std::map<int, CSCDCCEventData>::iterator dccMapItr = dccMap.begin();
391  dccMapItr != dccMap.end(); ++dccMapItr)
392  {
393  boost::dynamic_bitset<> dccBits = dccMapItr->second.pack();
394  FEDRawData & fedRawData = fed_buffers.FEDData(dccMapItr->first);
395  fedRawData.resize(dccBits.size());
396  //fill data with dccEvent
397  bitset_utilities::bitsetToChar(dccBits, fedRawData.data());
398  FEDTrailer cscFEDTrailer(fedRawData.data()+(fedRawData.size()-8));
399  cscFEDTrailer.set(fedRawData.data()+(fedRawData.size()-8),
400  fedRawData.size()/8,
401  evf::compute_crc(fedRawData.data(),fedRawData.size()), 0, 0);
402  }
403 
404  }
405  else if (formatVersion_ == 2013)
406  {
407 
408  std::map<int, CSCDDUEventData> dduMap;
409  unsigned int ddu_fmt_version = 0x7;
410  const unsigned postLS1_map [] = { 841, 842, 843, 844, 845, 846, 847, 848, 849,
411  831, 832, 833, 834, 835, 836, 837, 838, 839,
412  861, 862, 863, 864, 865, 866, 867, 868, 869,
413  851, 852, 853, 854, 855, 856, 857, 858, 859 };
414 
415 
417  for (unsigned int i = 0; i < 36; i++)
418  {
419  unsigned int iddu = postLS1_map[i];
420  // make a new one
421  CSCDDUHeader newDDUHeader(bx,l1a, iddu, ddu_fmt_version);
422 
423  dduMap.insert(std::pair<int, CSCDDUEventData>(iddu, CSCDDUEventData(newDDUHeader) ) );
424  }
425 
426 
428  for (unsigned int i = 0; i < 36; i++)
429  {
430  unsigned int iddu = postLS1_map[i];
431  // for every chamber with data, add to a DDU in this DCC Event
432  for (map<CSCDetId, CSCEventData>::iterator chamberItr = theChamberDataMap.begin();
433  chamberItr != theChamberDataMap.end(); ++chamberItr)
434  {
435  unsigned int indexDDU = mapping->slink(chamberItr->first);
436 
438  if ( (indexDDU >= FEDNumbering::MINCSCFEDID) && (indexDDU <= FEDNumbering::MAXCSCFEDID) ) // Still preLS1 DCC FEDs mapping
439  {
440 
441  int dduID = mapping->ddu(chamberItr->first); // try to switch to DDU ID mapping
442  if ((dduID >= FEDNumbering::MINCSCDDUFEDID) && (dduID <= FEDNumbering::MAXCSCDDUFEDID) ) // DDU ID is in expectedi post-LS1 FED ID range
443  {
444  indexDDU = dduID;
445  }
446  else // Messy
447  {
448  // Lets try to change pre-LS1 1-36 ID range to post-LS1 MINCSCDDUFEDID - MAXCSCDDUFEDID range
449  dduID &= 0xFF;
450  if ((dduID <= 36) && (dduID > 0)) indexDDU = postLS1_map[dduID -1]; // indexDDU = FEDNumbering::MINCSCDDUFEDID + dduID-1;
451  }
452 
453  }
454 
455  if (indexDDU == iddu)
456  {
457  std::map<int, CSCDDUEventData>::iterator dduMapItr = dduMap.find(indexDDU);
458  if (dduMapItr == dduMap.end())
459  {
460  throw cms::Exception("CSCDigiToRaw") << "Bad DDU number:" << indexDDU;
461  }
462  // get id's based on ChamberId from mapping
463 
464  int dduInput = mapping->dduInput(chamberItr->first);
465  int dmbId = mapping->dmb(chamberItr->first);
466  // int crateId = mapping->crate(chamberItr->first);
467  dduMapItr->second.add( chamberItr->second, dmbId, dduInput, formatVersion_ );
468  }
469  }
470  }
471 
472  // FIXME: FEDRawData size set to 2*64 to add FED header and trailer
473  for (std::map<int, CSCDDUEventData>::iterator dduMapItr = dduMap.begin();
474  dduMapItr != dduMap.end(); ++dduMapItr)
475  {
476  boost::dynamic_bitset<> dduBits = dduMapItr->second.pack();
477  FEDRawData & fedRawData = fed_buffers.FEDData(dduMapItr->first);
478  fedRawData.resize(dduBits.size()/8);
479  //fill data with dduEvent
480  bitset_utilities::bitsetToChar(dduBits, fedRawData.data());
481  FEDTrailer cscFEDTrailer(fedRawData.data()+(fedRawData.size()-8));
482  cscFEDTrailer.set(fedRawData.data()+(fedRawData.size()-8),
483  fedRawData.size()/8,
484  evf::compute_crc(fedRawData.data(),fedRawData.size()), 0, 0);
485 
486  }
487  }
488 }
489 
490 
01/20/05 A.Tumanov
int chamber() const
Definition: CSCDetId.h:81
EventNumber_t event() const
Definition: EventID.h:41
void add(const CSCStripDigiCollection &stripDigis, const CSCCLCTPreTriggerCollection &preTriggers)
int i
Definition: DBlmapReader.cc:9
int getStrip() const
Get the strip number.
uint16_t formatVersion_
Definition: CSCDigiToRaw.h:68
bool usePreTriggers_
Definition: CSCDigiToRaw.h:69
bool packEverything_
Definition: CSCDigiToRaw.h:70
int ddu(const CSCDetId &) const
ddu id for given DetId
int clctWindowMin_
Definition: CSCDigiToRaw.h:64
int dmb(const CSCDetId &) const
dmb id for given DetId
void setdmbVersion(unsigned int version)
Definition: CSCDMBHeader.h:43
bool accept(const edm::Event &event, const edm::TriggerResults &triggerTable, const std::string &triggerPath)
Definition: TopDQMHelpers.h:25
int getComparator() const
Get Comparator readings.
int layer() const
Definition: CSCDetId.h:74
size_t size() const
Lenght of the data buffer in bytes.
Definition: FEDRawData.h:47
int getStrip() const
Definition: CSCStripDigi.h:51
int endcap() const
Definition: CSCDetId.h:106
int alctWindowMax_
Definition: CSCDigiToRaw.h:63
const FEDRawData & FEDData(int fedid) const
retrieve data for fed
void setStrip(int istrip)
Definition: CSCStripDigi.h:75
const CSCChamberMap * theElectronicsMap
Definition: CSCDigiToRaw.h:61
void resize(size_t newsize)
Definition: FEDRawData.cc:32
int slink(const CSCDetId &) const
slink id for given DetId
unsigned short compute_crc(unsigned char *buffer, unsigned int bufSize)
Definition: CRC16.h:67
tuple result
Definition: query.py:137
int alctWindowMin_
Definition: CSCDigiToRaw.h:62
int dduInput(const CSCDetId &) const
ddu input for given DetId
int j
Definition: DBlmapReader.cc:9
CSCDetId chamberId() const
Definition: CSCDetId.h:66
CSCDetId chamberID(const CSCDetId &cscDetId)
takes layer ID, converts to chamber ID, switching ME1A to ME11
Definition: CSCDigiToRaw.cc:31
CSCDigiToRaw(const edm::ParameterSet &pset)
Constructor.
Definition: CSCDigiToRaw.cc:89
int getTimeBinWord() const
Return the word with each bit corresponding to a time bin.
int clctWindowMax_
Definition: CSCDigiToRaw.h:65
void beginEvent(const CSCChamberMap *electronicsMap)
void setCrateAddress(int crate, int dmbId)
Definition: CSCDMBHeader.h:41
int preTriggerWindowMin_
Definition: CSCDigiToRaw.h:66
unsigned short iChamberType()
Definition: CSCDetId.h:120
int preTriggerWindowMax_
Definition: CSCDigiToRaw.h:67
int ring() const
Definition: CSCDetId.h:88
PixelRecoRange< float > Range
std::map< CSCDetId, CSCEventData > theChamberDataMap
Definition: CSCDigiToRaw.h:60
void bitsetToChar(const boost::dynamic_bitset<> &bs, unsigned char *result)
this method takes bitset obj and returns char * array
int crate(const CSCDetId &) const
Interface required use in digi-to-raw.
std::vector< CSCCLCTPreTrigger >::const_iterator const_iterator
int dduSlot(const CSCDetId &) const
ddu slot for given DetId
CSCEventData & findEventData(const CSCDetId &cscDetId)
pick out the correct data object for this chamber
void add(const CSCStripDigi &, int layer)
routines to add digis to the data
edm::EventID id() const
Definition: EventBase.h:56
EcalElectronicsMapping const * electronicsMap(0)
bool accept(const CSCDetId &cscId, const LCTCollection &lcts, int bxMin, int bxMax)
Definition: CSCDigiToRaw.cc:42
const unsigned char * data() const
Return a const pointer to the beginning of the data buffer.
Definition: FEDRawData.cc:28
int station() const
Definition: CSCDetId.h:99
std::pair< const_iterator, const_iterator > Range
volatile std::atomic< bool > shutdown_flag false
void createFedBuffers(const CSCStripDigiCollection &stripDigis, const CSCWireDigiCollection &wireDigis, const CSCComparatorDigiCollection &comparatorDigis, const CSCALCTDigiCollection &alctDigis, const CSCCLCTDigiCollection &clctDigis, const CSCCLCTPreTriggerCollection &preTriggers, const CSCCorrelatedLCTDigiCollection &correlatedLCTDigis, FEDRawDataCollection &fed_buffers, const CSCChamberMap *theMapping, edm::Event &e, uint16_t theFormatVersion=2005, bool usePreTriggers=true, bool packEverything=false)
Take a vector of digis and fill the FEDRawDataCollection.
const CSCDMBHeader * dmbHeader() const
the DAQ motherboard header. A good place for event and chamber info
Definition: CSCEventData.h:96