      PROGRAM GKKA
*
      implicit none
*
*     program to generate massx distribution from
*     monte carlo data
*
      common /pawc/ hmemor (800000) ! book memory
*
      real hmemor, prob, wazerop, watwop, wafourp, wac, wad, wabg
      real xmass, xgam_jsi, xk_x, xphik_x
      real rmass, rgam_jsi, rk_x, rphik_x
      real b1mass, b1gam_jsi, b1k_x, b1phik_x
      real b2mass, b2gam_jsi, b2k_x, b2phik_x
      integer i, j, k, l, istat, icycle
*
*     set up histograms
*
      call hlimit (800000)
*
      call hbook1 (100, 'Mass all contribution', 20, 1.4, 1.8, 0.)
      call hbook1 (110, 'Mass 0++ contribution', 20, 1.4, 1.8, 0.)
      call hbook1 (120, 'Mass 2++ contribution', 20, 1.4, 1.8, 0.)
      call hbook1 (130, 'Gamma in Jpsi ', 16,-0.8,0.8, 0.)
      call hbook1 (131, 'Gamma in Jpsi ', 16,-0.8,0.8, 0.)
      call hbook1 (132, 'Gamma in Jpsi ', 16,-0.8,0.8, 0.)
      call hbook1 (133, 'Gamma in Jpsi ', 16,-0.8,0.8, 0.)
      call hbook1 (833, 'Gamma in Jpsi ', 16,-0.8,0.8, 0.)     
      call hbook1 (140, 'K in X ', 20,-1.0,1.0, 0.)
      call hbook1 (141, 'K in X ', 20,-1.0,1.0, 0.)
      call hbook1 (142, 'K in X ', 20,-1.0,1.0, 0.)
      call hbook1 (143, 'K in X ', 20,-1.0,1.0, 0.)
      call hbook1 (843, 'K in X ', 20,-1.0,1.0, 0.)       
      call hbook1 (191, 'K in X ', 20,-1.0,1.0, 0.)
      call hbook1 (192, 'K in X ', 20,-1.0,1.0, 0.)
      call hbook1 (193, 'K in X ', 20,-1.0,1.0, 0.)
      call hbook1 (194, 'K in X ', 20,-1.0,1.0, 0.)
      call hbook1 (195, 'K in X ', 20,-1.0,1.0, 0.)
      call hbook1 (150, 'K in X (Phi)', 20,0.0,6.3, 0.)
      call hbook1 (151, 'K in X (Phi)', 20,0.0,6.3, 0.)
      call hbook1 (152, 'K in X (Phi)', 20,0.0,6.3, 0.)
      call hbook1 (153, 'K in X (Phi)', 20,0.0,6.3, 0.)
*
      call hbook1 (200, 'Mass all contribution', 20, 1.4, 1.8, 0.)
      call hbook1 (230, 'Gamma in Jpsi ', 16,-0.8,0.8, 0.)
      call hbook1 (231, 'Gamma in Jpsi ', 16,-0.8,0.8, 0.)
      call hbook1 (232, 'Gamma in Jpsi ', 16,-0.8,0.8, 0.)
      call hbook1 (233, 'Gamma in Jpsi ', 16,-0.8,0.8, 0.)
      call hbook1 (240, 'K in X ', 20,-1.0,1.0, 0.)
      call hbook1 (241, 'K in X ', 20,-1.0,1.0, 0.)
      call hbook1 (242, 'K in X ', 20,-1.0,1.0, 0.)
      call hbook1 (243, 'K in X ', 20,-1.0,1.0, 0.)
      call hbook1 (250, 'K in X (Phi)', 20,0.0,6.3, 0.)
      call hbook1 (251, 'K in X (Phi)', 20,0.0,6.3, 0.)
      call hbook1 (252, 'K in X (Phi)', 20,0.0,6.3, 0.)
      call hbook1 (253, 'K in X (Phi)', 20,0.0,6.3, 0.)
