#include "goodness.h" // ********************************************** // quick sort algorithm to sort the hit ttofs // ********************************************** void goodness::qsort(short int *list,short int n) { if (n<2) return; // no need to sort for 1 or zero hits if (n==2) // 2 hits are easy to sort ... { if (ttof[*list]0; nfirst--) list[firstp++]=*(first++); } // ************************************************************* // * bubble sort algorithm for time-of-flight subtracted hit * // * ttofs * // ************************************************************* void goodness::bubble(void) { short int sweep,loop,swap,begin,end; // if sort list is a not initialized, initialize and quick sort it if (sort_list[0]==-1) { for(loop=0; loopt_sweep+1 swap // and remember position of last swap for(sweep=begin,swap=-1; sweepttof[sort_list[sweep+1]]) { swap=sort_list[sweep]; sort_list[sweep]=sort_list[sweep+1]; sort_list[sweep+1]=swap; swap=sweep; } if (swap==-1) return; // if no disorder was found, stop // set end of search interval to the last swap; search from end to // begin; if t_sweep>t_sweep+1 swap and remember position of first swap end=swap; for(sweep=end-1,swap=-1; sweep>=begin; sweep--) if (ttof[sort_list[sweep]]>ttof[sort_list[sweep+1]]) { swap=sort_list[sweep]; sort_list[sweep]=sort_list[sweep+1]; sort_list[sweep+1]=swap; swap=sweep+1; } if (swap==-1) return; // if no disorder was found, stop begin=swap; // set begin of searchinteval to the first swap } } // ************************************************************* // * calculate vertex goodness using a truncated chi^2: * // * 1-0.5(tau/twin)^2 for |tau/twin|gdns) { tav=tau_sum/nwin; // running average is the time of largest goodness if ((deviation=tau_end-1-tav)>0) { // if tav is below minimum time (tau_end-1), use minimum time if ((g=nwin*(1+tav*tav-deviation*deviation)-tau2_sum)>gdns) { // if goodness is largerest so far, set t0 and gdns tau0=tau_end-1; beststart=start; beststop=start+nwin; gdns=g; } } else if ((deviation=tav-tau_begin-1)>0) { // if tav is above maximum time (tau_begin+1), use max. time if ((g=nwin*(1+tav*tav-deviation*deviation)-tau2_sum)>gdns) { // if goodness is largerest so far, set t0 and gdns tau0=tau_begin+1; beststart=start; beststop=start+nwin; gdns=g; } } else { // use tav if ((g=nwin*(1+tav*tav)-tau2_sum)>gdns) { // if goodness is largerest so far, set t0 and gdns tau0=tav; beststart=start; beststop=start+nwin; gdns=g; } } t0=tcent+tnorm*tau0; } if (start+nwin>=nselected()) { // if the the end of the window is the end of // the hit list, subtract the first time; // if the goodness cannot exceed the largest // goodness so far, stop if (--nwin1e-10) //if (gdns>1e-10) { cosc/=sumg; //cosc/=gdns; theta=acos(sumdz/sumg); //theta=acos(sumdz/gdns); } else { cosc=1; if (sumdz>0) theta=0; else theta=3.1415927; } gdn0=gdns/nselected(); sumdx=cosc-cosc0; if (sumdx>0) { if (plusdang==FIT_PARAM_NONE) { gdns=gdn0; if (ngdn==1) set_worst(gdns); else check_worst(gdns); return(gdns); } sumdx=1-plusdang*sumdx*sumdx; } else { if (minusdang==FIT_PARAM_NONE) { gdns=gdn0; if (ngdn==1) set_worst(gdns); else check_worst(gdns); return(gdns); } sumdx=1-minusdang*sumdx*sumdx; } if (sumdx>0) gdns=((1-dirweight)*gdn0+dirweight*sumdx); else gdns=(1-dirweight)*gdn0; if (ngdn==1) set_worst(gdns); else check_worst(gdns); return(gdns); }