10 #define IFEE_NO_LANE_HDRS 1
12 #include "itpc_maps.h"
13 #include "itpc_rowlen.h"
14 #include "itpc_padplane.h"
19 static const u_char tpx_altro_to_j1[2][16] = {
20 {21,22,19,20,17,18,15,16, 8, 7,10, 9,12,11,14,13 },
21 {38,37,36,35,34,33,32,31, 23,24,25,26,27,28,29,30 }
25 static struct itpc_to_ifee_table_t {
28 } itpc_to_ifee_table[72+1][182+1] ;
30 static struct itpc_rowpad_to_id_t {
33 } itpc_rowpad_to_id_s[41][121] ;
36 void itpc_ifee_to_rowpad(
int fee_id,
int ch,
int &row,
int &pad)
42 if((fee_id<1)||(fee_id>55)) return ;
43 if((ch<0)||(ch>63)) return ;
46 if(ch > 31) pin = itpc_sampa_to_pin[1][ch-32] ;
47 else pin = itpc_sampa_to_pin[0][ch] ;
49 row = itpc_padplane[fee_id][pin].row ;
50 pad = itpc_padplane[fee_id][pin].pad ;
57 void itpc_rowpad_to_rdo(
int row,
int pad,
int &rdo)
61 itpc_rowpad_to_ifee(row,pad,fee_id,fee_ch) ;
63 if(fee_id<1 || fee_id>55) {
66 else return ifee_to_irdo_map[fee_id] ;
70 void itpc_rowpad_to_ifee(
int row,
int pad,
int &fee_id,
int &fee_ch)
77 for(
int f=1;f<=55;f++) {
78 for(
int c=0;c<64;c++) {
81 itpc_ifee_to_rowpad(f,c,row,pad) ;
83 itpc_to_ifee_table[row][pad].fee_id = f ;
84 itpc_to_ifee_table[row][pad].fee_ch = c ;
96 fee_id = itpc_to_ifee_table[row][pad].fee_id ;
97 fee_ch = itpc_to_ifee_table[row][pad].fee_ch ;
101 void itpc_sampa_to_rowpad(
int id,
int sampa,
int ch,
int &row,
int &pad)
105 if((
id<1)||(
id>55)) return ;
106 if((ch<0)||(ch>31)) return ;
110 int pin = itpc_sampa_to_pin[sampa][ch] ;
112 row = itpc_padplane[id][pin].row ;
113 pad = itpc_padplane[id][pin].pad ;
118 void itpc_rowpad_to_id(
int row,
int pad,
int &
id,
int &pin)
124 if((row<0)||(row>40)) return ;
125 if((pad<1)||(pad>120)) return ;
129 for(
int id=1;
id<=55;
id++) {
130 for(
int pin=1;pin<=78;pin++) {
136 for(
int s=0;s<2;s++) {
137 for(
int c=0;c<32;c++) {
138 if(pin==itpc_sampa_to_pin[s][c]) {
151 itpc_sampa_to_rowpad(
id,sampa,ch,r,p) ;
153 itpc_rowpad_to_id_s[r][p].id = id ;
154 itpc_rowpad_to_id_s[r][p].pin = pin ;
164 id = itpc_rowpad_to_id_s[row][pad].id ;
165 pin = itpc_rowpad_to_id_s[row][pad].pin ;
170 int itpc_altro_to_ifee(
int altro)
176 if(fee&(1<<0)) ifee |= (1<<0) ;
177 if(fee&(1<<1)) ifee |= (1<<2) ;
178 if(fee&(1<<2)) ifee |= (1<<1) ;
179 if(fee&(1<<3)) ifee |= (1<<3) ;
180 if(fee&(1<<4)) ifee |= (1<<4) ;
182 if(fee&(1<<6)) ifee |= (1<<5) ;
187 void itpc_altro_to_rowpad(
int altro,
int ch,
int odd,
int &row,
int &pad)
192 int ifee = itpc_altro_to_ifee(altro) ;
196 j1 = tpx_altro_to_j1[altro][ch] ;
198 int pin = itpc_adapter_jx_to_pin[odd][j1] ;
201 row = itpc_padplane[ifee][pin].row ;
202 pad = itpc_padplane[ifee][pin].pad ;
207 int itpc_data_c::ped_run = 0 ;
209 void itpc_data_c::data_accum(fee_ch_t *fee_p,
int tb,
int adc)
211 int fee = fee_p->fee ;
215 at[tb_cou].adc = adc ;
218 if(!ped_p || !ped_run) return ;
220 ped_p->g_mean[fee][ch] += adc ;
221 ped_p->g_rms[fee][ch] += adc*adc ;
222 ped_p->g_cou[fee][ch]++ ;
224 ped_p->mean[fee][ch][tb] += adc ;
225 ped_p->rms[fee][ch][tb] += adc*adc ;
226 ped_p->cou[fee][ch][tb]++ ;
230 void itpc_data_c::ped_start()
239 void itpc_data_c::ped_stop()
241 if(ped_p==0) return ;
242 if(!ped_run) return ;
244 for(
int f=0;f<64;f++) {
245 for(
int c=0;c<64;c++) {
247 for(
int t=0;t<512;t++) {
248 int cou = ped_p->cou[f][c][t] ;
250 if(cou==0) continue ;
253 double mean = ped_p->mean[f][c][t] / cou ;
254 double rms = ped_p->rms[f][c][t] / cou ;
256 rms = sqrt(rms-mean*mean) ;
258 ped_p->mean[f][c][t] = mean ;
259 ped_p->rms[f][c][t] = rms ;
261 ped_p->cou[f][c][0] = any_cou ;
263 int cou = ped_p->g_cou[f][c] ;
264 if(cou==0) continue ;
266 double mean = ped_p->g_mean[f][c]/cou ;
267 double rms = ped_p->g_rms[f][c]/cou ;
269 rms = sqrt(rms-mean*mean) ;
274 itpc_ifee_to_rowpad(f,c,row,pad) ;
276 printf(
"%2d %2d %2d %3d %f %f\n",f,c,row,pad,mean,rms) ;
283 time_t now = time(0) ;
284 struct tm *tm = localtime(&now) ;
288 sprintf(fname,
"/RTScache/itpc_pedestals_%d_%d_%d_%d_%d.txt",
296 FILE *of = fopen(fname,
"w") ;
297 LOG(TERR,
"Writing pedestals to %s",fname) ;
300 for(
int f=0;f<64;f++) {
301 for(
int c=0;c<64;c++) {
302 if(ped_p->cou[f][c][0]) ;
305 for(
int t=0;t<512;t++) {
306 fprintf(of,
"%d %d %d %.3f %.3f\n",f,c,t,ped_p->mean[f][c][t],ped_p->rms[f][c][t]) ;
314 int itpc_data_c::start(u_short *d16)
316 fee_ch_t *fee_p = rdo_p->fee_ch[rdo_p->fee_ch_cou] = (fee_ch_t *)malloc(
sizeof(fee_ch_t)) ;
317 rdo_p->fee_ch_cou++ ;
319 fee_p->port = port_id ;
320 fee_p->fee = fee_id ;
324 fee_p->words = words ;
327 u_short *d16_stop = d16 + words ;
329 int tb_stop_last = -1 ;
338 while(d16<d16_stop) {
340 int tb_cou = *d16++ & 0x3FF ;
341 int tb_start = *d16++ & 0x3FF ;
343 int tb_stop = tb_start + tb_cou - 1 ;
348 if(tb_start <= tb_stop_last) {
358 for(
int t=tb_start;t<=tb_stop;t++) {
359 data_accum(fee_p, t, *d16 & 0x3FF) ;
363 tb_stop_last = tb_stop ;
368 LOG(ERR,
"FEE %d:%d -- error 0x%X",fee_p->fee,fee_p->ch,fee_p->err) ;
378 void itpc_data_c::rdo_zap(
void *rdo_p)
381 LOG(ERR,
"rdo_p NULL") ;
385 rdo_t *rdo = (rdo_t *)rdo_p ;
387 for(
int i=0;i<rdo->fee_ch_cou;i++) {
388 if(rdo->fee_ch[i]) free(rdo->fee_ch[i]) ;
395 int itpc_data_c::fee_scan(u_short *d16,
int shorts)
432 memset(hdr_cou,0,
sizeof(hdr_cou)) ;
440 int type = sampa_type ;
445 int lane_expect = 0 ;
451 for(
int i=next_word;i<shorts;i++) {
452 int l = d16[i] >> 13 ;
453 int h = (d16[i] >> 10) & 0x7 ;
454 int d = d16[i] & 0x3FF ;
456 LOG(TERR,
"Lane %d, hdr %d, d 0x%03X - 0x%04X [%d]",l,h,d,d16[i],i) ;
464 if(sampa_type < 0) sampa_type = type ;
465 else if(sampa_type != type) {
467 LOG(ERR,
"Different event type %d, expect %d",type,sampa_type) ;
470 if(type==0) lane_expect = 2 ;
476 LOG(ERR,
"Words are not 0") ;
488 #ifdef IFEE_NO_LANE_HDRS
495 if(
id != sampa_id) fee_err |= 0x4 ;
501 LOG(ERR,
"Wrong channel") ;
505 #ifdef IFEE_NO_LANE_HDRS
508 LOG(ERR,
"SAMPA ch %d, expect %d",ch,sampa_ch) ;
517 if(
id & 1) fee_ch = 32 + ch ;
526 bx = (bx<<10)|(d&0x1FF) ;
528 if(sampa_bx < 0) sampa_bx = bx ;
529 else if(bx != sampa_bx) {
531 LOG(ERR,
"Different BX") ;
536 if(words!=0 || ch!=21) {
538 LOG(ERR,
"Wrong words in Hearbeat") ;
544 LOG(ERR,
"Bad event type %d",type) ;
552 LOG(INFO,
"Starting: FEE %d, SAMPA %d, ch %d, words %d",fee_id,sampa_id,sampa_ch,words) ;
556 if(fee_err) LOG(ERR,
"0x%X",fee_err) ;
558 LOG(INFO,
"Done: FEE %d, SAMPA %d, ch %d, words %d",fee_id,sampa_id,sampa_ch,words) ;
567 LOG(ERR,
"Can't have data 0x%04X!",d16[i]) ;
573 LOG(TERR,
"Lane %d: end of lane data 0x%04X, token %d",l,d16[i],d) ;
580 int ev = (d >> 6)&3 ;
582 LOG(TERR,
"Lane %d: start of lane data 0x%X [0x%04X] - fee %d, ev %d, tkn %d",
592 if(words != 0) fee_err |= 0x4000 ;
594 if(sampa_type == 4) {
598 if(hdr_cou[0] != 64) fee_err |= 0x1000 ;
601 if(hdr_cou[1] != 64) fee_err |= 0x1000 ;
602 if(hdr_cou[2] != 64) fee_err |= 0x1000 ;
603 if(hdr_cou[3] != 64) fee_err |= 0x1000 ;
604 if(hdr_cou[4] != 64) fee_err |= 0x1000 ;
605 if(hdr_cou[5] != 0) fee_err |= 0x2000 ;
607 #ifdef IFEE_NO_LANE_HDRS
609 if(hdr_cou[6] != 8) fee_err |= 0x1000 ;
610 if(hdr_cou[7] != 4) fee_err |= 0x1000 ;
614 else if(sampa_type == 0) {
615 if(hdr_cou[0] != 2) fee_err |= 0x2000 ;
616 if(hdr_cou[1] != 2) fee_err |= 0x2000 ;
617 if(hdr_cou[2] != 2) fee_err |= 0x2000 ;
618 if(hdr_cou[3] != 2) fee_err |= 0x2000 ;
619 if(hdr_cou[4] != 2) fee_err |= 0x2000 ;
620 if(hdr_cou[5] != 0) fee_err |= 0x2000 ;
621 #ifdef IFEE_NO_LANE_HDRS
623 if(hdr_cou[6] != 4) fee_err |= 0x2000 ;
624 if(hdr_cou[7] != 2) fee_err |= 0x2000 ;
629 LOG(ERR,
"Error 0x%08X",fee_err) ;
630 if(fee_err & 0x3000) {
631 for(
int i=0;i<8;i++) {
632 LOG(TERR,
"Hdr %d: %d",i,hdr_cou[i]) ;
648 for(
int id=1;
id<=55;
id++) {
649 for(
int sampa=0;sampa<2;sampa++) {
650 for(
int ch=0;ch<32;ch++) {
654 itpc_sampa_to_rowpad(
id,sampa,ch,row,pad) ;
656 itpc_rowpad_to_id(row,pad,iid,pin) ;
659 printf(
"Id %2d[==%2d], SAMPA %d, Ch %2d == row %2d, pad %3d, pin %2d\n",
id,iid,sampa,ch,row,pad,pin) ;
669 printf(
"ALTRO %d to iFEE %d %d\n",altro,itpc_altro_to_ifee(altro),itpc_altro_to_ifee(altro|1)) ;
671 for(
int i=0;i<16;i++) {
674 itpc_altro_to_rowpad(altro,i,adapter,row,pad) ;
676 printf(
"Altro %d, adapter %d: channel %d: row %d, pad %d\n",altro,adapter,i,row,pad) ;
678 for(
int i=0;i<16;i++) {
681 itpc_altro_to_rowpad(altro|1,i,adapter,row,pad) ;
683 printf(
"Altro %d, adapter %d: channel %d: row %d, pad %d\n",altro|1,adapter,i,row,pad) ;
689 for(
int i=0;i<16;i++) {
692 itpc_altro_to_rowpad(altro,i,adapter,row,pad) ;
694 printf(
"Altro %d, adapter %d: channel %d: row %d, pad %d\n",altro,adapter,i,row,pad) ;
696 for(
int i=0;i<16;i++) {
699 itpc_altro_to_rowpad(altro|1,i,adapter,row,pad) ;
701 printf(
"Altro %d, adapter %d: channel %d: row %d, pad %d\n",altro|1,adapter,i,row,pad) ;