go-hep.org/x/hep@v0.38.1/lcio/calohit.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  // CalorimeterHitContainer is a collection of calorimeter hits.
    15  type CalorimeterHitContainer struct {
    16  	Flags  Flags
    17  	Params Params
    18  	Hits   []CalorimeterHit
    19  }
    20  
    21  func (hits CalorimeterHitContainer) String() string {
    22  	o := new(strings.Builder)
    23  	fmt.Fprintf(o, "%[1]s print out of CalorimeterHit 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_LONG   : %v\n", hits.Flags.Test(BitsRChLong))
    26  	fmt.Fprintf(o, "     LCIO::RCHBIT_BARREL : %v\n", hits.Flags.Test(BitsRChBarrel))
    27  	fmt.Fprintf(o, "     LCIO::RCHBIT_ID1    : %v\n", hits.Flags.Test(BitsRChID1))
    28  	fmt.Fprintf(o, "     LCIO::RCHBIT_TIME   : %v\n", hits.Flags.Test(BitsRChTime))
    29  	fmt.Fprintf(o, "     LCIO::RCHBIT_NO_PTR : %v\n", hits.Flags.Test(BitsRChNoPtr))
    30  	fmt.Fprintf(o, "     LCIO::RCHBIT_ENERGY_ERROR  : %v\n", hits.Flags.Test(BitsRChEnergyError))
    31  
    32  	dec := NewCellIDDecoderFrom(hits.Params)
    33  
    34  	o.WriteString("\n")
    35  
    36  	const (
    37  		head = " [   id   ] |cellId0 |cellId1 |  energy  |energyerr |        position (x,y,z)           \n"
    38  		tail = "------------|--------|--------|----------|----------|-----------------------------------\n"
    39  	)
    40  	o.WriteString(head)
    41  	o.WriteString(tail)
    42  	for i := range hits.Hits {
    43  		hit := &hits.Hits[i]
    44  		fmt.Fprintf(o, "[%09d] |%08d|%08d|%+.3e|%+.3e|", ID(hit), hit.CellID0, hit.CellID1, hit.Energy, hit.EnergyErr)
    45  		if hits.Flags.Test(BitsChLong) {
    46  			fmt.Fprintf(o, "+%.3e, %+.3e, %+.3e", hit.Pos[0], hit.Pos[1], hit.Pos[2])
    47  		} else {
    48  			o.WriteString("    no position available         ")
    49  		}
    50  		if dec != nil {
    51  			fmt.Fprintf(o, "        id-fields: (%s)", dec.ValueString(hit))
    52  		} else {
    53  			o.WriteString("        id-fields: --- unknown/default ----   ")
    54  		}
    55  		o.WriteString("\n")
    56  	}
    57  	o.WriteString(tail)
    58  	return o.String()
    59  }
    60  
    61  func (*CalorimeterHitContainer) VersionSio() uint32 {
    62  	return Version
    63  }
    64  
    65  func (hits *CalorimeterHitContainer) MarshalSio(w sio.Writer) error {
    66  	enc := sio.NewEncoder(w)
    67  	enc.Encode(&hits.Flags)
    68  	enc.Encode(&hits.Params)
    69  	enc.Encode(int32(len(hits.Hits)))
    70  	for i := range hits.Hits {
    71  		hit := &hits.Hits[i]
    72  		enc.Encode(&hit.CellID0)
    73  		if hits.Flags.Test(BitsRChID1) {
    74  			enc.Encode(&hit.CellID1)
    75  		}
    76  		enc.Encode(&hit.Energy)
    77  		if hits.Flags.Test(BitsRChEnergyError) {
    78  			enc.Encode(&hit.EnergyErr)
    79  		}
    80  		if hits.Flags.Test(BitsRChTime) {
    81  			enc.Encode(&hit.Time)
    82  		}
    83  		if hits.Flags.Test(BitsRChLong) {
    84  			enc.Encode(&hit.Pos)
    85  		}
    86  		enc.Encode(&hit.Type)
    87  		enc.Pointer(&hit.Raw)
    88  		if !hits.Flags.Test(BitsRChNoPtr) {
    89  			enc.Tag(hit)
    90  		}
    91  	}
    92  	return enc.Err()
    93  }
    94  
    95  func (hits *CalorimeterHitContainer) UnmarshalSio(r sio.Reader) error {
    96  	dec := sio.NewDecoder(r)
    97  	dec.Decode(&hits.Flags)
    98  	dec.Decode(&hits.Params)
    99  	var n int32
   100  	dec.Decode(&n)
   101  	hits.Hits = make([]CalorimeterHit, int(n))
   102  	for i := range hits.Hits {
   103  		hit := &hits.Hits[i]
   104  		dec.Decode(&hit.CellID0)
   105  		if r.VersionSio() == 8 || hits.Flags.Test(BitsRChID1) {
   106  			dec.Decode(&hit.CellID1)
   107  		}
   108  		dec.Decode(&hit.Energy)
   109  		if r.VersionSio() > 1009 && hits.Flags.Test(BitsRChEnergyError) {
   110  			dec.Decode(&hit.EnergyErr)
   111  		}
   112  		if r.VersionSio() > 1002 && hits.Flags.Test(BitsRChTime) {
   113  			dec.Decode(&hit.Time)
   114  		}
   115  		if hits.Flags.Test(BitsRChLong) {
   116  			dec.Decode(&hit.Pos)
   117  		}
   118  		if r.VersionSio() > 1002 {
   119  			dec.Decode(&hit.Type)
   120  			dec.Pointer(&hit.Raw)
   121  		}
   122  		if r.VersionSio() > 1002 {
   123  			// the logic of the pointer bit has been inverted in v1.3
   124  			if !hits.Flags.Test(BitsRChNoPtr) {
   125  				dec.Tag(hit)
   126  			}
   127  		} else {
   128  			if hits.Flags.Test(BitsRChNoPtr) {
   129  				dec.Tag(hit)
   130  			}
   131  		}
   132  	}
   133  	return dec.Err()
   134  }
   135  
   136  type CalorimeterHit struct {
   137  	CellID0   int32
   138  	CellID1   int32
   139  	Energy    float32
   140  	EnergyErr float32
   141  	Time      float32
   142  	Pos       [3]float32
   143  	Type      int32
   144  	Raw       Hit
   145  }
   146  
   147  func (hit *CalorimeterHit) GetCellID0() int32 { return hit.CellID0 }
   148  func (hit *CalorimeterHit) GetCellID1() int32 { return hit.CellID1 }
   149  
   150  var (
   151  	_ sio.Versioner = (*CalorimeterHitContainer)(nil)
   152  	_ sio.Codec     = (*CalorimeterHitContainer)(nil)
   153  	_ Hit           = (*CalorimeterHit)(nil)
   154  )