go-hep.org/x/hep@v0.38.1/lcio/rawcalohit.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  // RawCalorimeterHitContainer is a collection of raw calorimeter hits.
    15  type RawCalorimeterHitContainer struct {
    16  	Flags  Flags
    17  	Params Params
    18  	Hits   []RawCalorimeterHit
    19  }
    20  
    21  func (hits RawCalorimeterHitContainer) String() string {
    22  	o := new(strings.Builder)
    23  	fmt.Fprintf(o, "%[1]s print out of RawCalorimeterHit collection %[1]s\n\n", strings.Repeat("-", 15))
    24  	fmt.Fprintf(o, "  flag:  0x%x\n%v", hits.Flags, hits.Params)
    25  	fmt.Fprintf(o, "     LCIO::RCHBIT_ID1    : %v\n", hits.Flags.Test(BitsRChID1))
    26  	fmt.Fprintf(o, "     LCIO::RCHBIT_TIME   : %v\n", hits.Flags.Test(BitsRChTime))
    27  	fmt.Fprintf(o, "     LCIO::RCHBIT_NO_PTR : %v\n", hits.Flags.Test(BitsRChNoPtr))
    28  	fmt.Fprintf(o, "\n")
    29  
    30  	dec := NewCellIDDecoderFrom(hits.Params)
    31  	const (
    32  		head = " [   id   ] |  cellId0 ( M, S, I, J, K) |cellId1 | amplitude |  time  \n"
    33  		tail = "------------|---------------------------|--------|-----------|---------\n"
    34  	)
    35  	o.WriteString(head)
    36  	o.WriteString(tail)
    37  	for i := range hits.Hits {
    38  		hit := &hits.Hits[i]
    39  		fmt.Fprintf(o, "[%09d] |%08d%19s|%08d|%10d |%8d\n", ID(hit), hit.CellID0, "", hit.CellID1, hit.Amplitude, hit.TimeStamp)
    40  		if dec != nil {
    41  			fmt.Fprintf(o, "        id-fields: (%s)", dec.ValueString(hit))
    42  		} else {
    43  			fmt.Fprintf(o, "        id-fields: --- unknown/default ----   ")
    44  		}
    45  		fmt.Fprintf(o, "\n")
    46  	}
    47  	o.WriteString(tail)
    48  	return o.String()
    49  }
    50  
    51  func (*RawCalorimeterHitContainer) VersionSio() uint32 {
    52  	return Version
    53  }
    54  
    55  func (hits *RawCalorimeterHitContainer) MarshalSio(w sio.Writer) error {
    56  	enc := sio.NewEncoder(w)
    57  	enc.Encode(&hits.Flags)
    58  	enc.Encode(&hits.Params)
    59  	enc.Encode(int32(len(hits.Hits)))
    60  	for i := range hits.Hits {
    61  		hit := &hits.Hits[i]
    62  		enc.Encode(&hit.CellID0)
    63  		if hits.Flags.Test(BitsRChID1) {
    64  			enc.Encode(&hit.CellID1)
    65  		}
    66  		enc.Encode(&hit.Amplitude)
    67  		if hits.Flags.Test(BitsRChTime) {
    68  			enc.Encode(&hit.TimeStamp)
    69  		}
    70  		if !hits.Flags.Test(BitsRChNoPtr) {
    71  			enc.Tag(hit)
    72  		}
    73  	}
    74  	return enc.Err()
    75  }
    76  
    77  func (hits *RawCalorimeterHitContainer) UnmarshalSio(r sio.Reader) error {
    78  	dec := sio.NewDecoder(r)
    79  	dec.Decode(&hits.Flags)
    80  	dec.Decode(&hits.Params)
    81  	var n int32
    82  	dec.Decode(&n)
    83  	hits.Hits = make([]RawCalorimeterHit, int(n))
    84  	for i := range hits.Hits {
    85  		hit := &hits.Hits[i]
    86  		dec.Decode(&hit.CellID0)
    87  		if r.VersionSio() == 8 || hits.Flags.Test(BitsRChID1) {
    88  			dec.Decode(&hit.CellID1)
    89  		}
    90  		dec.Decode(&hit.Amplitude)
    91  		if hits.Flags.Test(BitsRChTime) {
    92  			dec.Decode(&hit.TimeStamp)
    93  		}
    94  		if !hits.Flags.Test(BitsRChNoPtr) {
    95  			dec.Tag(hit)
    96  		}
    97  	}
    98  	return dec.Err()
    99  }
   100  
   101  type RawCalorimeterHit struct {
   102  	CellID0   int32
   103  	CellID1   int32
   104  	Amplitude int32
   105  	TimeStamp int32
   106  }
   107  
   108  func (hit *RawCalorimeterHit) GetCellID0() int32 { return hit.CellID0 }
   109  func (hit *RawCalorimeterHit) GetCellID1() int32 { return hit.CellID1 }
   110  
   111  var (
   112  	_ sio.Versioner = (*RawCalorimeterHitContainer)(nil)
   113  	_ sio.Codec     = (*RawCalorimeterHitContainer)(nil)
   114  	_ Hit           = (*RawCalorimeterHit)(nil)
   115  )