/[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.1 by mocchiut, Sat Jun 17 12:14:56 2006 UTC revision 1.14 by mocchiut, Wed Feb 17 11:50:53 2010 UTC
# Line 1  Line 1 
1  *****************************************************************************  
2    C******************************************************************************
3    C
4    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    C
7    C  jan-07 GF: ADCflags(4,12) inserted to flag artificial ADC values
8    C  jan-07 WM: artificial ADC values created using attenuation calibration
9    C  jan-07 WM: modified xtofpos flag "101". xtofpos must be inside physical
10    C             dimension of the paddle +/- 10 cm
11    C  jan-07 WM: if xtofpos=101 then this paddle is not used for beta
12    C             calculation
13    C  jan-07 WM: the definition for a "hit" is changed: Now we must have a
14    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  mar-08 WM: Call to "newbeta" changed, now a flag tells the function if the
30    C             call comes from "tofl2com" or form "toftrack"
31    C  mar-08 WM: Bug found in dEdx if check_charge>1
32    C  oct-08 WM: Calculation of zenith angle debugged, sometimes strange values
33    C             were possible
34    C  nov-09 WM: the dEdx part ("adctof_c") moved to the new dEdx routine from Napoli
35    C  feb-10 WM: k1 values now for Z=1, Z=2, Z>2, k2 values are fix
36    C******************************************************************************
37    
38        INTEGER FUNCTION TOFL2COM()        INTEGER FUNCTION TOFL2COM()
39  c      c    
40        IMPLICIT NONE        IMPLICIT NONE
# Line 13  C     Line 49  C    
49        LOGICAL check        LOGICAL check
50        REAL secure        REAL secure
51    
52        INTEGER j        INTEGER j,hitvec(6)
       REAL xhelp_a,xhelp_t  
53    
54        REAL dx,dy,dr,ds        REAL dx,dy,dr,ds
55        REAL yhelp,xdummy,xkorr0,xhelp,xhelp1,xhelp2        REAL yhelp,yhelp1,yhelp2,xhelp,xhelp1,xhelp2
56        REAL c1,c2,sw,sxw,w_i        REAL c1,c2
57        INTEGER icount        REAL dist
58    
59    C      REAL sw,sxw,w_i
60    C      INTEGER icount
61    C      REAL beta_mean
62    
63        INTEGER tof11_j,tof21_j,tof31_j        INTEGER tof11_j,tof21_j,tof31_j
64        INTEGER tof12_j,tof22_j,tof32_j        INTEGER tof12_j,tof22_j,tof32_j
65    
       REAL beta_mean  
   
   
66  c     value for status of each PM-data  c     value for status of each PM-data
67  c     first index  : 1 = left, 2 = right  c     first index  : 1 = left, 2 = right
68  c     second index : 1... number of paddle  c     second index : 1... number of paddle
# Line 34  c     second index : 1... number of padd Line 70  c     second index : 1... number of padd
70        INTEGER tof21_event(2,2),tof22_event(2,2)        INTEGER tof21_event(2,2),tof22_event(2,2)
71        INTEGER tof31_event(2,3),tof32_event(2,3)        INTEGER tof31_event(2,3),tof32_event(2,3)
72    
73    
74          REAL y_coor_lin11c(8,2),x_coor_lin12c(6,2)
75          REAL x_coor_lin21c(2,2),y_coor_lin22c(2,2)
76          REAL y_coor_lin31c(3,2),x_coor_lin32c(3,2)
77    
78          DATA y_coor_lin11c(1,1),y_coor_lin11c(1,2) /-20.66,-2.497/
79          DATA y_coor_lin11c(2,1),y_coor_lin11c(2,2) /-9.10, -2.52/
80          DATA y_coor_lin11c(3,1),y_coor_lin11c(3,2) /-24.07,-2.12/
81          DATA y_coor_lin11c(4,1),y_coor_lin11c(4,2) /-13.40,-2.47/
82          DATA y_coor_lin11c(5,1),y_coor_lin11c(5,2) /-31.07,-2.32/
83          DATA y_coor_lin11c(6,1),y_coor_lin11c(6,2) /-21.69,-2.63/
84          DATA y_coor_lin11c(7,1),y_coor_lin11c(7,2) /-12.37,-2.65/
85          DATA y_coor_lin11c(8,1),y_coor_lin11c(8,2) /-10.81,-3.15/
86    
87          DATA x_coor_lin12c(1,1),x_coor_lin12c(1,2) /12.96, -2.65/
88          DATA x_coor_lin12c(2,1),x_coor_lin12c(2,2) /17.12,-2.44/
89          DATA x_coor_lin12c(3,1),x_coor_lin12c(3,2) /7.26, -1.98/
90          DATA x_coor_lin12c(4,1),x_coor_lin12c(4,2) /-22.52,-2.27/
91          DATA x_coor_lin12c(5,1),x_coor_lin12c(5,2) /-18.54,-2.28/
92          DATA x_coor_lin12c(6,1),x_coor_lin12c(6,2) /-7.67,-2.15/
93    
94          DATA x_coor_lin21c(1,1),x_coor_lin21c(1,2) /22.56,-1.56/
95          DATA x_coor_lin21c(2,1),x_coor_lin21c(2,2) /13.94,-1.56/
96    
97          DATA y_coor_lin22c(1,1),y_coor_lin22c(1,2) /-24.24,-2.23/
98          DATA y_coor_lin22c(2,1),y_coor_lin22c(2,2) /-45.99,-1.68/
99    
100          DATA y_coor_lin31c(1,1),y_coor_lin31c(1,2) /-22.99,-3.54/
101          DATA y_coor_lin31c(2,1),y_coor_lin31c(2,2) /-42.28,-4.10/
102          DATA y_coor_lin31c(3,1),y_coor_lin31c(3,2) /-41.29,-3.69/
103    
104          DATA x_coor_lin32c(1,1),x_coor_lin32c(1,2) /0.961, -3.22/
105          DATA x_coor_lin32c(2,1),x_coor_lin32c(2,2) /4.98,-3.48/
106          DATA x_coor_lin32c(3,1),x_coor_lin32c(3,2) /-22.08,-3.37/
107    
108                
109        REAL theta12,theta13,theta23        REAL theta13
110  C--   DATA ZTOF/53.74,53.04,23.94,23.44,-23.49,-24.34/ !Sergio 9.05.2006  
111          DOUBLE PRECISION ZTOF(6)
112          DATA ZTOF/53.74,53.04,23.94,23.44,-23.49,-24.34/ !Sergio 9.05.2006
113    
114        REAL tofarm12        REAL tofarm12
115        PARAMETER (tofarm12 = 29.70) ! from 53.39 to 23.69        PARAMETER (tofarm12 = 29.70) ! from 53.39 to 23.69
116        REAL tofarm23                REAL tofarm23        
117        PARAMETER (tofarm23 = 47.61)  ! from 23.69 to -23.92        PARAMETER (tofarm23 = 47.61)  ! from 23.69 to -23.92
118        REAL tofarm13        REAL tofarm13
119        PARAMETER (tofarm13 = 77.31)  ! from 53.39 to -23.92        PARAMETER (tofarm13 = 77.31)  ! from 53.39 to -23.92
120          
121          REAL hepratio      
122    
123        INTEGER ihelp        INTEGER ihelp
124        REAL xkorr        REAL xkorr,btemp(12)
125    
126          REAL atten,pc_adc,check_charge,newbeta
127    
128          INTEGER IZ
129    
130    
131          INTEGER ifst
132          DATA  ifst /0/
133    
134  C---------------------------------------  C---------------------------------------
135  C      C    
# Line 56  C     Line 139  C    
139  C      C    
140  C     CALCULATE COMMON VARIABLES  C     CALCULATE COMMON VARIABLES
141  C      C    
142    C-------------------------------------------------------------------
143    
144  *******************************************************************          if (ifst.eq.0) then
145        icounter = icounter + 1  
146            ifst=1
147    
148  *     amplitude has to be 'secure' higher than pedestal for an adc event  C     amplitude has to be 'secure' higher than pedestal for an adc event
149        secure = 2.         secure = 2.
150    
151    C     ratio between helium and proton ca. 4
152           hepratio = 4.  !
153           offset = 1
154           slope = 2
155           left = 1
156           right = 2
157           none_ev = 0
158           none_find = 0
159           tdc_ev = 1
160           adc_ev = 1
161           itdc = 1
162           iadc = 2
163    
164            ENDIF
165    C---------------------------------------------------------------------
166    
167          icounter = icounter + 1
168    
       offset = 1  
       slope = 2  
       left = 1  
       right = 2  
       none_ev = 0  
       none_find = 0  
       tdc_ev = 1  
       adc_ev = 1  
       itdc = 1  
       iadc = 2  
169    
170        do i=1,13        do i=1,13
171           betatof_a(i) = 100.          ! As in "troftrk.for"           betatof_a(i) = 100.          ! As in "troftrk.for"
172        enddo        enddo
173    
174          do i=1,6
175             hitvec(i) = -1
176          enddo
177    
178        do i=1,4        do i=1,4
179           do j=1,12           do j=1,12
180              adctof_c(i,j) = 1000.              adctof_c(i,j) = 1000.
# Line 92  C     Line 189  C    
189        enddo        enddo
190    
191    
192  c the calibration files are read in the main program from xxx_tofcalib.rz        do i=1,12
193             do j=1,4
194                tofmask(j,i) = 0
195             enddo
196          enddo
197    
198    
199    c gf adc falg:
200          do i=1,4
201             do j=1,12
202                adcflagtof(i,j) = 0
203             enddo
204          enddo
205    
206    c gf tdc falg:
207          do i=1,4
208             do j=1,12
209                tdcflagtof(i,j) = 0
210             enddo
211          enddo
212    
213    
214    C---  Fill xtr_tof and ytr_tof: positions from tracker at ToF layers
215    C---  since this is standalone ToF fill with default values
216          do j=1,6
217          xtr_tof(j) = 101.
218          ytr_tof(j) = 101.
219          enddo
220    
221    c the calibration files are read in the main program from xxx_tofcalib.rz
222    
223  c-------------------------get ToF data --------------------------------  c-------------------------get ToF data --------------------------------
224    
225  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
226    c     adc valueas are then pC
227    
228        do j=1,8        do j=1,8
229           tof11(1,j,2) = adc(ch11a(j),hb11a(j))           tof11(1,j,2) = pc_adc(adc(ch11a(j),hb11a(j)))
230           tof11(2,j,2) = adc(ch11b(j),hb11b(j))           tof11(2,j,2) = pc_adc(adc(ch11b(j),hb11b(j)))
231           tof11(1,j,1) = tdc(ch11a(j),hb11a(j))           tof11(1,j,1) = (tdc(ch11a(j),hb11a(j)))
232           tof11(2,j,1) = tdc(ch11b(j),hb11b(j))           tof11(2,j,1) = (tdc(ch11b(j),hb11b(j)))
233        enddo        enddo
234    
235    
236        do j=1,6        do j=1,6
237           tof12(1,j,2) = adc(ch12a(j),hb12a(j))           tof12(1,j,2) = pc_adc(adc(ch12a(j),hb12a(j)))
238           tof12(2,j,2) = adc(ch12b(j),hb12b(j))           tof12(2,j,2) = pc_adc(adc(ch12b(j),hb12b(j)))
239           tof12(1,j,1) = tdc(ch12a(j),hb12a(j))           tof12(1,j,1) = (tdc(ch12a(j),hb12a(j)))
240           tof12(2,j,1) = tdc(ch12b(j),hb12b(j))           tof12(2,j,1) = (tdc(ch12b(j),hb12b(j)))
241        enddo        enddo
242    
243        do j=1,2        do j=1,2
244           tof21(1,j,2) = adc(ch21a(j),hb21a(j))           tof21(1,j,2) = pc_adc(adc(ch21a(j),hb21a(j)))
245           tof21(2,j,2) = adc(ch21b(j),hb21b(j))           tof21(2,j,2) = pc_adc(adc(ch21b(j),hb21b(j)))
246           tof21(1,j,1) = tdc(ch21a(j),hb21a(j))           tof21(1,j,1) = (tdc(ch21a(j),hb21a(j)))
247           tof21(2,j,1) = tdc(ch21b(j),hb21b(j))           tof21(2,j,1) = (tdc(ch21b(j),hb21b(j)))
248        enddo        enddo
249    
250        do j=1,2        do j=1,2
251           tof22(1,j,2) = adc(ch22a(j),hb22a(j))           tof22(1,j,2) = pc_adc(adc(ch22a(j),hb22a(j)))
252           tof22(2,j,2) = adc(ch22b(j),hb22b(j))           tof22(2,j,2) = pc_adc(adc(ch22b(j),hb22b(j)))
253           tof22(1,j,1) = tdc(ch22a(j),hb22a(j))           tof22(1,j,1) = (tdc(ch22a(j),hb22a(j)))
254           tof22(2,j,1) = tdc(ch22b(j),hb22b(j))           tof22(2,j,1) = (tdc(ch22b(j),hb22b(j)))
255        enddo        enddo
256    
257        do j=1,3        do j=1,3
258           tof31(1,j,2) = adc(ch31a(j),hb31a(j))           tof31(1,j,2) = pc_adc(adc(ch31a(j),hb31a(j)))
259           tof31(2,j,2) = adc(ch31b(j),hb31b(j))           tof31(2,j,2) = pc_adc(adc(ch31b(j),hb31b(j)))
260           tof31(1,j,1) = tdc(ch31a(j),hb31a(j))           tof31(1,j,1) = (tdc(ch31a(j),hb31a(j)))
261           tof31(2,j,1) = tdc(ch31b(j),hb31b(j))           tof31(2,j,1) = (tdc(ch31b(j),hb31b(j)))
262        enddo        enddo
263    
264        do j=1,3        do j=1,3
265           tof32(1,j,2) = adc(ch32a(j),hb32a(j))           tof32(1,j,2) = pc_adc(adc(ch32a(j),hb32a(j)))
266           tof32(2,j,2) = adc(ch32b(j),hb32b(j))           tof32(2,j,2) = pc_adc(adc(ch32b(j),hb32b(j)))
267           tof32(1,j,1) = tdc(ch32a(j),hb32a(j))           tof32(1,j,1) = (tdc(ch32a(j),hb32a(j)))
268           tof32(2,j,1) = tdc(ch32b(j),hb32b(j))           tof32(2,j,1) = (tdc(ch32b(j),hb32b(j)))
269        enddo        enddo
270    
271  C----------------------------------------------------------------------  C----------------------------------------------------------------------
# Line 188  C--------------------------------------- Line 313  C---------------------------------------
313           if (abs(tof32(2,i,iadc)).gt.10000.) tof32(2,i,iadc)= 10000.           if (abs(tof32(2,i,iadc)).gt.10000.) tof32(2,i,iadc)= 10000.
314        ENDDO        ENDDO
315    
316    C----------------------------------------------------------------------
317    C------------------  set ADC & TDC flag = 0    ------------------------
318    C----------------------------------------------------------------------
319    
320          do j=1,8
321          if (adc(ch11a(j),hb11a(j)).LT.4096)adcflagtof(ch11a(j),hb11a(j))=0
322          if (adc(ch11b(j),hb11b(j)).LT.4096)adcflagtof(ch11b(j),hb11b(j))=0
323          if (tdc(ch11a(j),hb11a(j)).LT.4096)tdcflagtof(ch11a(j),hb11a(j))=0
324          if (tdc(ch11b(j),hb11b(j)).LT.4096)tdcflagtof(ch11b(j),hb11b(j))=0
325          enddo
326          do j=1,6
327          if (adc(ch12a(j),hb12a(j)).LT.4096)adcflagtof(ch12a(j),hb12a(j))=0
328          if (adc(ch12b(j),hb12b(j)).LT.4096)adcflagtof(ch12b(j),hb12b(j))=0
329          if (tdc(ch12a(j),hb12a(j)).LT.4096)tdcflagtof(ch12a(j),hb12a(j))=0
330          if (tdc(ch12b(j),hb12b(j)).LT.4096)tdcflagtof(ch12b(j),hb12b(j))=0
331          enddo
332          do j=1,2
333          if (adc(ch21a(j),hb21a(j)).LT.4096)adcflagtof(ch21a(j),hb21a(j))=0
334          if (adc(ch21b(j),hb21b(j)).LT.4096)adcflagtof(ch21b(j),hb21b(j))=0
335          if (tdc(ch21a(j),hb21a(j)).LT.4096)tdcflagtof(ch21a(j),hb21a(j))=0
336          if (tdc(ch21b(j),hb21b(j)).LT.4096)tdcflagtof(ch21b(j),hb21b(j))=0
337          enddo
338          do j=1,2
339          if (adc(ch22a(j),hb22a(j)).LT.4096)adcflagtof(ch22a(j),hb22a(j))=0
340          if (adc(ch22b(j),hb22b(j)).LT.4096)adcflagtof(ch22b(j),hb22b(j))=0
341          if (tdc(ch22a(j),hb22a(j)).LT.4096)tdcflagtof(ch22a(j),hb22a(j))=0
342          if (tdc(ch22b(j),hb22b(j)).LT.4096)tdcflagtof(ch22b(j),hb22b(j))=0
343          enddo
344          do j=1,3
345          if (adc(ch31a(j),hb31a(j)).LT.4096)adcflagtof(ch31a(j),hb31a(j))=0
346          if (adc(ch31b(j),hb31b(j)).LT.4096)adcflagtof(ch31b(j),hb31b(j))=0
347          if (tdc(ch31a(j),hb31a(j)).LT.4096)tdcflagtof(ch31a(j),hb31a(j))=0
348          if (tdc(ch31b(j),hb31b(j)).LT.4096)tdcflagtof(ch31b(j),hb31b(j))=0
349          enddo
350          do j=1,3
351          if (adc(ch32a(j),hb32a(j)).LT.4096)adcflagtof(ch32a(j),hb32a(j))=0
352          if (adc(ch32b(j),hb32b(j)).LT.4096)adcflagtof(ch32b(j),hb32b(j))=0
353          if (tdc(ch32a(j),hb32a(j)).LT.4096)tdcflagtof(ch32a(j),hb32a(j))=0
354          if (tdc(ch32b(j),hb32b(j)).LT.4096)tdcflagtof(ch32b(j),hb32b(j))=0
355          enddo
356    
357  C----------------------------------------------------------------  C----------------------------------------------------------------
358  C------------Check Paddles for hits  -----------------------  C---------- Check PMTs 10 and 35 for strange TDC values----------
359    C----------------------------------------------------------------
360    
361    C---- S116A TDC=819
362           if (tof11(1,6,1).EQ.819) then
363                 tof11(1,6,1) = 4095
364                 tdcflagtof(ch11a(6),hb11a(6))=2
365           endif
366      
367    C---- S222B TDC=819
368           if (tof22(2,2,1).EQ.819) then
369                 tof22(2,2,1) = 4095
370                 tdcflagtof(ch22b(2),hb22b(2))=2
371           endif
372    
373    C----------------------------------------------------------------
374    C------------   Check Paddles for hits    -----------------------
375    C------  a "hit" means TDC values<4095 on both sides ------------
376  C----------------------------------------------------------------  C----------------------------------------------------------------
377    
378  C     upper tof  S11  C     upper tof  S11
# Line 199  C     upper tof  S11 Line 382  C     upper tof  S11
382              tof11_event(j,i) = none_ev              tof11_event(j,i) = none_ev
383              IF ((tof11(j,i,itdc).LT.2000).AND.(tof11(j,i,itdc).GT.100))              IF ((tof11(j,i,itdc).LT.2000).AND.(tof11(j,i,itdc).GT.100))
384       +           tof11_event(j,i) = tof11_event(j,i) + tdc_ev       +           tof11_event(j,i) = tof11_event(j,i) + tdc_ev
             IF ((tof11(j,i,iadc).GT.secure).AND.  
      +           (tof11(j,i,iadc).LT.4095))  
      +           tof11_event(j,i) = tof11_event(j,i) + adc_ev  
385           ENDDO           ENDDO
386        ENDDO        ENDDO
387    
# Line 232  C     upper tof  S12 Line 412  C     upper tof  S12
412              tof12_event(j,i) = none_ev              tof12_event(j,i) = none_ev
413              IF ((tof12(j,i,itdc).LT.2000).AND.(tof12(j,i,itdc).GT.100))              IF ((tof12(j,i,itdc).LT.2000).AND.(tof12(j,i,itdc).GT.100))
414       +           tof12_event(j,i) = tof12_event(j,i) + tdc_ev       +           tof12_event(j,i) = tof12_event(j,i) + tdc_ev
             IF ((tof12(j,i,iadc).GT.secure).AND.  
      +           (tof12(j,i,iadc).LT.4095))  
      +           tof12_event(j,i) = tof12_event(j,i) + adc_ev  
415           ENDDO           ENDDO
416        ENDDO        ENDDO
417    
# Line 265  C     middle tof  S21 Line 442  C     middle tof  S21
442              tof21_event(j,i) = none_ev              tof21_event(j,i) = none_ev
443              IF ((tof21(j,i,itdc).LT.2000).AND.(tof21(j,i,itdc).GT.100))              IF ((tof21(j,i,itdc).LT.2000).AND.(tof21(j,i,itdc).GT.100))
444       +           tof21_event(j,i) = tof21_event(j,i) + tdc_ev       +           tof21_event(j,i) = tof21_event(j,i) + tdc_ev
             IF ((tof21(j,i,iadc).GT.secure).AND.  
      +           (tof21(j,i,iadc).LT.4095))  
      +           tof21_event(j,i) = tof21_event(j,i) + adc_ev  
445           ENDDO           ENDDO
446        ENDDO        ENDDO
447    
# Line 297  C     middle tof  S22 Line 471  C     middle tof  S22
471              tof22_event(j,i) = none_ev              tof22_event(j,i) = none_ev
472              IF ((tof22(j,i,itdc).LT.2000).AND.(tof22(j,i,itdc).GT.100))              IF ((tof22(j,i,itdc).LT.2000).AND.(tof22(j,i,itdc).GT.100))
473       +           tof22_event(j,i) = tof22_event(j,i) + tdc_ev       +           tof22_event(j,i) = tof22_event(j,i) + tdc_ev
             IF ((tof22(j,i,iadc).GT.secure).AND.  
      +           (tof22(j,i,iadc).LT.4095))  
      +           tof22_event(j,i) = tof22_event(j,i) + adc_ev  
474           ENDDO           ENDDO
475        ENDDO        ENDDO
476    
# Line 330  C     bottom tof  S31 Line 501  C     bottom tof  S31
501              tof31_event(j,i) = none_ev              tof31_event(j,i) = none_ev
502              IF ((tof31(j,i,itdc).LT.2000).AND.(tof31(j,i,itdc).GT.100))              IF ((tof31(j,i,itdc).LT.2000).AND.(tof31(j,i,itdc).GT.100))
503       +           tof31_event(j,i) = tof31_event(j,i) + tdc_ev       +           tof31_event(j,i) = tof31_event(j,i) + tdc_ev
             IF ((tof31(j,i,iadc).GT.secure).AND.  
      +           (tof31(j,i,iadc).LT.4095))  
      +           tof31_event(j,i) = tof31_event(j,i) + adc_ev  
504           ENDDO           ENDDO
505        ENDDO        ENDDO
506    
# Line 362  C     bottom tof  S32 Line 530  C     bottom tof  S32
530              tof32_event(j,i) = none_ev              tof32_event(j,i) = none_ev
531              IF ((tof32(j,i,itdc).LT.2000).AND.(tof32(j,i,itdc).GT.100))              IF ((tof32(j,i,itdc).LT.2000).AND.(tof32(j,i,itdc).GT.100))
532       +           tof32_event(j,i) = tof32_event(j,i) + tdc_ev       +           tof32_event(j,i) = tof32_event(j,i) + tdc_ev
             IF ((tof32(j,i,iadc).GT.secure).AND.  
      +           (tof32(j,i,iadc).LT.4095))  
      +           tof32_event(j,i) = tof32_event(j,i) + adc_ev  
533           ENDDO           ENDDO
534        ENDDO        ENDDO
535    
# Line 386  c     check if an other paddle has also Line 551  c     check if an other paddle has also
551                 ENDIF                 ENDIF
552              ENDIF              ENDIF
553           ENDIF           ENDIF
554        ENDDO         ENDDO
555    
556        do i=1,6         do i=1,6
557           tof_i_flag(i)=0           tof_i_flag(i)=0
558           tof_j_flag(i)=0           tof_j_flag(i)=0
559        enddo         enddo
560    
561        tof_i_flag(1)=tof11_i         tof_i_flag(1)=tof11_i
562        tof_i_flag(2)=tof12_i         tof_i_flag(2)=tof12_i
563        tof_i_flag(3)=tof21_i         tof_i_flag(3)=tof21_i
564        tof_i_flag(4)=tof22_i         tof_i_flag(4)=tof22_i
565        tof_i_flag(5)=tof31_i         tof_i_flag(5)=tof31_i
566        tof_i_flag(6)=tof32_i         tof_i_flag(6)=tof32_i
567    
568        tof_j_flag(1)=tof11_j         tof_j_flag(1)=tof11_j
569        tof_j_flag(2)=tof12_j         tof_j_flag(2)=tof12_j
570        tof_j_flag(3)=tof21_j         tof_j_flag(3)=tof21_j
571        tof_j_flag(4)=tof22_j         tof_j_flag(4)=tof22_j
572        tof_j_flag(5)=tof31_j         tof_j_flag(5)=tof31_j
573        tof_j_flag(6)=tof32_j         tof_j_flag(6)=tof32_j
574    
575           hitvec(1)=tof11_i
576  C--------------------------------------------------------------------         hitvec(2)=tof12_i
577  C--------------------Time walk correction  -------------------------         hitvec(3)=tof21_i
578  C--------------------------------------------------------------------         hitvec(4)=tof22_i
579           hitvec(5)=tof31_i
580        DO i=1,8         hitvec(6)=tof32_i
        xhelp_a = tof11(left,i,iadc)  
        xhelp_t = tof11(left,i,itdc)  
        if(xhelp_a>0) xhelp = tw11(left,i)/sqrt(xhelp_a)  
        tof11(left,i,itdc) = xhelp_t  + xhelp  
        tdc_c(ch11a(i),hb11a(i))=tof11(left,i,itdc)  
        xhelp_a = tof11(right,i,iadc)  
        xhelp_t = tof11(right,i,itdc)  
        if(xhelp_a>0) xhelp = tw11(right,i)/sqrt(xhelp_a)  
        tof11(right,i,itdc) = xhelp_t  + xhelp  
        tdc_c(ch11b(i),hb11b(i))=tof11(right,i,itdc)  
       ENDDO  
   
       DO i=1,6  
        xhelp_a = tof12(left,i,iadc)  
        xhelp_t = tof12(left,i,itdc)  
        if(xhelp_a>0) xhelp = tw12(left,i)/sqrt(xhelp_a)  
        tof12(left,i,itdc) = xhelp_t  + xhelp  
        tdc_c(ch12a(i),hb12a(i))=tof12(left,i,itdc)  
        xhelp_a = tof12(right,i,iadc)  
        xhelp_t = tof12(right,i,itdc)  
        if(xhelp_a>0) xhelp = tw12(right,i)/sqrt(xhelp_a)  
        tof12(right,i,itdc) = xhelp_t  + xhelp  
        tdc_c(ch12b(i),hb12b(i))=tof12(right,i,itdc)  
       ENDDO  
 C----  
       DO i=1,2  
        xhelp_a = tof21(left,i,iadc)  
        xhelp_t = tof21(left,i,itdc)  
        if(xhelp_a>0) xhelp = tw21(left,i)/sqrt(xhelp_a)  
        tof21(left,i,itdc) = xhelp_t  + xhelp  
        tdc_c(ch21a(i),hb21a(i))=tof21(left,i,itdc)  
        xhelp_a = tof21(right,i,iadc)  
        xhelp_t = tof21(right,i,itdc)  
        if(xhelp_a>0) xhelp = tw21(right,i)/sqrt(xhelp_a)  
        tof21(right,i,itdc) = xhelp_t  + xhelp  
        tdc_c(ch21b(i),hb21b(i))=tof21(right,i,itdc)  
       ENDDO  
   
       DO i=1,2  
        xhelp_a = tof22(left,i,iadc)  
        xhelp_t = tof22(left,i,itdc)  
        if(xhelp_a>0) xhelp = tw22(left,i)/sqrt(xhelp_a)  
        tof22(left,i,itdc) = xhelp_t  + xhelp  
        tdc_c(ch22a(i),hb22a(i))=tof22(left,i,itdc)  
        xhelp_a = tof22(right,i,iadc)  
        xhelp_t = tof22(right,i,itdc)  
        if(xhelp_a>0) xhelp = tw22(right,i)/sqrt(xhelp_a)  
        tof22(right,i,itdc) = xhelp_t  + xhelp  
        tdc_c(ch22b(i),hb22b(i))=tof22(right,i,itdc)  
       ENDDO  
 C----  
581    
       DO i=1,3  
        xhelp_a = tof31(left,i,iadc)  
        xhelp_t = tof31(left,i,itdc)  
        if(xhelp_a>0) xhelp = tw31(left,i)/sqrt(xhelp_a)  
        tof31(left,i,itdc) = xhelp_t  + xhelp  
        tdc_c(ch31a(i),hb31a(i))=tof31(left,i,itdc)  
        xhelp_a = tof31(right,i,iadc)  
        xhelp_t = tof31(right,i,itdc)  
        if(xhelp_a>0) xhelp = tw31(right,i)/sqrt(xhelp_a)  
        tof31(right,i,itdc) = xhelp_t  + xhelp  
        tdc_c(ch31b(i),hb31b(i))=tof31(right,i,itdc)  
       ENDDO  
   
       DO i=1,3  
        xhelp_a = tof32(left,i,iadc)  
        xhelp_t = tof32(left,i,itdc)  
        if(xhelp_a>0) xhelp = tw32(left,i)/sqrt(xhelp_a)  
        tof32(left,i,itdc) = xhelp_t  + xhelp  
        tdc_c(ch32a(i),hb32a(i))=tof32(left,i,itdc)  
        xhelp_a = tof32(right,i,iadc)  
        xhelp_t = tof32(right,i,itdc)  
        if(xhelp_a>0) xhelp = tw32(right,i)/sqrt(xhelp_a)  
        tof32(right,i,itdc) = xhelp_t  + xhelp  
        tdc_c(ch32b(i),hb32b(i))=tof32(right,i,itdc)  
       ENDDO  
 C----  
582    
583  C------------------------------------------------------------------  C------------------------------------------------------------------
584  C---  calculate track position in paddle using timing difference  C--  calculate track position in paddle using timing difference
585    C--  this calculation is preliminary and uses some standard
586    C--  calibration values, but we need to find a rough position to
587    C--  be able to calculate artificial ADC values (needed for the
588    C--  timewalk...
589  C------------------------------------------------------------------  C------------------------------------------------------------------
590    
591        do i=1,3         do i=1,3
592           xtofpos(i)=100.           xtofpos(i)=100.
593           ytofpos(i)=100.           ytofpos(i)=100.
594        enddo         enddo
595    
596  C-----------------------------S1 --------------------------------  C-----------------------------S1 --------------------------------
597    
598        IF (tof11_i.GT.none_find) THEN        IF (tof11_i.GT.none_find) THEN
599           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.
600       +        -y_coor_lin11(tof11_i,offset))/y_coor_lin11(tof11_i,slope)       +   -y_coor_lin11c(tof11_i,offset))/y_coor_lin11c(tof11_i,slope)
601        endif        endif
602    
603        IF (tof12_i.GT.none_find) THEN        IF (tof12_i.GT.none_find) THEN
604           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.
605       +        -x_coor_lin12(tof12_i,offset))/x_coor_lin12(tof12_i,slope)       +   -x_coor_lin12c(tof12_i,offset))/x_coor_lin12c(tof12_i,slope)
606        endif        endif
607    
608    
# Line 517  C-----------------------------S2 ------- Line 610  C-----------------------------S2 -------
610    
611        IF (tof21_i.GT.none_find) THEN        IF (tof21_i.GT.none_find) THEN
612           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.
613       +        -x_coor_lin21(tof21_i,offset))/x_coor_lin21(tof21_i,slope)       +    -x_coor_lin21c(tof21_i,offset))/x_coor_lin21c(tof21_i,slope)
614        endif        endif
615    
616        IF (tof22_i.GT.none_find) THEN        IF (tof22_i.GT.none_find) THEN
617           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.
618       +        -y_coor_lin22(tof22_i,offset))/y_coor_lin22(tof22_i,slope)       +    -y_coor_lin22c(tof22_i,offset))/y_coor_lin22c(tof22_i,slope)
619        endif        endif
620                
621    
# Line 530  C-----------------------------S3 ------- Line 623  C-----------------------------S3 -------
623    
624        IF (tof31_i.GT.none_find) THEN        IF (tof31_i.GT.none_find) THEN
625           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.
626       +        -y_coor_lin31(tof31_i,offset))/y_coor_lin31(tof31_i,slope)       +    -y_coor_lin31c(tof31_i,offset))/y_coor_lin31c(tof31_i,slope)
627        endif        endif
628    
629        IF (tof32_i.GT.none_find) THEN        IF (tof32_i.GT.none_find) THEN
630           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.
631       +        -x_coor_lin32(tof32_i,offset))/x_coor_lin32(tof32_i,slope)       +    -x_coor_lin32c(tof32_i,offset))/x_coor_lin32c(tof32_i,slope)
632        endif        endif
633    
634    
       do i=1,3  
          if (abs(xtofpos(i)).gt.100.) then  
             xtofpos(i)=101.  
          endif  
          if (abs(ytofpos(i)).gt.100.) then  
             ytofpos(i)=101.  
          endif  
       enddo  
   
