Mhformat: Difference between revisions

From MidasWiki
Jump to navigation Jump to search
No edit summary
(The midas documentation has moved to https://midas.triumf.ca/docs/)
Tag: Replaced
 
Line 1: Line 1:
{{Pagelinks}}
The midas documentation has moved. See [https://midas.triumf.ca/docs/develop/reference/reference/history_formats/#legacy-midas-driver-hst-idx-and-idf-files Legacy midas driver (.hst, .idx and .idf files)] on the History file formats and database schema page for the new documentation about the format of midas .hst history files.
= Links =
<div style="column-count:3;-moz-column-count:3;-webkit-column-count:3">
* [[History System]]
* [[mh2sql]]
</div>
MIDAS history data is recorded into the MIDAS .hst files. These files are formatted like this:
<table border="1">
<tr>
<td rowspan="13">definition record</td>
<td rowspan="6">HIST_RECORD</td>
</tr>
<tr>
<td>uint32_t record_type</td>
<td>0x46445348 (RT_DEF)</td>
</tr>
<tr>
<td>uint32_t event_id</td>
<td>history event id</td>
</tr>
<tr>
<td>uint32_t time</td>
<td>unix time</td>
</tr>
<tr>
<td>uint32_t def_offset</td>
<td>0</td>
</tr>
<tr>
<td>uint32_t data_size</td>
<td>size in bytes of following data</td>
</tr>
<tr>
<td>event name</td>
<td>char name[32]</td>
<td>name of this event</td>
</tr>
<tr>
<td rowspan="4">TAG</td>
</tr>
<tr>
<td>char name[32]</td>
<td>tag name</td>
</tr>
<tr>
<td>uint32_t type</td>
<td>type of the data TID_xxx</td>
</tr>
<tr>
<td>uint32_t n_data</td>
<td>array size</td>
</tr>
<tr>
<td>TAG</td>
</tr>
<tr>
<td>...</td>
</tr>
<tr>
<td rowspan="7">data record</td>
<td rowspan="6">HIST_RECORD</td>
</tr>
<tr>
<td>uint32_t record_type</td>
<td>0x41445348 (RT_DATA)</td>
</tr>
<tr>
<td>uint32_t event_id</td>
<td>history event id</td>
</tr>
<tr>
<td>uint32_t time</td>
<td>unix time</td>
</tr>
<tr>
<td>uint32_t def_offset</td>
<td>location of the definition record for this event in the .def file (???)</td>
</tr>
<tr>
<td>uint32_t data_size</td>
<td>size in bytes of following data</td>
</tr>
<tr>
<td>event data</td>
<td>char[data_size]</td>
<td>history data for this event</td>
</tr>
<tr>
<td>data record</td>
</tr>
<tr>
<td>...</td>
</tr>
<tr>
<td>definition record</td>
</tr>
<tr>
<td>data record</td>
</tr>
<tr>
<td>...</td>
</tr>
</table>
<p>To decode the "event data" blob in a data record, one should parse the data definition for this event and remember the contents of all tag definitions. One way to parse the data is by computing the "offset" for each tag by adding up the tag size for each tag. For each tag, the corresponding data size is the product of n_data and the size of the MIDAS type, given in this table (extracted from midas.c):</p>
<pre>/* data type sizes */
int tid_size[] = {
  0,                          /* tid == 0 not defined                              */
  1,                          /* TID_BYTE      unsigned byte        0      255    */
  1,                          /* TID_SBYTE    signed byte        -128      127    */
  1,                          /* TID_CHAR      single character      0      255    */
  2,                          /* TID_WORD      two bytes            0      65535  */
  2,                          /* TID_SHORT    signed word        -32768    32767  */
  4,                          /* TID_DWORD    four bytes            0      2^32-1  */
  4,                          /* TID_INT      signed dword        -2^31    2^31-1  */
  4,                          /* TID_BOOL      four bytes bool      0        1    */
  4,                          /* TID_FLOAT    4 Byte float format                  */
  8,                          /* TID_DOUBLE    8 Byte float format                  */
  1,                          /* TID_BITFIELD  8 Bits Bitfield    00000000 11111111 */
  0,                          /* TID_STRING    zero terminated string              */
  0,                          /* TID_ARRAY    variable length array of unkown type */
  0,                          /* TID_STRUCT    C structure                          */
  0,                          /* TID_KEY      key in online database              */
  0                            /* TID_LINK      link in online database              */
};</pre>
<p>
        Example code for computing tag offsets:</p>
<pre>int offset = 0;
for (int itag=0; itag&lt;ntags; itag++) {
  int size = tags[itag].n_data * tid_size[tags[itag].type];
  type_for_tag[itag] = tags[itag].type;
  offset_for_tag[itag] = offset;
  offset += size;
}</pre>
<p>
        Example code for decoding the event data blob:</p>
<pre>char *buf = new char[data_size];
fread(buf, 1, data_size, f); // read data blob from file
 
int itag = ...; // the tag we want to look at
int j    = ...; // array index inside the tag
 
int offset = offset_for_tag[itag];
void* ptr = (void*)(buf+offset);
 
switch (type_for_tag[itag]) {
  default:
      printf("unknownType%d ",type_for_tag[itag]);
      break;
  case 6: /* DWORD */
      printf("%u ",((uint32_t*)ptr)[j]);
      break;
}</pre>
<p>
        For a complete working example of reading MIDAS history files, look at mhdump.cxx</p>
 
[[Category:History]]

Latest revision as of 22:01, 30 October 2026

The midas documentation has moved. See Legacy midas driver (.hst, .idx and .idf files) on the History file formats and database schema page for the new documentation about the format of midas .hst history files.