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