/
parser.go
145 lines (134 loc) · 3.02 KB
/
parser.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
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
// 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 heppdt
import (
"bufio"
"bytes"
"fmt"
"io"
"strconv"
)
// parse fills a Table from the content of r
func parse(r io.Reader, table *Table) error {
var err error
s := bufio.NewScanner(r)
lineno := 0
for s.Scan() {
lineno++
err = s.Err()
if err != nil {
break
}
bline := s.Bytes()
bline = bytes.Trim(bline, " \t\r\n")
if len(bline) <= 0 {
continue
}
if bline[0] == '#' {
continue
}
if len(bline) >= 2 && string(bline[:2]) == "//" {
continue
}
toks := bytes.Split(bline, []byte(" "))
tokens := make([]string, 0, len(toks))
for _, tok := range toks {
//fmt.Printf("--> [%s] => ", string(tok))
tok = bytes.Trim(tok, " \t\r\n")
//fmt.Printf("[%s]\n", string(tok))
if len(tok) > 0 {
tokens = append(tokens, string(tok))
}
}
if len(tokens) != 6 {
stoks := ""
for i, tok := range tokens {
stoks += fmt.Sprintf("%q", tok)
if i != len(tokens)-1 {
stoks += ", "
}
}
//fmt.Printf("** error: line %d (%d): %v\n", lineno, len(tokens), stoks)
return fmt.Errorf("heppdt: malformed line:%d: %v", lineno, string(bline))
}
var id int64
id, err = strconv.ParseInt(tokens[0], 10, 64)
if err != nil {
return fmt.Errorf("heppdt: line:%d: %w", lineno, err)
}
pid := PID(id)
name := string(tokens[1])
var icharge int64
icharge, err = strconv.ParseInt(tokens[2], 10, 64)
if err != nil {
return fmt.Errorf("heppdt: line:%d: %w", lineno, err)
}
var charge float64
// allow for Q-balls
if pid.IsQBall() {
// 10x the charge
charge = float64(icharge) * 0.01
} else {
// 3x the charge
const onethird = 1. / 3.0
charge = float64(icharge) * onethird
}
var mass float64
mass, err = strconv.ParseFloat(tokens[3], 64)
if err != nil {
return fmt.Errorf("heppdt: line:%d: %w", lineno, err)
}
var totwidth float64
totwidth, err = strconv.ParseFloat(tokens[4], 64)
if err != nil {
return fmt.Errorf("heppdt: line:%d: %w", lineno, err)
}
var lifetime float64
lifetime, err = strconv.ParseFloat(tokens[5], 64)
if err != nil {
return fmt.Errorf("heppdt: line:%d: %w", lineno, err)
}
res := Resonance{
Mass: Measurement{
Value: mass,
Sigma: 0,
},
}
// either width or lifetime is defined. not both.
switch {
case totwidth > 0.:
res.Width = Measurement{
Value: totwidth,
Sigma: 0,
}
case totwidth == -1.:
res.Width = Measurement{
Value: -1.,
Sigma: 0.,
}
case lifetime > 0.:
res.Width = Measurement{
Value: calcWidthFromLifetime(lifetime),
Sigma: 0.,
}
default:
res.Width = Measurement{}
}
part := Particle{
ID: pid,
Name: name,
PDG: int(id),
Mass: mass,
Charge: charge,
Resonance: res,
}
table.pdt[pid] = &part
table.pid[name] = pid
//fmt.Printf(">>> %d: [%s]\n", lineno, tokens)
}
if err == io.EOF {
err = nil
}
return err
}