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  std::cout<<"ESUnpacker::ESUnpacker : Look up table file can not be found in "<<lookup_.fullPath().c_str()<<std::endl;
46  }
47 
48 }
49 
51 }
52 
53 void ESUnpacker::interpretRawData(int fedId, const FEDRawData & rawData, ESRawDataCollection & dccs, ESLocalRawDataCollection & kchips, ESDigiCollection & digis) {
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, ttcEC;
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  std::cout<<"[ESUnpacker]: FED Header candidate. Is header? "<< ESHeader.check();
81  if (ESHeader.check())
82  std::cout <<". BXID: "<<bx_<<" SourceID : "<<fedId_<<" L1ID: "<<lv1_<<std::endl;
83  else std::cout<<" WARNING!, this is not a ES Header"<<std::endl;
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  std::cout<<"[ESUnpacker]: FED Trailer candidate. Is trailer? "<<ESTrailer.check();
124  if (ESTrailer.check())
125  std::cout<<". Length of the ES event: "<<ESTrailer.lenght()<<std::endl;
126  else std::cout<<" WARNING!, this is not a ES Trailer"<<std::endl;
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_) std::cout<<"DCC : "<<print(*word)<<std::endl;
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 
212  // Event data
213  int opto = 0;
214  for (const Word64* word=(header+dccWords+1); word!=trailer; ++word) {
215  if (debug_) std::cout<<"Event : "<<print(*word)<<std::endl;
216 
217  head = (*word >> 60) & m4;
218 
219  if (head == 12) {
220  if ((opto==1 && ESDCCHeader.getOptoRX0()==129) || (opto==2 && ESDCCHeader.getOptoRX1()==129) || (opto==3 && ESDCCHeader.getOptoRX2()==129))
221  word2digi(kid, kPACE, *word, digis);
222  } else if (head == 9) {
223  kid = (*word >> 2) & 0x07ff;
224  kPACE[0] = (*word >> 16) & m1;
225  kPACE[1] = (*word >> 17) & m1;
226  kPACE[2] = (*word >> 18) & m1;
227  kPACE[3] = (*word >> 19) & m1;
228  kFlag2 = (*word >> 20) & m4;
229  kFlag1 = (*word >> 24) & m8;
230  kBC = (*word >> 32) & m16;
231  kEC = (*word >> 48) & m8;
232 
233  ESKCHIPBlock ESKCHIP;
234  ESKCHIP.setId(kid);
235  ESKCHIP.setBC(kBC);
236  ESKCHIP.setEC(kEC);
237  ESKCHIP.setOptoBC(optoBC);
238  ESKCHIP.setOptoEC(optoEC);
239  ESKCHIP.setFlag1(kFlag1);
240  ESKCHIP.setFlag2(kFlag2);
241  kchips.push_back(ESKCHIP);
242  } else if (head == 6) {
243  ttcEC = (*word >> 0) & m32;
244  optoBC = (*word >> 32) & m16;
245  optoEC = (*word >> 48) & m8;
246  if (opto==0) ESDCCHeader.setOptoBC0(optoBC);
247  else if (opto==1) ESDCCHeader.setOptoBC1(optoBC);
248  else if (opto==2) ESDCCHeader.setOptoBC2(optoBC);
249  opto++;
250  }
251  }
252 
253  dccs.push_back(ESDCCHeader);
254 }
255 
256 void ESUnpacker::word2digi(int kid, int kPACE[4], const Word64 & word, ESDigiCollection & digis)
257 {
258 
259  int pace = (word >> 53) & m2;
260  if (kPACE[pace]==0) return;
261  if (kid > 1511 || kid < 1) return;
262 
263  int adc[3];
264  adc[0] = (word >> 0) & m16;
265  adc[1] = (word >> 16) & m16;
266  adc[2] = (word >> 32) & m16;
267  int strip = (word >> 48) & m5;
268 
269  if (debug_) std::cout<<kid<<" "<<strip<<" "<<pace<<" "<<adc[0]<<" "<<adc[1]<<" "<<adc[2]<<std::endl;
270 
271  int zside, plane, ix, iy;
272  zside = zside_[kid-1][pace];
273  plane = pl_[kid-1][pace];
274  ix = x_[kid-1][pace];
275  iy = y_[kid-1][pace];
276 
277  // convert strip number from electronics id to detector id
278  if (vmajor_ == 4 && (vminor_==2 || vminor_==3)) {
279  if (zside == 1 && plane == 1 && iy <= 20) strip = 31 - strip;
280  if (zside == 1 && plane == 2 && ix > 20) strip = 31 - strip;
281  if (zside == -1 && plane == 1 && iy > 20) strip = 31 - strip;
282  if (zside == -1 && plane == 2 && ix <= 20) strip = 31 - strip;
283  }
284 
285  if (debug_) std::cout<<"DetId : "<<zside<<" "<<plane<<" "<<ix<<" "<<iy<<" "<<strip+1<<std::endl;
286 
287  if (ESDetId::validDetId(strip+1, ix, iy, plane, zside)) {
288 
289  ESDetId detId(strip+1, ix, iy, plane, zside);
290  ESDataFrame df(detId);
291  df.setSize(3);
292 
293  for (int i=0; i<3; i++) df.setSample(i, adc[i]);
294 
295  digis.push_back(df);
296 
297  if (debug_)
298  std::cout<<"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()<<std::endl;
299  }
300 
301 }
302 
303 std::string ESUnpacker::print(const Word64 & word) const
304 {
305  std::ostringstream str;
306  str << "Word64: " << reinterpret_cast<const std::bitset<64>&> (word);
307  return str.str();
308 }
309 
int adc(sample_type sample)
get the ADC sample (12 bits)
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:41
void setGain(const int &gain)
edm::FileInPath lookup_
Definition: ESUnpacker.h:67
bool check()
Definition: FEDTrailer.cc:66
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
void strip(std::string &input, const std::string &blanks=" \n\t")
Definition: stringTools.cc: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:39
int getOptoRX0() const
void push_back(T const &t)
Word64 m5
Definition: ESUnpacker.h:73
int getOptoRX1() const
int six() const
Definition: ESDetId.h:37
int x_[4288][4]
Definition: ESUnpacker.h:75
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:30
void setFlag1(const int &flag1)
Definition: ESKCHIPBlock.h:31
void setSize(int size)
Definition: ESDataFrame.cc:16
int siy() const
Definition: ESDetId.h:39
int seqtype_
Definition: ESUnpacker.h:57
int optoRX2_
Definition: ESUnpacker.h:63
bool moreHeaders()
Definition: FEDHeader.cc:38
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:23
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:33
void setSample(int i, const ESSample &sam)
Definition: ESDataFrame.h:27
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:256
void setOptoRX2(const int &optoRX2)
int precision_
Definition: ESUnpacker.h:55
bool check()
Check that the header is OK.
Definition: FEDHeader.cc:66
std::string print(const Word64 &word) const
Definition: ESUnpacker.cc:303
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:19
static bool validDetId(int istrip, int ixs, int iys, int iplane, int iz)
check if a valid index combination
Definition: ESDetId.cc:50
int bxID()
The bunch crossing number.
Definition: FEDHeader.cc:26
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:29
int plane() const
Definition: ESDetId.h:35
Word64 m6
Definition: ESUnpacker.h:73
tuple cout
Definition: gather_cfg.py:41
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:22
int getOptoRX2() const
Word64 m16
Definition: ESUnpacker.h:73
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