/
ascii_io.go
107 lines (96 loc) · 2.98 KB
/
ascii_io.go
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
// Copyright 2017 The go-hep Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package hepevt
import (
"fmt"
"io"
)
// Encoder encodes ASCII files in the HEPEVT format.
type Encoder struct {
w io.Writer
}
// NewEncoder create a new Encoder, writing to the provided io.Writer.
func NewEncoder(w io.Writer) *Encoder {
return &Encoder{w: w}
}
// Encode encodes a full HEPEVT event to the underlying writer.
func (enc *Encoder) Encode(evt *Event) error {
_, err := fmt.Fprintf(enc.w, "%d %d\n", evt.Nevhep, evt.Nhep)
if err != nil {
return fmt.Errorf("could not encode event header line: %w", err)
}
for i := 0; i < evt.Nhep; i++ {
_, err = fmt.Fprintf(
enc.w,
"%d %d %d %d %d %d %E %E %E %E %E %E %E %E %E\n",
evt.Isthep[i],
evt.Idhep[i],
// convert 0-based indices to 1-based ones
evt.Jmohep[i][0]+1, evt.Jmohep[i][1]+1,
evt.Jdahep[i][0]+1, evt.Jdahep[i][1]+1,
// <---
evt.Phep[i][0], evt.Phep[i][1], evt.Phep[i][2], evt.Phep[i][3],
evt.Phep[i][4],
evt.Vhep[i][0], evt.Vhep[i][1], evt.Vhep[i][2], evt.Vhep[i][3],
)
if err != nil {
return fmt.Errorf("could not encode event particle line[%d]: %w", i, err)
}
}
return nil
}
// Decoder decodes ASCII files in the HEPEVT format.
type Decoder struct {
r io.Reader
}
// NewDecoder creates a new Decoder, reading from the provided io.Reader.
func NewDecoder(r io.Reader) *Decoder {
return &Decoder{r: r}
}
// Decode decodes a full HEPEVT event from the underlying reader.
func (dec *Decoder) Decode(evt *Event) error {
_, err := fmt.Fscanf(dec.r, "%d %d\n", &evt.Nevhep, &evt.Nhep)
if err != nil {
return fmt.Errorf("could not decode event header line: %w", err)
}
// resize
if len(evt.Isthep) > evt.Nhep {
evt.Isthep = evt.Isthep[:evt.Nhep]
evt.Idhep = evt.Idhep[:evt.Nhep]
evt.Jmohep = evt.Jmohep[:evt.Nhep]
evt.Jdahep = evt.Jdahep[:evt.Nhep]
evt.Phep = evt.Phep[:evt.Nhep]
evt.Vhep = evt.Vhep[:evt.Nhep]
} else {
sz := evt.Nhep - len(evt.Isthep)
evt.Isthep = append(evt.Isthep, make([]int, sz)...)
evt.Idhep = append(evt.Idhep, make([]int, sz)...)
evt.Jmohep = append(evt.Jmohep, make([][2]int, sz)...)
evt.Jdahep = append(evt.Jdahep, make([][2]int, sz)...)
evt.Phep = append(evt.Phep, make([][5]float64, sz)...)
evt.Vhep = append(evt.Vhep, make([][4]float64, sz)...)
}
for i := 0; i < evt.Nhep; i++ {
_, err = fmt.Fscanf(
dec.r,
"%d %d %d %d %d %d %E %E %E %E %E %E %E %E %E\n",
&evt.Isthep[i],
&evt.Idhep[i],
&evt.Jmohep[i][0], &evt.Jmohep[i][1],
&evt.Jdahep[i][0], &evt.Jdahep[i][1],
&evt.Phep[i][0], &evt.Phep[i][1], &evt.Phep[i][2], &evt.Phep[i][3],
&evt.Phep[i][4],
&evt.Vhep[i][0], &evt.Vhep[i][1], &evt.Vhep[i][2], &evt.Vhep[i][3],
)
if err != nil {
return fmt.Errorf("could not decode event line[%d]: %w", i, err)
}
// convert 0-based indices to 1-based ones
evt.Jmohep[i][0] -= 1
evt.Jmohep[i][1] -= 1
evt.Jdahep[i][0] -= 1
evt.Jdahep[i][1] -= 1
}
return nil
}