go-hep.org/x/hep@v0.38.1/lcio/README.md (about) 1 # lcio 2 3 [](https://godoc.org/go-hep.org/x/hep/lcio) 4 5 `lcio` is a pure `Go` implementation of [LCIO](https://github.com/iLCSoft/LCIO). 6 7 ## Installation 8 9 ```sh 10 $ go get go-hep.org/x/hep/lcio 11 ``` 12 13 ## Documentation 14 15 The documentation is browsable at godoc.org: 16 17 - https://godoc.org/go-hep.org/x/hep/lcio 18 19 ## Example 20 21 ### Reading a LCIO event file 22 23 [embedmd]:# (reader_test.go go /func ExampleReader/ /\n}/) 24 ```go 25 func ExampleReader() { 26 r, err := lcio.Open("testdata/event_golden.slcio") 27 if err != nil { 28 log.Fatal(err) 29 } 30 defer r.Close() 31 32 for r.Next() { 33 evt := r.Event() 34 fmt.Printf("event number = %d (weight=%+e)\n", evt.EventNumber, evt.Weight()) 35 fmt.Printf("run number = %d\n", evt.RunNumber) 36 fmt.Printf("detector = %q\n", evt.Detector) 37 fmt.Printf("collections = %v\n", evt.Names()) 38 calohits := evt.Get("CaloHits").(*lcio.CalorimeterHitContainer) 39 fmt.Printf("calohits: %d\n", len(calohits.Hits)) 40 for i, hit := range calohits.Hits { 41 fmt.Printf(" calohit[%d]: cell-id0=%d cell-id1=%d ene=%+e ene-err=%+e\n", 42 i, hit.CellID0, hit.CellID1, hit.Energy, hit.EnergyErr, 43 ) 44 } 45 } 46 47 err = r.Err() 48 if err == io.EOF { 49 err = nil 50 } 51 52 if err != nil { 53 log.Fatal(err) 54 } 55 56 // Output: 57 // event number = 52 (weight=+4.200000e+01) 58 // run number = 42 59 // detector = "my detector" 60 // collections = [McParticles SimCaloHits CaloHits] 61 // calohits: 1 62 // calohit[0]: cell-id0=1024 cell-id1=2048 ene=+1.000000e+03 ene-err=+1.000000e-01 63 } 64 ``` 65 66 ### Writing a LCIO event file 67 68 [embedmd]:# (writer_test.go go /func ExampleWriter/ /\n}/) 69 ```go 70 func ExampleWriter() { 71 w, err := lcio.Create("out.slcio") 72 if err != nil { 73 log.Fatal(err) 74 } 75 defer w.Close() 76 77 run := lcio.RunHeader{ 78 RunNumber: 42, 79 Descr: "a simple run header", 80 Detector: "my detector", 81 SubDetectors: []string{"det-1", "det-2"}, 82 Params: lcio.Params{ 83 Floats: map[string][]float32{ 84 "floats-1": {1, 2, 3}, 85 "floats-2": {4, 5, 6}, 86 }, 87 }, 88 } 89 90 err = w.WriteRunHeader(&run) 91 if err != nil { 92 log.Fatal(err) 93 } 94 95 const NEVENTS = 1 96 for ievt := range NEVENTS { 97 evt := lcio.Event{ 98 RunNumber: run.RunNumber, 99 Detector: run.Detector, 100 EventNumber: 52 + int32(ievt), 101 TimeStamp: 1234567890 + int64(ievt), 102 Params: lcio.Params{ 103 Floats: map[string][]float32{ 104 "_weight": {42}, 105 }, 106 Strings: map[string][]string{ 107 "Descr": {"a description"}, 108 }, 109 }, 110 } 111 112 calhits := lcio.CalorimeterHitContainer{ 113 Flags: lcio.BitsRChLong | lcio.BitsRChID1 | lcio.BitsRChTime | lcio.BitsRChNoPtr | lcio.BitsRChEnergyError, 114 Params: lcio.Params{ 115 Floats: map[string][]float32{"f32": {1, 2, 3}}, 116 Ints: map[string][]int32{"i32": {1, 2, 3}}, 117 Strings: map[string][]string{"str": {"1", "2", "3"}}, 118 }, 119 Hits: []lcio.CalorimeterHit{ 120 { 121 CellID0: 1024, 122 CellID1: 2048, 123 Energy: 1000, 124 EnergyErr: 0.1, 125 Time: 1234, 126 Pos: [3]float32{11, 22, 33}, 127 Type: 42, 128 }, 129 }, 130 } 131 132 evt.Add("CaloHits", &calhits) 133 134 fmt.Printf("evt has key %q: %v\n", "CaloHits", evt.Has("CaloHits")) 135 136 err = w.WriteEvent(&evt) 137 if err != nil { 138 log.Fatal(err) 139 } 140 } 141 142 err = w.Close() 143 if err != nil { 144 log.Fatal(err) 145 } 146 147 // Output: 148 // evt has key "CaloHits": true 149 } 150 ``` 151 152 ### Reading and plotting McParticles' energy 153 154 ```sh 155 $> lcio-ex-read-event ./DST01-06_ppr004_bbcsdu.slcio 156 lcio-ex-read-event: read 50 events from file "./DST01-06_ppr004_bbcsdu.slcio" 157 158 $> open out.png 159 ``` 160 161  162 163 [embedmd]:# (example/lcio-ex-read-event/main.go go /func main/ /\n}/) 164 ```go 165 func main() { 166 log.SetPrefix("lcio-ex-read-event: ") 167 log.SetFlags(0) 168 169 var ( 170 fname = "" 171 h = hbook.NewH1D(100, 0., 100.) 172 nevts = 0 173 mcname = flag.String("mc", "MCParticlesSkimmed", "name of the MCParticle collection to read") 174 ) 175 176 flag.Parse() 177 178 if flag.NArg() > 0 { 179 fname = flag.Arg(0) 180 } 181 182 if fname == "" { 183 flag.Usage() 184 os.Exit(1) 185 } 186 187 f, err := lcio.Open(fname) 188 if err != nil { 189 log.Fatal(err) 190 } 191 defer f.Close() 192 193 for f.Next() { 194 evt := f.Event() 195 mcs := evt.Get(*mcname).(*lcio.McParticleContainer) 196 for _, mc := range mcs.Particles { 197 h.Fill(mc.Energy(), 1) 198 } 199 nevts++ 200 } 201 202 err = f.Err() 203 if err == io.EOF { 204 err = nil 205 } 206 207 if err != nil { 208 log.Fatal(err) 209 } 210 log.Printf("read %d events from file %q", nevts, fname) 211 212 p := hplot.New() 213 p.Title.Text = "LCIO -- McParticles" 214 p.X.Label.Text = "E (GeV)" 215 216 hh := hplot.NewH1D(h) 217 hh.Color = color.RGBA{R: 255, A: 255} 218 hh.Infos.Style = hplot.HInfoSummary 219 220 p.Add(hh) 221 p.Add(hplot.NewGrid()) 222 223 err = p.Save(20*vg.Centimeter, -1, "out.png") 224 if err != nil { 225 log.Fatal(err) 226 } 227 } 228 ``` 229