635  C----------------------------------------------------------------------  C----------------------------------------------------------------------
636  C--------------------Corrections on ADC-data -------------------------  C---------------------  zenith angle theta  ---------------------------
 C---------------------zenith angle theta  ---------------------------  
637  C----------------------------------------------------------------------  C----------------------------------------------------------------------
638    
639        dx=0.         xhelp1=0.
640        dy=0.         if (tof11_i.GT.none_find) xhelp1=tof11_x(tof11_i)
641        dr=0.         if (xtofpos(1).lt.100)  xhelp1=xtofpos(1)
642        theta13 = 0.  
643           yhelp1=0.
644           IF ((tof12_i.GT.none_find).AND.(tof32_i.GT.none_find))         if (tof12_i.GT.none_find) yhelp1=tof12_y(tof12_i)
645       &        dx  = xtofpos(1) - xtofpos(3)         if (ytofpos(1).lt.100)  yhelp1=ytofpos(1)
646           IF ((tof11_i.GT.none_find).AND.(tof31_i.GT.none_find))  
647       &        dy  = ytofpos(1) - ytofpos(3)  
648           dr = sqrt(dx*dx+dy*dy)         yhelp2=0.
649           theta13 = atan(dr/tofarm13)         if (tof32_i.GT.none_find) yhelp2=tof32_y(tof32_i)
650           if (ytofpos(3).lt.100)  yhelp2=ytofpos(3)
651        dx=0.  
652        dy=0.         xhelp2=0.
653        dr=0.         if (tof31_i.GT.none_find) xhelp2=tof31_x(tof31_i)
654        theta12 = 0.         if (xtofpos(3).lt.100)  xhelp2=xtofpos(3)
655    
656           IF ((tof12_i.GT.none_find).AND.(tof21_i.GT.none_find))  
657       &        dx  = xtofpos(1) - xtofpos(2)         dx=0.
658           IF ((tof11_i.GT.none_find).AND.(tof22_i.GT.none_find))         dy=0.
659       &        dy  = ytofpos(1) - ytofpos(2)         dr=0.
660           dr = sqrt(dx*dx+dy*dy)         theta13 = 0.
661           theta12 = atan(dr/tofarm12)  
662                   dx  = xhelp1 - xhelp2
663        dx=0.         dy  = yhelp1 - yhelp2
664        dy=0.         dr = sqrt(dx*dx+dy*dy)
665        dr=0.         theta13 = atan(dr/tofarm13)
666        theta23 = 0.  
667    
          IF ((tof21_i.GT.none_find).AND.(tof32_i.GT.none_find))  
      &        dx  = xtofpos(2) - xtofpos(3)  
          IF ((tof22_i.GT.none_find).AND.(tof31_i.GT.none_find))  
      &        dy  = ytofpos(2) - ytofpos(3)  
          dr = sqrt(dx*dx+dy*dy)  
          theta23 = atan(dr/tofarm23)  
           
           