*
      call hbook1 (300, 'Mass all contribution', 20, 1.4, 1.8, 0.)
      call hbook1 (330, 'Gamma in Jpsi ', 16,-0.8,0.8, 0.)
      call hbook1 (331, 'Gamma in Jpsi ', 16,-0.8,0.8, 0.)
      call hbook1 (332, 'Gamma in Jpsi ', 16,-0.8,0.8, 0.)
      call hbook1 (333, 'Gamma in Jpsi ', 16,-0.8,0.8, 0.)
      call hbook1 (340, 'K in X ', 20,-1.0,1.0, 0.)
      call hbook1 (341, 'K in X ', 20,-1.0,1.0, 0.)
      call hbook1 (342, 'K in X ', 20,-1.0,1.0, 0.)
      call hbook1 (343, 'K in X ', 20,-1.0,1.0, 0.)
      call hbook1 (350, 'K in X (Phi)', 20,0.0,6.3, 0.)
      call hbook1 (351, 'K in X (Phi)', 20,0.0,6.3, 0.)
      call hbook1 (352, 'K in X (Phi)', 20,0.0,6.3, 0.)
      call hbook1 (353, 'K in X (Phi)', 20,0.0,6.3, 0.)
*
      call hbook1 (400, 'Mass all contribution', 20, 1.4, 1.8, 0.)
      call hbook1 (430, 'Gamma in Jpsi ', 16,-0.8,0.8, 0.)
      call hbook1 (431, 'Gamma in Jpsi ', 16,-0.8,0.8, 0.)
      call hbook1 (432, 'Gamma in Jpsi ', 16,-0.8,0.8, 0.)
      call hbook1 (433, 'Gamma in Jpsi ', 16,-0.8,0.8, 0.)
      call hbook1 (440, 'K in X ', 20,-1.0,1.0, 0.)
      call hbook1 (441, 'K in X ', 20,-1.0,1.0, 0.)
      call hbook1 (442, 'K in X ', 20,-1.0,1.0, 0.)
      call hbook1 (443, 'K in X ', 20,-1.0,1.0, 0.)
      call hbook1 (450, 'K in X (Phi)', 20,0.0,6.3, 0.)
      call hbook1 (451, 'K in X (Phi)', 20,0.0,6.3, 0.)
      call hbook1 (452, 'K in X (Phi)', 20,0.0,6.3, 0.)
      call hbook1 (453, 'K in X (Phi)', 20,0.0,6.3, 0.)
*
*     read in data
*
      open(66, file = 'gkk-fit.tohbk',status = 'old')
      do i=1,149971
        read(66,*) xmass, prob, wazerop, watwop, wafourp,
     +             wac, wad, wabg, xgam_jsi, xk_x, xphik_x
        call hfill (100, xmass, 0., prob)
        call hfill (110, xmass, 0., wazerop)
        call hfill (120, xmass, 0., watwop)
        call hfill (130, xgam_jsi, 0., prob)
        call hfill (140, xk_x, 0., prob)
        call hfill (150, xphik_x, 0., prob)
        if( xmass.gt.0.90.and.xmass.lt.1.40 ) then
          call hfill (131, xgam_jsi, 0., prob)
          call hfill (141, xk_x, 0., prob)
          call hfill (151, xphik_x, 0., prob)
        end if
        if( xmass.gt.1.45.and.xmass.lt.1.60 ) then
          call hfill (132, xgam_jsi, 0., prob)
          call hfill (142, xk_x, 0., prob)
          call hfill (152, xphik_x, 0., prob)
          call hfill (132, -xgam_jsi, 0., prob)
          call hfill (142, -xk_x, 0., prob)
          call hfill (152, -xphik_x, 0., prob)
        end if
        if( xmass.gt.1.65.and.xmass.lt.1.80 ) then
          call hfill (133, xgam_jsi, 0., prob)
          call hfill (833, xgam_jsi, 0., 1.)        
          call hfill (143, xk_x, 0., prob)
          call hfill (843, xk_x, 0., 1.)    
          call hfill (153, xphik_x, 0., prob)
          call hfill (191, xk_x, 0., 1.)        
          call hfill (192, xk_x, 0., wazerop)        
          call hfill (193, xk_x, 0., watwop)        
          call hfill (194, xk_x, 0., wac)        
          call hfill (195, xk_x, 0., wad)        
        end if
*
      end do
      close(66)
