StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
trg_reader.cxx
1 #include <stdio.h>
2 #include <string.h>
3 #include <arpa/inet.h>
4 
5 #include <rtsLog.h>
6 
7 #define TRG_VERSION 0x32
8 
9 #include <daqFormats.h>
10 #include <rts.h>
11 #include <rtsSystems.h>
12 
13 
14 
15 #include "daq_trg.h"
16 
17 extern int trgReader12(char *trgd, struct trg_t *trg) ;
18 extern int trgReader20(char *trgd, struct trg_t *trg) ;
19 extern int trgReader21(char *trgd, struct trg_t *trg) ; //
20 extern int trgReader22(char *trgd, struct trg_t *trg) ; //
21 extern int trgReader30(char *trgd, struct trg_t *trg);
22 extern int trgReader32(char *trgd, struct trg_t *trg); // called by trgReader10 2007 updates....
23 extern int trgReader10(char *trgd, struct trg_t *trg);
24 
25 // navigates to the start of the raw trigger data i.e. DATAP->TRGP->TRGD->data
26 
27 char *trg_find_raw(char *m, int *bytes)
28 {
29  struct TRGP *trgp ;
30  struct TRGD *trgd ;
31  struct DATAP *datap ;
32  int len, off ;
33  int swapit ;
34 
35  *bytes = 0 ;
36 
37  if(m == 0) return 0 ;
38  datap = (struct DATAP *) m ;
39 
40  swapit = 0 ;
41  if(datap->bh.byte_order != DAQ_RAW_FORMAT_ORDER) swapit = 1 ;
42 
43 
44  len = qswap32(swapit, datap->det[TRG_ID].len);
45  if(len == 0) return 0 ;
46  len *= 4 ;
47 
48  off = qswap32(swapit, datap->det[TRG_ID].off);
49  if(off == 0) return 0 ;
50 
51  LOG(DBG,"Trg raw len %d (0x%x), off %d(0x%x)",len,len,off,off,0) ;
52 
53  trgp = (struct TRGP *)((u_int *) m + off) ;
54  if(checkBank(trgp->bh.bank_type,"TRGP") < 0) { // wrong bank!
55  return 0 ;
56  }
57 
58  swapit = 0 ;
59  if(trgp->bh.byte_order != DAQ_RAW_FORMAT_ORDER) swapit = 1 ;
60 
61 
62  if(trgp->trgData.len == 0) return 0 ; // no raw data but so far OK, I guess
63  if(trgp->trgData.off == 0) return 0 ;
64 
65  off = qswap32(swapit, trgp->trgData.off);
66 
67  trgd = (struct TRGD *) ((u_int *)trgp + off) ;
68 
69 
70  swapit = 0 ;
71  if(trgd->bh.byte_order != DAQ_RAW_FORMAT_ORDER) swapit = 1;
72 
73  // check misc. things
74  if(checkBank(trgd->bh.bank_type,"TRGD") < 0) {
75  return 0 ;
76  }
77 
78  len = qswap32(swapit, trgd->bh.length) ;
79 
80  len -= 10 ; // skip the DAQ bank header ;
81 
82  *bytes = len*4 ; // we want bytes
83 
84  LOG(DBG,"Returning pointer to raw trigger data of %d bytes", *bytes) ;
85 
86  return (char *) &(trgd->desc) ; // points to the start of trigger descriptor
87 
88 }
89 
90 // read the Trigger RAW data
91 int trg_reader(char *m, struct trg_t *trg, u_int driver, u_int evp_daqbits)
92 {
93  struct TRGP *trgp ;
94  struct TRGD *trgd ;
95  struct DATAP *datap ;
96  int len, off ;
97  int trgp_banks ;
98 
99  int swapdatap=0;
100  int swaptrgp=0;
101  int swaptrgid=0;
102  int swaptrgd=0;
103 
104  // zap all first
105  memset(trg,0,sizeof(trg_t)) ;
106 
107  trg->mode = 0 ;
108  trg->max_channels = 240 * 128 * 6 ;
109  trg->channels = 0 ;
110  trg->trgc = NULL ;
111 
112  // Tonko: FY09... this is very ugly...
113  trg->daqbits = evp_daqbits; // ugly hack...
114 
115  if(m == NULL) return 0 ;
116 
117  datap = (struct DATAP *) m ;
118  if(datap->bh.byte_order != DAQ_RAW_FORMAT_ORDER)
119  swapdatap = 1;
120 
121 
122  len = qswap32(swapdatap, datap->det[TRG_ID].len);
123  if(len == 0) return 0 ;
124  len *= 4 ;
125 
126  off = qswap32(swapdatap, datap->det[TRG_ID].off);
127  if(off == 0) return 0 ;
128 
129  LOG(DBG,"Trg raw len %d (0x%x), off %d(0x%x)",len,len,off,off,0) ;
130 
131  trgp = (struct TRGP *)((u_int *)m+off) ;
132  if(checkBank(trgp->bh.bank_type,"TRGP") < 0) { // wrong bank!
133  return 0 ;
134  }
135 
136  if(trgp->bh.byte_order != DAQ_RAW_FORMAT_ORDER)
137  swaptrgp = 1;
138 
139  if(trgp->bh.token == 0) {
140  LOG(ERR,"Token 0 - skipping...",0,0,0,0,0) ;
141  return 0 ;
142  }
143 
144  // number of banks present in TGRP: 1 for TRGD only (old), 2 for TRGD & TRGID (new)
145  trgp_banks = qswap32(swaptrgp, trgp->bh.length);
146 
147  trgp_banks = (trgp_banks - 10)/2 ;
148 
149 
150  LOG(DBG,"TRGP bytes %d, TRGD off:len 0x%X:%d (banks %d)",
151  qswap32(swaptrgp, trgp->bh.length)*4,
152  qswap32(swaptrgp, trgp->trgData.off),
153  qswap32(swaptrgp, trgp->trgData.len),
154  trgp_banks) ;
155 
156  // new: TRGID
157  if(trgp_banks >= 2) {
158  if(trgp->trgId.len && trgp->trgId.off) {
159  int off = qswap32(swaptrgp, trgp->trgId.off);
160  struct TRGID *trgid = (struct TRGID *)((char *)trgp + 4*off) ;
161 
162  if(trgid->bh.byte_order != DAQ_RAW_FORMAT_ORDER) swaptrgid = 1;
163 
164  if(checkBank(trgid->bh.bank_type,CHAR_TRGID) >= 0) {
165  int i ;
166 
167  for(i=0;i<32;i++) {
168  if(evp_daqbits & (1 << i)) {
169  LOG(DBG,"TRGID %d: bit %2d is 0x%02X [%u dec]",i,i,
170  qswap32(swaptrgid, trgid->triggerId[i]),
171  qswap32(swaptrgid, trgid->triggerId[i]), 0);
172  trg->offline_id[i] = qswap32(swaptrgid, trgid->triggerId[i]);
173  }
174 
175  }
176  }
177  }
178  }
179 
180  if(trgp->trgData.len == 0) return 0 ; // no raw data but so far OK, I guess
181  if(trgp->trgData.off == 0) return 0 ;
182 
183  off = qswap32(swaptrgp, trgp->trgData.off);
184  trgd = (struct TRGD *) ((char *)trgp + 4*off) ;
185 
186  if(trgd->bh.byte_order != DAQ_RAW_FORMAT_ORDER) swaptrgd = 1;
187 
188  // check misc. things
189  if(checkBank(trgd->bh.bank_type,"TRGD") < 0) {
190  return 0 ;
191  }
192 
193  LOG(DBG,"TRGD len %d",
194  qswap32(swaptrgd, trgd->bh.length),0,0,0) ;
195 
196  if(qswap32(swaptrgd, trgd->bh.length) == 10) return 0 ; // no trigger data - just bh
197 
198 
199  trg->trgc = (u_char *) trgd ;
200 
201  u_char trg_version = trgd->desc.TrgDataFmtVer ;
202 
203 
204  switch(trg_version) {
205  case 0x12 : // pre Jan 2002
206  case 0x13 : // pre Jan 2002
207  LOG(NOTE,"TRG: version 0x%02X supported...",trg_version,0,0,0,0) ;
208  trgReader12((char *)trgd, trg) ;
209  break;
210  case 0x20 : // 2002-2003
211  LOG(NOTE,"TRG: version 0x%02X supported...",trg_version,0,0,0,0) ;
212  trgReader20((char *)trgd, trg) ;
213  break ;
214  case 0x21 : // 2003-2004
215  LOG(NOTE,"TRG: version 0x%02X supported...",trg_version,0,0,0,0) ;
216  trgReader21((char *)trgd, trg) ;
217  break ;
218  case 0x22 : // Nov 2004 - BBC extended
219  LOG(NOTE,"TRG: version 0x%02X supported...",trg_version,0,0,0,0) ;
220  trgReader22((char *)trgd, trg) ;
221  break ;
222  case 0x30 : // 30/31 same but for vpd order which was always wrong
223  case 0x31 : // in 30
224  LOG(NOTE, "TRG: version 0x%02X supported", trg_version,0,0,0,0);
225  trgReader30((char *)trgd, trg);
226  break;
227 
228  case 0x10 :
229  LOG(NOTE, "TRG: version 0x%02X supported, ", trg_version, 0,0,0,0);
230 
231  trgReader10((char *)trgd,trg);
232  break;
233 
234  default :
235  LOG(ERR,"Can't read Trigger Format Version 0x%02X!",trg_version,0,0,0,0) ;
236  return 0 ;
237  }
238 
239 
240  return len ;
241 }
242 
243 
Definition: daq_trg.h:9