668  C----------------------------------------------------------------------  C----------------------------------------------------------------------
669  C------------------angle and ADC(x) correction  C--- check charge:
670    C--- if Z=2 we should use the attenuation curve for helium to
671    C--- fill the artificail ADC values and NOT divide by "hepratio"
672    C--- if Z>2 we should do a correction to
673    C--- the k1 constants in the beta calculation
674  C----------------------------------------------------------------------  C----------------------------------------------------------------------
675  C-----------------------------S1 --------------------------------  
676             iz = int(check_charge(theta13,hitvec))
677    C         write(*,*) 'charge in tofl2com',iz
678    
679    C--------------------------------------------------------------------
680    C---- if TDCleft.and.TDCright and NO ADC insert artificial ADC
681    C---- values
682    C--------------------------------------------------------------------
683  c middle y (or x) position of the upper and middle ToF-Paddle  c middle y (or x) position of the upper and middle ToF-Paddle
684  c       DATA tof11_x/ -17.85,-12.75,-7.65,-2.55,2.55,7.65,12.75,17.85/  c       DATA tof11_x/ -17.85,-12.75,-7.65,-2.55,2.55,7.65,12.75,17.85/
685  c       DATA tof12_y/ -13.75,-8.25,-2.75,2.75,8.25,13.75/  c       DATA tof12_y/ -13.75,-8.25,-2.75,2.75,8.25,13.75/
686  c       DATA tof21_y/ -3.75,3.75/  c       DATA tof21_y/  3.75,-3.75/     ! paddles in different order
687  c       DATA tof22_x/ -4.5,4.5/  c       DATA tof22_x/ -4.5,4.5/
688  c       DATA tof31_x/ -6.0,0.,6.0/  c       DATA tof31_x/ -6.0,0.,6.0/
689  c       DATA tof32_y/ -5.0,0.0,5.0/  c       DATA tof32_y/ -5.0,0.0,5.0/
690    
       yhelp=0.  
       if (tof12_i.GT.none_find) yhelp=tof12_y(tof12_i)  
       if (ytofpos(1).lt.100)  yhelp=ytofpos(1)  
691    
692        IF (tof11_i.GT.none_find.AND.abs(yhelp).lt.100) THEN  C----------------------------  S1 -------------------------------------
693    
694    c       yhelp=0.
695           yhelp=100.  ! WM
696           if (tof12_i.GT.none_find) yhelp=tof12_y(tof12_i)
697           if (ytofpos(1).lt.100)  yhelp=ytofpos(1)
698    
699           IF (tof11_i.GT.none_find.AND.abs(yhelp).lt.100) THEN
700           i = tof11_i           i = tof11_i
701           xdummy=tof11(left,i,iadc)           if (adc(ch11a(i),hb11a(i)).eq.4095) then
702           tof11(left,i,iadc) = tof11(left,i,iadc)*cos(theta13)              xkorr = atten(left,11,i,yhelp)
703           if (tof11(left,i,iadc).lt.1000) then              if (iz.le.1) xkorr=xkorr/hepratio
704              xkorr=adcx11(left,i,1)*exp(-yhelp/adcx11(left,i,2))              tof11(left,i,iadc)=xkorr/cos(theta13)
705              xkorr0=adcx11(left,i,1)              adcflagtof(ch11a(i),hb11a(i)) = 1
             adctof_c(ch11a(i),hb11a(i))=tof11(left,i,iadc)/xkorr  
706           endif           endif
707             if (adc(ch11b(i),hb11b(i)).eq.4095) then
708           tof11(right,i,iadc) = tof11(right,i,iadc)*cos(theta13)              xkorr = atten(right,11,i,yhelp)
709           if (tof11(right,i,iadc).lt.1000) then              if (iz.le.1) xkorr=xkorr/hepratio
710              xkorr=adcx11(right,i,1)*exp(yhelp/adcx11(right,i,2))              tof11(right,i,iadc)=xkorr/cos(theta13)
711              xkorr0=adcx11(right,i,1)              adcflagtof(ch11b(i),hb11b(i)) = 1
             adctof_c(ch11b(i),hb11b(i))=tof11(right,i,iadc)/xkorr  
712           endif           endif
713        ENDIF         ENDIF
   
       xhelp=0.  
       if (tof11_i.GT.none_find) xhelp=tof11_x(tof11_i)  
       if (xtofpos(1).lt.100)  xhelp=xtofpos(1)  
714    
715        IF (tof12_i.GT.none_find.AND.abs(xhelp).lt.100) THEN  c       xhelp=0.
716           xhelp=100.  ! WM
717           if (tof11_i.GT.none_find) xhelp=tof11_x(tof11_i)
718           if (xtofpos(1).lt.100)  xhelp=xtofpos(1)
719    
720           IF (tof12_i.GT.none_find.AND.abs(xhelp).lt.100) THEN
721           i = tof12_i           i = tof12_i
722           tof12(left,i,iadc) = tof12(left,i,iadc)*cos(theta13)           if (adc(ch12a(i),hb12a(i)).eq.4095) then
723           if (tof12(left,i,iadc).lt.1000) then              xkorr = atten(left,12,i,xhelp)
724              xkorr=adcx12(left,i,1)*exp(-xhelp/adcx12(left,i,2))              if (iz.le.1) xkorr=xkorr/hepratio
725              xkorr0=adcx12(left,i,1)              tof12(left,i,iadc) = xkorr/cos(theta13)
726              adctof_c(ch12a(i),hb12a(i))=tof12(left,i,iadc)/xkorr              adcflagtof(ch12a(i),hb12a(i)) = 1
727           endif           endif
728             if (adc(ch12b(i),hb12b(i)).eq.4095) then
729           tof12(right,i,iadc) = tof12(right,i,iadc)*cos(theta13)              xkorr = atten(right,12,i,xhelp)
730           if (tof12(right,i,iadc).lt.1000) then              if (iz.le.1) xkorr=xkorr/hepratio
731              xkorr=adcx12(right,i,1)*exp(xhelp/adcx12(right,i,2))              tof12(right,i,iadc) = xkorr/cos(theta13)
732              xkorr0=adcx12(right,i,1)              adcflagtof(ch12b(i),hb12b(i)) = 1
             adctof_c(ch12b(i),hb12b(i))=tof12(right,i,iadc)/xkorr  
733           endif           endif
734        ENDIF         ENDIF
735    
736  C-----------------------------S2 --------------------------------  C-----------------------------S2 --------------------------------
737    
738        xhelp=0.  c       xhelp=0.
739        if (tof22_i.GT.none_find) xhelp=tof22_x(tof22_i)         xhelp=100.   ! WM
740        if (xtofpos(2).lt.100)  xhelp=xtofpos(2)         if (tof22_i.GT.none_find) xhelp=tof22_x(tof22_i)
741           if (xtofpos(2).lt.100)  xhelp=xtofpos(2)
       IF (tof21_i.GT.none_find.AND.abs(xhelp).lt.100) THEN  
742    
743           IF (tof21_i.GT.none_find.AND.abs(xhelp).lt.100) THEN
744           i = tof21_i           i = tof21_i
745           tof21(left,i,iadc) = tof21(left,i,iadc)*cos(theta13)           if (adc(ch21a(i),hb21a(i)).eq.4095) then
746           if (tof21(left,i,iadc).lt.1000) then              xkorr = atten(left,21,i,xhelp)
747              xkorr=adcx21(left,i,1)*exp(-xhelp/adcx21(left,i,2))              if (iz.le.1) xkorr=xkorr/hepratio
748              xkorr0=adcx21(left,i,1)              tof21(left,i,iadc) = xkorr/cos(theta13)
749              adctof_c(ch21a(i),hb21a(i))=tof21(left,i,iadc)/xkorr              adcflagtof(ch21a(i),hb21a(i)) = 1
750           endif           endif
751             if (adc(ch21b(i),hb21b(i)).eq.4095) then
752           tof21(right,i,iadc) = tof21(right,i,iadc)*cos(theta13)              xkorr = atten(right,21,i,xhelp)
753           if (tof21(right,i,iadc).lt.1000) then              if (iz.le.1) xkorr=xkorr/hepratio
754              xkorr=adcx21(right,i,1)*exp(xhelp/adcx21(right,i,2))              tof21(right,i,iadc) = xkorr/cos(theta13)
755              xkorr0=adcx21(right,i,1)              adcflagtof(ch21b(i),hb21b(i)) = 1
             adctof_c(ch21b(i),hb21b(i))=tof21(right,i,iadc)/xkorr  
756           endif           endif
757        ENDIF         ENDIF
   
758    
       yhelp=0.  
       if (tof21_i.GT.none_find) yhelp=tof21_y(tof21_i)  
       if (ytofpos(2).lt.100)  yhelp=ytofpos(2)  
759    
760        IF (tof22_i.GT.none_find.AND.abs(yhelp).lt.100) THEN  c       yhelp=0.
761           yhelp=100.   ! WM
762           if (tof21_i.GT.none_find) yhelp=tof21_y(tof21_i)
763           if (ytofpos(2).lt.100)  yhelp=ytofpos(2)
764    
765           IF (tof22_i.GT.none_find.AND.abs(yhelp).lt.100) THEN
766           i = tof22_i           i = tof22_i
767           tof22(left,i,iadc) = tof22(left,i,iadc)*cos(theta13)           if (adc(ch22a(i),hb22a(i)).eq.4095) then
768           if (tof22(left,i,iadc).lt.1000) then              xkorr = atten(left,22,i,yhelp)
769              xkorr=adcx22(left,i,1)*exp(-yhelp/adcx22(left,i,2))              if (iz.le.1) xkorr=xkorr/hepratio
770              xkorr0=adcx22(left,i,1)              tof22(left,i,iadc) = xkorr/cos(theta13)
771              adctof_c(ch22a(i),hb22a(i))=tof22(left,i,iadc)/xkorr              adcflagtof(ch22a(i),hb22a(i)) = 1
772           endif           endif
773             if (adc(ch22b(i),hb22b(i)).eq.4095) then
774           tof22(right,i,iadc) = tof22(right,i,iadc)*cos(theta13)              xkorr = atten(right,22,i,yhelp)
775           if (tof22(right,i,iadc).lt.1000) then              if (iz.le.1) xkorr=xkorr/hepratio
776              xkorr=adcx22(right,i,1)*exp(yhelp/adcx22(right,i,2))              tof22(right,i,iadc) = xkorr/cos(theta13)
777              xkorr0=adcx22(right,i,1)              adcflagtof(ch22b(i),hb22b(i)) = 1
             adctof_c(ch22b(i),hb22b(i))=tof22(right,i,iadc)/xkorr  
778           endif           endif
779        ENDIF         ENDIF
780    
781  C-----------------------------S3 --------------------------------  C-----------------------------S3 --------------------------------
782    
783        yhelp=0.  c       yhelp=0.
784        if (tof32_i.GT.none_find) yhelp=tof32_y(tof32_i)         yhelp=100.  ! WM
785        if (ytofpos(3).lt.100)  yhelp=ytofpos(3)         if (tof32_i.GT.none_find) yhelp=tof32_y(tof32_i)
786           if (ytofpos(3).lt.100)  yhelp=ytofpos(3)
       IF (tof31_i.GT.none_find.AND.abs(yhelp).lt.100) THEN  
