StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
l3_reader.cxx
1 #include <stdio.h>
2 #include <string.h>
3 #include <arpa/inet.h>
4 
5 #include <rtsLog.h>
6 #include <rtsSystems.h>
7 #include <rts.h>
8 
9 #include "daq_l3.h"
10 
11 
12 inline float fswap(float swapped)
13 {
14  unsigned int* uintptr = (unsigned int*) &swapped;
15  unsigned int uintvar = l2h32(*uintptr);
16  float* floatvar = (float*)&uintvar;
17  return *floatvar;
18 }
19 
20 
21 #ifdef RTS_LITTLE_ENDIAN
22 #define l2hfloat(x) (x)
23 //#define b2hfloat(x) (fswap(x))
24 #else
25 #define l2hfloat(x) (fswap(x))
26 //#define b2hfloat(x) (x)
27 #endif
28 
29 
30 int l3_reader(char *m, struct l3_t *l3, u_int driver)
31 {
32  struct DATAP *datap = (struct DATAP *)m ;
33  struct L3_P *l3p ;
34  int len, off ;
35  char *l3_gtd_start = 0 ;
36  int fy09_format = 0 ;
37 
38  l3->max_channels = 1280000 ;
39  l3->channels = 0 ;
40  l3->mode = 1 ;
41 
42 // Tonko, zero this out and make non-sensical in case of further
43 // problems down the road
44  l3->tracks_num = 0 ;
45  l3->cluster_num = 0 ;
46  l3->xVertex = -1000.0 ;
47  l3->yVertex = -1000.0 ;
48  l3->zVertex = -1000.0 ;
49 
50 
51  if(datap == NULL) return 0 ;
52 
53  if(driver) { // "m" points directly to L3_GTD! Used in FY09 HLT data
54  l3_gtd_start = m ; // directly points to L3_GTD!
55  fy09_format = 1 ;
56 
57  l3->mode = 0 ; // this holds the trigger def
58  l3->channels = 0 ; // this holds the sequnce number
59  }
60  else {
61 
62  len = ntohl(datap->det[L3_ID].len) * 4 ;
63  if(len == 0) return 0 ;
64 
65  off = ntohl(datap->det[L3_ID].off) ;
66  if(off == 0) return 0 ;
67 
68  l3p = (struct L3_P *)((u_int *)m+off) ;
69 
70 
71  len = l3p->bh.length;
72 
73 
74  LOG(DBG,"L3_P bytes %d",len,0,0,0) ;
75 
76  if(checkBank(l3p->bh.bank_type,(char *)"L3_P") < 0) {
77  return -1 ;
78  }
79 
80 
81  if(l3p->tracks.len && l3p->tracks.off){
82  l3_gtd_start = ((char*)l3p + l2h32(l3p->tracks.off)*4) ;
83  }
84  }
85 
86 
87  if(!l3_gtd_start) return 0 ;
88 
89 // if(l3p->tracks.len && l3p->tracks.off){
90 // struct L3_GTD* l3gtd = (struct L3_GTD*)((char*)l3p + l2h32(l3p->tracks.off)*4) ;
91 
92  if(l3_gtd_start) {
93  struct L3_GTD* l3gtd = (struct L3_GTD *) l3_gtd_start ;
94 
95 // Tonko, sanity check
96  if(checkBank(l3gtd->bh.bank_type,(char *)"L3_GTD") < 0) {
97  return -1 ;
98  }
99 
100  if(fy09_format) { // override stuff
101  l3->mode = l2h32(l3gtd->bh.crc) ; // contains the decision!
102  l3->channels = l2h32(l3gtd->bh.w9) ; // contains the event sequence number from trigger!
103  }
104 
105  l3->tracks_num = l2h32(l3gtd->nTracks);
106  l3->cluster_num = l2h32(l3gtd->nHits);
107  l3->xVertex = l2hfloat(l3gtd->xVert);
108  l3->yVertex = l2hfloat(l3gtd->yVert);
109  l3->zVertex = l2hfloat(l3gtd->zVert);
110 
111 // Tonko, sanity check
112  if(l3->tracks_num >= L3_MAX_NR_TRACKS) {
113  LOG(ERR,"L3 track number %d > %d! Maxing them out...",l3->tracks_num,L3_MAX_NR_TRACKS ,0,0,0) ;
114  l3->tracks_num = L3_MAX_NR_TRACKS ;
115  }
116 
117 
118  for (unsigned int i=0; i<l3->tracks_num; i++) {
119  global_track *tr = &(l3gtd->track[i]);
120 
121  l3->track[i].id = l2h32(tr->id);
122 
123 #ifndef UNIX_LITTLE_ENIDAN
124  l3->track[i].flag = tr->flag;
125  l3->track[i].innerMostRow = tr->innerMostRow;
126  l3->track[i].outerMostRow = tr->outerMostRow;
127 
128  l3->track[i].nHits = tr->nHits;
129  l3->track[i].ndedx = tr->ndedx;
130  l3->track[i].q = tr->q;
131 #else
132  l3->track[i].flag = ( ((unsigned short)tr->innerMostRow) |
133  ((unsigned short)tr->outerMostRow)<<8);
134  l3->track[i].innerMostRow = (char)( tr->flag & 0x00ff );
135  l3->track[i].outerMostRow = (char)((tr->flag & 0xff00)>>8);
136 
137  l3->track[i].nHits = (unsigned char)tr->q;
138  l3->track[i].reserved = (char)tr->ndedx;
139  l3->track[i].ndedx = (unsigned char)tr->reserved;
140  l3->track[i].q = (char)tr->nHits;
141 #endif
142  l3->track[i].chi2[0] = l2hfloat(tr->chi2[0]);
143  l3->track[i].chi2[1] = l2hfloat(tr->chi2[1]);
144  l3->track[i].dedx = l2hfloat(tr->dedx);
145  l3->track[i].pt = l2hfloat(tr->pt);
146  l3->track[i].phi0 = l2hfloat(tr->phi0);
147  l3->track[i].psi = l2hfloat(tr->psi);
148  l3->track[i].r0 = l2hfloat(tr->r0);
149  l3->track[i].tanl = l2hfloat(tr->tanl);
150  l3->track[i].z0 = l2hfloat(tr->z0);
151  l3->track[i].length = l2hfloat(tr->length);
152  l3->track[i].dpt = l2hfloat(tr->dpt);
153  l3->track[i].dpsi = l2hfloat(tr->dpsi);
154  l3->track[i].dz0 = l2hfloat(tr->dz0);
155  l3->track[i].dtanl = l2hfloat(tr->dtanl);
156 
157  }
158 
159 
160  }
161 
162 
163 #ifdef SHOW_DEBUG_INFO
164 
165 #ifdef UNIX_LITTLE_ENDIAN
166  printf("Running on LITTLE endian machine\n");
167 #else
168  printf("Running on BIG endian machine\n");
169 #endif
170 
171  printf("\nVertex: (%6.2f/%6.2f/%6.2f)\n",
172  l3->xVertex, l3->yVertex, l3->zVertex);
173 
174  printf("Tracks: %5d Clusters %7d\n",
175  l3->tracks_num, l3->cluster_num);
176 
177  for (unsigned int i=0; i<l3->tracks_num; i++) {
178  printf("%5d: pt=%5.3f z0=%7.2f q=%2d nHits=%2d ndedx=%2d ",
179  i, l3->track[i].pt, l3->track[i].z0, l3->track[i].q,
180  l3->track[i].nHits, l3->track[i].ndedx);
181 
182  printf("flag=0x%04x iRow=%2d oRow=%2d\n",
183  l3->track[i].flag, l3->track[i].innerMostRow,
184  l3->track[i].outerMostRow);
185  }
186 
187 #endif
188 
189  return 1 ; // anything positive...
190 
191 }
192 
Definition: daq_l3.h:10