StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
daq_etof.cxx
1 #include <assert.h>
2 #include <sys/types.h>
3 #include <errno.h>
4 
5 #include <rtsLog.h>
6 #include <rtsSystems.h>
7 #include <daqFormats.h>
8 
9 #include <SFS/sfs_index.h>
10 
11 #include <DAQ_READER/daqReader.h>
12 #include <DAQ_READER/daq_dta.h>
13 
14 
15 #include "daq_etof.h"
16 
17 extern int etof_reader(char *mem, struct etof_t *etof, u_int driver) ;
18 
19 
20 
21 
22 const char *daq_etof::help_string = "\
23 \n\
24 ETOF Help: \n\
25 Supported Banks: \n\
26  raw returns=ptr of start of DDL data; c1=sector[1..1]; c2=rdo[1..4]; \n\
27 \n\
28 \n\
29 " ;
30 
32 {
33 public:
35  daq_det_factory::det_factories[ETOF_ID] = this ;
36  }
37 
38  daq_det *create() {
39  return new daq_etof ;
40  }
41 } ;
42 
43 static daq_det_etof_factory etof_factory ;
44 
45 
46 
47 daq_etof::daq_etof(daqReader *rts_caller)
48 {
49  rts_id = ETOF_ID ;
50  name = rts2name(rts_id) ;
51  sfs_name = "etof" ;
52  caller = rts_caller ;
53  if(caller) caller->insert(this, rts_id) ;
54 
55  raw = new daq_dta ;
56 
57 
58 
59  LOG(DBG,"%s: constructor: caller %p",name,rts_caller) ;
60  return ;
61 }
62 
63 daq_etof::~daq_etof()
64 {
65  LOG(DBG,"%s: DEstructor",name) ;
66 
67  delete raw ;
68 
69 
70  return ;
71 }
72 
73 
74 
75 daq_dta *daq_etof::get(const char *bank, int sec, int row, int pad, void *p1, void *p2)
76 {
77  Make() ;
78 
79  if(present==0) return 0 ;
80 
81  LOG(DBG,"%s: looking for bank %s",name,bank) ;
82 
83  if(strcmp(bank,"*")==0) bank = "raw" ;
84 
85 
86 
87  if(strcasecmp(bank,"raw")==0) {
88  if((present & DET_PRESENT_SFS)==0) return 0 ; // no DDL
89  return handle_raw(sec,row) ; // actually sec, rdo; r1 is the number of bytes
90  }
91  else {
92  LOG(ERR,"%s: unknown bank type \"%s\"",name,bank) ;
93  }
94 
95  return 0 ;
96 }
97 
98 daq_dta *daq_etof::handle_raw(int sec, int rdo)
99 {
100  char str[128] ;
101 
102  // bring in the bacon from the SFS file....
103  assert(caller) ;
104 
105 
106  sprintf(str,"%s/sec01/rb01/raw",sfs_name) ;
107  char *full_name = caller->get_sfs_name(str) ;
108 
109  LOG(DBG,"%s: trying sfs on \"%s\"",name,str) ;
110  if(full_name == 0) return 0 ;
111 
112  int size = caller->sfs->fileSize(full_name) ; // this is bytes
113 
114  LOG(DBG,"Got size %d",size) ;
115  if(size <= 0) {
116  LOG(DBG,"%s: %s: not found in this event",name,str) ;
117  return 0 ;
118  }
119 
120  char *ptr = (char *) malloc(size) ;
121  LOG(DBG,"Malloc at %p",ptr) ;
122 
123  caller->sfs->read(full_name, ptr, size) ;
124 
125  LOG(DBG,"sfs read succeeded") ;
126 
127 #if 0
128  {
129  daq_trg_word trg ;
130  int ret = get_l2(ptr,size*4,&trg,1) ;
131  LOG(NOTE,"get_l2 returns %d: %d %d %d",ret,trg.trg,trg.daq,trg.t) ;
132  }
133 
134  LOG(TERR,"ETOF size %d",size) ;
135  u_int *d32 = (u_int *)ptr ;
136  for(int i=0;i<size/4;i++) {
137  LOG(TERR,"%d = 0x%08X",i,d32[i]) ;
138  }
139 #endif
140 
141  //I need to skip the 40 byte bankHeader!
142 
143  size -= 40 ; //bank header
144 
145  raw->create(size,"etof_raw",rts_id,DAQ_DTA_STRUCT(u_char)) ;
146 
147  char *st = (char *) raw->request(size) ;
148 
149 
150  memcpy(st,ptr+40,size) ;
151  free(ptr) ;
152 
153 
154  raw->finalize(size,1,1,0) ;
155 
156  raw->rewind() ;
157 
158  return raw ;
159 
160 }
161 
162 // knows how to get the token out of an event...
163 int daq_etof::get_token(char *addr, int words)
164 {
165  LOG(ERR,"get_token") ;
166 
167  int cou ;
168  struct daq_trg_word trg[128] ;
169 
170  cou = get_l2(addr,words,trg,1) ;
171 
172  if(cou==0) return -1000 ; // special marker...
173  if(trg[0].t==0) return -ENOSYS ;
174 
175  return trg[0].t ;
176 }
177 
178 
179 
180 /*
181  Trigger info is in the bank header crc field as
182 
183  0-3 trg_cmd
184  4-7 daq_cmd
185  8-19 token
186  20 star_trigger flag
187  21 star_trgx flag
188  22 star_trg4 flag
189  23 star_busy flag
190 */
191 
192 // knows how to get a/the L2 command out of the event...
193 int daq_etof::get_l2(char *addr, int words, struct daq_trg_word *trg, int rdo)
194 {
195  int t_cou = 0 ;
196  int in_words = words ;
197  int err = 0 ;
198  static int token ;
199 
200  //LOG(TERR,"get_l2") ;
201  struct bankHeader *bh ;
202 
203  bh = (bankHeader *)addr ;
204 
205  LOG(NOTE,"Token %d, CRC word 0x%08X, format number 0x%08X",bh->token,bh->crc,bh->format_number) ;
206 
207  trg[0].t = bh->crc & 0xFFF ;
208  trg[0].trg = (bh->crc>>16)&0xF ;
209  trg[0].daq = (bh->crc>>12)&0xF ;
210 
211  t_cou++ ;
212 
213  if(err) {
214  LOG(ERR,"[%d] Bad Event: T %4d: words %d",
215  rdo,trg[0].t,in_words) ;
216 
217  }
218 
219  if(err & 1) { // critical -- blow the whole event
220  return -1 ;
221  }
222 
223  return t_cou ;
224 }
225