aboutsummaryrefslogtreecommitdiff
path: root/src/hdf5_utils.c
blob: 5fc79adb926ac7581d407eaa3ecb18092405727d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include "hdf5_utils.h"
#include "hdf5.h"
#include <string.h> /* for strlen() */

char *GitSHA1(void);
char *GitDirty(void);

/* Write out data to an HDF5 file.
 *
 * This function writes out three datasets to an HDF5 file:
 *
 * 1. ev: the event dataset
 *
 *    This dataset contains event level information similar to the EV bank in
 *    SNOMAN. It also contains a data cleaning word and the FTP, FTK, and RSP
 *    fit values for the position and energy.
 *
 * 2. mcgn: the primary particle tracks
 *
 *     This dataset contains vertex and track information for all the primary
 *     particle tracks used to simulate the event.
 *
 * 3. fits: the fit information
 *
 *     This dataset contains the reconstructed fit information for each event.
 *     Note that this dataset will in general contain multiple rows for a
 *     single event since we fit for different particle hypotheses.
 *
 * Currently the ev and mcgn datasets are fixed size, and the fits dataset is
 * chunked and extensible to allow the cat-grid-jobs script to work.
 *
 */
int save_output(const char *output, HDF5Event *hdf5_events, size_t nevents, HDF5MCGN *hdf5_mcgn, size_t nmcgn, HDF5Fit *hdf5_fits, size_t nfits, HDF5RHDR *hdf5_rhdr, size_t nrhdr)
{
    hid_t      hdf5_events_tid;     /* File datatype identifier */
    hid_t      file, dataset, space; /* Handles */
    herr_t     status;
    hsize_t    dim[1];   /* Dataspace dimensions */
    hid_t      hdf5_mcgn_tid;     /* File datatype identifier */
    hid_t      dataset_mcgn, space_mcgn; /* Handles */
    hsize_t    dim_mcgn[1];   /* Dataspace dimensions */
    hid_t      hdf5_rhdr_tid;     /* File datatype identifier */
    hid_t      dataset_rhdr, space_rhdr; /* Handles */
    hsize_t    dim_rhdr[1];   /* Dataspace dimensions */
    hid_t      hdf5_fits_tid;     /* File datatype identifier */
    hid_t      dataset_fits, space_fits; /* Handles */
    hsize_t    dim_fits[1];   /* Dataspace dimensions */
    hsize_t    chunk_dims[1];   /* Dataspace dimensions */
    hid_t      dcpl_id; /* Handles */
    hsize_t    maxdims[1] = {H5S_UNLIMITED};

    /*
     * Create the data space.
     */
    dim[0] = nevents;
    space = H5Screate_simple(1, dim, NULL);
    dim_mcgn[0] = nmcgn;
    space_mcgn = H5Screate_simple(1, dim_mcgn, NULL);
    dim_rhdr[0] = nrhdr;
    space_rhdr = H5Screate_simple(1, dim_rhdr, NULL);
    dim_fits[0] = nfits;
    space_fits = H5Screate_simple(1, dim_fits, maxdims);
    chunk_dims[0] = 10;
    dcpl_id = H5Pcreate(H5P_DATASET_CREATE);
    H5Pset_chunk(dcpl_id, 1, chunk_dims);

    /*
     * Create the file.
     */
    file = H5Fcreate(output, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);

    hid_t   attr1, attr2; /* Attribute identifiers */
    hid_t   aid1, aid2;    /* Attribute dataspace identifiers */
    hid_t   atype1, atype2;    /* Attribute type */

    char *git_sha1 = GitSHA1();
    char *git_dirty = GitDirty();

    /*
     * Create string attribute.
     */
    aid1  = H5Screate(H5S_SCALAR);
    atype1 = H5Tcopy(H5T_C_S1);
             H5Tset_size(atype1, strlen(git_sha1));
             H5Tset_strpad(atype1,H5T_STR_NULLTERM);
    attr1 = H5Acreate2(file, "git_sha1", atype1, aid1, H5P_DEFAULT, H5P_DEFAULT);

    /*
     * Create string attribute.
     */
    aid2  = H5Screate(H5S_SCALAR);
    atype2 = H5Tcopy(H5T_C_S1);
             H5Tset_size(atype2, strlen(git_dirty));
             H5Tset_strpad(atype2,H5T_STR_NULLTERM);
    attr2 = H5Acreate2(file, "git_dirty", atype2, aid2, H5P_DEFAULT, H5P_DEFAULT);

    herr_t  ret;                /* Return value */

    /*
     * Write string attribute.
     */
    ret = H5Awrite(attr1, atype1, git_sha1);
    ret = H5Awrite(attr2, atype2, git_dirty);

    /*
     * Create the memory data type.
     */
    hdf5_rhdr_tid = H5Tcreate(H5T_COMPOUND, sizeof(HDF5RHDR));
    H5Tinsert(hdf5_rhdr_tid, "run",        HOFFSET(HDF5RHDR, run),        H5T_NATIVE_INT);
    H5Tinsert(hdf5_rhdr_tid, "run_mask",   HOFFSET(HDF5RHDR, run_mask),   H5T_NATIVE_UINT32);
    H5Tinsert(hdf5_rhdr_tid, "first_gtid", HOFFSET(HDF5RHDR, first_gtid), H5T_NATIVE_UINT32);

    hdf5_events_tid = H5Tcreate(H5T_COMPOUND, sizeof(HDF5Event));
    H5Tinsert(hdf5_events_tid, "run",        HOFFSET(HDF5Event, run),        H5T_NATIVE_INT);
    H5Tinsert(hdf5_events_tid, "sub_run",    HOFFSET(HDF5Event, sub_run),    H5T_NATIVE_INT);
    H5Tinsert(hdf5_events_tid, "evn",        HOFFSET(HDF5Event, evn),        H5T_NATIVE_INT);
    H5Tinsert(hdf5_events_tid, "gtr",        HOFFSET(HDF5Event, gtr),        H5T_NATIVE_DOUBLE);
    H5Tinsert(hdf5_events_tid, "jdy",        HOFFSET(HDF5Event, jdy),        H5T_NATIVE_INT);
    H5Tinsert(hdf5_events_tid, "ut1",        HOFFSET(HDF5Event, ut1),        H5T_NATIVE_INT);
    H5Tinsert(hdf5_events_tid, "ut2",        HOFFSET(HDF5Event, ut2),        H5T_NATIVE_INT);
    H5Tinsert(hdf5_events_tid, "dte",        HOFFSET(HDF5Event, dte),        H5T_NATIVE_INT);
    H5Tinsert(hdf5_events_tid, "hmsc",       HOFFSET(HDF5Event, hmsc),       H5T_NATIVE_INT);
    H5Tinsert(hdf5_events_tid, "nhit",       HOFFSET(HDF5Event, nhit),       H5T_NATIVE_INT);
    H5Tinsert(hdf5_events_tid, "nhit_cal",   HOFFSET(HDF5Event, nhit_cal),   H5T_NATIVE_INT);
    H5Tinsert(hdf5_events_tid, "gtid",       HOFFSET(HDF5Event, gtid),       H5T_NATIVE_UINT32);
    H5Tinsert(hdf5_events_tid, "trg_type",   HOFFSET(HDF5Event, trg_type),   H5T_NATIVE_UINT32);
    H5Tinsert(hdf5_events_tid, "dc",         HOFFSET(HDF5Event, dc),         H5T_NATIVE_UINT32);
    H5Tinsert(hdf5_events_tid, "ftp_x",      HOFFSET(HDF5Event, ftp_x),      H5T_NATIVE_DOUBLE);
    H5Tinsert(hdf5_events_tid, "ftp_y",      HOFFSET(HDF5Event, ftp_y),      H5T_NATIVE_DOUBLE);
    H5Tinsert(hdf5_events_tid, "ftp_z",      HOFFSET(HDF5Event, ftp_z),      H5T_NATIVE_DOUBLE);
    H5Tinsert(hdf5_events_tid, "ftk_energy", HOFFSET(HDF5Event, ftk_energy), H5T_NATIVE_DOUBLE);
    H5Tinsert(hdf5_events_tid, "rsp_energy", HOFFSET(HDF5Event, rsp_energy), H5T_NATIVE_DOUBLE);

    hdf5_mcgn_tid = H5Tcreate(H5T_COMPOUND, sizeof(HDF5MCGN));
    H5Tinsert(hdf5_mcgn_tid, "run",    HOFFSET(HDF5MCGN, run),    H5T_NATIVE_INT);
    H5Tinsert(hdf5_mcgn_tid, "evn",    HOFFSET(HDF5MCGN, evn),    H5T_NATIVE_INT);
    H5Tinsert(hdf5_mcgn_tid, "id",     HOFFSET(HDF5MCGN, id),     H5T_NATIVE_INT);
    H5Tinsert(hdf5_mcgn_tid, "energy", HOFFSET(HDF5MCGN, energy), H5T_NATIVE_DOUBLE);
    H5Tinsert(hdf5_mcgn_tid, "x",      HOFFSET(HDF5MCGN, x),      H5T_NATIVE_DOUBLE);
    H5Tinsert(hdf5_mcgn_tid, "y",      HOFFSET(HDF5MCGN, y),      H5T_NATIVE_DOUBLE);
    H5Tinsert(hdf5_mcgn_tid, "z",      HOFFSET(HDF5MCGN, z),      H5T_NATIVE_DOUBLE);
    H5Tinsert(hdf5_mcgn_tid, "dirx",   HOFFSET(HDF5MCGN, dirx),   H5T_NATIVE_DOUBLE);
    H5Tinsert(hdf5_mcgn_tid, "diry",   HOFFSET(HDF5MCGN, diry),   H5T_NATIVE_DOUBLE);
    H5Tinsert(hdf5_mcgn_tid, "dirz",   HOFFSET(HDF5MCGN, dirz),   H5T_NATIVE_DOUBLE);
    H5Tinsert(hdf5_mcgn_tid, "time",   HOFFSET(HDF5MCGN, time),   H5T_NATIVE_DOUBLE);

    hdf5_fits_tid = H5Tcreate(H5T_COMPOUND, sizeof(HDF5Fit));
    H5Tinsert(hdf5_fits_tid, "run",     HOFFSET(HDF5Fit, run),     H5T_NATIVE_INT);
    H5Tinsert(hdf5_fits_tid, "gtid",    HOFFSET(HDF5Fit, gtid),    H5T_NATIVE_UINT32);
    H5Tinsert(hdf5_fits_tid, "n",       HOFFSET(HDF5Fit, n),       H5T_NATIVE_INT);
    H5Tinsert(hdf5_fits_tid, "x",       HOFFSET(HDF5Fit, x),       H5T_NATIVE_DOUBLE);
    H5Tinsert(hdf5_fits_tid, "y",       HOFFSET(HDF5Fit, y),       H5T_NATIVE_DOUBLE);
    H5Tinsert(hdf5_fits_tid, "z",       HOFFSET(HDF5Fit, z),       H5T_NATIVE_DOUBLE);
    H5Tinsert(hdf5_fits_tid, "t0",      HOFFSET(HDF5Fit, t0),      H5T_NATIVE_DOUBLE);
    H5Tinsert(hdf5_fits_tid, "id1",     HOFFSET(HDF5Fit, id1),     H5T_NATIVE_INT);
    H5Tinsert(hdf5_fits_tid, "energy1", HOFFSET(HDF5Fit, energy1), H5T_NATIVE_DOUBLE);
    H5Tinsert(hdf5_fits_tid, "theta1",  HOFFSET(HDF5Fit, theta1),  H5T_NATIVE_DOUBLE);
    H5Tinsert(hdf5_fits_tid, "phi1",    HOFFSET(HDF5Fit, phi1),    H5T_NATIVE_DOUBLE);
    H5Tinsert(hdf5_fits_tid, "id2",     HOFFSET(HDF5Fit, id2),     H5T_NATIVE_INT);
    H5Tinsert(hdf5_fits_tid, "energy2", HOFFSET(HDF5Fit, energy2), H5T_NATIVE_DOUBLE);
    H5Tinsert(hdf5_fits_tid, "theta2",  HOFFSET(HDF5Fit, theta2),  H5T_NATIVE_DOUBLE);
    H5Tinsert(hdf5_fits_tid, "phi2",    HOFFSET(HDF5Fit, phi2),    H5T_NATIVE_DOUBLE);
    H5Tinsert(hdf5_fits_tid, "id3",     HOFFSET(HDF5Fit, id3),     H5T_NATIVE_INT);
    H5Tinsert(hdf5_fits_tid, "energy3", HOFFSET(HDF5Fit, energy3), H5T_NATIVE_DOUBLE);
    H5Tinsert(hdf5_fits_tid, "theta3",  HOFFSET(HDF5Fit, theta3),  H5T_NATIVE_DOUBLE);
    H5Tinsert(hdf5_fits_tid, "phi3",    HOFFSET(HDF5Fit, phi3),    H5T_NATIVE_DOUBLE);
    H5Tinsert(hdf5_fits_tid, "fmin",    HOFFSET(HDF5Fit, fmin),    H5T_NATIVE_DOUBLE);
    H5Tinsert(hdf5_fits_tid, "time",    HOFFSET(HDF5Fit, time),    H5T_NATIVE_DOUBLE);
    H5Tinsert(hdf5_fits_tid, "psi",     HOFFSET(HDF5Fit, psi),     H5T_NATIVE_DOUBLE);

    /*
     * Create the dataset.
     */
    dataset = H5Dcreate2(file, "ev", hdf5_events_tid, space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
    dataset_rhdr = H5Dcreate2(file, "rhdr", hdf5_rhdr_tid, space_rhdr, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
    dataset_mcgn = H5Dcreate2(file, "mcgn", hdf5_mcgn_tid, space_mcgn, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
    dataset_fits = H5Dcreate2(file, "fits", hdf5_fits_tid, space_fits, H5P_DEFAULT, dcpl_id, H5P_DEFAULT);

    /*
     * Wtite data to the dataset;
     */
    if (nevents)
        status = H5Dwrite(dataset, hdf5_events_tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, hdf5_events);
    if (nmcgn)
        status = H5Dwrite(dataset_mcgn, hdf5_mcgn_tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, hdf5_mcgn);
    if (nrhdr)
        status = H5Dwrite(dataset_rhdr, hdf5_rhdr_tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, hdf5_rhdr);
    if (nfits)
        status = H5Dwrite(dataset_fits, hdf5_fits_tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, hdf5_fits);

    /*
     * Release resources
     */
    H5Sclose(aid1);
    H5Sclose(aid2);
    H5Tclose(atype1);
    H5Tclose(atype2);
    H5Aclose(attr1);
    H5Aclose(attr2);
    H5Tclose(hdf5_events_tid);
    H5Tclose(hdf5_mcgn_tid);
    H5Tclose(hdf5_rhdr_tid);
    H5Tclose(hdf5_fits_tid);
    H5Sclose(space);
    H5Sclose(space_mcgn);
    H5Sclose(space_rhdr);
    H5Sclose(space_fits);
    H5Pclose(dcpl_id);
    H5Dclose(dataset);
    H5Dclose(dataset_mcgn);
    H5Dclose(dataset_rhdr);
    H5Dclose(dataset_fits);
    H5Fclose(file);

    return 0;
}