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