*
* $Id: gprnhran.F,v 1.5 2009-06-12 18:39:58 pam-rm2 Exp $
*
* $Log: gprnhran.F,v $
* Revision 1.5  2009-06-12 18:39:58  pam-rm2
* - Introduced user-defined names of output files and random seeds number.
* Users can do it use options of PamVMCApplication constructor:
* PamVMCApplication(const char* name,  const char *title, const char*
* filename="pamtest", Int_t seed=0).
* The Random object that I use is TRandom3 object which has astronomical
* large period (in case of default initialization 0). All random generators
* in the code use this object by calling of gRandom singleton which keeps
* it.
*
* - Corrected TOF digitization routine. No problems with TDC hits due to
* hadronic interactions anymore.
*
* - Some small changes was done to compile code under Root 5.23. +
* geant4_vmc v. 2.6 without any warnings
*
* - Some classes of PamG4RunConfiguartion was changed for geant4_vmc v.
* 2.6.Some obsolete classes was deleted as soon as developers implemented
* regions.
*
* - Navigation was changed from "geomRootToGeant4" to "geomRoot", because on
* VMC web page written that as soon as Geant4 has no option ONLY/MANY
* translation of overlapped geometry to Geant4 through VGM could be wrong.
* I'd like to stay with Root navigation:
* http://root.cern.ch/root/vmc/Geant4VMC.html. This should be default
* option.
*
* - New Tracker digitization routine written by Sergio was implemented
*
* - PamVMC again became compatible with geant4_vmc v.2.5 and ROOT 5.20.
*  The problem was that ROOT developers introduced in TVirtualMC class a new
* method SetMagField and new base class:TVirtualMagField from which
* user-defined classes shoukd be derived
*
* Revision 1.1  2009-02-19 17:46:26  nikolas
* Cleaning up before releasing
*
* Revision 3.1  2005/12/06 01:06:42  cafagna
* Adding new magnetic field routines
*
* Revision 1.2  1996/04/10 16:31:41  mclareni
* NAME given dimension 2
*
* Revision 1.1  1996/04/09 13:34:34  mclareni
* Add new routine rnhran.F (V149), also to Imakefile
*
*   27/05/2005  Sergio Bottai
* MODIFIED IN ORDER TO USE GEANT RANDOM NUMBERS
*
*
      SUBROUTINE GPRNHRAN(Y,N,XLO,XWID,XRAN)
      CHARACTER*8 NAME(2)
 
      DIMENSION Y(*)
 
      DATA IERR /0/
      DATA NAME /'GPRNHRAN','GPRNHPRE'/
 
      NTRY=1
      IF(Y(N) .EQ. 1) GOTO 4
      WRITE(6,101) NAME(1),Y(N)
      GOTO 5
 
      ENTRY GPRNHPRE(Y,N)
      NTRY=2
      
    5 YTOT=0
      DO 1 I = 1,N
      IF(Y(I) .LT. 0) GOTO 9
      YTOT=YTOT+Y(I)
    1 Y(I)=YTOT
      IF(YTOT .LE. 0) GOTO 9
      YINV=1/YTOT
      DO 2 I = 1,N
    2 Y(I)=Y(I)*YINV
      Y(N)=1
      IF(NTRY .EQ. 2) RETURN
 
c    4 CALL RANLUX(YR,1)
    4 CALL GRNDMC(YR,1)
c      PRINT*,'GPRNHRAN.F calls RNDM'

      L=LOCATR(Y,N,YR)
      IF(L .LT. 0) THEN
       L=-L
       XRAN=XLO+XWID*(L+((YR-Y(L))/(Y(L+1)-Y(L))))
      ELSEIF(L .EQ. 0) THEN
       XRAN=XLO+XWID*(YR/Y(1))
      ELSE
       XRAN=XLO+L*XWID
      ENDIF
      RETURN
 
    9 IERR=IERR+1
      IF(IERR .LT. 6) WRITE(6,102) NAME(NTRY)
      WRITE(6,'(1X,10F13.7)') (Y(K),K=1,N)
      XRAN=0
      RETURN
  101 FORMAT(/7X,'+++++ CERN V149 ',A6,' : Y(N) = ',E15.6,' .NE. 1; ',
     1           'Y(I) NOT IN CUMULATIVE FORM.'/)
  102 FORMAT(/7X,'+++++ CERN V149 ',A6,' : NOT ALL VALUES Y(I) > 0'/)
      END
      
