/[PAMELA software]/DarthVader/ToFLevel2/src/tofl2com.for
ViewVC logotype

Diff of /DarthVader/ToFLevel2/src/tofl2com.for

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.4 by mocchiut, Mon Jan 22 10:45:25 2007 UTC revision 1.7 by mocchiut, Mon Mar 3 09:51:03 2008 UTC
# Line 1  Line 1 
1  ******************************************************************************  
2  *  C******************************************************************************
3  *  08-12-06 WM: adc_c-bug :  The raw ADc value was multiplied with cos(theta)  C
4  *  and AFTER that there was an if statement "if tof32(right,i,iadc) < 4095"  C  08-12-06 WM: adc_c-bug :  The raw ADc value was multiplied with cos(theta)
5  *  C  and AFTER that there was an if statement "if tof32(right,i,iadc) < 4095"
6  *  jan-07 GF: ADCflags(4,12) inserted to flag artificial ADC values  C
7  *  jan-07 WM: artificial ADC values created using attenuation calibration  C  jan-07 GF: ADCflags(4,12) inserted to flag artificial ADC values
8  *  jan-07 WM: modified xtofpos flag "101". xtofpos must be inside physical  C  jan-07 WM: artificial ADC values created using attenuation calibration
9  *             dimension of the paddle +/- 10 cm  C  jan-07 WM: modified xtofpos flag "101". xtofpos must be inside physical
10  *  jan-07 WM: if xtofpos=101 then this paddle is not used for beta  C             dimension of the paddle +/- 10 cm
11  *             calculation  C  jan-07 WM: if xtofpos=101 then this paddle is not used for beta
12  *  jan-07 WM: the definition for a "hit" is changed: Now we must have a  C             calculation
13  *             valid TDC signal on both sides  C  jan-07 WM: the definition for a "hit" is changed: Now we must have a
14  *  jan-07 WM: flag for PMTs #10 and #35 added, TDC=819 due to bit-shift  C             valid TDC signal on both sides
15  ******************************************************************************  C  jan-07 WM: flag for PMTs #10 and #35 added, TDC=819 due to bit-shift
16    C  jan-07 WM: bug removed: in some cases tdc_tw was calculated due to a
17    C             leftover "xhelp" value
18    C  apr-07 WM: attenuation fit curve is now a double exponential fit
19    C             conversion from raw ADC to pC using calibration function
20    C             variables xtr_tof and ytr_tof inserted (filled with default)
21    C  jan-08 WM: Major Update: Time Walk correction introduced
22    C             Additionalyl we use the information from the "check_charge"
23    C             function to fill artificial ADC values and make small corrections
24    C             to the k1-parameter (for Z>2)
25    C  feb-08 WM: Calculation of beta(13) changed: First a mean beta is calculated,
26    C             then in a second step we check the residuals of the single
27    C             measurements, reject if > 10 sigma, calculate chi2 and "quality"
28    C             beta is taken as good if chi2<20 and quality>10
29    C******************************************************************************
30    
31        INTEGER FUNCTION TOFL2COM()        INTEGER FUNCTION TOFL2COM()
32  c      c    
# Line 28  C     Line 42  C    
42        LOGICAL check        LOGICAL check
43        REAL secure        REAL secure
44    
45        INTEGER j        INTEGER j,hitvec(6)
       REAL xhelp_a,xhelp_t  
46    
47        REAL dx,dy,dr,ds        REAL dx,dy,dr,ds
48        REAL yhelp,xhelp,xhelp1,xhelp2        REAL yhelp,xhelp,xhelp1,xhelp2
49        REAL c1,c2,sw,sxw,w_i        REAL c1,c2
       INTEGER icount  
50    
51  c      REAL xdummy  C      REAL sw,sxw,w_i
52    C      INTEGER icount
53    C      REAL beta_mean
54    
55        INTEGER tof11_j,tof21_j,tof31_j        INTEGER tof11_j,tof21_j,tof31_j
56        INTEGER tof12_j,tof22_j,tof32_j        INTEGER tof12_j,tof22_j,tof32_j
57    
   
       REAL beta_mean  
   
   
58  c     value for status of each PM-data  c     value for status of each PM-data
59  c     first index  : 1 = left, 2 = right  c     first index  : 1 = left, 2 = right
60  c     second index : 1... number of paddle  c     second index : 1... number of paddle
# Line 52  c     second index : 1... number of padd Line 62  c     second index : 1... number of padd
62        INTEGER tof21_event(2,2),tof22_event(2,2)        INTEGER tof21_event(2,2),tof22_event(2,2)
63        INTEGER tof31_event(2,3),tof32_event(2,3)        INTEGER tof31_event(2,3),tof32_event(2,3)
64    
65    
66          REAL y_coor_lin11c(8,2),x_coor_lin12c(6,2)
67          REAL x_coor_lin21c(2,2),y_coor_lin22c(2,2)
68          REAL y_coor_lin31c(3,2),x_coor_lin32c(3,2)
69    
70          DATA y_coor_lin11c(1,1),y_coor_lin11c(1,2) /-20.66,-2.497/
71          DATA y_coor_lin11c(2,1),y_coor_lin11c(2,2) /-9.10, -2.52/
72          DATA y_coor_lin11c(3,1),y_coor_lin11c(3,2) /-24.07,-2.12/
73          DATA y_coor_lin11c(4,1),y_coor_lin11c(4,2) /-13.40,-2.47/
74          DATA y_coor_lin11c(5,1),y_coor_lin11c(5,2) /-31.07,-2.32/
75          DATA y_coor_lin11c(6,1),y_coor_lin11c(6,2) /-21.69,-2.63/
76          DATA y_coor_lin11c(7,1),y_coor_lin11c(7,2) /-12.37,-2.65/
77          DATA y_coor_lin11c(8,1),y_coor_lin11c(8,2) /-10.81,-3.15/
78    
79          DATA x_coor_lin12c(1,1),x_coor_lin12c(1,2) /12.96, -2.65/
80          DATA x_coor_lin12c(2,1),x_coor_lin12c(2,2) /17.12,-2.44/
81          DATA x_coor_lin12c(3,1),x_coor_lin12c(3,2) /7.26, -1.98/
82          DATA x_coor_lin12c(4,1),x_coor_lin12c(4,2) /-22.52,-2.27/
83          DATA x_coor_lin12c(5,1),x_coor_lin12c(5,2) /-18.54,-2.28/
84          DATA x_coor_lin12c(6,1),x_coor_lin12c(6,2) /-7.67,-2.15/
85    
86          DATA x_coor_lin21c(1,1),x_coor_lin21c(1,2) /22.56,-1.56/
87          DATA x_coor_lin21c(2,1),x_coor_lin21c(2,2) /13.94,-1.56/
88    
89          DATA y_coor_lin22c(1,1),y_coor_lin22c(1,2) /-24.24,-2.23/
90          DATA y_coor_lin22c(2,1),y_coor_lin22c(2,2) /-45.99,-1.68/
91    
92          DATA y_coor_lin31c(1,1),y_coor_lin31c(1,2) /-22.99,-3.54/
93          DATA y_coor_lin31c(2,1),y_coor_lin31c(2,2) /-42.28,-4.10/
94          DATA y_coor_lin31c(3,1),y_coor_lin31c(3,2) /-41.29,-3.69/
95    
96          DATA x_coor_lin32c(1,1),x_coor_lin32c(1,2) /0.961, -3.22/
97          DATA x_coor_lin32c(2,1),x_coor_lin32c(2,2) /4.98,-3.48/
98          DATA x_coor_lin32c(3,1),x_coor_lin32c(3,2) /-22.08,-3.37/
99    
100                
101        REAL theta13        REAL theta13
102  C--   DATA ZTOF/53.74,53.04,23.94,23.44,-23.49,-24.34/ !Sergio 9.05.2006  C--   DATA ZTOF/53.74,53.04,23.94,23.44,-23.49,-24.34/ !Sergio 9.05.2006
# Line 65  C--   DATA ZTOF/53.74,53.04,23.94,23.44, Line 110  C--   DATA ZTOF/53.74,53.04,23.94,23.44,
110        REAL hepratio              REAL hepratio      
111    
112        INTEGER ihelp        INTEGER ihelp
113        REAL xkorr        REAL xkorr,btemp(12)
114    
115          REAL atten,pc_adc,check_charge,newbeta
116    
117          INTEGER IZ
118          REAL k1corrA1,k1corrB1,k1corrC1
119    
120    
121          INTEGER ifst
122          DATA  ifst /0/
123    
124  C---------------------------------------  C---------------------------------------
125  C      C    
# Line 75  C     Line 129  C    
129  C      C    
130  C     CALCULATE COMMON VARIABLES  C     CALCULATE COMMON VARIABLES
131  C      C    
132    C-------------------------------------------------------------------
133    
134  *******************************************************************          if (ifst.eq.0) then
       icounter = icounter + 1  
135    
136  *     amplitude has to be 'secure' higher than pedestal for an adc event          ifst=1
137        secure = 2.  
138    C     amplitude has to be 'secure' higher than pedestal for an adc event
139           secure = 2.
140    
141  C     ratio between helium and proton ca. 4  C     ratio between helium and proton ca. 4
142        hepratio = 4.5  !         hepratio = 4.  !
143        offset = 1         offset = 1
144        slope = 2         slope = 2
145        left = 1         left = 1
146        right = 2         right = 2
147        none_ev = 0         none_ev = 0
148        none_find = 0         none_find = 0
149        tdc_ev = 1         tdc_ev = 1
150        adc_ev = 1         adc_ev = 1
151        itdc = 1         itdc = 1
152        iadc = 2         iadc = 2
153    
154    C--- These are the corrections to the k1-value for Z>2 particles
155           k1corrA1 = 0.
156           k1corrB1 = -5.0
157           k1corrC1=  8.0
158    
159    
160            ENDIF
161    C---------------------------------------------------------------------
162    
163          icounter = icounter + 1
164    
165    
166        do i=1,13        do i=1,13
167           betatof_a(i) = 100.          ! As in "troftrk.for"           betatof_a(i) = 100.          ! As in "troftrk.for"
168        enddo        enddo
169    
170          do i=1,6
171             hitvec(i) = -1
172          enddo
173    
174        do i=1,4        do i=1,4
175           do j=1,12           do j=1,12
176              adctof_c(i,j) = 1000.              adctof_c(i,j) = 1000.
# Line 134  c gf tdc falg: Line 206  c gf tdc falg:
206           enddo           enddo
207        enddo        enddo
208    
 c the calibration files are read in the main program from xxx_tofcalib.rz  
209    
210    C---  Fill xtr_tof and ytr_tof: positions from tracker at ToF layers
211    C---  since this is standalone ToF fill with default values
212          do j=1,6
213          xtr_tof(j) = 101.
214          ytr_tof(j) = 101.
215          enddo
216    
217    c the calibration files are read in the main program from xxx_tofcalib.rz
218    
219  c-------------------------get ToF data --------------------------------  c-------------------------get ToF data --------------------------------
220    
221  c     put the adc and tdc values from ntuple into tofxx(i,j,k) variables  c     put the adc and tdc values from ntuple into tofxx(i,j,k) variables
222    c     adc valueas are then pC
223    
224        do j=1,8        do j=1,8
225           tof11(1,j,2) = adc(ch11a(j),hb11a(j))           tof11(1,j,2) = pc_adc(adc(ch11a(j),hb11a(j)))
226           tof11(2,j,2) = adc(ch11b(j),hb11b(j))           tof11(2,j,2) = pc_adc(adc(ch11b(j),hb11b(j)))
227           tof11(1,j,1) = tdc(ch11a(j),hb11a(j))           tof11(1,j,1) = (tdc(ch11a(j),hb11a(j)))
228           tof11(2,j,1) = tdc(ch11b(j),hb11b(j))           tof11(2,j,1) = (tdc(ch11b(j),hb11b(j)))
229        enddo        enddo
230    
231    
232        do j=1,6        do j=1,6
233           tof12(1,j,2) = adc(ch12a(j),hb12a(j))           tof12(1,j,2) = pc_adc(adc(ch12a(j),hb12a(j)))
234           tof12(2,j,2) = adc(ch12b(j),hb12b(j))           tof12(2,j,2) = pc_adc(adc(ch12b(j),hb12b(j)))
235           tof12(1,j,1) = tdc(ch12a(j),hb12a(j))           tof12(1,j,1) = (tdc(ch12a(j),hb12a(j)))
236           tof12(2,j,1) = tdc(ch12b(j),hb12b(j))           tof12(2,j,1) = (tdc(ch12b(j),hb12b(j)))
237        enddo        enddo
238    
239        do j=1,2        do j=1,2
240           tof21(1,j,2) = adc(ch21a(j),hb21a(j))           tof21(1,j,2) = pc_adc(adc(ch21a(j),hb21a(j)))
241           tof21(2,j,2) = adc(ch21b(j),hb21b(j))           tof21(2,j,2) = pc_adc(adc(ch21b(j),hb21b(j)))
242           tof21(1,j,1) = tdc(ch21a(j),hb21a(j))           tof21(1,j,1) = (tdc(ch21a(j),hb21a(j)))
243           tof21(2,j,1) = tdc(ch21b(j),hb21b(j))           tof21(2,j,1) = (tdc(ch21b(j),hb21b(j)))
244        enddo        enddo
245    
246        do j=1,2        do j=1,2
247           tof22(1,j,2) = adc(ch22a(j),hb22a(j))           tof22(1,j,2) = pc_adc(adc(ch22a(j),hb22a(j)))
248           tof22(2,j,2) = adc(ch22b(j),hb22b(j))           tof22(2,j,2) = pc_adc(adc(ch22b(j),hb22b(j)))
249           tof22(1,j,1) = tdc(ch22a(j),hb22a(j))           tof22(1,j,1) = (tdc(ch22a(j),hb22a(j)))
250           tof22(2,j,1) = tdc(ch22b(j),hb22b(j))           tof22(2,j,1) = (tdc(ch22b(j),hb22b(j)))
251        enddo        enddo
252    
253        do j=1,3        do j=1,3
254           tof31(1,j,2) = adc(ch31a(j),hb31a(j))           tof31(1,j,2) = pc_adc(adc(ch31a(j),hb31a(j)))
255           tof31(2,j,2) = adc(ch31b(j),hb31b(j))           tof31(2,j,2) = pc_adc(adc(ch31b(j),hb31b(j)))
256           tof31(1,j,1) = tdc(ch31a(j),hb31a(j))           tof31(1,j,1) = (tdc(ch31a(j),hb31a(j)))
257           tof31(2,j,1) = tdc(ch31b(j),hb31b(j))           tof31(2,j,1) = (tdc(ch31b(j),hb31b(j)))
258        enddo        enddo
259    
260        do j=1,3        do j=1,3
261           tof32(1,j,2) = adc(ch32a(j),hb32a(j))           tof32(1,j,2) = pc_adc(adc(ch32a(j),hb32a(j)))
262           tof32(2,j,2) = adc(ch32b(j),hb32b(j))           tof32(2,j,2) = pc_adc(adc(ch32b(j),hb32b(j)))
263           tof32(1,j,1) = tdc(ch32a(j),hb32a(j))           tof32(1,j,1) = (tdc(ch32a(j),hb32a(j)))
264           tof32(2,j,1) = tdc(ch32b(j),hb32b(j))           tof32(2,j,1) = (tdc(ch32b(j),hb32b(j)))
265        enddo        enddo
266    
267  C----------------------------------------------------------------------  C----------------------------------------------------------------------
# Line 287  C---- S222B TDC=819 Line 366  C---- S222B TDC=819
366               tdcflagtof(ch22b(2),hb22b(2))=2               tdcflagtof(ch22b(2),hb22b(2))=2
367         endif         endif
368    
   