787    
788           IF (tof31_i.GT.none_find.AND.abs(yhelp).lt.100) THEN
789           i = tof31_i           i = tof31_i
790           tof31(left,i,iadc) = tof31(left,i,iadc)*cos(theta13)           if (adc(ch31a(i),hb31a(i)).eq.4095) then
791           if (tof31(left,i,iadc).lt.1000) then              xkorr = atten(left,31,i,yhelp)
792              xkorr=adcx31(left,i,1)*exp(-yhelp/adcx31(left,i,2))              if (iz.le.1) xkorr=xkorr/hepratio
793              xkorr0=adcx31(left,i,1)              tof31(left,i,iadc) = xkorr/cos(theta13)
794              adctof_c(ch31a(i),hb31a(i))=tof31(left,i,iadc)/xkorr              adcflagtof(ch31a(i),hb31a(i)) = 1
795           endif           endif
796             if (adc(ch31b(i),hb31b(i)).eq.4095) then
797           tof31(right,i,iadc) = tof31(right,i,iadc)*cos(theta13)              xkorr = atten(right,31,i,yhelp)
798           if (tof31(right,i,iadc).lt.1000) then              if (iz.le.1) xkorr=xkorr/hepratio
799              xkorr=adcx31(right,i,1)*exp(yhelp/adcx31(right,i,2))              tof31(right,i,iadc) = xkorr/cos(theta13)
800              xkorr0=adcx31(right,i,1)              adcflagtof(ch31b(i),hb31b(i)) = 1
             adctof_c(ch31b(i),hb31b(i))=tof31(right,i,iadc)/xkorr  
801           endif           endif
802        ENDIF         ENDIF
   
       xhelp=0.  
       if (tof31_i.GT.none_find) xhelp=tof31_x(tof31_i)  
       if (xtofpos(3).lt.100)  xhelp=xtofpos(3)  
803    
804        IF (tof32_i.GT.none_find.AND.abs(xhelp).lt.100) THEN  c       xhelp=0.
805           xhelp=100.   ! WM
806           if (tof31_i.GT.none_find) xhelp=tof31_x(tof31_i)
807           if (xtofpos(3).lt.100)  xhelp=xtofpos(3)
808    
809           IF (tof32_i.GT.none_find.AND.abs(xhelp).lt.100) THEN
810           i = tof32_i           i = tof32_i
811           tof32(left,i,iadc) = tof32(left,i,iadc)*cos(theta13)           if (adc(ch32a(i),hb32a(i)).eq.4095) then
812           if (tof32(left,i,iadc).lt.1000) then              xkorr = atten(left,32,i,xhelp)
813              xkorr=adcx32(left,i,1)*exp(-xhelp/adcx32(left,i,2))              if (iz.le.1) xkorr=xkorr/hepratio
814              xkorr0=adcx32(left,i,1)              tof32(left,i,iadc) = xkorr/cos(theta13)
815              adctof_c(ch32a(i),hb32a(i))=tof32(left,i,iadc)/xkorr              adcflagtof(ch32a(i),hb32a(i)) = 1
816           endif           endif
817             if (adc(ch32b(i),hb32b(i)).eq.4095) then
818           tof32(right,i,iadc) = tof32(right,i,iadc)*cos(theta13)              xkorr = atten(right,32,i,xhelp)
819           if (tof32(right,i,iadc).lt.1000) then              if (iz.le.1) xkorr=xkorr/hepratio
820              xkorr=adcx32(right,i,1)*exp(xhelp/adcx32(right,i,2))              tof32(right,i,iadc) = xkorr/cos(theta13)
821              xkorr0=adcx32(right,i,1)              adcflagtof(ch32b(i),hb32b(i)) = 1
             adctof_c(ch32b(i),hb32b(i))=tof32(right,i,iadc)/xkorr  
822           endif           endif
823        ENDIF         ENDIF
824    
825    
826    C-------------------------------------------------------------------
827    C--------------------Time walk correction  -------------------------
828    C-------------------------------------------------------------------
829    C-------------------------------------------------------------------
830    C Now there is for each hitted paddle a TDC and ADC value, if the
831    C TDC was < 4095.
832    C There might be also TDC-ADC pairs in paddles not hitted
833    
834    C-------------------------------------------------------------------
835    C If we have multiple paddles hit, so that no artificial ADC value
836    C is created, we set the raw TDC value as "tdc_c"
837    C-------------------------------------------------------------------
838    c
839    c       do i=1,4
840    c         do j=1,12
841    c            tdc_c(i,j) = tdc(i,j)
842    c         enddo
843    c       enddo
844    c
845    C----  Let's correct the raw TDC value with the time walk  ---------
846    
847          DO i=1,8
848             if ((tdc(ch11a(i),hb11a(i)).lt.4095).and.
849         &             (tof11(left,i,iadc).lt.3786)) THEN
850             xhelp = tw11(left,i)/(tof11(left,i,iadc)**0.5)
851             tof11(left,i,itdc) = tof11(left,i,itdc) + xhelp
852             tdc_c(ch11a(i),hb11a(i))=tof11(left,i,itdc)
853                                                  ENDIF
854    
855             if ((tdc(ch11b(i),hb11b(i)).lt.4095).and.
856         &             (tof11(right,i,iadc).lt.3786)) THEN
857             xhelp = tw11(right,i)/(tof11(right,i,iadc)**0.5)
858             tof11(right,i,itdc) = tof11(right,i,itdc) + xhelp
859             tdc_c(ch11b(i),hb11b(i))=tof11(right,i,itdc)
860                                                 ENDIF
861          ENDDO
862    
863    
864          DO i=1,6
865             if ((tdc(ch12a(i),hb12a(i)).lt.4095).and.
866         &             (tof12(left,i,iadc).lt.3786)) THEN
867             xhelp = tw12(left,i)/(tof12(left,i,iadc)**0.5)
868             tof12(left,i,itdc) = tof12(left,i,itdc) + xhelp
869             tdc_c(ch12a(i),hb12a(i))=tof12(left,i,itdc)
870                                                  ENDIF
871    
872             if ((tdc(ch12b(i),hb12b(i)).lt.4095).and.
873         &             (tof12(right,i,iadc).lt.3786)) THEN
874             xhelp = tw12(right,i)/(tof12(right,i,iadc)**0.5)
875             tof12(right,i,itdc) = tof12(right,i,itdc) + xhelp
876             tdc_c(ch12b(i),hb12b(i))=tof12(right,i,itdc)
877                                                 ENDIF
878          ENDDO
879    
880    C----
881          DO I=1,2
882             if ((tdc(ch21a(i),hb21a(i)).lt.4095).and.
883         &             (tof21(left,i,iadc).lt.3786)) THEN
884             xhelp = tw21(left,i)/(tof21(left,i,iadc)**0.5)
885             tof21(left,i,itdc) = tof21(left,i,itdc) + xhelp
886             tdc_c(ch21a(i),hb21a(i))=tof21(left,i,itdc)
887                                                  ENDIF
888    
889             if ((tdc(ch21b(i),hb21b(i)).lt.4095).and.
890         &             (tof21(right,i,iadc).lt.3786)) THEN
891             xhelp = tw21(right,i)/(tof21(right,i,iadc)**0.5)
892             tof21(right,i,itdc) = tof21(right,i,itdc) + xhelp
893             tdc_c(ch21b(i),hb21b(i))=tof21(right,i,itdc)
894                                                 ENDIF
895          ENDDO
896    
897          DO I=1,2
898             if ((tdc(ch22a(i),hb22a(i)).lt.4095).and.
899         &             (tof22(left,i,iadc).lt.3786)) THEN
900             xhelp = tw22(left,i)/(tof22(left,i,iadc)**0.5)
901             tof22(left,i,itdc) = tof22(left,i,itdc) + xhelp
902             tdc_c(ch22a(i),hb22a(i))=tof22(left,i,itdc)
903                                                  ENDIF
904    
905             if ((tdc(ch22b(i),hb22b(i)).lt.4095).and.
906         &             (tof22(right,i,iadc).lt.3786)) THEN
907             xhelp = tw22(right,i)/(tof22(right,i,iadc)**0.5)
908             tof22(right,i,itdc) = tof22(right,i,itdc) + xhelp
909             tdc_c(ch22b(i),hb22b(i))=tof22(right,i,itdc)
910                                                 ENDIF
911          ENDDO
912    
913    C----
914          DO I=1,3
915             if ((tdc(ch31a(i),hb31a(i)).lt.4095).and.
916         &             (tof31(left,i,iadc).lt.3786)) THEN
917             xhelp = tw31(left,i)/(tof31(left,i,iadc)**0.5)
918             tof31(left,i,itdc) = tof31(left,i,itdc) + xhelp
919             tdc_c(ch31a(i),hb31a(i))=tof31(left,i,itdc)
920                                                  ENDIF
921    
922             if ((tdc(ch31b(i),hb31b(i)).lt.4095).and.
923         &             (tof31(right,i,iadc).lt.3786)) THEN
924             xhelp = tw31(right,i)/(tof31(right,i,iadc)**0.5)
925             tof31(right,i,itdc) = tof31(right,i,itdc) + xhelp
926             tdc_c(ch31b(i),hb31b(i))=tof31(right,i,itdc)
927                                                 ENDIF
928          ENDDO
929    
930          DO I=1,3
931             if ((tdc(ch32a(i),hb32a(i)).lt.4095).and.
932         &             (tof32(left,i,iadc).lt.3786)) THEN
933             xhelp = tw32(left,i)/(tof32(left,i,iadc)**0.5)
934             tof32(left,i,itdc) = tof32(left,i,itdc) + xhelp
935             tdc_c(ch32a(i),hb32a(i))=tof32(left,i,itdc)
936                                                  ENDIF
937    
938             if ((tdc(ch32b(i),hb32b(i)).lt.4095).and.
939         &             (tof32(right,i,iadc).lt.3786)) THEN
940             xhelp = tw32(right,i)/(tof32(right,i,iadc)**0.5)
941             tof32(right,i,itdc) = tof32(right,i,itdc) + xhelp
942             tdc_c(ch32b(i),hb32b(i))=tof32(right,i,itdc)
943                                                 ENDIF
944          ENDDO
945          
946    
947    C---------------------------------------------------------------
948    C--- calculate track position in paddle using timing difference
949    C--- now using the time-walk corrected TDC values
950    C---------------------------------------------------------------
951    
952          do i=1,3
953             xtofpos(i)=100.
954             ytofpos(i)=100.
955          enddo
956    
957    C-----------------------------S1 --------------------------------
958    
959          IF (tof11_i.GT.none_find) THEN
960             ytofpos(1)  = ((tof11(1,tof11_i,itdc)-tof11(2,tof11_i,itdc))/2.
961         +        -y_coor_lin11(tof11_i,offset))/y_coor_lin11(tof11_i,slope)
962           i=tof11_i
963          endif
964    
965          IF (tof12_i.GT.none_find) THEN
966             xtofpos(1)  = ((tof12(1,tof12_i,itdc)-tof12(2,tof12_i,itdc))/2.
967         +        -x_coor_lin12(tof12_i,offset))/x_coor_lin12(tof12_i,slope)
968          i=tof12_i
969          endif
970    
971    
972    C-----------------------------S2 --------------------------------
973    
974          IF (tof21_i.GT.none_find) THEN
975             xtofpos(2) = ((tof21(1,tof21_i,itdc)-tof21(2,tof21_i,itdc))/2.
976         +        -x_coor_lin21(tof21_i,offset))/x_coor_lin21(tof21_i,slope)
977          i=tof21_i
978          endif
979    
980          IF (tof22_i.GT.none_find) THEN
981             ytofpos(2) = ((tof22(1,tof22_i,itdc)-tof22(2,tof22_i,itdc))/2.
982         +        -y_coor_lin22(tof22_i,offset))/y_coor_lin22(tof22_i,slope)
983          i=tof22_i
984          endif
985          
986    
987    C-----------------------------S3 --------------------------------
988    
989          IF (tof31_i.GT.none_find) THEN
990             ytofpos(3)  = ((tof31(1,tof31_i,itdc)-tof31(2,tof31_i,itdc))/2.
991         +        -y_coor_lin31(tof31_i,offset))/y_coor_lin31(tof31_i,slope)
992          i=tof31_i
993          endif
994    
995  C-----------------------------------------------------------------------        IF (tof32_i.GT.none_find) THEN
996             xtofpos(3)  = ((tof32(1,tof32_i,itdc)-tof32(2,tof32_i,itdc))/2.
997         +        -x_coor_lin32(tof32_i,offset))/x_coor_lin32(tof32_i,slope)
998          i=tof32_i
999          endif
1000    
1001    
1002    c      do i=1,3
1003    c         if (abs(xtofpos(i)).gt.100.) then
1004    c            xtofpos(i)=101.
1005    c         endif
1006    c         if (abs(ytofpos(i)).gt.100.) then
1007    c            ytofpos(i)=101.
1008    c         endif
1009    c      enddo
1010    
1011    
1012    C--  restrict TDC measurements to physical paddle dimensions +/- 10 cm
1013    C--  this cut is now stronger than in the old versions
1014    
1015            if (abs(xtofpos(1)).gt.31.)  xtofpos(1)=101.
1016            if (abs(xtofpos(2)).gt.19.)  xtofpos(2)=101.
1017            if (abs(xtofpos(3)).gt.19.)  xtofpos(3)=101.
1018    
1019            if (abs(ytofpos(1)).gt.26.)  ytofpos(1)=101.
1020            if (abs(ytofpos(2)).gt.18.)  ytofpos(2)=101.
1021            if (abs(ytofpos(3)).gt.18.)  ytofpos(3)=101.
1022    
1023    C----------------------------------------------------------------------
1024    C---------------------  zenith angle theta  ---------------------------
1025    C----------------------------------------------------------------------
1026    C-------------------  improved calculation  ---------------------------
1027    
1028           xhelp1=0.
1029           if (tof11_i.GT.none_find) xhelp1=tof11_x(tof11_i)
1030           if (xtofpos(1).lt.100)  xhelp1=xtofpos(1)
1031    
1032           yhelp1=0.
1033           if (tof12_i.GT.none_find) yhelp1=tof12_y(tof12_i)
1034           if (ytofpos(1).lt.100)  yhelp1=ytofpos(1)
1035    
1036           yhelp2=0.
1037           if (tof32_i.GT.none_find) yhelp2=tof32_y(tof32_i)
1038           if (ytofpos(3).lt.100)  yhelp2=ytofpos(3)
1039    
1040           xhelp2=0.
1041           if (tof31_i.GT.none_find) xhelp2=tof31_x(tof31_i)
1042           if (xtofpos(3).lt.100)  xhelp2=xtofpos(3)
1043    
1044    
1045           dx=0.
1046           dy=0.
1047           dr=0.
1048           theta13 = 0.
1049    
1050           dx  = xhelp1 - xhelp2
1051           dy  = yhelp1 - yhelp2
1052           dr = sqrt(dx*dx+dy*dy)
1053           theta13 = atan(dr/tofarm13)
1054    
1055    
1056    C------------------------------------------------------------------
1057    C------------------------------------------------------------------
1058    C-------angle and ADC(x) correction: moved to new dEdx routine
1059    C------------------------------------------------------------------
1060    C------------------------------------------------------------------
1061    
1062    C--------------------------------------------------------------------
1063  C----------------------calculate Beta  ------------------------------  C----------------------calculate Beta  ------------------------------
1064  C-----------------------------------------------------------------------  C--------------------------------------------------------------------
1065  C-------------------difference of sums  ---------------------------  C-------------------difference of sums  -----------------------------
1066  C  C
1067  C     DS = (t1+t2) - t3+t4)  C     DS = (t1+t2) - t3+t4)
1068  C     DS = c1 + c2/beta*cos(theta)  C     DS = c1 + c2/beta*cos(theta)
# Line 752  C     =>  c2 =  ca.60 for  0.45 m    c2 Line 1071  C     =>  c2 =  ca.60 for  0.45 m    c2
1071  C     since TDC resolution varies slightly c2 has to be calibrated  C     since TDC resolution varies slightly c2 has to be calibrated
1072    
1073  C     S11 - S31  C     S11 - S31
1074        IF (tof11_i.GT.none_find.AND.tof31_i.GT.none_find) THEN  
1075          dist = ZTOF(1) - ZTOF(5)
1076          c2 = (2.*0.01*dist)/(3.E08*50.E-12  )
1077    
1078           IF ((tof11_i.GT.none_find).AND.(tof31_i.GT.none_find).AND.
1079         &    (ytofpos(1).NE.101.).AND.(ytofpos(3).NE.101.)) THEN
1080           xhelp1 = tof11(1,tof11_i,itdc)+tof11(2,tof11_i,itdc)           xhelp1 = tof11(1,tof11_i,itdc)+tof11(2,tof11_i,itdc)
1081           xhelp2 = tof31(1,tof31_i,itdc)+tof31(2,tof31_i,itdc)           xhelp2 = tof31(1,tof31_i,itdc)+tof31(2,tof31_i,itdc)
1082           ds = xhelp1-xhelp2           ds = xhelp1-xhelp2
1083           ihelp=(tof11_i-1)*3+tof31_i           ihelp=(tof11_i-1)*3+tof31_i
1084           c1 = k_S11S31(1,ihelp)           if (iz.le.1) c1 = k_S11S31(1,ihelp)
1085           c2 = k_S11S31(2,ihelp)           if (iz.eq.2) c1 = k_S11S31(2,ihelp)
1086             if (iz.gt.2) c1 = k_S11S31(3,ihelp)
1087           betatof_a(1) = c2/(cos(theta13)*(ds-c1))           betatof_a(1) = c2/(cos(theta13)*(ds-c1))
1088    
1089    C------- ToF Mask - S11 - S31
1090    
1091             tofmask(ch11a(tof11_i),hb11a(tof11_i)) =
1092         $        tofmask(ch11a(tof11_i),hb11a(tof11_i)) + 1
1093             tofmask(ch11b(tof11_i),hb11b(tof11_i)) =
1094         $        tofmask(ch11b(tof11_i),hb11b(tof11_i)) + 1
1095    
1096             tofmask(ch31a(tof31_i),hb31a(tof31_i)) =
1097         $        tofmask(ch31a(tof31_i),hb31a(tof31_i)) + 1
1098             tofmask(ch31b(tof31_i),hb31b(tof31_i)) =
1099         $        tofmask(ch31b(tof31_i),hb31b(tof31_i)) + 1
1100    
1101    C-------
1102          
1103        ENDIF        ENDIF
1104                
1105  C     S11 - S32  C     S11 - S32
1106        IF (tof11_i.GT.none_find.AND.tof32_i.GT.none_find) THEN  
1107          dist = ZTOF(1) - ZTOF(6)
1108          c2 = (2.*0.01*dist)/(3.E08*50.E-12  )
1109    
1110           IF ((tof11_i.GT.none_find).AND.(tof32_i.GT.none_find).AND.
1111         &    (ytofpos(1).NE.101.).AND.(xtofpos(3).NE.101.)) THEN
1112           xhelp1 = tof11(1,tof11_i,itdc)+tof11(2,tof11_i,itdc)           xhelp1 = tof11(1,tof11_i,itdc)+tof11(2,tof11_i,itdc)
1113           xhelp2 = tof32(1,tof32_i,itdc)+tof32(2,tof32_i,itdc)           xhelp2 = tof32(1,tof32_i,itdc)+tof32(2,tof32_i,itdc)
1114           ds = xhelp1-xhelp2           ds = xhelp1-xhelp2
1115           ihelp=(tof11_i-1)*3+tof32_i           ihelp=(tof11_i-1)*3+tof32_i
1116           c1 = k_S11S32(1,ihelp)           if (iz.le.1) c1 = k_S11S32(1,ihelp)
1117           c2 = k_S11S32(2,ihelp)           if (iz.eq.2) c1 = k_S11S32(2,ihelp)
1118             if (iz.gt.2) c1 = k_S11S32(3,ihelp)
1119           betatof_a(2) = c2/(cos(theta13)*(ds-c1))           betatof_a(2) = c2/(cos(theta13)*(ds-c1))
       ENDIF  
