StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
daq_rhicf.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_rhicf.h"
16 
17 extern int rhicf_reader(char *mem, struct rhicf_t *rhicf, u_int driver) ;
18 
19 
20 
21 
22 const char *daq_rhicf::help_string = "\
23 \n\
24 RHICF 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[RHICF_ID] = this ;
36  }
37 
38  daq_det *create() {
39  return new daq_rhicf ;
40  }
41 } ;
42 
43 static daq_det_rhicf_factory rhicf_factory ;
44 
45 
46 
47 daq_rhicf::daq_rhicf(daqReader *rts_caller)
48 {
49  rts_id = RHICF_ID ;
50  name = rts2name(rts_id) ;
51  sfs_name = "rhicf" ;
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_rhicf::~daq_rhicf()
64 {
65  LOG(DBG,"%s: DEstructor",name) ;
66 
67  delete raw ;
68 
69 
70  return ;
71 }
72 
73 
74 
75 daq_dta *daq_rhicf::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_rhicf::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  //I need to skip the 40 byte bankHeader!
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  size -= 40 ; //bank header
134 
135  raw->create(size,"rhicf_raw",rts_id,DAQ_DTA_STRUCT(u_char)) ;
136 
137  char *st = (char *) raw->request(size) ;
138 
139  memcpy(st,ptr+40,size) ;
140  free(ptr) ;
141 
142 
143  raw->finalize(size,1,1,0) ;
144 
145  raw->rewind() ;
146 
147  return raw ;
148 
149 }
150 
151 // knows how to get the token out of an event...
152 int daq_rhicf::get_token(char *addr, int words)
153 {
154  LOG(ERR,"get_token") ;
155 
156  int cou ;
157  struct daq_trg_word trg[128] ;
158 
159  cou = get_l2(addr,words,trg,1) ;
160 
161  if(cou==0) return -1000 ; // special marker...
162  if(trg[0].t==0) return -ENOSYS ;
163 
164  return trg[0].t ;
165 }
166 
167 
168 
169 /*
170  Trigger info is in the bank header crc field as
171 
172  0-3 trg_cmd
173  4-7 daq_cmd
174  8-19 token
175  20 star_trigger flag
176  21 star_trgx flag
177  22 star_trg4 flag
178  23 star_busy flag
179 */
180 
181 // knows how to get a/the L2 command out of the event...
182 int daq_rhicf::get_l2(char *addr, int words, struct daq_trg_word *trg, int rdo)
183 {
184  int t_cou = 0 ;
185  int in_words = words ;
186  int err = 0 ;
187  static int token ;
188 
189  //LOG(TERR,"get_l2") ;
190  struct bankHeader *bh ;
191 
192  bh = (bankHeader *)addr ;
193 
194  LOG(NOTE,"Token %d, CRC word 0x%08X, format number 0x%08X",bh->token,bh->crc,bh->format_number) ;
195 
196  trg[0].t = bh->crc & 0xFFF ;
197  trg[0].trg = (bh->crc>>16)&0xF ;
198  trg[0].daq = (bh->crc>>12)&0xF ;
199 
200  t_cou++ ;
201 
202  if(err) {
203  LOG(ERR,"[%d] Bad Event: T %4d: words %d",
204  rdo,trg[0].t,in_words) ;
205 
206  }
207 
208  if(err & 1) { // critical -- blow the whole event
209  return -1 ;
210  }
211 
212  return t_cou ;
213 }
214