StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
sstPed.cxx
1 #include <stdio.h>
2 #include <sys/types.h>
3 #include <math.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <errno.h>
7 #include <time.h>
8 #include <unistd.h>
9 
10 #include <rtsLog.h>
11 #include <daqModes.h>
12 #include <rtsSystems.h>
13 
14 #include <DAQ_READER/daq_dta.h>
15 
16 #include "daq_sst.h"
17 #include "sstPed.h"
18 
19 
20 
21 sstPed::sstPed()
22 {
23  valid = 0 ;
24  rb_mask = 0x07 ; // assume max...
25  sector = 1 ; // assume 1
26 
27  sizeof_ped = sizeof(daq_sst_ped_t) * SST_RDO_COU ; // for SST_RDO_COU RDOs
28 
29  ped_store = 0 ; // unassigned!
30 
31  sst_rdr = 0 ;
32 
33  return ;
34 }
35 
36 
37 sstPed::~sstPed()
38 {
39  if(ped_store) {
40  free(ped_store) ;
41  ped_store = 0 ;
42  }
43 
44  if(sst_rdr) {
45  delete sst_rdr ;
46  sst_rdr = 0 ;
47  }
48 
49  valid = 0 ;
50 
51  return ;
52 }
53 
54 
55 void sstPed::init(int active_rbs)
56 {
57  valid = 0 ;
58 
59  rb_mask = active_rbs ;
60 
61  if(ped_store == 0) {
62  ped_store = (daq_sst_ped_t *) malloc(sizeof_ped) ;
63  }
64 
65  if(sst_rdr == 0) sst_rdr = new daq_sst(0) ;
66 
67  memset(ped_store,0,sizeof_ped) ;
68 
69  memset(sst_rdr->events,0,sizeof(sst_rdr->events)) ;
70  memset(sst_rdr->fiber_events,0,sizeof(sst_rdr->fiber_events)) ;
71 
72  LOG(TERR,"Pedestals zapped: rb_mask 0x%02X",rb_mask) ;
73 }
74 
75 
76 
77 
78 /*
79  Called per event, per RDO. evbbuff is the raw RDO contribuition.
80  rdo counts from 1.
81 */
82 void sstPed::accum(char *evbuff, int bytes, int rdo1)
83 {
84  LOG(NOTE,"entering %d",rdo1) ;
85 
86  int rdo = rdo1 - 1 ; // since rdo1 is from 1
87 
88  LOG(NOTE,"accum %d",rdo1) ;
89 
90  daq_sst_ped_t *p = ped_store + rdo ;
91 
92  sst_rdr->raw_to_adc_utility(sector,rdo1,evbuff,bytes/4,p,2) ;
93 
94  LOG(NOTE,"accum done %d",rdo1) ;
95 
96  return ;
97 
98 }
99 
100 
101 void sstPed::calc()
102 {
103 
104  const u_int MIN_EVENTS = 90 ;
105 
106 
107  LOG(NOTE,"Calculating pedestals") ;
108 
109  u_int bad[SST_RDO_COU][SST_FIBER_COU] ;
110 
111  memset(bad,0,sizeof(bad)) ;
112 
113  for(int r=0;r<SST_RDO_COU;r++) {
114  if(rb_mask & (1<<r)) ;
115  else continue ;
116 
117  daq_sst_ped_t *ped = ped_store + r ;
118 
119  for(int fib=0;fib<SST_FIBER_COU;fib++) {
120  for(int hy=0;hy<SST_HYBRID_COU;hy++) {
121  for(int strip=0;strip<SST_STRIP_COU;strip++) {
122 
123 
124  if(ped->cou[fib][hy][strip] == 0) { // never seen in the data!
125  ped->ped[fib][hy][strip] = 0 ;
126  ped->rms[fib][hy][strip] = -1.0 ;
127  }
128  else {
129  double pp, rr ;
130 
131 
132  pp = (double) ped->ped[fib][hy][strip] / (double) ped->cou[fib][hy][strip] ;
133 
134  rr = (double) ped->rms[fib][hy][strip] / (double) ped->cou[fib][hy][strip] ;
135 
136  // due to roundoff I can have super small negative numbers
137  if(rr < (pp*pp)) rr = 0.0 ;
138  else rr = sqrt(rr - pp*pp) ;
139 
140  ped->ped[fib][hy][strip] = pp ;
141  ped->rms[fib][hy][strip] = rr ;
142 
143 
144 
145 
146  }
147 
148  if(ped->cou[fib][hy][strip] < MIN_EVENTS) {
149  bad[r][fib]++ ;
150  }
151  }
152  }
153  }
154 
155  }
156 
157  valid = 1 ;
158  for(int r=0;r<SST_RDO_COU;r++) {
159  if(rb_mask & (1<<r)) ;
160  else continue ;
161 
162  for(int fib=0;fib<SST_FIBER_COU;fib++) {
163  if(bad[r][fib]) {
164  LOG(WARN,"RDO %d, FIBER %d: has %d strips with insufficent counts!",r+1,fib,bad[r][fib]) ;
165  }
166  if(sst_rdr->fiber_events[r][fib] < MIN_EVENTS) {
167  LOG(ERR,"RDO %d, FIBER %d has only %d events (< %d)",r+1,fib,sst_rdr->fiber_events[r][fib],MIN_EVENTS) ;
168  valid = 0 ;
169  }
170  }
171  }
172 
173  LOG(TERR,"Pedestals calculated.") ;
174 
175  return ;
176 }
177 
178 
179 int sstPed::to_evb(char *buff)
180 {
181  int r,f,h,s ;
182  u_short *dta = (u_short *) buff ;
183 
184 
185  if(!valid) {
186  // log error but continue...
187  LOG(ERR,"ped::to_evb peds are bad: valid %d",valid) ;
188  }
189 
190 
191 
192  int rdo_cou = 0 ;
193  for(r=0;r<SST_RDO_COU;r++) {
194  if(rb_mask & (1<<r)) ;
195  else continue ;
196 
197  rdo_cou++ ;
198  }
199 
200 
201  LOG(TERR,"Preparing pedestals for later EVB: mask 0x%X, rdos %d",rb_mask,rdo_cou) ;
202 
203  *dta++ = 0xBEEF ; // signature
204  *dta++ = 0x0001 ; // version
205  *dta++ = rdo_cou ;
206  *dta++ = SST_FIBER_COU ;
207  *dta++ = SST_HYBRID_COU ;
208  *dta++ = SST_STRIP_COU ;
209 
210 
211  for(r=0;r<SST_RDO_COU;r++) {
212  if(rb_mask & (1<<r)) ;
213  else continue ;
214 
215  daq_sst_ped_t *ped = ped_store + r ;
216 
217  *dta++ = r+1 ; // RDO, from 1
218 
219 
220  for(f=0;f<SST_FIBER_COU;f++) {
221  for(h=0;h<SST_HYBRID_COU;h++) {
222 
223  for(s=0;s<SST_STRIP_COU;s++) {
224 
225  short pp ;
226 
227  pp = (short)(ped->ped[f][h][s] + 0.5) ;
228  *dta++ = pp;
229 
230  pp = (u_short)(ped->rms[f][h][s]*16.0 + 0.5) ;
231  *dta++ = pp ;
232  }
233  }
234  }
235  }
236 
237  while(((char *)dta-buff)%4) dta++ ;
238 
239  LOG(TERR,"Pedestals prepared for later EVB, %d bytes",(char *)dta-buff) ;
240 
241  return ((char *)dta-buff) ;
242 }
243 
244 
245 int sstPed::to_cache(char *fname, u_int run)
246 {
247  FILE *f ;
248  char f_fname[128] ;
249  int ret ;
250 
251  if(!valid) {
252  LOG(CAUTION,"Pedestals are not valid -- not caching!") ;
253  }
254 
255  time_t tim = time(0) ;
256 
257  for(int r=0;r<SST_RDO_COU;r++) {
258  if(rb_mask & (1<<r)) ;
259  else continue ;
260 
261 
262  if(fname) {
263  sprintf(f_fname,"%s/sst_pedestals_s%d_r%d.txt",fname,sector,r+1) ;
264  }
265  else {
266  sprintf(f_fname,"/RTScache/sst_pedestals_s%d_r%d.txt",sector,r+1) ;
267  }
268 
269 
270  f = fopen("/tmp/ssd_peds","w") ;
271  if(f==0) {
272  LOG(U_TONKO,"ped::to_cache can't open output file \"%s\" [%s]","/tmp/ssd_peds",strerror(errno)) ;
273  continue ;
274  }
275 
276 
277 
278  fprintf(f,"# Run %08u\n",run) ;
279  fprintf(f,"# Date %s",ctime(&tim)) ;
280  fprintf(f,"# Sector %d\n",sector) ;
281  fprintf(f,"# RDO %d\n",r+1) ;
282 
283  fprintf(f,"\n") ;
284 
285  daq_sst_ped_t *peds = ped_store + r ;
286 
287  for(int fib=0;fib<SST_FIBER_COU;fib++) {
288  for(int hy=0;hy<SST_HYBRID_COU;hy++) {
289  for(int strip=0;strip<SST_STRIP_COU;strip++) {
290 
291  fprintf(f,"%d %2d %3d %.1f %.1f\n",fib,hy,strip,
292  peds->ped[fib][hy][strip],
293  peds->rms[fib][hy][strip]) ;
294  }
295  }
296  }
297 
298  fclose(f) ;
299 
300 
301  char sys[256] ;
302 
303  if(valid) {
304  sprintf(sys,"/bin/cp /tmp/ssd_peds %s",f_fname) ;
305  ret = system(sys) ;
306  if(ret==0) {
307  LOG(TERR,"Pedestals written to cache \"%s\" [ret %d]",f_fname,ret) ;
308  }
309  else {
310  LOG(U_TONKO,"Pedestals not written to cache \"%s\" [ret %d]",f_fname,ret) ;
311  }
312  }
313 
314 
315 
316  sprintf(sys,"/bin/cp /tmp/ssd_peds /net/ssd-upgrade/data/PEDESTALS/sst_pedestals_%08u_s%d_r%d.txt >/dev/null 2>/dev/null",run,sector,r+1) ;
317  ret = system(sys) ;
318  if(ret==0) {
319  LOG(TERR,"Executed [%s], ret %d",sys,ret) ;
320  }
321  else {
322  LOG(U_TONKO,"Not executed [%s], ret %d",sys,ret) ;
323  }
324  }
325 
326 
327 
328 
329  return 1 ;
330 }
331