1120    
1121    C------- ToF Mask - S11 - S32
1122    
1123             tofmask(ch11a(tof11_i),hb11a(tof11_i)) =
1124         $        tofmask(ch11a(tof11_i),hb11a(tof11_i)) + 1
1125             tofmask(ch11b(tof11_i),hb11b(tof11_i)) =
1126         $        tofmask(ch11b(tof11_i),hb11b(tof11_i)) + 1
1127    
1128             tofmask(ch32a(tof32_i),hb32a(tof32_i)) =
1129         $        tofmask(ch32a(tof32_i),hb32a(tof32_i)) + 1
1130             tofmask(ch32b(tof32_i),hb32b(tof32_i)) =
1131         $        tofmask(ch32b(tof32_i),hb32b(tof32_i)) + 1
1132    
1133    C-------
1134    
1135          ENDIF
1136          
1137  C     S12 - S31  C     S12 - S31
1138        IF (tof12_i.GT.none_find.AND.tof31_i.GT.none_find) THEN  
1139          dist = ZTOF(2) - ZTOF(5)
1140          c2 = (2.*0.01*dist)/(3.E08*50.E-12  )
1141    
1142           IF ((tof12_i.GT.none_find).AND.(tof31_i.GT.none_find).AND.
1143         &    (xtofpos(1).NE.101.).AND.(ytofpos(3).NE.101.)) THEN
1144           xhelp1 = tof12(1,tof12_i,itdc)+tof12(2,tof12_i,itdc)           xhelp1 = tof12(1,tof12_i,itdc)+tof12(2,tof12_i,itdc)
1145           xhelp2 = tof31(1,tof31_i,itdc)+tof31(2,tof31_i,itdc)           xhelp2 = tof31(1,tof31_i,itdc)+tof31(2,tof31_i,itdc)
1146           ds = xhelp1-xhelp2           ds = xhelp1-xhelp2
1147           ihelp=(tof12_i-1)*3+tof31_i           ihelp=(tof12_i-1)*3+tof31_i
1148           c1 = k_S12S31(1,ihelp)           if (iz.le.1) c1 = k_S12S31(1,ihelp)
1149           c2 = k_S12S31(2,ihelp)           if (iz.eq.2) c1 = k_S12S31(2,ihelp)
1150             if (iz.gt.2) c1 = k_S12S31(3,ihelp)
1151           betatof_a(3) = c2/(cos(theta13)*(ds-c1))           betatof_a(3) = c2/(cos(theta13)*(ds-c1))
       ENDIF  
