go-hep.org/x/hep@v0.38.1/lcio/recparticle.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  // RecParticleContainer is a collection of RecParticles.
    15  type RecParticleContainer struct {
    16  	Flags  Flags
    17  	Params Params
    18  	Parts  []RecParticle
    19  }
    20  
    21  type RecParticle struct {
    22  	Type          int32
    23  	P             [3]float32  // momentum (Px,PyPz)
    24  	Energy        float32     // energy of particle
    25  	Cov           [10]float32 // covariance matrix for 4-vector (Px,Py,Pz,E)
    26  	Mass          float32     // mass of object used for 4-vector
    27  	Charge        float32     // charge of particle
    28  	Ref           [3]float32  // reference point of 4-vector
    29  	PIDs          []ParticleID
    30  	PIDUsed       *ParticleID
    31  	GoodnessOfPID float32 // overall quality of the particle identification
    32  	Recs          []*RecParticle
    33  	Tracks        []*Track
    34  	Clusters      []*Cluster
    35  	StartVtx      *Vertex // start vertex associated to the particle
    36  }
    37  
    38  type ParticleID struct {
    39  	Likelihood float32
    40  	Type       int32
    41  	PDG        int32
    42  	AlgType    int32
    43  	Params     []float32
    44  }
    45  
    46  func (recs *RecParticleContainer) String() string {
    47  	o := new(strings.Builder)
    48  	fmt.Fprintf(o, "%[1]s print out of ReconstructedParticle collection %[1]s\n\n", strings.Repeat("-", 15))
    49  	fmt.Fprintf(o, "  flag:  0x%x\n%v", recs.Flags, recs.Params)
    50  
    51  	fmt.Fprintf(o, "\n")
    52  
    53  	const (
    54  		head = " [   id   ] |com|type|     momentum( px,py,pz)       | energy | mass   | charge  |        position ( x,y,z)      | pidUsed |GoodnessOfPID|\n"
    55  		tail = "------------|---|----|-------------------------------|--------|--------|---------|-------------------------------|---------|-------------|\n"
    56  	)
    57  	o.WriteString(head)
    58  	o.WriteString(tail)
    59  	for i := range recs.Parts {
    60  		rec := &recs.Parts[i]
    61  		compound := 0
    62  		if len(rec.Recs) > 0 {
    63  			compound = 1
    64  		}
    65  		fmt.Fprintf(o,
    66  			"[%09d] |%3d|%4d|%+.2e, %+.2e, %+.2e|%.2e|%.2e|%+.2e|%+.2e, %+.2e, %+.2e|%09d|%+.2e| \n",
    67  			ID(rec),
    68  			compound, rec.Type,
    69  			rec.P[0], rec.P[1], rec.P[2], rec.Energy, rec.Mass, rec.Charge,
    70  			rec.Ref[0], rec.Ref[1], rec.Ref[2],
    71  			ID(rec.PIDUsed),
    72  			rec.GoodnessOfPID,
    73  		)
    74  	}
    75  	return o.String()
    76  }
    77  
    78  func (*RecParticleContainer) VersionSio() uint32 {
    79  	return Version
    80  }
    81  
    82  func (recs *RecParticleContainer) MarshalSio(w sio.Writer) error {
    83  	enc := sio.NewEncoder(w)
    84  	enc.Encode(&recs.Flags)
    85  	enc.Encode(&recs.Params)
    86  	enc.Encode(int32(len(recs.Parts)))
    87  	for i := range recs.Parts {
    88  		rec := &recs.Parts[i]
    89  		enc.Encode(&rec.Type)
    90  		enc.Encode(&rec.P)
    91  		enc.Encode(&rec.Energy)
    92  		enc.Encode(&rec.Cov)
    93  		enc.Encode(&rec.Mass)
    94  		enc.Encode(&rec.Charge)
    95  		enc.Encode(&rec.Ref)
    96  
    97  		enc.Encode(int32(len(rec.PIDs)))
    98  		for i := range rec.PIDs {
    99  			pid := &rec.PIDs[i]
   100  			enc.Encode(&pid.Likelihood)
   101  			enc.Encode(&pid.Type)
   102  			enc.Encode(&pid.PDG)
   103  			enc.Encode(&pid.AlgType)
   104  			enc.Encode(&pid.Params)
   105  			enc.Tag(pid)
   106  		}
   107  
   108  		enc.Pointer(&rec.PIDUsed)
   109  		enc.Encode(&rec.GoodnessOfPID)
   110  
   111  		enc.Encode(int32(len(rec.Recs)))
   112  		for i := range rec.Recs {
   113  			enc.Pointer(&rec.Recs[i])
   114  		}
   115  
   116  		enc.Encode(int32(len(rec.Tracks)))
   117  		for i := range rec.Tracks {
   118  			enc.Pointer(&rec.Tracks[i])
   119  		}
   120  
   121  		enc.Encode(int32(len(rec.Clusters)))
   122  		for i := range rec.Clusters {
   123  			enc.Pointer(&rec.Clusters[i])
   124  		}
   125  
   126  		enc.Pointer(&rec.StartVtx)
   127  		enc.Tag(rec)
   128  	}
   129  	return enc.Err()
   130  }
   131  
   132  func (recs *RecParticleContainer) UnmarshalSio(r sio.Reader) error {
   133  	dec := sio.NewDecoder(r)
   134  	dec.Decode(&recs.Flags)
   135  	dec.Decode(&recs.Params)
   136  	var n int32
   137  	dec.Decode(&n)
   138  	recs.Parts = make([]RecParticle, int(n))
   139  	if r.VersionSio() <= 1002 {
   140  		return fmt.Errorf("lcio: too old file (%d)", r.VersionSio())
   141  	}
   142  
   143  	for i := range recs.Parts {
   144  		rec := &recs.Parts[i]
   145  		dec.Decode(&rec.Type)
   146  		dec.Decode(&rec.P)
   147  		dec.Decode(&rec.Energy)
   148  		dec.Decode(&rec.Cov)
   149  		dec.Decode(&rec.Mass)
   150  		dec.Decode(&rec.Charge)
   151  		dec.Decode(&rec.Ref)
   152  
   153  		var n int32
   154  		dec.Decode(&n)
   155  		rec.PIDs = make([]ParticleID, int(n))
   156  		for i := range rec.PIDs {
   157  			pid := &rec.PIDs[i]
   158  			dec.Decode(&pid.Likelihood)
   159  			dec.Decode(&pid.Type)
   160  			dec.Decode(&pid.PDG)
   161  			dec.Decode(&pid.AlgType)
   162  			dec.Decode(&pid.Params)
   163  			dec.Tag(pid)
   164  		}
   165  		dec.Pointer(&rec.PIDUsed)
   166  		dec.Decode(&rec.GoodnessOfPID)
   167  
   168  		dec.Decode(&n)
   169  		rec.Recs = make([]*RecParticle, int(n))
   170  		for i := range rec.Recs {
   171  			dec.Pointer(&rec.Recs[i])
   172  		}
   173  
   174  		dec.Decode(&n)
   175  		rec.Tracks = make([]*Track, int(n))
   176  		for i := range rec.Tracks {
   177  			dec.Pointer(&rec.Tracks[i])
   178  		}
   179  
   180  		dec.Decode(&n)
   181  		rec.Clusters = make([]*Cluster, int(n))
   182  		for i := range rec.Clusters {
   183  			dec.Pointer(&rec.Clusters[i])
   184  		}
   185  
   186  		if r.VersionSio() > 1007 {
   187  			dec.Pointer(&rec.StartVtx)
   188  		}
   189  
   190  		dec.Tag(rec)
   191  	}
   192  
   193  	return dec.Err()
   194  }
   195  
   196  var (
   197  	_ sio.Versioner = (*RecParticleContainer)(nil)
   198  	_ sio.Codec     = (*RecParticleContainer)(nil)
   199  )