StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
daq_tpx.cxx
1 #include <sys/types.h>
2 #include <errno.h>
3 #include <assert.h>
4 
5 #include <rtsLog.h>
6 #include <rtsSystems.h>
7 
8 
9 #include <SFS/sfs_index.h>
10 #include <DAQ_READER/daqReader.h>
11 #include <DAQ_READER/daq_dta.h>
12 
13 
14 
15 
16 #include <DAQ_TPC/daq_tpc.h> // solely for the "legacy" use!
17 #include <TPC/trans_table.hh> // same...
18 #include <TPC/rowlen.h>
19 
20 #include <DAQ1000/ddl_struct.h> // for the misc DDL hardware constructs
21 
22 #include "daq_tpx.h"
23 #include "tpxCore.h"
24 #include "tpxGain.h"
25 #include "tpxPed.h"
26 #include "tpxFCF.h"
27 #include "tpxFCF_2D.h"
28 #include "tpxStat.h"
29 
30 #include <DAQ_TPC23/tpx23.h>
31 
32 
34 {
35 public:
37  daq_det_factory::det_factories[TPX_ID] = this ;
38  }
39 
40  daq_det *create() {
41  return new daq_tpx ;
42  }
43 } ;
44 
45 static daq_det_tpx_factory tpx_factory ;
46 
47 
48 
49 const char *daq_tpx::help_string = "\
50 \n\
51 ****** TPX Help ******* \n\
52 \n\
53 Sector is [1..24]\n\
54 Rdo is [1..6]\n\
55 \n\
56 Supported Banks: \n\
57  raw (sector,rdo); returns (char *) of start of DDL data\n\
58  adc (sector,rdo); returns tb_adc data\n\
59  cld (sector,rdo); returns float cld from data\n\
60  cld_raw (sector,rdo); returns cld_raw CLD data\n\
61 \n\
62 m_Mode:\n\
63  (1<<0) calc pedestals from data when get(adc) called\n\
64  (1<<1) calc gain from data when get(adc) called\n\
65 \n\
66 Not yet done: \n\
67  cld_c (sector,rdo); returns float cld calculated from data\n\
68  gain (sector,rdo,0,char *fname); loads gains from database: \n\
69  fname == 0 from Offline databases\n\
70  fname != 0 from file fname\n\
71  gain_c (sector,rdo); returns calculated gains (see m_Mode) \n\
72  ped (sector,rdo); returns ped/rms from file\n\
73  ped_c (sector,rdo); returns calculated ped/rms (see m_Mode) \n\
74  log\n\
75 " ;
76 
77 
78 void daq_tpx::help() const
79 {
80  printf("%s\n%s\n",GetCVS(),help_string) ;
81 } ;
82 
83 daq_tpx::daq_tpx(daqReader *rts_caller)
84 {
85  // override mother...
86 
87  rts_id = TPX_ID ;
88  name = rts2name(rts_id) ;
89  sfs_name = "tpx" ;
90  caller = rts_caller ;
91 
92  mode = 0 ;
93 
94  if(caller) caller->insert(this, rts_id) ;
95 
96  // create now!
97  legacy = new daq_dta ;
98 
99  LOG(DBG,"legacy %p",legacy) ;
100 
101  raw = new daq_dta ; // in file, compressed
102  cld_raw = new daq_dta ; // in file, compressed
103  ped_raw = new daq_dta ;
104 
105  adc = new daq_dta ; // from "raw", decompressed
106 
107  cld = new daq_dta ; // from "cld_raw", decompressed
108 
109  adc_sim = new daq_dta ; // external input
110  gain = new daq_dta ;
111  ped = new daq_dta ;
112 
113  cld_sim = new daq_dta ; // from "adc_sim", decompressed
114  cld_2d_sim = new daq_dta ;
115 
116  ped_c = new daq_dta ;
117  gain_c = new daq_dta ;
118 
119 
120  altro = new daq_dta ;
121 
122  gain_algo = new tpxGain() ; // always needed for bad pads...
123  stat_algo = 0 ;
124  ped_algo = 0 ;
125  for(int i=0;i<=24;i++) {
126  fcf_algo[i] = 0 ;
127  fcf_2d_algo[i] = 0 ;
128  }
129  fcf_tmp_storage = 0 ;
130 
131  fcf_afterburner_disable = 0 ;
132  fcf_run_compatibility = 9 ; // FY09 default, for now...
133  fcf_do_cuts = 2 ; // run09 default
134 
135  cld23 = 0 ;
136 
137  t23 = 0 ;
138 
139  LOG(DBG,"%s: constructor: caller %p",name, caller) ;
140  return ;
141 }
142 
143 daq_tpx::~daq_tpx()
144 {
145  LOG(DBG,"%s: DEstructor",name) ;
146 
147  // daq data
148  delete raw ;
149  delete cld_raw ;
150  delete ped_raw ;
151  delete altro ;
152 
153  LOG(DBG,"%s: DEstructor done",name) ;
154 
155  delete adc ;
156 
157  LOG(DBG,"%s: DEstructor done",name) ;
158 
159  delete cld ;
160 
161  LOG(DBG,"%s: DEstructor done",name) ;
162 
163  delete adc_sim ;
164  delete gain ;
165  delete ped ;
166 
167  delete cld_sim ;
168  delete cld_2d_sim ;
169 
170  LOG(DBG,"%s: DEstructor done",name) ;
171 
172  delete ped_c ;
173 
174  LOG(DBG,"%s: DEstructor done",name) ;
175 
176  delete gain_c ;
177 
178  LOG(DBG,"%s: DEstructor done %p ",name,legacy) ;
179 
180  delete legacy ; // dies here!
181 
182  LOG(DBG,"%s: DEstructor done",name) ;
183 
184  // algorithms and associated storage...
185  if(gain_algo) delete gain_algo ;
186  if(ped_algo) delete ped_algo ;
187  if(stat_algo) delete stat_algo ;
188 
189  LOG(DBG,"%s: DEstructor done",name) ;
190 
191  for(int i=0;i<=24;i++) {
192  if(fcf_algo[i]) delete fcf_algo[i] ;
193  if(fcf_2d_algo[i]) delete fcf_2d_algo[i] ;
194  }
195  LOG(DBG,"%s: DEstructor done",name) ;
196  if(fcf_tmp_storage) free(fcf_tmp_storage) ;
197 
198  LOG(DBG,"%s: DEstructor done",name) ;
199  return ;
200 }
201 
202 
203 #if 0
204 /*
205  For online: in_buffer, in_bytes, out_buffer, out_bytes MUST be set!
206 */
207 int daq_tpx::Make()
208 {
209  int pres ;
210  int mode ;
211 
212 
213  present = 0 ;
214 
215  mode = GetMode() ;
216 
217  pres = presence() ;
218  if(pres) {
219  evt_num++ ;
220  present |= 2 ;
221  LOG(NOTE,"%s: Make(): presence %d, evt %d: m_Mode 0x%08X, event_mode 0x%08X",name,pres,evt_num,mode,event_mode) ;
222  }
223  else {
224  LOG(DBG,"%s: Make(): not found in this event...",name) ;
225  return 0 ;
226  }
227 
228 
229  if(mode & m_Mode_DAQ_RT) {
230  // Online! DO NOT TOUCH THIS!
231  // it works _ONLY_ for one RDO of one sector!
232 
233  tpx_rdo_event rdo ;
234 
235  char *obuff = out_buffer ;
236  int max_bytes = out_bytes ;
237  out_bytes = 0 ; // clear
238 
239  if((in_buffer == 0) || (in_bytes<=0) || (out_buffer==0) || (max_bytes==0)) {
240  LOG(ERR,"Bad buffers -- skipping") ;
241  return -1 ;
242  }
243 
244  // get the tpx_rdo structure which is all we need for later...
245  ret = tpx_get_start(in_buffer, in_bytes/4, &rdo, 0) ;
246 
247  if(ret < 0) {
248  LOG(ERR,"Horrible error -- skipping") ;
249  return -1 ;
250  }
251 
252  int do_raw = 0 ;
253  switch(rdo.type) {
254  case DDL_TYPE_DTA:
255  if(event_mode & m_Mode_DAQ_RAW) {
256  do_raw = 1 ;
257  }
258  break ;
259  case DDL_TYPE_LOG :
260  // tpxCore deal with log
261  do_raw = 1 ;
262  break ;
263  default :
264  // tpxCore deal with misc
265  do_raw = 1 ;
266  break ;
267  }
268 
269  if(do_raw) {
270  ret = Raw->Make(&rdo,obuff,max_bytes) ;
271  obuff += ret ;
272  max_bytes -= ret ;
273  }
274 
275 
276  if(rdo.type != DDL_TYPE_DTA) return 0 ; // that's it
277 
278  // always!
279  Stat->Make(&rdo) ;
280 
281 
282  if(event_mode & m_Mode_DAQ_GAIN) {
283  Gain->Make(&rdo) ;
284  }
285  else if(event_mode & m_Mode_DAQ_PED) {
286  Ped->Make(&rdo) ;
287  }
288  else if(event_mode & m_Mode_DAQ_FCF_RAW) {
289  ret = Fcf->Make(&rdo, obuff, max_bytes) ;
290  obuff += ret ;
291  max_bytes -= ret ;
292  }
293 
294 
295  out_bytes = obuff - out_buffer ;
296  return 0 ; // all OK!
297  }
298 
299  // Offline is here...
300  int min_sec, max_sec ;
301  int min_rdo, max_rdo ;
302 
303  if(def_sector <= 0) {
304  min_sec = 1 ;
305  max_sec = 24 ;
306  }
307  else {
308  min_sec = max_sec = def_sector ;
309  }
310 
311  if(def_rdo <= 0) {
312  min_rdo = 1 ;
313  max_rdo = 6 ;
314  }
315  else {
316  min_rdo = max_rdo = def_rdo ;
317  }
318 
319 
320  // At this event, SFS file can have:
321  // rb01/raw
322  // rb01/cld
323  // rb01/ped (token 0 only!)
324  // rb01/gain (token 0 only!)
325 
326  // FCF needs cld
327  //
328 
329 
330  return 0 ;
331 }
332 
333 #endif
334 
335 int daq_tpx::InitRun(int run)
336 {
337  u_int mode = GetMode() ;
338 
339  LOG(NOTE,"%s: InitRun(%d), m_Mode 0x%08X",name, run, mode) ;
340 
341 
342  run_num = run ;
343  evt_num = 0 ;
344 
345  // for all runs
346 
347 
348 
349  // when working in Online
350  if(mode & m_Mode_DAQ_RT) {
351  LOG(ERR,"Not yet ready for Online!") ;
352  return -1 ;
353  }
354 
355 
356 
357 
358 
359 
360 
361  // if we have externally applied gains we will use them,
362  // otherwise we'll try to use them from the canonical location...
363  // look into "gains and set the found channels there
364  daq_dta *g = get("gain") ;
365 
366  LOG(DBG,"get(gain) returns %p",g) ;
367  if(g) {
368  LOG(NOTE,"Using externally generated gains") ;
369  gain_algo->init(0) ;
370  while(g->iterate()) {
371  if(g->row > 45) continue ; // NO iTPX yet!
372 
373  LOG(DBG,"\tsec %d, row %d: %d",g->sec,g->row,g->ncontent) ;
374  int max_pad = tpc_rowlen[g->row] ;
375  if((int)g->ncontent > max_pad) {
376  LOG(NOTE,"sector %d, row %d: want %d, have %d",g->sec,g->row,g->ncontent,max_pad) ;
377  }
378  else max_pad = g->ncontent ;
379 
380  for(int pad=1;pad<=max_pad;pad++) {
381  LOG(DBG,"Gains: %d %d %d %f %f",g->sec,g->row,pad,g->gain[pad].gain,g->gain[pad].t0) ;
382  gain_algo->set_gains(g->sec,g->row,pad,g->gain[pad].gain,g->gain[pad].t0) ;
383  }
384  }
385  }
386  else {
387  gain_algo->from_file("/RTS/conf/tpx/tpx_gains.txt",0) ; // all sectors!
388  }
389 
390 
391  if(mode & m_Mode_DAQ_PED) {
392  if(ped_algo==0) {
393  ped_algo = new tpxPed ;
394  }
395 
396 // ped_algo->init() ; // ALL sectors
397 
398  }
399 
400  if(mode & m_Mode_DAQ_GAIN) { // pulser run so we zap the gains!!!
401  gain_algo->init(def_sector) ; // ALL sectors!
402  }
403 
404 
405  // what about FCF?
406 
407 
408 
409  return 0 ;
410 }
411 
412 int daq_tpx::FinishRun(int old)
413 {
414  u_int mode = GetMode() ;
415 
416  LOG(NOTE,"%s: Run %d finished with %d events, m_Mode 0x%08X",name,run_num,evt_num,mode) ;
417 
418  if(mode & m_Mode_DAQ_PED) {
419  LOG(NOTE,"Calculating peds") ;
420 
421  assert(ped_algo) ;
422 
423 // ped_algo->calc() ;
424 
425  // dump them to "ped_c"!
426 
427  delete(ped_algo) ;
428  ped_algo = 0 ;
429 
430  }
431 
432  if(mode & m_Mode_DAQ_GAIN) {
433 
434  LOG(NOTE,"Calculating gains") ;
435 
436 // gain_algo->calc() ;
437 
438  // dump them to "gain_c"!
439  }
440 
441 
442  // we free FCF storage, if any!
443  for(int i=0;i<=24;i++) {
444  if(fcf_algo[i]) {
445  delete fcf_algo[i] ;
446  fcf_algo[i] = 0 ;
447  }
448  }
449 
450 
451  // we free FCF 2D storage, if any!
452  for(int i=0;i<=24;i++) {
453  if(fcf_2d_algo[i]) {
454  delete fcf_2d_algo[i] ;
455  fcf_2d_algo[i] = 0 ;
456  }
457  }
458 
459 
460  if(fcf_tmp_storage) {
461  free(fcf_tmp_storage) ;
462  fcf_tmp_storage = 0 ;
463  }
464 
465 
466  return 0 ;
467 }
468 
469 
470 
471 daq_dta *daq_tpx::get(const char *in_bank, int sec, int row, int pad, void *p1, void *p2)
472 {
473  const char *bank ;
474 
475  if(in_bank==0) { // just wants to know if I'm here so return some read-only non-NULL memory
476  bank = "cld" ; // default
477  }
478  else {
479  bank = in_bank ;
480  }
481 
482  LOG(DBG,"%s: looking for bank %s",name,bank) ;
483 
484 
485  // list created banks first...
486  if(strcasecmp(bank,"adc_sim")==0) {
487  if(adc_sim->is_empty()) return 0 ;
488  adc_sim->rewind() ;
489  return adc_sim ;
490  }
491  else if(strcasecmp(bank,"gain")==0) {
492  if(gain->is_empty()) return 0 ; // not created!
493  gain->rewind() ; // we want to use the bank...
494  return gain ;
495  }
496  else if(strcasecmp(bank,"cld_sim")==0) {
497  return handle_cld_sim(sec,row) ;
498  }
499  else if(strcasecmp(bank,"cld_2d_sim")==0) {
500  return handle_cld_2d_sim(sec,row) ;
501  }
502 
503  // after this all the banks need to be in the file...
504  Make() ;
505  if(!present) return 0 ; // this det is not in this event...
506  if(caller && (caller->detector_bugs & (1<<TPX_ID))) return 0 ; // FY12 UU future-protection bug
507 
508 
509  if(strcasecmp(bank,"raw")==0) {
510  return handle_raw(sec,row) ; // actually sec, rdo; r
511  }
512  else if(strcasecmp(bank,"adc")==0) {
513  return handle_adc(sec,row) ; // actually sec, rdo:
514  }
515  else if(strcasecmp(bank,"cld_raw")==0) {
516  return handle_cld_raw(sec,row) ; // actually sec, rdo:
517  }
518  else if(strcasecmp(bank,"cld")==0) {
519  return handle_cld(sec,row) ; // actually sec, rdo:
520  }
521  else if(strcasecmp(bank,"pedrms")==0) {
522  return handle_ped(sec) ; // just sec
523  }
524  else if(strcasecmp(bank,"legacy")==0) {
525  return handle_legacy(sec,row) ; // actually sec, rdo:
526  }
527  else if(strcasecmp(bank,"altro")==0) {
528  return handle_altro(sec,row) ; // actually sec, rdo:
529  }
530  else {
531  LOG(ERR,"%s: unknown bank type \"%s\"",name,bank) ;
532  }
533 
534 
535  return 0 ;
536 }
537 
538 daq_dta *daq_tpx::put(const char *in_bank, int sec, int row, int pad, void *p1, void *p2)
539 {
540 
541  assert(in_bank) ;
542 
543  if(strcasecmp(in_bank,"adc_sim")==0) {
544  if((row<=0)) { // current TPC
545  sim_row_count = 45 ; // this needs to be correct
546  sim_tpx_rowlen = 0 ;
547  }
548  else { // iTPC!
549  sim_row_count = row ;
550  sim_tpx_rowlen = (u_char *)p1 ;
551  }
552 
553  LOG(NOTE,"adc_sim: row count %d, rowlen %p",sim_row_count,sim_tpx_rowlen) ;
554 
555  adc_sim->create(32*1024,(char *)"adc_sim",rts_id,DAQ_DTA_STRUCT(daq_sim_adc_tb)) ;
556  return adc_sim ;
557  }
558  else if(strcasecmp(in_bank,"gain")==0) {
559  gain->create(32*1024,(char *)"gain",rts_id,DAQ_DTA_STRUCT(daq_det_gain)) ;
560  return gain ;
561  }
562 
563  LOG(ERR,"%s: unknown bank type \"%s\"",name,in_bank) ;
564  return 0 ;
565 
566 }
567 
568 daq_dta *daq_tpx::handle_legacy(int sec, int rdo)
569 {
570  const int tpx_tpc_tb_delta = 22 ;
571  int max_s, min_s ;
572  daq_dta *dd ;
573  int found_something = 0 ;
574 
575  if(sec <= 0) {
576  min_s = 1 ;
577  max_s = 24 ;
578  }
579  else {
580  min_s = max_s = sec ;
581  }
582 
583  legacy->create(1,"tpx_legacy",rts_id,DAQ_DTA_STRUCT(tpc_t)) ;
584 
585  for(int s=min_s;s<=max_s;s++) {
586  struct tpc_t *tpc_p = 0 ;
587 
588  // check for pedestal data first!
589  dd = handle_ped(s) ;
590  if(dd) {
591  tpc_p = (struct tpc_t *) legacy->request(1) ;
592  memset(tpc_p,0,sizeof(tpc_t)) ;
593 
594 
595  //LOG(WARN,"Found TPX peds sec %02d -- translating into TPC legacy peds not done yet!",dd->sec) ;
596 
597  tpc_p->mode = 1 ; // pedestal mode!
598 
599  while(dd->iterate()) {
600  int r = dd->row - 1 ; // tpc_t counts from 0
601  int p = dd->pad - 1 ; // tpc_t counts from 0 ;
602 
603  if((r<0) || (p<0)) continue ; // altro can have row or pad == 0 but not the legacy TPC...
604 
605  found_something = 1 ;
606 
607  //LOG(NOTE,"rp %d:%d, ncontent %d",r,p,dd->ncontent) ;
608  daq_det_pedrms *ped = (daq_det_pedrms *)dd->Void ;
609 
610  for(int i=0;i<(int)dd->ncontent;i++) {
611  int tpc_tb, tpc_adc ;
612 
613 
614  // adjust timebin
615  tpc_tb = i - tpx_tpc_tb_delta ;
616  if(tpc_tb <0) continue ;
617 
618  tpc_adc = ped[i].ped ;
619  if(tpc_adc > 255) tpc_adc = 255 ; // plato
620 
621  int c = tpc_p->counts[r][p] ; // shorthand
622  tpc_p->adc[r][p][c] = tpc_adc ;
623  tpc_p->timebin[r][p][c] = (u_char)(ped[i].rms*16.0) ;
624 
625  (tpc_p->counts[r][p])++ ;
626  tpc_p->channels_sector++ ;
627 
628  }
629 
630  found_something = 1 ;
631  }
632 
633 
634  tpc_p->max_channels_sector = 512 * 5692 ;
635  tpc_p->max_channels_all = 512 * 5692 * 24 ;
636 
637  legacy->finalize(1,s,0,0) ;
638 
639  continue ; // do NOT allow other ADC checks!
640  }
641 
642 
643 
644  // grab the ADC data first...
645  dd = handle_adc(s,-1) ;
646  if(dd) {
647  //LOG(NOTE,"legacy ADC") ;
648  tpc_p = (struct tpc_t *) legacy->request(1) ; // gimme 1 tpc_t
649  memset(tpc_p,0,sizeof(tpc_t)) ;
650 
651 
652  //LOG(NOTE,"legacy ADC 2") ;
653  while(dd->iterate()) {
654  int r = dd->row - 1 ; // tpc_t counts from 0
655  int p = dd->pad - 1 ; // tpc_t counts from 0 ;
656 
657  if((r<0) || (p<0)) continue ; // altro can have row or pad == 0
658 
659  found_something = 1 ;
660  tpc_p->max_channels_sector = 512 * 5692 ; // this is how I mark that an ADC bank was found...
661 
662  //LOG(NOTE,"rp %d:%d, ncontent %d",r,p,dd->ncontent) ;
663 
664  for(u_int i=0;i<dd->ncontent;i++) {
665  int tpc_tb, tpc_adc ;
666 
667  // adjust timebin
668  tpc_tb = dd->adc[i].tb - tpx_tpc_tb_delta ; // 22 is the current best estimate
669  if(tpc_tb < 0) continue ;
670 
671  // adjust logarithmic response
672  tpc_adc = log10to8_table[dd->adc[i].adc] ;
673 
674  if(tpc_adc == 0) continue ; // 0 is possible with the altro but not TPC ZS
675 
676  int c = tpc_p->counts[r][p] ; // shorthand
677  tpc_p->adc[r][p][c] = tpc_adc ;
678  tpc_p->timebin[r][p][c] = tpc_tb ;
679 
680  (tpc_p->counts[r][p])++ ;
681  tpc_p->channels_sector++ ;
682 
683  }
684 
685  }
686  }
687 
688  //LOG(NOTE,"legacy ADC done.") ;
689  // grab CLD data next
690  dd = handle_cld(s,-1) ;
691  if(dd) {
692  if(tpc_p == 0) { // create if it didn't exist already
693  tpc_p = (struct tpc_t *) legacy->request(1) ;
694  memset(tpc_p,0,sizeof(tpc_t)) ;
695  }
696 
697  while(dd->iterate()) {
698  int r = dd->row - 1 ; // tpc_t counts from 0
699 
700  if(r < 0) continue ; // possible with ALTRO
701 
702  tpc_p->has_clusters = 1 ;
703  found_something = 1 ;
704 
705  for(u_int i=0;i<dd->ncontent;i++) {
706  int c = tpc_p->cl_counts[r] ;
707  if(c >= TPC_READER_MAX_CLUSTERS) break ;
708 
709  // adjust and reject the timebin if necessary...
710  int tpc_t1, tpc_t2 ;
711  double tpc_t ;
712 
713  tpc_t1 = (int)dd->cld[i].t1 - tpx_tpc_tb_delta ;
714  tpc_t2 = (int)dd->cld[i].t2 - tpx_tpc_tb_delta ;
715  tpc_t = (double)dd->cld[i].tb - tpx_tpc_tb_delta ;
716 
717  // reject clusters which would not be seen by the TPC
718  if((tpc_t1<0) || (tpc_t2<0) || (tpc_t<0.0)) continue ;
719 
720  tpc_p->cl[r][c].t1 = tpc_t1 ;
721  tpc_p->cl[r][c].t2 = tpc_t2 ;
722  tpc_p->cl[r][c].t = tpc_t ;
723 
724  tpc_p->cl[r][c].p1 = dd->cld[i].p1 ;
725  tpc_p->cl[r][c].p2 = dd->cld[i].p2 ;
726  tpc_p->cl[r][c].p = dd->cld[i].pad ;
727 
728  tpc_p->cl[r][c].charge = dd->cld[i].charge ;
729  tpc_p->cl[r][c].flags = dd->cld[i].flags ;
730 
731  (tpc_p->cl_counts[r])++ ;
732  }
733 
734  }
735 
736 
737  }
738 
739  if(tpc_p) {
740  tpc_p->mode = 0 ;
741 
742  tpc_p->max_channels_all = 512 * 5692 * 24 ;
743 
744  legacy->finalize(1,s,0,0) ;
745  }
746 
747  }
748 
749  legacy->rewind() ;
750 
751  if(found_something) return legacy ;
752  else return 0 ;
753 
754 }
755 
756 daq_dta *daq_tpx::handle_ped(int sec)
757 {
758  char str[128] ;
759  int tot_bytes ;
760  int min_sec, max_sec ;
761 
762  // sanity
763  if(sec <= 0) { // ALL sectors
764  min_sec = 1 ;
765  max_sec = 24 ;
766  }
767  else if((sec<1) || (sec>24)) return 0 ;
768  else {
769  min_sec = max_sec = sec ;
770  }
771 
772 
773  assert(caller) ;
774 
775 
776  // calc total bytes
777  tot_bytes = 0 ;
778 
779  for(int s=min_sec;s<=max_sec;s++) {
780 
781  for(int rdo=0;rdo<=6;rdo++) {
782 
783 
784  if(rdo==0) sprintf(str,"%s/sec%02d/pedrms",sfs_name, s) ;
785  else sprintf(str,"%s/sec%02d/rb%02d/pedrms",sfs_name,s,rdo) ;
786 
787  LOG(NOTE,"%s: trying sfs on \"%s\"",name,str) ;
788 
789  char *full_name = caller->get_sfs_name(str) ;
790  if(full_name == 0) {
791  continue ; // not in this event...
792  }
793 
794  int size = caller->sfs->fileSize(full_name) ; // this is bytes
795 
796  LOG(NOTE,"%s: sector %d: ped size %d",name,s,size) ;
797 
798  if(size <= 0) {
799  continue;
800  //assert(!"can't be 0") ;
801  }
802 
803  // allocate temporary storage
804  char *tmp_cache = (char *) valloc(size) ;
805  assert(tmp_cache) ;
806 
807  // read in the data into temp storage...
808  int ret = caller->sfs->read(full_name, tmp_cache, size) ;
809 
810  if(ret != (int)size) {
811  LOG(ERR,"%s: %s: read failed, expect %d, got %d [%s]",name,str,
812  size,ret,strerror(errno)) ;
813  free(tmp_cache) ;
814  continue ;
815  }
816 
817 
818  if(tot_bytes == 0) { // nothing done so far so we will create the first guess...
819  ped->create(size,"ped",rts_id,DAQ_DTA_STRUCT(daq_det_pedrms)) ;
820  }
821 
822  tot_bytes += size ;
823 
824 
825  // do the actual decoding...
826  u_short *d16 = (u_short *) tmp_cache ;
827 
828  // data format must be the same as in tpxPed::to_evb()!
829  while(size > 0) {
830 /* old, wrong!
831  u_int r_id = l2h32(*(u_int *)d16) ;
832 
833  size -= 4 ; // 1 int == 4 bytes
834  d16 += 2 ; // 1 int == 2 shorts
835 
836  int row = (r_id & 0xFF000000) >> 24 ;
837  int pad = (r_id & 0x00FF0000) >> 16 ;
838  int cou = (r_id & 0x0000FFFF) ;
839 
840 */
841  int cou = l2h16(*d16++) ;
842  //int cou = 512 ;
843  int row = l2h16(*d16++) ;
844  int pad = row & 0xFF ;
845  row >>= 8 ;
846 
847  LOG(DBG,"sector %d: row %d, pad %d, cou %d",s,row,pad,cou) ;
848 
849  size -= 2 * 2 ; // to account for the 2 shorts in the header
850  size -= cou * 2 ; // data is shorts
851 
852  daq_det_pedrms *d = (daq_det_pedrms *) ped->request(cou*10) ; // force more allocation
853 
854  for(int i=0;i<cou;i++) {
855  u_short tmp = l2h16(*d16++) ;
856 
857  int i_rms = (tmp & 0xFC00) >> 10 ;
858  int i_ped = tmp & 0x3FF ;
859 
860  d[i].rms = (float)i_rms / 16.0 ;
861  d[i].ped = i_ped ;
862 
863  }
864 
865  ped->finalize(cou,s,row,pad) ;
866  }
867 
868  free(tmp_cache) ; // release temporary storage...
869 
870  }
871  }
872 
873 
874  LOG(DBG,"Returning from ped_handler") ;
875 
876  if(tot_bytes == 0) return 0 ; // nothing found...
877 
878  ped->rewind() ;
879  return ped ;
880 
881 }
882 
883 
884 daq_dta *daq_tpx::handle_adc(int sec, int rdo)
885 {
886  class tpx23Data tpx_d ;
887 
888  int min_sec, max_sec ;
889  int min_rdo, max_rdo ;
890 
891 
892  // sanity
893  if(sec <= 0) {
894  min_sec = 1 ;
895  max_sec = MAX_SEC ;
896  }
897  else if((sec<1) || (sec>24)) return 0 ;
898  else {
899  min_sec = sec ;
900  max_sec = sec ;
901  }
902 
903  if(rdo <= 0) {
904  min_rdo = 1 ;
905  max_rdo = 6 ;
906  }
907  else if((rdo<0) || (rdo>6)) return 0 ;
908  else {
909  min_rdo = max_rdo = rdo ;
910  }
911 
912  // get a size estimate
913  int rdos = 0 ;
914 
915  for(int s=min_sec;s<=max_sec;s++) {
916  for(int r=min_rdo;r<=max_rdo;r++) {
917  rdos++ ;
918  }
919  }
920 
921  tpx_d.dta = adc ;
922 
923  // guess the byte size...
924  int guess_bytes = rdos * 1152 * (sizeof(daq_store) + 10*sizeof(daq_adc_tb)) ;
925 
926  adc->create(guess_bytes,(char *)"adc",rts_id,DAQ_DTA_STRUCT(daq_adc_tb)) ;
927 
928 
929  for(int s=min_sec;s<=max_sec;s++) {
930  for(int r=min_rdo;r<=max_rdo;r++) {
931  daq_dta *rdo_dta ;
932 
933 
934  char *rdo_ptr ;
935  struct tpx_rdo_event rdo ;
936  struct tpx_altro_struct a ;
937  int rdo_words ;
938  int token ;
939 
940  LOG(NOTE,"Calling handle_raw for %d:%d",s,r) ;
941  rdo_dta = handle_raw(s, r) ; // bring the raw data in, RDO-by_RDO!
942 
943 
944  if(rdo_dta == 0) {
945  LOG(WARN,"rdo_dta NULL?") ;
946  continue ; // sorry, not found...
947  }
948 
949  int ret = rdo_dta->iterate() ; // move from the header...
950  if(ret==0) { // no content
951  continue ;
952  }
953 
954  LOG(DBG,"Called handle_raw for %d:%d, iterate %d, returned %d objs",s,r,ret,rdo_dta->ncontent) ;
955  if(rdo_dta->ncontent == 0) continue ; // nothing found...
956 
957  rdo_ptr = (char *)rdo_dta->Byte ;
958  rdo_words = rdo_dta->ncontent / 4 ;
959 
960  // check header version here
961  u_int *hdr_version = (u_int *)rdo_ptr ;
962 
963  int t23_started = 0 ;
964 
965  if((hdr_version[0]>>24)&0xF) {
966  // handle_adc
967  //altro_c.dta = adc ;
968  //altro_c.in_adc = 1 ;
969 
970 
971  if(t23==0) {
972  t23 = new tpx23 ;
973  t23->online = 0 ;
974  t23->run_type = 3 ;
975  t23->no_cld = 1 ;
976  t23->log_level = 0 ;
977 
978  t23->mode = mode ;
979 
980  t23->data_c = 0 ; // &altro_c ;
981  t23->tpx_d = 0 ;
982  }
983 
984 
985  t23->tpx_d = &tpx_d ;
986 
987  t23->run_start() ;
988  t23->evt_start() ;
989  t23_started = 1 ;
990 
991  tpx_d.sector = s ;
992  tpx_d.rdo = r ;
993 
994 
995  t23->set_rdo(s,r) ;
996  ret = t23->rdo_scan((char *)rdo_ptr,rdo_words) ;
997  token = t23->token ;
998 
999  u_int *dd = (u_int *)rdo_ptr+rdo_words ;
1000 
1001  if(dd[-2]&0xFF000000) {
1002  LOG(NOTE,"Token %d, bad event 0x%08X",token,dd[-2]) ;
1003  }
1004  LOG(NOTE,"FMT23: handle_adc(%d,%d), token %d, ret 0x%X, status 0x%X",s,r,token,ret,dd[-2]) ;
1005  }
1006  else {
1007 
1008  token = tpx_get_start(rdo_ptr, rdo_words, &rdo, 0) ;
1009 
1010  if(token <= 0) {
1011  LOG(ERR,"horrible error, token is %d?",token) ;
1012  continue ;
1013  }
1014 
1015  if(rdo.rdo != r) {
1016  LOG(ERR,"RDO mismatch: in data %d, expect %d",rdo.rdo,r) ;
1017  }
1018 
1019  u_int *data_end = rdo.data_end ;
1020 
1021  a.rdo = rdo.rdo -1 ;
1022  a.t = token ;
1023  a.what = TPX_ALTRO_DO_ADC ;
1024  a.log_err = 0 ;
1025  a.sector = s ;
1026 
1027  do {
1028  data_end = tpx_scan_to_next(data_end, rdo.data_start, &a) ;
1029 
1030  if(a.count == 0) continue ; // no data for this guy...
1031 
1032  // unallowed rows, pads...
1033  if((a.row>45) || (a.pad==0) || (a.pad>182)) {
1034  LOG(ERR,"TPX: S%02d:RDO%d: row %d, pad %d",a.sector,rdo.rdo,a.row,a.pad) ;
1035  }
1036 
1037  daq_adc_tb *at = (daq_adc_tb *) adc->request(a.count) ;
1038 
1039  //LOG(DBG,"%d: %d:%d %d",adc->obj_cou,a.row,a.pad,a.count) ;
1040 
1041  for(u_int i=0 ; i < a.count ; i++) {
1042  at[i].adc = a.adc[i] ;
1043  at[i].tb = a.tb[i] ;
1044 
1045  }
1046 
1047  adc->finalize(a.count, s, a.row, a.pad) ;
1048 
1049  } while(data_end && (data_end > rdo.data_start)) ;
1050  }
1051 
1052  if(t23_started) {
1053  t23->evt_stop() ;
1054  t23->run_stop() ;
1055  }
1056  }
1057  }
1058 
1059 
1060  adc->rewind() ; // wind data pointers to the beginning so that they can be used
1061 
1062 
1063 
1064 
1065  return adc ;
1066 
1067 }
1068 
1069 daq_dta *daq_tpx::handle_altro(int sec, int rdo)
1070 {
1071 
1072  int min_sec, max_sec ;
1073  int min_rdo, max_rdo ;
1074 
1075 
1076  // sanity
1077  if(sec <= 0) {
1078  min_sec = 1 ;
1079  max_sec = MAX_SEC ;
1080  }
1081  else if((sec<1) || (sec>24)) return 0 ;
1082  else {
1083  min_sec = sec ;
1084  max_sec = sec ;
1085  }
1086 
1087  if(rdo <= 0) {
1088  min_rdo = 1 ;
1089  max_rdo = 6 ;
1090  }
1091  else if((rdo<0) || (rdo>6)) return 0 ;
1092  else {
1093  min_rdo = max_rdo = rdo ;
1094  }
1095 
1096  // get a size estimate
1097  int rdos = 0 ;
1098 
1099  for(int s=min_sec;s<=max_sec;s++) {
1100  for(int r=min_rdo;r<=max_rdo;r++) {
1101  rdos++ ;
1102  }
1103  }
1104 
1105  // guess the byte size...
1106  int guess_bytes = rdos * 1152 * (sizeof(daq_store) + 10*sizeof(daq_adc_tb)) ;
1107 
1108  altro->create(guess_bytes,(char *)"adc",rts_id,DAQ_DTA_STRUCT(daq_adc_tb)) ;
1109 
1110 
1111  for(int s=min_sec;s<=max_sec;s++) {
1112  for(int r=min_rdo;r<=max_rdo;r++) {
1113  daq_dta *rdo_dta ;
1114  int token ;
1115 
1116  char *rdo_ptr ;
1117  struct tpx_rdo_event rdo ;
1118  struct tpx_altro_struct a ;
1119  int rdo_words ;
1120 
1121  LOG(NOTE,"Calling handle_raw for %d:%d",s,r) ;
1122  rdo_dta = handle_raw(s, r) ; // bring the raw data in, RDO-by_RDO!
1123 
1124 
1125  int t23_started = 0 ;
1126 
1127  if(rdo_dta == 0) {
1128  LOG(WARN,"rdo_dta NULL?") ;
1129  continue ; // sorry, not found...
1130  }
1131 
1132  int ret = rdo_dta->iterate() ; // move from the header...
1133  if(ret==0) { // no content
1134  continue ;
1135  }
1136 
1137  LOG(DBG,"Called handle_raw for %d:%d, iterate %d, returned %d objs",s,r,ret,rdo_dta->ncontent) ;
1138  if(rdo_dta->ncontent == 0) continue ; // nothing found...
1139 
1140  rdo_ptr = (char *)rdo_dta->Byte ;
1141  rdo_words = rdo_dta->ncontent / 4 ;
1142 
1143 
1144  // check header version here
1145  u_int *hdr_version = (u_int *)rdo_ptr ;
1146 
1147  rdo_fmt = 0 ;
1148  if((hdr_version[0]>>24)&0xF) {
1149  LOG(NOTE,"FMT23: handle_altro(%d,%d): version 0x%X",s,r,hdr_version[0]) ;
1150  rdo_fmt = 23 ;
1151  }
1152 
1153  if(rdo_fmt>22) {
1154  if(t23==0) { // class member
1155  t23 = new tpx23 ;
1156  t23->online = 0 ;
1157  t23->run_type = 3 ;
1158  t23->no_cld = 1 ;
1159  t23->log_level = 0 ;
1160 
1161  t23->data_c = 0 ; // &altro_c ;
1162 
1163  }
1164 
1165  t23->tpx_d = 0 ;
1166 
1167  if(t23_started==0) {
1168  t23->run_start() ;
1169  t23->evt_start() ;
1170  t23_started = 1 ;
1171  }
1172  }
1173 
1174  if(rdo_fmt>22) {
1175  // handle_altro
1176  t23->set_rdo(s,r) ;
1177  ret = t23->rdo_scan((char *)rdo_ptr,rdo_words) ;
1178  token = t23->token ;
1179 
1180  //LOG(TERR,"FMT23 token %d",token) ;
1181  }
1182  else {
1183 
1184  token = tpx_get_start(rdo_ptr, rdo_words, &rdo, 0) ;
1185 
1186  if(token <= 0) {
1187  LOG(ERR,"horrible error, token is %d?",token) ;
1188  continue ;
1189  }
1190 
1191  if(rdo.rdo != r) {
1192  LOG(ERR,"RDO mismatch: in data %d, expect %d",rdo.rdo,r) ;
1193  }
1194 
1195  u_int *data_end = rdo.data_end ;
1196 
1197  a.rdo = rdo.rdo -1 ;
1198  a.t = token ;
1199  a.what = TPX_ALTRO_DO_ADC ;
1200  a.log_err = 0 ;
1201  a.sector = s ;
1202 
1203  do {
1204  data_end = tpx_scan_to_next(data_end, rdo.data_start, &a) ;
1205 
1206  if(a.count == 0) continue ; // no data for this guy...
1207 
1208  // unallowed rows, pads...
1209  //if((a.row>45) || (a.pad==0) || (a.pad>182)) {
1210  // LOG(ERR,"TPX: S%02d:RDO%d: row %d, pad %d",a.sector,rdo.rdo,a.row,a.pad) ;
1211  //}
1212 
1213  daq_adc_tb *at = (daq_adc_tb *) altro->request(a.count) ;
1214 
1215  //LOG(DBG,"%d: %d:%d %d",altro->obj_cou,a.row,a.pad,a.count) ;
1216 
1217  for(u_int i=0 ; i < a.count ; i++) {
1218  at[i].adc = a.adc[i] ;
1219  at[i].tb = a.tb[i] ;
1220  }
1221 
1222  altro->finalize(a.count, s, a.id, a.ch) ;
1223 
1224  } while(data_end && (data_end > rdo.data_start)) ;
1225  }
1226 
1227  if(t23_started) {
1228  t23->evt_stop() ;
1229  t23->run_stop() ;
1230  }
1231 
1232  } // loop over RDOs
1233  } // loop over sectors
1234 
1235 
1236  altro->rewind() ; // wind data pointers to the beginning so that they can be used
1237 
1238 
1239 
1240 
1241  return altro ;
1242 
1243 }
1244 
1245 
1246 
1247 
1248 daq_dta *daq_tpx::handle_raw(int sec, int rdo)
1249 {
1250  char str[128] ;
1251  int tot_bytes ;
1252  int min_sec, max_sec, min_rdo, max_rdo ;
1253  struct {
1254  int sec ;
1255  int rb ;
1256  u_int bytes ;
1257  } obj[24*6] ;
1258 
1259  // sanity
1260  if(sec <= 0) { // ALL sectors
1261  min_sec = 1 ;
1262  max_sec = 24 ;
1263  }
1264  else if((sec<1) || (sec>24)) return 0 ;
1265  else {
1266  min_sec = max_sec = sec ;
1267  }
1268 
1269  if(rdo <= 0) { // ALL RDOs in this sector
1270  min_rdo = 1 ;
1271  max_rdo = 6 ;
1272  }
1273  else if((rdo<1) || (rdo>6)) return 0 ;
1274  else {
1275  min_rdo = max_rdo = rdo ;
1276  }
1277 
1278  assert(caller) ;
1279 
1280 
1281  // calc total bytes
1282  tot_bytes = 0 ;
1283  int o_cou = 0 ;
1284 
1285  for(int s=min_sec;s<=max_sec;s++) {
1286  for(int r=min_rdo;r<=max_rdo;r++) {
1287  char *full_name ;
1288 
1289  //sprintf(str,"%s/%s/sec%02d/rb%02d/adc",caller->fs_cur_evt, "tpx", s, r) ;
1290  sprintf(str,"%s/sec%02d/rb%02d/adc",sfs_name, s, r) ;
1291 
1292  LOG(NOTE,"%s: trying sfs on \"%s\"",name,str) ;
1293 
1294  full_name = caller->get_sfs_name(str) ;
1295  if(full_name == 0) {
1296  sprintf(str,"%s/sec%02d/rb%02d/adc23",sfs_name, s, r) ;
1297 
1298  LOG(NOTE,"%s: trying sfs on \"%s\"",name,str) ;
1299 
1300  full_name = caller->get_sfs_name(str) ;
1301 
1302  if(full_name==0) continue ;
1303 
1304 
1305  }
1306 
1307 
1308  int size = caller->sfs->fileSize(full_name) ; // this is bytes
1309 
1310  LOG(NOTE,"%s: sector %d, rdo %d : raw size %d",name,s,r,size) ;
1311 
1312  if(size <= 0) {
1313  if(size < 0) {
1314  LOG(DBG,"%s: %s: not found in this event",name,str) ;
1315  }
1316  continue ;
1317  }
1318  else {
1319  obj[o_cou].rb = r ;
1320  obj[o_cou].sec = s ;
1321  obj[o_cou].bytes = size ;
1322 
1323  o_cou++ ;
1324 
1325  tot_bytes += size ;
1326 
1327  LOG(DBG,"%s: %s: reading in \"%s\": bytes %d",name,str,"raw", size) ;
1328  }
1329  }
1330  }
1331 
1332  raw->create(tot_bytes,(char *)"raw",rts_id,DAQ_DTA_STRUCT(u_char)) ;
1333 
1334  // bring in the bacon from the SFS file....
1335  for(int i=0;i<o_cou;i++) {
1336 
1337  sprintf(str,"%s/sec%02d/rb%02d/adc",sfs_name,obj[i].sec, obj[i].rb) ;
1338  char *full_name = caller->get_sfs_name(str) ;
1339 
1340  if(full_name == 0) {
1341  sprintf(str,"%s/sec%02d/rb%02d/adc23",sfs_name, obj[i].sec, obj[i].rb) ;
1342 
1343  LOG(NOTE,"%s: trying sfs on \"%s\"",name,str) ;
1344 
1345  full_name = caller->get_sfs_name(str) ;
1346 
1347  if(full_name==0) continue ;
1348 
1349  }
1350 
1351 
1352 
1353  LOG(NOTE,"%s: request %d bytes",name,obj[i].bytes) ;
1354 
1355  char *mem = (char *) raw->request(obj[i].bytes) ;
1356 
1357  int ret = caller->sfs->read(full_name, mem, obj[i].bytes) ;
1358 
1359  if(ret != (int)obj[i].bytes) {
1360  LOG(ERR,"%s: %s: read failed, expect %d, got %d [%s]",name,str,
1361  obj[i].bytes,ret,strerror(errno)) ;
1362  }
1363  else {
1364  LOG(NOTE,"%s: %s read %d bytes",name,str,ret) ;
1365  }
1366 
1367  raw->finalize(obj[i].bytes, obj[i].sec, obj[i].rb, 0) ;
1368  }
1369 
1370 
1371  LOG(DBG,"Returning from raw_handler") ;
1372  raw->rewind() ;
1373  return raw ;
1374 
1375 }
1376 
1377 daq_dta *daq_tpx::handle_cld_raw(int sec, int rdo)
1378 {
1379  char str[128] ;
1380  int tot_bytes ;
1381  int min_sec, max_sec, min_rdo, max_rdo ;
1382  struct {
1383  int sec ;
1384  int rb ;
1385  u_int bytes ;
1386  } obj[24*6] ;
1387 
1388  // sanity
1389  if(sec <= 0) { // ALL sectors
1390  min_sec = 1 ;
1391  max_sec = 24 ;
1392  }
1393  else if((sec<1) || (sec>24)) return 0 ;
1394  else {
1395  min_sec = max_sec = sec ;
1396  }
1397 
1398  if(rdo <= 0) { // ALL RDOs in this sector
1399  min_rdo = 1 ;
1400  max_rdo = 6 ;
1401  }
1402  else if((rdo<1) || (rdo>6)) return 0 ;
1403  else {
1404  min_rdo = max_rdo = rdo ;
1405  }
1406 
1407  assert(caller) ;
1408 
1409 
1410  cld23 = 0 ;
1411 
1412  // calc total bytes
1413  tot_bytes = 0 ;
1414  int o_cou = 0 ;
1415  for(int s=min_sec;s<=max_sec;s++) {
1416  // test for new bank first
1417  sprintf(str,"%s/sec%02d/cld23",sfs_name, s) ;
1418 
1419  LOG(NOTE,"%s: trying sfs on \"%s\"",name,str) ;
1420 
1421  char *full_name = caller->get_sfs_name(str) ;
1422  if(full_name && min_rdo==1) {
1423  cld23 = 1 ;
1424  }
1425 
1426  if(cld23==1) { // old, broken version in FY23 until 09-May ;
1427 
1428  int size = caller->sfs->fileSize(full_name) ; // this is bytes
1429 
1430  LOG(NOTE,"%s: CLD23: sector %d, rdo 23 : cld size %d",name,s,size) ;
1431 
1432 
1433  if(size <= 0) {
1434  if(size < 0) {
1435  LOG(DBG,"%s: %s: not found in this event",name,str) ;
1436  }
1437  continue ;
1438  }
1439  else {
1440  obj[o_cou].rb = 23 ;
1441  obj[o_cou].sec = s ;
1442  obj[o_cou].bytes = size ;
1443 
1444  o_cou++ ;
1445 
1446  tot_bytes += size ;
1447 
1448  LOG(DBG,"%s: %s: reading in \"%s\": bytes %d",name,str,"cld_raw", size) ;
1449  }
1450 
1451  continue ; // NOTE: continue
1452  }
1453 
1454 
1455  // test for new bank first
1456  sprintf(str,"%s/sec%02d/cld10",sfs_name, s) ;
1457 
1458  LOG(NOTE,"%s: trying sfs on \"%s\"",name,str) ;
1459 
1460  full_name = caller->get_sfs_name(str) ;
1461  if(full_name && min_rdo==1) {
1462  cld23 = 2 ;
1463  }
1464 
1465 
1466  if(cld23==2) { // FY23 version, RDOs 3 & 4
1467 
1468  int size = caller->sfs->fileSize(full_name) ; // this is bytes
1469 
1470  LOG(NOTE,"%s: CLD23: sector %d, rdo 10 : cld size %d",name,s,size) ;
1471 
1472 
1473  if(size <= 0) {
1474  if(size < 0) {
1475  LOG(DBG,"%s: %s: not found in this event",name,str) ;
1476  }
1477  continue ;
1478  }
1479  else {
1480  obj[o_cou].rb = 10 ;
1481  obj[o_cou].sec = s ;
1482  obj[o_cou].bytes = size ;
1483 
1484  o_cou++ ;
1485 
1486  tot_bytes += size ;
1487 
1488  LOG(DBG,"%s: %s: reading in \"%s\": bytes %d",name,str,"cld_raw", size) ;
1489  }
1490  }
1491 
1492 
1493  // test for new bank first
1494  sprintf(str,"%s/sec%02d/cld11",sfs_name, s) ;
1495 
1496  LOG(NOTE,"%s: trying sfs on \"%s\"",name,str) ;
1497 
1498  full_name = caller->get_sfs_name(str) ;
1499  if(full_name && min_rdo==2) {
1500  cld23 = 3 ;
1501  }
1502 
1503 
1504  if(cld23==3) { // FY23: RDOs 5 & 6
1505 
1506  int size = caller->sfs->fileSize(full_name) ; // this is bytes
1507 
1508  LOG(NOTE,"%s: CLD23: sector %d, rdo 11 : cld size %d",name,s,size) ;
1509 
1510 
1511  if(size <= 0) {
1512  if(size < 0) {
1513  LOG(DBG,"%s: %s: not found in this event",name,str) ;
1514  }
1515  continue ;
1516  }
1517  else {
1518  obj[o_cou].rb = 11 ;
1519  obj[o_cou].sec = s ;
1520  obj[o_cou].bytes = size ;
1521 
1522  o_cou++ ;
1523 
1524  tot_bytes += size ;
1525 
1526  LOG(DBG,"%s: %s: reading in \"%s\": bytes %d",name,str,"cld_raw", size) ;
1527  }
1528  }
1529 
1530 
1531  if(cld23) continue ;
1532 
1533  // OLD code
1534 
1535  for(int r=min_rdo;r<=max_rdo;r++) {
1536 
1537 
1538  sprintf(str,"%s/sec%02d/cld%02d",sfs_name, s, r) ;
1539 
1540  LOG(NOTE,"%s: trying sfs on \"%s\"",name,str) ;
1541 
1542  char *full_name = caller->get_sfs_name(str) ;
1543  if(full_name == 0) continue ;
1544 
1545  int size = caller->sfs->fileSize(full_name) ; // this is bytes
1546 
1547  LOG(NOTE,"%s: sector %d, rdo %d : cld size %d",name,s,r,size) ;
1548 
1549 
1550  if(size <= 0) {
1551  if(size < 0) {
1552  LOG(DBG,"%s: %s: not found in this event",name,str) ;
1553  }
1554  continue ;
1555  }
1556  else {
1557  obj[o_cou].rb = r ;
1558  obj[o_cou].sec = s ;
1559  obj[o_cou].bytes = size ;
1560 
1561  o_cou++ ;
1562 
1563  tot_bytes += size ;
1564 
1565  LOG(DBG,"%s: %s: reading in \"%s\": bytes %d",name,str,"cld_raw", size) ;
1566  }
1567  }
1568  }
1569 
1570  cld_raw->create(tot_bytes,(char *)"cld_raw",rts_id,DAQ_DTA_STRUCT(u_char)) ;
1571 
1572  // bring in the bacon from the SFS file....
1573  for(int i=0;i<o_cou;i++) {
1574 
1575  sprintf(str,"%s/sec%02d/cld%02d",sfs_name,obj[i].sec, obj[i].rb) ;
1576  char *full_name = caller->get_sfs_name(str) ;
1577  if(full_name==0) continue ;
1578 
1579  char *mem = (char *) cld_raw->request(obj[i].bytes) ;
1580 
1581  int ret = caller->sfs->read(full_name, mem, obj[i].bytes) ;
1582 
1583  if(ret != (int)obj[i].bytes) {
1584  LOG(ERR,"%s: %s: read failed, expect %d, got %d [%s]",name,str,
1585  obj[i].bytes,ret,strerror(errno)) ;
1586  }
1587  else {
1588  LOG(NOTE,"%s: %s read %d bytes",name,str,ret) ;
1589  }
1590 
1591  cld_raw->finalize(obj[i].bytes, obj[i].sec, obj[i].rb, 0) ;
1592  }
1593 
1594 
1595  LOG(DBG,"Returning from cld_raw_handler") ;
1596  cld_raw->rewind() ;
1597 
1598  return cld_raw ;
1599 
1600 }
1601 
1602 
1603 daq_dta *daq_tpx::handle_cld(int sec, int rdo)
1604 {
1605 
1606  int min_sec, max_sec ;
1607  int min_rdo, max_rdo ;
1608  int found_broken_edges = 0 ; // afterburner assist
1609 
1610  // sanity
1611  if(sec <= 0) {
1612  min_sec = 1 ;
1613  max_sec = MAX_SEC ;
1614  }
1615  else if((sec<1) || (sec>24)) return 0 ;
1616  else {
1617  min_sec = sec ;
1618  max_sec = sec ;
1619  }
1620 
1621  if(rdo <= 0) {
1622  min_rdo = 1 ;
1623  max_rdo = 6 ;
1624  }
1625  else if((rdo<0) || (rdo>6)) return 0 ;
1626  else {
1627  min_rdo = max_rdo = rdo ;
1628  }
1629 
1630  // get a size estimate
1631  int rdos = 0 ;
1632 
1633  for(int s=min_sec;s<=max_sec;s++) {
1634  for(int r=min_rdo;r<=max_rdo;r++) {
1635  rdos++ ;
1636  }
1637  }
1638 
1639  // guess the byte size...
1640  int guess_bytes = rdos * (1152/10) * (sizeof(daq_store) + 10*sizeof(daq_cld)) ;
1641 
1642  // start the allocation with the guessed size...
1643  cld->create(guess_bytes,(char *)"cld",rts_id,DAQ_DTA_STRUCT(daq_cld)) ;
1644 
1645 
1646  for(int s=min_sec;s<=max_sec;s++) {
1647  for(int r=min_rdo;r<=max_rdo;r++) {
1648  daq_dta *dd ;
1649 
1650 
1651  LOG(NOTE,"Calling handle_cld_raw for %d:%d",s,r) ;
1652  dd = handle_cld_raw(s, r) ; // bring the raw data in, RDO-by_RDO!
1653 
1654 
1655  if(dd == 0) {
1656  continue ; // sorry, not found...
1657  }
1658 
1659  int ret = dd->iterate() ; // move from the header...
1660  if(ret==0) { // no content
1661  continue ;
1662  }
1663 
1664  LOG(NOTE,"Called handle_cld_raw for %d:%d, iterate %d, returned %d objs",s,r,ret,dd->ncontent) ;
1665 
1666  int bytes = dd->ncontent ;
1667  if(bytes <= 0) continue ;
1668 
1669  u_int *buff = dd->Int32 ;
1670 
1671  u_int *end_buff = buff + bytes/4 ;
1672  u_int *p_buff = buff ;
1673 
1674  while(p_buff < end_buff) {
1675  u_int row = *p_buff++ ;
1676  u_int cou = *p_buff++ ;
1677 
1678  if(cld23) {
1679  LOG(NOTE,"CLD23 %d: S%02d:%d: row 0x%08X, cou 0x%08X",cld23,s,r,row,cou) ;
1680  cou /= 2 ;
1681  }
1682 
1683  if(cou > 1000000) {
1684  LOG(WARN,"Bad data in S%02d-%d, row %d -- count is %d -- skipping",
1685  s,r,row,cou) ;
1686  break ;
1687  }
1688 
1689  u_int version = (row >> 16) ; // decoder needs the version
1690  row &= 0xFFFF ; // finalize the row...
1691 
1692  daq_cld *dc = (daq_cld *) cld->request(cou) ; // ask for storage; we know exactly how much...
1693 
1694  if(cld23) {
1695  LOG(NOTE,"CLD23: S%02d:%d: row %d, version 0x%04X, count %d",s,r,row,version,cou) ;
1696  }
1697 
1698  for(u_int i=0;i<cou;i++) {
1699  p_buff += fcf_algo[0]->fcf_decode(p_buff, dc, version) ;
1700  if((row==8) && (dc->flags & FCF_BROKEN_EDGE)) {
1701  found_broken_edges = 1 ; // mark for later use...
1702  }
1703  dc++ ;
1704  }
1705 
1706  cld->finalize(cou,s,row,0) ; // commit storage...
1707 
1708 
1709  }
1710 
1711 
1712 
1713  } // end of RDO loop
1714  } // end of sector loop
1715 
1716 
1717  cld->rewind() ; // wind data pointers to the beginning so that they can be used
1718 
1719  // and now run the afterburner if any found...
1720  if(found_broken_edges && !fcf_afterburner_disable) {
1721  // prepare afterburner
1722  const int FCF_MAX_MERGED_COU = 256 ;
1723 
1724  daq_cld *merged_store[25][FCF_MAX_MERGED_COU] ;
1725  int merged_cou[25] ;
1726 
1727  memset(merged_cou,0, sizeof(merged_cou)) ;
1728 
1729  while(cld->iterate()) {
1730  if(cld->row != 8) continue ; // immediatelly skip the row if not 8
1731 
1732  int s = cld->sec ; // shorthand
1733 
1734  for(u_int i=0;i<cld->ncontent;i++) {
1735  if((cld->cld[i].flags & FCF_BROKEN_EDGE) && (merged_cou[s] < FCF_MAX_MERGED_COU)) {
1736  merged_store[s][merged_cou[s]] = &cld->cld[i] ; // remember the ptr...
1737  merged_cou[s]++ ; // count 'em
1738  }
1739  }
1740  }
1741 
1742  for(int s=1;s<=24;s++) {
1743  fcf_algo[0]->afterburner(merged_cou[s],merged_store[s]) ;
1744  }
1745 
1746  cld->rewind() ; // and go back
1747  }
1748 
1749 
1750 
1751  return cld ;
1752 
1753 }
1754 
1755 
1756 
1757 
1758 static int cmpr_sim_adc(const void *first, const void *second)
1759 {
1760  daq_sim_adc_tb *f = (daq_sim_adc_tb *)first ;
1761  daq_sim_adc_tb *s = (daq_sim_adc_tb *)second ;
1762 
1763  if(f->tb == s->tb) return 0 ;
1764  if(f->tb > s->tb) return -1 ; // reverse sort!
1765 
1766  return 1 ;
1767 }
1768 
1769 
1770 /*
1771  cld_sim works on sector row!
1772 */
1773 daq_dta *daq_tpx::handle_cld_2d_sim(int sec, int row)
1774 {
1775 
1776  int min_sec, max_sec ;
1777  int min_row, max_row ;
1778 
1779 
1780  // sanity
1781  if(sec <= 0) {
1782  min_sec = 1 ;
1783  max_sec = MAX_SEC ;
1784  }
1785  else if((sec<1) || (sec>24)) return 0 ;
1786  else {
1787  min_sec = sec ;
1788  max_sec = sec ;
1789  }
1790 
1791  if(row <= 0) {
1792  min_row = 1 ;
1793  max_row = sim_row_count ;
1794  }
1795  else if((row<0) || (row>250)) return 0 ;
1796  else {
1797  min_row = max_row = row ;
1798  }
1799 
1800  // get a size estimate
1801  int rows = 0 ;
1802 
1803  for(int s=min_sec;s<=max_sec;s++) {
1804  if(fcf_2d_algo[s]) {
1805  LOG(DBG,"start_evt(): sec %d",s) ;
1806  fcf_2d_algo[s]->start_evt_2d(s,0) ;
1807  }
1808 
1809  for(int r=min_row;r<=max_row;r++) {
1810  rows++ ;
1811  }
1812  }
1813 
1814  // guess the byte size: assume 30 hits per row
1815  int guess_bytes = rows * (sizeof(daq_store) + 30*sizeof(daq_sim_cld)) ;
1816 
1817  cld_2d_sim->create(guess_bytes,(char *)"cld_2d_sim",rts_id,DAQ_DTA_STRUCT(daq_sim_cld)) ;
1818 
1819  // adc_sim data is flattened out so we will run if we find the sec/row
1820  // requested
1821  daq_dta *sim = get("adc_sim") ;
1822 
1823  if(sim==0) {
1824  LOG(ERR,"%s: sector %d, row %d: you need to add simulated data first!",name,sec,row) ;
1825  return 0 ;
1826  return cld_2d_sim ;
1827  }
1828 
1829 
1830  while(sim->iterate()) {
1831  if((min_sec<=sim->sec) && (sim->sec<= max_sec) && (min_row<=sim->row) && (sim->row <= max_row)) ;
1832  else continue ;
1833 
1834  // this is how I allocate the algorithm
1835  if(fcf_2d_algo[sim->sec]==0) {
1836 
1837  LOG(NOTE,"No algo assigned for sector %d -- creating one!",sim->sec) ;
1838  fcf_2d_algo[sim->sec] = new tpxFCF_2D ;
1839  fcf_2d_algo[sim->sec]->set_id(sim->sec) ;
1840  fcf_2d_algo[sim->sec]->fcf_style = 2 ;
1841 
1842  fcf_2d_algo[sim->sec]->run_compatibility = fcf_run_compatibility ;
1843  fcf_2d_algo[sim->sec]->do_cuts = fcf_do_cuts ;
1844 
1845  for(int r=1;r<=6;r++) {
1846  fcf_2d_algo[sim->sec]->config2(sim->sec,r,1,sim_row_count,sim_tpx_rowlen) ;
1847  }
1848 
1849 
1850  //LOG(TERR,"DOne with config2") ;
1851 
1852  fcf_2d_algo[sim->sec]->apply_gains2(gain_algo) ;
1853 
1854  //LOG(TERR,"Done with apply gains") ;
1855 
1856  if(fcf_tmp_storage==0) { // for the results!!!
1857  fcf_tmp_storage = (u_int *)valloc(FCF_TMP_BYTES) ;
1858  }
1859 
1860  fcf_2d_algo[sim->sec]->start_evt_2d(sim->sec,0) ;
1861  }
1862 
1863  //u_short track_id[512] ;
1864  tpx_altro_struct a ;
1865 
1866  a.row = sim->row ;
1867  a.pad = sim->pad ;
1868  a.count = 0 ;
1869 
1870  // NEED to sort in falling timebin!
1871  qsort(sim->sim_adc, sim->ncontent, sizeof(sim->sim_adc[0]),cmpr_sim_adc) ;
1872 
1873 
1874 
1875  for(u_int i=0;i<sim->ncontent;i++) {
1876  a.adc[i] = sim->sim_adc[i].adc ;
1877  a.tb[i] = sim->sim_adc[i].tb ;
1878  //track_id[i] = sim->sim_adc[i].track_id ;
1879  a.count++ ;
1880  }
1881 
1882  //LOG(TERR,"before") ;
1883  fcf_2d_algo[sim->sec]->do_pad_2d(&a, sim->sim_adc) ;
1884  //LOG(TERR,"do_pad(): sec %d, row %d, pad %d: %d",sim->sec,a.row,a.pad,a.count) ;
1885  }
1886 
1887 
1888 
1889  for(int s=min_sec;s<=max_sec;s++) {
1890  if(fcf_2d_algo[s]) {
1891  LOG(NOTE,"Trying sec %d",s) ;
1892 
1893  int words = fcf_2d_algo[s]->stage_2d(fcf_tmp_storage,FCF_TMP_BYTES) ;
1894  if(words<=0) continue ;
1895 
1896  LOG(NOTE,"Sector %d: %d words",s,words) ;
1897 
1898  u_int *p_buff = fcf_tmp_storage ;
1899  u_int *end_buff = p_buff + words ;
1900 
1901  while(p_buff < end_buff) {
1902  u_int row = *p_buff++ ;
1903  u_int cou = *p_buff++ ;
1904  int g_cou = 0 ;
1905 
1906 
1907  u_int version = (row >> 16) ;
1908  row &= 0xFFFF ;
1909 
1910 
1911  //LOG(TERR,"row %d, version 0x%X, cou %d",row,version,cou) ;
1912 
1913  daq_sim_cld *cld = (daq_sim_cld *) cld_2d_sim->request(cou) ;
1914 
1915  while(cou) {
1916  int skip = fcf_2d_algo[s]->fcf_decode(p_buff, cld + g_cou, version) ;
1917 
1918  //LOG(TERR,"skip %d",skip) ;
1919  g_cou++ ;
1920 
1921  p_buff += skip ;
1922  cou-- ;
1923 
1924  }
1925 
1926  cld_2d_sim->finalize(g_cou,s,row) ;
1927  }
1928  }
1929  }
1930 
1931  LOG(NOTE,"Sec done") ;
1932 
1933  cld_2d_sim->rewind() ;
1934 
1935  return cld_2d_sim ;
1936 
1937 }
1938 
1939 /*
1940  cld_sim works on sector row!
1941 */
1942 daq_dta *daq_tpx::handle_cld_sim(int sec, int row)
1943 {
1944 
1945  int min_sec, max_sec ;
1946  int min_row, max_row ;
1947 
1948 
1949  // sanity
1950  if(sec <= 0) {
1951  min_sec = 1 ;
1952  max_sec = MAX_SEC ;
1953  }
1954  else if((sec<1) || (sec>24)) return 0 ;
1955  else {
1956  min_sec = sec ;
1957  max_sec = sec ;
1958  }
1959 
1960  if(row <= 0) {
1961  min_row = 1 ;
1962  max_row = sim_row_count ;
1963  }
1964  else if((row<0) || (row>250)) return 0 ;
1965  else {
1966  min_row = max_row = row ;
1967  }
1968 
1969  // get a size estimate
1970  int rows = 0 ;
1971 
1972  for(int s=min_sec;s<=max_sec;s++) {
1973  if(fcf_algo[s]) {
1974  LOG(DBG,"start_evt(): sec %d",s) ;
1975  fcf_algo[s]->start_evt() ; // make sure we start a new event!
1976  }
1977 
1978  for(int r=min_row;r<=max_row;r++) {
1979  rows++ ;
1980  }
1981  }
1982 
1983  // guess the byte size: assume 30 hits per row
1984  int guess_bytes = rows * (sizeof(daq_store) + 30*sizeof(daq_sim_cld)) ;
1985 
1986  cld_sim->create(guess_bytes,(char *)"cld_sim",rts_id,DAQ_DTA_STRUCT(daq_sim_cld)) ;
1987 
1988  // adc_sim data is flattened out so we will run if we find the sec/row
1989  // requested
1990  daq_dta *sim = get("adc_sim") ;
1991 
1992  if(sim==0) {
1993  LOG(ERR,"%s: sector %d, row %d: you need to add simulated data first!",name,sec,row) ;
1994  return 0 ;
1995  return cld_sim ;
1996  }
1997 
1998 
1999  while(sim->iterate()) {
2000  if((min_sec<=sim->sec) && (sim->sec<= max_sec) && (min_row<=sim->row) && (sim->row <= max_row)) ;
2001  else continue ;
2002 
2003  // this is how I allocate the algorithm
2004  if(fcf_algo[sim->sec]==0) {
2005  LOG(NOTE,"No algo assigned for sector %d -- creating one!",sim->sec) ;
2006  fcf_algo[sim->sec] = new tpxFCF ;
2007  fcf_algo[sim->sec]->config(0x3F,1,sim_row_count,sim_tpx_rowlen) ; // assume all 6 RDOs; extra data + annotations
2008  fcf_algo[sim->sec]->run_compatibility = fcf_run_compatibility ;
2009  fcf_algo[sim->sec]->do_cuts = fcf_do_cuts ;
2010 
2011  fcf_algo[sim->sec]->apply_gains(sim->sec,gain_algo) ;
2012 
2013  fcf_algo[sim->sec]->start_evt() ;
2014 
2015  if(fcf_tmp_storage==0) {
2016  fcf_tmp_storage = (u_int *)valloc(FCF_TMP_BYTES) ;
2017  }
2018  }
2019 
2020  //u_short track_id[512] ;
2021  tpx_altro_struct a ;
2022 
2023  a.row = sim->row ;
2024  a.pad = sim->pad ;
2025  a.count = 0 ;
2026 
2027  // NEED to sort in falling timebin!
2028  qsort(sim->sim_adc, sim->ncontent, sizeof(sim->sim_adc[0]),cmpr_sim_adc) ;
2029 
2030 
2031 
2032  for(u_int i=0;i<sim->ncontent;i++) {
2033  a.adc[i] = sim->sim_adc[i].adc ;
2034  a.tb[i] = sim->sim_adc[i].tb ;
2035  //track_id[i] = sim->sim_adc[i].track_id ;
2036  a.count++ ;
2037  }
2038 
2039 
2040  fcf_algo[sim->sec]->do_pad(&a, sim->sim_adc) ;
2041  LOG(DBG,"do_pad(): sec %d, row %d, pad %d: %d",sim->sec,a.row,a.pad,a.count) ;
2042  }
2043 
2044 
2045 
2046  for(int s=min_sec;s<=max_sec;s++) {
2047  if(fcf_algo[s]) {
2048 
2049  int words = fcf_algo[s]->stage2(fcf_tmp_storage,FCF_TMP_BYTES) ;
2050  if(words<=0) continue ;
2051 
2052  LOG(DBG,"Sector %d: %d words",s,words) ;
2053  u_int *p_buff = fcf_tmp_storage ;
2054  u_int *end_buff = p_buff + words ;
2055 
2056  while(p_buff < end_buff) {
2057  u_int row = *p_buff++ ;
2058  u_int cou = *p_buff++ ;
2059  int g_cou = 0 ;
2060 
2061 
2062  u_int version = (row >> 16) ;
2063  row &= 0xFFFF ;
2064 
2065 
2066  daq_sim_cld *cld = (daq_sim_cld *) cld_sim->request(cou) ;
2067 
2068  while(cou) {
2069  int skip = fcf_algo[s]->fcf_decode(p_buff, cld + g_cou, version) ;
2070 
2071  g_cou++ ;
2072 
2073  p_buff += skip ;
2074  cou-- ;
2075 
2076  }
2077 
2078  cld_sim->finalize(g_cou,s,row) ;
2079  }
2080  }
2081  }
2082 
2083 
2084  cld_sim->rewind() ;
2085 
2086  return cld_sim ;
2087 }
2088 
2089 
2090 // knows how to get the token out of an event while trying also find a l0 command
2091 int daq_tpx::get_token(char *addr, int words)
2092 {
2093  daq_trg_word trgs[128] ;
2094 
2095  get_l2(addr, words, trgs, 1) ;
2096 
2097 
2098 
2099  if(trgs[0].t==0) {
2100  LOG(ERR,"Token 0 not allowed but I will try to use the other triggers...") ;
2101  trgs[0].t = 4097 ;
2102  }
2103 
2104 
2105  return trgs[0].t ;
2106 
2107 }
2108 
2109 
2110 // HACK for Dec 2019 Trigger Cable problem of TPC Sector 12
2111 // 3rd and 4th bit were reversed on the TCD cable.
2112 static u_int swap_s12(u_int dta)
2113 {
2114  u_int n_dta = (dta & 0xFFF00000) ;
2115  for(int n=0;n<5;n++) {
2116  int dd = (dta>>(n*4)) & 0xF ;
2117 
2118  int b4 = (dd & 0x8)?1:0 ;
2119  int b3 = (dd & 0x4)?1:0 ;
2120  int b2 = (dd & 0x2)?1:0 ;
2121  int b1 = (dd & 0x1)?1:0 ;
2122 
2123  int sdd = (b3<<3)|(b4<<2)|(b2<<1)|(b1<<0) ;
2124  //LOG(TERR," %d: 0x%X %d %d %d %d 0x%X",n,dd,b4,b3,b2,b1,sdd) ;
2125 
2126  n_dta |= (sdd<<(n*4)) ;
2127  }
2128 
2129  return n_dta ;
2130 
2131 }
2132 
2133 
2134 // dumps the known accept/abort trigger decisions from
2135 // the FIFO part of the event.
2136 // returns the count
2137 int daq_tpx::get_l2(char *addr, int words, struct daq_trg_word *trgs, int do_log)
2138 {
2139  struct tpx_rdo_event rdo ;
2140  int cou = 0 ;
2141  u_int collision = 0 ;
2142  int err = 0 ;
2143 
2144  int ret = tpx_get_start(addr, words, &rdo, do_log) ;
2145  if(ret < 0) {
2146  LOG(ERR,"get_l2: broken data!") ;
2147  return 0 ;
2148  }
2149 
2150  LOG(DBG,"rdo %d, rdo token %d, trg cou %d",rdo.rdo,rdo.token,rdo.trg_cou) ;
2151 
2152  // grab only the prompt contribution...
2153  for(u_int i=0;i<rdo.trg_cou;i++) {
2154  u_int dta = rdo.trg[i].data ;
2155  u_int marker = rdo.trg[i].csr >> 24 ;
2156  u_int rhic = rdo.trg[i].rhic_counter ;
2157 
2158 #if 0
2159  if(rdo.sector==12) {
2160  dta = swap_s12(dta) ;
2161 
2162 #if 0
2163  LOG(WARN,"RDO %d: %d/%d: T %d, trg %d, daq %d: 0x%08X: marker 0x%X",rdo.rdo,i,rdo.trg_cou,
2164  dta&0xFFF,
2165  (dta>>16)&0xF,
2166  (dta>>12)&0xF,
2167  dta,marker) ;
2168 #endif
2169 
2170 
2171  }
2172 #endif
2173 
2174 
2175 //#define WANT_LOGGING
2176 #ifdef WANT_LOGGING
2177  if(rdo.rdo==1 && rdo.sector==1) {
2178  int delta = rhic - rdo.trg[0].rhic_counter ;
2179  LOG(TERR,"RDO %d: trg %d/%d: dta 0x%08X, CSR 0x%08X, RHIC %u, delta %u",rdo.rdo,
2180  i,rdo.trg_cou,
2181  dta,rdo.trg[i].csr,rhic,
2182  delta) ;
2183 
2184  }
2185 #endif
2186 
2187  if((marker==0) || (marker==0xEE)) { // marks the prompt configuration
2188 
2189  trgs[cou].t = dta & 0xFFF ;
2190  trgs[cou].daq = (dta >> 12) & 0xF ;
2191  trgs[cou].trg = (dta >> 16) & 0xF ;
2192  trgs[cou].rhic_delta = 0 ;
2193  trgs[cou].rhic = rhic ;
2194  trgs[cou].reserved[0] = 0xF0000000 | (0x0FFFFFFF & dta) ; // 0xF for "fired"
2195 
2196 
2197 
2198  switch(trgs[cou].trg) {
2199  case 4 : // physics
2200  case 8 : // interleaved laser
2201  case 9 : // usual laser
2202  case 10 : // pulser
2203  case 6 : // sector 12 interleaved laser
2204  case 7 : // sector 12 normal laser
2205  break ;
2206  default :
2207  LOG(ERR,"RDO %d: T %d: prompt: bad trg: 0x%08X",rdo.rdo,trgs[cou].t,dta) ;
2208  err = 1 ;
2209  continue ;
2210  }
2211 
2212  if(trgs[cou].t==0) {
2213  LOG(ERR,"RDO %d: token 0 (prompt) -- ignoring: 0x%08X",rdo.rdo,dta) ;
2214  err = 1 ;
2215  continue ;
2216  }
2217 
2218  // check for busy overrun
2219  if((dta & 0x3000000) != 0x2000000) {
2220  LOG(ERR,"RDO %d: T %d: prompt: BUSY overrun: 0x%08X",rdo.rdo,trgs[cou].t,dta) ;
2221  err = 1 ;
2222  continue ;
2223  }
2224 
2225  if(cou) {
2226  LOG(ERR,"RDO %d: duplicate prompt trigger",rdo.rdo) ;
2227  err = 1 ;
2228  continue ;
2229  }
2230 
2231  cou++ ;
2232 
2233  collision = rhic ; // mark the collission time...
2234  }
2235 
2236  }
2237 
2238  if(cou==0) { // no prompt trigger contrib; use the one from the RDO header.
2239  // either for a logging event (4096) or the one from the trigger-only (4097)
2240 
2241  if(rdo.token != 4096) {
2242  rdo.token = 4097 ;
2243  LOG(NOTE,"No triggers in event, making it %d",rdo.token) ;
2244  }
2245 
2246  trgs[cou].t = rdo.token ;
2247  trgs[cou].daq = 0 ;
2248  trgs[cou].trg = 4 ; // dummy
2249  trgs[cou].rhic_delta = 0 ;
2250  trgs[cou].rhic = 0 ;
2251  trgs[cou].reserved[0] = 0 ;
2252 
2253  cou++ ;
2254 
2255  }
2256  else if (cou > 1) {
2257  LOG(ERR,"RDO %d: token %d? -- too many prompt contributions!",rdo.rdo,trgs[0].t) ;
2258  err = 1 ;
2259  }
2260 
2261  // at this point at least one contribution exists (i.e. cou>=1):
2262  // real L0 t
2263  // 4096 for log events
2264  // 4097 for events without an L0
2265 
2266  if(trgs[0].t == 0) {
2267  LOG(ERR,"Token 0 in RDO %d: making it 4097",rdo.rdo) ;
2268  err = 1 ;
2269  trgs[0].t = 4097 ;
2270  }
2271 
2272  for(u_int i=0;i<rdo.trg_cou;i++) {
2273  u_int dta = rdo.trg[i].data ;
2274  u_int marker = rdo.trg[i].csr >> 24 ;
2275  u_int rhic = rdo.trg[i].rhic_counter ;
2276 #if 0
2277  if(rdo.sector==12) {
2278  dta = swap_s12(dta) ;
2279  }
2280 #endif
2281 
2282  if(marker==0xFF) { // FIFO
2283  int daq10k = 0 ;
2284 
2285  trgs[cou].t = dta & 0xFFF ;
2286  trgs[cou].daq = (dta >> 12) & 0xF ;
2287  trgs[cou].trg = (dta >> 16) & 0xF ;
2288  trgs[cou].rhic = rhic ;
2289  trgs[cou].reserved[0] = 0x20000000 | (0x0FFFFFFF & dta) ;
2290 
2291  switch(trgs[cou].trg) {
2292  case 13 : // L2 ABORT
2293  case 14 : // L1 ACCEPT
2294  case 15 : // L2 ACCEPT
2295  break ;
2296 
2297  case 2 : // RESET
2298 
2299  if((trgs[cou].t == 0x345) && (trgs[cou].daq == 2)) { // reset at run start from L0
2300  continue ; // skip it
2301  }
2302  if((trgs[cou].t == 1001) && (trgs[cou].daq == 3)) { // reset at run start from TCD in local
2303  continue ; // skip it
2304  }
2305 
2306  // WARNING: no "break" here!!!
2307 
2308 
2309  default : // readout command; BUT not necessarily for DAQ10k!
2310 
2311  // check for overrun UNLESS the actual command
2312 
2313  if((dta & 0x03A00000) == 0x00800000) {
2314  //LOG(WARN,"DAQ10k trigger; no data") ;
2315  daq10k = 1 ;
2316  }
2317  else {
2318  if((dta & 0x3000000) != 0x2000000) {
2319  if(trgs[0].trg == 9) { // laser!
2320  LOG(NOTE,"RDO %d: T %d: FIFO: BUSY overrun: 0x%08X",rdo.rdo,trgs[cou].t,dta) ;
2321  }
2322  else {
2323  LOG(ERR,"RDO S%d:%d: T %d: FIFO: BUSY overrun: 0x%08X",rdo.sector,rdo.rdo,trgs[cou].t,dta) ;
2324  err = 1 ;
2325  }
2326  }
2327  }
2328 
2329  switch(trgs[cou].trg) {
2330  case 4 : // physics
2331  case 8 : // interspersed laser
2332  case 9 : // laser
2333  case 10 : // pulser
2334  case 6 :
2335  case 7 :
2336  break ;
2337  default:
2338  if(trgs[0].trg == 9) { //laser!
2339  LOG(NOTE,"RDO %d: T %d: FIFO: bad trg_cmd: 0x%08X",rdo.rdo,trgs[cou].t,dta) ;
2340  }
2341  else {
2342  LOG(ERR,"RDO %d: T %d: FIFO: bad trg_cmd: 0x%08X",rdo.rdo,trgs[cou].t,dta) ;
2343  err = 1 ;
2344  }
2345  break ;
2346  }
2347 
2348  trgs[cou].reserved[0] = 0xA0000000 | (0x0FFFFFFF & dta) ; // maybe DAQ10k?
2349 
2350  // new in FY12, DAQ10k -- we leave all of them for receiver.C!
2351 
2352  if(daq10k) break ; // use it
2353  else continue ; // skip it...
2354 
2355  }
2356 
2357  // we took out all the FIFO l0 contribs here!
2358 
2359  if(rhic >= collision) {
2360  trgs[cou].rhic_delta = rhic - collision ;
2361  }
2362  else {
2363  trgs[cou].rhic_delta = -(collision - rhic) ;
2364  }
2365 
2366  if(trgs[cou].rhic_delta == 1) {
2367  LOG(NOTE,"RDO %d: T %d: FIFO: delta == 1: 0x%08X",rdo.rdo,trgs[cou].t,dta) ;
2368  if(trgs[0].trg == 9) { // a laser...
2369  continue ;
2370  }
2371  }
2372 
2373  if((trgs[cou].t == 0)) {
2374  LOG(ERR,"RDO %d: token 0 in L2 contribution 0x%08X -- skipping",rdo.rdo,dta) ;
2375  err = 1 ;
2376  continue ;
2377  }
2378 
2379  cou++ ;
2380  }
2381 
2382  }
2383 
2384  if(err) { // dump out everyhign
2385  LOG(ERR," RDO %d: words %d",rdo.rdo,words) ;
2386  for(u_int i=0;i<rdo.trg_cou;i++) {
2387  LOG(ERR," RDO %d: T %4d: %d/%d: data 0x%08X, CSR 0x%08X, RHIC %u",rdo.rdo, rdo.token, i, rdo.trg_cou, rdo.trg[i].data, rdo.trg[i].csr, rdo.trg[i].rhic_counter) ;
2388  }
2389  }
2390 /*
2391  else if((rdo.rdo>=6) && ((rdo.sector==24))) {
2392  LOG(TERR," RDO %d: words %d",rdo.rdo,words) ;
2393  for(u_int i=0;i<rdo.trg_cou;i++) {
2394  LOG(TERR," dbg: RDO %d: T %4d: %d: data 0x%08X, CSR 0x%08X, res 0x%08X, RHIC %u",rdo.rdo, rdo.token, i, rdo.trg[i].data, rdo.trg[i].csr, trgs[i].reserved[0],rdo.trg[i].rhic_counter) ;
2395  }
2396  }
2397 */
2398 
2399 
2400 
2401  return cou ;
2402 }
Definition: tpxFCF.h:87
Definition: tpxPed.h:14
Definition: rb.hh:21
void version(std::ostream &os=std::cout)
print HepMC version
Definition: Version.h:27
Definition: daq_tpc.h:22
Definition: tpx23.h:48