1152    
1153    C------- ToF Mask - S12 - S31
1154    
1155             tofmask(ch12a(tof12_i),hb12a(tof12_i)) =
1156         $        tofmask(ch12a(tof12_i),hb12a(tof12_i)) + 1
1157             tofmask(ch12b(tof12_i),hb12b(tof12_i)) =
1158         $        tofmask(ch12b(tof12_i),hb12b(tof12_i)) + 1
1159    
1160             tofmask(ch31a(tof31_i),hb31a(tof31_i)) =
1161         $        tofmask(ch31a(tof31_i),hb31a(tof31_i)) + 1
1162             tofmask(ch31b(tof31_i),hb31b(tof31_i)) =
1163         $        tofmask(ch31b(tof31_i),hb31b(tof31_i)) + 1
1164    
1165    C-------
1166    
1167          ENDIF
1168                
1169  C     S12 - S32  C     S12 - S32
1170        IF (tof12_i.GT.none_find.AND.tof32_i.GT.none_find) THEN  
1171          dist = ZTOF(2) - ZTOF(6)
1172          c2 = (2.*0.01*dist)/(3.E08*50.E-12  )
1173    
1174           IF ((tof12_i.GT.none_find).AND.(tof32_i.GT.none_find).AND.
1175         &    (xtofpos(1).NE.101.).AND.(xtofpos(3).NE.101.)) THEN
1176           xhelp1 = tof12(1,tof12_i,itdc)+tof12(2,tof12_i,itdc)           xhelp1 = tof12(1,tof12_i,itdc)+tof12(2,tof12_i,itdc)
1177           xhelp2 = tof32(1,tof32_i,itdc)+tof32(2,tof32_i,itdc)           xhelp2 = tof32(1,tof32_i,itdc)+tof32(2,tof32_i,itdc)
1178           ds = xhelp1-xhelp2           ds = xhelp1-xhelp2
1179           ihelp=(tof12_i-1)*3+tof32_i           ihelp=(tof12_i-1)*3+tof32_i
1180           c1 = k_S12S32(1,ihelp)           if (iz.le.1) c1 = k_S12S32(1,ihelp)
1181           c2 = k_S12S32(2,ihelp)           if (iz.eq.2) c1 = k_S12S32(2,ihelp)
1182             if (iz.gt.2) c1 = k_S12S32(3,ihelp)
1183           betatof_a(4) = c2/(cos(theta13)*(ds-c1))           betatof_a(4) = c2/(cos(theta13)*(ds-c1))
1184    
1185    C------- ToF Mask - S12 - S32
1186    
1187             tofmask(ch12a(tof12_i),hb12a(tof12_i)) =
1188         $        tofmask(ch12a(tof12_i),hb12a(tof12_i)) + 1
1189             tofmask(ch12b(tof12_i),hb12b(tof12_i)) =
1190         $        tofmask(ch12b(tof12_i),hb12b(tof12_i)) + 1
1191    
1192             tofmask(ch32a(tof32_i),hb32a(tof32_i)) =
1193         $        tofmask(ch32a(tof32_i),hb32a(tof32_i)) + 1
1194             tofmask(ch32b(tof32_i),hb32b(tof32_i)) =
1195         $        tofmask(ch32b(tof32_i),hb32b(tof32_i)) + 1
1196    
1197    C-------
1198    
1199        ENDIF        ENDIF
1200    
1201  C     S21 - S31  C     S21 - S31
1202        IF (tof21_i.GT.none_find.AND.tof31_i.GT.none_find) THEN  
1203          dist = ZTOF(3) - ZTOF(5)
1204          c2 = (2.*0.01*dist)/(3.E08*50.E-12  )
1205    
1206           IF ((tof21_i.GT.none_find).AND.(tof31_i.GT.none_find).AND.
1207         &    (xtofpos(2).NE.101.).AND.(ytofpos(3).NE.101.)) THEN
1208           xhelp1 = tof21(1,tof21_i,itdc)+tof21(2,tof21_i,itdc)           xhelp1 = tof21(1,tof21_i,itdc)+tof21(2,tof21_i,itdc)
1209           xhelp2 = tof31(1,tof31_i,itdc)+tof31(2,tof31_i,itdc)           xhelp2 = tof31(1,tof31_i,itdc)+tof31(2,tof31_i,itdc)
1210           ds = xhelp1-xhelp2           ds = xhelp1-xhelp2
1211           ihelp=(tof21_i-1)*3+tof31_i           ihelp=(tof21_i-1)*3+tof31_i
1212           c1 = k_S21S31(1,ihelp)           if (iz.le.1) c1 = k_S21S31(1,ihelp)
1213           c2 = k_S21S31(2,ihelp)           if (iz.eq.2) c1 = k_S21S31(2,ihelp)
1214           betatof_a(5) = c2/(cos(theta23)*(ds-c1))           if (iz.gt.2) c1 = k_S21S31(3,ihelp)
1215             betatof_a(5) = c2/(cos(theta13)*(ds-c1))
1216    
1217    C------- ToF Mask - S21 - S31
1218    
1219             tofmask(ch21a(tof21_i),hb21a(tof21_i)) =
1220         $        tofmask(ch21a(tof21_i),hb21a(tof21_i)) + 1
1221             tofmask(ch21b(tof21_i),hb21b(tof21_i)) =
1222         $        tofmask(ch21b(tof21_i),hb21b(tof21_i)) + 1
1223    
1224             tofmask(ch31a(tof31_i),hb31a(tof31_i)) =
1225         $        tofmask(ch31a(tof31_i),hb31a(tof31_i)) + 1
1226             tofmask(ch31b(tof31_i),hb31b(tof31_i)) =
1227         $        tofmask(ch31b(tof31_i),hb31b(tof31_i)) + 1
1228    
1229    C-------
1230    
1231        ENDIF        ENDIF
1232    
1233  C     S21 - S32  C     S21 - S32
1234        IF (tof21_i.GT.none_find.AND.tof32_i.GT.none_find) THEN  
1235          dist = ZTOF(3) - ZTOF(6)
1236          c2 = (2.*0.01*dist)/(3.E08*50.E-12  )
1237    
1238    
1239           IF ((tof21_i.GT.none_find).AND.(tof32_i.GT.none_find).AND.
1240         &    (xtofpos(2).NE.101.).AND.(xtofpos(3).NE.101.)) THEN
1241           xhelp1 = tof21(1,tof21_i,itdc)+tof21(2,tof21_i,itdc)           xhelp1 = tof21(1,tof21_i,itdc)+tof21(2,tof21_i,itdc)
1242           xhelp2 = tof32(1,tof32_i,itdc)+tof32(2,tof32_i,itdc)           xhelp2 = tof32(1,tof32_i,itdc)+tof32(2,tof32_i,itdc)
1243           ds = xhelp1-xhelp2           ds = xhelp1-xhelp2
1244           ihelp=(tof21_i-1)*3+tof32_i           ihelp=(tof21_i-1)*3+tof32_i
1245           c1 = k_S21S32(1,ihelp)           if (iz.le.1) c1 = k_S21S32(1,ihelp)
1246           c2 = k_S21S32(2,ihelp)           if (iz.eq.2) c1 = k_S21S32(2,ihelp)
1247           betatof_a(6) = c2/(cos(theta23)*(ds-c1))           if (iz.gt.2) c1 = k_S21S32(3,ihelp)
1248             betatof_a(6) = c2/(cos(theta13)*(ds-c1))
1249                      
1250    C------- ToF Mask - S21 - S32
1251    
1252             tofmask(ch21a(tof21_i),hb21a(tof21_i)) =
1253         $        tofmask(ch21a(tof21_i),hb21a(tof21_i)) + 1
1254             tofmask(ch21b(tof21_i),hb21b(tof21_i)) =
1255         $        tofmask(ch21b(tof21_i),hb21b(tof21_i)) + 1
1256    
1257             tofmask(ch32a(tof32_i),hb32a(tof32_i)) =
1258         $        tofmask(ch32a(tof32_i),hb32a(tof32_i)) + 1
1259             tofmask(ch32b(tof32_i),hb32b(tof32_i)) =
1260         $        tofmask(ch32b(tof32_i),hb32b(tof32_i)) + 1
1261    
1262    C-------
1263    
1264        ENDIF        ENDIF
1265    
1266  C     S22 - S31  C     S22 - S31
1267        IF (tof22_i.GT.none_find.AND.tof31_i.GT.none_find) THEN  
1268          dist = ZTOF(4) - ZTOF(5)
1269          c2 = (2.*0.01*dist)/(3.E08*50.E-12  )
1270    
1271           IF ((tof22_i.GT.none_find).AND.(tof31_i.GT.none_find).AND.
1272         &    (ytofpos(2).NE.101.).AND.(ytofpos(3).NE.101.)) THEN
1273           xhelp1 = tof22(1,tof22_i,itdc)+tof22(2,tof22_i,itdc)           xhelp1 = tof22(1,tof22_i,itdc)+tof22(2,tof22_i,itdc)
1274           xhelp2 = tof31(1,tof31_i,itdc)+tof31(2,tof31_i,itdc)           xhelp2 = tof31(1,tof31_i,itdc)+tof31(2,tof31_i,itdc)
1275           ds = xhelp1-xhelp2           ds = xhelp1-xhelp2
1276           ihelp=(tof22_i-1)*3+tof31_i           ihelp=(tof22_i-1)*3+tof31_i
1277           c1 = k_S22S31(1,ihelp)           if (iz.le.1) c1 = k_S22S31(1,ihelp)
1278           c2 = k_S22S31(2,ihelp)           if (iz.eq.2) c1 = k_S22S31(2,ihelp)
1279             if (iz.gt.2) c1 = k_S22S31(3,ihelp)
1280           betatof_a(7) = c2/(cos(theta13)*(ds-c1))           betatof_a(7) = c2/(cos(theta13)*(ds-c1))
1281    
1282    C------- ToF Mask - S22 - S31
1283    
1284             tofmask(ch22a(tof22_i),hb22a(tof22_i)) =
1285         $        tofmask(ch22a(tof22_i),hb22a(tof22_i)) + 1
1286             tofmask(ch22b(tof22_i),hb22b(tof22_i)) =
1287         $        tofmask(ch22b(tof22_i),hb22b(tof22_i)) + 1
1288    
1289             tofmask(ch31a(tof31_i),hb31a(tof31_i)) =
1290         $        tofmask(ch31a(tof31_i),hb31a(tof31_i)) + 1
1291             tofmask(ch31b(tof31_i),hb31b(tof31_i)) =
1292         $        tofmask(ch31b(tof31_i),hb31b(tof31_i)) + 1
1293    
1294    C-------          
1295    
1296        ENDIF        ENDIF
1297                
1298  C     S22 - S32  C     S22 - S32
1299        IF (tof22_i.GT.none_find.AND.tof32_i.GT.none_find) THEN  
1300          dist = ZTOF(4) - ZTOF(6)
1301          c2 = (2.*0.01*dist)/(3.E08*50.E-12  )
1302    
1303           IF ((tof22_i.GT.none_find).AND.(tof32_i.GT.none_find).AND.
1304         &    (ytofpos(2).NE.101.).AND.(xtofpos(3).NE.101.)) THEN
1305           xhelp1 = tof22(1,tof22_i,itdc)+tof22(2,tof22_i,itdc)           xhelp1 = tof22(1,tof22_i,itdc)+tof22(2,tof22_i,itdc)
1306           xhelp2 = tof32(1,tof32_i,itdc)+tof32(2,tof32_i,itdc)           xhelp2 = tof32(1,tof32_i,itdc)+tof32(2,tof32_i,itdc)
1307           ds = xhelp1-xhelp2           ds = xhelp1-xhelp2
1308           ihelp=(tof22_i-1)*3+tof32_i           ihelp=(tof22_i-1)*3+tof32_i
1309           c1 = k_S22S32(1,ihelp)           if (iz.le.1) c1 = k_S22S32(1,ihelp)
1310           c2 = k_S22S32(2,ihelp)           if (iz.eq.2) c1 = k_S22S32(2,ihelp)
1311             if (iz.gt.2) c1 = k_S22S32(3,ihelp)
1312           betatof_a(8) = c2/(cos(theta13)*(ds-c1))           betatof_a(8) = c2/(cos(theta13)*(ds-c1))
1313    
1314    C------- ToF Mask - S22 - S32
1315    
1316             tofmask(ch22a(tof22_i),hb22a(tof22_i)) =
1317         $        tofmask(ch22a(tof22_i),hb22a(tof22_i)) + 1
1318             tofmask(ch22b(tof22_i),hb22b(tof22_i)) =
1319         $        tofmask(ch22b(tof22_i),hb22b(tof22_i)) + 1
1320    
1321             tofmask(ch32a(tof32_i),hb32a(tof32_i)) =
1322         $        tofmask(ch32a(tof32_i),hb32a(tof32_i)) + 1
1323             tofmask(ch32b(tof32_i),hb32b(tof32_i)) =
1324         $        tofmask(ch32b(tof32_i),hb32b(tof32_i)) + 1
1325    
1326    C-------  
1327    
1328        ENDIF        ENDIF
1329    
1330  C     S11 - S21  C     S11 - S21
1331        IF (tof11_i.GT.none_find.AND.tof21_i.GT.none_find) THEN  
1332          dist = ZTOF(1) - ZTOF(3)
1333          c2 = (2.*0.01*dist)/(3.E08*50.E-12  )
1334    
1335           IF ((tof11_i.GT.none_find).AND.(tof21_i.GT.none_find).AND.
1336         &    (ytofpos(1).NE.101.).AND.(xtofpos(2).NE.101.)) THEN
1337           xhelp1 = tof11(1,tof11_i,itdc)+tof11(2,tof11_i,itdc)           xhelp1 = tof11(1,tof11_i,itdc)+tof11(2,tof11_i,itdc)
1338           xhelp2 = tof21(1,tof21_i,itdc)+tof21(2,tof21_i,itdc)           xhelp2 = tof21(1,tof21_i,itdc)+tof21(2,tof21_i,itdc)
1339           ds = xhelp1-xhelp2           ds = xhelp1-xhelp2
1340           ihelp=(tof11_i-1)*2+tof21_i           ihelp=(tof11_i-1)*2+tof21_i
1341           c1 = k_S11S21(1,ihelp)           if (iz.le.1) c1 = k_S11S21(1,ihelp)
1342           c2 = k_S11S21(2,ihelp)           if (iz.eq.2) c1 = k_S11S21(2,ihelp)
1343             if (iz.gt.2) c1 = k_S11S21(3,ihelp)
1344           betatof_a(9) = c2/(cos(theta13)*(ds-c1))           betatof_a(9) = c2/(cos(theta13)*(ds-c1))
1345    
1346    C------- ToF Mask - S11 - S21
1347    
1348             tofmask(ch11a(tof11_i),hb11a(tof11_i)) =
1349         $        tofmask(ch11a(tof11_i),hb11a(tof11_i)) + 1
1350             tofmask(ch11b(tof11_i),hb11b(tof11_i)) =
1351         $        tofmask(ch11b(tof11_i),hb11b(tof11_i)) + 1
1352    
1353             tofmask(ch21a(tof21_i),hb21a(tof21_i)) =
1354         $        tofmask(ch21a(tof21_i),hb21a(tof21_i)) + 1
1355             tofmask(ch21b(tof21_i),hb21b(tof21_i)) =
1356         $        tofmask(ch21b(tof21_i),hb21b(tof21_i)) + 1
1357    
1358    C-------  
1359    
1360        ENDIF        ENDIF
1361                
1362  C     S11 - S22  C     S11 - S22
1363        IF (tof11_i.GT.none_find.AND.tof22_i.GT.none_find) THEN  
1364          dist = ZTOF(1) - ZTOF(4)
1365          c2 = (2.*0.01*dist)/(3.E08*50.E-12  )
1366    
1367           IF ((tof11_i.GT.none_find).AND.(tof22_i.GT.none_find).AND.
1368         &    (ytofpos(1).NE.101.).AND.(ytofpos(2).NE.101.)) THEN
1369           xhelp1 = tof11(1,tof11_i,itdc)+tof11(2,tof11_i,itdc)           xhelp1 = tof11(1,tof11_i,itdc)+tof11(2,tof11_i,itdc)
1370           xhelp2 = tof22(1,tof22_i,itdc)+tof22(2,tof22_i,itdc)           xhelp2 = tof22(1,tof22_i,itdc)+tof22(2,tof22_i,itdc)
1371           ds = xhelp1-xhelp2           ds = xhelp1-xhelp2
1372           ihelp=(tof11_i-1)*2+tof22_i           ihelp=(tof11_i-1)*2+tof22_i
1373           c1 = k_S11S22(1,ihelp)           if (iz.le.1) c1 = k_S11S22(1,ihelp)
1374           c2 = k_S11S22(2,ihelp)           if (iz.eq.2) c1 = k_S11S22(2,ihelp)
1375             if (iz.gt.2) c1 = k_S11S22(3,ihelp)
1376           betatof_a(10) = c2/(cos(theta13)*(ds-c1))           betatof_a(10) = c2/(cos(theta13)*(ds-c1))
1377    
1378    C------- ToF Mask - S11 - S22
1379    
1380             tofmask(ch11a(tof11_i),hb11a(tof11_i)) =
1381         $        tofmask(ch11a(tof11_i),hb11a(tof11_i)) + 1
1382             tofmask(ch11b(tof11_i),hb11b(tof11_i)) =
1383         $        tofmask(ch11b(tof11_i),hb11b(tof11_i)) + 1
1384    
1385             tofmask(ch22a(tof22_i),hb22a(tof22_i)) =
1386         $        tofmask(ch22a(tof22_i),hb22a(tof22_i)) + 1
1387             tofmask(ch22b(tof22_i),hb22b(tof22_i)) =
1388         $        tofmask(ch22b(tof22_i),hb22b(tof22_i)) + 1
1389    
1390    C-------  
1391    
1392        ENDIF        ENDIF
1393    
1394  C     S12 - S21  C     S12 - S21
1395        IF (tof12_i.GT.none_find.AND.tof21_i.GT.none_find) THEN  
1396          dist = ZTOF(2) - ZTOF(3)
1397          c2 = (2.*0.01*dist)/(3.E08*50.E-12  )
1398    
1399           IF ((tof12_i.GT.none_find).AND.(tof21_i.GT.none_find).AND.
1400         &    (xtofpos(1).NE.101.).AND.(xtofpos(2).NE.101.)) THEN
1401           xhelp1 = tof12(1,tof12_i,itdc)+tof12(2,tof12_i,itdc)           xhelp1 = tof12(1,tof12_i,itdc)+tof12(2,tof12_i,itdc)
1402           xhelp2 = tof21(1,tof21_i,itdc)+tof21(2,tof21_i,itdc)           xhelp2 = tof21(1,tof21_i,itdc)+tof21(2,tof21_i,itdc)
1403           ds = xhelp1-xhelp2           ds = xhelp1-xhelp2
1404           ihelp=(tof12_i-1)*2+tof21_i           ihelp=(tof12_i-1)*2+tof21_i
1405           c1 = k_S12S21(1,ihelp)           if (iz.le.1) c1 = k_S12S21(1,ihelp)
1406           c2 = k_S12S21(2,ihelp)           if (iz.eq.2) c1 = k_S12S21(2,ihelp)
1407             if (iz.gt.2) c1 = k_S12S21(3,ihelp)
1408           betatof_a(11) = c2/(cos(theta13)*(ds-c1))           betatof_a(11) = c2/(cos(theta13)*(ds-c1))
1409    
1410    C------- ToF Mask - S12 - S21
1411    
1412             tofmask(ch12a(tof12_i),hb12a(tof12_i)) =
1413         $        tofmask(ch12a(tof12_i),hb12a(tof12_i)) + 1
1414             tofmask(ch12b(tof12_i),hb12b(tof12_i)) =
1415         $        tofmask(ch12b(tof12_i),hb12b(tof12_i)) + 1
1416    
1417             tofmask(ch21a(tof21_i),hb21a(tof21_i)) =
1418         $        tofmask(ch21a(tof21_i),hb21a(tof21_i)) + 1
1419             tofmask(ch21b(tof21_i),hb21b(tof21_i)) =
1420         $        tofmask(ch21b(tof21_i),hb21b(tof21_i)) + 1
1421    
1422    C-------  
1423    
1424        ENDIF        ENDIF
1425    
1426  C     S12 - S22  C     S12 - S22
1427        IF (tof12_i.GT.none_find.AND.tof22_i.GT.none_find) THEN  
1428          dist = ZTOF(2) - ZTOF(4)
1429          c2 = (2.*0.01*dist)/(3.E08*50.E-12  )
1430    
1431           IF ((tof12_i.GT.none_find).AND.(tof22_i.GT.none_find).AND.
1432         &    (xtofpos(1).NE.101.).AND.(ytofpos(2).NE.101.)) THEN
1433           xhelp1 = tof12(1,tof12_i,itdc)+tof12(2,tof12_i,itdc)           xhelp1 = tof12(1,tof12_i,itdc)+tof12(2,tof12_i,itdc)
1434           xhelp2 = tof22(1,tof22_i,itdc)+tof22(2,tof22_i,itdc)           xhelp2 = tof22(1,tof22_i,itdc)+tof22(2,tof22_i,itdc)
1435           ds = xhelp1-xhelp2           ds = xhelp1-xhelp2
1436           ihelp=(tof12_i-1)*2+tof22_i           ihelp=(tof12_i-1)*2+tof22_i
1437           c1 = k_S12S22(1,ihelp)           if (iz.le.1) c1 = k_S12S22(1,ihelp)
1438           c2 = k_S12S22(2,ihelp)           if (iz.eq.2) c1 = k_S12S22(2,ihelp)
1439             if (iz.gt.2) c1 = k_S12S22(3,ihelp)
1440           betatof_a(12) = c2/(cos(theta13)*(ds-c1))           betatof_a(12) = c2/(cos(theta13)*(ds-c1))
1441    
1442    C------- ToF Mask - S12 - S22
1443    
1444             tofmask(ch12a(tof12_i),hb12a(tof12_i)) =
1445         $        tofmask(ch12a(tof12_i),hb12a(tof12_i)) + 1
1446             tofmask(ch12b(tof12_i),hb12b(tof12_i)) =
1447         $        tofmask(ch12b(tof12_i),hb12b(tof12_i)) + 1
1448    
1449             tofmask(ch22a(tof22_i),hb22a(tof22_i)) =
1450         $        tofmask(ch22a(tof22_i),hb22a(tof22_i)) + 1
1451             tofmask(ch22b(tof22_i),hb22b(tof22_i)) =
1452         $        tofmask(ch22b(tof22_i),hb22b(tof22_i)) + 1
1453    
1454    C-------  
1455    
1456        ENDIF        ENDIF
         
 C-------  
1457    
1458        icount=0  C---------------------------------------------------------
1459        sw=0.  C
1460        sxw=0.  C      icount=0
1461        beta_mean=100.  C      sw=0.
1462    C      sxw=0.
1463    C      beta_mean=100.
1464    C
1465    C      do i=1,12
1466    C         if ((betatof_a(i).gt.-1.5).and.(betatof_a(i).lt.1.5)) then
1467    C            icount= icount+1
1468    C            if (i.le.4) w_i=1./(0.13**2.)
1469    C            if ((i.ge.5).and.(i.le.8)) w_i=1./(0.16**2.)
1470    C         if (i.ge.9) w_i=1./(0.25**2.)     ! to be checked
1471    C            sxw=sxw + betatof_a(i)*w_i
1472    C            sw =sw + w_i
1473    C         endif
1474    C      enddo
1475    C      
1476    C      if (icount.gt.0) beta_mean=sxw/sw
1477    C      betatof_a(13) = beta_mean
1478    C
1479    
1480        do i=1,12  C--------  New mean beta  calculation  -----------------------
          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  
 c      write(*,*) '------------- end  tofl2com ----------'  
