go-hep.org/x/hep@v0.38.1/fwk/examples/fwk-ex-tuto-2/main.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 main
     6  
     7  import (
     8  	"flag"
     9  	"fmt"
    10  	"os"
    11  	"reflect"
    12  	"time"
    13  
    14  	"go-hep.org/x/hep/fwk"
    15  
    16  	// job is the scripting interface to 'fwk'
    17  	"go-hep.org/x/hep/fwk/job"
    18  
    19  	// we need to access some tools defined in fwktest (the ascii InputStream)
    20  	// so we need to directly import that package
    21  	"go-hep.org/x/hep/fwk/internal/fwktest"
    22  )
    23  
    24  var (
    25  	lvl    = flag.String("l", "INFO", "message level (DEBUG|INFO|WARN|ERROR)")
    26  	evtmax = flag.Int64("evtmax", -1, "number of events to process")
    27  	nprocs = flag.Int("nprocs", -1, "number of events to process concurrently")
    28  )
    29  
    30  func main() {
    31  	flag.Usage = func() {
    32  		fmt.Fprintf(os.Stderr, `Usage: %[1]s [options] <input-file>
    33  
    34  ex:
    35   $ %[1]s -l=INFO -evtmax=-1 ./input.ascii
    36  
    37  options:
    38  `,
    39  			os.Args[0],
    40  		)
    41  		flag.PrintDefaults()
    42  	}
    43  
    44  	flag.Parse()
    45  
    46  	fname := "input.ascii"
    47  	if flag.NArg() > 0 {
    48  		fname = flag.Arg(0)
    49  	}
    50  
    51  	start := time.Now()
    52  	fmt.Printf("::: %s...\n", os.Args[0])
    53  
    54  	// create a default fwk application, with some properties
    55  	// extracted from the CLI
    56  	app := job.New(job.P{
    57  		"EvtMax":   *evtmax,
    58  		"NProcs":   *nprocs,
    59  		"MsgLevel": job.MsgLevel(*lvl),
    60  	})
    61  
    62  	f, err := os.Open(fname)
    63  	if err != nil {
    64  		app.Errorf("could not open file [%s]: %v\n", fname, err)
    65  		os.Exit(1)
    66  	}
    67  	defer f.Close()
    68  
    69  	// create a task that reads integers from some location
    70  	// and publish the square of these integers under some other location
    71  	app.Create(job.C{
    72  		Type: "go-hep.org/x/hep/fwk/internal/fwktest.task2",
    73  		Name: "t2",
    74  		Props: job.P{
    75  			"Input":  "t1-ints1",
    76  			"Output": "t1-ints1-massaged",
    77  		},
    78  	})
    79  
    80  	// create an input-stream, reading from some io.Reader
    81  	// note we create it after the one that consumes these integers
    82  	// to exercize the automatic data-flow scheduling.
    83  	app.Create(job.C{
    84  		Type: "go-hep.org/x/hep/fwk.InputStream",
    85  		Name: "input",
    86  		Props: job.P{
    87  			"Ports": []fwk.Port{
    88  				{
    89  					Name: "t1-ints1",               // location where to publish our data
    90  					Type: reflect.TypeOf(int64(0)), // type of that data
    91  				},
    92  			},
    93  			"Streamer": &fwktest.InputStream{
    94  				R: f,
    95  			},
    96  		},
    97  	})
    98  
    99  	// run the application
   100  	app.Run()
   101  
   102  	fmt.Printf("::: %s... [done] (cpu=%v)\n", os.Args[0], time.Since(start))
   103  }
   104  
   105  /*
   106  output:
   107  
   108  $ fwk-ex-tuto-2
   109  ::: fwk-ex-tuto-2...
   110  t2                   INFO configure...
   111  t2                   INFO configure... [done]
   112  t2                   INFO start...
   113  app                  INFO >>> running evt=0...
   114  t2                   INFO proc... (id=0|0) => [0 -> 0]
   115  app                  INFO >>> running evt=1...
   116  t2                   INFO proc... (id=1|0) => [1 -> 1]
   117  app                  INFO >>> running evt=2...
   118  t2                   INFO proc... (id=2|0) => [2 -> 4]
   119  app                  INFO >>> running evt=3...
   120  t2                   INFO proc... (id=3|0) => [3 -> 9]
   121  app                  INFO >>> running evt=4...
   122  t2                   INFO proc... (id=4|0) => [4 -> 16]
   123  app                  INFO >>> running evt=5...
   124  t2                   INFO proc... (id=5|0) => [5 -> 25]
   125  app                  INFO >>> running evt=6...
   126  t2                   INFO proc... (id=6|0) => [6 -> 36]
   127  app                  INFO >>> running evt=7...
   128  t2                   INFO proc... (id=7|0) => [7 -> 49]
   129  app                  INFO >>> running evt=8...
   130  t2                   INFO proc... (id=8|0) => [8 -> 64]
   131  app                  INFO >>> running evt=9...
   132  t2                   INFO proc... (id=9|0) => [9 -> 81]
   133  app                  INFO >>> running evt=10...
   134  t2                   INFO proc... (id=10|0) => [10 -> 100]
   135  app                  INFO >>> running evt=11...
   136  t2                   INFO proc... (id=11|0) => [11 -> 121]
   137  app                  INFO >>> running evt=12...
   138  t2                   INFO proc... (id=12|0) => [12 -> 144]
   139  app                  INFO >>> running evt=13...
   140  t2                   INFO proc... (id=13|0) => [13 -> 169]
   141  app                  INFO >>> running evt=14...
   142  t2                   INFO proc... (id=14|0) => [14 -> 196]
   143  app                  INFO >>> running evt=15...
   144  t2                   INFO proc... (id=15|0) => [15 -> 225]
   145  app                  INFO >>> running evt=16...
   146  t2                   INFO proc... (id=16|0) => [16 -> 256]
   147  app                  INFO >>> running evt=17...
   148  t2                   INFO proc... (id=17|0) => [17 -> 289]
   149  app                  INFO >>> running evt=18...
   150  t2                   INFO proc... (id=18|0) => [18 -> 324]
   151  app                  INFO >>> running evt=19...
   152  t2                   INFO proc... (id=19|0) => [19 -> 361]
   153  app                  INFO >>> running evt=20...
   154  t2                   INFO stop...
   155  ::: fwk-ex-tuto-2... [done] (cpu=1.06487ms)
   156  
   157  */