go-hep.org/x/hep@v0.38.1/fwk/examples/fwk-ex-tuto-3/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> <output-file>
    33  
    34  ex:
    35   $ %[1]s -l=INFO -evtmax=-1 ./input.ascii ./output.ascii
    36  
    37  options:
    38  `,
    39  			os.Args[0],
    40  		)
    41  		flag.PrintDefaults()
    42  	}
    43  
    44  	flag.Parse()
    45  
    46  	ifname := "input.ascii"
    47  	if flag.NArg() > 0 {
    48  		ifname = flag.Arg(0)
    49  	}
    50  
    51  	ofname := "output.ascii"
    52  	if flag.NArg() > 1 {
    53  		ofname = flag.Arg(1)
    54  	}
    55  
    56  	start := time.Now()
    57  	fmt.Printf("::: %s...\n", os.Args[0])
    58  
    59  	// create a default fwk application, with some properties
    60  	// extracted from the CLI
    61  	app := job.New(job.P{
    62  		"EvtMax":   *evtmax,
    63  		"NProcs":   *nprocs,
    64  		"MsgLevel": job.MsgLevel(*lvl),
    65  	})
    66  
    67  	r, err := os.Open(ifname)
    68  	if err != nil {
    69  		app.Errorf("could not open input file [%s]: %v\n", ifname, err)
    70  		os.Exit(1)
    71  	}
    72  	defer r.Close()
    73  
    74  	w, err := os.Create(ofname)
    75  	if err != nil {
    76  		app.Errorf("could not create output file [%s]: %v\n", ofname, err)
    77  		os.Exit(1)
    78  	}
    79  	defer w.Close()
    80  
    81  	// create a task that reads integers from some location
    82  	// and publish the square of these integers under some other location
    83  	app.Create(job.C{
    84  		Type: "go-hep.org/x/hep/fwk/internal/fwktest.task2",
    85  		Name: "t2",
    86  		Props: job.P{
    87  			"Input":  "t1-ints1",
    88  			"Output": "t1-ints1-massaged",
    89  		},
    90  	})
    91  
    92  	// create an input-stream, reading from some io.Reader
    93  	// note we create it after the one that consumes these integers
    94  	// to exercize the automatic data-flow scheduling.
    95  	app.Create(job.C{
    96  		Type: "go-hep.org/x/hep/fwk.InputStream",
    97  		Name: "input",
    98  		Props: job.P{
    99  			"Ports": []fwk.Port{
   100  				{
   101  					Name: "t1-ints1",               // location where to publish our data
   102  					Type: reflect.TypeOf(int64(0)), // type of that data
   103  				},
   104  			},
   105  			"Streamer": &fwktest.InputStream{
   106  				R: r,
   107  			},
   108  		},
   109  	})
   110  
   111  	// create an output-stream
   112  	app.Create(job.C{
   113  		Type: "go-hep.org/x/hep/fwk.OutputStream",
   114  		Name: "output",
   115  		Props: job.P{
   116  			"Ports": []fwk.Port{
   117  				{
   118  					Name: "t1-ints1-massaged",      // location of data to write out
   119  					Type: reflect.TypeOf(int64(0)), // type of that data
   120  				},
   121  			},
   122  			"Streamer": &fwktest.OutputStream{
   123  				W: w,
   124  			},
   125  		},
   126  	})
   127  
   128  	// run the application
   129  	app.Run()
   130  
   131  	fmt.Printf("::: %s... [done] (cpu=%v)\n", os.Args[0], time.Since(start))
   132  }
   133  
   134  /*
   135  output:
   136  
   137  $ ::: fwk-ex-tuto-3...
   138  t2                   INFO configure...
   139  t2                   INFO configure... [done]
   140  t2                   INFO start...
   141  app                  INFO >>> running evt=0...
   142  t2                   INFO proc... (id=0|0) => [0 -> 0]
   143  app                  INFO >>> running evt=1...
   144  t2                   INFO proc... (id=1|0) => [1 -> 1]
   145  app                  INFO >>> running evt=2...
   146  t2                   INFO proc... (id=2|0) => [2 -> 4]
   147  app                  INFO >>> running evt=3...
   148  t2                   INFO proc... (id=3|0) => [3 -> 9]
   149  app                  INFO >>> running evt=4...
   150  t2                   INFO proc... (id=4|0) => [4 -> 16]
   151  app                  INFO >>> running evt=5...
   152  t2                   INFO proc... (id=5|0) => [5 -> 25]
   153  app                  INFO >>> running evt=6...
   154  t2                   INFO proc... (id=6|0) => [6 -> 36]
   155  app                  INFO >>> running evt=7...
   156  t2                   INFO proc... (id=7|0) => [7 -> 49]
   157  app                  INFO >>> running evt=8...
   158  t2                   INFO proc... (id=8|0) => [8 -> 64]
   159  app                  INFO >>> running evt=9...
   160  t2                   INFO proc... (id=9|0) => [9 -> 81]
   161  app                  INFO >>> running evt=10...
   162  t2                   INFO proc... (id=10|0) => [10 -> 100]
   163  app                  INFO >>> running evt=11...
   164  t2                   INFO proc... (id=11|0) => [11 -> 121]
   165  app                  INFO >>> running evt=12...
   166  t2                   INFO proc... (id=12|0) => [12 -> 144]
   167  app                  INFO >>> running evt=13...
   168  t2                   INFO proc... (id=13|0) => [13 -> 169]
   169  app                  INFO >>> running evt=14...
   170  t2                   INFO proc... (id=14|0) => [14 -> 196]
   171  app                  INFO >>> running evt=15...
   172  t2                   INFO proc... (id=15|0) => [15 -> 225]
   173  app                  INFO >>> running evt=16...
   174  t2                   INFO proc... (id=16|0) => [16 -> 256]
   175  app                  INFO >>> running evt=17...
   176  t2                   INFO proc... (id=17|0) => [17 -> 289]
   177  app                  INFO >>> running evt=18...
   178  t2                   INFO proc... (id=18|0) => [18 -> 324]
   179  app                  INFO >>> running evt=19...
   180  t2                   INFO proc... (id=19|0) => [19 -> 361]
   181  app                  INFO >>> running evt=20...
   182  t2                   INFO stop...
   183  ::: fwk-ex-tuto-3... [done] (cpu=2.127406ms)
   184  
   185  $ cat output.ascii
   186  0
   187  1
   188  4
   189  9
   190  16
   191  25
   192  36
   193  49
   194  64
   195  81
   196  100
   197  121
   198  144
   199  169
   200  196
   201  225
   202  256
   203  289
   204  324
   205  361
   206  */