go-hep.org/x/hep@v0.38.1/lcio/calohit_test.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_test
     6  
     7  import (
     8  	"compress/flate"
     9  	"reflect"
    10  	"testing"
    11  
    12  	"go-hep.org/x/hep/lcio"
    13  )
    14  
    15  func TestRWCalo(t *testing.T) {
    16  	for _, test := range []struct {
    17  		fname   string
    18  		complvl int
    19  	}{
    20  		{"testdata/calohit.slcio", flate.NoCompression},
    21  		{"testdata/calohit-compressed.slcio", flate.BestCompression},
    22  	} {
    23  		testRWCalo(t, test.complvl, test.fname)
    24  	}
    25  }
    26  
    27  func testRWCalo(t *testing.T, compLevel int, fname string) {
    28  	w, err := lcio.Create(fname)
    29  	if err != nil {
    30  		t.Error(err)
    31  		return
    32  	}
    33  	defer w.Close()
    34  	w.SetCompressionLevel(compLevel)
    35  
    36  	const (
    37  		nevents    = 10
    38  		nhits      = 100
    39  		CALHITS    = "CalorimeterHits"
    40  		CALHITSERR = "CalorimeterHitsWithEnergyError"
    41  	)
    42  
    43  	for i := range nevents {
    44  		evt := lcio.Event{
    45  			RunNumber:   4711,
    46  			EventNumber: int32(i),
    47  		}
    48  
    49  		var (
    50  			calhits    = lcio.CalorimeterHitContainer{Flags: lcio.BitsRChLong}
    51  			calhitsErr = lcio.CalorimeterHitContainer{Flags: lcio.BitsRChLong | lcio.BitsRChEnergyError}
    52  		)
    53  		for j := range nhits {
    54  			hit := lcio.CalorimeterHit{
    55  				CellID0:   int32(i*100000 + j),
    56  				Energy:    float32(i*j) + 117,
    57  				EnergyErr: float32(i*j) * 0.117,
    58  				Pos:       [3]float32{float32(i), float32(j), float32(i * j)},
    59  			}
    60  			calhits.Hits = append(calhits.Hits, hit)
    61  
    62  			hit.EnergyErr = float32(i*j) * 0.117
    63  			calhitsErr.Hits = append(calhitsErr.Hits, hit)
    64  		}
    65  
    66  		evt.Add(CALHITS, &calhits)
    67  		evt.Add(CALHITSERR, &calhitsErr)
    68  
    69  		err = w.WriteEvent(&evt)
    70  		if err != nil {
    71  			t.Errorf("%s: error writing event %d: %v", fname, i, err)
    72  			return
    73  		}
    74  	}
    75  
    76  	err = w.Close()
    77  	if err != nil {
    78  		t.Errorf("%s: error closing file: %v", fname, err)
    79  		return
    80  	}
    81  
    82  	r, err := lcio.Open(fname)
    83  	if err != nil {
    84  		t.Errorf("%s: error opening file: %v", fname, err)
    85  	}
    86  	defer r.Close()
    87  
    88  	for i := range nevents {
    89  		if !r.Next() {
    90  			t.Errorf("%s: error reading event %d", fname, i)
    91  			return
    92  		}
    93  		evt := r.Event()
    94  		if got, want := evt.RunNumber, int32(4711); got != want {
    95  			t.Errorf("%s: run-number error. got=%d. want=%d", fname, got, want)
    96  			return
    97  		}
    98  
    99  		if got, want := evt.EventNumber, int32(i); got != want {
   100  			t.Errorf("%s: run-number error. got=%d. want=%d", fname, got, want)
   101  			return
   102  		}
   103  
   104  		if !evt.Has(CALHITS) {
   105  			t.Errorf("%s: no %s collection", fname, CALHITS)
   106  			return
   107  		}
   108  
   109  		if !evt.Has(CALHITSERR) {
   110  			t.Errorf("%s: no %s collection", fname, CALHITSERR)
   111  			return
   112  		}
   113  
   114  		calhits := evt.Get(CALHITS).(*lcio.CalorimeterHitContainer)
   115  		calhitsErr := evt.Get(CALHITSERR).(*lcio.CalorimeterHitContainer)
   116  
   117  		for j := range nhits {
   118  			got := calhits.Hits[j]
   119  			want := lcio.CalorimeterHit{
   120  				CellID0: int32(i*100000 + j),
   121  				Energy:  float32(i*j) + 117,
   122  				Pos:     [3]float32{float32(i), float32(j), float32(i * j)},
   123  			}
   124  
   125  			if !reflect.DeepEqual(got, want) {
   126  				t.Errorf("%s: event %d hit[%d]\ngot= %#v\nwant=%#v\n",
   127  					fname, i, j,
   128  					got, want,
   129  				)
   130  				return
   131  			}
   132  
   133  			want.EnergyErr = float32(i*j) * 0.117
   134  			got = calhitsErr.Hits[j]
   135  			if !reflect.DeepEqual(got, want) {
   136  				t.Errorf("%s: event %d hit[%d]\ngot= %#v\nwant=%#v\n",
   137  					fname, i, j,
   138  					got, want,
   139  				)
   140  				return
   141  			}
   142  		}
   143  	}
   144  
   145  	err = r.Close()
   146  	if err != nil {
   147  		t.Errorf("%s: error closing file: %v", fname, err)
   148  		return
   149  	}
   150  }