go-hep.org/x/hep@v0.38.1/fads/unique_obj_finder.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 fads 6 7 import ( 8 "reflect" 9 10 "go-hep.org/x/hep/fwk" 11 ) 12 13 type ObjPair struct { 14 In string 15 Out string 16 } 17 18 type UniqueObjectFinder struct { 19 fwk.TaskBase 20 21 colls []ObjPair 22 } 23 24 func (tsk *UniqueObjectFinder) Configure(ctx fwk.Context) error { 25 var err error 26 27 for _, pair := range tsk.colls { 28 err = tsk.DeclInPort(pair.In, reflect.TypeOf([]Candidate{})) 29 if err != nil { 30 return err 31 } 32 33 err = tsk.DeclOutPort(pair.Out, reflect.TypeOf([]Candidate{})) 34 if err != nil { 35 return err 36 } 37 } 38 39 return err 40 } 41 42 func (tsk *UniqueObjectFinder) StartTask(ctx fwk.Context) error { 43 var err error 44 45 return err 46 } 47 48 func (tsk *UniqueObjectFinder) StopTask(ctx fwk.Context) error { 49 var err error 50 51 return err 52 } 53 54 func (tsk *UniqueObjectFinder) Process(ctx fwk.Context) error { 55 var err error 56 store := ctx.Store() 57 58 type Pair struct { 59 In []Candidate 60 Out []Candidate 61 } 62 63 colls := make([]Pair, 0, len(tsk.colls)) 64 for i, pair := range tsk.colls { 65 v, err2 := store.Get(pair.In) 66 if err2 != nil { 67 return err2 68 } 69 70 input := v.([]Candidate) 71 output := make([]Candidate, 0, len(input)) 72 colls = append(colls, Pair{ 73 In: input, 74 Out: output, 75 }) 76 77 defer func(i int) { 78 err2 := store.Put(tsk.colls[i].Out, colls[i].Out) 79 if err2 != nil { 80 err = err2 81 } 82 }(i) 83 } 84 85 for icol := range colls { 86 pair := &colls[icol] 87 input := pair.In 88 output := pair.Out 89 for i := range input { 90 cand := &input[i] 91 unique := false 92 uniqueloop: 93 for jcol := range icol { 94 jcands := colls[jcol].In 95 for j := range jcands { 96 jcand := &jcands[j] 97 if cand.Overlaps(jcand) { 98 unique = true 99 break uniqueloop 100 } 101 } 102 } 103 104 if !unique { 105 continue 106 } 107 output = append(output, *cand) 108 } 109 110 pair.Out = output 111 } 112 113 return err 114 } 115 116 func newUniqueObjectFinder(typ, name string, mgr fwk.App) (fwk.Component, error) { 117 var err error 118 119 tsk := &UniqueObjectFinder{ 120 TaskBase: fwk.NewTask(typ, name, mgr), 121 colls: make([]ObjPair, 0), 122 } 123 124 err = tsk.DeclProp("Keys", &tsk.colls) 125 if err != nil { 126 return nil, err 127 } 128 129 return tsk, err 130 } 131 132 func init() { 133 fwk.Register(reflect.TypeOf(UniqueObjectFinder{}), newUniqueObjectFinder) 134 }