CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
CSCTMBHeader.cc
Go to the documentation of this file.
11 #include <math.h>
12 #include <string.h> // memcpy
13 
14 #ifdef LOCAL_UNPACK
15 bool CSCTMBHeader::debug = false;
16 #else
17 std::atomic<bool> CSCTMBHeader::debug{false};
18 #endif
19 
20 CSCTMBHeader::CSCTMBHeader(int firmwareVersion, int firmwareRevision):
21  theHeaderFormat(),
22  theFirmwareVersion(firmwareVersion)
23 {
24  if(firmwareVersion == 2013)
25  {
26  theHeaderFormat = boost::shared_ptr<CSCVTMBHeaderFormat>(new CSCTMBHeader2013());
27  }
28  else if(firmwareVersion == 2006)
29  {
30  theHeaderFormat = boost::shared_ptr<CSCVTMBHeaderFormat>(new CSCTMBHeader2006());
31  }
32  else if(firmwareVersion == 2007)
33  {
34  if(firmwareRevision >= 0x50c3)
35  {
36  if (firmwareRevision >= 0x7a76)
37  {
38  theHeaderFormat = boost::shared_ptr<CSCVTMBHeaderFormat>(new CSCTMBHeader2013());
39  }
40  else
41  {
42  theHeaderFormat = boost::shared_ptr<CSCVTMBHeaderFormat>(new CSCTMBHeader2007_rev0x50c3());
43  }
44  }
45  else
46  {
47  theHeaderFormat = boost::shared_ptr<CSCVTMBHeaderFormat>(new CSCTMBHeader2007());
48  }
49  }
50  else
51  {
52  edm::LogError("CSCTMBHeader|CSCRawToDigi") <<"failed to determine TMB firmware version!!";
53  }
54 }
55 
56 //CSCTMBHeader::CSCTMBHeader(const CSCTMBStatusDigi & digi) {
57 // CSCTMBHeader(digi.header());
58 //}
59 
60 CSCTMBHeader::CSCTMBHeader(const unsigned short * buf)
61  : theHeaderFormat()
62 {
64  if (buf[0]==0xDB0C)
65  {
66  theFirmwareVersion=2007;
67  theHeaderFormat = boost::shared_ptr<CSCVTMBHeaderFormat>(new CSCTMBHeader2007(buf));
68  if(theHeaderFormat->firmwareRevision() >= 0x50c3)
69  {
70  if (theHeaderFormat->firmwareRevision() >= 0x7a76)
71  {
72  theFirmwareVersion=2013;
73  theHeaderFormat = boost::shared_ptr<CSCVTMBHeaderFormat>(new CSCTMBHeader2013(buf));
74  }
75  else
76  {
77  theHeaderFormat = boost::shared_ptr<CSCVTMBHeaderFormat>(new CSCTMBHeader2007_rev0x50c3(buf));
78  }
79  }
80 
81  }
82  else if (buf[0]==0x6B0C)
83  {
84  theFirmwareVersion=2006;
85  theHeaderFormat = boost::shared_ptr<CSCVTMBHeaderFormat>(new CSCTMBHeader2006(buf));
86  }
87  else
88  {
89  edm::LogError("CSCTMBHeader|CSCRawToDigi") <<"failed to determine TMB firmware version!!";
90  }
91 }
92 
93 /*
94 void CSCTMBHeader::swapCLCTs(CSCCLCTDigi& digi1, CSCCLCTDigi& digi2)
95 {
96  bool me11 = (theChamberId.station() == 1 &&
97  (theChamberId.ring() == 1 || theChamberId.ring() == 4));
98  if (!me11) return;
99 
100  int cfeb1 = digi1.getCFEB();
101  int cfeb2 = digi2.getCFEB();
102  if (cfeb1 != cfeb2) return;
103 
104  bool me1a = (cfeb1 == 4);
105  bool me1b = (cfeb1 != 4);
106  bool zplus = (theChamberId.endcap() == 1);
107 
108  if ( (me1a && zplus) || (me1b && !zplus)) {
109  // Swap CLCTs if they have the same quality and pattern # (priority
110  // has to be given to the lower key).
111  if (digi1.getQuality() == digi2.getQuality() &&
112  digi1.getPattern() == digi2.getPattern()) {
113  CSCCLCTDigi temp = digi1;
114  digi1 = digi2;
115  digi2 = temp;
116 
117  // Also re-number them.
118  digi1.setTrknmb(1);
119  digi2.setTrknmb(2);
120  }
121  }
122 }
123 */
124 
125 
126 //FIXME Pick which LCT goes first
127 void CSCTMBHeader::add(const std::vector<CSCCLCTDigi> & digis)
128 {
129  // sort???
130  if(digis.size() > 0) {
131  addCLCT0(digis[0]);
132 
133  }
134  if(digis.size() > 1) addCLCT1(digis[1]);
135 }
136 
137 void CSCTMBHeader::add(const std::vector<CSCCorrelatedLCTDigi> & digis)
138 {
139  // sort???
140  if(digis.size() > 0) addCorrelatedLCT0(digis[0]);
141  if(digis.size() > 1) addCorrelatedLCT1(digis[1]);
142 }
143 
144 
146 {
147  CSCTMBHeader2007 * result = dynamic_cast<CSCTMBHeader2007 *>(theHeaderFormat.get());
148  if(result == 0)
149  {
150  throw cms::Exception("Could not get 2007 TMB header format");
151  }
152  return *result;
153 }
154 
156 {
157  CSCTMBHeader2013 * result = dynamic_cast<CSCTMBHeader2013 *>(theHeaderFormat.get());
158  if(result == 0)
159  {
160  throw cms::Exception("Could not get 2013 TMB header format");
161  }
162  return *result;
163 }
164 
165 
167 {
168  CSCTMBHeader2006 * result = dynamic_cast<CSCTMBHeader2006 *>(theHeaderFormat.get());
169  if(result == 0)
170  {
171  throw cms::Exception("Could not get 2006 TMB header format");
172  }
173  return *result;
174 }
175 
176 
178 {
179  static bool debug = false;
180 
181  // tests packing and unpacking
182  for(int station = 1; station <= 4; ++station)
183  {
184  for(int iendcap = 1; iendcap <= 2; ++iendcap)
185  {
186  CSCDetId detId(iendcap, station, 1, 1, 0);
187 
188  // the next-to-last is the BX, which only gets
189  // saved in two bits and must be the same for clct0 and clct1.
190  //CSCCLCTDigi clct0(1, 1, 4, 0, 0, 30, 3, 0, 1); // valid for 2006
191  // In 2007 firmware, there are no distrips, so the 4th argument (strip
192  // type) should always be set to 1 (halfstrips).
193  CSCCLCTDigi clct0(1, 1, 4, 1, 0, 30, 4, 2, 1); // valid for 2007
194  CSCCLCTDigi clct1(1, 1, 2, 1, 1, 31, 1, 2, 2);
195 
196  // BX of LCT (8th argument) is 1-bit word (the least-significant bit
197  // of ALCT's bx).
198  CSCCorrelatedLCTDigi lct0(1, 1, 2, 10, 98, 5, 0, 1, 0, 0, 0, 0);
199  CSCCorrelatedLCTDigi lct1(2, 1, 2, 20, 15, 9, 1, 0, 0, 0, 0, 0);
200 
201  CSCTMBHeader tmbHeader(2007, 0x50c3);
202  tmbHeader.addCLCT0(clct0);
203  tmbHeader.addCLCT1(clct1);
204  tmbHeader.addCorrelatedLCT0(lct0);
205  tmbHeader.addCorrelatedLCT1(lct1);
206  std::vector<CSCCLCTDigi> clcts = tmbHeader.CLCTDigis(detId.rawId());
207  // guess they got reordered
208  assert(cscPackerCompare(clcts[0],clct0));
209  assert(cscPackerCompare(clcts[1],clct1));
210  if (debug)
211  {
212  std::cout << "Match for: " << clct0 << "\n";
213  std::cout << " " << clct1 << "\n \n";
214  }
215 
216  std::vector<CSCCorrelatedLCTDigi> lcts = tmbHeader.CorrelatedLCTDigis(detId.rawId());
217  assert(cscPackerCompare(lcts[0], lct0));
218  assert(cscPackerCompare(lcts[1], lct1));
219  if (debug)
220  {
221  std::cout << "Match for: " << lct0 << "\n";
222  std::cout << " " << lct1 << "\n";
223  }
224 
225  // try packing and re-packing, to make sure they're the same
226  unsigned short int * data = tmbHeader.data();
227  CSCTMBHeader newHeader(data);
228  clcts = newHeader.CLCTDigis(detId.rawId());
229  assert(cscPackerCompare(clcts[0],clct0));
230  assert(cscPackerCompare(clcts[1],clct1));
231  lcts = newHeader.CorrelatedLCTDigis(detId.rawId());
232  assert(cscPackerCompare(lcts[0], lct0));
233  assert(cscPackerCompare(lcts[1], lct1));
234 
235 
236  }
237  }
238 }
239 
240 
241 std::ostream & operator<<(std::ostream & os, const CSCTMBHeader & hdr)
242 {
243  hdr.theHeaderFormat->print(os);
244  return os;
245 }
CSCTMBHeader2007 tmbHeader2007() const
will throw if the cast fails
int theFirmwareVersion
Definition: CSCTMBHeader.h:156
std::vector< CSCCorrelatedLCTDigi > CorrelatedLCTDigis(uint32_t idlayer) const
returns CorrelatedLCT digis
Definition: CSCTMBHeader.h:96
assert(m_qm.get())
void addCorrelatedLCT0(const CSCCorrelatedLCTDigi &digi)
Definition: CSCTMBHeader.h:128
void add(const std::vector< CSCCLCTDigi > &digis)
these methods need more brains to figure which one goes first
std::ostream & operator<<(std::ostream &out, const ALILine &li)
Definition: ALILine.cc:187
void addCorrelatedLCT1(const CSCCorrelatedLCTDigi &digi)
Definition: CSCTMBHeader.h:131
void addCLCT1(const CSCCLCTDigi &digi)
Definition: CSCTMBHeader.h:125
uint32_t rawId() const
get the raw id
Definition: DetId.h:43
boost::shared_ptr< CSCVTMBHeaderFormat > theHeaderFormat
Definition: CSCTMBHeader.h:155
static void selfTest()
tests that packing and unpacking give same results
tuple result
Definition: query.py:137
unsigned short * data()
Definition: CSCTMBHeader.h:109
static std::atomic< bool > debug
Definition: CSCTMBHeader.h:152
std::vector< CSCCLCTDigi > CLCTDigis(uint32_t idlayer)
returns CLCT digis
Definition: CSCTMBHeader.h:90
CSCTMBHeader2006 tmbHeader2006() const
void addCLCT0(const CSCCLCTDigi &digi)
Needed before data packing.
Definition: CSCTMBHeader.h:124
bool cscPackerCompare(const T &t1, const T &t2)
tuple cout
Definition: gather_cfg.py:121
CSCTMBHeader(int firmwareVersion, int firmwareRevision)
Definition: CSCTMBHeader.cc:20
CSCTMBHeader2013 tmbHeader2013() const