*
      open(77,file='real_proj.dat',status='old')
      do j=1,8585
        read(77,*) rmass, rgam_jsi, rk_x, rphik_x
        call hfill (200, rmass, 0., 1.)
        call hfill (230, rgam_jsi, 0., 1.)
        call hfill (240, rk_x, 0., 1.)
        call hfill (250, rphik_x, 0., 1.)
        if( rmass.gt.0.90.and.rmass.lt.1.40 ) then
          call hfill (231, rgam_jsi, 0., 1.)
          call hfill (241, rk_x, 0., 1.)
          call hfill (251, rphik_x, 0., 1.)
        end if
        if( rmass.gt.1.45.and.rmass.lt.1.60 ) then
          call hfill (232, rgam_jsi, 0., 1.)
          call hfill (242, rk_x, 0., 1.)
          call hfill (252, rphik_x, 0., 1.)
          call hfill (232, -rgam_jsi, 0., 1.)
          call hfill (242, -rk_x, 0., 1.)
          call hfill (252, -rphik_x, 0., 1.)
        end if
        if( rmass.gt.1.60.and.rmass.lt.1.80 ) then
          call hfill (233, rgam_jsi, 0., 1.)
          call hfill (243, rk_x, 0., 1.)
          call hfill (253, rphik_x, 0., 1.)
        end if
      end do
      close(77)
*
      open(88,file='bg1_proj.dat',status='old')
      do k=1,126
        read(88,*) b1mass, b1gam_jsi, b1k_x, b1phik_x
        call hfill (300, b1mass, 0., 1.)
        call hfill (330, b1gam_jsi, 0., 1.)
        call hfill (340, b1k_x, 0., 1.)
        call hfill (350, b1phik_x, 0., 1.)
        if( b1mass.gt.0.90.and.b1mass.lt.1.40 ) then
          call hfill (331, b1gam_jsi, 0., 1.)
          call hfill (341, b1k_x, 0., 1.)
          call hfill (351, b1phik_x, 0., 1.)
        end if
        if( b1mass.gt.1.45.and.b1mass.lt.1.60 ) then
          call hfill (332, b1gam_jsi, 0., 1.)
          call hfill (342, b1k_x, 0., 1.)
          call hfill (352, b1phik_x, 0., 1.)
          call hfill (332, -b1gam_jsi, 0., 1.)
          call hfill (342, -b1k_x, 0., 1.)
          call hfill (352, -b1phik_x, 0., 1.)
        end if
        if( b1mass.gt.1.65.and.b1mass.lt.1.80 ) then
          call hfill (333, b1gam_jsi, 0., 1.)
          call hfill (343, b1k_x, 0., 1.)
          call hfill (353, b1phik_x, 0., 1.)
        end if
      end do
      close(88)
*
      open(99,file='bg2_proj.dat',status='old')
      do l=1,422
        read(99,*) b2mass, b2gam_jsi, b2k_x, b2phik_x
        call hfill (400, b2mass, 0., 1.)
        call hfill (430, b2gam_jsi, 0., 1.)
        call hfill (440, b2k_x, 0., 1.)
        call hfill (450, b2phik_x, 0., 1.)
        if( b2mass.gt.0.90.and.b2mass.lt.1.40 ) then
          call hfill (431, b2gam_jsi, 0., 1.)
          call hfill (441, b2k_x, 0., 1.)
          call hfill (451, b2phik_x, 0., 1.)
        end if
        if( b2mass.gt.1.45.and.b2mass.lt.1.60 ) then
          call hfill (432, b2gam_jsi, 0., 1.)
          call hfill (442, b2k_x, 0., 1.)
          call hfill (452, b2phik_x, 0., 1.)
          call hfill (432, -b2gam_jsi, 0., 1.)
          call hfill (442, -b2k_x, 0., 1.)
          call hfill (452, -b2phik_x, 0., 1.)
        end if
        if( b2mass.gt.1.65.and.b2mass.lt.1.80 ) then
          call hfill (433, b2gam_jsi, 0., 1.)
          call hfill (443, b2k_x, 0., 1.)
          call hfill (453, b2phik_x, 0., 1.)
        end if
      end do
      close(99)
*
*     send histogram to histogram file
*
      call hropen(96,'gkkfit','gkk-fit.hbk','n', 1024, istat)
      call hrout(0,icycle,' ')
      call hrend('gkkfit')
      close (96)
*
      end
