go-hep.org/x/hep@v0.38.1/lcio/simcalohit.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  // SimCalorimeterHitContainer is a collection of simulation calorimter hits.
    15  type SimCalorimeterHitContainer struct {
    16  	Flags  Flags
    17  	Params Params
    18  	Hits   []SimCalorimeterHit
    19  }
    20  
    21  func (hits SimCalorimeterHitContainer) String() string {
    22  	o := new(strings.Builder)
    23  	fmt.Fprintf(o, "%[1]s print out of SimCalorimeterHit 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::CHBIT_LONG   : %v\n", hits.Flags.Test(BitsChLong))
    26  	fmt.Fprintf(o, "     LCIO::CHBIT_BARREL : %v\n", hits.Flags.Test(BitsChBarrel))
    27  	fmt.Fprintf(o, "     LCIO::CHBIT_ID1    : %v\n", hits.Flags.Test(BitsChID1))
    28  	fmt.Fprintf(o, "     LCIO::CHBIT_STEP   : %v\n", hits.Flags.Test(BitsChStep))
    29  	fmt.Fprintf(o, "\n")
    30  
    31  	dec := NewCellIDDecoderFrom(hits.Params)
    32  	const (
    33  		head = " [   id   ] |cellId0 |cellId1 |  energy  |        position (x,y,z)          | nMCParticles \n" +
    34  			"           -> MC contribution: prim. PDG |  energy  |   time   | sec. PDG | stepPosition (x,y,z) \n"
    35  		tail = "------------|--------|--------|----------|----------------------------------|--------------\n"
    36  	)
    37  	o.WriteString(head)
    38  	o.WriteString(tail)
    39  	for i := range hits.Hits {
    40  		hit := &hits.Hits[i]
    41  		fmt.Fprintf(o, "[%09d] |%08d|%08d|%+.3e|", ID(hit), hit.CellID0, hit.CellID1, hit.Energy)
    42  		if hits.Flags.Test(BitsChLong) {
    43  			fmt.Fprintf(o, "+%.3e, %+.3e, %+.3e", hit.Pos[0], hit.Pos[1], hit.Pos[2])
    44  		} else {
    45  			fmt.Fprintf(o, "    no position available         ")
    46  		}
    47  		fmt.Fprintf(o, "|%+12d\n", len(hit.Contributions))
    48  		if dec != nil {
    49  			fmt.Fprintf(o, "        id-fields: (%s)", dec.ValueString(hit))
    50  		} else {
    51  			fmt.Fprintf(o, "        id-fields: --- unknown/default ----   ")
    52  		}
    53  		for _, c := range hit.Contributions {
    54  			var pdg int32
    55  			if c.Mc != nil {
    56  				pdg = c.Mc.PDG
    57  			}
    58  			fmt.Fprintf(o, "\n           ->                  %+10d|%+1.3e|%+1.3e|", pdg, c.Energy, c.Time)
    59  			if hits.Flags.Test(BitsChStep) {
    60  				fmt.Fprintf(o, "%+d| (%+1.3e, %+1.3e, %+1.3e)", c.PDG, c.StepPos[0], c.StepPos[1], c.StepPos[2])
    61  			} else {
    62  				fmt.Fprintf(o, " no PDG")
    63  			}
    64  		}
    65  		fmt.Fprintf(o, "\n")
    66  	}
    67  	o.WriteString(tail)
    68  	return o.String()
    69  }
    70  
    71  func (*SimCalorimeterHitContainer) VersionSio() uint32 {
    72  	return Version
    73  }
    74  
    75  func (hits *SimCalorimeterHitContainer) MarshalSio(w sio.Writer) error {
    76  	enc := sio.NewEncoder(w)
    77  	enc.Encode(&hits.Flags)
    78  	enc.Encode(&hits.Params)
    79  	enc.Encode(int32(len(hits.Hits)))
    80  	for i := range hits.Hits {
    81  		hit := &hits.Hits[i]
    82  		enc.Encode(&hit.CellID0)
    83  		if hits.Flags.Test(BitsChID1) {
    84  			enc.Encode(&hit.CellID1)
    85  		}
    86  		enc.Encode(&hit.Energy)
    87  		if hits.Flags.Test(BitsChLong) {
    88  			enc.Encode(&hit.Pos)
    89  		}
    90  		enc.Encode(int32(len(hit.Contributions)))
    91  		for i := range hit.Contributions {
    92  			c := &hit.Contributions[i]
    93  			enc.Pointer(&c.Mc)
    94  			enc.Encode(&c.Energy)
    95  			enc.Encode(&c.Time)
    96  			if hits.Flags.Test(BitsChStep) {
    97  				enc.Encode(&c.PDG)
    98  				enc.Encode(&c.StepPos)
    99  			}
   100  		}
   101  		enc.Tag(hit)
   102  	}
   103  	return enc.Err()
   104  }
   105  
   106  func (hits *SimCalorimeterHitContainer) UnmarshalSio(r sio.Reader) error {
   107  	dec := sio.NewDecoder(r)
   108  	dec.Decode(&hits.Flags)
   109  	dec.Decode(&hits.Params)
   110  	var n int32
   111  	dec.Decode(&n)
   112  	hits.Hits = make([]SimCalorimeterHit, int(n))
   113  	for i := range hits.Hits {
   114  		hit := &hits.Hits[i]
   115  		dec.Decode(&hit.CellID0)
   116  		if r.VersionSio() < 9 || hits.Flags.Test(BitsChID1) {
   117  			dec.Decode(&hit.CellID1)
   118  		}
   119  		dec.Decode(&hit.Energy)
   120  		if hits.Flags.Test(BitsChLong) {
   121  			dec.Decode(&hit.Pos)
   122  		}
   123  		var n int32
   124  		dec.Decode(&n)
   125  		hit.Contributions = make([]Contrib, int(n))
   126  		for i := range hit.Contributions {
   127  			c := &hit.Contributions[i]
   128  			dec.Pointer(&c.Mc)
   129  			dec.Decode(&c.Energy)
   130  			dec.Decode(&c.Time)
   131  			if hits.Flags.Test(BitsChStep) {
   132  				dec.Decode(&c.PDG)
   133  				if r.VersionSio() > 1051 {
   134  					dec.Decode(&c.StepPos)
   135  				}
   136  			}
   137  		}
   138  		if r.VersionSio() > 1000 {
   139  			dec.Tag(hit)
   140  		}
   141  	}
   142  	return dec.Err()
   143  }
   144  
   145  type SimCalorimeterHit struct {
   146  	Params        Params
   147  	CellID0       int32
   148  	CellID1       int32
   149  	Energy        float32
   150  	Pos           [3]float32
   151  	Contributions []Contrib
   152  }
   153  
   154  func (hit *SimCalorimeterHit) GetCellID0() int32 { return hit.CellID0 }
   155  func (hit *SimCalorimeterHit) GetCellID1() int32 { return hit.CellID1 }
   156  
   157  type Contrib struct {
   158  	Mc      *McParticle
   159  	Energy  float32
   160  	Time    float32
   161  	PDG     int32
   162  	StepPos [3]float32
   163  }
   164  
   165  var (
   166  	_ sio.Versioner = (*SimCalorimeterHitContainer)(nil)
   167  	_ sio.Codec     = (*SimCalorimeterHitContainer)(nil)
   168  	_ Hit           = (*SimCalorimeterHit)(nil)
   169  )