Millepede-II  V04-00-00_preview
 All Classes Files Functions Variables Enumerator Pages
mptest2.f90
Go to the documentation of this file.
1 
2 ! Code converted using TO_F90 by Alan Miller
3 ! Date: 2012-03-16 Time: 11:08:55
4 
37 
39 MODULE mptest2
40  IMPLICIT NONE
41  SAVE
42 
43  INTEGER, PARAMETER :: nlyr=10 !< number of detector layers
44  INTEGER, PARAMETER :: nmlyr=14 !< number of measurement layers
45  INTEGER, PARAMETER :: nmx=10 !< number of modules in x direction
46  INTEGER, PARAMETER :: nmy=5 !< number of modules in y direction
47  INTEGER, PARAMETER :: ntot=nlyr*nmx*nmy !< total number of modules
48  ! define detector geometry
49  REAL, PARAMETER :: dets= 10.0 !< arclength of first plane
50  REAL, PARAMETER :: diss= 10.0 !< distance between planes
51  REAL, PARAMETER :: thck= 0.02 !< thickness of plane (X0)
52  REAL, PARAMETER :: offs= 0.5 !< offset of stereo modules
53  REAL, PARAMETER :: stereo=0.08727 !< stereo angle
54  REAL, PARAMETER :: sizel= 20.0 !< size of layers
55  REAL, PARAMETER :: sigl =0.002 ! <resolution
56 
57  INTEGER :: nhits !< number of hits
58  REAL :: the0 !< multiple scattering error
59  INTEGER, DIMENSION(nmlyr) :: islyr !< (detector) layer
60  INTEGER, DIMENSION(nmlyr) :: ihits !< module number
61  REAL, DIMENSION(ntot) :: sdevx !< shift in x (alignment parameter)
62  REAL, DIMENSION(ntot) :: sdevy !< shift in y (alignment parameter)
63  REAL, DIMENSION(nmlyr) :: sarc !< arc length
64  REAL, DIMENSION(nmlyr) :: ssig !< resolution
65  REAL, DIMENSION(2,nmlyr) :: spro !< projection of measurent direction in (XY)
66  REAL, DIMENSION(nmlyr) :: xhits !< position perp. to plane (hit)
67  REAL, DIMENSION(nmlyr) :: yhits !< measured position in plane (hit)
68  REAL, DIMENSION(nmlyr) :: sigma !< measurement sigma (hit)
69 
70 END MODULE mptest2
71 
89 
90 SUBROUTINE mptst2(imodel) ! generate test files
91  USE mptest2
92  IMPLICIT NONE
93  REAL :: cmbbrl
94  REAL :: dispxm
95  REAL :: dispym
96  REAL :: dn
97  REAL :: dp
98  REAL :: gran
99  REAL :: one
100  REAL :: p
101  REAL :: s
102  REAL :: sgn
103  REAL :: sbrl
104  REAL :: sold
105  REAL :: uran
106  REAL :: wbrl
107  INTEGER :: i
108  INTEGER :: ibrl
109  INTEGER :: icount
110  INTEGER :: im
111  INTEGER :: ios
112  INTEGER :: ip
113  INTEGER :: j
114  INTEGER :: k
115  INTEGER :: l
116  INTEGER :: labelt
117  INTEGER :: layer
118  INTEGER :: lb
119  INTEGER :: lbrl
120  INTEGER :: luns
121  INTEGER :: lunt
122  INTEGER :: lyr
123  INTEGER :: nalc
124  INTEGER :: nbrl
125  INTEGER :: ncount
126  INTEGER :: ncx
127  INTEGER :: nmxy
128  INTEGER :: nrecds
129  INTEGER :: nthits
130 
131  INTEGER, INTENT(IN) :: imodel
132 
133  REAL :: derlc(nmlyr*2+3)
134  REAL :: dergl(nmlyr*2+3)
135  INTEGER :: label(2)
136  LOGICAL :: ex1
137  LOGICAL :: ex2
138  LOGICAL :: ex3
139  ! for broken lines: 1=fine, 2=coarse
140  dimension nbrl(2),lbrl(nmlyr,2),sbrl(nmlyr,2),wbrl(nmlyr,2), cmbbrl(2)
141  DATA cmbbrl / 0.0, 1.0 / ! cut for combining layers
142  ! ...
143  !CC CALL RNTIME
144  INQUIRE(file='mp2str.txt',iostat=ios,exist=ex1) ! keep, if existing
145  INQUIRE(file='mp2con.txt',iostat=ios,exist=ex2) ! keep, if existing
146 
147  INQUIRE(file='mp2tst.bin',iostat=ios,exist=ex3) ! remove, if existing
148 
149  WRITE(*,*) ' '
150  WRITE(*,*) 'Generating test data for mp II...'
151  WRITE(*,*) ' '
152  ! file management
153  IF(ex3) CALL system('rm mp2tst.bin') ! remove old file
154 
155  IF(.NOT.ex1) OPEN(unit=7,access='SEQUENTIAL',form='FORMATTED', &
156  file='mp2str.txt')
157  IF(.NOT.ex2) OPEN(unit=9,access='SEQUENTIAL',form='FORMATTED', &
158  file='mp2con.txt')
159  OPEN(unit=51,access='SEQUENTIAL',form='UNFORMATTED', file='mp2tst.bin')
160 
161  s=dets
162  i=0
163  sgn=1.0
164  DO layer=1,10
165  i=i+1
166  islyr(i)=layer ! layer
167  sarc(i)=s ! arclength
168  ssig(i)=sigl ! resolution
169  spro(1,i)=1.0 ! module measures 'X'
170  spro(2,i)=0.0
171  IF (mod(layer,3) == 1) THEN
172  i=i+1
173  islyr(i)=layer ! layer
174  sarc(i)=s+offs ! arclength stereo module
175  ssig(i)=sigl ! resolution
176  spro(1,i)=sqrt(1.0-stereo**2)
177  spro(2,i)=stereo*sgn ! module measures both 'X' and 'Y'
178  sgn=-sgn ! stereo orientation
179  END IF
180  s=s+diss
181  END DO
182 
183  ! define broken lines
184  sold=-1000.
185  nbrl(1)=0
186  nbrl(2)=0
187  DO k=1,2
188  DO i=1, nmlyr
189  IF (abs(sarc(i)-sold) > cmbbrl(k)) nbrl(k)=nbrl(k)+1
190  lb=nbrl(k)
191  lbrl(i,k)=lb
192  sbrl(lb,k)=sbrl(lb,k)+sarc(i)
193  wbrl(lb,k)=wbrl(lb,k)+1.0
194  sold=sarc(i)
195  END DO
196  DO i=1,nbrl(k)
197  sbrl(i,k)=sbrl(i,k)/wbrl(i,k)
198  wbrl(i,k)=sqrt(wbrl(i,k))
199  END DO
200  END DO
201  ibrl=imodel-2
202 
203  ! misalign detector modules -----------------------------------------
204 
205  dispxm=0.01 ! module displacement in X .05 mm * N(0,1)
206  dispym=0.01 ! module displacement in Y .05 mm * N(0,1)
207 
208  DO i=0,nlyr-1
209  DO k=0,nmy-1
210  DO l=1,nmx
211  sdevx(((i*nmy+k)*nmx+l))=dispxm*gran() ! shift in x
212  sdevy(((i*nmy+k)*nmx+l))=dispym*gran() ! shift in y
213  END DO
214  END DO
215  END DO
216  ! write text files -------------------------------------------------
217 
218  IF(.NOT.ex1) THEN
219  luns=7 ! steerfile
220  WRITE(luns,101) '* Default test steering file'
221  WRITE(luns,101) 'fortranfiles ! following bin files are fortran'
222  WRITE(luns,101) 'mp2con.txt ! constraints text file '
223  WRITE(luns,101) 'mp2tst.bin ! binary data file'
224  WRITE(luns,101) 'Cfiles ! following bin files are Cfiles'
225  ! WRITE(LUNS,101) '*outlierrejection 100.0 ! reject if Chi^2/Ndf >'
226  ! WRITE(LUNS,101) '*outliersuppression 3 ! 3 local_fit iterations'
227 
228  WRITE(luns,101) '*hugecut 50.0 !cut factor in iteration 0'
229  WRITE(luns,101) '*chisqcut 1.0 1.0 ! cut factor in iterations 1 and 2'
230  WRITE(luns,101) '*entries 10 ! lower limit on number of entries/parameter'
231  WRITE(luns,101) &
232  '*pairentries 10 ! lower limit on number of parameter pairs', &
233  ' ! (not yet!)'
234  WRITE(luns,101) '*printrecord 1 2 ! debug printout for records'
235  WRITE(luns,101) &
236  '*printrecord -1 -1 ! debug printout for bad data records'
237  WRITE(luns,101) &
238  '*outlierdownweighting 2 ! number of internal iterations (> 1)'
239  WRITE(luns,101) '*dwfractioncut 0.2 ! 0 < value < 0.5'
240  WRITE(luns,101) '*presigma 0.01 ! default value for presigma'
241  WRITE(luns,101) '*regularisation 1.0 ! regularisation factor'
242  WRITE(luns,101) '*regularisation 1.0 0.01 ! regularisation factor, pre-sigma'
243 
244  WRITE(luns,101) ' '
245  WRITE(luns,101) '*bandwidth 0 ! width of precond. band matrix'
246  WRITE(luns,101) 'method diagonalization 3 0.001 ! diagonalization '
247  WRITE(luns,101) 'method fullMINRES 3 0.01 ! minimal residual '
248  WRITE(luns,101) 'method sparseMINRES 3 0.01 ! minimal residual '
249  WRITE(luns,101) '*mrestol 1.0D-8 ! epsilon for MINRES'
250  WRITE(luns,101) 'method inversion 3 0.001 ! Gauss matrix inversion'
251  WRITE(luns,101) '* last method is applied'
252  WRITE(luns,101) '*matiter 3 ! recalculate matrix in iterations'
253  WRITE(luns,101) ' '
254  WRITE(luns,101) 'end ! optional for end-of-data'
255  END IF
256 
257  ! constraints: fix center modules in first/last layer
258 
259  ncx=(nmx+1)/2
260  nmxy=nmx*nmy
261  lunt=9
262  one=1.0
263  DO i=1,nlyr,nlyr-1
264  IF(.NOT.ex2) WRITE(lunt,*) 'Constraint 0.0'
265  DO k=0,nmy-1
266  labelt=(i*nmy+k)*nmx+ncx-1
267  IF(.NOT.ex2) WRITE(lunt,103) labelt,one
268  sdevx(((i-1)*nmy+k)*nmx+ncx)=0.0 ! fix center modules at 0.
269  END DO
270  IF(.NOT.ex2) WRITE(lunt,*) 'Constraint 0.0'
271  DO k=0,nmy-1
272  labelt=(i*nmy+k)*nmx+ncx+1000-1
273  IF(.NOT.ex2) WRITE(lunt,103) labelt,one
274  sdevy(((i-1)*nmy+k)*nmx+ncx)=0.0 ! fix center modules at 0.
275  END DO
276  END DO
277 
278  ! record loop ------------------------------------------------------
279 
280  ncount=10000
281  nthits=0
282  nrecds=0
283 
284  DO icount=1,ncount
285  ! 10..100 GeV
286  p=10.0**(1.+uran())
287  the0=sqrt(thck)*0.014/p
288  ip=0
289  ! IF (ICOUNT.LE.3) IP=1
290  CALL genln2(ip) ! generate hits
291 
292 
293  DO i=1,nhits
294  ! simple straight line
295  lyr=ihits(i)/nmxy+1
296  im =mod(ihits(i),nmxy)
297  nalc=4
298  derlc(1)=spro(1,lyr)
299  derlc(2)=spro(2,lyr)
300  derlc(3)=xhits(i)*spro(1,lyr)
301  derlc(4)=xhits(i)*spro(2,lyr)
302  dergl(1)=spro(1,lyr)
303  dergl(2)=spro(2,lyr)
304  label(1)=im+nmxy*islyr(lyr)
305  label(2)=im+nmxy*islyr(lyr)+1000
306  ! add multiple scattering errors (no correlations)
307  IF (imodel == 1) THEN
308  DO j=i,nhits
309  sigma(j)=sqrt(sigma(j)**2+((xhits(j)-xhits(i))*the0)**2)
310  END DO
311  END IF
312  ! add 'break points' for multiple scattering
313  IF (imodel == 2.AND.i > 1) THEN
314  DO j=1,i-1
315  ! 2 scattering angles from each layer in front of current
316  nalc=nalc+1
317  derlc(nalc)=(xhits(i)-xhits(j))*spro(1,lyr)
318  nalc=nalc+1
319  derlc(nalc)=(xhits(i)-xhits(j))*spro(2,lyr)
320  END DO
321  END IF
322  ! add 'broken lines' offsets for multiple scattering
323  IF (imodel >= 3) THEN
324  nalc=2*nbrl(ibrl)
325  DO k=1, nalc
326  derlc(k)=0.0
327  END DO
328  ! 2 offsets
329  lb=lbrl(lyr,ibrl)
330  derlc(lb*2-1)=spro(1,lyr)
331  derlc(lb*2 )=spro(2,lyr)
332  END IF
333 
334  CALL mille(nalc,derlc,2,dergl,label,yhits(i),sigma(i))
335  nthits=nthits+1 ! count hits
336  END DO
337  ! additional measurements from MS
338  IF (imodel == 2) THEN
339  DO i=1,(nhits-1)*2
340  nalc=i+4
341  DO k=1,nalc
342  derlc(k)=0.0
343  END DO
344  derlc(nalc)=1.0
345  CALL mille(nalc,derlc,0,dergl,label,0.0,the0)
346  END DO
347  END IF
348 
349  IF (imodel >= 3) THEN
350  DO i=2,nbrl(ibrl)-1
351  dp=1.0/(sbrl(i,ibrl)-sbrl(i-1,ibrl))
352  dn=1.0/(sbrl(i+1,ibrl)-sbrl(i,ibrl))
353  nalc=(i+1)*2
354  DO l=-1,0
355  DO k=1,nalc
356  derlc(k)=0.0
357  END DO
358  derlc(2*(i-1)+l)= dp
359  derlc(2* i +l)=-dp-dn
360  derlc(2*(i+1)+l)= dn
361  CALL mille(nalc,derlc,0,dergl,label,0.0,the0*wbrl(i,ibrl))
362  END DO
363  END DO
364  END IF
365 
366  CALL endle
367  nrecds=nrecds+1 ! count records
368  END DO
369 
370  ! ------------------------------------------------------------------
371  IF(.NOT.ex1) THEN
372  rewind(7)
373  CLOSE (7)
374  END IF
375  IF(.NOT.ex2) THEN
376  rewind(9)
377  CLOSE (9)
378  END IF
379  rewind(51)
380  CLOSE (51)
381 
382  ! WRITE(*,*) ' '
383  ! WRITE(*,*) 'Shifts and drift velocity deviations:'
384  ! DO I=1,NPLAN
385  ! WRITE(*,102) I,DEL(I),DVD(I)
386  ! END DO
387 
388 
389  WRITE(*,*) ' '
390  WRITE(*,*) ' '
391  WRITE(*,*) ncount,' tracks generated with ',nthits,' hits.'
392  WRITE(*,*) nrecds,' records written.'
393  WRITE(*,*) ' '
394 101 format(a)
395  ! 102 FORMAT(I6,2F10.5)
396 103 format(i8,f10.5)
397 END SUBROUTINE mptst2
398 
402 
403 SUBROUTINE genln2(ip)
404  USE mptest2
405 
406  IMPLICIT NONE
407  REAL:: ds
408  REAL:: dx
409  REAL:: dy
410  REAL:: gran
411  INTEGER:: i
412  INTEGER:: ihit
413  INTEGER:: imx
414  INTEGER:: imy
415  INTEGER:: ioff
416  REAL:: sold
417  REAL:: uran
418  REAL:: x
419  REAL:: xexit
420  REAL:: xl
421  REAL:: xnull
422  REAL:: xs
423  REAL:: xslop
424  REAL:: y
425  REAL:: yexit
426  REAL:: yl
427  REAL:: ynull
428  REAL:: ys
429  REAL:: yslop
430 
431 
432  INTEGER, INTENT(IN) :: ip
433 
434  ! track parameters
435  xnull=sizel*(uran()-0.5) ! uniform vertex
436  ynull=sizel*(uran()-0.5) ! uniform vertex
437  xexit=sizel*(uran()-0.5) ! uniform exit point
438  yexit=sizel*(uran()-0.5) ! uniform exit point
439  xslop=(xexit-xnull)/sarc(nmlyr)
440  yslop=(yexit-ynull)/sarc(nmlyr)
441  IF(ip /= 0) THEN
442  WRITE(*,*) ' '
443  WRITE(*,*) ' Track ', xnull, ynull, xslop, yslop
444  END IF
445 
446  nhits=0
447  x=xnull
448  y=ynull
449  dx=xslop
450  dy=yslop
451  sold=0.0
452 
453  DO i=1,nmlyr
454  ds=sarc(i)-sold
455  sold=sarc(i)
456  ! position with parameters 1. hit
457  xs=xnull+sarc(i)*xslop
458  ys=ynull+sarc(i)*yslop
459  ! true track position
460  x=x+dx*ds
461  y=y+dy*ds
462  ! multiple scattering
463  dx=dx+gran()*the0
464  dy=dy+gran()*the0
465 
466  imx=ifix((x+sizel*0.5)/sizel*float(nmx))
467  IF (imx < 0.OR.imx >= nmx) cycle
468  imy=ifix((y+sizel*0.5)/sizel*float(nmy))
469  IF (imy < 0.OR.imy >= nmy) cycle
470 
471  ihit=((i-1)*nmy+imy)*nmx+imx
472  ioff=((islyr(i)-1)*nmy+imy)*nmx+imx+1
473  nhits=nhits+1
474  ihits(nhits)=ihit
475  xl=x-sdevx(ioff)
476  yl=y-sdevy(ioff)
477  xhits(nhits)=sarc(i)
478  yhits(nhits)=(xl-xs)*spro(1,i)+(yl-ys)*spro(2,i)+gran()*ssig(i)
479  sigma(nhits)=ssig(i)
480 
481  IF(ip /= 0) THEN
482  WRITE(*,101) nhits,i,ihit,x,y,xhits(nhits), yhits(nhits),sigma(nhits)
483  END IF
484  END DO
485 101 format(3i3,5f8.4)
486 END SUBROUTINE genln2
487