go-hep.org/x/hep@v0.38.1/lcio/trackerpulse.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  type TrackerPulseContainer struct {
    15  	Flags  Flags
    16  	Params Params
    17  	Pulses []TrackerPulse
    18  }
    19  
    20  type TrackerPulse struct {
    21  	CellID0 int32
    22  	CellID1 int32
    23  	Time    float32      // time of pulse
    24  	Charge  float32      // charge of pulse
    25  	Cov     [3]float32   // covariance matrix of charge (c) and time (t) measurements
    26  	Quality int32        // quality flag word
    27  	TPC     *TrackerData // TPC corrected data: spectrum used to create this pulse
    28  }
    29  
    30  func (hit *TrackerPulse) GetCellID0() int32 { return hit.CellID0 }
    31  func (hit *TrackerPulse) GetCellID1() int32 { return hit.CellID1 }
    32  
    33  func (ps *TrackerPulseContainer) String() string {
    34  	o := new(strings.Builder)
    35  	fmt.Fprintf(o, "%[1]s print out of TrackerPulse collection %[1]s\n\n", strings.Repeat("-", 15))
    36  	fmt.Fprintf(o, "  flag:  0x%x\n", ps.Flags)
    37  	fmt.Fprintf(o, "     LCIO::TRAWBIT_ID1 : %v\n", ps.Flags.Test(BitsTRawID1))
    38  	fmt.Fprintf(o, "     LCIO::TRAWBIT_CM  : %v\n", ps.Flags.Test(BitsTRawCM))
    39  
    40  	fmt.Fprintf(o, "%v\n", ps.Params)
    41  
    42  	const (
    43  		head = " [   id   ] | cellid0  | cellid1  |  time | charge | quality  | [corr.Data] |  cellid-fields: | cov(c,c), cov(t,c), cov(t,t) \n"
    44  		tail = "------------|----------|----------|-------|--------|----------|-------------|-----------------|------------------------------\n"
    45  	)
    46  	o.WriteString(head)
    47  	o.WriteString(tail)
    48  	for i := range ps.Pulses {
    49  		p := &ps.Pulses[i]
    50  		fmt.Fprintf(o,
    51  			"[%09d] | %08d | %08d |%+.4f| %+.4f| %8d | [%09d] | unknown/default | %+.2e, %+.2e, %.2e|\n",
    52  			ID(p),
    53  			p.CellID0, p.CellID1,
    54  			p.Time, p.Charge,
    55  			p.Quality,
    56  			ID(p.TPC),
    57  			p.Cov[0], p.Cov[1], p.Cov[2],
    58  		)
    59  	}
    60  	return o.String()
    61  }
    62  
    63  func (*TrackerPulseContainer) VersionSio() uint32 {
    64  	return Version
    65  }
    66  
    67  func (ps *TrackerPulseContainer) MarshalSio(w sio.Writer) error {
    68  	enc := sio.NewEncoder(w)
    69  	enc.Encode(&ps.Flags)
    70  	enc.Encode(&ps.Params)
    71  	enc.Encode(int32(len(ps.Pulses)))
    72  	for i := range ps.Pulses {
    73  		p := &ps.Pulses[i]
    74  		enc.Encode(&p.CellID0)
    75  		if ps.Flags.Test(BitsTRawID1) {
    76  			enc.Encode(&p.CellID1)
    77  		}
    78  		enc.Encode(&p.Time)
    79  		enc.Encode(&p.Charge)
    80  		if ps.Flags.Test(BitsTRawCM) {
    81  			enc.Encode(&p.Cov)
    82  		}
    83  		enc.Encode(&p.Quality)
    84  		enc.Pointer(&p.TPC)
    85  		enc.Tag(p)
    86  	}
    87  	return enc.Err()
    88  }
    89  
    90  func (ps *TrackerPulseContainer) UnmarshalSio(r sio.Reader) error {
    91  	dec := sio.NewDecoder(r)
    92  	dec.Decode(&ps.Flags)
    93  	dec.Decode(&ps.Params)
    94  	var n int32
    95  	dec.Decode(&n)
    96  	ps.Pulses = make([]TrackerPulse, int(n))
    97  	for i := range ps.Pulses {
    98  		p := &ps.Pulses[i]
    99  		dec.Decode(&p.CellID0)
   100  		if ps.Flags.Test(BitsTRawID1) {
   101  			dec.Decode(&p.CellID1)
   102  		}
   103  		dec.Decode(&p.Time)
   104  		dec.Decode(&p.Charge)
   105  		if r.VersionSio() > 1012 && ps.Flags.Test(BitsTRawCM) {
   106  			dec.Decode(&p.Cov)
   107  		}
   108  		dec.Decode(&p.Quality)
   109  		dec.Pointer(&p.TPC)
   110  		dec.Tag(p)
   111  	}
   112  	return dec.Err()
   113  }
   114  
   115  var (
   116  	_ sio.Versioner = (*TrackerPulseContainer)(nil)
   117  	_ sio.Codec     = (*TrackerPulseContainer)(nil)
   118  	_ Hit           = (*TrackerPulse)(nil)
   119  )