go-hep.org/x/hep@v0.38.1/lcio/trackerhit.go (about) 1 // Copyright ©2017 The go-hep Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package lcio 6 7 import ( 8 "fmt" 9 "strings" 10 11 "go-hep.org/x/hep/sio" 12 ) 13 14 // TrackerHitContainer is a collection of tracker hits. 15 type TrackerHitContainer struct { 16 Flags Flags 17 Params Params 18 Hits []TrackerHit 19 } 20 21 type TrackerHit struct { 22 CellID0 int32 23 CellID1 int32 24 Type int32 // type of Track; encoded in parameters TrackerHitTypeName+TrackerHit TypeValue 25 Pos [3]float64 26 Cov [6]float64 // covariance matrix of position (x,y,z) 27 EDep float32 // energy deposit on the hit 28 EDepErr float32 // error measured on EDep 29 Time float32 30 Quality int32 // quality flag word 31 RawHits []Hit 32 } 33 34 func (hit *TrackerHit) GetCellID0() int32 { return hit.CellID0 } 35 func (hit *TrackerHit) GetCellID1() int32 { return hit.CellID1 } 36 37 func (hits TrackerHitContainer) String() string { 38 o := new(strings.Builder) 39 fmt.Fprintf(o, "%[1]s print out of TrackerHit collection %[1]s\n\n", strings.Repeat("-", 15)) 40 fmt.Fprintf(o, " flag: 0x%x\n%v", hits.Flags, hits.Params) 41 fmt.Fprintf(o, " LCIO::THBIT_BARREL : %v\n", hits.Flags.Test(BitsThBarrel)) 42 43 // FIXME(sbinet): quality-bits 44 45 fmt.Fprintf(o, "\n") 46 47 dec := NewCellIDDecoderFrom(hits.Params) 48 const ( 49 head = " [ id ] |cellId0 |cellId1 | position (x,y,z) | time |[type]|[qual]| EDep |EDepError| cov(x,x), cov(y,x), cov(y,y), cov(z,x), cov(z,y), cov(z,z)\n" 50 tail = "------------|--------|--------|-----------------------------|---------|------|------|---------|---------|-----------------------------------------------------------------\n" 51 ) 52 o.WriteString(head) 53 o.WriteString(tail) 54 for i := range hits.Hits { 55 hit := &hits.Hits[i] 56 fmt.Fprintf(o, 57 "[%09d] |%08d|%08d|%+.2e,%+.2e,%+.2e|%+.2e|[%04d]|[%04d]|%+.2e|%+.2e|%+.2e, %+.2e, %+.2e, %+.2e, %+.2e, %+.2e\n", 58 ID(hit), 59 hit.CellID0, hit.CellID1, 60 hit.Pos[0], hit.Pos[1], hit.Pos[2], 61 hit.Time, hit.Type, hit.Quality, 62 hit.EDep, hit.EDepErr, 63 hit.Cov[0], hit.Cov[1], hit.Cov[2], hit.Cov[3], hit.Cov[4], hit.Cov[5], 64 ) 65 if dec != nil { 66 fmt.Fprintf(o, " id-fields: (%s)\n", dec.ValueString(hit)) 67 } 68 } 69 o.WriteString(tail) 70 return o.String() 71 } 72 73 func (*TrackerHitContainer) VersionSio() uint32 { 74 return Version 75 } 76 77 func (hits *TrackerHitContainer) MarshalSio(w sio.Writer) error { 78 enc := sio.NewEncoder(w) 79 enc.Encode(&hits.Flags) 80 enc.Encode(&hits.Params) 81 enc.Encode(int32(len(hits.Hits))) 82 for i := range hits.Hits { 83 hit := &hits.Hits[i] 84 enc.Encode(&hit.CellID0) 85 if hits.Flags.Test(BitsThID1) { 86 enc.Encode(&hit.CellID1) 87 } 88 enc.Encode(&hit.Type) 89 enc.Encode(&hit.Pos) 90 var cov [6]float32 91 cov[0] = float32(hit.Cov[0]) 92 cov[1] = float32(hit.Cov[1]) 93 cov[2] = float32(hit.Cov[2]) 94 cov[3] = float32(hit.Cov[3]) 95 cov[4] = float32(hit.Cov[4]) 96 cov[5] = float32(hit.Cov[5]) 97 enc.Encode(&cov) 98 enc.Encode(&hit.EDep) 99 enc.Encode(&hit.EDepErr) 100 enc.Encode(&hit.Time) 101 enc.Encode(&hit.Quality) 102 103 enc.Encode(int32(len(hit.RawHits))) 104 for ii := range hit.RawHits { 105 enc.Pointer(&hit.RawHits[ii]) 106 } 107 enc.Tag(hit) 108 } 109 return enc.Err() 110 } 111 112 func (hits *TrackerHitContainer) UnmarshalSio(r sio.Reader) error { 113 dec := sio.NewDecoder(r) 114 dec.Decode(&hits.Flags) 115 dec.Decode(&hits.Params) 116 var n int32 117 dec.Decode(&n) 118 hits.Hits = make([]TrackerHit, int(n)) 119 for i := range hits.Hits { 120 hit := &hits.Hits[i] 121 if r.VersionSio() > 1051 { 122 dec.Decode(&hit.CellID0) 123 if hits.Flags.Test(BitsThID1) { 124 dec.Decode(&hit.CellID1) 125 } 126 } 127 if r.VersionSio() > 1002 { 128 dec.Decode(&hit.Type) 129 } 130 dec.Decode(&hit.Pos) 131 var cov [6]float32 132 dec.Decode(&cov) 133 hit.Cov[0] = float64(cov[0]) 134 hit.Cov[1] = float64(cov[1]) 135 hit.Cov[2] = float64(cov[2]) 136 hit.Cov[3] = float64(cov[3]) 137 hit.Cov[4] = float64(cov[4]) 138 hit.Cov[5] = float64(cov[5]) 139 140 dec.Decode(&hit.EDep) 141 if r.VersionSio() > 1012 { 142 dec.Decode(&hit.EDepErr) 143 } 144 dec.Decode(&hit.Time) 145 if r.VersionSio() > 1011 { 146 dec.Decode(&hit.Quality) 147 } 148 149 var n int32 = 1 150 if r.VersionSio() > 1002 { 151 dec.Decode(&n) 152 } 153 hit.RawHits = make([]Hit, int(n)) 154 for ii := range hit.RawHits { 155 dec.Pointer(&hit.RawHits[ii]) 156 } 157 dec.Tag(hit) 158 } 159 160 return dec.Err() 161 } 162 163 var ( 164 _ sio.Versioner = (*TrackerHitContainer)(nil) 165 _ sio.Codec = (*TrackerHitContainer)(nil) 166 _ Hit = (*TrackerHit)(nil) 167 )