StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
trgReader21.cxx
1 #include <stdio.h>
2 #include <string.h>
3 #include <arpa/inet.h>
4 
5 #include <rtsLog.h>
6 
7 #include <rtsSystems.h>
8 #include <rts.h> // fror the sake of swap32 entry
9 
10 // This reader works for TRG_VERSIONS: 0x21 (FY04 run); 0x22 (Nov 04)
11 
12 // one needs to diffuse the crummy trigger stuff first
13 #define TRG_VERSION 0x21
14 #include <daqFormats.h>
15 
16 
17 
18 #include "daq_trg.h"
19 
20 // this maps from the raw data to the following representation
21 // sector1,slat1,ch1 ... ch5, sector1,slat2,ch1.... ch5, sector2,slat1,ch1...
22 
23 static unsigned char ctbMap[240] = {
24 7, 6, 5, 4, 3, 23, 22, 21, 20, 19,
25 2, 1, 0, 15, 14, 18, 17, 16, 31, 30,
26 13, 12, 11, 10, 9, 29, 28, 27, 26, 25,
27 39, 38, 37, 36, 35, 55, 54, 53, 52, 51,
28 34, 33, 32, 47, 46, 50, 49, 48, 63, 62,
29 45, 44, 43, 42, 41, 61, 60, 59, 58, 57,
30 71, 70, 69, 68, 67, 87, 86, 85, 84, 83,
31 66, 65, 64, 79, 78, 82, 81, 80, 95, 94,
32 77, 76, 75, 74, 73, 93, 92, 91, 90, 89,
33 103, 102, 101, 100, 99, 119, 118, 117, 116, 115,
34 98, 97, 96, 111, 110, 114, 113, 112, 127, 126,
35 109, 108, 107, 106, 105, 125, 124, 123, 122, 121,
36 135, 134, 133, 132, 131, 151, 150, 149, 148, 147,
37 130, 129, 128, 143, 142, 146, 145, 144, 159, 158,
38 141, 140, 139, 138, 137, 157, 156, 155, 154, 153,
39 167, 166, 165, 164, 163, 183, 182, 181, 180, 179,
40 162, 161, 160, 175, 174, 178, 177, 176, 191, 190,
41 173, 172, 171, 170, 169, 189, 188, 187, 186, 185,
42 199, 198, 197, 196, 195, 215, 214, 213, 212, 211,
43 194, 193, 192, 207, 206, 210, 209, 208, 223, 222,
44 205, 204, 203, 202, 201, 221, 220, 219, 218, 217,
45 231, 230, 229, 228, 227, 247, 246, 245, 244, 243,
46 226, 225, 224, 239, 238, 242, 241, 240, 255, 254,
47 237, 236, 235, 234, 233, 253, 252, 251, 250, 249,
48 } ;
49 
50 
51 static unsigned char mwcMap[96] = {
52 71, 70, 69, 68, 67, 66, 65, 64, 79, 78, 77, 76,
53 95, 94, 93, 92, 87, 86, 85, 84, 83, 82, 81, 80,
54 99, 98, 97, 96, 111, 110, 109, 108, 103, 102, 101, 100,
55 119, 118, 117, 116, 115, 114, 113, 112, 127, 126, 125, 124,
56 7, 6, 5, 4, 3, 2, 1, 0, 15, 14, 13, 12,
57 31, 30, 29, 28, 23, 22, 21, 20, 19, 18, 17, 16,
58 35, 34, 33, 32, 47, 46, 45, 44, 39, 38, 37, 36,
59 55, 54, 53, 52, 51, 50, 49, 48, 63, 62, 61, 60,
60 } ;
61 
62 
63 static TrgSumData trg_sum ;
64 
65 // read the Trigger RAW data
66 int trgReader21(char *arg, struct trg_t *trg)
67 {
68  int i ;
69  int sdes, ssum, sraw ;
70  int len ;
71 
72 
73  struct TRGD *trgd = (struct TRGD *)arg ;
74 
75  int swap = 0;
76  if(trgd->bh.byte_order != DAQ_RAW_FORMAT_ORDER) swap=1;
77 
78  len = swap ? swap32(trgd->bh.length)*4 : trgd->bh.length*4;
79 
80  sdes = sizeof(trgd->bh) + sizeof(trgd->desc) ;
81  ssum = sdes + sizeof(trgd->sum) ;
82  sraw = ssum + sizeof(trgd->raw[0]) ;
83 
84  if(len < ssum) {
85  LOG(NOTE,"No TRG Summaries, or Raw...",0,0,0,0,0) ;
86  return 0 ;
87  }
88  else if(len < sraw) {
89  LOG(NOTE,"TRG: Summaries ONLY!",0,0,0,0,0) ;
90  return 0 ;
91  }
92 
93  LOG(DBG,"len %d, desc len %d, sum len %d, raw trg 0 len %d, all raw len %d",
94  len,sizeof(trgd->desc),sizeof(trgd->sum),sizeof(trgd->raw[0]),sizeof(trgd->raw)) ;
95 
96  LOG(DBG,"evt desc bytes %d, sum bytes %d, raw bytes %d",trgd->desc.TCUdataBytes,
97  trgd->sum.TrgSumBytes,trgd->raw[0].RawDetBytes,0,0) ;
98 
99 
100  trg->npre = swap ? swap16(trgd->desc.npre) : trgd->desc.npre ;
101  trg->npost = swap ? swap16(trgd->desc.npost) : trgd->desc.npost ;
102 
103  trg->xing_lo = swap ? swap32(trgd->desc.bunchXing_lo) : trgd->desc.bunchXing_lo ;
104  trg->xing_hi = swap ? swap32(trgd->desc.bunchXing_hi) : trgd->desc.bunchXing_hi ;
105 
106  trg->phys_word = swap ? swap16(trgd->desc.physicsWord) : trgd->desc.physicsWord ;
107  trg->trg_word = swap ? swap16(trgd->desc.TriggerWord) : trgd->desc.TriggerWord ;
108 
109  trg->trgd = (void *) trgd ;
110 
111 // what the hell did I mean by this "40 is spurious"???
112 // int trg_length = len - sizeof(struct bankHeader) - 40 ; // 40 is supurious
113 
114  int trg_length = len - sizeof(struct bankHeader) ;
115  int exp_length = sizeof(EvtDescData) + sizeof(trgd->sum) + sizeof(trgd->raw[0]) +
116  sizeof(trgd->raw[0])*trg->npre + sizeof(trgd->raw[0])*trg->npost ;
117 
118  if(trg_length != exp_length) {
119  LOG(NOTE,"Trigger data: is %d, expect %d bytes, trg->npre %d, trg->npost %d",
120  trg_length, exp_length,trg->npre,trg->npost,0) ;
121  }
122  else {
123  LOG(DBG,"Trigger data: is %d, expect %d bytes, trg->npre %d, trg->npost %d",
124  trg_length, exp_length,trg->npre,trg->npost,0) ;
125  }
126 
127  // get the last DSM aka TCU bits
128  trg->tcubits = swap ? swap16(trgd->desc.DSMInput) : trgd->desc.DSMInput;
129  trg->detlive = swap ? swap16(trgd->desc.externalBusy) : trgd->desc.externalBusy ;
130 
131 
132  LOG(DBG,"TrgDataFmtVer 0x%X, 0x%04X %c %c", trgd->desc.TrgDataFmtVer,
133  trgd->sum.TrgSumHeader,
134  trgd->raw[0].CTBdataHeader[0],
135  trgd->raw[0].CTBdataHeader[1],0) ;
136 
137  LOG(DBG,"TrgSumBytes %d, 0x%04X %c %c %c", trgd->sum.TrgSumBytes,
138  trgd->sum.L0SumHeader,
139  trgd->raw[0].RawDetHeader[0],
140  trgd->raw[0].RawDetHeader[1],0) ;
141 
142 
143 
144 
145  // Trigger Summary
146  // set pointer & clear all to 0
147  trg->trg_sum = (void *) &trg_sum ;
148  memset(&trg_sum,0,sizeof(trg_sum)) ;
149 
150  for(i=0;i<2;i++) {
151  trg_sum.L1Sum[i] = swap ? swap32(trgd->sum.L1Sum[i]) : trgd->sum.L1Sum[i] ;
152  trg_sum.L2Sum[i] = swap ? swap32(trgd->sum.L2Sum[i]) : trgd->sum.L2Sum[i] ;
153  }
154 
155 
156 // Arghhh, I wish I had some generic programming tools...
157 #define REAL_BAD_HACK(x) trg_sum.DSMdata.x = (swap ? swap16(trgd->sum.DSMdata.x) : trgd->sum.DSMdata.x)
158 
159  for(i=0;i<32;i++) {
160  REAL_BAD_HACK(CPA[i]) ;
161  }
162  for(i=0;i<16;i++) {
163  REAL_BAD_HACK(BCdata[i]) ;
164  }
165 
166  for(i=0;i<8;i++) {
167  REAL_BAD_HACK(quadDSM[i]) ;
168  REAL_BAD_HACK(lastDSM[i]) ;
169  REAL_BAD_HACK(VTX[i]) ;
170  REAL_BAD_HACK(EMC[i]) ;
171  REAL_BAD_HACK(specialTriggers[i]) ;
172  REAL_BAD_HACK(FPD[i]) ;
173  }
174 
175 #undef REAL_BAD_HACK
176 
177 
178  // RAW data stuff. Just the crossing - NOT pre/post!
179 
180 
181  for(i=0;i<240;i++) {
182  trg->CTB[i] = trgd->raw[0].CTB[ctbMap[i]] ;
183  }
184 
185  for(i=0;i<96;i++) {
186  trg->MWC[i] = trgd->raw[0].MWC[mwcMap[i]] ;
187  }
188 
189  // east
190  for(i=0;i<240;i++) {
191  trg->BEMC[0][i] = trgd->raw[0].BEMCEast[i] ;
192  }
193  // west
194  for(i=0;i<240;i++) {
195  trg->BEMC[1][i] = trgd->raw[0].BEMCWest[i] ;
196  }
197  // layer1
198  for(i=0;i<48;i++) {
199  trg->BEMC_l1[i] = swap ? swap16(trgd->raw[0].BEMClayer1[i]) : trgd->raw[0].BEMClayer1[i] ;
200  }
201 
202 
203 
204  for(i=0;i<144;i++) {
205  trg->EEMC[i] = trgd->raw[0].EEMC[i] ;
206  }
207  for(i=0;i<16;i++) {
208  trg->EEMC_l1[i] = swap ? swap16(trgd->raw[0].EEMClayer1[i]) : trgd->raw[0].EEMClayer1[i];
209  }
210 
211  // FPD:
212  // first index is 0 East, 1 West
213  // second index is 0 NortSouth, 1 TopBottom
214  // East, NS
215  for(i=0;i<112;i++) {
216  trg->FPD[0][0][i] = trgd->raw[0].FPDEastNSLayer0[i] ;
217  }
218  for(i=0;i<8;i++) {
219  trg->FPD_l1[0][0][i] = swap ? swap16(trgd->raw[0].FPDEastNSLayer1[i]) : trgd->raw[0].FPDEastNSLayer1[i] ;
220  }
221  // East, TB
222  for(i=0;i<64;i++) {
223  trg->FPD[0][1][i] = trgd->raw[0].FPDEastTBLayer0[i] ;
224  }
225  for(i=0;i<8;i++) {
226  trg->FPD_l1[0][1][i] = swap ? swap16(trgd->raw[0].FPDEastTBLayer1[i]) : trgd->raw[0].FPDEastTBLayer1[i] ;
227  }
228  // West, NS
229  for(i=0;i<112;i++) {
230  trg->FPD[1][0][i] = trgd->raw[0].FPDWestNSLayer0[i] ;
231  }
232  for(i=0;i<8;i++) {
233  trg->FPD_l1[1][0][i] = swap ? swap16(trgd->raw[0].FPDWestNSLayer1[i]) : trgd->raw[0].FPDWestNSLayer1[i];
234  }
235  // West, TB
236  for(i=0;i<64;i++) {
237  trg->FPD[1][1][i] = trgd->raw[0].FPDWestTBLayer0[i] ;
238  }
239  for(i=0;i<8;i++) {
240  trg->FPD_l1[1][1][i] = swap ? swap16(trgd->raw[0].FPDWestTBLayer1[i]) : trgd->raw[0].FPDWestTBLayer1[i];
241  }
242 
243 
244 
245  // get the size from trgStructures.h
246  for(i=0;i<(int)sizeof(trgd->raw[0].BBC)/(int)sizeof(trgd->raw[0].BBC[0]);i++) {
247  trg->BBC[i] = trgd->raw[0].BBC[i] ;
248  }
249 
250  for(i=0;i<16;i++) {
251  trg->BBC_l1[i] = swap ? swap16(trgd->raw[0].BBClayer1[i]) : trgd->raw[0].BBClayer1[i] ;
252  }
253 
254  for(i=0;i<16;i++) {
255  trg->ZDC[i] = trgd->raw[0].ZDC[i] ;
256  }
257  for(i=0;i<8;i++) {
258  trg->ZDC_l1[i] = swap ? swap16(trgd->raw[0].ZDClayer1[i]) : trgd->raw[0].ZDClayer1[i];
259  }
260 
261  for(i=0;i<32;i++) {
262  trg->ZDCSMD[i] = trgd->raw[0].ZDCSMD[i] ;
263  }
264 
265 
266 /*
267  int j ;
268  for(j=0;j<11;j++) {
269  for(i=0;i<16;i++) {
270  printf("pre %d, ZDC %d = %u\n",j,i,trgd->raw[j].ZDC[i]) ;
271  }
272  for(i=0;i<8;i++) {
273  printf("pre %d, ZDC_l1 %d = %u\n",j,i,b2h16(trgd->raw[j].ZDClayer1[i])) ;
274  }
275  }
276 */
277 
278  return len ;
279 }
280 
Definition: daq_trg.h:9