main.go 4.69 KB
Newer Older
1 2 3 4 5 6 7 8 9
package main

import (
	"flag"
	"fmt"
	"log"
	"math"
	"os"

David Blyth's avatar
David Blyth committed
10 11
	"github.com/proio-org/go-proio"
	"github.com/proio-org/go-proio-pb/model/eic"
12 13 14 15
	"go-hep.org/x/hep/hbook"
	"gonum.org/v1/plot"
	"gonum.org/v1/plot/palette/moreland"
	"gonum.org/v1/plot/plotter"
16 17
	"gonum.org/v1/plot/vg/draw"
	"gonum.org/v1/plot/vg/vgimg"
18

David Blyth's avatar
David Blyth committed
19
	"eicweb.phy.anl.gov/EIC/eicplot"
20 21 22
)

var (
23 24 25 26 27 28 29 30
	pTMin     = flag.Float64("minpt", 0.5, "minimum transverse momentum")
	pTMax     = flag.Float64("maxpt", 30, "maximum transverse momentum")
	etaLimit  = flag.Float64("etalimit", 4, "maximum absolute value of eta")
	pullLimit = flag.Float64("pulllimit", 0.1, "maximum momentum pull in the color map")
	nBinsPT   = flag.Int("nbinspt", 10, "number of bins in transverse momentum")
	nBinsEta  = flag.Int("nbinseta", 10, "number of bins in eta")
	title     = flag.String("title", "", "plot title")
	output    = flag.String("output", "out.png", "output file")
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
)

func printUsage() {
	fmt.Fprintf(os.Stderr, `Usage: `+os.Args[0]+` [options] <proio-input-file>

options:
`,
	)
	flag.PrintDefaults()
}

func main() {
	flag.Usage = printUsage
	flag.Parse()
	if flag.NArg() != 1 {
		printUsage()
		log.Fatal("Invalid arguments")
	}

	p, _ := plot.New()
	p.Title.Text = *title
	p.X.Label.Text = "eta"
	p.Y.Label.Text = "p_T"
	p.X.Tick.Marker = eicplot.PreciseTicks{NSuggestedTicks: 5}
	p.Y.Tick.Marker = eicplot.PreciseTicks{NSuggestedTicks: 5}

	resGrid := NewPullGrid(*nBinsEta, -*etaLimit, *etaLimit, *nBinsPT, *pTMin, *pTMax)

	filename := flag.Arg(0)
	reader, err := proio.Open(filename)
	if err != nil {
		log.Fatal(err)
	}

David Blyth's avatar
David Blyth committed
65
	for event := range reader.ScanEvents(10) {
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
		ids := event.TaggedEntries("Reconstructed")
		for _, id := range ids {
			track, ok := event.GetEntry(id).(*eic.Track)
			if !ok {
				continue
			}

			partCandID := make(map[uint64]uint64)
			for _, obsID := range track.Observation {
				eDep, ok := event.GetEntry(obsID).(*eic.EnergyDep)
				if !ok {
					continue
				}

				for _, sourceID := range eDep.Source {
					simHit, ok := event.GetEntry(sourceID).(*eic.SimHit)
					if !ok {
						continue
					}

David Blyth's avatar
David Blyth committed
86
					partCandID[simHit.GetParticle()]++
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
				}
			}

			partID := uint64(0)
			hitCount := uint64(0)
			for id, count := range partCandID {
				if count > hitCount {
					partID = id
					hitCount = count
				}
			}

			part, ok := event.GetEntry(partID).(*eic.Particle)
			if !ok {
				continue
			}

			if len(track.Segment) == 0 {
				continue
			}

108 109 110
			pMag := math.Sqrt(math.Pow(float64(part.GetP().GetX()), 2) + math.Pow(float64(part.GetP().GetY()), 2) + math.Pow(float64(part.GetP().GetZ()), 2))
			eta := math.Atanh(float64(part.GetP().GetZ()) / pMag)
			pT := math.Sqrt(math.Pow(float64(part.GetP().GetX()), 2) + math.Pow(float64(part.GetP().GetY()), 2))
David Blyth's avatar
David Blyth committed
111
			chargeMag := math.Abs(float64(part.GetCharge()))
112
			poqMag := pMag / chargeMag
David Blyth's avatar
David Blyth committed
113 114 115
			trackPoqMag := math.Sqrt(math.Pow(track.Segment[0].GetPoq().GetX(), 2) +
				math.Pow(track.Segment[0].GetPoq().GetY(), 2) +
				math.Pow(track.Segment[0].GetPoq().GetZ(), 2))
116 117 118 119 120 121 122

			resGrid.Fill(eta, pT, trackPoqMag/poqMag)
		}
	}

	reader.Close()

123 124 125 126 127
	img := vgimg.New(670, 400)
	dc := draw.New(img)
	dc0 := draw.Crop(dc, 0, -70, 0, 0)
	dc1 := draw.Crop(dc, 620, 0, 0, 0)

128
	colorMap := moreland.SmoothBlueRed()
129 130
	colorMap.SetMin(1.0 - *pullLimit)
	colorMap.SetMax(1.0 + *pullLimit)
131
	heatMap := plotter.NewHeatMap(resGrid, colorMap.Palette(1000))
132 133
	heatMap.Min = 1.0 - *pullLimit
	heatMap.Max = 1.0 + *pullLimit
134 135
	p.Add(heatMap)

136
	p.Draw(dc0)
137 138 139 140 141 142 143 144 145 146

	p, _ = plot.New()

	colorBar := &plotter.ColorBar{ColorMap: colorMap}
	colorBar.Vertical = true
	p.Add(colorBar)
	p.HideX()
	p.Y.Padding = 0
	p.Y.Tick.Marker = eicplot.PreciseTicks{NSuggestedTicks: 3}

147 148 149 150 151 152 153 154 155 156
	p.Draw(dc1)

	w, err := os.Create(*output)
	if err != nil {
		log.Panic(err)
	}
	png := vgimg.PngCanvas{Canvas: img}
	if _, err = png.WriteTo(w); err != nil {
		log.Panic(err)
	}
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
}

type PullGrid struct {
	hCount, hV, hV2 *hbook.H2D
	nBinsX, nBinsY  int
	xLow, xHigh     float64
	yLow, yHigh     float64
}

func NewPullGrid(nBinsX int, xLow, xHigh float64, nBinsY int, yLow, yHigh float64) *PullGrid {
	return &PullGrid{
		hbook.NewH2D(nBinsX, xLow, xHigh, nBinsY, yLow, yHigh),
		hbook.NewH2D(nBinsX, xLow, xHigh, nBinsY, yLow, yHigh),
		hbook.NewH2D(nBinsX, xLow, xHigh, nBinsY, yLow, yHigh),
		nBinsX, nBinsY,
		xLow, xHigh,
		yLow, yHigh,
	}
}

func (g *PullGrid) Fill(x, y, z float64) {
	g.hCount.Fill(x, y, 1)
	g.hV.Fill(x, y, z)
	g.hV2.Fill(x, y, z*z)
}

func (g *PullGrid) Dims() (int, int) {
	return g.nBinsX, g.nBinsY
}

func (g *PullGrid) Z(i, j int) float64 {
	n := g.hCount.GridXYZ().Z(i, j)
	if n < 3 {
		return 0
	}
	mean := g.hV.GridXYZ().Z(i, j) / n

	return mean
}

func (g *PullGrid) X(i int) float64 {
	return g.hCount.GridXYZ().X(i)
}

func (g *PullGrid) Y(j int) float64 {
	return g.hCount.GridXYZ().Y(j)
}