StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
daq_fps.cxx
1 #include <sys/types.h>
2 #include <errno.h>
3 #include <assert.h>
4 #include <stdlib.h>
5 
6 #include <rtsLog.h>
7 #include <rtsSystems.h>
8 
9 
10 
11 #include <SFS/sfs_index.h>
12 #include <DAQ_READER/daqReader.h>
13 #include <DAQ_READER/daq_dta.h>
14 
15 
16 #include "daq_fps.h"
17 
18 
19 const char *daq_fps::help_string = "FPS\n\
20 raw returns raw data\n" ;
21 
23 {
24 public:
26  daq_det_factory::det_factories[FPS_ID] = this ;
27  }
28 
29  daq_det *create() {
30  return new daq_fps ;
31  }
32 } ;
33 
34 static daq_det_fps_factory fps_factory ;
35 
36 
37 daq_fps::daq_fps(daqReader *rts_caller)
38 {
39  rts_id = FPS_ID ;
40  name = rts2name(rts_id) ;
41  sfs_name = "fps" ;
42  caller = rts_caller ;
43 
44  if(caller) caller->insert(this, rts_id) ;
45 
46  raw = new daq_dta ;
47  adc = new daq_dta ;
48  pedrms = new daq_dta ;
49 
50  LOG(DBG,"%s: constructor: caller %p",name,rts_caller) ;
51  return ;
52 }
53 
54 daq_fps::~daq_fps()
55 {
56  LOG(DBG,"%s: DEstructor",name) ;
57 
58  delete raw ;
59  delete adc ;
60  delete pedrms ;
61 
62  return ;
63 }
64 
65 
66 
67 daq_dta *daq_fps::get(const char *bank, int sec, int rdo, int pad, void *p1, void *p2)
68 {
69  Make() ;
70 
71  if(present == 0) return 0 ;
72 
73 
74  if(strcasecmp(bank,"raw")==0) {
75  return handle_raw(sec) ;
76  }
77  else if(strcasecmp(bank,"adc")==0) {
78  return handle_adc(sec) ;
79  }
80  else if(strcasecmp(bank,"pedrms")==0) {
81  return handle_pedrms(sec) ;
82  }
83 
84 
85  LOG(ERR,"%s: unknown bank type \"%s\"",name,bank) ;
86  return 0 ;
87 }
88 
89 
90 
91 
92 daq_dta *daq_fps::handle_adc(int sec)
93 {
94 
95  daq_dta *raw_d ;
96  int s_start, s_stop ;
97 
98  if(sec<=0) {
99  s_start = 1 ;
100  s_stop = 2 ;
101  }
102  else {
103  s_start = s_stop = sec ;
104  }
105 
106  adc->create(32,"fps_adc",rts_id,DAQ_DTA_STRUCT(fps_adc_t)) ;
107 
108  for(int s=s_start;s<=s_stop;s++) {
109 
110  raw_d = handle_raw(s) ;
111  if(raw_d==0) continue ;
112  if(raw_d->iterate() == 0) continue ;
113 
114 
115  fps_evt_hdr_t *hdr = (fps_evt_hdr_t *) raw_d->Void ;
116  u_int *d32 = (u_int *) raw_d->Void ;
117 
118 
119 
120  int tb_cou = hdr->pre_post_cou ;
121  int qt_cou = hdr->qt_cou ;
122 
123  //LOG(TERR,"token %d, pre/post %d, qt %d, hdr ver 0x%08X",hdr->stp_data[0],tb_cou,qt_cou,hdr->ver) ;
124 
125  //note that this gets overriden sector by sector and can thus
126  //potentially only show data from the 2nd (last) sector!
127 
128  memcpy(&meta_hdr,hdr,sizeof(meta_hdr)) ;
129  adc->meta = (void *) &meta_hdr ;
130 
131  d32 += (hdr->ver & 0xFF) ; // skip to data...
132 
133 
134 
135  for(int tb=0;tb<tb_cou;tb++) {
136  int rel_xing = *d32++ ; //first is the relative Xing as a signed number!
137 
138  //LOG(TERR,"Rel xing %d",rel_xing) ;
139 
140  for(int q=0;q<qt_cou;q++) {
141  int qt = *d32++ ;
142  int chs = *d32++ ;
143 
144  //LOG(TERR,"qt %d, chs %d",qt,chs) ;
145 
146  if(chs==0) continue ;
147 
148  fps_adc_t *a = (fps_adc_t *) adc->request(chs) ;
149 
150  //LOG(TERR,"TB %d, QT %d, chs %d",tb,qt,chs) ;
151 
152  for(int c=0;c<chs;c++) {
153  u_int datum = *d32++ ;
154 
155  int ch = datum >> 27 ;
156  int aadc = datum & 0x0FFF ;
157  int tdc = (datum >> 16) & 0x07FF ;
158 
159  a->ch = ch ;
160  a->adc = aadc ;
161  a->tdc = tdc ;
162  a++ ;
163  }
164 
165  adc->finalize(chs,s,qt,rel_xing) ;
166  }
167  }
168  }
169 
170 
171  adc->rewind() ;
172 
173  return adc ;
174 
175 }
176 
177 
178 daq_dta *daq_fps::handle_raw(int sec)
179 {
180  char *st ;
181  int bytes ;
182  char str[256] ;
183  char *full_name ;
184  int s_start, s_stop ;
185 
186  assert(caller) ; // sanity...
187 
188  if(!present) {
189  return 0 ;
190  }
191  else {
192  LOG(DBG,"%s: present %d",name,present) ;
193  }
194 
195 
196  if(sec<=0) {
197  s_start = 1 ;
198  s_stop = 2 ;
199  }
200  else {
201  s_start = s_stop = sec ;
202  }
203 
204 
205  raw->create(1024,"fps_raw",rts_id,DAQ_DTA_STRUCT(char)) ;
206 
207 
208  for(int s=s_start;s<=s_stop;s++) {
209  sprintf(str,"%s/sec0%d/rb01/raw",sfs_name,s) ;
210  full_name = caller->get_sfs_name(str) ;
211 
212  //LOG(TERR,"got full name") ;
213 
214  if(!full_name) continue ;
215  bytes = caller->sfs->fileSize(full_name) ; // this is bytes
216 
217  st = (char *) raw->request(bytes) ;
218 
219  int ret = caller->sfs->read(str, st, bytes) ;
220  if(ret != bytes) {
221  LOG(ERR,"ret is %d") ;
222  }
223 
224  //LOG(TERR,"got bytes",bytes) ;
225 
226  raw->finalize(bytes,s,0,0) ; ;
227  }
228 
229  raw->rewind() ;
230 
231  return raw ;
232 
233 }
234 
235 
236 
237 daq_dta *daq_fps::handle_pedrms(int sec)
238 {
239  char str[128] ;
240  char *full_name ;
241  int bytes ;
242  int s_start, s_stop ;
243 
244  if(sec<=0) {
245  s_start = 1 ;
246  s_stop = 2 ;
247  }
248  else {
249  s_start = s_stop = sec ;
250  }
251 
252  for(int s=s_start;s<=s_stop;s++) {
253 
254  sprintf(str,"%s/sec%02d/pedrms",sfs_name, s) ;
255 
256  LOG(NOTE,"Trying %s",str) ;
257 
258  full_name = caller->get_sfs_name(str) ;
259 
260  if(full_name) {
261  LOG(NOTE,"full_name %s",full_name) ;
262  }
263 
264  if(!full_name) continue ; ;
265 
266 
267  bytes = caller->sfs->fileSize(full_name) ; // this is bytes
268 
269  LOG(NOTE,"bytes %d",bytes) ;
270 
271  int nitems = bytes / sizeof(fps_pedrms_t) ;
272  int remain = bytes % sizeof(fps_pedrms_t) ;
273 
274  if(remain) {
275  LOG(ERR,"Got %d, expect %d",bytes,sizeof(fps_pedrms_t)) ;
276  return 0 ;
277  }
278 
279  char *data = (char *)malloc(bytes) ;
280 
281  int ret = caller->sfs->read(str, (char *)data, bytes) ;
282  if(ret != bytes) {
283  LOG(ERR,"ret is %d") ;
284  }
285 
286  pedrms->create(1,"fps_pedrms",rts_id,DAQ_DTA_STRUCT(fps_pedrms_t)) ;
287 
288  for(int i=0;i<nitems;i++) {
289 
290  fps_pedrms_t *ped = (fps_pedrms_t *)pedrms->request(1) ;
291 
292  memcpy(ped,data+i*sizeof(fps_pedrms_t),sizeof(fps_pedrms_t)) ;
293 
294  if(ped->version != FPS_PED_VERSION) {
295  LOG(ERR,"Wrong version %d in file, expect %d",ped->version,FPS_PED_VERSION) ;
296  }
297 
298  pedrms->finalize(1,s,ped->qt_ix,0) ;
299  }
300 
301  free(data) ;
302  }
303 
304  pedrms->rewind() ;
305 
306  return pedrms ;
307 }
308 
309 /*
310  Checks the STP trigger contribution and extracts the token
311 */
312 
313 int daq_fps::get_l2(char *addr, int words, struct daq_trg_word *trg, int rdo)
314 {
315  int t_cou = 0 ;
316  int err = 0 ;
317 
318  fps_evt_hdr_t *h = (fps_evt_hdr_t *) addr ;
319 
320 
321 
322  trg[t_cou].trg = 4 ;
323  trg[t_cou].daq = 2 ;
324  trg[t_cou].rhic = h->tick ;
325  trg[t_cou].rhic_delta = 0 ;
326  trg[t_cou].t = h->token;
327  t_cou++ ;
328 
329  if(h->status == 0) {
330  err |= 1 ;
331  }
332 
333  if(err) {
334  LOG(ERR,"%u: bad QT event 0x%08X 0x%08X 0x%08X [%d]",h->ev,h->stp_data[0],h->stp_data[1],h->stp_data[2],err) ;
335  return -1 ;
336  }
337 
338  return t_cou ;
339 }
340