Skip to content

Commit

Permalink
all: introduce fmom.Vec4, make fmom.PxPyPzE a struct instead of [4]fl…
Browse files Browse the repository at this point in the history
…oat64

this is for performance reasons, see:
- golang/go#15925
  • Loading branch information
sbinet committed Mar 25, 2020
1 parent d21ad2a commit 1a8accc
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 69 deletions.
12 changes: 6 additions & 6 deletions cmd/lhef2hepmc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,21 +132,21 @@ func main() {
Barcode: -1,
}
p1 := hepmc.Particle{
Momentum: fmom.PxPyPzE{
Momentum: fmom.NewPxPyPzE(
0, 0,
dec.Run.EBMUP[0],
dec.Run.EBMUP[0],
},
),
PdgID: dec.Run.IDBMUP[0],
Status: 4,
Barcode: 1,
}
p2 := hepmc.Particle{
Momentum: fmom.PxPyPzE{
Momentum: fmom.NewPxPyPzE(
0, 0,
dec.Run.EBMUP[1],
dec.Run.EBMUP[1],
},
),
PdgID: dec.Run.IDBMUP[1],
Status: 4,
Barcode: 2,
Expand All @@ -170,12 +170,12 @@ func main() {
}
nmax += 1
vtx.AddParticleOut(&hepmc.Particle{
Momentum: fmom.PxPyPzE{
Momentum: fmom.NewPxPyPzE(
lhevt.PUP[i][0],
lhevt.PUP[i][1],
lhevt.PUP[i][2],
lhevt.PUP[i][3],
},
),
GeneratedMass: lhevt.PUP[i][4],
PdgID: lhevt.IDUP[i],
Status: 1,
Expand Down
8 changes: 5 additions & 3 deletions fads/fastjet_finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,10 @@ func (tsk *FastJetFinder) Process(ctx fwk.Context) error {
dphiMax := 0.0
output = make([]Candidate, 0, len(outjets))
for i := range outjets {
jet := &outjets[i]
area := fmom.PxPyPzE{0, 0, 0, 0}
var (
jet = &outjets[i]
area fmom.PxPyPzE
)
if tsk.areaDef != nil {
// FIXME
panic("not implemented")
Expand Down Expand Up @@ -187,7 +189,7 @@ func (tsk *FastJetFinder) Process(ctx fwk.Context) error {
cand.Add(cst)
}

cand.Pos[3] = time / wtime
cand.Pos.P4.T = time / wtime
cand.Area = area
cand.DEta = detaMax
cand.DPhi = dphiMax
Expand Down
2 changes: 1 addition & 1 deletion fads/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ func newPtEtaPhiE(pt, eta, phi, ene float64) fmom.PxPyPzE {
py := pt * math.Sin(phi)
pz := pt * math.Sinh(eta)

return fmom.PxPyPzE{px, py, pz, ene}
return fmom.NewPxPyPzE(px, py, pz, ene)
}
8 changes: 4 additions & 4 deletions fmom/ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ func IAdd(dst, src P4) P4 {
default:
panic(fmt.Errorf("fmom: invalid P4 concrete value: %#v", dst))
}
p4[0] += src.Px()
p4[1] += src.Py()
p4[2] += src.Pz()
p4[3] += src.E()
p4.P4.X += src.Px()
p4.P4.Y += src.Py()
p4.P4.Z += src.Pz()
p4.P4.T += src.E()
sum.Set(p4)
return sum
}
Expand Down
5 changes: 5 additions & 0 deletions fmom/p4.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,8 @@ type P4 interface {
Set(p4 P4)
Clone() P4
}

// Vec4 holds the four components of a Lorentz vector.
type Vec4 struct {
X, Y, Z, T float64
}
52 changes: 16 additions & 36 deletions fmom/pxpypze.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,28 @@ import (
"math"
)

type PxPyPzE [4]float64
type PxPyPzE struct {
P4 Vec4
}

func NewPxPyPzE(px, py, pz, e float64) PxPyPzE {
return PxPyPzE([4]float64{px, py, pz, e})
return PxPyPzE{P4: Vec4{X: px, Y: py, Z: pz, T: e}}
}

func (p4 *PxPyPzE) Clone() P4 {
pp := *p4
return &pp
}

func (p4 *PxPyPzE) Px() float64 {
return p4[0]
}

func (p4 *PxPyPzE) Py() float64 {
return p4[1]
}

func (p4 *PxPyPzE) Pz() float64 {
return p4[2]
}

func (p4 *PxPyPzE) E() float64 {
return p4[3]
}
func (p4 *PxPyPzE) Px() float64 { return p4.P4.X }
func (p4 *PxPyPzE) Py() float64 { return p4.P4.Y }
func (p4 *PxPyPzE) Pz() float64 { return p4.P4.Z }
func (p4 *PxPyPzE) E() float64 { return p4.P4.T }

func (p4 *PxPyPzE) X() float64 {
return p4[0]
}

func (p4 *PxPyPzE) Y() float64 {
return p4[1]
}

func (p4 *PxPyPzE) Z() float64 {
return p4[2]
}

func (p4 *PxPyPzE) T() float64 {
return p4[3]
}
func (p4 *PxPyPzE) X() float64 { return p4.P4.X }
func (p4 *PxPyPzE) Y() float64 { return p4.P4.Y }
func (p4 *PxPyPzE) Z() float64 { return p4.P4.Z }
func (p4 *PxPyPzE) T() float64 { return p4.P4.T }

func (p4 *PxPyPzE) M2() float64 {
px := p4.Px()
Expand Down Expand Up @@ -206,8 +186,8 @@ func (p4 *PxPyPzE) Rapidity() float64 {
}

func (p4 *PxPyPzE) Set(p P4) {
p4[0] = p.Px()
p4[1] = p.Py()
p4[2] = p.Pz()
p4[3] = p.E()
p4.P4.X = p.Px()
p4.P4.Y = p.Py()
p4.P4.Z = p.Pz()
p4.P4.T = p.E()
}
16 changes: 8 additions & 8 deletions hepmc/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,22 +503,22 @@ func (dec *Decoder) decodeVertex(evt *Event, vtx *Vertex, pidxToEndVtx map[int]i
return err
}

vtx.Position[0], err = tokens.float64()
vtx.Position.P4.X, err = tokens.float64()
if err != nil {
return err
}

vtx.Position[1], err = tokens.float64()
vtx.Position.P4.Y, err = tokens.float64()
if err != nil {
return err
}

vtx.Position[2], err = tokens.float64()
vtx.Position.P4.Z, err = tokens.float64()
if err != nil {
return err
}

vtx.Position[3], err = tokens.float64()
vtx.Position.P4.T, err = tokens.float64()
if err != nil {
return err
}
Expand Down Expand Up @@ -602,22 +602,22 @@ func (dec *Decoder) decodeParticle(evt *Event, p *Particle, pidxToEndVtx map[int
return err
}

p.Momentum[0], err = tokens.float64()
p.Momentum.P4.X, err = tokens.float64()
if err != nil {
return err
}

p.Momentum[1], err = tokens.float64()
p.Momentum.P4.Y, err = tokens.float64()
if err != nil {
return err
}

p.Momentum[2], err = tokens.float64()
p.Momentum.P4.Z, err = tokens.float64()
if err != nil {
return err
}

p.Momentum[3], err = tokens.float64()
p.Momentum.P4.T, err = tokens.float64()
if err != nil {
return err
}
Expand Down
6 changes: 4 additions & 2 deletions hepmc/hepmc.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,8 +506,10 @@ func (vtx *Vertex) removeParticleOut(p *Particle) error {

// Print prints the vertex to w in a human-readable format
func (vtx *Vertex) Print(w io.Writer) error {
var err error
zero := fmom.PxPyPzE{0, 0, 0, 0}
var (
err error
zero fmom.PxPyPzE
)
if vtx.Barcode != 0 {
if vtx.Position != zero {
_, err = fmt.Fprintf(
Expand Down
18 changes: 9 additions & 9 deletions hepmc/hepmc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ func ExampleEvent_buildFromScratch() {
}

err = v1.AddParticleIn(&hepmc.Particle{
Momentum: fmom.PxPyPzE{0, 0, 7000, 7000},
Momentum: fmom.NewPxPyPzE(0, 0, 7000, 7000),
PdgID: 2212,
Status: 3,
})
Expand All @@ -297,7 +297,7 @@ func ExampleEvent_buildFromScratch() {
}

err = v2.AddParticleIn(&hepmc.Particle{
Momentum: fmom.PxPyPzE{0, 0, -7000, 7000},
Momentum: fmom.NewPxPyPzE(0, 0, -7000, 7000),
PdgID: 2212,
Status: 3,
//Barcode: 2,
Expand All @@ -308,7 +308,7 @@ func ExampleEvent_buildFromScratch() {

// create the outgoing particles of v1 and v2
p3 := &hepmc.Particle{
Momentum: fmom.PxPyPzE{.750, -1.569, 32.191, 32.238},
Momentum: fmom.NewPxPyPzE(.750, -1.569, 32.191, 32.238),
PdgID: 1,
Status: 3,
// Barcode: 3,
Expand All @@ -319,7 +319,7 @@ func ExampleEvent_buildFromScratch() {
}

p4 := &hepmc.Particle{
Momentum: fmom.PxPyPzE{-3.047, -19., -54.629, 57.920},
Momentum: fmom.NewPxPyPzE(-3.047, -19., -54.629, 57.920),
PdgID: -2,
Status: 3,
// Barcode: 4,
Expand Down Expand Up @@ -347,7 +347,7 @@ func ExampleEvent_buildFromScratch() {
}

err = v3.AddParticleOut(&hepmc.Particle{
Momentum: fmom.PxPyPzE{-3.813, 0.113, -1.833, 4.233},
Momentum: fmom.NewPxPyPzE(-3.813, 0.113, -1.833, 4.233),
PdgID: 22,
Status: 1,
})
Expand All @@ -356,7 +356,7 @@ func ExampleEvent_buildFromScratch() {
}

p5 := &hepmc.Particle{
Momentum: fmom.PxPyPzE{1.517, -20.68, -20.605, 85.925},
Momentum: fmom.NewPxPyPzE(1.517, -20.68, -20.605, 85.925),
PdgID: -24,
Status: 3,
}
Expand All @@ -367,7 +367,7 @@ func ExampleEvent_buildFromScratch() {

// create v4
v4 := &hepmc.Vertex{
Position: fmom.PxPyPzE{0.12, -0.3, 0.05, 0.004},
Position: fmom.NewPxPyPzE(0.12, -0.3, 0.05, 0.004),
}
err = evt.AddVertex(v4)
if err != nil {
Expand All @@ -380,7 +380,7 @@ func ExampleEvent_buildFromScratch() {
}

err = v4.AddParticleOut(&hepmc.Particle{
Momentum: fmom.PxPyPzE{-2.445, 28.816, 6.082, 29.552},
Momentum: fmom.NewPxPyPzE(-2.445, 28.816, 6.082, 29.552),
PdgID: 1,
Status: 1,
})
Expand All @@ -389,7 +389,7 @@ func ExampleEvent_buildFromScratch() {
}

err = v4.AddParticleOut(&hepmc.Particle{
Momentum: fmom.PxPyPzE{3.962, -49.498, -26.687, 56.373},
Momentum: fmom.NewPxPyPzE(3.962, -49.498, -26.687, 56.373),
PdgID: -2,
Status: 1,
})
Expand Down

0 comments on commit 1a8accc

Please sign in to comment.