369  C----------------------------------------------------------------  C----------------------------------------------------------------
370  C------------   Check Paddles for hits    -----------------------  C------------   Check Paddles for hits    -----------------------
371  C------  a "hit" means TDC values<4095 on both sides ------------  C------  a "hit" means TDC values<4095 on both sides ------------
# Line 469  c     check if an other paddle has also Line 547  c     check if an other paddle has also
547                 ENDIF                 ENDIF
548              ENDIF              ENDIF
549           ENDIF           ENDIF
550        ENDDO         ENDDO
551    
552        do i=1,6         do i=1,6
553           tof_i_flag(i)=0           tof_i_flag(i)=0
554           tof_j_flag(i)=0           tof_j_flag(i)=0
555        enddo         enddo
556    
557           tof_i_flag(1)=tof11_i
558           tof_i_flag(2)=tof12_i
559           tof_i_flag(3)=tof21_i
560           tof_i_flag(4)=tof22_i
561           tof_i_flag(5)=tof31_i
562           tof_i_flag(6)=tof32_i
563    
564           tof_j_flag(1)=tof11_j
565           tof_j_flag(2)=tof12_j
566           tof_j_flag(3)=tof21_j
567           tof_j_flag(4)=tof22_j
568           tof_j_flag(5)=tof31_j
569           tof_j_flag(6)=tof32_j
570    
571           hitvec(1)=tof11_i
572           hitvec(2)=tof12_i
573           hitvec(3)=tof21_i
574           hitvec(4)=tof22_i
575           hitvec(5)=tof31_i
576           hitvec(6)=tof32_i
577    
578        tof_i_flag(1)=tof11_i  c       write(*,*) 'tofl2com',
579        tof_i_flag(2)=tof12_i  c     &   tof11_i,tof12_i,tof21_i,tof22_i,tof31_i,tof32_i
       tof_i_flag(3)=tof21_i  
       tof_i_flag(4)=tof22_i  
       tof_i_flag(5)=tof31_i  
       tof_i_flag(6)=tof32_i  
   
       tof_j_flag(1)=tof11_j  
       tof_j_flag(2)=tof12_j  
       tof_j_flag(3)=tof21_j  
       tof_j_flag(4)=tof22_j  
       tof_j_flag(5)=tof31_j  
       tof_j_flag(6)=tof32_j  
580    
           
