Exemple #1
0
func (tsk *testhsvc) StopTask(ctx fwk.Context) error {
	var err error

	h := tsk.h1d.Hist
	if h.Entries() != *evtmax {
		return fwk.Errorf("expected %d entries. got=%d", *evtmax, h.Entries())
	}
	mean := h.Mean()
	if mean != 4.5 {
		return fwk.Errorf("expected mean=%v. got=%v", 4.5, mean)
	}

	rms := h.RMS()
	if rms != 2.8722813232690143 {
		return fwk.Errorf("expected RMS=%v. got=%v", 2.8722813232690143, rms)
	}
	msg := ctx.Msg()
	msg.Infof("histo[%s]: entries=%v mean=%v RMS=%v\n",
		tsk.h1d.ID,
		h.Entries(),
		h.Mean(),
		h.RMS(),
	)

	return err
}
Exemple #2
0
func (input *InputStreamer) Read(ctx fwk.Context) error {
	var err error
	store := ctx.Store()
	recs := make(map[string]struct{}, len(input.ports))
	for i := 0; i < len(input.ports); i++ {
		if !input.scan.Scan() {
			err = input.scan.Err()
			if err == nil {
				return io.EOF
			}
		}
		rec := input.scan.Record()
		blk := rec.Block(rec.Name())
		obj := reflect.New(input.ports[rec.Name()].Type).Elem()
		err := blk.Read(obj.Addr().Interface())
		if err != nil {
			return fwk.Errorf("block-read error: %v", err)
		}
		err = store.Put(rec.Name(), obj.Interface())
		if err != nil {
			return fwk.Errorf("store-put error: %v", err)
		}
		recs[rec.Name()] = struct{}{}
	}

	if len(recs) != len(input.ports) {
		return fwk.Errorf("fwk.rio: expected inputs: %d. got: %d.", len(input.ports), len(recs))
	}

	return err
}
Exemple #3
0
func (svc *hsvc) StopSvc(ctx fwk.Context) error {
	var err error

	errs := make([]error, 0, len(svc.r)+len(svc.w))

	// closing write-streams
	for n, w := range svc.w {

		werr := w.write()
		if werr != nil {
			errs = append(errs, fwk.Errorf("error flushing %q: %v", n, werr))
		}

		werr = w.close()
		if werr != nil {
			errs = append(errs, fwk.Errorf("error closing %q: %v", n, werr))
		}
	}

	// closing read-streams
	for n, r := range svc.r {

		rerr := r.close()
		if rerr != nil {
			errs = append(errs, fwk.Errorf("error closing %q: %v", n, rerr))
		}
	}

	if len(errs) > 0 {
		// FIXME(sbinet): return the complete list instead of the first one.
		//                use an errlist.Error ?
		return errs[0]
	}
	return err
}
Exemple #4
0
func (stream *ostream) write() error {
	for i := range stream.objs {
		obj := stream.objs[i]
		name := string(obj.ID)
		rec := stream.w.Record(name)
		err := rec.Connect(name, obj.Hist)
		if err != nil {
			return fwk.Errorf(
				"error writing object [%s] to stream [%s]: %v",
				name, stream.name, err,
			)
		}

		blk := rec.Block(name)
		err = blk.Write(obj.Hist)
		if err != nil {
			return fwk.Errorf(
				"error writing object [%s] to stream [%s]: %v",
				name, stream.name, err,
			)
		}

		err = rec.Write()
		if err != nil {
			return fwk.Errorf(
				"error writing object [%s] to stream [%s]: %v",
				name, stream.name, err,
			)
		}
	}

	return nil
}
Exemple #5
0
func (svc *hsvc) BookH1D(name string, nbins int, low, high float64) (fwk.H1D, error) {
	var err error
	var h fwk.H1D

	if !(fsm.Configured < svc.FSMState() && svc.FSMState() < fsm.Running) {
		return h, fwk.Errorf("fwk: can not book histograms during FSM-state %v", svc.FSMState())
	}

	stream, hid := svc.split(name)
	h = fwk.H1D{
		ID:   fwk.HID(hid),
		Hist: hbook.NewH1D(nbins, low, high),
	}

	switch stream {
	case "":
		// ok, temporary histo.
	default:
		sname := "/" + stream
		str, ok := svc.streams[sname]
		if !ok {
			return h, fwk.Errorf("fwk: no stream [%s] declared", sname)
		}
		switch str.Mode {
		case Read:
			r, ok := svc.r[sname]
			if !ok {
				return h, fwk.Errorf("fwk: no read-stream [%s] declared", sname)
			}
			err = r.read(hid, h.Hist)
			if err != nil {
				return h, err
			}

			r.objs = append(r.objs, h)
			svc.r[sname] = r

		case Write:
			w, ok := svc.w[sname]
			if !ok {
				return h, fwk.Errorf("fwk: no write-stream [%s] declared: %v", sname, svc.w)
			}
			w.objs = append(w.objs, h)
			svc.w[sname] = w
		default:
			return h, fwk.Errorf("%s: invalid stream mode (%d)", svc.Name(), str.Mode)
		}
	}

	hh := &h1d{H1D: h}
	svc.h1ds[h.ID] = hh
	return hh.H1D, err
}
Exemple #6
0
func (stream *istream) read(name string, ptr interface{}) error {
	var err error

	seekr, ok := stream.f.(io.Seeker)
	if !ok {
		return fwk.Errorf("hbooksvc: input stream [%s] is not seek-able", stream.name)
	}

	pos, err := seekr.Seek(0, 1)
	if err != nil {
		return err
	}
	defer seekr.Seek(pos, 0)

	_, err = seekr.Seek(0, 0)
	if err != nil {
		return err
	}

	r := seekr.(io.Reader)
	rr, err := rio.NewReader(r)
	if err != nil {
		return err
	}
	defer rr.Close()

	scan := rio.NewScanner(rr)
	scan.Select([]rio.Selector{{Name: name, Unpack: true}})
	if !scan.Scan() {
		return scan.Err()
	}
	rec := scan.Record()
	if rec == nil {
		return fwk.Errorf("hbooksvc: could not find record [%s] in stream [%s]", name, stream.name)
	}
	blk := rec.Block(name)
	if blk == nil {
		return fwk.Errorf(
			"hbooksvc: could not get block [%s] from record [%s] in stream [%s]",
			name, name, stream.name,
		)
	}
	err = blk.Read(ptr)
	if err != nil {
		return fwk.Errorf(
			"hbooksvc: could not read data from block [%s] from record [%s] in stream [%s]: %v",
			name, name, stream.name, err,
		)
	}
	return err
}
Exemple #7
0
func (o *OutputStreamer) Write(ctx fwk.Context) error {
	var err error
	store := ctx.Store()

	for i, rec := range o.recs {
		port := o.ports[i]

		n := rec.Name()
		blk := rec.Block(n)
		obj, err := store.Get(n)
		if err != nil {
			return err
		}

		rt := reflect.TypeOf(obj)
		if rt != port.Type {
			return fwk.Errorf("record[%s]: got type=%q. want type=%q.",
				rec.Name(),
				rt.Name(),
				port.Type,
			)
		}

		err = blk.Write(obj)
		if err != nil {
			return err
		}

		err = rec.Write()
		if err != nil {
			return err
		}
	}
	return err
}
Exemple #8
0
func (tsk *CPUCruncher) Configure(ctx fwk.Context) error {
	var err error

	for _, input := range tsk.inputs {
		if input == "" {
			continue
		}
		err = tsk.DeclInPort(input, reflect.TypeOf(int64(0)))
		if err != nil {
			return err
		}
	}

	for _, output := range tsk.outputs {
		if output == "" {
			continue
		}
		err = tsk.DeclOutPort(output, reflect.TypeOf(int64(0)))
		if err != nil {
			return err
		}
	}

	if len(tsk.cpus) <= 0 {
		msg := ctx.Msg()
		msg.Errorf("invalid cpu-timings list: %v\n", tsk.cpus)
		return fwk.Errorf("invalid cpu-timings")
	}

	return err
}
Exemple #9
0
func (tsk *testhsvc) StopTask(ctx fwk.Context) error {
	var err error

	h := tsk.h1d.Hist
	if h.Entries() != nentries {
		return fwk.Errorf("expected %d entries. got=%d", nentries, h.Entries())
	}
	mean := h.Mean()
	if mean != 4.5 {
		return fwk.Errorf("expected mean=%v. got=%v", 4.5, mean)
	}

	rms := h.RMS()
	if rms != 2.8722813232690143 {
		return fwk.Errorf("expected RMS=%v. got=%v", 2.8722813232690143, rms)
	}
	return err
}
Exemple #10
0
func (svc *hsvc) StartSvc(ctx fwk.Context) error {
	var err error

	for name, stream := range svc.streams {
		switch stream.Mode {
		case Read:
			_, dup := svc.r[name]
			if dup {
				return fwk.Errorf("%s: duplicate read-stream %q", svc.Name(), name)
			}
			// FIXME(sbinet): handle remote/local files + protocols
			f, err := os.Open(stream.Name)
			if err != nil {
				return fwk.Errorf("error opening file [%s]: %v", stream.Name, err)
			}
			r, err := rio.NewReader(f)
			if err != nil {
				return fwk.Errorf("error opening rio-stream [%s]: %v", stream.Name, err)
			}

			svc.r[name] = istream{
				name:  name,
				fname: stream.Name,
				f:     f,
				r:     r,
			}

		case Write:
			_, dup := svc.w[name]
			if dup {
				return fwk.Errorf("%s: duplicate write-stream %q", svc.Name(), name)
			}
			// FIXME(sbinet): handle remote/local files + protocols
			f, err := os.Create(stream.Name)
			if err != nil {
				return fwk.Errorf("error creating file [%s]: %v", stream.Name, err)
			}
			w, err := rio.NewWriter(f)
			if err != nil {
				return fwk.Errorf("error creating rio-stream [%s]: %v", stream.Name, err)
			}

			svc.w[name] = ostream{
				name:  name,
				fname: stream.Name,
				f:     f,
				w:     w,
			}

		default:
			return fwk.Errorf("%s: invalid stream mode (%d)", svc.Name(), stream.Mode)
		}
	}
	return err
}
Exemple #11
0
// Build applies some type-checking, collects setup functions and generates the sources of the fwk-based application.
func (b *Builder) Build() error {
	var err error

	if b.Name == "" {
		pwd, err := os.Getwd()
		if err != nil {
			return fwk.Error(err)
		}
		b.Name = filepath.Base(pwd)
	}

	err = b.doTypeCheck()
	if err != nil {
		return err
	}

	// check we build a 'main' package
	if !b.isMain() {
		return fwk.Errorf("not a 'main' package")
	}

	err = b.scanSetupFuncs()
	if err != nil {
		return err
	}

	if len(b.funcs) <= 0 {
		return fwk.Errorf("no setup function found")
	}

	err = b.genSources()
	if err != nil {
		return err
	}

	return err
}
Exemple #12
0
func (svc *hsvc) BookH1D(name string, nbins int, low, high float64) (fwk.H1D, error) {
	var err error
	h := fwk.H1D{
		ID:   fwk.HID(name),
		Hist: hbook.NewH1D(nbins, low, high),
	}

	if !(svc.FSMState() < fsm.Running) {
		return h, fwk.Errorf("fwk: can not book histograms during FSM-state %v", svc.FSMState())
	}

	hh := &h1d{H1D: h}
	svc.h1ds[h.ID] = hh
	return hh.H1D, err
}
Exemple #13
0
func (tsk *reducer) StopTask(ctx fwk.Context) error {
	var err error

	tsk.mux.RLock()
	sum := tsk.sum
	nevts := tsk.nevts
	tsk.mux.RUnlock()

	msg := ctx.Msg()
	if sum != tsk.exp {
		msg.Errorf("expected sum=%v. got=%v (nevts=%d)\n", tsk.exp, sum, nevts)
		return fwk.Errorf("%s: expected sum=%v. got=%v (nevts=%d)", tsk.Name(), tsk.exp, sum, nevts)
	}
	msg.Debugf("expected sum=%v. got=%v (all GOOD) (nevts=%d)\n", tsk.exp, sum, nevts)

	return err
}
Exemple #14
0
// NewBuilder creates a Builder from a list of file names or directories
func NewBuilder(fnames ...string) (*Builder, error) {
	var err error

	b := &Builder{
		fset:  token.NewFileSet(),
		files: make(map[string]*file, len(fnames)),
		funcs: make([]string, 0),
		Usage: `Usage: %[1]s [options] <input> <output>

ex:
 $ %[1]s -l=INFO -evtmax=-1 input.dat output.dat

options:
`,
	}

	for _, fname := range fnames {
		fi, err := os.Stat(fname)
		if err != nil {
			return nil, fwk.Error(err)
		}
		fm := fi.Mode()
		if fm.IsRegular() {
			src, err := ioutil.ReadFile(fname)
			if err != nil {
				return nil, fwk.Error(err)
			}
			f, err := parser.ParseFile(b.fset, fname, src, parser.ParseComments)
			if err != nil {
				return nil, fwk.Error(err)
			}
			b.files[fname] = &file{
				app:  b,
				f:    f,
				fset: b.fset,
				src:  src,
				name: fname,
			}
		}
		if fm.IsDir() {
			return nil, fwk.Errorf("directories not (yet) handled (got=%q)", fname)
		}
	}
	return b, err
}
Exemple #15
0
func (job *Job) setProp(c fwk.Component, name string, value interface{}) {
	if !job.app.HasProp(c, name) {
		err := fwk.Errorf("component [%s:%s] has no property named %q\n",
			c.Type(),
			c.Name(),
			name,
		)
		job.Errorf(err.Error())
		panic(err)
	}

	err := job.app.SetProp(c, name, value)
	if err != nil {
		job.Errorf(
			"could not set property name=%q value=%#v on component [%s]: %v\n",
			name, value,
			c.Name(),
			err,
		)
		panic(err)
	}
}