CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ESUnpacker.cc
Go to the documentation of this file.
7 
8 #include <fstream>
9 
11  : pset_(ps), fedId_(0), run_number_(0), orbit_number_(0), bx_(0), lv1_(0), trgtype_(0)
12 {
13 
14  debug_ = pset_.getUntrackedParameter<bool>("debugMode", false);
15  lookup_ = ps.getParameter<edm::FileInPath>("LookupTable");
16 
17  m1 = ~(~Word64(0) << 1);
18  m2 = ~(~Word64(0) << 2);
19  m4 = ~(~Word64(0) << 4);
20  m5 = ~(~Word64(0) << 5);
21  m6 = ~(~Word64(0) << 6);
22  m8 = ~(~Word64(0) << 8);
23  m12 = ~(~Word64(0) << 12);
24  m16 = ~(~Word64(0) << 16);
25  m32 = ~(~Word64(0) << 32);
26 
27  // read in look-up table
28  int nLines, iz, ip, ix, iy, fed, kchip, pace, bundle, fiber, optorx;
29  std::ifstream file;
30  file.open(lookup_.fullPath().c_str());
31  if( file.is_open() ) {
32 
33  file >> nLines;
34 
35  for (int i=0; i<nLines; ++i) {
36  file>> iz >> ip >> ix >> iy >> fed >> kchip >> pace >> bundle >> fiber >> optorx;
37 
38  zside_[kchip-1][pace-1] = iz;
39  pl_[kchip-1][pace-1] = ip;
40  x_[kchip-1][pace-1] = ix;
41  y_[kchip-1][pace-1] = iy;
42  }
43 
44  } else {
45  edm::LogWarning("Invalid Data")<<"ESUnpacker::ESUnpacker : Look up table file can not be found in "<<lookup_.fullPath().c_str();
46  }
47 
48 }
49 
51 }
52 
54 
55  int nWords = rawData.size()/sizeof(Word64);
56  if (nWords==0) return;
57  int dccWords = 6;
58  int head, kPACE[4], kFlag1, kFlag2, kBC, kEC, optoBC, optoEC;
59  int kid = -1;
60 
61  ESDCCHeaderBlock ESDCCHeader;
62  ESDCCHeader.setFedId(fedId);
63 
64  // Event header
65  const Word64* header = reinterpret_cast<const Word64* >(rawData.data()); --header;
66  bool moreHeaders = true;
67  while (moreHeaders) {
68  ++header;
69  FEDHeader ESHeader( reinterpret_cast<const unsigned char*>(header) );
70  if ( !ESHeader.check() ) {
71  if (debug_) edm::LogWarning("Invalid Data")<<"ES : Failed header check !";
72  return;
73  }
74 
75  fedId_ = ESHeader.sourceID();
76  lv1_ = ESHeader.lvl1ID();
77  bx_ = ESHeader.bxID();
78 
79  if (debug_) {
80  LogDebug("ESUnpacker") << "[ESUnpacker]: FED Header candidate. Is header? "<< ESHeader.check();
81  if (ESHeader.check())
82  LogDebug("ESUnpacker") << ". BXID: "<<bx_<<" SourceID : "<<fedId_<<" L1ID: "<<lv1_;
83  else LogDebug("ESUnpacker")<<" WARNING!, this is not a ES Header";
84  }
85 
86  moreHeaders = ESHeader.moreHeaders();
87  }
88  if ( fedId != fedId_) {
89  if (debug_) edm::LogWarning("Invalid Data")<<"Invalid ES data with source id " <<fedId_;
90  ESDCCHeader.setDCCErrors(1);
91  dccs.push_back(ESDCCHeader);
92  return;
93  }
94  ESDCCHeader.setLV1(lv1_);
95  ESDCCHeader.setBX(bx_);
96 
97  // Event trailer
98  int slinkCRC = 1;
99  const Word64* trailer = reinterpret_cast<const Word64* >(rawData.data())+(nWords-1); ++trailer;
100  bool moreTrailers = true;
101  while (moreTrailers) {
102  --trailer;
103  FEDTrailer ESTrailer(reinterpret_cast<const unsigned char*>(trailer));
104  if ( !ESTrailer.check()) {
105  ++trailer;
106  if (debug_) edm::LogWarning("Invalid Data")<<"ES : Failed trailer check !";
107  return;
108  }
109  if ( ESTrailer.lenght() != nWords) {
110  if (debug_) edm::LogWarning("Invalid Data")<<"Invalid ES data : the length is not correct !";
111  ESDCCHeader.setDCCErrors(2);
112  dccs.push_back(ESDCCHeader);
113  return;
114  }
115  if ( ESTrailer.lenght() < 8) {
116  if (debug_) edm::LogWarning("Invalid Data")<<"Invalid ES data : the length is not correct !";
117  ESDCCHeader.setDCCErrors(3);
118  dccs.push_back(ESDCCHeader);
119  return;
120  }
121  slinkCRC = (*trailer >> 2 ) & 0x1;
122  if (debug_) {
123  LogDebug("ESUnpacker")<<"[ESUnpacker]: FED Trailer candidate. Is trailer? "<<ESTrailer.check();
124  if (ESTrailer.check())
125  LogDebug("ESUnpacker")<<". Length of the ES event: "<<ESTrailer.lenght();
126  else LogDebug("ESUnpacker")<<" WARNING!, this is not a ES Trailer";
127  }
128 
129  moreTrailers = ESTrailer.moreTrailers();
130  }
131 
132  if (slinkCRC != 0) {
133  ESDCCHeader.setDCCErrors(101);
134  dccs.push_back(ESDCCHeader);
135  return;
136  }
137 
138  // DCC data
139  std::vector<int> FEch_status;
140  int dccHeaderCount = 0;
141  int dccLineCount = 0;
142  int dccHead, dccLine;
143  int dccCRC1_ = 0;
144  int dccCRC2_ = 0;
145  int dccCRC3_ = 0;
146  for (const Word64* word=(header+1); word!=(header+dccWords+1); ++word) {
147  if (debug_) LogDebug("ESUnpacker")<<"DCC : "<<print(*word);
148  dccHead = (*word >> 60) & m4;
149  if (dccHead == 3) dccHeaderCount++;
150  dccLine = (*word >> 56) & m4;
151  dccLineCount++;
152  if (dccLine != dccLineCount) {
153  if (debug_) edm::LogWarning("Invalid Data")<<"Invalid ES data : DCC header order is not correct !";
154  ESDCCHeader.setDCCErrors(4);
155  dccs.push_back(ESDCCHeader);
156  return;
157  }
158  if (dccLineCount == 1) {
159  dccCRC1_ = (*word >> 24) & m1;
160  dccCRC2_ = (*word >> 25) & m1;
161  dccCRC3_ = (*word >> 26) & m1;
162  } else if (dccLineCount == 2) {
163  runtype_ = (*word >> 0) & m4;
164  seqtype_ = (*word >> 4) & m4;
165  dac_ = (*word >> 8) & m12;
166  gain_ = (*word >> 20) & m1;
167  precision_ = (*word >> 21) & m1;
168  trgtype_ = (*word >> 34) & m6;
169 
170  ESDCCHeader.setRunType(runtype_);
171  ESDCCHeader.setSeqType(seqtype_);
172  ESDCCHeader.setTriggerType(trgtype_);
173  ESDCCHeader.setDAC(dac_);
174  ESDCCHeader.setGain(gain_);
175  ESDCCHeader.setPrecision(precision_);
176  }
177  if (dccLineCount == 3) {
178  orbit_number_ = (*word >> 0) & m32;
179  vminor_ = (*word >> 40) & m8;
180  vmajor_ = (*word >> 48) & m8;
181 
182  ESDCCHeader.setOrbitNumber(orbit_number_);
183  ESDCCHeader.setMajorVersion(vmajor_);
184  ESDCCHeader.setMinorVersion(vminor_);
185  }
186  if (dccLineCount == 4) optoRX0_ = (*word >> 48) & m8;
187  if (dccLineCount == 5) optoRX1_ = (*word >> 48) & m8;
188  if (dccLineCount == 6) optoRX2_ = (*word >> 48) & m8;
189  if (dccLineCount >=4) {
190  for (unsigned int j=0; j<12; ++j) {
191  FEch_[(dccLineCount-4)*12+j] = (*word >> (j*4)) & m4;
192  FEch_status.push_back(FEch_[(dccLineCount-4)*12+j]);
193  }
194  }
195  }
196  if (vmajor_ < 4) {
197  if (debug_)
198  edm::LogWarning("Invalid Data")<<"Invalid ES data format : "<<vmajor_<<" "<<vminor_;
199  return;
200  }
201  if (dccHeaderCount != 6) {
202  edm::LogWarning("Invalid Data")<<"Invalid ES data : DCC header lines are "<<dccHeaderCount;
203  ESDCCHeader.setDCCErrors(5);
204  dccs.push_back(ESDCCHeader);
205  return;
206  }
207  ESDCCHeader.setOptoRX0(optoRX0_ + dccCRC1_);
208  ESDCCHeader.setOptoRX1(optoRX1_ + dccCRC2_);
209  ESDCCHeader.setOptoRX2(optoRX2_ + dccCRC3_);
210  ESDCCHeader.setFEChannelStatus(FEch_status);
211  int enableOptoRX[3] = {-1, -1, -1};
212  int NenableOptoRX = 0;
213  if (optoRX0_ == 128) {
214  enableOptoRX[NenableOptoRX] = 0;
215  NenableOptoRX++;
216  }
217  if (optoRX1_ == 128) {
218  enableOptoRX[NenableOptoRX] = 1;
219  NenableOptoRX++;
220  }
221  if (optoRX2_ == 128) {
222  enableOptoRX[NenableOptoRX] = 2;
223  }
224 
225  // Event data
226  int iopto = 0;
227  int opto = -1;
228  for (const Word64* word=(header+dccWords+1); word!=trailer; ++word) {
229  if (debug_) LogDebug("ESUnpacker")<<"Event : "<<print(*word);
230 
231  head = (*word >> 60) & m4;
232 
233  if (head == 12) {
234  if ((opto==0 && ESDCCHeader.getOptoRX0()==129) || (opto==1 && ESDCCHeader.getOptoRX1()==129) || (opto==2 && ESDCCHeader.getOptoRX2()==129))
235  word2digi(kid, kPACE, *word, digis);
236  } else if (head == 9) {
237  kid = (*word >> 2) & 0x07ff;
238  kPACE[0] = (*word >> 16) & m1;
239  kPACE[1] = (*word >> 17) & m1;
240  kPACE[2] = (*word >> 18) & m1;
241  kPACE[3] = (*word >> 19) & m1;
242  kFlag2 = (*word >> 20) & m4;
243  kFlag1 = (*word >> 24) & m8;
244  kBC = (*word >> 32) & m16;
245  kEC = (*word >> 48) & m8;
246 
247  ESKCHIPBlock ESKCHIP;
248  ESKCHIP.setId(kid);
249  ESKCHIP.setBC(kBC);
250  ESKCHIP.setEC(kEC);
251  ESKCHIP.setOptoBC(optoBC);
252  ESKCHIP.setOptoEC(optoEC);
253  ESKCHIP.setFlag1(kFlag1);
254  ESKCHIP.setFlag2(kFlag2);
255  kchips.push_back(ESKCHIP);
256  } else if (head == 6) {
257  optoBC = (*word >> 32) & m16;
258  optoEC = (*word >> 48) & m8;
259 
260  opto = enableOptoRX[iopto];
261  if (opto==0) ESDCCHeader.setOptoBC0(optoBC);
262  else if (opto==1) ESDCCHeader.setOptoBC1(optoBC);
263  else if (opto==2) ESDCCHeader.setOptoBC2(optoBC);
264  if (iopto < 2) ++iopto;
265  }
266  }
267 
268  dccs.push_back(ESDCCHeader);
269 }
270 
271 void ESUnpacker::word2digi(int kid, int kPACE[4], const Word64 & word, ESDigiCollection & digis)
272 {
273 
274  int pace = (word >> 53) & m2;
275  if (kPACE[pace]==0) return;
276  if (kid > 1511 || kid < 1) return;
277 
278  int adc[3];
279  adc[0] = (word >> 0) & m16;
280  adc[1] = (word >> 16) & m16;
281  adc[2] = (word >> 32) & m16;
282  int strip = (word >> 48) & m5;
283 
284  if (debug_) LogDebug("ESUnpacker")<<kid<<" "<<strip<<" "<<pace<<" "<<adc[0]<<" "<<adc[1]<<" "<<adc[2];
285 
286  int zside, plane, ix, iy;
287  zside = zside_[kid-1][pace];
288  plane = pl_[kid-1][pace];
289  ix = x_[kid-1][pace];
290  iy = y_[kid-1][pace];
291 
292  // convert strip number from electronics id to detector id
293  if (vmajor_ == 4 && (vminor_==2 || vminor_==3)) {
294  if (zside == 1 && plane == 1 && iy <= 20) strip = 31 - strip;
295  if (zside == 1 && plane == 2 && ix > 20) strip = 31 - strip;
296  if (zside == -1 && plane == 1 && iy > 20) strip = 31 - strip;
297  if (zside == -1 && plane == 2 && ix <= 20) strip = 31 - strip;
298  }
299 
300  if (debug_) LogDebug("ESUnpacker")<<"DetId : "<<zside<<" "<<plane<<" "<<ix<<" "<<iy<<" "<<strip+1;
301 
302  if (ESDetId::validDetId(strip+1, ix, iy, plane, zside)) {
303 
304  ESDetId detId(strip+1, ix, iy, plane, zside);
305  ESDataFrame df(detId);
306  df.setSize(3);
307 
308  for (int i=0; i<3; i++) df.setSample(i, adc[i]);
309 
310  digis.push_back(df);
311 
312  if (debug_)
313  LogDebug("ESUnpacker")<<"Si : "<<detId.zside()<<" "<<detId.plane()<<" "<<detId.six()<<" "<<detId.siy()<<" "<<detId.strip()<<" ("<<kid<<","<<pace<<") "<<df.sample(0).adc()<<" "<<df.sample(1).adc()<<" "<<df.sample(2).adc();
314  }
315 
316 }
317 
319 {
320  std::ostringstream str;
321  str << "Word64: " << reinterpret_cast<const std::bitset<64>&> (word);
322  return str.str();
323 }
324 
int adc(sample_type sample)
get the ADC sample (12 bits)
#define LogDebug(id)
void setOptoBC2(const int &optoBC2)
int runtype_
Definition: ESUnpacker.h:56
void setOrbitNumber(const int &orbit)
T getParameter(std::string const &) const
int vmajor_
Definition: ESUnpacker.h:60
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
bool debug_
Definition: ESUnpacker.h:66
int strip() const
Definition: ESDetId.h:52
void setGain(const int &gain)
edm::FileInPath lookup_
Definition: ESUnpacker.h:67
bool check()
Definition: FEDTrailer.cc:64
void setOptoBC(const int &OptoBC)
Definition: ESKCHIPBlock.h:29
void setFEChannelStatus(const std::vector< int > &FEch)
void setFedId(const int &fedId)
void setId(const int &kId)
Definition: ESKCHIPBlock.h:16
int fedId_
Definition: ESUnpacker.h:48
int optoRX0_
Definition: ESUnpacker.h:61
int y_[4288][4]
Definition: ESUnpacker.h:75
void setOptoRX1(const int &optoRX1)
bool moreTrailers()
Definition: FEDTrailer.cc:37
int getOptoRX0() const
void push_back(T const &t)
Word64 m5
Definition: ESUnpacker.h:73
int getOptoRX1() const
int six() const
Definition: ESDetId.h:48
int x_[4288][4]
Definition: ESUnpacker.h:75
int zside(DetId const &)
Word64 m12
Definition: ESUnpacker.h:73
void setPrecision(const int &precision)
void setOptoBC0(const int &optoBC0)
size_t size() const
Lenght of the data buffer in bytes.
Definition: FEDRawData.h:47
void setDCCErrors(const int &dccErrs)
ESUnpacker(const edm::ParameterSet &ps)
Definition: ESUnpacker.cc:10
void setEC(const int &EC)
Definition: ESKCHIPBlock.h:28
int zside_[4288][4]
Definition: ESUnpacker.h:75
void setBX(const int &BX)
int sourceID()
Identifier of the FED.
Definition: FEDHeader.cc:28
void setFlag1(const int &flag1)
Definition: ESKCHIPBlock.h:31
void setSize(int size)
Definition: ESDataFrame.cc:27
int siy() const
Definition: ESDetId.h:50
int seqtype_
Definition: ESUnpacker.h:57
int optoRX2_
Definition: ESUnpacker.h:63
bool moreHeaders()
Definition: FEDHeader.cc:36
void setMajorVersion(const int &vmajor)
void setSeqType(const int &seqType)
Word64 m1
Definition: ESUnpacker.h:73
int vminor_
Definition: ESUnpacker.h:59
void setLV1(const int &LV1)
Word64 m32
Definition: ESUnpacker.h:73
const ESSample & sample(int i) const
Definition: ESDataFrame.h:26
int j
Definition: DBlmapReader.cc:9
void setOptoBC1(const int &optoBC1)
Word64 m2
Definition: ESUnpacker.h:73
int optoRX1_
Definition: ESUnpacker.h:62
int zside() const
Definition: ESDetId.h:44
void setSample(int i, const ESSample &sam)
Definition: ESDataFrame.h:30
void interpretRawData(int fedId, const FEDRawData &rawData, ESRawDataCollection &dccs, ESLocalRawDataCollection &kchips, ESDigiCollection &digis)
Definition: ESUnpacker.cc:53
void setOptoRX0(const int &optoRX0)
void setMinorVersion(const int &vminor)
void setOptoEC(const int &OptoEC)
Definition: ESKCHIPBlock.h:30
void word2digi(int kchip, int kPACE[4], const Word64 &word, ESDigiCollection &digis)
Definition: ESUnpacker.cc:271
void setOptoRX2(const int &optoRX2)
int precision_
Definition: ESUnpacker.h:55
bool check()
Check that the header is OK.
Definition: FEDHeader.cc:64
std::string print(const Word64 &word) const
Definition: ESUnpacker.cc:318
void setFlag2(const int &flag2)
Definition: ESKCHIPBlock.h:32
int pl_[4288][4]
Definition: ESUnpacker.h:75
int lenght()
The length of the event fragment counted in 64-bit words including header and trailer.
Definition: FEDTrailer.cc:17
static bool validDetId(int istrip, int ixs, int iys, int iplane, int iz)
check if a valid index combination
Definition: ESDetId.cc:34
int bxID()
The bunch crossing number.
Definition: FEDHeader.cc:24
void push_back(unsigned int i)
Word64 m8
Definition: ESUnpacker.h:73
int FEch_[36]
Definition: ESUnpacker.h:64
void setBC(const int &BC)
Definition: ESKCHIPBlock.h:27
void setTriggerType(const int &trgType)
void setRunType(const int &runType)
const unsigned char * data() const
Return a const pointer to the beginning of the data buffer.
Definition: FEDRawData.cc:28
int plane() const
Definition: ESDetId.h:46
Word64 m6
Definition: ESUnpacker.h:73
int adc() const
get the ADC sample (singed 16 bits)
Definition: ESSample.h:18
const edm::ParameterSet pset_
Definition: ESUnpacker.h:42
int lvl1ID()
Level-1 event number generated by the TTC system.
Definition: FEDHeader.cc:20
int getOptoRX2() const
Word64 m16
Definition: ESUnpacker.h:73
unsigned long long Word64
Definition: ESUnpacker.h:30
int orbit_number_
Definition: ESUnpacker.h:50
void setDAC(const int &dac)
int trgtype_
Definition: ESUnpacker.h:58
Word64 m4
Definition: ESUnpacker.h:73