581  C------------------------------------------------------------------  C------------------------------------------------------------------
582  C---  calculate track position in paddle using timing difference  C--  calculate track position in paddle using timing difference
583    C--  this calculation is preliminary and uses some standard
584    C--  calibration values, but we need to find a rough position to
585    C--  be able to calculate artificial ADC values (needed for the
586    C--  timewalk...
587  C------------------------------------------------------------------  C------------------------------------------------------------------
588    
589        do i=1,3         do i=1,3
590           xtofpos(i)=100.           xtofpos(i)=100.
591           ytofpos(i)=100.           ytofpos(i)=100.
592        enddo         enddo
593    
594  C-----------------------------S1 --------------------------------  C-----------------------------S1 --------------------------------
595    
596        IF (tof11_i.GT.none_find) THEN        IF (tof11_i.GT.none_find) THEN
597           ytofpos(1)  = ((tof11(1,tof11_i,itdc)-tof11(2,tof11_i,itdc))/2.           ytofpos(1)  = ((tof11(1,tof11_i,itdc)-tof11(2,tof11_i,itdc))/2.
598       +        -y_coor_lin11(tof11_i,offset))/y_coor_lin11(tof11_i,slope)       +   -y_coor_lin11c(tof11_i,offset))/y_coor_lin11c(tof11_i,slope)
599        endif        endif
600    
601        IF (tof12_i.GT.none_find) THEN        IF (tof12_i.GT.none_find) THEN
602           xtofpos(1)  = ((tof12(1,tof12_i,itdc)-tof12(2,tof12_i,itdc))/2.           xtofpos(1)  = ((tof12(1,tof12_i,itdc)-tof12(2,tof12_i,itdc))/2.
603       +        -x_coor_lin12(tof12_i,offset))/x_coor_lin12(tof12_i,slope)       +   -x_coor_lin12c(tof12_i,offset))/x_coor_lin12c(tof12_i,slope)
604        endif        endif
605    
606    
# Line 516  C-----------------------------S2 ------- Line 608  C-----------------------------S2 -------
608    
609        IF (tof21_i.GT.none_find) THEN        IF (tof21_i.GT.none_find) THEN
610           xtofpos(2) = ((tof21(1,tof21_i,itdc)-tof21(2,tof21_i,itdc))/2.           xtofpos(2) = ((tof21(1,tof21_i,itdc)-tof21(2,tof21_i,itdc))/2.
611       +        -x_coor_lin21(tof21_i,offset))/x_coor_lin21(tof21_i,slope)       +    -x_coor_lin21c(tof21_i,offset))/x_coor_lin21c(tof21_i,slope)
612        endif        endif
613    
614        IF (tof22_i.GT.none_find) THEN        IF (tof22_i.GT.none_find) THEN
615           ytofpos(2) = ((tof22(1,tof22_i,itdc)-tof22(2,tof22_i,itdc))/2.           ytofpos(2) = ((tof22(1,tof22_i,itdc)-tof22(2,tof22_i,itdc))/2.
616       +        -y_coor_lin22(tof22_i,offset))/y_coor_lin22(tof22_i,slope)       +    -y_coor_lin22c(tof22_i,offset))/y_coor_lin22c(tof22_i,slope)
617        endif        endif
618                
619    
# Line 529  C-----------------------------S3 ------- Line 621  C-----------------------------S3 -------
621    
622        IF (tof31_i.GT.none_find) THEN        IF (tof31_i.GT.none_find) THEN
623           ytofpos(3)  = ((tof31(1,tof31_i,itdc)-tof31(2,tof31_i,itdc))/2.           ytofpos(3)  = ((tof31(1,tof31_i,itdc)-tof31(2,tof31_i,itdc))/2.
624       +        -y_coor_lin31(tof31_i,offset))/y_coor_lin31(tof31_i,slope)       +    -y_coor_lin31c(tof31_i,offset))/y_coor_lin31c(tof31_i,slope)
625        endif        endif
626    
627        IF (tof32_i.GT.none_find) THEN        IF (tof32_i.GT.none_find) THEN
628           xtofpos(3)  = ((tof32(1,tof32_i,itdc)-tof32(2,tof32_i,itdc))/2.           xtofpos(3)  = ((tof32(1,tof32_i,itdc)-tof32(2,tof32_i,itdc))/2.
629       +        -x_coor_lin32(tof32_i,offset))/x_coor_lin32(tof32_i,slope)       +    -x_coor_lin32c(tof32_i,offset))/x_coor_lin32c(tof32_i,slope)
630        endif        endif
631    
632    
 c      do i=1,3  
 c         if (abs(xtofpos(i)).gt.100.) then  
 c            xtofpos(i)=101.  
 c         endif  
 c         if (abs(ytofpos(i)).gt.100.) then  
 c            ytofpos(i)=101.  
 c         endif  
 c      enddo  
   
 C--  restrict TDC measurements to physical paddle dimensions +/- 10 cm  
 C--  this cut is now stronger than in the old versions  
   
         if (abs(xtofpos(1)).gt.31.)  xtofpos(1)=101.  
         if (abs(xtofpos(2)).gt.19.)  xtofpos(2)=101.  
         if (abs(xtofpos(3)).gt.19.)  xtofpos(3)=101.  
   
         if (abs(ytofpos(1)).gt.26.)  ytofpos(1)=101.  
         if (abs(ytofpos(2)).gt.18.)  ytofpos(2)=101.  
         if (abs(ytofpos(3)).gt.18.)  ytofpos(3)=101.  
   
   
633  C----------------------------------------------------------------------  C----------------------------------------------------------------------
634  C---------------------  zenith angle theta  ---------------------------  C---------------------  zenith angle theta  ---------------------------
635  C----------------------------------------------------------------------  C----------------------------------------------------------------------
# Line 575  C--------------------------------------- Line 646  C---------------------------------------
646           dr = sqrt(dx*dx+dy*dy)           dr = sqrt(dx*dx+dy*dy)
647           theta13 = atan(dr/tofarm13)           theta13 = atan(dr/tofarm13)
648    
 C------------------------------------------------------------------  
 c      dx=0.  
 c      dy=0.  
 c      dr=0.  
 c      theta12 = 0.  
 c  
 c         IF ((tof12_i.GT.none_find).AND.(tof21_i.GT.none_find))  
 c     &        dx  = xtofpos(1) - xtofpos(2)  
 c         IF ((tof11_i.GT.none_find).AND.(tof22_i.GT.none_find))  
 c     &        dy  = ytofpos(1) - ytofpos(2)  
 c         dr = sqrt(dx*dx+dy*dy)  
 c         theta12 = atan(dr/tofarm12)  
 c          
 c      dx=0.  
 c      dy=0.  
 c      dr=0.  
 c      theta23 = 0.  
 c  
 c         IF ((tof21_i.GT.none_find).AND.(tof32_i.GT.none_find))  
 c     &        dx  = xtofpos(2) - xtofpos(3)  
 c         IF ((tof22_i.GT.none_find).AND.(tof31_i.GT.none_find))  
 c     &        dy  = ytofpos(2) - ytofpos(3)  
 c         dr = sqrt(dx*dx+dy*dy)  
 c         theta23 = atan(dr/tofarm23)  
 c          
 C---------------------------------------------------------------------  
649    
650    C----------------------------------------------------------------------
651    C--- check charge:
652    C--- if Z=2 we should use the attenuation curve for helium to
653    C--- fill the artificail ADC values and NOT divide by "hepratio"
654    C--- if Z>2 we should do a correction to
655    C--- the k1 constants in the beta calculation
656    C----------------------------------------------------------------------
657    
658             iz = int(check_charge(theta13,hitvec))
659    C         write(*,*) 'in tofl2com',iz
660    
661  C--------------------------------------------------------------------  C--------------------------------------------------------------------
662  C---- if TDCleft.and.TDCright and NO ADC insert artificial ADC  C---- if TDCleft.and.TDCright and NO ADC insert artificial ADC
# Line 624  C----------------------------  S1 ------ Line 679  C----------------------------  S1 ------
679    
680         IF (tof11_i.GT.none_find.AND.abs(yhelp).lt.100) THEN         IF (tof11_i.GT.none_find.AND.abs(yhelp).lt.100) THEN
681           i = tof11_i           i = tof11_i
682           if (tof11(left,i,iadc).eq.4095) then           if (adc(ch11a(i),hb11a(i)).eq.4095) then
683              xkorr=adcx11(left,i,1)*exp(-yhelp/adcx11(left,i,2))              xkorr = atten(left,11,i,yhelp)
684              xkorr=xkorr/hepratio              if (iz.le.1) xkorr=xkorr/hepratio
685              tof11(left,i,iadc)=xkorr/cos(theta13)              tof11(left,i,iadc)=xkorr/cos(theta13)
 c            write(*,*) 'tofl2 left ',i, tof11(left,i,iadc)  
686              adcflagtof(ch11a(i),hb11a(i)) = 1              adcflagtof(ch11a(i),hb11a(i)) = 1
687           endif           endif
688           if (tof11(right,i,iadc).eq.4095) then           if (adc(ch11b(i),hb11b(i)).eq.4095) then
689              xkorr=adcx11(right,i,1)*exp(yhelp/adcx11(right,i,2))              xkorr = atten(right,11,i,yhelp)
690              xkorr=xkorr/hepratio              if (iz.le.1) xkorr=xkorr/hepratio
691              tof11(right,i,iadc)=xkorr/cos(theta13)              tof11(right,i,iadc)=xkorr/cos(theta13)
 c            write(*,*) 'tofl2 right ',i, tof11(right,i,iadc)  
692              adcflagtof(ch11b(i),hb11b(i)) = 1              adcflagtof(ch11b(i),hb11b(i)) = 1
693           endif           endif
694         ENDIF         ENDIF
# Line 646  c            write(*,*) 'tofl2 right ',i Line 699  c            write(*,*) 'tofl2 right ',i
699    
700         IF (tof12_i.GT.none_find.AND.abs(xhelp).lt.100) THEN         IF (tof12_i.GT.none_find.AND.abs(xhelp).lt.100) THEN
701           i = tof12_i           i = tof12_i
702           if (tof12(left,i,iadc).eq.4095) then           if (adc(ch12a(i),hb12a(i)).eq.4095) then
703              xkorr=adcx12(left,i,1)*exp(-xhelp/adcx12(left,i,2))              xkorr = atten(left,12,i,xhelp)
704              xkorr=xkorr/hepratio              if (iz.le.1) xkorr=xkorr/hepratio
705              tof12(left,i,iadc) = xkorr/cos(theta13)              tof12(left,i,iadc) = xkorr/cos(theta13)
706              adcflagtof(ch12a(i),hb12a(i)) = 1              adcflagtof(ch12a(i),hb12a(i)) = 1
707           endif           endif
708           if (tof12(right,i,iadc).eq.4095) then           if (adc(ch12b(i),hb12b(i)).eq.4095) then
709              xkorr=adcx12(right,i,1)*exp(xhelp/adcx12(right,i,2))              xkorr = atten(right,12,i,xhelp)
710              xkorr=xkorr/hepratio              if (iz.le.1) xkorr=xkorr/hepratio
711              tof12(right,i,iadc) = xkorr/cos(theta13)              tof12(right,i,iadc) = xkorr/cos(theta13)
712              adcflagtof(ch12b(i),hb12b(i)) = 1              adcflagtof(ch12b(i),hb12b(i)) = 1
713           endif           endif
# Line 668  C-----------------------------S2 ------- Line 721  C-----------------------------S2 -------
721    
722         IF (tof21_i.GT.none_find.AND.abs(xhelp).lt.100) THEN         IF (tof21_i.GT.none_find.AND.abs(xhelp).lt.100) THEN
723           i = tof21_i           i = tof21_i
724           if (tof21(left,i,iadc).eq.4095) then           if (adc(ch21a(i),hb21a(i)).eq.4095) then
725              xkorr=adcx21(left,i,1)*exp(-xhelp/adcx21(left,i,2))              xkorr = atten(left,21,i,xhelp)
726              xkorr=xkorr/hepratio              if (iz.le.1) xkorr=xkorr/hepratio
727              tof21(left,i,iadc) = xkorr/cos(theta13)              tof21(left,i,iadc) = xkorr/cos(theta13)
728              adcflagtof(ch21a(i),hb21a(i)) = 1              adcflagtof(ch21a(i),hb21a(i)) = 1
729           endif           endif
730           if (tof21(right,i,iadc).eq.4095) then           if (adc(ch21b(i),hb21b(i)).eq.4095) then
731              xkorr=adcx21(right,i,1)*exp(xhelp/adcx21(right,i,2))              xkorr = atten(right,21,i,xhelp)
732              xkorr=xkorr/hepratio              if (iz.le.1) xkorr=xkorr/hepratio
733              tof21(right,i,iadc) = xkorr/cos(theta13)              tof21(right,i,iadc) = xkorr/cos(theta13)
734              adcflagtof(ch21b(i),hb21b(i)) = 1              adcflagtof(ch21b(i),hb21b(i)) = 1
735           endif           endif
# Line 689  C-----------------------------S2 ------- Line 742  C-----------------------------S2 -------
742    
743         IF (tof22_i.GT.none_find.AND.abs(yhelp).lt.100) THEN         IF (tof22_i.GT.none_find.AND.abs(yhelp).lt.100) THEN
744           i = tof22_i           i = tof22_i
745           if (tof22(left,i,iadc).eq.4095) then           if (adc(ch22a(i),hb22a(i)).eq.4095) then
746              xkorr=adcx22(left,i,1)*exp(-yhelp/adcx22(left,i,2))              xkorr = atten(left,22,i,yhelp)
747              xkorr=xkorr/hepratio              if (iz.le.1) xkorr=xkorr/hepratio
748              tof22(left,i,iadc) = xkorr/cos(theta13)              tof22(left,i,iadc) = xkorr/cos(theta13)
749              adcflagtof(ch22a(i),hb22a(i)) = 1              adcflagtof(ch22a(i),hb22a(i)) = 1
750           endif           endif
751           if (tof22(right,i,iadc).eq.4095) then           if (adc(ch22b(i),hb22b(i)).eq.4095) then
752              xkorr=adcx22(right,i,1)*exp(yhelp/adcx22(right,i,2))              xkorr = atten(right,22,i,yhelp)
753              xkorr=xkorr/hepratio              if (iz.le.1) xkorr=xkorr/hepratio
754              tof22(right,i,iadc) = xkorr/cos(theta13)              tof22(right,i,iadc) = xkorr/cos(theta13)
755              adcflagtof(ch22b(i),hb22b(i)) = 1              adcflagtof(ch22b(i),hb22b(i)) = 1
756           endif           endif
# Line 711  C-----------------------------S3 ------- Line 764  C-----------------------------S3 -------
764    
765         IF (tof31_i.GT.none_find.AND.abs(yhelp).lt.100) THEN         IF (tof31_i.GT.none_find.AND.abs(yhelp).lt.100) THEN
766           i = tof31_i           i = tof31_i
767           if (tof31(left,i,iadc).eq.4095) then           if (adc(ch31a(i),hb31a(i)).eq.4095) then
768              xkorr=adcx31(left,i,1)*exp(-yhelp/adcx31(left,i,2))              xkorr = atten(left,31,i,yhelp)
769              xkorr=xkorr/hepratio              if (iz.le.1) xkorr=xkorr/hepratio
770              tof31(left,i,iadc) = xkorr/cos(theta13)              tof31(left,i,iadc) = xkorr/cos(theta13)
771              adcflagtof(ch31a(i),hb31a(i)) = 1              adcflagtof(ch31a(i),hb31a(i)) = 1
772           endif           endif
773           if (tof31(right,i,iadc).eq.4095) then           if (adc(ch31b(i),hb31b(i)).eq.4095) then
774              xkorr=adcx31(right,i,1)*exp(yhelp/adcx31(right,i,2))              xkorr = atten(right,31,i,yhelp)
775              xkorr=xkorr/hepratio              if (iz.le.1) xkorr=xkorr/hepratio
776              tof31(right,i,iadc) = xkorr/cos(theta13)              tof31(right,i,iadc) = xkorr/cos(theta13)
777              adcflagtof(ch31b(i),hb31b(i)) = 1              adcflagtof(ch31b(i),hb31b(i)) = 1
778           endif           endif
# Line 731  C-----------------------------S3 ------- Line 784  C-----------------------------S3 -------
784    
785         IF (tof32_i.GT.none_find.AND.abs(xhelp).lt.100) THEN         IF (tof32_i.GT.none_find.AND.abs(xhelp).lt.100) THEN
786           i = tof32_i           i = tof32_i
787           if (tof32(left,i,iadc).eq.4095) then           if (adc(ch32a(i),hb32a(i)).eq.4095) then
788              xkorr=adcx32(left,i,1)*exp(-xhelp/adcx32(left,i,2))              xkorr = atten(left,32,i,xhelp)
789              xkorr=xkorr/hepratio              if (iz.le.1) xkorr=xkorr/hepratio
790              tof32(left,i,iadc) = xkorr/cos(theta13)              tof32(left,i,iadc) = xkorr/cos(theta13)
791              adcflagtof(ch32a(i),hb32a(i)) = 1              adcflagtof(ch32a(i),hb32a(i)) = 1
792           endif           endif
793           if (tof32(right,i,iadc).eq.4095) then           if (adc(ch32b(i),hb32b(i)).eq.4095) then
794              xkorr=adcx32(right,i,1)*exp(xhelp/adcx32(right,i,2))              xkorr = atten(right,32,i,xhelp)
795              xkorr=xkorr/hepratio              if (iz.le.1) xkorr=xkorr/hepratio
796              tof32(right,i,iadc) = xkorr/cos(theta13)              tof32(right,i,iadc) = xkorr/cos(theta13)
797              adcflagtof(ch32b(i),hb32b(i)) = 1              adcflagtof(ch32b(i),hb32b(i)) = 1
798           endif           endif
799         ENDIF         ENDIF
800    
801    
802  C--------------------------------------------------------------------  C-------------------------------------------------------------------
803  C--------------------Time walk correction  -------------------------  C--------------------Time walk correction  -------------------------
804  C--------------------------------------------------------------------  C-------------------------------------------------------------------
805    C-------------------------------------------------------------------
806    C Now there is for each hitted paddle a TDC and ADC value, if the
807    C TDC was < 4095.
808    C There might be also TDC-ADC pairs in paddles not hitted
809    
810    C-------------------------------------------------------------------
811    C If we have multiple paddles hit, so that no artificial ADC value
812    C is created, we set the raw TDC value as "tdc_c"
813    C-------------------------------------------------------------------
814    c
815    c       do i=1,4
816    c         do j=1,12
817    c            tdc_c(i,j) = tdc(i,j)
818    c         enddo
819    c       enddo
820    c
821    C----  Let's correct the raw TDC value with the time walk  ---------
822    
823        DO i=1,8        DO i=1,8
824         xhelp_a = tof11(left,i,iadc)           if ((tdc(ch11a(i),hb11a(i)).lt.4095).and.
825         xhelp_t = tof11(left,i,itdc)       &             (tof11(left,i,iadc).lt.3786)) THEN
826         if(xhelp_a<4095) xhelp = tw11(left,i)/sqrt(xhelp_a)           xhelp = tw11(left,i)/(tof11(left,i,iadc)**0.5)
827         tof11(left,i,itdc) = xhelp_t  + xhelp           tof11(left,i,itdc) = tof11(left,i,itdc) + xhelp
828         tdc_c(ch11a(i),hb11a(i))=tof11(left,i,itdc)           tdc_c(ch11a(i),hb11a(i))=tof11(left,i,itdc)
829         xhelp_a = tof11(right,i,iadc)                                                ENDIF
830         xhelp_t = tof11(right,i,itdc)  
831         if(xhelp_a<4095) xhelp = tw11(right,i)/sqrt(xhelp_a)           if ((tdc(ch11b(i),hb11b(i)).lt.4095).and.
832         tof11(right,i,itdc) = xhelp_t  + xhelp       &             (tof11(right,i,iadc).lt.3786)) THEN
833         tdc_c(ch11b(i),hb11b(i))=tof11(right,i,itdc)           xhelp = tw11(right,i)/(tof11(right,i,iadc)**0.5)
834             tof11(right,i,itdc) = tof11(right,i,itdc) + xhelp
835             tdc_c(ch11b(i),hb11b(i))=tof11(right,i,itdc)
836                                                 ENDIF
837        ENDDO        ENDDO
838    
839    
840        DO i=1,6        DO i=1,6
841         xhelp_a = tof12(left,i,iadc)           if ((tdc(ch12a(i),hb12a(i)).lt.4095).and.
842         xhelp_t = tof12(left,i,itdc)       &             (tof12(left,i,iadc).lt.3786)) THEN
843         if(xhelp_a<4095) xhelp = tw12(left,i)/sqrt(xhelp_a)           xhelp = tw12(left,i)/(tof12(left,i,iadc)**0.5)
844         tof12(left,i,itdc) = xhelp_t  + xhelp           tof12(left,i,itdc) = tof12(left,i,itdc) + xhelp
845         tdc_c(ch12a(i),hb12a(i))=tof12(left,i,itdc)           tdc_c(ch12a(i),hb12a(i))=tof12(left,i,itdc)
846         xhelp_a = tof12(right,i,iadc)                                                ENDIF
847         xhelp_t = tof12(right,i,itdc)  
848         if(xhelp_a<4095) xhelp = tw12(right,i)/sqrt(xhelp_a)           if ((tdc(ch12b(i),hb12b(i)).lt.4095).and.
849         tof12(right,i,itdc) = xhelp_t  + xhelp       &             (tof12(right,i,iadc).lt.3786)) THEN
850         tdc_c(ch12b(i),hb12b(i))=tof12(right,i,itdc)           xhelp = tw12(right,i)/(tof12(right,i,iadc)**0.5)
851             tof12(right,i,itdc) = tof12(right,i,itdc) + xhelp
852             tdc_c(ch12b(i),hb12b(i))=tof12(right,i,itdc)
853                                                 ENDIF
854        ENDDO        ENDDO
855    
856  C----  C----
857        DO i=1,2        DO I=1,2
858         xhelp_a = tof21(left,i,iadc)           if ((tdc(ch21a(i),hb21a(i)).lt.4095).and.
859         xhelp_t = tof21(left,i,itdc)       &             (tof21(left,i,iadc).lt.3786)) THEN
860         if(xhelp_a<4095) xhelp = tw21(left,i)/sqrt(xhelp_a)           xhelp = tw21(left,i)/(tof21(left,i,iadc)**0.5)
861         tof21(left,i,itdc) = xhelp_t  + xhelp           tof21(left,i,itdc) = tof21(left,i,itdc) + xhelp
862         tdc_c(ch21a(i),hb21a(i))=tof21(left,i,itdc)           tdc_c(ch21a(i),hb21a(i))=tof21(left,i,itdc)
863         xhelp_a = tof21(right,i,iadc)                                                ENDIF
864         xhelp_t = tof21(right,i,itdc)  
865         if(xhelp_a<4095) xhelp = tw21(right,i)/sqrt(xhelp_a)           if ((tdc(ch21b(i),hb21b(i)).lt.4095).and.
866         tof21(right,i,itdc) = xhelp_t  + xhelp       &             (tof21(right,i,iadc).lt.3786)) THEN
867         tdc_c(ch21b(i),hb21b(i))=tof21(right,i,itdc)           xhelp = tw21(right,i)/(tof21(right,i,iadc)**0.5)
868        ENDDO           tof21(right,i,itdc) = tof21(right,i,itdc) + xhelp
869             tdc_c(ch21b(i),hb21b(i))=tof21(right,i,itdc)
870        DO i=1,2                                               ENDIF
871         xhelp_a = tof22(left,i,iadc)        ENDDO
872         xhelp_t = tof22(left,i,itdc)  
873         if(xhelp_a<4095) xhelp = tw22(left,i)/sqrt(xhelp_a)        DO I=1,2
874         tof22(left,i,itdc) = xhelp_t  + xhelp           if ((tdc(ch22a(i),hb22a(i)).lt.4095).and.
875         tdc_c(ch22a(i),hb22a(i))=tof22(left,i,itdc)       &             (tof22(left,i,iadc).lt.3786)) THEN
876         xhelp_a = tof22(right,i,iadc)           xhelp = tw22(left,i)/(tof22(left,i,iadc)**0.5)
877         xhelp_t = tof22(right,i,itdc)           tof22(left,i,itdc) = tof22(left,i,itdc) + xhelp
878         if(xhelp_a<4095) xhelp = tw22(right,i)/sqrt(xhelp_a)           tdc_c(ch22a(i),hb22a(i))=tof22(left,i,itdc)
879         tof22(right,i,itdc) = xhelp_t  + xhelp                                                ENDIF
880         tdc_c(ch22b(i),hb22b(i))=tof22(right,i,itdc)  
881             if ((tdc(ch22b(i),hb22b(i)).lt.4095).and.
882         &             (tof22(right,i,iadc).lt.3786)) THEN
883             xhelp = tw22(right,i)/(tof22(right,i,iadc)**0.5)
884             tof22(right,i,itdc) = tof22(right,i,itdc) + xhelp
885             tdc_c(ch22b(i),hb22b(i))=tof22(right,i,itdc)
886                                                 ENDIF
887        ENDDO        ENDDO
 C----  
888    
889        DO i=1,3  C----
890         xhelp_a = tof31(left,i,iadc)        DO I=1,3
891         xhelp_t = tof31(left,i,itdc)           if ((tdc(ch31a(i),hb31a(i)).lt.4095).and.
892         if(xhelp_a<4095) xhelp = tw31(left,i)/sqrt(xhelp_a)       &             (tof31(left,i,iadc).lt.3786)) THEN
893         tof31(left,i,itdc) = xhelp_t  + xhelp           xhelp = tw31(left,i)/(tof31(left,i,iadc)**0.5)
894         tdc_c(ch31a(i),hb31a(i))=tof31(left,i,itdc)           tof31(left,i,itdc) = tof31(left,i,itdc) + xhelp
895         xhelp_a = tof31(right,i,iadc)           tdc_c(ch31a(i),hb31a(i))=tof31(left,i,itdc)
896         xhelp_t = tof31(right,i,itdc)                                                ENDIF
897         if(xhelp_a<4095) xhelp = tw31(right,i)/sqrt(xhelp_a)  
898         tof31(right,i,itdc) = xhelp_t  + xhelp           if ((tdc(ch31b(i),hb31b(i)).lt.4095).and.
899         tdc_c(ch31b(i),hb31b(i))=tof31(right,i,itdc)       &             (tof31(right,i,iadc).lt.3786)) THEN
900        ENDDO           xhelp = tw31(right,i)/(tof31(right,i,iadc)**0.5)
901             tof31(right,i,itdc) = tof31(right,i,itdc) + xhelp
902        DO i=1,3           tdc_c(ch31b(i),hb31b(i))=tof31(right,i,itdc)
903         xhelp_a = tof32(left,i,iadc)                                               ENDIF
904         xhelp_t = tof32(left,i,itdc)        ENDDO
905         if(xhelp_a<4095) xhelp = tw32(left,i)/sqrt(xhelp_a)  
906         tof32(left,i,itdc) = xhelp_t  + xhelp        DO I=1,3
907         tdc_c(ch32a(i),hb32a(i))=tof32(left,i,itdc)           if ((tdc(ch32a(i),hb32a(i)).lt.4095).and.
908         xhelp_a = tof32(right,i,iadc)       &             (tof32(left,i,iadc).lt.3786)) THEN
909         xhelp_t = tof32(right,i,itdc)           xhelp = tw32(left,i)/(tof32(left,i,iadc)**0.5)
910         if(xhelp_a<4095) xhelp = tw32(right,i)/sqrt(xhelp_a)           tof32(left,i,itdc) = tof32(left,i,itdc) + xhelp
911         tof32(right,i,itdc) = xhelp_t  + xhelp           tdc_c(ch32a(i),hb32a(i))=tof32(left,i,itdc)
912         tdc_c(ch32b(i),hb32b(i))=tof32(right,i,itdc)                                                ENDIF
913    
914             if ((tdc(ch32b(i),hb32b(i)).lt.4095).and.
915         &             (tof32(right,i,iadc).lt.3786)) THEN
916             xhelp = tw32(right,i)/(tof32(right,i,iadc)**0.5)
917             tof32(right,i,itdc) = tof32(right,i,itdc) + xhelp
918             tdc_c(ch32b(i),hb32b(i))=tof32(right,i,itdc)
919                                                 ENDIF
920        ENDDO        ENDDO
921            
922    C---------------------------------------------------------------
923    C--- calculate track position in paddle using timing difference
924    C--- now using the time-walk corrected TDC values
925    C---------------------------------------------------------------
926    
927          do i=1,3
928             xtofpos(i)=100.
929             ytofpos(i)=100.
930          enddo
931    
932    C-----------------------------S1 --------------------------------
933    
934          IF (tof11_i.GT.none_find) THEN
935             ytofpos(1)  = ((tof11(1,tof11_i,itdc)-tof11(2,tof11_i,itdc))/2.
936         +        -y_coor_lin11(tof11_i,offset))/y_coor_lin11(tof11_i,slope)
937          endif
938    
939          IF (tof12_i.GT.none_find) THEN
940             xtofpos(1)  = ((tof12(1,tof12_i,itdc)-tof12(2,tof12_i,itdc))/2.
941         +        -x_coor_lin12(tof12_i,offset))/x_coor_lin12(tof12_i,slope)
942          endif
943    
944    
945    C-----------------------------S2 --------------------------------
946    
947          IF (tof21_i.GT.none_find) THEN
948             xtofpos(2) = ((tof21(1,tof21_i,itdc)-tof21(2,tof21_i,itdc))/2.
949         +        -x_coor_lin21(tof21_i,offset))/x_coor_lin21(tof21_i,slope)
950          endif
951    
952          IF (tof22_i.GT.none_find) THEN
953             ytofpos(2) = ((tof22(1,tof22_i,itdc)-tof22(2,tof22_i,itdc))/2.
954         +        -y_coor_lin22(tof22_i,offset))/y_coor_lin22(tof22_i,slope)
955          endif
956          
957    
958    C-----------------------------S3 --------------------------------
959    
960          IF (tof31_i.GT.none_find) THEN
961             ytofpos(3)  = ((tof31(1,tof31_i,itdc)-tof31(2,tof31_i,itdc))/2.
962         +        -y_coor_lin31(tof31_i,offset))/y_coor_lin31(tof31_i,slope)
963          endif
964    
965          IF (tof32_i.GT.none_find) THEN
966             xtofpos(3)  = ((tof32(1,tof32_i,itdc)-tof32(2,tof32_i,itdc))/2.
967         +        -x_coor_lin32(tof32_i,offset))/x_coor_lin32(tof32_i,slope)
968          endif
969    
970    
971    c      do i=1,3
972    c         if (abs(xtofpos(i)).gt.100.) then
973    c            xtofpos(i)=101.
974    c         endif
975    c         if (abs(ytofpos(i)).gt.100.) then
976    c            ytofpos(i)=101.
977    c         endif
978    c      enddo
979    
980    C--  restrict TDC measurements to physical paddle dimensions +/- 10 cm
981    C--  this cut is now stronger than in the old versions
982    
983            if (abs(xtofpos(1)).gt.31.)  xtofpos(1)=101.
984            if (abs(xtofpos(2)).gt.19.)  xtofpos(2)=101.
985            if (abs(xtofpos(3)).gt.19.)  xtofpos(3)=101.
986    
987            if (abs(ytofpos(1)).gt.26.)  ytofpos(1)=101.
988            if (abs(ytofpos(2)).gt.18.)  ytofpos(2)=101.
989            if (abs(ytofpos(3)).gt.18.)  ytofpos(3)=101.
990    
991    
992    C----------------------------------------------------------------------
993    C---------------------  zenith angle theta  ---------------------------
994    C----------------------------------------------------------------------
995    
996          dx=0.
997          dy=0.
998          dr=0.
999          theta13 = 0.
1000    
1001             IF ((tof12_i.GT.none_find).AND.(tof32_i.GT.none_find))
1002         &        dx  = xtofpos(1) - xtofpos(3)
1003             IF ((tof11_i.GT.none_find).AND.(tof31_i.GT.none_find))
1004         &        dy  = ytofpos(1) - ytofpos(3)
1005             dr = sqrt(dx*dx+dy*dy)
1006             theta13 = atan(dr/tofarm13)
1007    
1008    C------------------------------------------------------------------
1009    c      dx=0.
1010    c      dy=0.
1011    c      dr=0.
1012    c      theta12 = 0.
1013    c
1014    c         IF ((tof12_i.GT.none_find).AND.(tof21_i.GT.none_find))
1015    c     &        dx  = xtofpos(1) - xtofpos(2)
1016    c         IF ((tof11_i.GT.none_find).AND.(tof22_i.GT.none_find))
1017    c     &        dy  = ytofpos(1) - ytofpos(2)
1018    c         dr = sqrt(dx*dx+dy*dy)
1019    c         theta12 = atan(dr/tofarm12)
1020    c        
1021    c      dx=0.
1022    c      dy=0.
1023    c      dr=0.
1024    c      theta23 = 0.
1025    c
1026    c         IF ((tof21_i.GT.none_find).AND.(tof32_i.GT.none_find))
1027    c     &        dx  = xtofpos(2) - xtofpos(3)
1028    c         IF ((tof22_i.GT.none_find).AND.(tof31_i.GT.none_find))
1029    c     &        dy  = ytofpos(2) - ytofpos(3)
1030    c         dr = sqrt(dx*dx+dy*dy)
1031    c         theta23 = atan(dr/tofarm23)
1032    c        
1033  C----------------------------------------------------------------------  C----------------------------------------------------------------------
1034  C------------------angle and ADC(x) correction  C------------------angle and ADC(x) correction
1035  C----------------------------------------------------------------------  C----------------------------------------------------------------------
# Line 848  c       DATA tof32_y/ -5.0,0.0,5.0/ Line 1049  c       DATA tof32_y/ -5.0,0.0,5.0/
1049        IF (tof11_i.GT.none_find.AND.abs(yhelp).lt.100) THEN        IF (tof11_i.GT.none_find.AND.abs(yhelp).lt.100) THEN
1050    
1051           i = tof11_i           i = tof11_i
1052           if (tof11(left,i,iadc).lt.4095) then           if (tof11(left,i,iadc).lt.3786) then
1053    c          if (adc(ch11a(i),hb11a(i)).lt.4095) then
1054              tof11(left,i,iadc) = tof11(left,i,iadc)*cos(theta13)              tof11(left,i,iadc) = tof11(left,i,iadc)*cos(theta13)
1055              xkorr=adcx11(left,i,1)*exp(-yhelp/adcx11(left,i,2))              xkorr = atten(left,11,i,yhelp)
1056              xkorr=xkorr/hepratio  c            write(40+i,*) yhelp,xkorr
1057                if (iz.le.1) xkorr=xkorr/hepratio
1058              adctof_c(ch11a(i),hb11a(i))=tof11(left,i,iadc)/xkorr              adctof_c(ch11a(i),hb11a(i))=tof11(left,i,iadc)/xkorr
1059           endif           endif
1060    
1061           if (tof11(right,i,iadc).lt.4095) then           if (tof11(right,i,iadc).lt.3786) then
1062    c          if (adc(ch11b(i),hb11b(i)).lt.4095) then
1063              tof11(right,i,iadc) = tof11(right,i,iadc)*cos(theta13)              tof11(right,i,iadc) = tof11(right,i,iadc)*cos(theta13)
1064              xkorr=adcx11(right,i,1)*exp(yhelp/adcx11(right,i,2))              xkorr = atten(right,11,i,yhelp)
1065              xkorr=xkorr/hepratio  c            write(40+i,*) yhelp,xkorr
1066                if (iz.le.1) xkorr=xkorr/hepratio
1067              adctof_c(ch11b(i),hb11b(i))=tof11(right,i,iadc)/xkorr              adctof_c(ch11b(i),hb11b(i))=tof11(right,i,iadc)/xkorr
1068           endif           endif
1069        ENDIF        ENDIF
# Line 870  c       DATA tof32_y/ -5.0,0.0,5.0/ Line 1075  c       DATA tof32_y/ -5.0,0.0,5.0/
1075        IF (tof12_i.GT.none_find.AND.abs(xhelp).lt.100) THEN        IF (tof12_i.GT.none_find.AND.abs(xhelp).lt.100) THEN
1076    
1077           i = tof12_i           i = tof12_i
1078           if (tof12(left,i,iadc).lt.4095) then           if (tof12(left,i,iadc).lt.3786) then
1079    c          if (adc(ch12a(i),hb12a(i)).lt.4095) then
1080              tof12(left,i,iadc) = tof12(left,i,iadc)*cos(theta13)              tof12(left,i,iadc) = tof12(left,i,iadc)*cos(theta13)
1081              xkorr=adcx12(left,i,1)*exp(-xhelp/adcx12(left,i,2))              xkorr = atten(left,12,i,xhelp)
1082              xkorr=xkorr/hepratio  c            write(50+i,*) xhelp,xkorr
1083                if (iz.le.1) xkorr=xkorr/hepratio
1084              adctof_c(ch12a(i),hb12a(i))=tof12(left,i,iadc)/xkorr              adctof_c(ch12a(i),hb12a(i))=tof12(left,i,iadc)/xkorr
1085           endif           endif
1086    
1087           if (tof12(right,i,iadc).lt.4095) then           if (tof12(right,i,iadc).lt.3786) then
1088    c          if (adc(ch12b(i),hb12b(i)).lt.4095) then
1089              tof12(right,i,iadc) = tof12(right,i,iadc)*cos(theta13)              tof12(right,i,iadc) = tof12(right,i,iadc)*cos(theta13)
1090              xkorr=adcx12(right,i,1)*exp(xhelp/adcx12(right,i,2))              xkorr = atten(right,12,i,xhelp)
1091              xkorr=xkorr/hepratio  c            write(50+i,*) xhelp,xkorr
1092                if (iz.le.1) xkorr=xkorr/hepratio
1093              adctof_c(ch12b(i),hb12b(i))=tof12(right,i,iadc)/xkorr              adctof_c(ch12b(i),hb12b(i))=tof12(right,i,iadc)/xkorr
1094           endif           endif
1095        ENDIF        ENDIF
# Line 894  C-----------------------------S2 ------- Line 1103  C-----------------------------S2 -------
1103        IF (tof21_i.GT.none_find.AND.abs(xhelp).lt.100) THEN        IF (tof21_i.GT.none_find.AND.abs(xhelp).lt.100) THEN
1104    
1105           i = tof21_i           i = tof21_i
1106           if (tof21(left,i,iadc).lt.4095) then           if (tof21(left,i,iadc).lt.3786) then
1107    c          if (adc(ch21a(i),hb21a(i)).lt.4095) then
1108              tof21(left,i,iadc) = tof21(left,i,iadc)*cos(theta13)              tof21(left,i,iadc) = tof21(left,i,iadc)*cos(theta13)
1109              xkorr=adcx21(left,i,1)*exp(-xhelp/adcx21(left,i,2))              xkorr = atten(left,21,i,xhelp)
1110              xkorr=xkorr/hepratio  c            write(60+i,*) xhelp,xkorr
1111                if (iz.le.1) xkorr=xkorr/hepratio
1112              adctof_c(ch21a(i),hb21a(i))=tof21(left,i,iadc)/xkorr              adctof_c(ch21a(i),hb21a(i))=tof21(left,i,iadc)/xkorr
1113           endif           endif
1114    
1115           if (tof21(right,i,iadc).lt.4095) then           if (tof21(right,i,iadc).lt.3786) then
1116    c          if (adc(ch21b(i),hb21b(i)).lt.4095) then
1117              tof21(right,i,iadc) = tof21(right,i,iadc)*cos(theta13)              tof21(right,i,iadc) = tof21(right,i,iadc)*cos(theta13)
1118              xkorr=adcx21(right,i,1)*exp(xhelp/adcx21(right,i,2))              xkorr=adcx21(right,i,1)*exp(xhelp/adcx21(right,i,2))
1119              xkorr=xkorr/hepratio              xkorr = atten(right,21,i,xhelp)
1120    c            write(60+i,*) xhelp,xkorr
1121                if (iz.le.1) xkorr=xkorr/hepratio
1122              adctof_c(ch21b(i),hb21b(i))=tof21(right,i,iadc)/xkorr              adctof_c(ch21b(i),hb21b(i))=tof21(right,i,iadc)/xkorr
1123           endif           endif
1124        ENDIF        ENDIF
# Line 917  C-----------------------------S2 ------- Line 1131  C-----------------------------S2 -------
1131        IF (tof22_i.GT.none_find.AND.abs(yhelp).lt.100) THEN        IF (tof22_i.GT.none_find.AND.abs(yhelp).lt.100) THEN
1132    
1133           i = tof22_i           i = tof22_i
1134           if (tof22(left,i,iadc).lt.4095) then           if (tof22(left,i,iadc).lt.3786) then
1135    c          if (adc(ch22a(i),hb22a(i)).lt.4095) then
1136              tof22(left,i,iadc) = tof22(left,i,iadc)*cos(theta13)              tof22(left,i,iadc) = tof22(left,i,iadc)*cos(theta13)
1137              xkorr=adcx22(left,i,1)*exp(-yhelp/adcx22(left,i,2))              xkorr = atten(left,22,i,yhelp)
1138              xkorr=xkorr/hepratio  c            write(70+i,*) yhelp,xkorr
1139                if (iz.le.1) xkorr=xkorr/hepratio
1140              adctof_c(ch22a(i),hb22a(i))=tof22(left,i,iadc)/xkorr              adctof_c(ch22a(i),hb22a(i))=tof22(left,i,iadc)/xkorr
1141           endif           endif
1142    
1143           if (tof22(right,i,iadc).lt.4095) then           if (tof22(right,i,iadc).lt.3786) then
1144    c          if (adc(ch22b(i),hb22b(i)).lt.4095) then
1145              tof22(right,i,iadc) = tof22(right,i,iadc)*cos(theta13)              tof22(right,i,iadc) = tof22(right,i,iadc)*cos(theta13)
1146              xkorr=adcx22(right,i,1)*exp(yhelp/adcx22(right,i,2))              xkorr = atten(right,22,i,yhelp)
1147              xkorr=xkorr/hepratio  c            write(70+i,*) yhelp,xkorr
1148                if (iz.le.1) xkorr=xkorr/hepratio
1149              adctof_c(ch22b(i),hb22b(i))=tof22(right,i,iadc)/xkorr              adctof_c(ch22b(i),hb22b(i))=tof22(right,i,iadc)/xkorr
1150           endif           endif
1151        ENDIF        ENDIF
# Line 941  C-----------------------------S3 ------- Line 1159  C-----------------------------S3 -------
1159        IF (tof31_i.GT.none_find.AND.abs(yhelp).lt.100) THEN        IF (tof31_i.GT.none_find.AND.abs(yhelp).lt.100) THEN
1160    
1161           i = tof31_i           i = tof31_i
1162           if (tof31(left,i,iadc).lt.4095) then           if (tof31(left,i,iadc).lt.3786) then
1163    c          if (adc(ch31a(i),hb31a(i)).lt.4095) then
1164              tof31(left,i,iadc) = tof31(left,i,iadc)*cos(theta13)              tof31(left,i,iadc) = tof31(left,i,iadc)*cos(theta13)
1165              xkorr=adcx31(left,i,1)*exp(-yhelp/adcx31(left,i,2))              xkorr = atten(left,31,i,yhelp)
1166              xkorr=xkorr/hepratio  c            write(80+i,*) yhelp,xkorr
1167                if (iz.le.1) xkorr=xkorr/hepratio
1168              adctof_c(ch31a(i),hb31a(i))=tof31(left,i,iadc)/xkorr              adctof_c(ch31a(i),hb31a(i))=tof31(left,i,iadc)/xkorr
1169           endif           endif
1170    
1171           if (tof31(right,i,iadc).lt.4095) then           if (tof31(right,i,iadc).lt.3786) then
1172    c          if (adc(ch31b(i),hb31b(i)).lt.4095) then
1173              tof31(right,i,iadc) = tof31(right,i,iadc)*cos(theta13)              tof31(right,i,iadc) = tof31(right,i,iadc)*cos(theta13)
1174              xkorr=adcx31(right,i,1)*exp(yhelp/adcx31(right,i,2))              xkorr = atten(right,31,i,yhelp)
1175              xkorr=xkorr/hepratio  c            write(80+i,*) yhelp,xkorr
1176                if (iz.le.1) xkorr=xkorr/hepratio
1177              adctof_c(ch31b(i),hb31b(i))=tof31(right,i,iadc)/xkorr              adctof_c(ch31b(i),hb31b(i))=tof31(right,i,iadc)/xkorr
1178           endif           endif
1179        ENDIF        ENDIF
# Line 963  C-----------------------------S3 ------- Line 1185  C-----------------------------S3 -------
1185        IF (tof32_i.GT.none_find.AND.abs(xhelp).lt.100) THEN        IF (tof32_i.GT.none_find.AND.abs(xhelp).lt.100) THEN
1186    
1187           i = tof32_i           i = tof32_i
1188           if (tof32(left,i,iadc).lt.4095) then           if (tof32(left,i,iadc).lt.3786) then
1189    c          if (adc(ch32a(i),hb32a(i)).lt.4095) then
1190              tof32(left,i,iadc) = tof32(left,i,iadc)*cos(theta13)              tof32(left,i,iadc) = tof32(left,i,iadc)*cos(theta13)
1191              xkorr=adcx32(left,i,1)*exp(-xhelp/adcx32(left,i,2))              xkorr = atten(left,32,i,xhelp)
1192              xkorr=xkorr/hepratio  c            write(90+i,*) xhelp,xkorr
1193                if (iz.le.1) xkorr=xkorr/hepratio
1194              adctof_c(ch32a(i),hb32a(i))=tof32(left,i,iadc)/xkorr              adctof_c(ch32a(i),hb32a(i))=tof32(left,i,iadc)/xkorr
1195           endif           endif
1196    
1197           if (tof32(right,i,iadc).lt.4095) then           if (tof32(right,i,iadc).lt.3786) then
1198    c          if (adc(ch32b(i),hb32b(i)).lt.4095) then
1199              tof32(right,i,iadc) = tof32(right,i,iadc)*cos(theta13)              tof32(right,i,iadc) = tof32(right,i,iadc)*cos(theta13)
1200              xkorr=adcx32(right,i,1)*exp(xhelp/adcx32(right,i,2))              xkorr = atten(right,32,i,xhelp)
1201              xkorr=xkorr/hepratio  c            write(90+i,*) xhelp,xkorr
1202                if (iz.le.1) xkorr=xkorr/hepratio
1203              adctof_c(ch32b(i),hb32b(i))=tof32(right,i,iadc)/xkorr              adctof_c(ch32b(i),hb32b(i))=tof32(right,i,iadc)/xkorr
1204           endif           endif
1205        ENDIF        ENDIF
1206    
   
1207  C--------------------------------------------------------------------  C--------------------------------------------------------------------
1208  C----------------------calculate Beta  ------------------------------  C----------------------calculate Beta  ------------------------------
1209  C--------------------------------------------------------------------  C--------------------------------------------------------------------
# Line 999  C     S11 - S31 Line 1224  C     S11 - S31
1224           ds = xhelp1-xhelp2           ds = xhelp1-xhelp2
1225           ihelp=(tof11_i-1)*3+tof31_i           ihelp=(tof11_i-1)*3+tof31_i
1226           c1 = k_S11S31(1,ihelp)           c1 = k_S11S31(1,ihelp)
1227             if (iz.gt.2) c1 = c1 + k1corrA1
1228           c2 = k_S11S31(2,ihelp)           c2 = k_S11S31(2,ihelp)
1229           betatof_a(1) = c2/(cos(theta13)*(ds-c1))           betatof_a(1) = c2/(cos(theta13)*(ds-c1))
1230    
# Line 1027  C     S11 - S32 Line 1253  C     S11 - S32
1253           ds = xhelp1-xhelp2           ds = xhelp1-xhelp2
1254           ihelp=(tof11_i-1)*3+tof32_i           ihelp=(tof11_i-1)*3+tof32_i
1255           c1 = k_S11S32(1,ihelp)           c1 = k_S11S32(1,ihelp)
1256             if (iz.gt.2) c1 = c1 + k1corrA1
1257           c2 = k_S11S32(2,ihelp)           c2 = k_S11S32(2,ihelp)
1258           betatof_a(2) = c2/(cos(theta13)*(ds-c1))           betatof_a(2) = c2/(cos(theta13)*(ds-c1))
1259    
# Line 1055  C     S12 - S31 Line 1282  C     S12 - S31
1282           ds = xhelp1-xhelp2           ds = xhelp1-xhelp2
1283           ihelp=(tof12_i-1)*3+tof31_i           ihelp=(tof12_i-1)*3+tof31_i
1284           c1 = k_S12S31(1,ihelp)           c1 = k_S12S31(1,ihelp)
1285             if (iz.gt.2) c1 = c1 + k1corrA1
1286           c2 = k_S12S31(2,ihelp)           c2 = k_S12S31(2,ihelp)
1287           betatof_a(3) = c2/(cos(theta13)*(ds-c1))           betatof_a(3) = c2/(cos(theta13)*(ds-c1))
1288    
# Line 1083  C     S12 - S32 Line 1311  C     S12 - S32
1311           ds = xhelp1-xhelp2           ds = xhelp1-xhelp2
1312           ihelp=(tof12_i-1)*3+tof32_i           ihelp=(tof12_i-1)*3+tof32_i
1313           c1 = k_S12S32(1,ihelp)           c1 = k_S12S32(1,ihelp)
1314             if (iz.gt.2) c1 = c1 + k1corrA1
1315           c2 = k_S12S32(2,ihelp)           c2 = k_S12S32(2,ihelp)
1316           betatof_a(4) = c2/(cos(theta13)*(ds-c1))           betatof_a(4) = c2/(cos(theta13)*(ds-c1))
1317    
# Line 1111  C     S21 - S31 Line 1340  C     S21 - S31
1340           ds = xhelp1-xhelp2           ds = xhelp1-xhelp2
1341           ihelp=(tof21_i-1)*3+tof31_i           ihelp=(tof21_i-1)*3+tof31_i
1342           c1 = k_S21S31(1,ihelp)           c1 = k_S21S31(1,ihelp)
1343             if (iz.gt.2) c1 = c1 + k1corrB1
1344           c2 = k_S21S31(2,ihelp)           c2 = k_S21S31(2,ihelp)
1345           betatof_a(5) = c2/(cos(theta13)*(ds-c1))           betatof_a(5) = c2/(cos(theta13)*(ds-c1))
1346    
# Line 1139  C     S21 - S32 Line 1369  C     S21 - S32
1369           ds = xhelp1-xhelp2           ds = xhelp1-xhelp2
1370           ihelp=(tof21_i-1)*3+tof32_i           ihelp=(tof21_i-1)*3+tof32_i
1371           c1 = k_S21S32(1,ihelp)           c1 = k_S21S32(1,ihelp)
1372             if (iz.gt.2) c1 = c1 + k1corrB1
1373           c2 = k_S21S32(2,ihelp)           c2 = k_S21S32(2,ihelp)
1374           betatof_a(6) = c2/(cos(theta13)*(ds-c1))           betatof_a(6) = c2/(cos(theta13)*(ds-c1))
1375                                        
# Line 1167  C     S22 - S31 Line 1398  C     S22 - S31
1398           ds = xhelp1-xhelp2           ds = xhelp1-xhelp2
1399           ihelp=(tof22_i-1)*3+tof31_i           ihelp=(tof22_i-1)*3+tof31_i
1400           c1 = k_S22S31(1,ihelp)           c1 = k_S22S31(1,ihelp)
1401             if (iz.gt.2) c1 = c1 + k1corrB1
1402           c2 = k_S22S31(2,ihelp)           c2 = k_S22S31(2,ihelp)
1403           betatof_a(7) = c2/(cos(theta13)*(ds-c1))           betatof_a(7) = c2/(cos(theta13)*(ds-c1))
1404    
# Line 1195  C     S22 - S32 Line 1427  C     S22 - S32
1427           ds = xhelp1-xhelp2           ds = xhelp1-xhelp2
1428           ihelp=(tof22_i-1)*3+tof32_i           ihelp=(tof22_i-1)*3+tof32_i
1429           c1 = k_S22S32(1,ihelp)           c1 = k_S22S32(1,ihelp)
1430             if (iz.gt.2) c1 = c1 + k1corrB1
1431           c2 = k_S22S32(2,ihelp)           c2 = k_S22S32(2,ihelp)
1432           betatof_a(8) = c2/(cos(theta13)*(ds-c1))           betatof_a(8) = c2/(cos(theta13)*(ds-c1))
1433    
# Line 1223  C     S11 - S21 Line 1456  C     S11 - S21
1456           ds = xhelp1-xhelp2           ds = xhelp1-xhelp2
1457           ihelp=(tof11_i-1)*2+tof21_i           ihelp=(tof11_i-1)*2+tof21_i
1458           c1 = k_S11S21(1,ihelp)           c1 = k_S11S21(1,ihelp)
1459             if (iz.gt.2) c1 = c1 + k1corrC1
1460           c2 = k_S11S21(2,ihelp)           c2 = k_S11S21(2,ihelp)
1461           betatof_a(9) = c2/(cos(theta13)*(ds-c1))           betatof_a(9) = c2/(cos(theta13)*(ds-c1))
1462    
# Line 1251  C     S11 - S22 Line 1485  C     S11 - S22
1485           ds = xhelp1-xhelp2           ds = xhelp1-xhelp2
1486           ihelp=(tof11_i-1)*2+tof22_i           ihelp=(tof11_i-1)*2+tof22_i
1487           c1 = k_S11S22(1,ihelp)           c1 = k_S11S22(1,ihelp)
1488             if (iz.gt.2) c1 = c1 + k1corrC1
1489           c2 = k_S11S22(2,ihelp)           c2 = k_S11S22(2,ihelp)
1490           betatof_a(10) = c2/(cos(theta13)*(ds-c1))           betatof_a(10) = c2/(cos(theta13)*(ds-c1))
1491    
# Line 1279  C     S12 - S21 Line 1514  C     S12 - S21
1514           ds = xhelp1-xhelp2           ds = xhelp1-xhelp2
1515           ihelp=(tof12_i-1)*2+tof21_i           ihelp=(tof12_i-1)*2+tof21_i
1516           c1 = k_S12S21(1,ihelp)           c1 = k_S12S21(1,ihelp)
1517             if (iz.gt.2) c1 = c1 + k1corrC1
1518           c2 = k_S12S21(2,ihelp)           c2 = k_S12S21(2,ihelp)
1519           betatof_a(11) = c2/(cos(theta13)*(ds-c1))           betatof_a(11) = c2/(cos(theta13)*(ds-c1))
1520    
# Line 1307  C     S12 - S22 Line 1543  C     S12 - S22
1543           ds = xhelp1-xhelp2           ds = xhelp1-xhelp2
1544           ihelp=(tof12_i-1)*2+tof22_i           ihelp=(tof12_i-1)*2+tof22_i
1545           c1 = k_S12S22(1,ihelp)           c1 = k_S12S22(1,ihelp)
1546             if (iz.gt.2) c1 = c1 + k1corrC1
1547           c2 = k_S12S22(2,ihelp)           c2 = k_S12S22(2,ihelp)
1548           betatof_a(12) = c2/(cos(theta13)*(ds-c1))           betatof_a(12) = c2/(cos(theta13)*(ds-c1))
1549    
# Line 1327  C-------   Line 1564  C-------  
1564        ENDIF        ENDIF
1565    
1566  C---------------------------------------------------------  C---------------------------------------------------------
1567    C
1568    C      icount=0
1569    C      sw=0.
1570    C      sxw=0.
1571    C      beta_mean=100.
1572    C
1573    C      do i=1,12
1574    C         if ((betatof_a(i).gt.-1.5).and.(betatof_a(i).lt.1.5)) then
1575    C            icount= icount+1
1576    C            if (i.le.4) w_i=1./(0.13**2.)
1577    C            if ((i.ge.5).and.(i.le.8)) w_i=1./(0.16**2.)
1578    C           if (i.ge.9) w_i=1./(0.25**2.)     ! to be checked
1579    C            sxw=sxw + betatof_a(i)*w_i
1580    C            sw =sw + w_i
1581    C         endif
1582    C      enddo
1583    C      
1584    C      if (icount.gt.0) beta_mean=sxw/sw
1585    C      betatof_a(13) = beta_mean
1586    C
1587    C--------  New mean beta  calculation  -----------------------
1588    
1589        icount=0          do i=1,12
1590        sw=0.           btemp(i) = betatof_a(i)
1591        sxw=0.          enddo
       beta_mean=100.  
1592    
1593        do i=1,12         betatof_a(13)=newbeta(btemp,hitvec,10.,10.,20.)
          if ((betatof_a(i).gt.-1.5).and.(betatof_a(i).lt.1.5)) then  
             icount= icount+1  
             if (i.le.4) w_i=1./(0.13**2.)  
             if ((i.ge.5).and.(i.le.8)) w_i=1./(0.16**2.)  
             if (i.ge.9) w_i=1./(0.25**2.)     ! to be checked  
             sxw=sxw + betatof_a(i)*w_i  
             sw =sw + w_i  
          endif  
       enddo  
         
       if (icount.gt.0) beta_mean=sxw/sw  
       betatof_a(13) = beta_mean  
1594    
1595    C--------------------------------------------------------------
1596    C      write(*,*) betatof_a
1597  c      write(*,*) xtofpos  c      write(*,*) xtofpos
1598  c      write(*,*) ytofpos  c      write(*,*) ytofpos
1599  c      write(*,*) betatof_a  c      write(*,*)'tofl2com beta', betatof_a
1600  C      write(*,*) adcflagtof  C      write(*,*) adcflagtof
1601    c      write(*,*) 'tofl2com'
1602    c      write(*,*) xtofpos
1603    c      write(*,*) ytofpos
1604    c      write(*,*) xtr_tof
1605    c      write(*,*) ytr_tof
1606          
1607   100  continue   100  continue
1608    
1609  C  C
1610        RETURN        RETURN
1611        END        END
1612    
1613    
1614    C------------------------------------------------------------------
1615    C------------------------------------------------------------------
1616    
1617           function atten(is,ilay,ipad,x)
1618           include  'input_tof.txt'
1619           real atten
1620           real x
1621           real xmin,xmax
1622           integer  ilay,ipad
1623    
1624    *  S11 8 paddles  33.0 x 5.1 cm
1625    *  S12 6 paddles  40.8 x 5.5 cm
1626    *  S21 2 paddles  18.0 x 7.5 cm
1627    *  S22 2 paddles  15.0 x 9.0 cm
1628    *  S31 3 paddles  15.0 x 6.0 cm
1629    *  S32 3 paddles  18.0 x 5.0 cm
1630    
1631    
1632    c       if (ilay.eq.11) write(*,*) 'start ',ipad,is,adcx11(is,ipad,1),
1633    c     &  adcx11(is,ipad,2),adcx11(is,ipad,3),adcx11(is,ipad,4)
1634    c       if (ilay.eq.12) write(*,*) 'start ',ipad,is,adcx12(is,ipad,1),
1635    c     &  adcx12(is,ipad,2),adcx12(is,ipad,3),adcx12(is,ipad,4)
1636    
1637    
1638           if (ilay.eq.11)  xmin=-33.0/2.
1639           if (ilay.eq.11)  xmax= 33.0/2.
1640           if (ilay.eq.12)  xmin=-40.8/2.
1641           if (ilay.eq.12)  xmax= 40.8/2.
1642    
1643           if (ilay.eq.21)  xmin=-18.0/2.
1644           if (ilay.eq.21)  xmax= 18.0/2.
1645           if (ilay.eq.22)  xmin=-15.0/2.
1646           if (ilay.eq.22)  xmax= 15.0/2.
1647    
1648           if (ilay.eq.31)  xmin=-15.0/2.
1649           if (ilay.eq.31)  xmax= 15.0/2.
1650           if (ilay.eq.32)  xmin=-18.0/2.
1651           if (ilay.eq.32)  xmax= 18.0/2.
1652    
1653           if (x .lt. xmin) x=xmin
1654           if (x .gt. xmax) x=xmax
1655    
1656    
1657           if (ilay.eq.11) atten=
1658         &    adcx11(is,ipad,1)*exp(x*adcx11(is,ipad,2))
1659         &  + adcx11(is,ipad,3)*exp(x*adcx11(is,ipad,4))
1660    
1661           if (ilay.eq.12) atten=
1662         &    adcx12(is,ipad,1)*exp(x*adcx12(is,ipad,2))
1663         &  + adcx12(is,ipad,3)*exp(x*adcx12(is,ipad,4))
1664    
1665           if (ilay.eq.21) atten=
1666         &    adcx21(is,ipad,1)*exp(x*adcx21(is,ipad,2))
1667         &  + adcx21(is,ipad,3)*exp(x*adcx21(is,ipad,4))
1668    
1669           if (ilay.eq.22) atten=
1670         &    adcx22(is,ipad,1)*exp(x*adcx22(is,ipad,2))
1671         &  + adcx22(is,ipad,3)*exp(x*adcx22(is,ipad,4))
1672    
1673           if (ilay.eq.31) atten=
1674         &    adcx31(is,ipad,1)*exp(x*adcx31(is,ipad,2))
1675         &  + adcx31(is,ipad,3)*exp(x*adcx31(is,ipad,4))
1676    
1677           if (ilay.eq.32) atten=
1678         &    adcx32(is,ipad,1)*exp(x*adcx32(is,ipad,2))
1679         &  + adcx32(is,ipad,3)*exp(x*adcx32(is,ipad,4))
1680    
1681            if (atten.gt.10000) atten=10000.
1682    
1683           end
1684    
1685    C------------------------------------------------------------------
1686    C------------------------------------------------------------------
1687    
1688           function pc_adc(ix)
1689           include  'input_tof.txt'
1690           real pc_adc
1691           integer ix
1692    
1693           pc_adc=28.0407 + 0.628929*ix
1694         &   - 5.80901e-05*ix*ix + 3.14092e-08*ix*ix*ix
1695    c       write(*,*) ix,pc_adc
1696           end
1697    
1698    C------------------------------------------------------------------
1699    C------------------------------------------------------------------
1700    
1701            function check_charge(theta,hitvec)
1702    
1703            include  'input_tof.txt'
1704            include  'tofcomm.txt'
1705    
1706            real check_charge  
1707            integer hitvec(6)  
1708            REAL CHARGE, theta
1709    
1710    C  upper and lower limits  for the helium selection
1711            REAL A_l(24),A_h(24)
1712            DATA A_l /200,190,300,210,220,200,210,60,60,120,220,
1713         &  120,160,50,300,200,120,250,350,300,350,250,280,300/
1714            DATA A_h /550,490,800,600,650,600,600,260,200,380,
1715         &  620,380,550,200,850,560,400,750,900,800,880,800,750,800/
1716    
1717    C The k1 constants for the beta calculation, only for S1-S3
1718    C k2 constant is taken to be the standard 2D/c
1719            REAL k1(84)
1720            DATA k1 /50,59.3296,28.4328,-26.0818,5.91253,-19.588,
1721         &   -9.26316,24.7544,2.32465,-50.5058,-15.3195,-39.1443,
1722         &   -91.2546,-58.6243,-84.5641,-63.1516,-32.2091,-58.3358,
1723         &   13.8084,45.5322,33.2416,-11.5313,51.3271,75,-14.1141,
1724         &   42.8466,15.1794,-63.6672,-6.07739,-32.164,-41.771,10.5274,
1725         &   -9.46096,-81.7404,-28.783,-52.7167,-127.394,-69.6166,
1726         &   -93.4655,-98.9543,-42.863,-67.8244,-19.3238,31.1221,8.7319,
1727         &   -43.1627,5.55573,-14.4078,-83.4466,-47.4647,-77.8379,
1728         &   -108.222,-75.986,-101.297,-96.0205,-63.1881,-90.1372,
1729         &   -22.7347,8.31409,-19.6912,-7.49008,23.6979,-1.66677,
1730         &   1.81556,34.4668,6.23693,-100,-59.5861,-90.9159,-141.639,
1731         &   -89.2521,-112.881,-130.199,-77.0357,-98.4632,-60.2086,
1732         &   -4.82097,-29.3705,-43.6469,10.5884,-9.31304,-35.3329,
1733         &   25.2514,25.6/
1734    
1735    
1736    
1737            REAL zin(6)
1738            DATA zin /53.74, 53.04, 23.94, 23.44, -23.49, -24.34/
1739    
1740            REAL  c1,c2,xhelp,xhelp1,xhelp2,ds,dist,F
1741            REAL  sw,sxw,beta_mean_tof,w_i
1742            INTEGER  ihelp
1743            INTEGER ipmt(4)
1744            REAL time(4),beta1(4)
1745    
1746            REAL  adca(48), tdca(48)
1747    
1748            REAL  a1,a2
1749            INTEGER jj
1750    
1751    C-----------------------------------------------------------
1752    C--- get data
1753    C-----------------------------------------------------------
1754    
1755             do j=1,8
1756             ih = 1 + 0 +((j-1)*2)
1757             adca(ih)   = adc(ch11a(j),hb11a(j))
1758             adca(ih+1) = adc(ch11b(j),hb11b(j))
1759             tdca(ih)   = tdc(ch11a(j),hb11a(j))
1760             tdca(ih+1) = tdc(ch11b(j),hb11b(j))
1761             enddo
1762    
1763             do j=1,6
1764             ih = 1 + 16+((j-1)*2)
1765             adca(ih)   = adc(ch12a(j),hb12a(j))
1766             adca(ih+1) = adc(ch12b(j),hb12b(j))
1767             tdca(ih)   = tdc(ch12a(j),hb12a(j))
1768             tdca(ih+1) = tdc(ch12b(j),hb12b(j))
1769             enddo
1770    
1771             do j=1,2
1772             ih = 1 + 28+((j-1)*2)
1773             adca(ih)   = adc(ch21a(j),hb21a(j))
1774             adca(ih+1) = adc(ch21b(j),hb21b(j))
1775             tdca(ih)   = tdc(ch21a(j),hb21a(j))
1776             tdca(ih+1) = tdc(ch21b(j),hb21b(j))
1777             enddo
1778    
1779             do j=1,2
1780             ih = 1 + 32+((j-1)*2)
1781             adca(ih)   = adc(ch22a(j),hb22a(j))
1782             adca(ih+1) = adc(ch22b(j),hb22b(j))
1783             tdca(ih)   = tdc(ch22a(j),hb22a(j))
1784             tdca(ih+1) = tdc(ch22b(j),hb22b(j))
1785             enddo
1786    
1787             do j=1,3
1788             ih = 1 + 36+((j-1)*2)
1789             adca(ih)   = adc(ch31a(j),hb31a(j))
1790             adca(ih+1) = adc(ch31b(j),hb31b(j))
1791             tdca(ih)   = tdc(ch31a(j),hb31a(j))
1792             tdca(ih+1) = tdc(ch31b(j),hb31b(j))
1793             enddo
1794    
1795             do j=1,3
1796             ih = 1 + 42+((j-1)*2)
1797             adca(ih)   = adc(ch32a(j),hb32a(j))
1798             adca(ih+1) = adc(ch32b(j),hb32b(j))
1799             tdca(ih)   = tdc(ch32a(j),hb32a(j))
1800             tdca(ih+1) = tdc(ch32b(j),hb32b(j))
1801             enddo
1802    
1803    
1804    c         write(*,*) adca
1805    c         write(*,*) tdca
1806    
1807    
1808    C============   calculate beta and select charge > Z=1   ===============
1809    
1810            ICHARGE=1
1811    
1812    C find hitted paddle by looking for ADC values on both sides
1813    C since we looking for Z>1 this gives decent results
1814    
1815            tof11_i = hitvec(1)-1
1816            tof12_i = hitvec(2)-1
1817            tof21_i = hitvec(3)-1
1818            tof22_i = hitvec(4)-1
1819            tof31_i = hitvec(5)-1
1820            tof32_i = hitvec(6)-1
1821    
1822    c        write(*,*) ' in charge check'
1823    c        write(*,*) theta,tof11_i,tof12_i,tof21_i,tof22_i,tof31_i,tof32_i
1824    
1825    C----------------------------------------------------------------
1826    
1827            beta_help=100.
1828            beta_mean_tof=100.
1829    
1830            do jj=1,4
1831              beta1(jj) = 100.
1832            enddo
1833    
1834    C----------------------------------------------------------------
1835    C---------  S1 - S3 ---------------------------------------------
1836    C----------------------------------------------------------------
1837    
1838    C---------  S11 - S31 -------------------------------------------
1839    
1840            if ((tof11_i.gt.-1).and.(tof31_i.gt.-1)) then
1841    
1842            dist = zin(1) - zin(5)
1843            c2 = (2.*0.01*dist)/(3.E08*50.E-12)
1844            F = 1./cos(theta)
1845    
1846            ipmt(1)   = (tof11_i)*2+1
1847            ipmt(2)   = (tof11_i)*2+2
1848            ipmt(3)   = 36+(tof31_i)*2+1
1849            ipmt(4)   = 36+(tof31_i)*2+2
1850    
1851    c        write(*,*) ipmt
1852    
1853            do jj=1,4
1854               time(jj) = tdca(ipmt(jj))
1855            enddo
1856    
1857    c        write(*,*) time
1858    
1859            if ((time(1).lt.4095).and.(time(2).lt.4095).and.
1860         &     (time(3).lt.4095).and.(time(4).lt.4095)) then
1861             xhelp1 = time(1) + time(2)
1862             xhelp2 = time(3) + time(4)
1863             ds = xhelp1-xhelp2
1864             ihelp=0+(tof11_i)*3+tof31_i
1865             c1 = k1(ihelp+1)
1866             beta1(1) = c2*F/(ds-c1);
1867                                                     endif
1868    c         write(*,*) beta1(1)
1869             endif  ! tof_....
1870    
1871    
1872    C---------  S11 - S32 -------------------------------------------
1873    
1874            if ((tof11_i.gt.-1).and.(tof32_i.gt.-1)) then
1875    
1876            dist = zin(1) - zin(6)
1877            c2 = (2.*0.01*dist)/(3.E08*50.E-12)
1878            F = 1./cos(theta)
1879    
1880            ipmt(1)   = (tof11_i)*2+1
1881            ipmt(2)   = (tof11_i)*2+2
1882            ipmt(3)   = 42+(tof32_i)*2+1
1883            ipmt(4)   = 42+(tof32_i)*2+2
1884    
1885            do jj=1,4
1886               time(jj) = tdca(ipmt(jj))
1887            enddo
1888    
1889            if ((time(1).lt.4095).and.(time(2).lt.4095).and.
1890         &     (time(3).lt.4095).and.(time(4).lt.4095)) then
1891             xhelp1 = time(1) + time(2)
1892             xhelp2 = time(3) + time(4)
1893             ds = xhelp1-xhelp2
1894             ihelp=24+(tof11_i)*3+tof32_i
1895             c1 = k1(ihelp+1)
1896             beta1(2) = c2*F/(ds-c1);
1897                                                     endif
1898             endif  ! tof_....
1899    
1900    
1901    C---------  S12 - S31 -------------------------------------------
1902    
1903            if ((tof12_i.gt.-1).and.(tof31_i.gt.-1)) then
1904    
1905            dist = zin(2) - zin(5)
1906            c2 = (2.*0.01*dist)/(3.E08*50.E-12)
1907            F = 1./cos(theta)
1908    
1909            ipmt(1)   = 16+(tof12_i)*2+1
1910            ipmt(2)   = 16+(tof12_i)*2+2
1911            ipmt(3)   = 36+(tof31_i)*2+1
1912            ipmt(4)   = 36+(tof31_i)*2+2
1913    
1914            do jj=1,4
1915               time(jj) = tdca(ipmt(jj))
1916            enddo
1917    
1918            if ((time(1).lt.4095).and.(time(2).lt.4095).and.
1919         &     (time(3).lt.4095).and.(time(4).lt.4095)) then
1920             xhelp1 = time(1) + time(2)
1921             xhelp2 = time(3) + time(4)
1922             ds = xhelp1-xhelp2
1923             ihelp=48+(tof12_i)*3+tof31_i
1924             c1 = k1(ihelp+1)
1925             beta1(3) = c2*F/(ds-c1);
1926                                                     endif
1927             endif  ! tof_....
1928    
1929    
1930    C---------  S12 - S32 -------------------------------------------
1931    
1932            if ((tof12_i.gt.-1).and.(tof32_i.gt.-1)) then
1933    
1934            dist = zin(2) - zin(6)
1935            c2 = (2.*0.01*dist)/(3.E08*50.E-12)
1936            F = 1./cos(theta)
1937    
1938            ipmt(1)   = 16+(tof12_i)*2+1
1939            ipmt(2)   = 16+(tof12_i)*2+2
1940            ipmt(3)   = 42+(tof32_i)*2+1
1941            ipmt(4)   = 42+(tof32_i)*2+2
1942    
1943            do jj=1,4
1944               time(jj) = tdca(ipmt(jj))
1945            enddo
1946    
1947            if ((time(1).lt.4095).and.(time(2).lt.4095).and.
1948         &     (time(3).lt.4095).and.(time(4).lt.4095)) then
1949             xhelp1 = time(1) + time(2)
1950             xhelp2 = time(3) + time(4)
1951             ds = xhelp1-xhelp2
1952             ihelp=56+(tof12_i)*3+tof32_i
1953             c1 = k1(ihelp+1)
1954             beta1(4) = c2*F/(ds-c1);
1955                                                     endif
1956    
1957             endif  ! tof_....
1958    
1959    c         write(*,*) beta1
1960      
1961    C----  calculate  beta mean, only downward going particles are interesting ----
1962    
1963             sw=0.
1964             sxw=0.
1965             beta_mean_tof=100.
1966    
1967            do jj=1,4
1968            if ((beta1(jj).gt.0.1).and.(beta1(jj).lt.2.0)) then
1969                w_i=1./(0.13*0.13)
1970                sxw=sxw + beta1(jj)*w_i
1971                sw =sw + w_i ;
1972                                                          endif
1973            enddo
1974    
1975            if (sw.gt.0) beta_mean_tof=sxw/sw;
1976    
1977    c        write(*,*) 'beta_mean_tof ',beta_mean_tof
1978    
1979            beta_help = beta_mean_tof  !  pow(beta_mean_tof,1.0) gave best results
1980    
1981    CCCCC        endif  !  if tof11_i > -1 && ...... beta calculation
1982    
1983    C-----------------------  Select charge   --------------------------
1984    
1985           charge=0
1986    
1987           if ((beta_mean_tof.gt.0.2).and.(beta_mean_tof.lt.2.0)) then
1988    
1989            icount1=0
1990            icount2=0
1991            icount3=0
1992    
1993            do jj=0,23
1994            a1 = adca(2*jj+1)
1995            a2 = adca(2*jj+2)
1996            if ((a1.lt.4095).and.(a2.lt.4095)) then
1997            a1 = adca(2*jj+1)*cos(theta)
1998            a2 = adca(2*jj+2)*cos(theta)
1999            xhelp  = 100000.
2000            xhelp1 = 100000.
2001            xhelp = sqrt(a1*a2)  ! geometric mean
2002            xhelp1 = beta_help*xhelp
2003    C if geometric mean multiplied by beta_help is inside/outside helium
2004    C limits, increase counter
2005           if (xhelp1.lt.A_l(jj+1))  icount1=icount1+1
2006           if ((xhelp1.gt.A_l(jj+1)).and.(xhelp1.lt.A_h(jj+1)))
2007         &                           icount2=icount2+1
2008           if (xhelp1.gt.A_h(jj+1))  icount3=icount3+1
2009                                                endif
2010            enddo
2011    
2012    
2013    C  if more than three paddles see the same...
2014    
2015            if (icount1 .gt. 3) charge=1
2016            if (icount2 .gt. 3) charge=2
2017            if (icount3 .gt. 3) charge=3
2018    
2019                                                        endif  ! 0.2<beta<2.0
2020    
2021    C  no beta found? Sum up geometric means of paddles and derive the mean...
2022    
2023           if (beta_mean_tof.eq.100.) then
2024    
2025           xhelp  = 0.
2026           icount = 0
2027    
2028           if (tof11_i.gt.-1) then
2029           jj=tof11_i
2030           a1 = adca(0+2*jj+1)
2031           a2 = adca(0+2*jj+2)
2032           if ((a1.lt.4095).and.(a2.lt.4095)) then
2033           a1 = a1*cos(theta)
2034           a2 = a2*cos(theta)
2035           xhelp = xhelp + sqrt(a1*a2)
2036           icount=icount+1
2037                        endif
2038                        endif
2039    
2040           if (tof12_i.gt.-1) then
2041           jj=tof12_i
2042           a1 = adca(16+2*jj+1)
2043           a2 = adca(16+2*jj+2)
2044           if ((a1.lt.4095).and.(a2.lt.4095)) then
2045           a1 = a1*cos(theta)
2046           a2 = a2*cos(theta)
2047           xhelp = xhelp + sqrt(a1*a2)
2048           icount=icount+1
2049                        endif
2050                        endif
2051    
2052           if (tof21_i.gt.-1) then
2053           jj=tof21_i
2054           a1 = adca(28+2*jj+1)
2055           a2 = adca(28+2*jj+2)
2056           if ((a1.lt.4095).and.(a2.lt.4095)) then
2057           a1 = a1*cos(theta)
2058           a2 = a2*cos(theta)
2059           xhelp = xhelp + sqrt(a1*a2)
2060           icount=icount+1
2061                        endif
2062                        endif
2063    
2064           if (tof22_i.gt.-1) then
2065           jj=tof22_i
2066           a1 = adca(32+2*jj+1)
2067           a2 = adca(32+2*jj+2)
2068           if ((a1.lt.4095).and.(a2.lt.4095)) then
2069           a1 = a1*cos(theta)
2070           a2 = a2*cos(theta)
2071           xhelp = xhelp + sqrt(a1*a2)
2072           icount=icount+1
2073                        endif
2074                        endif
2075    
2076           if (tof31_i.gt.-1) then
2077           jj=tof31_i
2078           a1 = adca(36+2*jj+1)
2079           a2 = adca(36+2*jj+2)
2080           if ((a1.lt.4095).and.(a2.lt.4095)) then
2081           a1 = a1*cos(theta)
2082           a2 = a2*cos(theta)
2083           xhelp = xhelp + sqrt(a1*a2)
2084           icount=icount+1
2085                        endif
2086                        endif
2087    
2088           if (tof32_i.gt.-1) then
2089           jj=tof32_i
2090           a1 = adca(42+2*jj+1)
2091           a2 = adca(42+2*jj+2)
2092           if ((a1.lt.4095).and.(a2.lt.4095)) then
2093           a1 = a1*cos(theta)
2094           a2 = a2*cos(theta)
2095           xhelp = xhelp + sqrt(a1*a2)
2096           icount=icount+1
2097                        endif
2098                        endif
2099    
2100    
2101           if (icount.gt.0) xhelp=xhelp/icount
2102           if ((icount.gt.2).and.(xhelp.gt.1500.)) charge=3
2103    
2104                                      endif  ! beta_mean_tof.eq.100.
2105    
2106    c        write(*,*) 'in function charge: ',charge
2107            check_charge = charge
2108    
2109    
2110            END
2111    
2112    C****************************************************************************
2113    C****************************************************************************
2114    C****************************************************************************
2115    
2116            function newbeta(b,hitvec,resmax,qualitycut,chi2cut)
2117    
2118            include  'input_tof.txt'
2119            include  'output_tof.txt'
2120            include  'tofcomm.txt'
2121    
2122            REAL newbeta
2123            REAL resmax,qualitycut,chi2cut
2124            REAL w_i(12),w_il(6),quality,res,betachi,beta_mean_inv
2125            REAL sw,sxw,b(12),beta_mean,chi2,xhelp
2126    
2127            INTEGER icount,hitvec(6)
2128    
2129            INTEGER itop(12),ibot(12)
2130            DATA itop /1,1,2,2,3,3,4,4,1,1,2,2/
2131            DATA ibot /5,6,5,6,5,6,5,6,3,4,3,4/
2132    
2133    C====================================================================
2134    
2135            tof11_i = hitvec(1)
2136            tof12_i = hitvec(2)
2137            tof21_i = hitvec(3)
2138            tof22_i = hitvec(4)
2139            tof31_i = hitvec(5)
2140            tof32_i = hitvec(6)
2141    
2142    c        write(*,*) '------------ In NEWBETA  ----------------'
2143    c        write(*,*) hitvec
2144    c        write(*,*) b
2145    
2146    C---  Find out ToF layers with artificial TDC values    -------------
2147    
2148            do jj=1,6
2149            w_il(jj) = 1000.
2150            enddo
2151    
2152    C        write(*,*) tdcflagtof
2153    
2154            if (tof11_i.gt.0) then
2155            if ((tofmask(ch11a(tof11_i),hb11a(tof11_i)).gt.0).or.
2156         &   (tofmask(ch11b(tof11_i),hb11b(tof11_i)).gt.0)) then
2157            w_il(1)=0
2158            i1=tdcflagtof(ch11a(tof11_i),hb11a(tof11_i))
2159            i2=tdcflagtof(ch11b(tof11_i),hb11b(tof11_i))
2160    c               write(*,*) '11 ',i1,i2
2161            if ((i1.eq.1).or.(i2.eq.1)) w_il(1) = 1  ! tdcflag
2162                                                          endif
2163                               endif
2164    
2165            if (tof12_i.gt.0) then
2166            if ((tofmask(ch12a(tof12_i),hb12a(tof12_i)).gt.0).or.
2167         &   (tofmask(ch12b(tof12_i),hb12b(tof12_i)).gt.0)) then
2168            w_il(2)=0
2169            i1=tdcflagtof(ch12a(tof12_i),hb12a(tof12_i))
2170            i2=tdcflagtof(ch12b(tof12_i),hb12b(tof12_i))
2171    c               write(*,*) '12 ',i1,i2
2172            if ((i1.eq.1).or.(i2.eq.1)) w_il(2) = 1  ! tdcflag
2173                                                          endif
2174                               endif
2175    
2176            if (tof21_i.gt.0) then
2177            if ((tofmask(ch21a(tof21_i),hb21a(tof21_i)).gt.0).or.
2178         &   (tofmask(ch21b(tof21_i),hb21b(tof21_i)).gt.0)) then
2179            w_il(3)=0
2180            i1=tdcflagtof(ch21a(tof21_i),hb21a(tof21_i))
2181            i2=tdcflagtof(ch21b(tof21_i),hb21b(tof21_i))
2182    c               write(*,*) '21 ',i1,i2
2183            if ((i1.eq.1).or.(i2.eq.1)) w_il(3) = 1  ! tdcflag
2184                                                          endif
2185                               endif
2186    
2187            if (tof22_i.gt.0) then
2188            if ((tofmask(ch22a(tof22_i),hb22a(tof22_i)).gt.0).or.
2189         &   (tofmask(ch22b(tof22_i),hb22b(tof22_i)).gt.0)) then
2190            w_il(4)=0
2191            i1=tdcflagtof(ch22a(tof22_i),hb22a(tof22_i))
2192            i2=tdcflagtof(ch22b(tof22_i),hb22b(tof22_i))
2193    c               write(*,*) '22 ',i1,i2
2194            if ((i1.eq.1).or.(i2.eq.1)) w_il(4) = 1  ! tdcflag
2195                                                          endif
2196                               endif
2197    
2198            if (tof31_i.gt.0) then
2199            if ((tofmask(ch31a(tof31_i),hb11a(tof31_i)).gt.0).or.
2200         &   (tofmask(ch31b(tof31_i),hb31b(tof31_i)).gt.0)) then
2201            w_il(5)=0
2202            i1=tdcflagtof(ch31a(tof31_i),hb31a(tof31_i))
2203            i2=tdcflagtof(ch31b(tof31_i),hb31b(tof31_i))
2204    c               write(*,*) '31 ',i1,i2
2205            if ((i1.eq.1).or.(i2.eq.1)) w_il(5) = 1  ! tdcflag
2206                                                          endif
2207                               endif
2208    
2209            if (tof32_i.gt.0) then
2210            if ((tofmask(ch32a(tof32_i),hb32a(tof32_i)).gt.0).or.
2211         &   (tofmask(ch32b(tof32_i),hb32b(tof32_i)).gt.0)) then
2212            w_il(6)=0
2213            i1=tdcflagtof(ch32a(tof32_i),hb32a(tof32_i))
2214            i2=tdcflagtof(ch32b(tof32_i),hb32b(tof32_i))
2215    c               write(*,*) '32 ',i1,i2
2216            if ((i1.eq.1).or.(i2.eq.1)) w_il(6) = 1  ! tdcflag
2217                                                          endif
2218                               endif
2219    
2220    c        write(*,*) w_il
2221    C------------------------------------------------------------------------
2222    C---  Set weights for the 12 measurements using information for top and bottom:
2223    C---  if no measurements: weight = set to very high value=> not used
2224    C---  top or bottom artificial: weight*sqrt(2)
2225    C---  top and bottom artificial: weight*sqrt(2)*sqrt(2)
2226    
2227           DO jj=1,12
2228           if (jj.le.4)           xhelp = 0.11            ! S1-S3
2229           if ((jj.gt.4).and.(jj.le.8)) xhelp = 0.18      ! S2-S3
2230           if (jj.gt.8)           xhelp = 0.28            ! S1-S2
2231           if ((w_il(itop(jj)).eq.1000.).and.(w_il(ibot(jj)).eq.1000.))
2232         &   xhelp = 1.E09
2233           if ((w_il(itop(jj)).eq.1).or.(w_il(ibot(jj)).eq.1.))
2234         &   xhelp = xhelp*1.414
2235           if ((w_il(itop(jj)).eq.1).and.(w_il(ibot(jj)).eq.1.))
2236         &   xhelp = xhelp*2.
2237           w_i(jj) = 1./xhelp
2238           ENDDO
2239    
2240    c       write(*,*) w_i
2241    
2242    C========================================================================
2243    C--- Calculate mean beta for the first time -----------------------------
2244    C--- We are using "1/beta" since its error is gaussian ------------------
2245    
2246            icount=0
2247            sw=0.
2248            sxw=0.
2249            beta_mean=100.
2250    
2251            DO jj=1,12
2252            IF ((abs(1./b(jj)).gt.0.1).and.(abs(1./b(jj)).lt.15.)) THEN
2253                icount = icount+1
2254                sxw    = sxw + (1./b(jj))*w_i(jj)*w_i(jj)
2255                sw     = sw + w_i(jj)*w_i(jj)
2256            ENDIF
2257            ENDDO
2258    
2259            if (icount.gt.0) beta_mean=1./(sxw/sw)
2260            beta_mean_inv = 1./beta_mean
2261    
2262    c        write(*,*) icount,beta_mean
2263      
2264    C--- Calculate beta for the second time, use residuals of the single
2265    C--- measurements to get a chi2 value
2266    
2267            icount  = 0
2268            sw      = 0.
2269            sxw     = 0.
2270            betachi = 100.
2271            chi2    = 0.
2272            quality = 0.
2273    
2274            DO jj=1,12
2275            IF ((abs(1./b(jj)).gt.0.1).and.(abs(1./b(jj)).lt.15.)
2276         &                                .and.(w_i(jj).GT.0.01)) THEN
2277                res    = beta_mean_inv - (1./b(jj)) ;
2278    C            write(*,*) jj,abs(res*w_i(jj))
2279                if (abs(res*w_i(jj)).lt.resmax) THEN
2280                chi2   = chi2 + (res*w_i(jj))**2.
2281                icount = icount+1
2282                sxw    = sxw + (1./b(jj))*w_i(jj)*w_i(jj)
2283                sw     = sw + w_i(jj)*w_i(jj)
2284                                                ENDIF
2285            ENDIF
2286            ENDDO
2287    
2288    c        quality = sw
2289            quality = sqrt(sw)
2290    
2291            if (icount.eq.0) chi2 = 1000.
2292            if (icount.gt.0) chi2 = chi2/(icount)
2293    
2294            if (icount.gt.0) betachi=1./(sxw/sw);
2295    
2296            beta_mean=100.
2297            if ((chi2.lt.chi2cut).and.(quality.gt.qualitycut))
2298         &  beta_mean = betachi
2299    c        write(*,*) icount,chi2,quality,beta_mean
2300            newbeta = beta_mean
2301    
2302            END
2303    
2304    C****************************************************************************
2305    
2306    
2307          

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.7

  ViewVC Help
Powered by ViewVC 1.1.23