go-hep.org/x/hep@v0.38.1/lcio/vertex.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  // VertexContainer is a collection of vertices
    15  type VertexContainer struct {
    16  	Flags  Flags
    17  	Params Params
    18  	Vtxs   []Vertex
    19  }
    20  
    21  type Vertex struct {
    22  	Primary int32      // primary vertex of the event
    23  	AlgType int32      // algorithm type
    24  	Chi2    float32    // Chi^2 of vertex
    25  	Prob    float32    // probability of the fit
    26  	Pos     [3]float32 // position of the vertex (Px,Py,Pz)
    27  	Cov     [6]float32 // covariance matrix
    28  	Params  []float32
    29  	RecPart *RecParticle // reconstructed particle associated to the vertex
    30  }
    31  
    32  func (vtx *Vertex) AlgName() string {
    33  	// FIXME(sbinet)
    34  	return fmt.Sprintf("Unknown (id=%d)", vtx.AlgType)
    35  }
    36  
    37  func (vtxs VertexContainer) String() string {
    38  	o := new(strings.Builder)
    39  	fmt.Fprintf(o, "%[1]s print out of Vertex collection %[1]s\n\n", strings.Repeat("-", 15))
    40  	fmt.Fprintf(o, "%v", vtxs.Params)
    41  
    42  	fmt.Fprintf(o, "\n")
    43  
    44  	const (
    45  		head = " [   id   ] |pri|     alg. type     |    chi2   |    prob.  |       position ( x, y, z)       | [par] |  [idRecP]  \n"
    46  		tail = "------------|---|-------------------|-----------|-----------|---------------------------------|-------|------------\n"
    47  	)
    48  	o.WriteString(head)
    49  	o.WriteString(tail)
    50  
    51  	for _, vtx := range vtxs.Vtxs {
    52  		fmt.Fprintf(o, " [%08d] | %d | %-17s | %+.2e | %+.2e | %+.2e, %+.2e, %+.2e | [%03d] | [%06d]\n",
    53  			0, // id
    54  			vtx.Primary, vtx.AlgName(), vtx.Chi2, vtx.Prob,
    55  			vtx.Pos[0], vtx.Pos[1], vtx.Pos[2],
    56  			len(vtx.Params),
    57  			0, // vtx.RecPart
    58  		)
    59  	}
    60  
    61  	return o.String()
    62  }
    63  
    64  func (*VertexContainer) VersionSio() uint32 {
    65  	return Version
    66  }
    67  
    68  func (vtxs *VertexContainer) MarshalSio(w sio.Writer) error {
    69  	enc := sio.NewEncoder(w)
    70  	enc.Encode(&vtxs.Flags)
    71  	enc.Encode(&vtxs.Params)
    72  	enc.Encode(int32(len(vtxs.Vtxs)))
    73  	for i := range vtxs.Vtxs {
    74  		vtx := &vtxs.Vtxs[i]
    75  		enc.Encode(&vtx.Primary)
    76  		enc.Encode(&vtx.AlgType)
    77  		enc.Encode(&vtx.Chi2)
    78  		enc.Encode(&vtx.Prob)
    79  		enc.Encode(&vtx.Pos)
    80  		enc.Encode(&vtx.Cov)
    81  		enc.Encode(&vtx.Params)
    82  		enc.Pointer(&vtx.RecPart)
    83  		enc.Tag(vtx)
    84  	}
    85  	return enc.Err()
    86  }
    87  
    88  func (vtxs *VertexContainer) UnmarshalSio(r sio.Reader) error {
    89  	dec := sio.NewDecoder(r)
    90  	dec.Decode(&vtxs.Flags)
    91  	dec.Decode(&vtxs.Params)
    92  	var n int32
    93  	dec.Decode(&n)
    94  	vtxs.Vtxs = make([]Vertex, int(n))
    95  	for i := range vtxs.Vtxs {
    96  		vtx := &vtxs.Vtxs[i]
    97  		dec.Decode(&vtx.Primary)
    98  		dec.Decode(&vtx.AlgType)
    99  		dec.Decode(&vtx.Chi2)
   100  		dec.Decode(&vtx.Prob)
   101  		dec.Decode(&vtx.Pos)
   102  		dec.Decode(&vtx.Cov)
   103  		dec.Decode(&vtx.Params)
   104  		dec.Pointer(&vtx.RecPart)
   105  		dec.Tag(vtx)
   106  	}
   107  	return dec.Err()
   108  }
   109  
   110  var (
   111  	_ sio.Versioner = (*VertexContainer)(nil)
   112  	_ sio.Codec     = (*VertexContainer)(nil)
   113  )