~maddevelopers/mg5amcnlo/new_clustering

« back to all changes in this revision

Viewing changes to Template/NLO/SubProcesses/mint-integrator2.f

  • Committer: Rikkert Frederix
  • Date: 2021-09-09 15:51:40 UTC
  • mfrom: (78.75.502 3.2.1)
  • Revision ID: frederix@physik.uzh.ch-20210909155140-rg6umfq68h6h47cf
merge with 3.2.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
2
 
c MINT Integrator Package
3
 
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
4
 
c Original version by Paolo Nason (for POWHEG (BOX))
5
 
c Modified by Rikkert Frederix (for aMC@NLO)
6
 
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
7
 
c      subroutine mint(fun,ndim,ncalls0,nitmax,imode,
8
 
c ndim=number of dimensions
9
 
c ncalls0=# of calls per iteration
10
 
c nitmax =# of iterations
11
 
c fun(xx,www,ifirst): returns the function to be integrated multiplied by www;
12
 
c                     xx(1:ndim) are the variables of integration
13
 
c                     ifirst=0: normal behaviour
14
 
c imode: integer flag
15
 
c
16
 
c imode=-1:
17
 
c same as imode=0 as far as this routine is concerned, except for the
18
 
c fact that a grid is read at the beginning (rather than initialized).
19
 
c The return value of imode will be zero.
20
 
c
21
 
c imode=0:
22
 
c When called with imode=0 the routine integrates the absolute value of
23
 
c the function and sets up a grid xgrid(0:50,ndim) such that in each
24
 
c ndim-1 dimensional slice (i.e. xgrid(m-1,n)<xx(n)<xgrid(m,n)) the
25
 
c contribution of the integral is the same the array xgrid is setup at
26
 
c this stage; ans and err are the integral and its error
27
 
c
28
 
c imode=1 (in fact #0)
29
 
c When called with imode=1, the routine performs the integral of the
30
 
c function fun using the grid xgrid. If some number in the array ifold,
31
 
c (say, ifold(n)) is different from 1, it must be a divisor of 50, and
32
 
c the 50 intervals xgrid(0:50,n) are grouped into ifold(n) groups, each
33
 
c group containing 50/ifold(n) nearby intervals. For example, if
34
 
c ifold(1)=5, the 50 intervals for the first dimension are divided in 5
35
 
c groups of 10. The integral is then performed by folding on top of each
36
 
c other these 5 groups. Suppose, for example, that we choose a random
37
 
c point in xx(1) = xgrid(2,1)+x*(xgrid(3,1)-xgrid(2,1)), in the group of
38
 
c the first 5 interval.  we sum the contribution of this point to the
39
 
c contributions of points
40
 
c xgrid(2+m*10,1)+x*(xgrid(3+m*10,1)-xgrid(2+m*10,1)), with m=1,...,4.
41
 
c In the sequence of calls to the function fun, the call for the first
42
 
c point is performed with ifirst=0, and that for all subsequent points
43
 
c with ifirst=1, so that the function can avoid to compute quantities
44
 
c that only depend upon dimensions that have ifold=1, and do not change
45
 
c in each group of folded call. The values returned by fun in a sequence
46
 
c of folded calls with ifirst=0 and ifirst=1 are not used. The function
47
 
c itself must accumulate the values, and must return them when called
48
 
c with ifirst=2.
49
 
50
 
c Added the posibility to keep track of more than one integral:
51
 
 
52
 
c nintegrals=1 : the function that is used to update the grids. This is
53
 
c the ABS cross section. If imode.eq.1, this does not contain the
54
 
c virtual corrections because for them a separate maximum is kept using (5).
55
 
c nintegrals=2 : the actual cross section. This includes virtual corrections.
56
 
c nintegrals=3 : the cross section from the M_Virt/M_Born ratio alone:
57
 
c this defines the average virtual that is added to each phase-space
58
 
c point
59
 
c nintegrals=4 : the cross section of the actual virtual minus the
60
 
c average virtual. This is used to determine the fraction of phase-space
61
 
c points for which we include the virtual.
62
 
c nintegrals=5 : abs of 3
63
 
c nintegrals=6 : born*alpha_S/2Pi
64
 
c
65
 
      subroutine mint(fun,ndim,ncalls0,nitmax,imode,xgrid,ymax,ymax_virt
66
 
     $     ,ans,unc,chi2,nhits_in_grids)
67
 
c imode= 0: integrate and adapt the grid
68
 
c imode= 1: frozen grid, compute the integral and the upper bounds
69
 
c imode=-1: same as imode=0, but use previously generated grids
70
 
c others: same as 1 (for now)
71
 
      implicit none
72
 
      include "mint.inc"
73
 
      include "FKSParams.inc"
74
 
      integer i,j,ncalls0,ndim,nitmax,imode
75
 
      real * 8 fun,xgrid(0:nintervals,ndimmax,maxchannels)
76
 
     $     ,xint(maxchannels),ymax(nintervals,ndimmax,maxchannels)
77
 
     $     ,ans(nintegrals,0:maxchannels),unc(nintegrals,0:maxchannels)
78
 
     $     ,ans3(nintegrals,3),unc3(nintegrals,3),ans_l3(nintegrals)
79
 
     $     ,unc_l3(nintegrals),chi2_l3(nintegrals),vol_chan
80
 
      real * 8 xint_virt(maxchannels),ymax_virt(0:maxchannels)
81
 
     $     ,ans_chan(0:maxchannels)
82
 
      real * 8 x(ndimmax),vol
83
 
      real * 8 xacc(0:nintervals,ndimmax,maxchannels)
84
 
      integer icell(ndimmax),ncell(ndimmax),ncell_virt
85
 
      integer ifold(ndimmax),kfold(ndimmax)
86
 
      common/cifold/ifold
87
 
      integer nhits(nintervals,ndimmax,maxchannels)
88
 
     $     ,nhits_in_grids(maxchannels)
89
 
      real * 8 rand(ndimmax),HwU_values(2)
90
 
      real * 8 dx(ndimmax),f(nintegrals),vtot(nintegrals,0:maxchannels)
91
 
     $     ,etot(nintegrals,0:maxchannels),prod,f1(nintegrals)
92
 
     $     ,chi2(nintegrals,0:maxchannels),efrac(nintegrals),dummy
93
 
      integer kdim,kint,kpoint,nit,ncalls,iret,nintcurr,nintcurr_virt
94
 
     $     ,ifirst,nit_included,kpoint_iter,non_zero_point(nintegrals)
95
 
     $     ,ntotcalls(nintegrals),nint_used,nint_used_virt,min_it,np
96
 
      real * 8 ran3
97
 
      external ran3,fun
98
 
      logical even,double_events,bad_iteration,regridded(maxchannels)
99
 
     $     ,reset
100
 
      double precision average_virtual(maxchannels)
101
 
     $     ,virtual_fraction(maxchannels)
102
 
      common/c_avg_virt/average_virtual,virtual_fraction
103
 
      character*13 title(nintegrals)
104
 
      logical new_point
105
 
      common /c_new_point/ new_point
106
 
      data title(1)/'ABS integral '/
107
 
      data title(2)/'Integral     '/
108
 
      data title(3)/'Virtual      '/
109
 
      data title(4)/'Virtual ratio'/
110
 
      data title(5)/'ABS virtual  '/
111
 
      data title(6)/'Born*ao2pi   '/
112
 
c APPLgrid switch
113
 
      integer iappl
114
 
      common /for_applgrid/ iappl
115
 
      logical              fixed_order,nlo_ps
116
 
      common /c_fnlo_nlops/fixed_order,nlo_ps
117
 
      integer                                   npoints
118
 
      double precision            cross_section
119
 
      common /for_FixedOrder_lhe/ cross_section,npoints
120
 
      reset=.false.
121
 
 
122
 
c if ncalls0 is greater than 0, use the default running, i.e. do not
123
 
c double the events after each iteration as well as use a fixed number
124
 
c of intervals in the grids.
125
 
      if (ncalls0.gt.0) then
126
 
         double_events=.false.
127
 
         nint_used=nintervals
128
 
         nint_used_virt=nintervals_virt
129
 
      else
130
 
c if ncalls0.le.0, reset it and double the events per iteration
131
 
         ncalls0=80*ndim*(nchans/3+1)
132
 
         double_events=.true.
133
 
         if (imode.eq.1 .or. imode.eq.-1) then
134
 
            nint_used=nintervals
135
 
            nint_used_virt=nintervals_virt
136
 
         else
137
 
            nint_used=min_inter
138
 
            nint_used_virt=min_inter
139
 
         endif
140
 
      endif
141
 
      bad_iteration=.false.
142
 
c
143
 
      ncalls=0  ! # PS points (updated below)
144
 
      if(imode.eq.-1) then
145
 
c Grids read from file
146
 
         even=.true.
147
 
         imode=0
148
 
         min_it=min_it0
149
 
         do kdim=1,ndim
150
 
            ifold(kdim)=1
151
 
         enddo
152
 
         ans_chan(0)=0d0
153
 
         do kchan=1,nchans
154
 
            ans_chan(kchan)=ans(1,kchan)
155
 
            ans_chan(0)=ans_chan(0)+ans(1,kchan)
156
 
         enddo
157
 
      elseif(imode.eq.0) then
158
 
c Initialize grids
159
 
         even=.true.
160
 
         min_it=min_it0
161
 
         do kdim=1,ndim
162
 
            ifold(kdim)=1
163
 
            do kchan=1,nchans
164
 
               do kint=0,nint_used
165
 
                  xgrid(kint,kdim,kchan)=dble(kint)/nint_used
166
 
                  if(kint.gt.0) then
167
 
                     nhits(kint,kdim,kchan)=0
168
 
                  endif
169
 
               enddo
170
 
               regridded(kchan)=.true.
171
 
            enddo
172
 
         enddo
173
 
         do kchan=1,maxchannels
174
 
            nhits_in_grids(kchan)=0
175
 
         enddo
176
 
         call init_ave_virt(nint_used_virt,ndim)
177
 
         do kchan=0,nchans
178
 
            ans_chan(kchan)=0d0
179
 
         enddo
180
 
         if (double_events) then
181
 
c           when double events, start with the very first channel
182
 
c           only. For the first iteration, we compute each channel
183
 
c           separately.
184
 
            ans_chan(0)=1d0
185
 
            ans_chan(1)=1d0
186
 
            ncalls0=ncalls0/nchans
187
 
         endif
188
 
      elseif(imode.eq.1) then
189
 
c Initialize upper bounding envelope
190
 
         do kchan=1,nchans
191
 
            xint(kchan)=ans(1,kchan)
192
 
            xint_virt(kchan)=ans(5,kchan)
193
 
         enddo
194
 
         even=.false.
195
 
         min_it=min_it1
196
 
         do kdim=1,ndim
197
 
            nintcurr=nint_used/ifold(kdim)
198
 
            nintcurr_virt=nint_used_virt/ifold(kdim)
199
 
            if(nintcurr*ifold(kdim).ne.nint_used .or.
200
 
     &         nintcurr_virt*ifold(kdim).ne.nint_used_virt) then
201
 
               write(*,*) 'mint: the values in the ifold array'/
202
 
     $              /'shoud be divisors of',nint_used,'and'
203
 
     $              ,nint_used_virt
204
 
               stop
205
 
            endif
206
 
            do kchan=1,nchans
207
 
               do kint=1,nintcurr
208
 
                  ymax(kint,kdim,kchan)=xint(kchan)**(1d0/ndim)
209
 
               enddo
210
 
            enddo
211
 
         enddo
212
 
         do kchan=1,nchans
213
 
            ymax_virt(kchan)=xint_virt(kchan)
214
 
         enddo
215
 
         ans_chan(0)=0d0
216
 
         do kchan=1,nchans
217
 
            ans_chan(kchan)=ans(1,kchan)
218
 
            ans_chan(0)=ans_chan(0)+ans(1,kchan)
219
 
         enddo
220
 
      endif
221
 
      cross_section=ans_chan(0)
222
 
      nit=0
223
 
      nit_included=0
224
 
      do i=1,nintegrals
225
 
         do kchan=0,nchans
226
 
            ans(i,kchan)=0d0
227
 
            unc(i,kchan)=0d0
228
 
         enddo
229
 
         do j=1,3
230
 
            ans3(i,j)=0d0
231
 
            unc3(i,j)=0d0
232
 
         enddo
233
 
      enddo
234
 
      do i=1,2
235
 
         HwU_values(i)=0d0
236
 
      enddo
237
 
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
238
 
c Main loop over the iterations
239
 
 10   continue
240
 
      do kchan=1,nchans
241
 
         np=0
242
 
         do kint=1,nint_used
243
 
            np=np+nhits(kint,1,kchan)
244
 
         enddo
245
 
         write (*,250) 'channel',kchan,':',iconfigs(kchan)
246
 
     $        ,regridded(kchan),np,nhits_in_grids(kchan),ans_chan(kchan)
247
 
     $        ,ans(2,kchan),virtual_fraction(kchan)
248
 
      enddo
249
 
      call flush(6)
250
 
      if(nit.ge.nitmax) then
251
 
c We did enough iterations, update arguments and return
252
 
         do kchan=0,nchans
253
 
            if (nit_included.ge.2) then
254
 
               chi2(1,kchan)=chi2(1,kchan)/dble(nit_included-1)
255
 
            else
256
 
               chi2(1,kchan)=0d0
257
 
            endif
258
 
         enddo
259
 
         write (*,*) '-------'
260
 
         ncalls0=ncalls*kpoint_iter ! return number of points used
261
 
         if (double_events) then
262
 
            nitmax=2
263
 
         else
264
 
            nitmax=nit_included
265
 
         endif
266
 
         cross_section=ans(2,0)
267
 
         do kchan=1,nchans
268
 
            if (regridded(kchan)) then
269
 
               np=0
270
 
               do kint=1,nint_used
271
 
                  np=np+nhits(kint,1,kchan)
272
 
               enddo
273
 
               nhits_in_grids(kchan)=np ! set equal to number of points
274
 
                                        ! used for last update
275
 
            endif
276
 
         enddo
277
 
         return
278
 
      endif
279
 
      nit=nit+1
280
 
      write (*,*) '------- iteration',nit
281
 
      if (even .and. ncalls.ne.ncalls0) then
282
 
c Uses more evenly distributed random numbers. This overwrites the
283
 
c number of calls
284
 
         call initialize_even_random_numbers(ncalls0,ndim,ncalls)
285
 
         write (*,*) 'Update # PS points (even): ',ncalls0,' --> '
286
 
     &        ,ncalls
287
 
      elseif (ncalls0.ne.ncalls) then
288
 
         ncalls=ncalls0
289
 
         write (*,*) 'Update # PS points: ',ncalls0,' --> ',ncalls
290
 
      endif
291
 
      npoints=ncalls
292
 
c Reset the accumulated results for grid updating
293
 
      if(imode.eq.0) then
294
 
         do kchan=1,nchans
295
 
            if (regridded(kchan).or.reset) then ! only reset if grids were 
296
 
                                                ! updated (or forced reset)
297
 
               if (regridded(kchan) .and. .not. reset) then
298
 
                  np=0
299
 
                  do kint=1,nint_used
300
 
                     np=np+nhits(kint,1,kchan)
301
 
                  enddo
302
 
                  nhits_in_grids(kchan)=np ! set equal to number of
303
 
                                           ! points used for last update
304
 
               elseif (regridded(kchan) .and. reset) then
305
 
                  nhits_in_grids(kchan)=0
306
 
               endif
307
 
               do kdim=1,ndim
308
 
                  do kint=0,nint_used
309
 
                     xacc(kint,kdim,kchan)=0
310
 
                     if(kint.gt.0) then
311
 
                        nhits(kint,kdim,kchan)=0
312
 
                     endif
313
 
                  enddo
314
 
               enddo
315
 
            endif
316
 
         enddo
317
 
         reset=.false.
318
 
      endif
319
 
      do kchan=0,nchans
320
 
         do i=1,nintegrals
321
 
            vtot(i,kchan)=0
322
 
            etot(i,kchan)=0
323
 
         enddo
324
 
      enddo
325
 
      kpoint_iter=0
326
 
      do i=1,nintegrals
327
 
         non_zero_point(i)=0
328
 
      enddo
329
 
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
330
 
c Loop over PS points
331
 
 2    kpoint_iter=kpoint_iter+1
332
 
      do kpoint=1,ncalls
333
 
         new_point=.true.
334
 
c first determine which integration channel we want to pick this point from         
335
 
         call get_channel(ans_chan,vol_chan)
336
 
c find random x, and its random cell
337
 
         do kdim=1,ndim
338
 
            kfold(kdim)=1
339
 
c if(even), we should compute the ncell and the rand from the ran3()
340
 
            if (even) then
341
 
               rand(kdim)=ran3(even)
342
 
               ncell(kdim)= min(int(rand(kdim)*nint_used)+1,
343
 
     &              nint_used)
344
 
               rand(kdim)=rand(kdim)*nint_used-(ncell(kdim)-1)
345
 
            else
346
 
               ncell(kdim)=min(int(nint_used/ifold(kdim)*ran3(even))+1,
347
 
     &              nint_used)
348
 
               rand(kdim)=ran3(even)
349
 
            endif
350
 
         enddo
351
 
         do i=1,nintegrals
352
 
            f(i)=0
353
 
         enddo
354
 
         ifirst=0
355
 
 1       continue
356
 
         vol=1d0/vol_chan * wgt_mult
357
 
c c convert 'flat x' ('rand') to 'vegas x' ('x') and include jacobian ('vol')
358
 
         do kdim=1,ndim
359
 
            nintcurr=nint_used/ifold(kdim)
360
 
            icell(kdim)=ncell(kdim)+(kfold(kdim)-1)*nintcurr
361
 
            dx(kdim)=xgrid(icell(kdim),kdim,ichan)-xgrid(icell(kdim)-1
362
 
     $           ,kdim,ichan)
363
 
            vol=vol*dx(kdim)*nintcurr
364
 
            x(kdim)=xgrid(icell(kdim)-1,kdim,ichan)+rand(kdim)*dx(kdim)
365
 
            if(imode.eq.0) nhits(icell(kdim),kdim,ichan)=
366
 
     $           nhits(icell(kdim),kdim,ichan)+1
367
 
         enddo
368
 
         call get_ave_virt(x,nint_used_virt,ndim,average_virtual)
369
 
c contribution to integral
370
 
         if(imode.eq.0) then
371
 
            dummy=fun(x,vol,ifirst,f1)
372
 
            do i=1,nintegrals
373
 
               f(i)=f(i)+f1(i)
374
 
            enddo
375
 
         else
376
 
c this accumulated value will not be used
377
 
            dummy=fun(x,vol,ifirst,f1)
378
 
            do i=1,nintegrals
379
 
               f(i)=f(i)+f1(i)
380
 
            enddo
381
 
            ifirst=1
382
 
            call nextlexi(ndim,ifold,kfold,iret)
383
 
            if(iret.eq.0) goto 1
384
 
c closing call: accumulated value with correct sign
385
 
            dummy=fun(x,vol,2,f1)
386
 
            do i=1,nintegrals
387
 
               f(i)=f1(i)
388
 
            enddo
389
 
         endif
390
 
c
391
 
         if(imode.eq.0) then
392
 
c accumulate the function in xacc(icell(kdim),kdim) to adjust the grid later
393
 
            do kdim=1,ndim
394
 
               xacc(icell(kdim),kdim,ichan)= 
395
 
     $              xacc(icell(kdim),kdim,ichan) + f(1)
396
 
            enddo
397
 
c Set the Born contribution (to compute the average_virtual) to zero if
398
 
c the virtual was not computed for this phase-space point. Compensate by
399
 
c including the virtual_fraction.
400
 
            if (f(3).ne.0d0) then
401
 
               f(6)=f(6)/virtual_fraction(ichan)
402
 
               call fill_ave_virt(x,nint_used_virt,ndim,f(3),f(6))
403
 
            else
404
 
               f(6)=0d0
405
 
            endif
406
 
         else
407
 
c update the upper bounding envelope total rate
408
 
            prod=1d0
409
 
            do kdim=1,ndim
410
 
               prod=prod*ymax(ncell(kdim),kdim,ichan)
411
 
            enddo
412
 
            prod=(f(1)/prod)
413
 
            if (prod.gt.1d0) then
414
 
c Weight for this PS point is larger than current upper bound. Increase
415
 
c the bound so that it is equal to the current max weight.  If the new
416
 
c point is more than twice as large as current upper bound, increase
417
 
c bound by factor 2 only to prevent a single unstable points to
418
 
c completely screw up the efficiency
419
 
               prod=min(2d0,prod)
420
 
               prod=prod**(1d0/dble(ndim))
421
 
               do kdim=1,ndim
422
 
                  ymax(ncell(kdim),kdim,ichan)= 
423
 
     $                 ymax(ncell(kdim),kdim,ichan)*prod
424
 
               enddo
425
 
            endif
426
 
c Update the upper bounding envelope virtual. Do not include the
427
 
c enhancement due to the virtual_fraction. (And again limit by factor 2
428
 
c at most).
429
 
            if (f(5)*virtual_fraction(ichan).gt.ymax_virt(ichan))
430
 
     $           ymax_virt(ichan)=min(f(5)*virtual_fraction(ichan)
431
 
     $           ,ymax_virt(ichan)*2d0)
432
 
c for consistent printing in the log files (in particular when doing LO
433
 
c runs), set also f(6) to zero when imode.eq.1 and the virtuals are not
434
 
c included.
435
 
            if (f(3).eq.0) f(6)=0d0
436
 
         endif
437
 
         do i=1,nintegrals
438
 
            if (f(i).ne.0d0) non_zero_point(i)=non_zero_point(i)+1
439
 
         enddo
440
 
c Add the PS point to the result of this iteration
441
 
         do i=1,nintegrals
442
 
            vtot(i,ichan)=vtot(i,ichan)+f(i)
443
 
            etot(i,ichan)=etot(i,ichan)+f(i)**2
444
 
         enddo
445
 
         if (f(1).ne.0d0) call HwU_add_points
446
 
      enddo
447
 
      do i=1,nintegrals
448
 
c Number of phase-space points used
449
 
         ntotcalls(i)=ncalls*kpoint_iter
450
 
c Special for the computation of the 'computed virtual'
451
 
         if (i.eq.4 .and. non_zero_point(i).ne.0 )
452
 
     &        ntotcalls(i) = non_zero_point(i)
453
 
      enddo
454
 
      if (ntotcalls(1).gt.max_points .and. non_zero_point(1).lt.25 .and.
455
 
     &     double_events) then
456
 
         write (*,*) 'ERROR: INTEGRAL APPEARS TO BE ZERO.'
457
 
         write (*,*) 'TRIED',ntotcalls(1),'PS POINTS AND ONLY '
458
 
     &        ,non_zero_point(1),' GAVE A NON-ZERO INTEGRAND.'
459
 
         stop 1
460
 
      endif
461
 
c Goto beginning of loop over PS points until enough points have found
462
 
c that pass cuts.
463
 
      if (non_zero_point(1).lt.int(0.99*ncalls)
464
 
     &                        .and. double_events) goto 2
465
 
c This is the loop over all the channels for the very first iteration.
466
 
      if (imode.eq.0 .and. nit.eq.1 .and. double_events) then
467
 
         do kchan=nchans,1,-1
468
 
            if (ans_chan(kchan).eq.1d0) then
469
 
               do i=1,nintegrals
470
 
                  vtot(i,kchan)=vtot(i,kchan)/dble(ntotcalls(i))
471
 
                  etot(i,kchan)=etot(i,kchan)/dble(ntotcalls(i))
472
 
                  etot(i,kchan)=sqrt(abs(etot(i,kchan)-vtot(i,kchan)**2)
473
 
     $                 /dble(ntotcalls(i)))
474
 
               enddo
475
 
               if (kchan.eq.nchans) exit
476
 
               ans_chan(kchan)=0d0
477
 
               ans_chan(kchan+1)=1d0
478
 
               do i=1,nintegrals
479
 
                  ntotcalls(i)=0
480
 
                  non_zero_point(i)=0
481
 
               enddo
482
 
               kpoint_iter=0
483
 
               goto 2
484
 
            endif
485
 
         enddo
486
 
c set the total result for the first iteration as the sum over all the channels
487
 
         do i=1,nintegrals
488
 
            do kchan=1,nchans
489
 
               vtot(i,0)=vtot(i,0)+vtot(i,kchan)
490
 
               etot(i,0)=etot(i,0)+etot(i,kchan)**2
491
 
            enddo
492
 
            etot(i,0)=sqrt(etot(i,0))
493
 
         enddo
494
 
         ncalls0=ncalls0*nchans
495
 
      endif
496
 
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
497
 
c Iteration done. Update the accumulated results and print them to the
498
 
c screen
499
 
      do i=1,nintegrals
500
 
         if (imode.eq.0 .and. nit.eq.1 .and. double_events) then
501
 
            continue
502
 
         else
503
 
            do kchan=nchans,0,-1
504
 
               if (kchan.ne.0) then
505
 
                  vtot(i,0)=vtot(i,0)+vtot(i,kchan)
506
 
                  etot(i,0)=etot(i,0)+etot(i,kchan)
507
 
               endif
508
 
               vtot(i,kchan)=vtot(i,kchan)/dble(ntotcalls(i))
509
 
               etot(i,kchan)=etot(i,kchan)/dble(ntotcalls(i))
510
 
c the abs is to avoid tiny negative values
511
 
               etot(i,kchan)=sqrt(abs(etot(i,kchan)-vtot(i,kchan)**2)
512
 
     $              /dble(ntotcalls(i)))
513
 
            enddo
514
 
         endif
515
 
         if (vtot(i,0).ne.0d0) then
516
 
            efrac(i)=abs(etot(i,0)/vtot(i,0))
517
 
         else
518
 
            efrac(i)=0d0
519
 
         endif
520
 
      enddo
521
 
      do i=1,nintegrals
522
 
         write(*,'(a,1x,e10.4,1x,a,1x,e10.4,1x,a,1x,f7.3,1x,a)')
523
 
     $        title(i)//' =',vtot(i,0),' +/- ',etot(i,0),' (',efrac(i)
524
 
     $        *100d0 ,'%)'
525
 
      enddo
526
 
C If there was a large fluctation in this iteration, be careful with
527
 
C including it in the accumalated results and plots.
528
 
      if (efrac(1).gt.0.3d0 .and. iappl.eq.0 .and. nit.gt.3) then
529
 
c Do not include the results in the plots
530
 
         if (fixed_order) call accum(.false.)
531
 
         if (fixed_order) call HwU_accum_iter(.false.,ntotcalls(1)
532
 
     $        ,HwU_values)
533
 
      endif
534
 
      if (efrac(1).gt.0.3d0 .and. nit.gt.3 .and. iappl.eq.0) then
535
 
c Do not include the results in the updating of the grids.
536
 
         write (*,*) 'Large fluctuation ( >30 % ).'
537
 
     &        //'Not including iteration in results.'
538
 
c empty the accumulated results in the MC over integers
539
 
         call empty_MC_integer
540
 
c empty the accumalated results for the MINT grids
541
 
         if (imode.eq.0) then
542
 
c emptying accum. results is done above when the iteration starts
543
 
            reset=.true.
544
 
         elseif (imode.eq.1) then
545
 
c Cannot really skip the increase of the upper bounding envelope. So,
546
 
c simply continue here. Note that no matter how large the integrand for
547
 
c the PS point, the upper bounding envelope is at most increased by a
548
 
c factor 2, so this should be fine.
549
 
            continue
550
 
         endif
551
 
c double the number of points for the next iteration
552
 
         if (double_events) ncalls0=ncalls0*2
553
 
         if (bad_iteration .and. imode.eq.0 .and. double_events) then
554
 
c 2nd bad iteration is a row. Reset grids
555
 
            write (*,*)'2nd bad iteration in a row. '/
556
 
     &           /'Resetting grids and starting from scratch...'
557
 
            if (double_events) then
558
 
               if (imode.eq.0) nint_used=min_inter ! reset number of intervals
559
 
               ncalls0=ncalls0/8   ! Start with larger number
560
 
            endif
561
 
            nit=0
562
 
            nit_included=0
563
 
c Reset the MINT grids
564
 
            if (imode.eq.0) then
565
 
               do kchan=1,nchans
566
 
                  do kdim=1,ndim
567
 
                     do kint=0,nint_used
568
 
                        xgrid(kint,kdim,kchan)=dble(kint)/nint_used
569
 
                     enddo
570
 
                     regridded(kchan)=.true.
571
 
                  enddo
572
 
               enddo
573
 
               do kchan=1,maxchannels
574
 
                  nhits_in_grids(kchan)=0
575
 
               enddo
576
 
               call init_ave_virt(nint_used_virt,ndim)
577
 
               do kchan=0,nchans
578
 
                  ans_chan(kchan)=0d0
579
 
               enddo
580
 
               if (double_events) then
581
 
                  ans_chan(0)=1d0
582
 
                  ans_chan(1)=1d0
583
 
                  ncalls0=ncalls0/nchans
584
 
               endif
585
 
            elseif (imode.eq.1) then
586
 
               do kdim=1,ndim
587
 
                  nintcurr=nint_used/ifold(kdim)
588
 
                  do kchan=1,nchans
589
 
                     do kint=1,nintcurr
590
 
                        ymax(kint,kdim,kchan)=xint(kchan)**(1d0/ndim)
591
 
                     enddo
592
 
                  enddo
593
 
                  nintcurr_virt=nint_used_virt/ifold(kdim)
594
 
               enddo
595
 
               do kchan=1,nchans
596
 
                  ymax_virt(kchan)=xint_virt(kchan)
597
 
               enddo
598
 
            endif
599
 
            call reset_MC_grid  ! reset the grid for the integers
600
 
            if (fixed_order) call initplot  ! Also reset all the plots
601
 
            do i=1,nintegrals
602
 
               do kchan=0,nchans
603
 
                  ans(i,kchan)=0d0
604
 
                  unc(i,kchan)=0d0
605
 
                  chi2(i,kchan)=0d0
606
 
               enddo
607
 
               do j=1,3
608
 
                  ans3(i,j)=0d0
609
 
                  unc3(i,j)=0d0
610
 
               enddo
611
 
            enddo
612
 
            do i=1,2
613
 
               HwU_values(i)=0d0
614
 
            enddo
615
 
            bad_iteration=.false.
616
 
         else
617
 
            bad_iteration=.true.
618
 
         endif
619
 
         goto 10
620
 
      else
621
 
         bad_iteration=.false.
622
 
      endif
623
 
      HwU_values(1)=etot(1,0)
624
 
      HwU_values(2)=unc(1,0)
625
 
      if(nit.eq.1) then
626
 
         do kchan=0,nchans
627
 
            do i=1,nintegrals
628
 
               ans(i,kchan)=vtot(i,kchan)
629
 
               unc(i,kchan)=etot(i,kchan)
630
 
            enddo
631
 
            ans_chan(kchan)=ans(1,kchan)
632
 
         enddo
633
 
         write (*,'(a,1x,e10.4)') 'Chi^2 per d.o.f.',0d0
634
 
      else
635
 
c prevent annoying division by zero for nearly zero
636
 
c integrands
637
 
         do kchan=nchans,0,-1 ! go backwards so that kchan=0 goes last
638
 
                              ! (this makes sure central value is
639
 
                              ! correctly updated).
640
 
         do i=1,nintegrals
641
 
            if(etot(i,0).eq.0.and.unc(i,0).eq.0) then
642
 
               if(ans(i,0).eq.vtot(i,0) .and. i.eq.1) then
643
 
c double the number of points for the next iteration
644
 
                  if (double_events) ncalls0=ncalls0*2
645
 
                  goto 10
646
 
               else
647
 
                  unc(i,kchan)=abs(vtot(i,kchan)-ans(i,kchan))
648
 
                  etot(i,kchan)=abs(vtot(i,kchan)-ans(i,kchan))
649
 
               endif
650
 
            elseif(etot(i,0).eq.0) then
651
 
               etot(i,kchan)=unc(i,kchan)
652
 
            elseif(unc(i,0).eq.0) then ! 1st iteration; set to a large value
653
 
               unc(i,kchan)=etot(i,kchan)*1d99
654
 
            endif
655
 
            if (i.ne.1 .and. (etot(i,0).eq.0 .or. unc(i,0).eq.0)) then
656
 
               ans(i,kchan)=0d0
657
 
               unc(i,kchan)=0d0
658
 
               chi2(i,kchan)=0d0
659
 
            else
660
 
               ans(i,kchan)=(ans(i,kchan)/unc(i,0)+vtot(i,kchan)/etot(i
661
 
     $              ,0))/(1/unc(i,0)+1/etot(i,0))
662
 
               unc(i,kchan)=1/sqrt(1/unc(i,kchan)**2+1/etot(i,kchan)**2)
663
 
               chi2(i,kchan)=chi2(i,kchan)+(vtot(i,kchan)-ans(i,kchan))
664
 
     $              **2/etot(i,kchan)**2
665
 
            endif
666
 
         enddo
667
 
         ans_chan(kchan)=ans(1,kchan)
668
 
         enddo
669
 
         write (*,'(a,1x,e10.4)') 'Chi^2=',(vtot(1,0)-ans(1,0))**2
670
 
     $        /etot(1,0)**2
671
 
      endif
672
 
      nit_included=nit_included+1
673
 
      do i=1,nintegrals
674
 
         if (ans(i,0).ne.0d0) then
675
 
            efrac(i)=abs(unc(i,0)/ans(i,0))
676
 
         else
677
 
            efrac(i)=0d0
678
 
         endif
679
 
         write(*,'(a,1x,e10.4,1x,a,1x,e10.4,1x,a,1x,f7.3,1x,a)')
680
 
     $        'accumulated results '//title(i)//' =',ans(i,0),' +/- '
681
 
     $        ,unc(i,0) ,' (',efrac(i)*100d0,'%)'
682
 
      enddo
683
 
      cross_section=ans(1,0)
684
 
      if (nit_included.le.1) then
685
 
         write (*,'(a,1x,e10.4)') 'accumulated result Chi^2 per DoF ='
686
 
     $        ,0d0
687
 
      else
688
 
         write (*,'(a,1x,e10.4)') 'accumulated result Chi^2 per DoF =',
689
 
     &        chi2(1,0)/dble(nit_included-1)
690
 
      endif
691
 
      if (imode.eq.0) then
692
 
c Update the fraction of the events for which we include the virtual corrections
693
 
c in the calculation
694
 
         do kchan=1,nchans
695
 
            virtual_fraction(kchan)=max(min(virtual_fraction(kchan)
696
 
     $           *max(min(2d0*etot(3,kchan)/etot(1,kchan),2d0),0.25d0)
697
 
     $           ,1d0),Min_virt_fraction)
698
 
         enddo
699
 
      endif
700
 
c Update the results of the last tree iterations
701
 
      do i=1,nintegrals
702
 
         do j=1,2
703
 
            ans3(i,j)=ans3(i,j+1)
704
 
            unc3(i,j)=unc3(i,j+1)
705
 
         enddo
706
 
         ans3(i,3)=vtot(i,0)
707
 
         unc3(i,3)=etot(i,0)
708
 
      enddo
709
 
c Compute the results of the last three iterations
710
 
      if (nit_included.ge.4) then
711
 
         do i=1,nintegrals
712
 
            ans_l3(i)=0d0
713
 
            unc_l3(i)=ans3(i,1)*1d99
714
 
            chi2_l3(i)=0d0
715
 
            do j=1,3
716
 
               if (i.ne.1 .and. (unc_l3(i).eq.0d0 .or. unc3(i
717
 
     $              ,j).eq.0d0)) then
718
 
                  ans_l3(i)=0d0
719
 
                  unc_l3(i)=0d0
720
 
                  chi2_l3(i)=0d0
721
 
               else
722
 
                  ans_l3(i)=(ans_l3(i)/unc_l3(i)+ans3(i,j)/unc3(i,j))/
723
 
     &                 (1/unc_l3(i)+1/unc3(i,j))
724
 
                  unc_l3(i)=1/sqrt(1/unc_l3(i)**2+1/unc3(i,j)**2)
725
 
                  chi2_l3(i)=chi2_l3(i)+
726
 
     &                 (ans3(i,j)-ans_l3(i))**2/unc3(i,j)**2
727
 
               endif
728
 
            enddo
729
 
            chi2_l3(i)=chi2_l3(i)/2d0 ! three iterations, so 2 degrees of freedom
730
 
         enddo
731
 
         do i=1,2
732
 
            if (ans_l3(i).ne.0d0) then
733
 
               efrac(i)=abs(unc_l3(i)/ans_l3(i))
734
 
            else
735
 
               efrac(i)=0d0
736
 
            endif
737
 
            write(*,'(a,1x,e10.4,1x,a,1x,e10.4,1x,a,1x,f7.3,1x,a)')
738
 
     $           'accumulated results last 3 iterations '//title(i)/
739
 
     $           /' =' ,ans_l3(i),' +/- ',unc_l3(i) ,' (',efrac(i)*100d0
740
 
     $           ,'%)'
741
 
         enddo
742
 
         write(*,'(a,1x,e10.4)')
743
 
     $        'accumulated result last 3 iterrations Chi^2 per DoF ='
744
 
     $        ,chi2_l3(1)
745
 
      endif
746
 
      if(imode.eq.0) then
747
 
c Iteration is finished; now rearrange the grid
748
 
         do kchan=1,nchans
749
 
            do kdim=1,ndim
750
 
               call regrid(xacc(0,kdim,kchan),xgrid(0,kdim,kchan)
751
 
     $              ,nhits(1,kdim,kchan),nint_used,nhits_in_grids(kchan)
752
 
     $              ,regridded(kchan))
753
 
            enddo
754
 
         enddo
755
 
         call regrid_ave_virt(nint_used_virt,ndim)
756
 
c Regrid the MC over integers (used for the MC over FKS dirs)
757
 
         call regrid_MC_integer
758
 
      endif
759
 
c Quit if the desired accuracy has been reached
760
 
      if (nit_included.ge.min_it .and. accuracy.gt.0d0) then
761
 
         if (unc(1,0)/ans(1,0)*max(1d0,chi2(1,0)/dble(nit_included-1))
762
 
     $        .lt.accuracy) then
763
 
            write (*,*) 'Found desired accuracy'
764
 
            nit=nitmax
765
 
c Improve the stats in the plots
766
 
            if (fixed_order) call accum(.true.)
767
 
            if (fixed_order) call HwU_accum_iter(.true.,ntotcalls(1)
768
 
     $           ,HwU_values)
769
 
            goto 10
770
 
         elseif(unc_l3(1)/ans_l3(1)*max(1d0,chi2_l3(1)).lt.accuracy)
771
 
     $           then
772
 
            write (*,*)
773
 
     &           'Found desired accuracy in last 3 iterations'
774
 
            nit=nitmax
775
 
            do i=1,nintegrals
776
 
               ans(i,0)=ans_l3(i)
777
 
               unc(i,0)=unc_l3(i)
778
 
               chi2(i,0)=chi2_l3(i)*dble(nit_included-1)
779
 
            enddo
780
 
c Improve the stats in the plots
781
 
            if (fixed_order) call accum(.true.)
782
 
            if (fixed_order) call HwU_accum_iter(.true.,ntotcalls(1)
783
 
     $           ,HwU_values)
784
 
            goto 10
785
 
         endif
786
 
      endif
787
 
c Double the number of intervals in the grids if not yet reach the maximum
788
 
      if (2*nint_used.le.nintervals .and. double_events) then
789
 
         do kchan=1,nchans
790
 
            do kdim=1,ndim
791
 
               call double_grid(xgrid(0,kdim,kchan),nhits(1,kdim,kchan)
792
 
     $              ,nint_used)
793
 
            enddo
794
 
         enddo
795
 
         nint_used=2*nint_used
796
 
      endif
797
 
      if (2*nint_used_virt.le.nintervals_virt .and. double_events) then
798
 
         call double_ave_virt(nint_used_virt,ndim)
799
 
         nint_used_virt=2*nint_used_virt
800
 
      endif
801
 
 
802
 
c double the number of points for the next iteration
803
 
      if (double_events) ncalls0=ncalls0*2
804
 
c Also improve stats in plots
805
 
      if (fixed_order) call accum(.true.)
806
 
      if (fixed_order) call HwU_accum_iter(.true.,ntotcalls(1)
807
 
     $     ,HwU_values)
808
 
c Do next iteration
809
 
      goto 10
810
 
      return
811
 
 250  format(a7,i5,1x,a1,1x,i5,1x,l,1x,i8,1x,i8,2x,e10.4,2x,e10.4,2x
812
 
     $     ,e10.4)
813
 
      end
814
 
 
815
 
      subroutine double_grid(xgrid,nhits,ninter)
816
 
      implicit none
817
 
      include "mint.inc"
818
 
      integer  ninter,nhits(nintervals)
819
 
      real * 8 xgrid(0:nintervals)
820
 
      integer i
821
 
      do i=ninter,1,-1
822
 
         xgrid(i*2)=xgrid(i)
823
 
         xgrid(i*2-1)=(xgrid(i)+xgrid(i-1))/2d0
824
 
         nhits(i*2)=nhits(i)/2
825
 
         nhits(i*2-1)=nhits(i)-nhits(i*2)
826
 
      enddo
827
 
      return
828
 
      end
829
 
 
830
 
 
831
 
      subroutine regrid(xacc,xgrid,nhits,ninter,nhits_in_grid,regridded)
832
 
      implicit none
833
 
      include "mint.inc"
834
 
      logical regridded
835
 
      integer  ninter,nhits(nintervals),np,nhits_in_grid
836
 
      real * 8 xacc(0:nintervals),xgrid(0:nintervals)
837
 
      real * 8 xn(nintervals),r,tiny,xl,xu,nl,nu,sum
838
 
      parameter ( tiny=1d-8 )
839
 
      integer kint,jint
840
 
c compute total number of points and update grids if large
841
 
      np=0
842
 
      do kint=1,ninter
843
 
         np=np+nhits(kint)
844
 
      enddo
845
 
      regridded=.false.
846
 
      if (np.lt.nhits_in_grid) return
847
 
      regridded=.true.
848
 
      
849
 
c Use the same smoothing as in VEGAS uses for the grids, i.e. use the
850
 
c average of the central and the two neighbouring grid points: (Only do
851
 
c this if we are already at the maximum intervals, because the doubling
852
 
c of the grids also includes a smoothing).
853
 
      if (ninter.eq.nintervals) then
854
 
         xl=xacc(1)
855
 
         xu=xacc(2)
856
 
         xacc(1)=(xl+xu)/2d0
857
 
         nl=nhits(1)
858
 
         nu=nhits(2)
859
 
         nhits(1)=nint((nl+nu)/2d0)
860
 
         do kint=2,ninter-1
861
 
            xacc(kint)=xl+xu
862
 
            xl=xu
863
 
            xu=xacc(kint+1)
864
 
            xacc(kint)=(xacc(kint)+xu)/3d0
865
 
            nhits(kint)=nl+nu
866
 
            nl=nu
867
 
            nu=nhits(kint+1)
868
 
            nhits(kint)=nint((nhits(kint)+nu)/3d0)
869
 
         enddo
870
 
         xacc(ninter)=(xu+xl)/2d0
871
 
         nhits(ninter)=nint((nu+nl)/2d0)
872
 
      endif
873
 
c
874
 
      sum=0d0
875
 
      do kint=1,ninter
876
 
         if (nhits(kint).ne.0) then
877
 
            xacc(kint)=abs(xacc(kint))/nhits(kint)
878
 
            sum=sum+xacc(kint)
879
 
         endif
880
 
      enddo
881
 
c
882
 
      do kint=1,ninter
883
 
c xacc (xerr) already contains a factor equal to the interval size
884
 
c Thus the integral of rho is performed by summing up
885
 
         if(nhits(kint).ne.0) then
886
 
c     take logarithm to help convergence (taken from LO dsample.f)
887
 
            if (xacc(kint).ne.sum) then
888
 
               xacc(kint)=((xacc(kint)/sum-1d0)/
889
 
     &                        log(xacc(kint)/sum))**1.5
890
 
            else
891
 
               xacc(kint)=1d0
892
 
            endif
893
 
            xacc(kint)= xacc(kint-1) + abs(xacc(kint))
894
 
         else
895
 
            xacc(kint)=xacc(kint-1)
896
 
         endif
897
 
      enddo
898
 
      do kint=1,ninter
899
 
         xacc(kint)=xacc(kint)/xacc(ninter)
900
 
      enddo
901
 
c Check that we have a reasonable result and update the accumulated
902
 
c results if need be
903
 
      do kint=1,ninter
904
 
         if (xacc(kint).lt.(xacc(kint-1)+tiny)) then
905
 
            xacc(kint)=xacc(kint-1)+tiny
906
 
         endif
907
 
      enddo
908
 
c it could happen that the change above yielded xacc() values greater
909
 
c than 1: one more update needed
910
 
      xacc(ninter)=1d0
911
 
      do kint=1,ninter
912
 
         if (xacc(ninter-kint).gt.(xacc(ninter-kint+1)-tiny)) then
913
 
            xacc(ninter-kint)=1d0-dble(kint)*tiny
914
 
         else
915
 
            exit
916
 
         endif
917
 
      enddo
918
 
 
919
 
      do kint=1,ninter
920
 
         r=dble(kint)/dble(ninter)
921
 
         do jint=1,ninter
922
 
            if(r.lt.xacc(jint)) then
923
 
               xn(kint)=xgrid(jint-1)+(r-xacc(jint-1))
924
 
     #        /(xacc(jint)-xacc(jint-1))*(xgrid(jint)-xgrid(jint-1))
925
 
               goto 11
926
 
            endif
927
 
         enddo
928
 
         if(jint.ne.ninter+1.and.kint.ne.ninter) then
929
 
            write(*,*) ' error',jint,ninter
930
 
            stop
931
 
         endif
932
 
         xn(ninter)=1
933
 
 11      continue
934
 
      enddo
935
 
      do kint=1,ninter
936
 
         xgrid(kint)=xn(kint)
937
 
      enddo
938
 
      end
939
 
 
940
 
      subroutine nextlexi(ndim,iii,kkk,iret)
941
 
c kkk: array of integers 1 <= kkk(j) <= iii(j), j=1,ndim
942
 
c at each call iii is increased lexicographycally.
943
 
c for example, starting from ndim=3, kkk=(1,1,1), iii=(2,3,2)
944
 
c subsequent calls to nextlexi return
945
 
c         kkk(1)      kkk(2)      kkk(3)    iret
946
 
c 0 calls   1           1           1       0
947
 
c 1         1           1           2       0    
948
 
c 2         1           2           1       0
949
 
c 3         1           2           2       0
950
 
c 4         1           3           1       0
951
 
c 5         1           3           2       0
952
 
c 6         2           1           1       0
953
 
c 7         2           1           2       0
954
 
c 8         2           2           1       0
955
 
c 9         2           2           2       0
956
 
c 10        2           3           1       0
957
 
c 11        2           3           2       0
958
 
c 12        2           3           2       1
959
 
      implicit none
960
 
      integer ndim,iret,kkk(ndim),iii(ndim)
961
 
      integer k
962
 
      k=ndim
963
 
 1    continue
964
 
      if(kkk(k).lt.iii(k)) then
965
 
         kkk(k)=kkk(k)+1
966
 
         iret=0
967
 
         return
968
 
      else
969
 
         kkk(k)=1
970
 
         k=k-1
971
 
         if(k.eq.0) then
972
 
            iret=1
973
 
            return
974
 
         endif
975
 
         goto 1
976
 
      endif
977
 
      end
978
 
 
979
 
 
980
 
      subroutine gen(fun,ndim,xgrid,ymax,ymax_virt,imode,x,vn)
981
 
c imode=0 to initialize
982
 
c imode=1 to generate
983
 
c imode=3 store generation efficiency in x(1)
984
 
      implicit none
985
 
      integer ndim,imode
986
 
      include "mint.inc"
987
 
      real * 8 fun,xgrid(0:nintervals,ndimmax,maxchannels)
988
 
     $     ,ymax(nintervals,ndimmax,maxchannels)
989
 
     $     ,ymax_virt(0:maxchannels),x(ndimmax)
990
 
      real * 8 dx(ndimmax),xx(ndimmax),vol_chan,dummy
991
 
      integer icell(ndimmax),ncell(ndimmax),ncell_virt
992
 
      integer ifold(ndimmax),kfold(ndimmax)
993
 
      common/cifold/ifold
994
 
      real * 8 r,f(nintegrals),f1(nintegrals),ubound,vol,ran3
995
 
     $     ,xmmm(nintervals,ndimmax,maxchannels)
996
 
      real * 8 rand(ndimmax)
997
 
      external fun,ran3
998
 
      integer icalls,mcalls,kdim,kint,nintcurr,nintcurr_virt,iret,ifirst
999
 
     $     ,i,vn,icalls_virt,mcalls_virt,icalls_nz,icalls_virt_nz
1000
 
      double precision average_virtual(maxchannels)
1001
 
     $     ,virtual_fraction(maxchannels)
1002
 
      common/c_avg_virt/average_virtual,virtual_fraction
1003
 
      save icalls,mcalls,icalls_virt,mcalls_virt,xmmm,icalls_nz
1004
 
     $     ,icalls_virt_nz
1005
 
      logical new_point
1006
 
      common /c_new_point/ new_point
1007
 
      if(imode.eq.0) then
1008
 
         do kchan=1,nchans
1009
 
            do kdim=1,ndim
1010
 
               nintcurr=nintervals/ifold(kdim)
1011
 
               xmmm(1,kdim,kchan)=ymax(1,kdim,kchan)
1012
 
               do kint=2,nintcurr
1013
 
                  xmmm(kint,kdim,kchan)=xmmm(kint-1,kdim,kchan)
1014
 
     $                 +ymax(kint,kdim,kchan)
1015
 
               enddo
1016
 
               do kint=1,nintcurr
1017
 
                  xmmm(kint,kdim,kchan)=xmmm(kint,kdim,kchan)
1018
 
     $                 /xmmm(nintcurr,kdim,kchan)
1019
 
               enddo
1020
 
            enddo
1021
 
         enddo
1022
 
         icalls=0
1023
 
         icalls_nz=0
1024
 
         mcalls=0
1025
 
         icalls_virt=0
1026
 
         icalls_virt_nz=0
1027
 
         mcalls_virt=0
1028
 
         xx(2)=0d0
1029
 
         xx(3)=0d0
1030
 
         xx(5)=0d0
1031
 
         xx(6)=0d0
1032
 
         return
1033
 
      elseif(imode.eq.3) then
1034
 
         if(icalls.gt.0) then
1035
 
            x(1)=dble(mcalls)/dble(icalls)
1036
 
            x(2)=xx(2)/dble(icalls)
1037
 
            x(3)=xx(3)/dble(icalls)
1038
 
         else
1039
 
            x(1)=-1d0
1040
 
            x(2)=-1d0
1041
 
            x(3)=-1d0
1042
 
         endif
1043
 
         if(icalls_virt.gt.0) then
1044
 
            x(4)=dble(mcalls_virt)/dble(icalls_virt)
1045
 
            x(5)=xx(5)/dble(icalls_virt)
1046
 
            x(6)=xx(6)/dble(icalls_virt)
1047
 
         else
1048
 
            x(4)=-1d0
1049
 
            x(5)=-1d0
1050
 
            x(6)=-1d0
1051
 
         endif
1052
 
         call increasecnt(' ',imode)
1053
 
         return
1054
 
      endif
1055
 
      if (vn.eq.1) then
1056
 
         mcalls_virt=mcalls_virt+1
1057
 
      elseif(vn.eq.2 .or. vn.eq.3) then
1058
 
         mcalls=mcalls+1
1059
 
      else
1060
 
         write (*,*) 'vn not correct in mint-integrator2.f',vn,imode
1061
 
         stop
1062
 
      endif
1063
 
 10   continue
1064
 
      call get_channel(ymax_virt,vol_chan)
1065
 
      new_point=.true.
1066
 
      if (vn.eq.1) then
1067
 
         icalls_virt=icalls_virt+1
1068
 
      elseif(vn.eq.2 .or. vn.eq.3) then
1069
 
         icalls=icalls+1
1070
 
      endif
1071
 
      if (vn.eq.1) then
1072
 
c Choose cell flat
1073
 
         do kdim=1,ndim
1074
 
            ncell(kdim)=min(int(ran3(.false.)*nintcurr)+1,nintcurr)
1075
 
            rand(kdim)=ran3(.false.)
1076
 
         enddo
1077
 
      elseif(vn.eq.2 .or. vn.eq.3) then
1078
 
         do kdim=1,ndim
1079
 
            nintcurr=nintervals/ifold(kdim)
1080
 
            r=ran3(.false.)
1081
 
            do kint=1,nintcurr
1082
 
               if(r.lt.xmmm(kint,kdim,ichan)) then
1083
 
                  ncell(kdim)=kint
1084
 
                  exit
1085
 
               endif
1086
 
            enddo
1087
 
            rand(kdim)=ran3(.false.)
1088
 
         enddo
1089
 
      endif
1090
 
      if (vn.eq.2 .or. vn.eq.3) then
1091
 
         ubound=1
1092
 
         do kdim=1,ndim
1093
 
            ubound=ubound*ymax(ncell(kdim),kdim,ichan)
1094
 
         enddo
1095
 
      endif
1096
 
      do kdim=1,ndim
1097
 
         kfold(kdim)=1
1098
 
      enddo
1099
 
      do i=1,nintegrals
1100
 
         f(i)=0
1101
 
      enddo
1102
 
      ifirst=0
1103
 
 5    continue
1104
 
      vol=1d0/vol_chan
1105
 
      do kdim=1,ndim
1106
 
         nintcurr=nintervals/ifold(kdim)
1107
 
         nintcurr_virt=nintervals_virt/ifold(kdim)
1108
 
         icell(kdim)=ncell(kdim)+(kfold(kdim)-1)*nintcurr
1109
 
         dx(kdim)=xgrid(icell(kdim),kdim,ichan)-xgrid(icell(kdim)-1,kdim
1110
 
     $        ,ichan)
1111
 
         vol=vol*dx(kdim)*nintervals/ifold(kdim)
1112
 
         x(kdim)=xgrid(icell(kdim)-1,kdim,ichan)+rand(kdim)*dx(kdim)
1113
 
      enddo
1114
 
      call get_ave_virt(x,nintcurr_virt,ndim,average_virtual)
1115
 
      if (vn.eq.1) then
1116
 
         ubound=ymax_virt(ichan)
1117
 
      endif
1118
 
      dummy=fun(x,vol,ifirst,f1)
1119
 
      do i=1,nintegrals
1120
 
         f(i)=f(i)+f1(i)
1121
 
      enddo
1122
 
      ifirst=1
1123
 
      call nextlexi(ndim,ifold,kfold,iret)
1124
 
      if(iret.eq.0) goto 5
1125
 
c get final value (x and vol not used in this call)
1126
 
      dummy=fun(x,vol,2,f1)
1127
 
      do i=1,nintegrals
1128
 
         f(i)=f1(i)
1129
 
      enddo
1130
 
      if (vn.eq.2 .or. vn.eq.3) then
1131
 
         xx(2)=xx(2)+f(2)
1132
 
         xx(3)=xx(3)+f(1)
1133
 
      else
1134
 
         xx(5)=xx(5)+f(2)
1135
 
         xx(6)=xx(6)+f(1)
1136
 
      endif
1137
 
      call increasecnt('another call to the function',imode)
1138
 
      if (f(1).eq.0d0) then
1139
 
         call increasecnt('failed generation cuts',imode)
1140
 
      else
1141
 
         if (vn.eq.1) then
1142
 
            icalls_virt_nz=icalls_virt_nz+1
1143
 
         elseif(vn.eq.2 .or.vn.eq.3) then
1144
 
            icalls_nz=icalls_nz+1
1145
 
         endif
1146
 
      endif
1147
 
      if(f(1).lt.0) then
1148
 
         write(*,*) 'gen: non positive function'
1149
 
         stop
1150
 
      endif
1151
 
      if(f(1).gt.ubound) then
1152
 
         if (vn.eq.2) then
1153
 
            call increasecnt('ubound fail novi',imode)
1154
 
         elseif (vn.eq.1) then
1155
 
            call increasecnt('ubound fail virt',imode)
1156
 
         elseif (vn.eq.3) then
1157
 
            call increasecnt('ubound fail born',imode)
1158
 
         endif
1159
 
      endif
1160
 
      ubound=ubound*ran3(.false.)
1161
 
      if(ubound.gt.f(1)) then
1162
 
         call increasecnt
1163
 
     &        ('vetoed calls in inclusive cross section',imode)
1164
 
         goto 10
1165
 
      endif
1166
 
      if (vn.eq.2) then
1167
 
         call increasecnt('events gen novi',imode)
1168
 
      elseif (vn.eq.1) then
1169
 
         call increasecnt('events gen virt',imode)
1170
 
      elseif (vn.eq.3) then
1171
 
         call increasecnt('events gen born',imode)
1172
 
      endif
1173
 
      end
1174
 
 
1175
 
 
1176
 
c Dummy subroutine (normally used with vegas when resuming plots)
1177
 
      subroutine resume()
1178
 
      end
1179
 
 
1180
 
 
1181
 
      subroutine increasecnt(argument,imode)
1182
 
c Be careful, argument should be at least 15 characters
1183
 
c long for this subroutine to work properly
1184
 
      implicit none
1185
 
      character*(*) argument
1186
 
      character*15 list(100)
1187
 
      integer ilist(0:100),i,j,imode
1188
 
      logical firsttime
1189
 
      data firsttime/.true./
1190
 
      save ilist,list
1191
 
 
1192
 
      if (firsttime) then
1193
 
         ilist(0)=1
1194
 
         do i=1,100
1195
 
            ilist(i)=0
1196
 
            list(i)='               '
1197
 
         enddo
1198
 
         firsttime=.false.
1199
 
      endif
1200
 
 
1201
 
      if(imode.ne.3) then
1202
 
         i=1
1203
 
         do while (i.le.ilist(0))
1204
 
            if(i.eq.ilist(0)) then
1205
 
               list(i)=argument(1:15)
1206
 
               ilist(i)=1
1207
 
               ilist(0)=ilist(0)+1
1208
 
               goto 14
1209
 
            endif
1210
 
            if (argument(1:15).eq.list(i)) then
1211
 
               ilist(i)=ilist(i)+1
1212
 
               goto 14
1213
 
            endif
1214
 
            i=i+1
1215
 
            if (i.ge.100) then
1216
 
               write (*,*) 'error #1 in increasecnt'
1217
 
               do j=1,ilist(0)
1218
 
                  write (*,*) list(j),ilist(j)
1219
 
               enddo
1220
 
               stop
1221
 
            endif
1222
 
         enddo
1223
 
 14      continue
1224
 
      else
1225
 
         do i=1,ilist(0)-1
1226
 
            write (*,*) list(i),ilist(i)
1227
 
         enddo
1228
 
      endif
1229
 
      end
1230
 
 
1231
 
      double precision function ran3(even)
1232
 
      implicit none
1233
 
      double precision ran2,get_ran
1234
 
      logical even
1235
 
      external get_ran
1236
 
      if (even) then
1237
 
         ran3=get_ran()
1238
 
      else
1239
 
         ran3=ran2()
1240
 
      endif
1241
 
      return
1242
 
      end
1243
 
 
1244
 
      subroutine initialize_even_random_numbers(ncalls0,ndim,ncalls)
1245
 
c Recompute the number of calls. Uses the algorithm from VEGAS
1246
 
      implicit none
1247
 
      integer ncalls0,ndim,ncalls,i
1248
 
      integer dim,ng,npg,k
1249
 
      logical firsttime
1250
 
      common /even_ran/dim,ng,npg,k,firsttime
1251
 
c Make sure that hypercubes are newly initialized
1252
 
      firsttime=.true.
1253
 
c Number of dimension of the integral
1254
 
      dim=ndim
1255
 
c Number of elements in which we can split one dimension
1256
 
      ng=(ncalls0/2.)**(1./ndim)
1257
 
c Total number of hypercubes
1258
 
      k=ng**ndim
1259
 
c Number of PS points in each hypercube (at least 2)
1260
 
      npg=max(ncalls0/k,2)
1261
 
c Number of PS points for this iteration
1262
 
      ncalls=npg*k
1263
 
      return
1264
 
      end
1265
 
 
1266
 
 
1267
 
      double precision function get_ran()
1268
 
      implicit none
1269
 
      double precision ran2,dng
1270
 
      external ran2
1271
 
      integer dim,ng,npg,k
1272
 
      logical firsttime
1273
 
      common /even_ran/dim,ng,npg,k,firsttime
1274
 
      integer maxdim
1275
 
      parameter (maxdim=100)
1276
 
      integer iii(maxdim),kkk(maxdim),i,iret
1277
 
      integer current_dim
1278
 
      save current_dim,dng,kkk,iii
1279
 
      if (firsttime) then
1280
 
         dng=1d0/dble(ng)
1281
 
         current_dim=0
1282
 
         do i=1,dim
1283
 
           iii(i)=ng
1284
 
           kkk(i)=1
1285
 
        enddo
1286
 
        firsttime=.false.
1287
 
      endif
1288
 
      current_dim=mod(current_dim,dim)+1
1289
 
c This is the random number in the hypercube 'k' for current_dim
1290
 
      get_ran=dng*(ran2()+dble(kkk(current_dim)-1))
1291
 
c Got random numbers for all dimensions, update kkk() for the next call
1292
 
      if (current_dim.eq.dim) then
1293
 
         call nextlexi(dim,iii,kkk,iret)
1294
 
         if (iret.eq.1) then
1295
 
            call nextlexi(dim,iii,kkk,iret)
1296
 
         endif
1297
 
      endif
1298
 
      return
1299
 
      end
1300
 
 
1301
 
 
1302
 
      subroutine init_ave_virt(ninter,ndim)
1303
 
      implicit none
1304
 
      include "mint.inc"
1305
 
      integer kdim,ndim,ninter,i
1306
 
      do kchan=1,nchans
1307
 
         do kdim=1,ndim
1308
 
            do i=1,ninter
1309
 
               nvirt(i,kdim,kchan)=0
1310
 
               ave_virt(i,kdim,kchan)=0d0
1311
 
               nvirt_acc(i,kdim,kchan)=0
1312
 
               ave_virt_acc(i,kdim,kchan)=0d0
1313
 
               ave_born_acc(i,kdim,kchan)=0d0
1314
 
            enddo
1315
 
         enddo
1316
 
      enddo
1317
 
      return
1318
 
      end
1319
 
 
1320
 
      subroutine get_ave_virt(x,ninter,ndim,average_virtual)
1321
 
      implicit none
1322
 
      include "mint.inc"
1323
 
      integer kdim,ndim,ninter,ncell
1324
 
      double precision x(ndimmax),average_virtual(maxchannels)
1325
 
      average_virtual(ichan)=0d0
1326
 
      do kdim=1,ndim
1327
 
         ncell=min(int(x(kdim)*ninter)+1,ninter)
1328
 
         average_virtual(ichan)=average_virtual(ichan)+
1329
 
     &        ave_virt(ncell,kdim,ichan)
1330
 
      enddo
1331
 
      average_virtual(ichan)=average_virtual(ichan)/ndim
1332
 
      return
1333
 
      end
1334
 
 
1335
 
      subroutine fill_ave_virt(x,ninter,ndim,virtual,born)
1336
 
      implicit none
1337
 
      include "mint.inc"
1338
 
      integer kdim,ndim,ninter,ncell
1339
 
      double precision x(ndimmax),virtual,born
1340
 
      do kdim=1,ndim
1341
 
         ncell=min(int(x(kdim)*ninter)+1,ninter)
1342
 
         nvirt_acc(ncell,kdim,ichan)=nvirt_acc(ncell,kdim,ichan)+1
1343
 
         ave_virt_acc(ncell,kdim,ichan)=ave_virt_acc(ncell,kdim,ichan)
1344
 
     $        +virtual
1345
 
         ave_born_acc(ncell,kdim,ichan)=ave_born_acc(ncell,kdim,ichan)
1346
 
     $        +born
1347
 
      enddo
1348
 
      return
1349
 
      end
1350
 
 
1351
 
      subroutine regrid_ave_virt(ninter,ndim)
1352
 
      implicit none
1353
 
      include "mint.inc"
1354
 
      integer ninter,ndim,kdim,i
1355
 
c need to solve for k_new = (virt+k_old*born)/born = virt/born + k_old
1356
 
      do kchan=1,nchans
1357
 
      do kdim=1,ndim
1358
 
         do i=1,ninter
1359
 
            if (ave_born_acc(i,kdim,kchan).eq.0d0) cycle
1360
 
            if (ave_virt(i,kdim,kchan).eq.0d0) then ! i.e. first iteration
1361
 
               ave_virt(i,kdim,kchan)= ave_virt_acc(i,kdim,kchan)
1362
 
     $              /ave_born_acc(i,kdim,kchan)+ave_virt(i,kdim,kchan)
1363
 
            else  ! give some importance to the iterations already
1364
 
                  ! done: simply base it on the number of points already
1365
 
                  ! computed in that bin. Give half the weight to old iterations
1366
 
                  ! k_new = [N_new*(V/B+k_old) + (N_old/2)*k_old] / [N_new+(N_old/2)]
1367
 
               ave_virt(i,kdim,kchan)=(nvirt_acc(i,kdim,kchan)
1368
 
     $              *ave_virt_acc(i,kdim,kchan)/ave_born_acc(i,kdim
1369
 
     $              ,kchan)+(nvirt_acc(i,kdim,kchan)+nint(nvirt(i,kdim
1370
 
     $              ,kchan)/2d0))*ave_virt(i,kdim,kchan))
1371
 
     $              /dble(nvirt_acc(i,kdim,kchan)+nint(nvirt(i,kdim
1372
 
     $              ,kchan)/2d0))
1373
 
            endif
1374
 
         enddo
1375
 
      enddo
1376
 
c reset the acc values
1377
 
      do kdim=1,ndim
1378
 
         do i=1,ninter
1379
 
            nvirt(i,kdim,kchan)=nvirt(i,kdim,kchan)+nvirt_acc(i,kdim
1380
 
     $           ,kchan)
1381
 
            nvirt_acc(i,kdim,kchan)=0
1382
 
            ave_born_acc(i,kdim,kchan)=0d0
1383
 
            ave_virt_acc(i,kdim,kchan)=0d0
1384
 
         enddo
1385
 
      enddo
1386
 
      enddo
1387
 
      return
1388
 
      end
1389
 
 
1390
 
 
1391
 
      subroutine double_ave_virt(ninter,ndim)
1392
 
      implicit none
1393
 
      include "mint.inc"
1394
 
      integer kdim,ndim,i,ninter
1395
 
      do kchan=1,nchans
1396
 
      do kdim=1,ndim
1397
 
         do i=ninter,1,-1
1398
 
            ave_virt(i*2,kdim,kchan)=ave_virt(i,kdim,kchan)
1399
 
            if (nvirt(i,kdim,kchan).ne.0) then
1400
 
               nvirt(i*2,kdim,kchan)=max(nvirt(i,kdim,kchan)/2,1)
1401
 
            else
1402
 
               nvirt(i*2,kdim,kchan)=0
1403
 
            endif
1404
 
            if (i.ne.1) then
1405
 
               ave_virt(i*2-1,kdim,kchan)=(ave_virt(i,kdim,kchan)
1406
 
     $              +ave_virt(i-1,kdim,kchan))/2d0
1407
 
               if (nvirt(i,kdim,kchan)+nvirt(i-1,kdim,kchan).ne.0) then
1408
 
                  nvirt(i*2-1,kdim,kchan)=
1409
 
     &              max((nvirt(i,kdim,kchan)+nvirt(i-1,kdim,kchan))/4,1)
1410
 
               else
1411
 
                  nvirt(i*2-1,kdim,kchan)=0
1412
 
               endif
1413
 
            else
1414
 
               if (nvirt(1,kdim,kchan).ne.0) then
1415
 
                  nvirt(1,kdim,kchan)=max(nvirt(1,kdim,kchan)/2,1)
1416
 
               else
1417
 
                  nvirt(1,kdim,kchan)=0
1418
 
               endif
1419
 
            endif
1420
 
         enddo
1421
 
      enddo
1422
 
      enddo
1423
 
      return
1424
 
      end
1425
 
 
1426
 
 
1427
 
 
1428
 
      subroutine get_channel(ans,vol_chan)
1429
 
c Picks one random 'ichan' among the 'nchans' integration channels and
1430
 
c fills the channels common block in mint.inc.
1431
 
      implicit none
1432
 
      include 'mint.inc'
1433
 
      double precision ans(0:maxchannels),vol_chan,ran3,target,sum
1434
 
      external ran3
1435
 
      if (nchans.eq.1) then
1436
 
         ichan=1
1437
 
         iconfig=iconfigs(ichan)
1438
 
         vol_chan=1d0
1439
 
      elseif (nchans.gt.1) then
1440
 
         if (ans(0).le.0d0) then
1441
 
c     pick one at random (flat)
1442
 
            ichan=int(ran3(.false.)*nchans)+1
1443
 
            iconfig=iconfigs(ichan)
1444
 
            vol_chan=1d0/dble(nchans)
1445
 
         else
1446
 
c     pick one at random (weighted by cross section)
1447
 
            sum=0d0
1448
 
            do kchan=1,nchans
1449
 
               sum=sum+ans(kchan)
1450
 
            enddo
1451
 
            if (abs(sum-ans(0))/(sum+ans(0)).gt.1d-8) then
1452
 
               write (*,*) 'ERROR: sum should be equal to ans',
1453
 
     &              sum,ans(0)
1454
 
               stop 1
1455
 
            endif
1456
 
            target=ans(0)*ran3(.false.)
1457
 
            sum=0d0
1458
 
            ichan=0
1459
 
            do while (sum.lt.target)
1460
 
               ichan=ichan+1
1461
 
               sum=sum+ans(ichan)
1462
 
            enddo
1463
 
            if (ichan.eq.0 .or. ichan.gt.nchans) then
1464
 
               write (*,*) 'ERROR: ichan cannot be zero or'/
1465
 
     $              /' larger than nchans',ichan,nchans
1466
 
               stop
1467
 
            endif
1468
 
            iconfig=iconfigs(ichan)
1469
 
            vol_chan=ans(ichan)/ans(0)
1470
 
         endif
1471
 
      endif
1472
 
      return
1473
 
      end
1474