StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
trgReader32.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 0x32
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 static TrgSumData trg_sum ;
52 
53 // read the Trigger RAW data
54 int trgReader32(char *arg, int bytes, int swap, struct trg_t *trg)
55 {
56  int i ;
57  int sdes, ssum, sraw ;
58 
59  EvtDescData *desc = (EvtDescData *)arg;
60  TrgSumData *sum = (TrgSumData *)(arg + sizeof(EvtDescData));
61  //int swap = 1;
62 
63  sdes = sizeof(EvtDescData) ;
64  ssum = sdes + sizeof(TrgSumData) ;
65 
66  if(bytes < ssum + 4) {
67  LOG(NOTE,"No TRG Summaries, or Raw...",0,0,0,0,0) ;
68  return 0 ;
69  }
70 
71  RawTrgDet *raw = (RawTrgDet *)(arg + sizeof(EvtDescData) + sizeof(TrgSumData));
72  int rdb = swap ? swap16(raw[0].RawDetBytes) : raw[0].RawDetBytes;
73 
74  sraw = ssum + rdb;
75 
76  int minsize = sizeof(EvtDescData) + sizeof(TrgSumData) + 1772;
77 
78 
79  if(sraw < minsize) {
80  LOG(NOTE, "Trigger bank size smaller than minimum %d vs %d", sraw, minsize);
81  return 0;
82  }
83 
84  if(bytes < sraw) {
85  LOG(NOTE,"Trigger Bank too small %d vs %d",bytes,sraw);
86  return 0 ;
87  }
88 
89 
90 // LOG(DBG,"evt desc bytes %d, sum bytes %d, raw bytes %d",desc->TCUdataBytes,
91 // sum->TrgSumBytes,raw[0].RawDetBytes,0,0) ;
92  LOG(DBG,"evt desc bytes %d, sum bytes %d, raw bytes %d",sizeof(EvtDescData),sizeof(TrgSumData),rdb,0) ;
93 
94 
95  trg->npre = swap ? swap16(desc->npre) : desc->npre ;
96  trg->npost = swap ? swap16(desc->npost) : desc->npost ;
97 
98  trg->xing_lo = swap ? swap32(desc->bunchXing_lo) : desc->bunchXing_lo ;
99  trg->xing_hi = swap ? swap32(desc->bunchXing_hi) : desc->bunchXing_hi ;
100 
101  trg->phys_word = swap ? swap16(desc->physicsWord) : desc->physicsWord ;
102  trg->trg_word = swap ? swap16(desc->TriggerWord) : desc->TriggerWord ;
103 
104 // what the hell did I mean by this "40 is spurious"???
105 // int trg_length = len - sizeof(struct bankHeader) - 40 ; // 40 is supurious
106 
107  unsigned int trg_length = bytes;
108 
109  /*
110  int exp_length = sizeof(EvtDescData) + sizeof(trgd->sum) + sizeof(trgd->raw[0]) +
111  sizeof(trgd->raw[0])*trg->npre + sizeof(trgd->raw[0])*trg->npost ;
112 
113  if(trg_length != exp_length) {
114  LOG(NOTE,"Trigger data: is %d, expect %d bytes, trg->npre %d, trg->npost %d",
115  trg_length, exp_length,trg->npre,trg->npost,0) ;
116  }
117  else {
118  LOG(DBG,"Trigger data: is %d, expect %d bytes, trg->npre %d, trg->npost %d",
119  trg_length, exp_length,trg->npre,trg->npost,0) ;
120  } */
121 
122  if(trg_length > sizeof(TrgDataType)) {
123  LOG(ERR, "Trigger data is too large: %d bytes more than max of %d",trg_length,sizeof(TrgDataType));
124  }
125 
126  if(trg_length < sizeof(EvtDescData) + sizeof(TrgSumData)) {
127  LOG(ERR, "Trigger data is too small: %d bytes less than min of %d",trg_length,(sizeof(EvtDescData) +sizeof(TrgSumData)));
128  }
129 
130  // get the last DSM aka TCU bits
131  trg->tcubits = swap ? swap16(desc->DSMInput) : desc->DSMInput;
132  trg->detlive = swap ? swap16(desc->externalBusy) : desc->externalBusy ;
133 
134 
135  LOG(DBG,"TrgDataFmtVer 0x%X, 0x%04X %c %c", desc->TrgDataFmtVer,
136  sum->TrgSumHeader,
137  raw[0].CTBdataHeader[0],
138  raw[0].CTBdataHeader[1],0) ;
139 
140  LOG(DBG,"TrgSumBytes %d, 0x%04X %c %c", sum->TrgSumBytes,
141  sum->L0SumHeader,
142  raw[0].RawDetHeader[0],
143  raw[0].RawDetHeader[1],0) ;
144 
145 
146 
147 
148  // Trigger Summary
149  // set pointer & clear all to 0
150  trg->trg_sum = (void *) &trg_sum ;
151  memset(&trg_sum,0,sizeof(trg_sum)) ;
152 
153  for(i=0;i<2;i++) {
154  trg_sum.L1Sum[i] = swap ? swap32(sum->L1Sum[i]) : sum->L1Sum[i] ;
155  trg_sum.L2Sum[i] = swap ? swap32(sum->L2Sum[i]) : sum->L2Sum[i] ;
156  }
157 
158 
159 // Arghhh, I wish I had some generic programming tools...
160 #define REAL_BAD_HACK(x) trg_sum.DSMdata.x = (swap ? swap16(sum->DSMdata.x) : sum->DSMdata.x)
161 
162  for(i=0;i<8;i++) REAL_BAD_HACK(MTD[i]);
163  for(i=0;i<8;i++) REAL_BAD_HACK(VPD[i]);
164  for(i=0;i<16;i++) REAL_BAD_HACK(CPA[i]);
165  for(i=0;i<8;i++) REAL_BAD_HACK(CTB[i]);
166  for(i=0;i<8;i++) REAL_BAD_HACK(lastDSM[i]);
167  for(i=0;i<8;i++) REAL_BAD_HACK(VTX[i]);
168  for(i=0;i<8;i++) REAL_BAD_HACK(EMC[i]);
169  for(i=0;i<16;i++) REAL_BAD_HACK(BCdata[i]);
170  for(i=0;i<8;i++) REAL_BAD_HACK(specialTriggers[i]);
171  for(i=0;i<8;i++) REAL_BAD_HACK(FPD[i]);
172 
173 #undef REAL_BAD_HACK
174 
175  // RAW data stuff. Just the crossing - NOT pre/post!
176 
177  for(i=0;i<240;i++) {
178  trg->CTB[i] = raw[0].CTB[ctbMap[i]] ;
179  }
180 
181  // MWC is now gone...
182 // for(i=0;i<96;i++) {
183 // trg->MWC[i] = raw[0].MWC[mwcMap[i]] ;
184 // }
185  memset(trg->MWC, 0, sizeof(trg->MWC));
186 
187  for(i=0;i<32;i++) trg->MTD[i] = raw[0].MTD[i];
188  for(i=0;i<64;i++) trg->VPD[i] = raw[0].VPD[i];
189  if(desc->TrgDataFmtVer >= 0x32) {
190  for(i=0;i<32;i++) {
191  trg->P2P[i] = raw[0].P2P[i];
192  }
193  }
194  else {
195  memset(trg->P2P, 0, sizeof(trg->P2P));
196  }
197  for(i=0;i<16;i++) trg->TOF[i] = raw[0].TOF[i];
198 
199  // east
200  for(i=0;i<240;i++) {
201  trg->BEMC[0][i] = raw[0].BEMCEast[i] ;
202  }
203  // west
204  for(i=0;i<240;i++) {
205  trg->BEMC[1][i] = raw[0].BEMCWest[i] ;
206  }
207  // layer1
208  for(i=0;i<48;i++) {
209  trg->BEMC_l1[i] = swap ? swap16(raw[0].BEMClayer1[i]) : raw[0].BEMClayer1[i] ;
210  }
211 
212 
213 
214  for(i=0;i<144;i++) {
215  trg->EEMC[i] = raw[0].EEMC[i] ;
216  }
217  for(i=0;i<16;i++) {
218  trg->EEMC_l1[i] = swap ? swap16(raw[0].EEMClayer1[i]) : 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] = raw[0].FPDEastNSLayer0[i] ;
227  }
228  for(i=0;i<8;i++) {
229  trg->FPD_l1[0][0][i] = swap ? swap16(raw[0].FPDEastNSLayer1[i]) : raw[0].FPDEastNSLayer1[i] ;
230  }
231  // East, TB
232  //for(i=0;i<64;i++) {
233  for(i=0;i<16;i++) { // 9/23/22 FPDEastTBLayer0 only has 16
234  trg->FPD[0][1][i] = 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] = raw[0].FPDW[i];
258 
259 
260  // get the size from trgStructures.h
261  for(i=0;i<(int)sizeof(raw[0].BBC)/(int)sizeof(raw[0].BBC[0]);i++) {
262  trg->BBC[i] = raw[0].BBC[i] ;
263  }
264 
265  for(i=0;i<16;i++) {
266  trg->BBC_l1[i] = swap ? swap16(raw[0].BBClayer1[i]) : raw[0].BBClayer1[i] ;
267  }
268 
269  for(i=0;i<16;i++) {
270  trg->ZDC[i] = raw[0].ZDC[i] ;
271  }
272  for(i=0;i<8;i++) {
273  trg->ZDC_l1[i] = swap ? swap16(raw[0].ZDClayer1[i]) : raw[0].ZDClayer1[i];
274  }
275 
276  for(i=0;i<32;i++) {
277  trg->ZDCSMD[i] = raw[0].ZDCSMD[i] ;
278  }
279 
280 /*
281  int j ;
282  for(j=0;j<11;j++) {
283  for(i=0;i<16;i++) {
284  printf("pre %d, ZDC %d = %u\n",j,i,trgd->raw[j].ZDC[i]) ;
285  }
286  for(i=0;i<8;i++) {
287  printf("pre %d, ZDC_l1 %d = %u\n",j,i,b2h16(trgd->raw[j].ZDClayer1[i])) ;
288  }
289  }
290 */
291 
292  trg->QQTdataBytes = swap ? swap16(raw[0].QQTdataBytes) : raw[0].QQTdataBytes;
293  if(trg->QQTdataBytes/4 <= 1600){
294  for(i=0; i<trg->QQTdataBytes/4; i++){
295  trg->QQTdata[i] = swap ? swap32(raw[0].QQTdata[i]) : raw[0].QQTdata[i];
296  }
297  }
298 
299  return bytes ;
300 }
Definition: daq_trg.h:9