1481    
1482   100  continue          do i=1,12
1483             btemp(i) = betatof_a(i)
1484            enddo
1485    
1486            betatof_a(13)=newbeta(1,btemp,hitvec,10.,10.,20.)
1487    
1488    C--------------------------------------------------------------
1489    C      write(*,*) betatof_a
1490    c      write(*,*) xtofpos
1491    c      write(*,*) ytofpos
1492    c      write(*,*)'tofl2com beta', betatof_a
1493    C      write(*,*) adcflagtof
1494    c      write(*,*) 'tofl2com'
1495    c      write(*,*) xtofpos
1496    c      write(*,*) ytofpos
1497    c      write(*,*) xtr_tof
1498    c      write(*,*) ytr_tof
1499          
1500    c 100  continue
1501            continue
1502    
1503  C  C
1504        RETURN        RETURN
1505        END        END
1506    
1507    
1508    C------------------------------------------------------------------
1509    C------------------------------------------------------------------
1510    
1511           function atten(is,ilay,ipad,x)
1512           include  'input_tof.txt'
1513           real atten
1514           real x
1515           real xmin,xmax
1516           integer  ilay,ipad
1517    
1518    *  S11 8 paddles  33.0 x 5.1 cm
1519    *  S12 6 paddles  40.8 x 5.5 cm
1520    *  S21 2 paddles  18.0 x 7.5 cm
1521    *  S22 2 paddles  15.0 x 9.0 cm
1522    *  S31 3 paddles  15.0 x 6.0 cm
1523    *  S32 3 paddles  18.0 x 5.0 cm
1524    
1525    
1526    c       if (ilay.eq.11) write(*,*) 'start ',ipad,is,adcx11(is,ipad,1),
1527    c     &  adcx11(is,ipad,2),adcx11(is,ipad,3),adcx11(is,ipad,4)
1528    c       if (ilay.eq.12) write(*,*) 'start ',ipad,is,adcx12(is,ipad,1),
1529    c     &  adcx12(is,ipad,2),adcx12(is,ipad,3),adcx12(is,ipad,4)
1530    
1531    
1532           if (ilay.eq.11)  xmin=-33.0/2.
1533           if (ilay.eq.11)  xmax= 33.0/2.
1534           if (ilay.eq.12)  xmin=-40.8/2.
1535           if (ilay.eq.12)  xmax= 40.8/2.
1536    
1537           if (ilay.eq.21)  xmin=-18.0/2.
1538           if (ilay.eq.21)  xmax= 18.0/2.
1539           if (ilay.eq.22)  xmin=-15.0/2.
1540           if (ilay.eq.22)  xmax= 15.0/2.
1541    
1542           if (ilay.eq.31)  xmin=-15.0/2.
1543           if (ilay.eq.31)  xmax= 15.0/2.
1544           if (ilay.eq.32)  xmin=-18.0/2.
1545           if (ilay.eq.32)  xmax= 18.0/2.
1546    
1547           if (x .lt. xmin) x=xmin
1548           if (x .gt. xmax) x=xmax
1549    
1550    
1551           if (ilay.eq.11) atten=
1552         &    adcx11(is,ipad,1)*exp(x*adcx11(is,ipad,2))
1553         &  + adcx11(is,ipad,3)*exp(x*adcx11(is,ipad,4))
1554    
1555           if (ilay.eq.12) atten=
1556         &    adcx12(is,ipad,1)*exp(x*adcx12(is,ipad,2))
1557         &  + adcx12(is,ipad,3)*exp(x*adcx12(is,ipad,4))
1558    
1559           if (ilay.eq.21) atten=
1560         &    adcx21(is,ipad,1)*exp(x*adcx21(is,ipad,2))
1561         &  + adcx21(is,ipad,3)*exp(x*adcx21(is,ipad,4))
1562    
1563           if (ilay.eq.22) atten=
1564         &    adcx22(is,ipad,1)*exp(x*adcx22(is,ipad,2))
1565         &  + adcx22(is,ipad,3)*exp(x*adcx22(is,ipad,4))
1566    
1567           if (ilay.eq.31) atten=
1568         &    adcx31(is,ipad,1)*exp(x*adcx31(is,ipad,2))
1569         &  + adcx31(is,ipad,3)*exp(x*adcx31(is,ipad,4))
1570    
1571           if (ilay.eq.32) atten=
1572         &    adcx32(is,ipad,1)*exp(x*adcx32(is,ipad,2))
1573         &  + adcx32(is,ipad,3)*exp(x*adcx32(is,ipad,4))
1574    
1575            if (atten.gt.10000) atten=10000.
1576    
1577           end
1578    
1579    C------------------------------------------------------------------
1580    C------------------------------------------------------------------
1581    
1582           function pc_adc(ix)
1583           include  'input_tof.txt'
1584           real pc_adc
1585           integer ix
1586    
1587           pc_adc=28.0407 + 0.628929*ix
1588         &   - 5.80901e-05*ix*ix + 3.14092e-08*ix*ix*ix
1589    c       write(*,*) ix,pc_adc
1590           end
1591    
1592    C------------------------------------------------------------------
1593    C------------------------------------------------------------------
1594    
1595            function check_charge(theta,hitvec)
1596    
1597            include  'input_tof.txt'
1598            include  'tofcomm.txt'
1599    
1600            real check_charge  
1601            integer hitvec(6)  
1602            REAL CHARGE, theta
1603    
1604    C  upper and lower limits  for the helium selection
1605            REAL A_l(24),A_h(24)
1606            DATA A_l /200,190,300,210,220,200,210,60,60,120,220,
1607         &  120,160,50,300,200,120,250,350,300,350,250,280,300/
1608            DATA A_h /550,490,800,600,650,600,600,260,200,380,
1609         &  620,380,550,200,850,560,400,750,900,800,880,800,750,800/
1610    
1611    C The k1 constants for the beta calculation, only for S1-S3
1612    C k2 constant is taken to be the standard 2D/c
1613            REAL k1(84)
1614            DATA k1 /50,59.3296,28.4328,-26.0818,5.91253,-19.588,
1615         &   -9.26316,24.7544,2.32465,-50.5058,-15.3195,-39.1443,
1616         &   -91.2546,-58.6243,-84.5641,-63.1516,-32.2091,-58.3358,
1617         &   13.8084,45.5322,33.2416,-11.5313,51.3271,75,-14.1141,
1618         &   42.8466,15.1794,-63.6672,-6.07739,-32.164,-41.771,10.5274,
1619         &   -9.46096,-81.7404,-28.783,-52.7167,-127.394,-69.6166,
1620         &   -93.4655,-98.9543,-42.863,-67.8244,-19.3238,31.1221,8.7319,
1621         &   -43.1627,5.55573,-14.4078,-83.4466,-47.4647,-77.8379,
1622         &   -108.222,-75.986,-101.297,-96.0205,-63.1881,-90.1372,
1623         &   -22.7347,8.31409,-19.6912,-7.49008,23.6979,-1.66677,
1624         &   1.81556,34.4668,6.23693,-100,-59.5861,-90.9159,-141.639,
1625         &   -89.2521,-112.881,-130.199,-77.0357,-98.4632,-60.2086,
1626         &   -4.82097,-29.3705,-43.6469,10.5884,-9.31304,-35.3329,
1627         &   25.2514,25.6/
1628    
1629    
1630    
1631            REAL zin(6)
1632            DATA zin /53.74, 53.04, 23.94, 23.44, -23.49, -24.34/
1633    
1634            REAL  c1,c2,xhelp,xhelp1,xhelp2,ds,dist,F
1635            REAL  sw,sxw,beta_mean_tof,w_i
1636            INTEGER  ihelp
1637            INTEGER ipmt(4)
1638            REAL time(4),beta1(4)
1639    
1640            REAL  adca(48), tdca(48)
1641    
1642            REAL  a1,a2
1643            INTEGER jj
1644    
1645    c get rid of warnings EMILIANO
1646            i = 0
1647            slope = 0
1648            offset = 0
1649            none_find = 0
1650            none_ev = 0
1651            adc_ev = 0
1652            tdc_ev = 0
1653            iadc = 0
1654            itdc = 0
1655            right = 0
1656            left = 0
1657            tof12_y(1) = tof12_y(1)
1658            tof11_x(1) = tof11_x(1)
1659            tof21_y(1) = tof21_y(1)
1660            tof22_x(1) = tof22_x(1)
1661            tof32_y(1) = tof32_y(1)
1662            tof31_x(1) = tof31_x(1)
1663    c get rid of warnings
1664    
1665    C-----------------------------------------------------------
1666    C--- get data
1667    C-----------------------------------------------------------
1668    
1669             do j=1,8
1670             ih = 1 + 0 +((j-1)*2)
1671             adca(ih)   = adc(ch11a(j),hb11a(j))
1672             adca(ih+1) = adc(ch11b(j),hb11b(j))
1673             tdca(ih)   = tdc(ch11a(j),hb11a(j))
1674             tdca(ih+1) = tdc(ch11b(j),hb11b(j))
1675             enddo
1676    
1677             do j=1,6
1678             ih = 1 + 16+((j-1)*2)
1679             adca(ih)   = adc(ch12a(j),hb12a(j))
1680             adca(ih+1) = adc(ch12b(j),hb12b(j))
1681             tdca(ih)   = tdc(ch12a(j),hb12a(j))
1682             tdca(ih+1) = tdc(ch12b(j),hb12b(j))
1683             enddo
1684    
1685             do j=1,2
1686             ih = 1 + 28+((j-1)*2)
1687             adca(ih)   = adc(ch21a(j),hb21a(j))
1688             adca(ih+1) = adc(ch21b(j),hb21b(j))
1689             tdca(ih)   = tdc(ch21a(j),hb21a(j))
1690             tdca(ih+1) = tdc(ch21b(j),hb21b(j))
1691             enddo
1692    
1693             do j=1,2
1694             ih = 1 + 32+((j-1)*2)
1695             adca(ih)   = adc(ch22a(j),hb22a(j))
1696             adca(ih+1) = adc(ch22b(j),hb22b(j))
1697             tdca(ih)   = tdc(ch22a(j),hb22a(j))
1698             tdca(ih+1) = tdc(ch22b(j),hb22b(j))
1699             enddo
1700    
1701             do j=1,3
1702             ih = 1 + 36+((j-1)*2)
1703             adca(ih)   = adc(ch31a(j),hb31a(j))
1704             adca(ih+1) = adc(ch31b(j),hb31b(j))
1705             tdca(ih)   = tdc(ch31a(j),hb31a(j))
1706             tdca(ih+1) = tdc(ch31b(j),hb31b(j))
1707             enddo
1708    
1709             do j=1,3
1710             ih = 1 + 42+((j-1)*2)
1711             adca(ih)   = adc(ch32a(j),hb32a(j))
1712             adca(ih+1) = adc(ch32b(j),hb32b(j))
1713             tdca(ih)   = tdc(ch32a(j),hb32a(j))
1714             tdca(ih+1) = tdc(ch32b(j),hb32b(j))
1715             enddo
1716    
1717    
1718    c         write(*,*) adca
1719    c         write(*,*) tdca
1720    
1721    
1722    C============   calculate beta and select charge > Z=1   ===============
1723    
1724            ICHARGE=1
1725    
1726    C find hitted paddle by looking for ADC values on both sides
1727    C since we looking for Z>1 this gives decent results
1728    
1729            tof11_i = hitvec(1)-1
1730            tof12_i = hitvec(2)-1
1731            tof21_i = hitvec(3)-1
1732            tof22_i = hitvec(4)-1
1733            tof31_i = hitvec(5)-1
1734            tof32_i = hitvec(6)-1
1735    
1736    c        write(*,*) ' in charge check'
1737    c        write(*,*) theta,tof11_i,tof12_i,tof21_i,tof22_i,tof31_i,tof32_i
1738    
1739    C----------------------------------------------------------------
1740    
1741            beta_help=100.
1742            beta_mean_tof=100.
1743    
1744            do jj=1,4
1745              beta1(jj) = 100.
1746            enddo
1747    
1748    C----------------------------------------------------------------
1749    C---------  S1 - S3 ---------------------------------------------
1750    C----------------------------------------------------------------
1751    
1752    C---------  S11 - S31 -------------------------------------------
1753    
1754            if ((tof11_i.gt.-1).and.(tof31_i.gt.-1)) then
1755    
1756            dist = zin(1) - zin(5)
1757            c2 = (2.*0.01*dist)/(3.E08*50.E-12)
1758            F = 1./cos(theta)
1759    
1760            ipmt(1)   = (tof11_i)*2+1
1761            ipmt(2)   = (tof11_i)*2+2
1762            ipmt(3)   = 36+(tof31_i)*2+1
1763            ipmt(4)   = 36+(tof31_i)*2+2
1764    
1765    c        write(*,*) ipmt
1766    
1767            do jj=1,4
1768               time(jj) = tdca(ipmt(jj))
1769            enddo
1770    
1771    c        write(*,*) time
1772    
1773            if ((time(1).lt.4095).and.(time(2).lt.4095).and.
1774         &     (time(3).lt.4095).and.(time(4).lt.4095)) then
1775             xhelp1 = time(1) + time(2)
1776             xhelp2 = time(3) + time(4)
1777             ds = xhelp1-xhelp2
1778             ihelp=0+(tof11_i)*3+tof31_i
1779             c1 = k1(ihelp+1)
1780             beta1(1) = c2*F/(ds-c1);
1781                                                     endif
1782    c         write(*,*) beta1(1)
1783             endif  ! tof_....
1784    
1785    
1786    C---------  S11 - S32 -------------------------------------------
1787    
1788            if ((tof11_i.gt.-1).and.(tof32_i.gt.-1)) then
1789    
1790            dist = zin(1) - zin(6)
1791            c2 = (2.*0.01*dist)/(3.E08*50.E-12)
1792            F = 1./cos(theta)
1793    
1794            ipmt(1)   = (tof11_i)*2+1
1795            ipmt(2)   = (tof11_i)*2+2
1796            ipmt(3)   = 42+(tof32_i)*2+1
1797            ipmt(4)   = 42+(tof32_i)*2+2
1798    
1799            do jj=1,4
1800               time(jj) = tdca(ipmt(jj))
1801            enddo
1802    
1803            if ((time(1).lt.4095).and.(time(2).lt.4095).and.
1804         &     (time(3).lt.4095).and.(time(4).lt.4095)) then
1805             xhelp1 = time(1) + time(2)
1806             xhelp2 = time(3) + time(4)
1807             ds = xhelp1-xhelp2
1808             ihelp=24+(tof11_i)*3+tof32_i
1809             c1 = k1(ihelp+1)
1810             beta1(2) = c2*F/(ds-c1);
1811                                                     endif
1812             endif  ! tof_....
1813    
1814    
1815    C---------  S12 - S31 -------------------------------------------
1816    
1817            if ((tof12_i.gt.-1).and.(tof31_i.gt.-1)) then
1818    
1819            dist = zin(2) - zin(5)
1820            c2 = (2.*0.01*dist)/(3.E08*50.E-12)
1821            F = 1./cos(theta)
1822    
1823            ipmt(1)   = 16+(tof12_i)*2+1
1824            ipmt(2)   = 16+(tof12_i)*2+2
1825            ipmt(3)   = 36+(tof31_i)*2+1
1826            ipmt(4)   = 36+(tof31_i)*2+2
1827    
1828            do jj=1,4
1829               time(jj) = tdca(ipmt(jj))
1830            enddo
1831    
1832            if ((time(1).lt.4095).and.(time(2).lt.4095).and.
1833         &     (time(3).lt.4095).and.(time(4).lt.4095)) then
1834             xhelp1 = time(1) + time(2)
1835             xhelp2 = time(3) + time(4)
1836             ds = xhelp1-xhelp2
1837             ihelp=48+(tof12_i)*3+tof31_i
1838             c1 = k1(ihelp+1)
1839             beta1(3) = c2*F/(ds-c1);
1840                                                     endif
1841             endif  ! tof_....
1842    
1843    
1844    C---------  S12 - S32 -------------------------------------------
1845    
1846            if ((tof12_i.gt.-1).and.(tof32_i.gt.-1)) then
1847    
1848            dist = zin(2) - zin(6)
1849            c2 = (2.*0.01*dist)/(3.E08*50.E-12)
1850            F = 1./cos(theta)
1851    
1852            ipmt(1)   = 16+(tof12_i)*2+1
1853            ipmt(2)   = 16+(tof12_i)*2+2
1854            ipmt(3)   = 42+(tof32_i)*2+1
1855            ipmt(4)   = 42+(tof32_i)*2+2
1856    
1857            do jj=1,4
1858               time(jj) = tdca(ipmt(jj))
1859            enddo
1860    
1861            if ((time(1).lt.4095).and.(time(2).lt.4095).and.
1862         &     (time(3).lt.4095).and.(time(4).lt.4095)) then
1863             xhelp1 = time(1) + time(2)
1864             xhelp2 = time(3) + time(4)
1865             ds = xhelp1-xhelp2
1866             ihelp=56+(tof12_i)*3+tof32_i
1867             c1 = k1(ihelp+1)
1868             beta1(4) = c2*F/(ds-c1);
1869                                                     endif
1870    
1871             endif  ! tof_....
1872    
1873    c         write(*,*) beta1
1874      
1875    C----  calculate  beta mean, only downward going particles are interesting ----
1876    
1877             sw=0.
1878             sxw=0.
1879             beta_mean_tof=100.
1880    
1881            do jj=1,4
1882            if ((beta1(jj).gt.0.1).and.(beta1(jj).lt.2.0)) then
1883                w_i=1./(0.13*0.13)
1884                sxw=sxw + beta1(jj)*w_i
1885                sw =sw + w_i ;
1886                                                          endif
1887            enddo
1888    
1889            if (sw.gt.0) beta_mean_tof=sxw/sw;
1890    
1891    C        write(*,*) 'beta_mean_tof ',beta_mean_tof
1892    
1893            beta_help = beta_mean_tof  !  pow(beta_mean_tof,1.0) gave best results
1894    
1895    CCCCC        endif  !  if tof11_i > -1 && ...... beta calculation
1896    
1897    C-----------------------  Select charge   --------------------------
1898    
1899           charge=0
1900    
1901           if ((beta_mean_tof.gt.0.2).and.(beta_mean_tof.lt.2.0)) then
1902    
1903            icount1=0
1904            icount2=0
1905            icount3=0
1906    
1907            do jj=0,23
1908            a1 = adca(2*jj+1)
1909            a2 = adca(2*jj+2)
1910            if ((a1.lt.4095).and.(a2.lt.4095)) then
1911            a1 = adca(2*jj+1)*cos(theta)
1912            a2 = adca(2*jj+2)*cos(theta)
1913            xhelp  = 100000.
1914            xhelp1 = 100000.
1915            xhelp = sqrt(a1*a2)  ! geometric mean
1916            xhelp1 = beta_help*xhelp
1917    C if geometric mean multiplied by beta_help is inside/outside helium
1918    C limits, increase counter
1919           if (xhelp1.lt.A_l(jj+1))  icount1=icount1+1
1920           if ((xhelp1.gt.A_l(jj+1)).and.(xhelp1.lt.A_h(jj+1)))
1921         &                           icount2=icount2+1
1922           if (xhelp1.gt.A_h(jj+1))  icount3=icount3+1
1923                                                endif
1924            enddo
1925    
1926    
1927    C  if more than three paddles see the same...
1928    
1929            if (icount1 .gt. 3) charge=1
1930            if (icount2 .gt. 3) charge=2
1931            if (icount3 .gt. 3) charge=3
1932    
1933                                                        endif  ! 0.2<beta<2.0
1934    
1935    C  no beta found? Sum up geometric means of paddles and derive the mean...
1936    
1937           if (beta_mean_tof.eq.100.) then
1938    
1939           xhelp  = 0.
1940           icount = 0
1941    
1942           if (tof11_i.gt.-1) then
1943           jj=tof11_i
1944           a1 = adca(0+2*jj+1)
1945           a2 = adca(0+2*jj+2)
1946           if ((a1.lt.4095).and.(a2.lt.4095)) then
1947           a1 = a1*cos(theta)
1948           a2 = a2*cos(theta)
1949           xhelp = xhelp + sqrt(a1*a2)
1950           icount=icount+1
1951                        endif
1952                        endif
1953    
1954           if (tof12_i.gt.-1) then
1955           jj=tof12_i
1956           a1 = adca(16+2*jj+1)
1957           a2 = adca(16+2*jj+2)
1958           if ((a1.lt.4095).and.(a2.lt.4095)) then
1959           a1 = a1*cos(theta)
1960           a2 = a2*cos(theta)
1961           xhelp = xhelp + sqrt(a1*a2)
1962           icount=icount+1
1963                        endif
1964                        endif
1965    
1966           if (tof21_i.gt.-1) then
1967           jj=tof21_i
1968           a1 = adca(28+2*jj+1)
1969           a2 = adca(28+2*jj+2)
1970           if ((a1.lt.4095).and.(a2.lt.4095)) then
1971           a1 = a1*cos(theta)
1972           a2 = a2*cos(theta)
1973           xhelp = xhelp + sqrt(a1*a2)
1974           icount=icount+1
1975                        endif
1976                        endif
1977    
1978           if (tof22_i.gt.-1) then
1979           jj=tof22_i
1980           a1 = adca(32+2*jj+1)
1981           a2 = adca(32+2*jj+2)
1982           if ((a1.lt.4095).and.(a2.lt.4095)) then
1983           a1 = a1*cos(theta)
1984           a2 = a2*cos(theta)
1985           xhelp = xhelp + sqrt(a1*a2)
1986           icount=icount+1
1987                        endif
1988                        endif
1989    
1990           if (tof31_i.gt.-1) then
1991           jj=tof31_i
1992           a1 = adca(36+2*jj+1)
1993           a2 = adca(36+2*jj+2)
1994           if ((a1.lt.4095).and.(a2.lt.4095)) then
1995           a1 = a1*cos(theta)
1996           a2 = a2*cos(theta)
1997           xhelp = xhelp + sqrt(a1*a2)
1998           icount=icount+1
1999                        endif
2000                        endif
2001    
2002           if (tof32_i.gt.-1) then
2003           jj=tof32_i
2004           a1 = adca(42+2*jj+1)
2005           a2 = adca(42+2*jj+2)
2006           if ((a1.lt.4095).and.(a2.lt.4095)) then
2007           a1 = a1*cos(theta)
2008           a2 = a2*cos(theta)
2009           xhelp = xhelp + sqrt(a1*a2)
2010           icount=icount+1
2011                        endif
2012                        endif
2013    
2014    
2015           if (icount.gt.0) xhelp=xhelp/icount
2016           if ((icount.gt.2).and.(xhelp.gt.1500.)) charge=3
2017    
2018                                      endif  ! beta_mean_tof.eq.100.
2019    
2020    C        write(*,*) 'in function charge: ',beta_mean_tof,charge
2021    
2022            check_charge = charge
2023    
2024    
2025            END
2026    
2027    C****************************************************************************
2028    C****************************************************************************
2029    C****************************************************************************
2030    
2031            function newbeta(iflag,b,hitvec,resmax,qualitycut,chi2cut)
2032    
2033            include  'input_tof.txt'
2034            include  'output_tof.txt'
2035            include  'tofcomm.txt'
2036    
2037            REAL newbeta
2038            REAL resmax,qualitycut,chi2cut
2039            REAL w_i(12),w_il(6),quality,res,betachi,beta_mean_inv
2040            REAL sw,sxw,b(12),beta_mean,chi2,xhelp
2041            REAL tdcfl(4,12)
2042    
2043            INTEGER iflag,icount,hitvec(6)
2044    
2045            INTEGER itop(12),ibot(12)
2046            DATA itop /1,1,2,2,3,3,4,4,1,1,2,2/
2047            DATA ibot /5,6,5,6,5,6,5,6,3,4,3,4/
2048    
2049    
2050    c get rid of warnings EMILIANO
2051            slope = 0
2052            offset = 0
2053            none_find = 0
2054            none_ev = 0
2055            adc_ev = 0
2056            tdc_ev = 0
2057            iadc = 0
2058            itdc = 0
2059            right = 0
2060            left = 0
2061            tof12_y(1) = tof12_y(1)
2062            tof11_x(1) = tof11_x(1)
2063            tof21_y(1) = tof21_y(1)
2064            tof22_x(1) = tof22_x(1)
2065            tof32_y(1) = tof32_y(1)
2066            tof31_x(1) = tof31_x(1)
2067    c get rid of warnings
2068    
2069    C====================================================================
2070    
2071            tof11_i = hitvec(1)
2072            tof12_i = hitvec(2)
2073            tof21_i = hitvec(3)
2074            tof22_i = hitvec(4)
2075            tof31_i = hitvec(5)
2076            tof32_i = hitvec(6)
2077    
2078             if (iflag.eq.1) then   ! call from tofl2com
2079             do i=1,4
2080             do j=1,12
2081              tdcfl(i,j) =  tdcflagtof(i,j)
2082             enddo
2083             enddo
2084                            endif
2085    
2086             if (iflag.eq.2) then   ! call from toftrk
2087             do i=1,4
2088             do j=1,12
2089              tdcfl(i,j) =  tdcflag(i,j)
2090             enddo
2091             enddo
2092                            endif
2093    
2094    
2095    C---  Find out ToF layers with artificial TDC values    -------------
2096    
2097            do jj=1,6
2098            w_il(jj) = 1000.
2099            enddo
2100    
2101    
2102            if (tof11_i.gt.0) then
2103            if ((tofmask(ch11a(tof11_i),hb11a(tof11_i)).gt.0).or.
2104         &   (tofmask(ch11b(tof11_i),hb11b(tof11_i)).gt.0)) then
2105            w_il(1)=0
2106            i1=tdcfl(ch11a(tof11_i),hb11a(tof11_i))
2107            i2=tdcfl(ch11b(tof11_i),hb11b(tof11_i))
2108            if ((i1.eq.1).or.(i2.eq.1)) w_il(1) = 1  ! tdcflag
2109                                                          endif
2110                               endif
2111    
2112            if (tof12_i.gt.0) then
2113            if ((tofmask(ch12a(tof12_i),hb12a(tof12_i)).gt.0).or.
2114         &   (tofmask(ch12b(tof12_i),hb12b(tof12_i)).gt.0)) then
2115            w_il(2)=0
2116            i1=tdcfl(ch12a(tof12_i),hb12a(tof12_i))
2117            i2=tdcfl(ch12b(tof12_i),hb12b(tof12_i))
2118            if ((i1.eq.1).or.(i2.eq.1)) w_il(2) = 1  ! tdcflag
2119                                                          endif
2120                               endif
2121    
2122            if (tof21_i.gt.0) then
2123            if ((tofmask(ch21a(tof21_i),hb21a(tof21_i)).gt.0).or.
2124         &   (tofmask(ch21b(tof21_i),hb21b(tof21_i)).gt.0)) then
2125            w_il(3)=0
2126            i1=tdcfl(ch21a(tof21_i),hb21a(tof21_i))
2127            i2=tdcfl(ch21b(tof21_i),hb21b(tof21_i))
2128            if ((i1.eq.1).or.(i2.eq.1)) w_il(3) = 1  ! tdcflag
2129                                                          endif
2130                               endif
2131    
2132            if (tof22_i.gt.0) then
2133            if ((tofmask(ch22a(tof22_i),hb22a(tof22_i)).gt.0).or.
2134         &   (tofmask(ch22b(tof22_i),hb22b(tof22_i)).gt.0)) then
2135            w_il(4)=0
2136            i1=tdcfl(ch22a(tof22_i),hb22a(tof22_i))
2137            i2=tdcfl(ch22b(tof22_i),hb22b(tof22_i))
2138            if ((i1.eq.1).or.(i2.eq.1)) w_il(4) = 1  ! tdcflag
2139                                                          endif
2140                               endif
2141    
2142            if (tof31_i.gt.0) then
2143            if ((tofmask(ch31a(tof31_i),hb11a(tof31_i)).gt.0).or.
2144         &   (tofmask(ch31b(tof31_i),hb31b(tof31_i)).gt.0)) then
2145            w_il(5)=0
2146            i1=tdcfl(ch31a(tof31_i),hb31a(tof31_i))
2147            i2=tdcfl(ch31b(tof31_i),hb31b(tof31_i))
2148            if ((i1.eq.1).or.(i2.eq.1)) w_il(5) = 1  ! tdcflag
2149                                                          endif
2150                               endif
2151    
2152            if (tof32_i.gt.0) then
2153            if ((tofmask(ch32a(tof32_i),hb32a(tof32_i)).gt.0).or.
2154         &   (tofmask(ch32b(tof32_i),hb32b(tof32_i)).gt.0)) then
2155            w_il(6)=0
2156            i1=tdcfl(ch32a(tof32_i),hb32a(tof32_i))
2157            i2=tdcfl(ch32b(tof32_i),hb32b(tof32_i))
2158            if ((i1.eq.1).or.(i2.eq.1)) w_il(6) = 1  ! tdcflag
2159                                                          endif
2160                               endif
2161    
2162    C------------------------------------------------------------------------
2163    C---  Set weights for the 12 measurements using information for top and bottom:
2164    C---  if no measurements: weight = set to very high value=> not used
2165    C---  top or bottom artificial: weight*sqrt(2)
2166    C---  top and bottom artificial: weight*sqrt(2)*sqrt(2)
2167    
2168           DO jj=1,12
2169           if (jj.le.4)           xhelp = 0.11            ! S1-S3
2170           if ((jj.gt.4).and.(jj.le.8)) xhelp = 0.18      ! S2-S3
2171           if (jj.gt.8)           xhelp = 0.28            ! S1-S2
2172           if ((w_il(itop(jj)).eq.1000.).and.(w_il(ibot(jj)).eq.1000.))
2173         &   xhelp = 1.E09
2174           if ((w_il(itop(jj)).eq.1).or.(w_il(ibot(jj)).eq.1.))
2175         &   xhelp = xhelp*1.414
2176           if ((w_il(itop(jj)).eq.1).and.(w_il(ibot(jj)).eq.1.))
2177         &   xhelp = xhelp*2.
2178           w_i(jj) = 1./xhelp
2179           ENDDO
2180    
2181    C========================================================================
2182    C--- Calculate mean beta for the first time -----------------------------
2183    C--- We are using "1/beta" since its error is gaussian ------------------
2184    
2185            icount=0
2186            sw=0.
2187            sxw=0.
2188            beta_mean=100.
2189    
2190            DO jj=1,12
2191            IF ((abs(1./b(jj)).gt.0.1).and.(abs(1./b(jj)).lt.15.)) THEN
2192                icount = icount+1
2193                sxw    = sxw + (1./b(jj))*w_i(jj)*w_i(jj)
2194                sw     = sw + w_i(jj)*w_i(jj)
2195            ENDIF
2196            ENDDO
2197    
2198            if (icount.gt.0) beta_mean=1./(sxw/sw)
2199            beta_mean_inv = 1./beta_mean
2200    
2201      
2202    C--- Calculate beta for the second time, use residuals of the single
2203    C--- measurements to get a chi2 value
2204    
2205            icount  = 0
2206            sw      = 0.
2207            sxw     = 0.
2208            betachi = 100.
2209            chi2    = 0.
2210            quality = 0.
2211    
2212            DO jj=1,12
2213            IF ((abs(1./b(jj)).gt.0.1).and.(abs(1./b(jj)).lt.15.)
2214         &                                .and.(w_i(jj).GT.0.01)) THEN
2215                res    = beta_mean_inv - (1./b(jj)) ;
2216                if (abs(res*w_i(jj)).lt.resmax) THEN
2217                chi2   = chi2 + (res*w_i(jj))**2.
2218                icount = icount+1
2219                sxw    = sxw + (1./b(jj))*w_i(jj)*w_i(jj)
2220                sw     = sw + w_i(jj)*w_i(jj)
2221                                                ENDIF
2222            ENDIF
2223            ENDDO
2224    
2225    c        quality = sw
2226            quality = sqrt(sw)
2227    
2228            if (icount.eq.0) chi2 = 1000.
2229            if (icount.gt.0) chi2 = chi2/(icount)
2230    
2231            if (icount.gt.0) betachi=1./(sxw/sw);
2232    
2233            beta_mean=100.
2234            if ((chi2.lt.chi2cut).and.(quality.gt.qualitycut))
2235         &  beta_mean = betachi
2236            newbeta = beta_mean
2237    
2238            END
2239    
2240    C****************************************************************************
2241    

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.14

  ViewVC Help
Powered by ViewVC 1.1.23