|
package main |
|
|
|
import ( |
|
"bytes" |
|
"encoding/json" |
|
"errors" |
|
"flag" |
|
"io" |
|
"log" |
|
"os" |
|
) |
|
|
|
var ( |
|
ErrEmptyEventID = errors.New("empty EventID JSON data") |
|
) |
|
|
|
type Point2D struct { |
|
X float64 |
|
Y float64 |
|
} |
|
|
|
type Point3D struct { |
|
Point2D |
|
Z float64 |
|
} |
|
|
|
type Layer byte |
|
|
|
const ( |
|
LayerIBL Layer = iota |
|
LayerB1 |
|
LayerB2 |
|
LayerB3 |
|
LayerEC1 |
|
LayerEC2 |
|
LayerEC3 |
|
LayerEC4 |
|
LayerEC5 |
|
LayerEC6 |
|
NumLayers |
|
) |
|
|
|
type Geometry struct { |
|
IDCompact uint64 |
|
BarrelEC byte |
|
LayerDisk Layer |
|
PhiIndex int |
|
EtaIndex int |
|
Local Point2D |
|
Global Point3D |
|
} |
|
|
|
func (g *Geometry) UnmarshalJSON(data []byte) error { |
|
m := struct { |
|
IDCompact uint64 |
|
BarrelEC int |
|
LayerDisk int |
|
PhiIndex int |
|
EtaIndex int |
|
LocalX float64 |
|
LocalY float64 |
|
GlobalX float64 |
|
GlobalY float64 |
|
GlobalZ float64 |
|
}{} |
|
dec := json.NewDecoder(bytes.NewReader(data)) |
|
err := dec.Decode(&m) |
|
if err != nil { |
|
return err |
|
} |
|
g.IDCompact = m.IDCompact |
|
g.BarrelEC = byte(m.BarrelEC) |
|
g.LayerDisk = Layer(m.LayerDisk) |
|
g.PhiIndex = m.PhiIndex |
|
g.EtaIndex = m.EtaIndex |
|
g.Local.X = m.LocalX |
|
g.Local.Y = m.LocalY |
|
g.Global.X = m.GlobalX |
|
g.Global.Y = m.GlobalY |
|
g.Global.Z = m.GlobalZ |
|
|
|
return err |
|
} |
|
|
|
type Event struct { |
|
ID struct { |
|
Run int |
|
Evt int |
|
} |
|
|
|
Clusters []Cluster |
|
} |
|
|
|
func (evt *Event) UnmarshalJSON(data []byte) error { |
|
m := struct { |
|
EventID []uint64 |
|
Clusters []struct { |
|
Identifier uint64 |
|
GlobalX float64 |
|
GlobalY float64 |
|
GlobalZ float64 |
|
WidthPhiR float32 |
|
WidthZ float32 |
|
EnergyLoss int `json:"energyLoss"` |
|
SplitProba1 int `json:"splitProbability1"` |
|
SplitProba2 int `json:"splitProbability2"` |
|
Deposits []Deposit |
|
} |
|
}{} |
|
|
|
err := json.NewDecoder(bytes.NewReader(data)).Decode(&m) |
|
if err != nil { |
|
return err |
|
} |
|
|
|
if len(m.EventID) < 2 { |
|
log.Printf("**warn** empty EventID\n") |
|
//return ErrEmptyEventID |
|
return nil |
|
} |
|
|
|
evt.ID.Run = int(m.EventID[1]) |
|
evt.ID.Evt = int(m.EventID[0]) |
|
evt.Clusters = make([]Cluster, 0, len(m.Clusters)) |
|
for i, clu := range m.Clusters { |
|
evt.Clusters = append( |
|
evt.Clusters, |
|
Cluster{ |
|
Identifier: clu.Identifier, |
|
Global: Point3D{ |
|
Point2D: Point2D{ |
|
X: clu.GlobalX, |
|
Y: clu.GlobalY, |
|
}, |
|
Z: clu.GlobalZ, |
|
}, |
|
WidthPhiR: clu.WidthPhiR, |
|
WidthZ: clu.WidthZ, |
|
EnergyLoss: clu.EnergyLoss, |
|
SplitProba1: clu.SplitProba1, |
|
SplitProba2: clu.SplitProba2, |
|
Deposits: make([]Deposit, len(clu.Deposits)), |
|
}, |
|
) |
|
copy(evt.Clusters[i].Deposits, clu.Deposits) |
|
} |
|
|
|
return err |
|
} |
|
|
|
type Cluster struct { |
|
Identifier uint64 |
|
Global Point3D |
|
WidthPhiR float32 |
|
WidthZ float32 |
|
EnergyLoss int `json:"energyLoss"` |
|
SplitProba1 int `json:"splitProbability1"` |
|
SplitProba2 int `json:"splitProbability2"` |
|
Deposits []Deposit |
|
} |
|
|
|
type Deposit struct { |
|
Charge float32 |
|
TruthEventIndex int |
|
Barcode int `json:"BarCode"` |
|
} |
|
|
|
var ( |
|
geoFname = flag.String("geo", "100Mu_geodump.json", "path to geo data file") |
|
pixFname = flag.String("pix", "100Mu_pixclusterdump.json", "path to pixel-clusters data file") |
|
) |
|
|
|
func main() { |
|
flag.Parse() |
|
fgeo, err := os.Open(*geoFname) |
|
if err != nil { |
|
log.Fatalf("could not open [%s]: %v\n", *geoFname, err) |
|
} |
|
defer fgeo.Close() |
|
|
|
geo, err := loadGeometry(fgeo) |
|
if err != nil { |
|
log.Fatalf("could not load geometry: %v\n", err) |
|
} |
|
log.Printf("geo: %d\n", len(geo)) |
|
|
|
fpix, err := os.Open(*pixFname) |
|
if err != nil { |
|
log.Fatalf("could not open [%s]: %v\n", *pixFname, err) |
|
} |
|
defer fpix.Close() |
|
|
|
evts, err := loadEvents(fpix) |
|
if err != nil { |
|
log.Fatalf("could not load events: %v\n", err) |
|
} |
|
log.Printf("evts: %d\n", len(evts)) |
|
} |
|
|
|
func loadGeometry(r io.Reader) ([]Geometry, error) { |
|
geo := make([]Geometry, 0, 1024) |
|
dec := json.NewDecoder(r) |
|
err := dec.Decode(&geo) |
|
if err != nil { |
|
return nil, err |
|
} |
|
return geo, err |
|
} |
|
|
|
func loadEvents(r io.Reader) ([]Event, error) { |
|
rawevts := make([]Event, 0, 1024) |
|
err := json.NewDecoder(r).Decode(&rawevts) |
|
if err != nil { |
|
return nil, err |
|
} |
|
|
|
evts := make([]Event, 0, len(rawevts)) |
|
for _, evt := range rawevts { |
|
if evt.ID.Evt == 0 && evt.ID.Run == 0 { |
|
continue |
|
} |
|
evts = append(evts, evt) |
|
} |
|
|
|
return evts, err |
|
} |