StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
tof_reader.cxx
1 #include <stdio.h>
2 #include <string.h>
3 #include <arpa/inet.h>
4 
5 #include <rtsLog.h>
6 #include <rtsSystems.h>
7 #include <rts.h>
8 #include <daqFormats.h>
9 
10 
11 
12 #include "daq_tof.h"
13 
14 
15 enum { ADCD = 0, TDCD, A2DD, SCAD };
16 
17 const unsigned short gaindata[32] = {
18  8, 15, 15, 15,
19  15, 15, 15, 15,
20  15, 15, 15, 15,
21  15, 15, 15, 15,
22  15, 15, 15, 15,
23  15, 15, 15, 15,
24  15, 15, 15, 15,
25  15, 0, 0, 0 };
26 
27 const float decal[5] = {0., 0.38748E-1, 0.332922E-4, 0.20618E-6, -0.2188E-8};
28 const float calib[7] = {0., 2.5928E-2, -7.602961E-7, 4.637791E-11,
29  -2.165394E-13,6.048144E-20, -7.293422E-25};
30 const float calib2[7] = {0., 2.5929192E-2, -2.1316967E-7, 7.9018692E-10,
31  4.2527777E-13, 1.330447E-16, 2.0241446E-20};
32 const float calibration[16] = {
33  305.175E-6, 152.580E-6, 0, 76.290E-6,
34  0, 38.150E-6, 19.073E-6, 0,
35  9.536E-6, 4.786E-6, 0, 2.384E-6,
36  1.192E-6, 596.046E-9, 0, 298.023E-9
37 };
38 
39 
40 
41 
42 int tof_reader(char *m, struct tof_t *tof, u_int driver)
43 {
44  struct DATAP *datap = (struct DATAP *)m ;
45  struct TOFP *tofp ;
46  struct TOFADCD *tofadcd;
47  struct TOFTDCD *toftdcd;
48  struct TOFA2DD *tofa2dd;
49  struct TOFSCAD *tofscad;
50  struct TOFDDLR *ddlr ;
51  int do_swap ;
52  int data_words ;
53 
54  u_int blen ;
55  int len, off ;
56 
57  tof->mode = 1 ;
58  tof->max_channels = 48+48+32+12 ; // Tonko: this is old and stale...
59  tof->channels = 0 ;
60 
61  memset(tof->ddl_words,0,sizeof(tof->ddl_words)) ; // zap em first.
62 
63  if(datap == NULL) return 0 ;
64 
65  if(datap->bh.byte_order != DAQ_RAW_FORMAT_ORDER) {
66  do_swap = 1 ;
67  }
68  else {
69  do_swap = 0 ;
70  }
71 
72  len = (datap->det[TOF_ID].len) ;
73  if(len == 0) return 0 ;
74 
75  off = (datap->det[TOF_ID].off) ;
76  if(off == 0) return 0 ;
77 
78 
79  off = qswap32(do_swap,off) ;
80  len = qswap32(do_swap,len) ;
81 
82 
83  len *= 4 ; // turn it into bytes
84 
85  LOG(DBG,"TOF len %d, off %d",len,off,0,0,0);
86 
87  tofp = (struct TOFP *)((u_int *)m + off) ;
88 
89  if(checkBank(tofp->bh.bank_type,CHAR_TOFP) < 0) {
90  return -1 ;
91  }
92 
93  if(tofp->bh.token == 0) {
94  LOG(DBG,"token 0 - skipping ...",0,0,0,0,0);
95  return 0;
96  }
97 
98  u_int fmt_version ;
99  if(tofp->bh.byte_order != DAQ_RAW_FORMAT_ORDER) {
100  fmt_version = swap32(tofp->bh.format_ver) ;
101  }
102  else {
103  fmt_version = tofp->bh.format_ver ;
104  }
105 
106  int max_contrib ;
107  if(fmt_version >= 0x50000) { // new FY05 format
108  LOG(NOTE,"TOF FY05+ data version 0x%X",fmt_version) ;
109  max_contrib = 8 ;
110  }
111  else {
112  LOG(DBG,"TOF pre-FY05 data version 0x%X",fmt_version) ;
113  max_contrib = 4 ;
114  }
115 
116 
117 
118  memset(tof->adc,0,sizeof(tof->adc));
119  memset(tof->tdc,0,sizeof(tof->tdc));
120  memset(tof->a2d,0,sizeof(tof->a2d));
121  memset(tof->sca,0,sizeof(tof->sca));
122 
123  int bank_t;
124 
125 
126 
127  for(bank_t=0; bank_t<max_contrib; bank_t++) {
128  if(tofp->type[bank_t].len <= 0) continue;
129  switch (bank_t) {
130  case ADCD:
131  tofadcd = (struct TOFADCD *)((u_int *)tofp + l2h32(tofp->type[bank_t].off));
132  if(checkBank(tofadcd->bh.bank_type,CHAR_TOFADCD) < 0) {
133  return -1;
134  }
135 
136  blen = l2h32(tofadcd->bh.length) ;
137  blen -= sizeof(struct bankHeader)/4 ;
138 
139  LOG(DBG,"TOFADCD has %d items",blen,0,0,0,0) ;
140  if(blen > sizeof(tof->adc)/sizeof(tof->adc[0])) {
141  LOG(ERR,"TOFADCD too big [%d > %d] - skipping!",blen,sizeof(tof->adc)/sizeof(tof->adc[0]),0,0,0) ;
142  break ;
143  }
144 
145  for(u_int i=0;i<blen;i++) {
146  tof->adc[i] = (( (l2h32(tofadcd->data[i])) & 0xffff0000)>>16);
147  if(tof->adc[i] > 1024) tof->adc[i] = 1024;
148  tof->channels++ ;
149  }
150  break;
151 
152  case TDCD:
153  toftdcd = (struct TOFTDCD *)((u_int *)tofp + l2h32(tofp->type[bank_t].off));
154  if(checkBank(toftdcd->bh.bank_type,CHAR_TOFTDCD) < 0) {
155  return -1;
156  }
157 
158  blen = l2h32(toftdcd->bh.length) ;
159  blen -= sizeof(struct bankHeader)/4 ;
160 
161  LOG(DBG,"TOFTDCD has %d items",blen,0,0,0,0) ;
162  if(blen > sizeof(tof->tdc)/sizeof(tof->tdc[0])) {
163  LOG(ERR,"TOFTDCD too big [%d > %d] - skipping!",blen,sizeof(tof->tdc)/sizeof(tof->tdc[0]),0,0,0) ;
164  break ;
165  }
166 
167  for(u_int i=0;i<blen;i++) {
168  tof->tdc[i] = (( (l2h32(toftdcd->data[i])) & 0xffff0000)>>16);
169  if(tof->tdc[i] > 2048) tof->tdc[i] = 2048;
170  // what's so special in channel 41??? Tonko
171  if(i==41)tof->tdc[i] = 0;
172  tof->channels++ ;
173  }
174  break;
175 
176  case A2DD: // 32 channels
177  tofa2dd = (struct TOFA2DD *)((u_int *)tofp + l2h32(tofp->type[bank_t].off));
178  if(checkBank(tofa2dd->bh.bank_type,CHAR_TOFA2DD) < 0) {
179  return -1;
180  }
181  tof->a2d[0] = (int)((int) (l2h32(tofa2dd->data[0])) >>16)*calibration[gaindata[0]]/.001-273.;
182  tof->channels++;
183  for(int i=1;i<29;i++) {
184  float V, uncorT;
185  if( (i!=13) && (i!= 14) && (i!=15)) {
186  V = short( (l2h32(tofa2dd->data[i])) >>16)*calibration[gaindata[i]]/.000001;
187  if(V<0) {
188  uncorT = calib2[0]+calib2[1]*V+calib2[2]*V*V+calib2[3]*V*V*V+calib2[4]*V*V*V*V+calib2[5]*V*V*V*V*V+calib2[6]*V*V*V*V*V*V;
189  } else {
190  uncorT = calib[0]+calib[1]*V+calib[2]*V*V+calib[3]*V*V*V+calib[4]*V*V*V*V+calib[5]*V*V*V*V*V+calib[6]*V*V*V*V*V*V;
191  }
192  tof->a2d[i] = uncorT+tof->a2d[0];
193  if(tof->a2d[i] < 0) tof->a2d[i] = 0;
194  if(tof->a2d[i] > 100) tof->a2d[i] = 100;
195  tof->channels++ ;
196  } else {
197  tof->a2d[i] = 0;
198  tof->channels++ ;
199  }
200  }
201  // threshold channel
202  // formula: count = 13.08*X mv +3280
203  for(int i=29;i<32;i++) {
204  tof->a2d[i] = ((short)((l2h32(tofa2dd->data[i]))>>16)-3280)/13.08;
205  if(tof->a2d[i] < 0) tof->a2d[i] = 0;
206  tof->channels++ ;
207  }
208  break;
209 
210  case SCAD: // 12 channels
211  tofscad = (struct TOFSCAD *)((u_int *)tofp + l2h32(tofp->type[bank_t].off));
212  if(checkBank(tofscad->bh.bank_type,CHAR_TOFSCAD) < 0) {
213  return -1;
214  }
215  for(int i=0;i<12;i++) {
216  tof->sca[i] = (( (l2h32(tofscad->data[i])) & 0xffffff00)>>8);
217  tof->channels++ ;
218  }
219  break;
220 
221  default: // new DDLR stuff
222  ddlr = (struct TOFDDLR *)((u_int *)tofp + l2h32(tofp->type[bank_t].off)) ;
223  if(checkBank(ddlr->bh.bank_type,CHAR_TOFDDLR) < 0) {
224  return -1 ;
225  }
226  // do something now...
227  LOG(DBG,"TOFDDLR %d: length %d words",bank_t-3,l2h32(ddlr->bh.length)-10) ;
228  data_words = l2h32(ddlr->bh.length) - 10 ;
229 
230  tof->ddl_words[bank_t-4] = data_words ;
231 
232  u_int *data_word_p = (u_int *)ddlr->data ;
233  for(int i=0;i<data_words;i++) {
234  if(i<10000) {
235  tof->ddl[bank_t-4][i] = l2h32(data_word_p[i]) ;
236  }
237  LOG(DBG,"DDLR %d: %3d: 0x%08X",bank_t-3,i,l2h32(data_word_p[i])) ;
238  }
239 
240  break;
241  }
242  }
243  return len ;
244 
245 }
246 
Definition: daq_tof.h:7
Definition: tof.h:15