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