Previous: Tutorial
Up: Tutorial
Next: .dspackrc file
Previous Page: Tutorial
Next Page: .dspackrc file
Structures are used widely in C. Most modern Fortran compilers also provide structures, records and pointers, although there are some (platform dependent) limitations. Throughout this manual we use examples tested with f77 compiler on SunOS and HPUX, although they should also work on other platforms.
Example of user's structure in Fortran
structure/track_t/ real x_start real y_start real z_start real px real py real pz real dx real dy real dz real dpx real dpy real dpz real charge real chisq integer n_hits integer flag1 integer flag2 integer point_p ! pointer to first hit integer vertex ! pointer to vertex structure integer nextvt_p ! pointer to next vertex track end structure
Similarly one can define a track in C:
Example of user's structure in c
typedef struct track_t { float x_start; float y_start; float z_start; float px; float py; float pz; float dx; float dy; float dz; float dpx; float dpy; float dpz; float charge; float chisq; int n_hits; int flag1; int flag2; point_t *point_p; /* pointer to first hit */ vertex_t *vertex_p; /* pointer to vertex structure */ track_t *nextvt_p; /* pointer to next vertex track */ };
The definitions above describe the same object and it is easy to map between them by passing a pointer between programs. The problem arises when we need to write data into a disk file or tape or when we want to access the data interactively.
In DSPACK the track structure may be defined in a similar way:
Structure definition in DSPACK
DEFINE track_t S 1 ! define structure track_t, section 1 .REAL4 x_start .REAL4 y_start .REAL4 z_start .REAL4 px .REAL4 py .REAL4 pz .REAL4 dx .REAL4 dy .REAL4 dz .REAL4 dpx .REAL4 dpy .REAL4 dpz .REAL4 charge .REAL4 chisq .INT4 n_hits .INT4 flag1 .INT4 flag2 point_t *point_p ! pointer to first hit vertex_t *vertex_p ! pointer to vertex structure track_t *nextvt_p ! pointer to next vertex track END DEFINE
Some items in this example require explanation.
DEFINE .INT4 I 0 ! A template of type INTEGER "Integer number" I ! Comment instead of type END DEFINE
Just like in C or Fortran we can simplify the definition by grouping elements:
Structure definition in DSPACK
DEFINE V3 R 0 ! A template for three real numbers "X component" x "Y component" y "Z component" z END DEFINEDEFINE track_t S 1 ! define structure track_t, section 1 V3 start ! start position V3 p ! momentum V3 d ! position errors V3 dp ! momentum errors .REAL4 charge .REAL4 chisq .INT4 n_hits .INT4 flags(2) point_t *point_p ! pointer to first hit vertex_t *vertex_p ! pointer to vertex structure track_t *nextvt_p ! pointer to next vertex track END DEFINE
To see how this works you need a little program which will read the definitions and interpret them.
example_1.f - test program for example_1
program example_1 * * ******************************************************************** * * * *$$ * program example_1 DSPACK tutorial example_1 * * * * * * Ryszard Zybert Jul 1 12:58:10 1994 * * ******************************************************************** ** Initialise DSPACK, local mode, 2MB memory
call dsinit(0,'example_1',6,500000,50000,ierr)
* Call ds_examine and do the rest interactively
call ds_examine(' ')
end
This program and the definition file are kept in $DSPACK_HOME/doc/tutorial.
The example session below demonstrates how it may be used.
Example session with example_1
tmp> mkdir dspack_test tmp> setenv DSPACK_HOME /na49/dspack/newtmp> cd $DSPACK_HOME/doc /na49/dspack/new/doc
doc> tar cf - tutorial | (cd /data/na49/tmp/dspack_test; tar xf - )
doc> cd /data/na49/tmp/dspack_test/tutorial/examples /data/na49/tmp/dspack_test/tutorial/examples
examples> make example_1 f77 -c -xl -g -DSUN -I/na49/dspack/new/inc -o example_1.o example_1.f example_1.f: MAIN example_1: f77 -o example_1 -xl -g -DSUN -I/na49/dspack/new/inc example_1.o /na49/dspack/new/lib/libdspack.a /na49/dspack/new/lib/zemu.a /na49/dspack/new/lib/libdspack.a `cernlib genlib packlib kernlib`
examples> example_1 DSPACK 1.129, 30 Jun 1994 dsdb> errors ! enable all error messages and warnings Errors enabled dsdb> echo ! enable echoing of *.d files to the screen Echo on dsdb> read dspack_tut_1.d * * ******************************************************************** * * * *$$ * dspack_tut_1.d DSPACK tutorial - example 1 * * * * * ******************************************************************** * DEFINE V3 R 0 ! A template for three real numbers "X component" x "Y component" y "Z component" z END DEFINE
DEFINE track_t S 1 ! define structure track_t, section 1 V3 start ! start position V3 p ! momentum V3 d ! position errors V3 dp ! momentum errors .REAL4 charge .REAL4 chisq .INT4 n_hits .INT4 flags(2) point_t *point_p ! pointer to first hit vertex_t *vertex_p ! pointer to vertex structure track_t *nextvt_p ! pointer to next vertex track END DEFINE DS_FIXPNT Warning: Void pointer: point_t DS_FIXPNT Warning: From: track_t DS_FIXPNT Warning: Void pointer: vertex_t DS_FIXPNT Warning: From: track_t
dsdb> ls -l Name Type Id Sect Size Items Nent Bytes V3 R 17 0 3 3 0 0 track_t S 18 1 20 11 0 0
dsdb> c ! use C mode to display definitions dsdb> track_t Definition of data set: track_t (trac) ID: 18 Type: Structure Section: 1 Machine words: 20 Items: 11 1 start V3 2 p V3 3 d V3 4 dp V3 5 charge .REAL4 6 chisq .REAL4 7 n_hits .INT4 8 flags(2) .INT4 9 *point_p point_t 10 *vertex_p vertex_t 11 *nextvt_p track_t
typedef struct V3 { float x; float y; float z; }; typedef struct track_t { struct V3 start; struct V3 p; struct V3 d; struct V3 dp; float charge; float chisq; int n_hits; int flags[2]; struct point_t *point_p; struct vertex_t *vertex_p; struct track_t *nextvt_p; }; dsdb> quit
examples>
Reading the file produced warnings because two of the pointers defined in the structure refer to unknown objects. We can define the needed objects later, otherwise the pointers will always be set to 0.