DBin (`dee-bee-in') DataBase INput System
User's Guide and Reference

Torre Wenaus, LLNL

Under (perpetual) construction, but hopefully useful


The basics: [Intro] - [Operation] - [f77 and C/C++] - [Generated files] - [Data types] - [Syntax]
Using DBin: [Creating a db] - [Data Struct Def'n] - [Command Def'n] - [XDR I/O] - [Running DBin]
Reference: [Internals of DBin] - [Directives Reference]
Applications: [MCFast (Fermilab)] - [BBSIM (BaBar)]
Examples: [Database] - [Data structure definition] - [Command definition]
History: [6/95 status (BaBar version)] - [Changes since]


Introduction to DBin

DBin is a tool used to define and build databases and data structures used in Fortran, C/C++, or mixed language programs in a language independent way. It generates automatically all the code necessary to initialize, read, write, and access the data structures from either language, with a user-written ASCII DBin file defining the data structures and (for the database application) serving as the data repository.

XDR-based I/O is supported: DBin automatically generates the filters and associated code necessary for XDR I/O on databases and data structures.

In a single-language program environment DBin is useful to maintain databases and data structures in an organized, easily transportable, self-documented way, with much tedious coding of I/O routines, include files, XDR filters, and so on eliminated by DBin's automatic code generation.

It is, however, in multi-language environments -- programs which either support coding in multiple languages or are migrating between languages (most commonly, from Fortran to C++) -- that DBin's utility is greatest. Without DBin such an environment can present a maintenance nightmare requiring redundant coding of Fortran and C/C++ I/O codes, careful maintenance of consistency between the Fortran and C/C++ data structures coded into the header files, multiple coding changes in multiple languages when a data structure or database object changes, etc. With DBin, the coding overhead remains the same as for a single language: zero. DBin makes a multiple language environment much more practical and maintainable.

DBin maps the Fortran and C databases/data structures onto the same memory, so that inter-language data sharing concerns are eliminated and data can be accessed from either language in a transparent way.


DBin operation

DBin works with an ASCII definition file written by the user that provides The DBin file is processed by the DBin program, the output of which is a set of source and include files for Fortran and C that provide everything needed to work with the database and/or data structures in the two languages and C++.

The DBin program is a lex program that runs on any Unix platform. Auxiliary material for DBin (mostly parsers) is in Fortran and C.

The databases and data structures generated by DBin are implemented using conventional C structures on the C side and VMS-style Fortran structures on the Fortran side. A Fortran 90 option is under consideration. If only a Fortran 90 compiler is available, a VMS to Fortran 90 structure syntax converter is available from the author and can be run on DBin's output to turn it into Fortran 90.


Use of DBin in Fortran, C and C++ programs

The database objects and data structures defined using DBin are implemented as C structures and VMS Fortran structures in the respective languages. The representation in memory of these structures, including character strings (implemented in C as char arrays) and nested structures, is identical in the two languages, permitting the C and Fortran structures to be mapped to the same memory for transparent dual-language access to the same data space.


Files generated by DBin

The following files are generated by DBin in processing a definition file given the name foo (using the database directive; see the reference section).

Fortran interface files:

foo_read.F
Database application only. Reads the database file treating it as a data source, and loads internal data structures defined in the .inc file. The database file read need not be complete; it can be only a subset, containing values different from the defaults (see next routine). Or, no database file need be read at all at runtime if the default values are desired.
foo_init.F
Database application only. Sets default values for all database parameters. Values used are those present in the file on which DBin was run to generate the code. Default values can be overridden at run time by the optional read routine (see previous routine).
foo_db.inc
The header file containing the data structures DBin has built to represent the database or data structures. Often, the incname directive is used to direct header material to one or more files with user-defined names rather than the default behaviour of everything being written to this file.

C interface files:

fooRead.c
C version of foo_read.F; see above.
fooInit.c
C version of foo_init.F; see above.
foo_db.h
C version of foo_db.inc; see above. Note that the data space that the structure definitions in the two languages map to is the same space in memory.

XDR interface files:

fooXDR.c
The XDR filter code for all defined data structures. One filter routine for each structure.
fooXDRall.c
The controller routine that calls the IO routines for all the defined structures. If not all structures should be written to XDR output, this file should be modified (appropriate structure calls commented out).
fooXDRinc.h
Needed function declarations.
fooXDRmgr.c
Manages the IO for each structure. Calls each structure's filter routine for each instantiation of the structure. One function per structure. These functions are called in the fooXDRall.c routine.

GUI interface file:

fooGUI.h
The menu definitions for Kai Mantsch's geomedit Motif-based DBin geometry file editor.


Structures, data types and directives

DBin organizes data into objects analogous to C structures (the objects are implemented as C structures in the generated code). There are two basic types of object defined by DBin. One is the structure, which is an object with only a single instantiation. This object is used only in the database application. It is used to define parameters for which there is only a single instance. (Structures are used extensively in bbsim geometry definitions, and not at all -- at present -- in MCFast). In a structure, the data values are set within the body of the structure definition.

The other object type is the template, which can be multiply instantiated (implemented as an array of C structures in the code). Data values cannot be declared within the template definition, because multiple instantiations can exist. Rather, instantiations are created and their data values set using the make directive.

In the data structure definition application, the make directive is not used, because no explicit instantiations are declared in this case.

Within structures and templates, the standard data types are available: integer, real, double precision, and character string, with keywords int, real, double, char. Arrays are permitted; singly dimensioned only, for the database application. Arrays of higher dimension are permitted in data structure definitions. Nested templates are permitted in data structure definitions. They are not permitted in databases because of the complexity it would introduce to declaring the data values.


Syntax rules


Creating a database

For the database application, a DBin file simultaneously serves as data structure definition and data repository. DBin structures contain embedded data. DBin template definitions are followed by explicit instantiations via the make directive.

By convention, DBin database files have file type .db .

The basic structure of a database file, with examples of both structures and templates, is as follows.

------------------------------------------------------------------------
database mydb  0100   ! name and version number

incname pars          ! specify pars.h, pars.inc as destination
                      ! for header material

structure pars
  char name "An example"
  real rad 320.
  real zlen 400.
  int nphi 36
  real origin(3) 10. 20. 30. 
end structure

incname plane        ! new destination: plane.h, plane.inc

template plane
  char name
  int index
  real normal(3)
  real point(3)
end template

make plane "xy" 1 0. 0. 1. 0. 0. 0.
make plane "yz" 2 1. 0. 0. 0. 0. 0.

include otherstuff.db   ! include material in another file

end
------------------------------------------------------------------------


Using DBin to define program data structures

When DBin is used to define data structures, only template definitions appear in the DBin file. No data appears in the file; hence no instantiations (make) or DBin structures (structure). The declaration mode internal must appear as the first line in the DBin file to indicate the application mode is generation of program-internal data structures rather than a database (the default usage mode).

By convention, DBin data structure definition files have file type .dst .

Because DBin does not have the job of handling actual data in this application, DBin's rules on allowed declarations are more liberal. The following are permitted in data structure definitions, but not in the database application:

An example of a simple data definition file is as follows.
------------------------------------------------------------------------
mode internal   ! usage mode = data structure def. Must be 1st line.
database mcfdat 0100
!
!  Definition of data structures used internal to MCFast
!

!******************** hit_trk ********************

incname trk_channel_struct   ! include file for structure declaration
template trk_channel
        int devtype   !Type of device
        int devnum    !Device # of this type
        int devlayer  !Layer #
        int devwid(3) !Info within layer
end template

incname hit_trk_struct   ! include file for structure declaration
provide trk_channel_struct     ! provide the channel declaration
template hit_trk
        int hep                !Position of track in /HEPEVT/ list
        int trace              !Position of hit in trace list
        nest trk_channel chan  !Device info
        double pos(3)          !Position of wire
        double dircos(3)       !Dir. cosines
end template

incname hit_trk  ! instantiations go to a separate include file
provide hit_trk_struct        ! provide the structure declaration
dimension hit_trk_max 10000    ! define the dimension parameter
record hit_trk(hit_trk_max)   ! create an array: hit_trk

incname another_hit_trk       ! create another include file
provide hit_trk_struct        ! provide the structure declaration
dimension hit2_trk_max 10000     ! define the dimension parameter
record hit_trk(hit2_trk_max) hit2_trk ! create an array, hit2_trk


end
------------------------------------------------------------------------


XDR-based I/O using FNAL's MCFIO package

XDR filter routine

XDR I/O management routine

See above discussion on generated files.


Running DBin

To run DBin:
  dbin dbfile.db [-help] [-list]         # database application
or
  dbin dstfile.dst [-help] [-list]       # data structure def'n

-help:   list available command line options
-list:   debug listing as DBin executes


Current DBin applications


The internals of DBin

Data storage

Data is stored in C as C structures, with strings represented by 80 character char arrays. In Fortran, data is represented as VMS-style Fortran structures, with strings as CHARACTER*80. Happily and probably not accidentally, the representation of the data in memory is identical in the two languages (at least on the platforms tested). This permits the data spaces of the two languages to be coincident, and this is implemented by mapping the C data space to a Fortran common block.

The DBin program

DBin is a lex program, dbin.lex. Lex is a standard unix utility used for developing parsers. Auxiliary code includes parser code for database reading in Fortran and C, dbinf.F and dbinc.c.


Directives Reference


General: [mode] [database] [include] [incname] [provide] [end]
Data Types: [integer] [real] [double] [char] [material] [dimension] [index]
Data Structures: [structure] [template] [record] [child] [nest] [parent] [make] [define]
FFREAD file inclusion: [ffread] [ffend] [any]
General file inclusion: [file] [fileend]
Command mode: [command] [call]


General directives


mode

mode internal

The mode directive is used to change the mode of operation of the program from the default database mode to something else. One other mode is presently defined: internal mode, in which DBin is used to define a program's internal data structures. If present, it must appear as the first line of the DBin file as above.


database

database db_name version

The database directive names the database; it sets the name used in filenames generated by DBin. It also defines a version number, used to ensure that DBin-generated code is used only with compatible database files (ie. with matching version number).

The database directive must appear, and should be the first directive in the DBin file (unless preceded by the mode directive).

database bbgeom 0100


include

include file_name

The include directive permits the in-line inclusion of DBin statements from another file (just like the cpp #include command). The file name will be looked for in the same directory as the parent DBin file, unless the name includes a relative or absolute path name.

include db/subsystem.db


incname

incname file_name_root

The incname directive reroutes include file code generated by DBin to a file of the specified name, rather than to DBin's master include file (the master include file instead uses #include to include these user-specified files). Using this directive the user has complete control over the partitioning of include file code among files.

The specified file name should not contain an extension. Appropriate extensions (.inc,.h) will be added by DBin for the Fortran and C/C++ versions of the file.

incname muon  ! header code directed to muon.inc, muon.h
[...DBin statements...]
incname ecal  ! header code directed to ecal.inc, ecal.h


provide

provide include_file_root

The provide directive inserts an #include statement for the named include file into the current header file. The file type should not be given. File types of .inc and .h will be added for the Fortran and C includes respectively.

provide muon ! inserts '#include "muon.[inc,h]"' into Fortran,C header


end

end

The end statement signals the end of the dbin file. It must be present. Note that included files should not contain an end statement.


Data Types

All data types can be dimensioned, eg. real array(5). In databases, only one dimensional arrays with an explicit numeric dimension are allowed. In structure definitions, multi-dimensional arrays are allowed, comma delineated. Also, dimensions can be given in terms of constants declared (previously) with the dimension directive.

In structure definitions (ie. only in databases), a type declaration is followed by the data for the parameter. A single value if undimensioned, all N values if dimensioned. In all other contexts (ie. templates, in either database or data structure def'n applications), the type declaration does not include data.

Arrayed types can be followed by the keyword variable to indicate that it is a variable size array; that is, its declared size is an upper bound on the number of elements actually used. The previous element in the template declaration should be an integer, and is interpreted as the number of elements actually used. eg.

   real hits(100) variable


integer

integer name[(dimension)] [variable]


real

real name[(dimension)] [variable]


double

double name[(dimension)] [variable]


char

char name[(dimension)] [variable]


material

material name[(dimension)] [variable]

Identical to char. Flags the string as of a special type, one specifying the name of a material. Provokes special treatment eg. in GUI menu code.


dimension

dimension name value

Acts like a PARAMETER statement in Fortran or static const statement in C (these are how it is implemented).

dimension maxsz 30


index

index prefix name1 name2 name3 ...

Sets up an array of index mnemonics useful for referencing elements of arrays with mnemonics. Handles the 0/1 array start discrepancy between Fortran and C such that the mnemonic in the given language refers to the same element. The example below defines ix_x, ix_y, ix_z, ix_px, ... to be mnemonics pointing to the 1st (0th), 2nd (1st), etc. element in an array in Fortran (C).

index ix x y z px py pz e


Data Structures


structure

structure name

Start a structure declaration. Structure is terminated by end structure.


template

template name

Start a template declaration. Template is terminated by end template.


record

record template_name[(dimension)] [alternate_name]

Data structure definition mode only. Create an instantiation of a declared template, ie. create an object in memory. Dimension is optional. By default (no third parameter), name of the created structure as the template name. The third parameter is used to specify an alternate name.

record fourvec(3) products  ! == record /fourvec_s/ products(3) in Fortran


child

child template_name

Databases only. Appears in a template declaration. Declares that the object has a child array of the stated type at the current point in the object. The preceding element should be an integer (if not another child) which is interpreted as the number of child elements in use.


nest

nest template_name[dimension] [variable]

Data structure definitions only. Equivalent to child. In the context of a data structure definition, the effect of the nest directive is to insert a nested structure in the declared structure.


parent

parent parent_template_name

Database definitions only. Appears in template definitions. If the template is a child of another template, the parent directive should be inserted in the child template definition (anywhere) to flag that the template has the named parent.


make

make template_name template_parameters

Create an instantiation of a template. Template instantiations must follow the template definition with no intervening template or structure definitions. That is, template instantiations must be grouped together and appear right after the template definition.

template point
  real x
  real y
  real z
end template
make point 1. 2. 3. 


define

Not presently used. Reserved to re-implement the old style make command that included a name for each instantiation.


FFREAD file inclusion


ffread


ffend


any


General file inclusion


file


fileend


Command mode


command


call


Applications



MCFast, Fermilab's fast simulation package

See also the MCFast extensions documentation.


bbsim, the Geant simulation of BaBar

See also the bbsim User's Guide.


Examples



Database example

See MCFast distribution, src/dbin/template.db and included template files.


Data structure definition example

See MCFast distribution, src/dst/mcfdat.dst


Command definition example


History


To add:
'index' assigns 1,2,3,4... (C: 0,1,2,3...) to the symbols. Token following 'index'
! is a prefix to attach to each subsequent symbol, eg
       ! 'index mu px py pz' defines mu_px=1, mu_py=2, mu_pz=3