*
* $Id: gpfil.F,v 3.5 2005/12/05 12:15:20 pam-ba Exp $
*
* $Log: gpfil.F,v $
* Revision 3.5  2005/12/05 12:15:20  pam-ba
* new spectrometer geometry and internal magnetic field
*
* Revision 3.3  2005/06/21 02:41:46  cafagna
* Major modification to the geometry and to the random number chain
*
* Revision 3.2  2003/12/17 11:32:47  pamela
* CALO SIMULATION COMPLETED: geometry and special tracking parameters updated and simulation checked by a comparison with the Trieste's standalone Monte Carlo simulation
*
* Revision 3.1.1.1  2002/07/11 16:02:00  cafagna
* First GPAMELA release on CVS
*
*
*               14/04/2005  12:59     by Marialuigia Ambriola
*CMZ :  3.00/00 30/03/2001  14.16.15  by  Marialuigia Ambriola
*CMZ :  2.01/00 08/03/2000  18.33.09  by  Unknown
*CMZ :  2.00/00 03/03/2000  15.39.05  by  Francesco Cafagna
*CMZ :  1.02/00 22/10/99  17.53.59  by  Francesco Cafagna
*CMZ :  1.01/00 02/05/96  09.43.56  by  Francesco Cafagna
*CMZ :  1.00/02 21/02/96  10.12.34  by  Francesco Cafagna
*-- Author :    Francesco Cafagna   30/11/95
      SUBROUTINE GPFIL
************************************************************************
*                                                                      *
*            To open FFREAD and HBOOK files
*
* Called by: GPAMELA,GXINT
*                                                                      *
* Author: Francesco Cafagna, 30/11/95 15.53.48                         *
*                                                                      *
************************************************************************
#include "gppntp.inc"
#include "gpunit.inc"
*
#include "gcflag.inc"
#include "gcbank.inc"
*ML: 3/5/03
#include "gpkey.inc"
#include "gpseed.inc"
#include "gcunit.inc"
*END ML
      LOGICAL EXISTS
      INTEGER IJUNK
      CHARACTER*160 FNAM
      CHARACTER*4 SUF
*
* User action
*
      CALL GPUFIL
*
*             Open a HBOOK4 direct access file
*
      CALL HROPEN(LUHBK,'GPAMELA',CHHBK,'N',NBSIZE,ISTAT)
*
*             Open the magnetic field map
*
C fc +SELF,IF=UNIX.
C fc       OPEN(UNIT=LUFMAP,FILE=CHFMAP,FORM='UNFORMATTED',
C fc      +     STATUS='OLD')
C fc +SELF,IF=-UNIX.
C fc       OPEN(UNIT=LUFMAP,FILE=CHFMAP,FORM='UNFORMATTED',SHARED,
C fc      +     STATUS='OLD',READONLY)
C fc +SELF.
*ML:
CML17/04/2003      CALL HROPEN(LUFMAP,'FIELD',CHFMAP,' ',NBSIZE,ISTAT)
C      CALL HROPEN(LUFMAP,'FIELD',CHFMAP,' ',1024,ISTAT)
C*END ML.

      CALL HROPEN(LUFMAP1,'FIELD1',CHFMAP1,' ',1024,ISTAT)
      IF(ISTAT.NE.0) PRINT*,'ERROR WHILE OPENING MAGN. MAP FILE 1'

      CALL HROPEN(LUFMAP2,'FIELD2',CHFMAP2,' ',1024,ISTAT)
      IF(ISTAT.NE.0) PRINT*,'ERROR WHILE OPENING MAGN. MAP FILE 2'

      CALL HROPEN(LUFMAP3,'FIELD3',CHFMAPEX,' ',1024,ISTAT)
      IF(ISTAT.NE.0) PRINT*,'ERROR WHILE OPENING MAGN. MAP FILE EX'
     




C
C open file with spe hit resolution histograms
C      
      CALL HROPEN(LURSPE,'RESSPE',CHRSPE,' ',1024,istat)

*
*ML: (to simulate the TR from TRD)
      OPEN(UNIT=LUTR,FILE=CHTR,FORM='UNFORMATTED',STATUS='UNKNOWN')
*END ML.
*Ml 27/04/04:
      IF(HSEED.EQ.1.OR.HSEED.EQ.2) THEN
         INQUIRE(FILE=CHFSF,EXIST=EXISTS)
         IF(EXISTS) THEN
            OPEN(UNIT=LUFSF,FILE=CHFSF,FORM='UNFORMATTED',STATUS='OLD')
            WRITE(CHMAIL,10000)HSEED
            CALL GMAIL(1,0)
         ELSE 
            WRITE(CHMAIL,10001)
            CALL GMAIL(1,0)
            STOP
         ENDIF
      ENDIF
      IF(DCLSFI) THEN
         FNAM=CHLSF
      ELSE
         SUF='SEED'
         IJUNK=INDEX(CHHBK,'HIS')
         FNAM = CHHBK(1:IJUNK-1)//SUF         
      ENDIF
C     OPEN(UNIT=LULSF,FILE=CHLSF,FORM='UNFORMATTED',STATUS='UNKNOWN')
      OPEN(UNIT=LULSF,FILE=FNAM,FORM='UNFORMATTED',STATUS='UNKNOWN')
*end ml. 27/04/04
10000 FORMAT(' GPFIL: data card HSEED used with value = ',I1,
     + '. Input file of random seed opened. ')
10001 FORMAT('GPFIL WARNING: INPUT FILE OF RANDOM SEED NOT FOUND.',
     +  '                             THE PROGRAM IS EXITING.',
     +  ' PLEASE CHECK THE CARD *FSFI.')
      END
