StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
trgUtil_2009.hh
1 //
2 // Pibero Djawotho <pibero@comp.tamu.edu>
3 // Texas A&M University Cyclotron Institute
4 // 7 Jan 2009
5 //
6 
7 #ifndef TRG_UTIL_2009_HH
8 #define TRG_UTIL_2009_HH
9 
10 #include <cstdio>
11 #include <cassert>
12 #include <byteswap.h>
13 #include <algorithm>
14 
15 using namespace std;
16 
17 #ifdef __ROOT__
18 #include "RTS/trg/include/trgDataDefs_46.h"
19 #include "RTS/trg/include/trgConfNum.h"
20 #else
21 #include "trgDataDefs_46.h"
22 #include "trgConfNum.h"
23 #endif
24 
25 inline unsigned short bswap16(unsigned short x) { return bswap_16(x); }
26 inline unsigned int bswap32(unsigned int x) { return bswap_32(x); }
27 
28 inline void swapTrgOfflen(TrgOfflen& trgOfflen)
29 {
30  trgOfflen.offset = bswap32(trgOfflen.offset);
31  trgOfflen.length = bswap32(trgOfflen.length);
32 }
33 
34 inline void swapEvtDescData(EvtDescData& evtDesc)
35 {
36  evtDesc.length = bswap32(evtDesc.length);
37  evtDesc.bunchXing_hi = bswap32(evtDesc.bunchXing_hi);
38  evtDesc.bunchXing_lo = bswap32(evtDesc.bunchXing_lo);
39  evtDesc.actionWdDetectorBitMask = bswap16(evtDesc.actionWdDetectorBitMask);
40  evtDesc.TrgToken = bswap16(evtDesc.TrgToken);
41  evtDesc.addBits = bswap16(evtDesc.addBits);
42  evtDesc.DSMInput = bswap16(evtDesc.DSMInput);
43  evtDesc.externalBusy = bswap16(evtDesc.externalBusy);
44  evtDesc.internalBusy = bswap16(evtDesc.internalBusy);
45  evtDesc.physicsWord = bswap16(evtDesc.physicsWord);
46  evtDesc.TriggerWord = bswap16(evtDesc.TriggerWord);
47  evtDesc.DSMAddress = bswap16(evtDesc.DSMAddress);
48  evtDesc.TCU_Mark = bswap16(evtDesc.TCU_Mark);
49  evtDesc.npre = bswap16(evtDesc.npre);
50  evtDesc.npost = bswap16(evtDesc.npost);
51 }
52 
53 inline void swapL1_DSM_Data(L1_DSM_Data& L1data)
54 {
55  L1data.length = bswap32(L1data.length);
56  transform(L1data.TOF,L1data.TOF+8,L1data.TOF,bswap16);
57  transform(L1data.VTX,L1data.VTX+8,L1data.VTX,bswap16);
58  transform(L1data.EMC,L1data.EMC+8,L1data.EMC,bswap16);
59  transform(L1data.BCdata,L1data.BCdata+16,L1data.BCdata,bswap16);
60  transform(L1data.specialTriggers,L1data.specialTriggers+8,L1data.specialTriggers,bswap16);
61  transform(L1data.FPD,L1data.FPD+8,L1data.FPD,bswap16);
62  transform(L1data.lastDSM,L1data.lastDSM+8,L1data.lastDSM,bswap16);
63 }
64 
65 inline void swapTrgSumData(TrgSumData& trgSum)
66 {
67  trgSum.length = bswap32(trgSum.length);
68  transform(trgSum.L1Sum,trgSum.L1Sum+2,trgSum.L1Sum,bswap32);
69  transform(trgSum.L2Sum,trgSum.L2Sum+2,trgSum.L2Sum,bswap32);
70  transform(trgSum.L1Result,trgSum.L1Result+32,trgSum.L1Result,bswap32);
71  transform(trgSum.L2Result,trgSum.L2Result+64,trgSum.L2Result,bswap32);
72  transform(trgSum.C2Result,trgSum.C2Result+64,trgSum.C2Result,bswap32);
73 }
74 
75 inline void swapDataBlock(DataBlock& data)
76 {
77  data.length = bswap32(data.length);
78 }
79 
80 inline void swapBELayerBlock(BELayerBlock& bc1)
81 {
82  bc1.length = bswap32(bc1.length);
83  transform(bc1.BEMClayer1,bc1.BEMClayer1+48,bc1.BEMClayer1,bswap16);
84  transform(bc1.EEMClayer1,bc1.EEMClayer1+16,bc1.EEMClayer1,bswap16);
85 }
86 
87 inline void swapMIXBlock(MIXBlock& mix)
88 {
89  mix.length = bswap32(mix.length);
90  transform(mix.FPDEastNSLayer1,mix.FPDEastNSLayer1+8,mix.FPDEastNSLayer1,bswap16);
91  transform(mix.TOFLayer1,mix.TOFLayer1+8,mix.TOFLayer1,bswap16);
92  transform(mix.TOF,mix.TOF+48,mix.TOF,bswap16);
93 }
94 
95 inline void swapBWestBlock(BWestBlock& bcw)
96 {
97  bcw.length = bswap32(bcw.length);
98 }
99 
100 inline void swapBEastBlock(BEastBlock& bce)
101 {
102  bce.length = bswap32(bce.length);
103 }
104 
105 inline void swapBBCBlock(BBCBlock& bbc)
106 {
107  bbc.length = bswap32(bbc.length);
108  transform(bbc.BBClayer1,bbc.BBClayer1+16,bbc.BBClayer1,bswap16);
109  transform(bbc.ZDClayer1,bbc.ZDClayer1+8,bbc.ZDClayer1,bswap16);
110  transform(bbc.VPD,bbc.VPD+8,bbc.VPD,bswap16);
111 }
112 
113 inline void swapFMSBlock(FMSBlock& fms)
114 {
115  fms.length = bswap32(fms.length);
116 }
117 
118 inline void swapQTBlock(QTBlock& qt)
119 {
120  qt.length = bswap32(qt.length);
121  qt.dataLoss = bswap32(qt.dataLoss);
122 }
123 
124 inline void swapDataBlockAtCrate(DataBlock& data, int crate)
125 {
126  switch (crate) {
127  case BC1_CONF_NUM:
128  {
129  BELayerBlock* bc1 = (BELayerBlock*)&data;
130  assert(strncmp(bc1->name, "BC1", 3) == 0);
131  swapBELayerBlock(*bc1);
132  break;
133  }
134  case MIX_CONF_NUM:
135  {
136  MIXBlock* mix = (MIXBlock*)&data;
137  assert(strncmp(mix->name, "MIX", 3) == 0);
138  swapMIXBlock(*mix);
139  break;
140  }
141  case BCW_CONF_NUM:
142  {
143  BWestBlock* bcw = (BWestBlock*)&data;
144  assert(strncmp(bcw->name, "BCW", 3) == 0);
145  swapBWestBlock(*bcw);
146  break;
147  }
148  case BCE_CONF_NUM:
149  {
150  BEastBlock* bce = (BEastBlock*)&data;
151  assert(strncmp(bce->name, "BCE", 3) == 0);
152  swapBEastBlock(*bce);
153  break;
154  }
155  case BBC_CONF_NUM:
156  {
157  BBCBlock* bbc = (BBCBlock*)&data;
158  assert(strncmp(bbc->name, "BBC", 3) == 0);
159  swapBBCBlock(*bbc);
160  break;
161  }
162  case FMS_CONF_NUM:
163  {
164  FMSBlock* fms = (FMSBlock*)&data;
165  assert(strncmp(fms->name, "FMS", 3) == 0);
166  swapFMSBlock(*fms);
167  break;
168  }
169  case QT1_CONF_NUM:
170  case QT2_CONF_NUM:
171  case QT3_CONF_NUM:
172  case QT4_CONF_NUM:
173  {
174  QTBlock* qt = (QTBlock*)&data;
175  assert(strncmp(qt->name, "QT1", 3) == 0 ||
176  strncmp(qt->name, "QT2", 3) == 0 ||
177  strncmp(qt->name, "QT3", 3) == 0 ||
178  strncmp(qt->name, "QT4", 3) == 0);
179  swapQTBlock(*qt);
180  break;
181  }
182  default:
183  {
184  swapDataBlock(data);
185  break;
186  }
187  }
188 }
189 
190 inline void swapTriggerDataBlk(TriggerDataBlk& trgData)
191 {
192  // Swap format version, total trigger length and event number
193 
194  trgData.FormatVersion = bswap32(trgData.FormatVersion);
195  //assert(trgData.FormatVersion == FORMAT_VERSION);
196  trgData.totalTriggerLength = bswap32(trgData.totalTriggerLength);
197  trgData.eventNumber = bswap32(trgData.eventNumber);
198 
199  // Swap event descriptor
200 
201  swapTrgOfflen(trgData.EventDesc_ofl);
202  EvtDescData* evtDesc = (EvtDescData*)((int)&trgData+trgData.EventDesc_ofl.offset);
203  assert(strncmp(evtDesc->name, "EVD", 3) == 0 || strncmp(evtDesc->name, "EVT", 3) == 0);
204  swapEvtDescData(*evtDesc);
205 
206  // Swap L1 DSM data
207 
208  swapTrgOfflen(trgData.L1_DSM_ofl);
209  L1_DSM_Data* L1data = (L1_DSM_Data*)((int)&trgData+trgData.L1_DSM_ofl.offset);
210  assert(strncmp(L1data->name, "L1DS", 4) == 0);
211  swapL1_DSM_Data(*L1data);
212 
213  // Swap trigger summary
214 
215  swapTrgOfflen(trgData.Summary_ofl);
216  TrgSumData* trgSum = (TrgSumData*)((int)&trgData+trgData.Summary_ofl.offset);
217  assert(strncmp(trgSum->name, "TSUM", 4) == 0);
218  swapTrgSumData(*trgSum);
219 
220  // Swap data in crates for main bXing
221 
222  for (int crate = 0; crate < MAX_OFFLEN; ++crate) {
223  swapTrgOfflen(trgData.MainX[crate]);
224  if (trgData.MainX[crate].offset && trgData.MainX[crate].length) {
225  DataBlock* data = (DataBlock*)((int)&trgData+trgData.MainX[crate].offset);
226  swapDataBlockAtCrate(*data, crate);
227  }
228  }
229 
230  // Swap data in crates for pre/post bXing
231 
232  const int nentries = evtDesc->npre + evtDesc->npost;
233 
234  for (int i = 0; i < nentries; ++i) {
235  trgData.PrePostList[i] = bswap32(trgData.PrePostList[i]);
236  TrgOfflen* PrePostX = (TrgOfflen*)((int)&trgData+trgData.PrePostList[i]);
237  for (int crate = 0; crate < MAX_OFFLEN; ++crate) {
238  swapTrgOfflen(PrePostX[crate]);
239  if (PrePostX[crate].offset && PrePostX[crate].length) {
240  DataBlock* data = (DataBlock*)((int)&trgData+PrePostX[crate].offset);
241  swapDataBlockAtCrate(*data, crate);
242  }
243  }
244  }
245 }
246 
247 inline void printName(const char* name)
248 {
249  printf("name: ");
250  for (int i = 0; i < 4; ++i) putchar(name[i]);
251  printf("\n");
252 }
253 
254 inline void printEvtDescData(const EvtDescData& evtDesc)
255 {
256  printName(evtDesc.name);
257  printf("TrgDataFmtVer: 0x%x\n", evtDesc.TrgDataFmtVer);
258  printf("length: %d\n", evtDesc.length);
259  printf("bunchXing_hi: %d\n", evtDesc.bunchXing_hi);
260  printf("bunchXing_lo: %d\n", evtDesc.bunchXing_lo);
261  printf("actionWdDetectorBitMask: %d\n", evtDesc.actionWdDetectorBitMask);
262  printf("actionWdTrgCommand: %d\n", evtDesc.actionWdTrgCommand);
263  printf("actionWdDaqCommand: %d\n", evtDesc.actionWdDaqCommand);
264  printf("TrgToken: %d\n", evtDesc.TrgToken);
265  printf("addBits: %d\n", evtDesc.addBits);
266  printf("DSMInput: %d\n", evtDesc.DSMInput);
267  printf("externalBusy: %d\n", evtDesc.externalBusy);
268  printf("internalBusy: %d\n", evtDesc.internalBusy);
269  printf("physicsWord: %d\n", evtDesc.physicsWord);
270  printf("TriggerWord: %d\n", evtDesc.TriggerWord);
271  printf("DSMAddress: %d\n", evtDesc.DSMAddress);
272  printf("TCU_Mark: %d\n", evtDesc.TCU_Mark);
273  printf("npre: %d\n", evtDesc.npre);
274  printf("npost: %d\n", evtDesc.npost);
275 }
276 
277 inline void printL1_DSM_Data(const L1_DSM_Data& L1data)
278 {
279  printName(L1data.name);
280  printf("length: %d\n", L1data.length);
281  printf("TOF: ");
282  for (int i = 0; i < 8; ++i) printf("%04x ", L1data.TOF[i]);
283  printf("\n");
284  printf("VTX: ");
285  for (int i = 0; i < 8; ++i) printf("%04x ", L1data.VTX[i]);
286  printf("\n");
287  printf("EMC: ");
288  for (int i = 0; i < 8; ++i) printf("%04x ", L1data.EMC[i]);
289  printf("\n");
290  printf("BCdata:\n");
291  for (int i = 0; i < 16; ++i) {
292  printf("%04x ", L1data.BCdata[i]);
293  if (i % 8 == 7) printf("\n");
294  }
295  printf("specialTriggers: ");
296  for (int i = 0; i < 8; ++i) printf("%04x ", L1data.specialTriggers[i]);
297  printf("\n");
298  printf("FPD: ");
299  for (int i = 0; i < 8; ++i) printf("%04x ", L1data.FPD[i]);
300  printf("\n");
301  printf("lastDSM: ");
302  for (int i = 0; i < 8; ++i) printf("%04x ", L1data.lastDSM[i]);
303  printf("\n");
304 }
305 
306 inline void printTrgSumData(const TrgSumData& trgSum)
307 {
308  printName(trgSum.name);
309  printf("length: %d\n", trgSum.length);
310  printf("L1Sum: ");
311  for (int i = 0; i < 2; ++i) printf("%08x ", trgSum.L1Sum[i]);
312  printf("\n");
313  printf("L2Sum: ");
314  for (int i = 0; i < 2; ++i) printf("%08x ", trgSum.L2Sum[i]);
315  printf("\n");
316  printf("L1Result:\n");
317  for (int i = 0; i < 32; ++i) {
318  printf("%08x ", trgSum.L1Result[i]);
319  if (i % 8 == 7) printf("\n");
320  }
321  printf("L2Result:\n");
322  for (int i = 0; i < 64; ++i) {
323  printf("%08x ", trgSum.L2Result[i]);
324  if (i %8 == 7) printf("\n");
325  }
326  printf("C2Result:\n");
327  for (int i = 0; i < 64; ++i) {
328  printf("%08x ", trgSum.C2Result[i]);
329  if (i % 8 == 7) printf("\n");
330  }
331 }
332 
333 inline void printMIXBlock(const MIXBlock& mix)
334 {
335  printName(mix.name);
336  printf("length: %d\n", mix.length);
337  printf("FPDEastNSLayer1: ");
338  for (int i = 0; i < 8; ++i) printf("%04x ", mix.FPDEastNSLayer1[i]);
339  printf("\n");
340  printf("MTD_P2PLayer1:\n");
341  for (int i = 0; i < 16; ++i) {
342  printf("%x ", mix.MTD_P2PLayer1[i]);
343  if (i % 8 == 7) printf("\n");
344  }
345  printf("TOFLayer1: ");
346  for (int i = 0; i < 8; ++i) printf("%04x ", mix.TOFLayer1[i]);
347  printf("\n");
348  printf("TOF:\n");
349  for (int i = 0; i < 48; ++i) {
350  printf("%04x ", mix.TOF[i]);
351  if (i % 8 == 7) printf("\n");
352  }
353 }
354 
355 inline void printBBCBlock(const BBCBlock& bbc)
356 {
357  printName(bbc.name);
358  printf("length: %d\n", bbc.length);
359  printf("BBClayer1:\n");
360  for (int i = 0; i < 16; ++i) {
361  printf("%04x ", bbc.BBClayer1[i]);
362  if (i % 8 == 7) printf("\n");
363  }
364  printf("ZDClayer1:\n");
365  for (int i = 0; i < 8; ++i) printf("%04x ", bbc.ZDClayer1[i]);
366  printf("\n");
367  printf("VPD:\n");
368  for (int i = 0; i < 8; ++i) printf("%04x ", bbc.VPD[i]);
369  printf("\n");
370 }
371 
372 inline void printFMSBlock(const FMSBlock& fms)
373 {
374  printName(fms.name);
375  printf("length: %d\n", fms.length);
376  for (int i = 0; i < 256; ++i) {
377  printf("%02x ", fms.FMS[i]);
378  if (i % 16 == 15) printf("\n");
379  }
380 }
381 
382 inline void printQTBlock(const QTBlock& qt)
383 {
384  printName(qt.name);
385  printf("length: %d\n", qt.length);
386  printf("dataLoss: %d\n", qt.dataLoss);
387  int len = qt.length-4;
388  unsigned char* data = (unsigned char*)&qt.data;
389  for (int i = 0; i < len; ++i) {
390  printf("%02x ", data[i]);
391  if (i % 16 == 15) printf("\n");
392  }
393 }
394 
395 inline void printBELayerBlock(const BELayerBlock& bc1)
396 {
397  printName(bc1.name);
398  printf("length: %d\n", bc1.length);
399  printf("BEMClayer1:\n");
400  for (int i = 0; i < 48; ++i) {
401  printf("%04x ", bc1.BEMClayer1[i]);
402  if (i % 8 == 7) printf("\n");
403  }
404  printf("EEMClayer1:\n");
405  for (int i = 0; i < 16; ++i) {
406  printf("%04x ", bc1.EEMClayer1[i]);
407  if (i % 8 == 7) printf("\n");
408  }
409  printf("EEMC:\n");
410  for (int i = 0; i < 144; ++i) {
411  printf("%02x ", bc1.EEMC[i]);
412  if (i % 16 == 15) printf("\n");
413  }
414 }
415 
416 inline void printBWestBlock(const BWestBlock& bcw)
417 {
418  printName(bcw.name);
419  printf("length: %d\n", bcw.length);
420  printf("BEMCWest:\n");
421  for (int i = 0; i < 240; ++i) {
422  printf("%02x ", bcw.BEMCWest[i]);
423  if (i % 16 == 15) printf("\n");
424  }
425 }
426 
427 inline void printBEastBlock(const BEastBlock& bce)
428 {
429  printName(bce.name);
430  printf("length: %d\n", bce.length);
431  printf("BEMCEast:\n");
432  for (int i = 0; i < 240; ++i) {
433  printf("%02x ", bce.BEMCEast[i]);
434  if (i % 16 == 15) printf("\n");
435  }
436 }
437 
438 inline void printDataBlock(const DataBlock& dataBlock)
439 {
440  printName(dataBlock.name);
441  printf("length: %d\n", dataBlock.length);
442 }
443 
444 #endif // TRG_UTIL_2009_HH