go-hep.org/x/hep@v0.38.1/lcio/example/lcio-ex-read-event/main.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 // lcio-ex-read-event is the hep/x/lcio example equivalent to: 6 // 7 // https://github.com/iLCSoft/LCIO/blob/master/examples/cpp/rootDict/readEventTree.C 8 // 9 // example: 10 // 11 // $> lcio-ex-read-event ./DST01-06_ppr004_bbcsdu.slcio 12 // lcio-ex-read-event: read 50 events from file "./DST01-06_ppr004_bbcsdu.slcio" 13 // $> open out.png 14 package main 15 16 import ( 17 "flag" 18 "image/color" 19 "io" 20 "log" 21 "os" 22 23 "go-hep.org/x/hep/hbook" 24 "go-hep.org/x/hep/hplot" 25 "go-hep.org/x/hep/lcio" 26 "gonum.org/v1/plot/vg" 27 ) 28 29 func main() { 30 log.SetPrefix("lcio-ex-read-event: ") 31 log.SetFlags(0) 32 33 var ( 34 fname = "" 35 h = hbook.NewH1D(100, 0., 100.) 36 nevts = 0 37 mcname = flag.String("mc", "MCParticlesSkimmed", "name of the MCParticle collection to read") 38 ) 39 40 flag.Parse() 41 42 if flag.NArg() > 0 { 43 fname = flag.Arg(0) 44 } 45 46 if fname == "" { 47 flag.Usage() 48 os.Exit(1) 49 } 50 51 f, err := lcio.Open(fname) 52 if err != nil { 53 log.Fatal(err) 54 } 55 defer f.Close() 56 57 for f.Next() { 58 evt := f.Event() 59 mcs := evt.Get(*mcname).(*lcio.McParticleContainer) 60 for _, mc := range mcs.Particles { 61 h.Fill(mc.Energy(), 1) 62 } 63 nevts++ 64 } 65 66 err = f.Err() 67 if err == io.EOF { 68 err = nil 69 } 70 71 if err != nil { 72 log.Fatal(err) 73 } 74 log.Printf("read %d events from file %q", nevts, fname) 75 76 p := hplot.New() 77 p.Title.Text = "LCIO -- McParticles" 78 p.X.Label.Text = "E (GeV)" 79 80 hh := hplot.NewH1D(h) 81 hh.Color = color.RGBA{R: 255, A: 255} 82 hh.Infos.Style = hplot.HInfoSummary 83 84 p.Add(hh) 85 p.Add(hplot.NewGrid()) 86 87 err = p.Save(20*vg.Centimeter, -1, "out.png") 88 if err != nil { 89 log.Fatal(err) 90 } 91 }