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