go-hep.org/x/hep@v0.38.1/lcio/simtrackerhit.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  // SimTrackerHitContainer is a collection of simulated tracker hits.
    15  type SimTrackerHitContainer struct {
    16  	Flags  Flags
    17  	Params Params
    18  	Hits   []SimTrackerHit
    19  }
    20  
    21  type SimTrackerHit struct {
    22  	CellID0    int32
    23  	CellID1    int32 // second word for cell ID
    24  	Pos        [3]float64
    25  	EDep       float32 // energy deposited on the hit
    26  	Time       float32
    27  	Mc         *McParticle
    28  	Momentum   [3]float32
    29  	PathLength float32
    30  	Quality    int32
    31  }
    32  
    33  func (hit *SimTrackerHit) GetCellID0() int32 { return hit.CellID0 }
    34  func (hit *SimTrackerHit) GetCellID1() int32 { return hit.CellID1 }
    35  
    36  func (hits SimTrackerHitContainer) String() string {
    37  	o := new(strings.Builder)
    38  	fmt.Fprintf(o, "%[1]s print out of SimTrackerHit collection %[1]s\n\n", strings.Repeat("-", 15))
    39  	fmt.Fprintf(o, "  flag:  0x%x\n%v", hits.Flags, hits.Params)
    40  	fmt.Fprintf(o, "     LCIO::THBIT_BARREL   : %v\n", hits.Flags.Test(BitsThBarrel))
    41  	fmt.Fprintf(o, "     LCIO::THBIT_MOMENTUM : %v\n", hits.Flags.Test(BitsThMomentum))
    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)               |   EDep   |   time   |  PDG  |        (px,  py, pz)          | path-len | Quality \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  		var pdg int32 = 0
    57  		if hit.Mc != nil {
    58  			pdg = hit.Mc.PDG
    59  		}
    60  		fmt.Fprintf(o,
    61  			"[%09d] |%08d|%08d|(%+.2e, %+.2e, %+.2e)| %.2e | %.2e | %05d |(%+.2e,%+.2e,%+.2e)|%+.3e|%5d\n",
    62  			ID(hit),
    63  			hit.CellID0, hit.CellID1,
    64  			hit.Pos[0], hit.Pos[1], hit.Pos[2],
    65  			hit.EDep, hit.Time,
    66  			pdg,
    67  			hit.Momentum[0], hit.Momentum[1], hit.Momentum[2],
    68  			hit.PathLength,
    69  			hit.Quality,
    70  		)
    71  		if dec != nil {
    72  			fmt.Fprintf(o, "        id-fields: (%s)\n", dec.ValueString(hit))
    73  		}
    74  	}
    75  	o.WriteString(tail)
    76  	return o.String()
    77  }
    78  
    79  func (*SimTrackerHitContainer) VersionSio() uint32 {
    80  	return Version
    81  }
    82  
    83  func (hits *SimTrackerHitContainer) MarshalSio(w sio.Writer) error {
    84  	enc := sio.NewEncoder(w)
    85  	enc.Encode(&hits.Flags)
    86  	enc.Encode(&hits.Params)
    87  	enc.Encode(int32(len(hits.Hits)))
    88  	for i := range hits.Hits {
    89  		hit := &hits.Hits[i]
    90  		enc.Encode(&hit.CellID0)
    91  		if hits.Flags.Test(BitsThID1) {
    92  			enc.Encode(&hit.CellID1)
    93  		}
    94  		enc.Encode(&hit.Pos)
    95  		enc.Encode(&hit.EDep)
    96  		enc.Encode(&hit.Time)
    97  		enc.Pointer(&hit.Mc)
    98  		if hits.Flags.Test(BitsThMomentum) {
    99  			enc.Encode(&hit.Momentum)
   100  			enc.Encode(&hit.PathLength)
   101  		}
   102  		enc.Encode(&hit.Quality)
   103  		enc.Tag(hit)
   104  	}
   105  	return enc.Err()
   106  }
   107  
   108  func (hits *SimTrackerHitContainer) UnmarshalSio(r sio.Reader) error {
   109  	dec := sio.NewDecoder(r)
   110  	dec.Decode(&hits.Flags)
   111  	dec.Decode(&hits.Params)
   112  	var n int32
   113  	dec.Decode(&n)
   114  	hits.Hits = make([]SimTrackerHit, int(n))
   115  	for i := range hits.Hits {
   116  		hit := &hits.Hits[i]
   117  		dec.Decode(&hit.CellID0)
   118  		if r.VersionSio() > 1051 && hits.Flags.Test(BitsThID1) {
   119  			dec.Decode(&hit.CellID1)
   120  		}
   121  		dec.Decode(&hit.Pos)
   122  		dec.Decode(&hit.EDep)
   123  		dec.Decode(&hit.Time)
   124  		dec.Pointer(&hit.Mc)
   125  		if hits.Flags.Test(BitsThMomentum) {
   126  			dec.Decode(&hit.Momentum)
   127  			if r.VersionSio() > 1006 {
   128  				dec.Decode(&hit.PathLength)
   129  			}
   130  		}
   131  		if r.VersionSio() > 2007 {
   132  			dec.Decode(&hit.Quality)
   133  		}
   134  		if r.VersionSio() > 1000 {
   135  			dec.Tag(hit)
   136  		}
   137  	}
   138  	return dec.Err()
   139  }
   140  
   141  var (
   142  	_ sio.Versioner = (*SimTrackerHitContainer)(nil)
   143  	_ sio.Codec     = (*SimTrackerHitContainer)(nil)
   144  	_ Hit           = (*SimTrackerHit)(nil)
   145  )