go-hep.org/x/hep@v0.38.1/fads/cmd/fads-rivet-mc-generic/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  // fads-rivet-mc-generic is a command mirroring the MC_GENERIC analysis example from Rivet.
     6  //
     7  // More informations about Rivet: https://rivet.hepforge.org/
     8  //
     9  // fads-rivet-mc-generic reads HepMC events from some input file and
    10  // runs a single task: mc-generic.
    11  // mc-generic is modeled after Rivet's MC_GENERIC analysis (rivet/src/Analyses/MC_GENERIC.cc)
    12  //
    13  // mc-generic selects final state particles passing some acceptance
    14  // cuts (|eta|<5 && pt>0.5 GeV), and fills performance plots for these final
    15  // state particles (and the sub-sample of the charged final state particles.)
    16  //
    17  // Example:
    18  //
    19  //	$> curl -O -L http://www.hepforge.org/archive/rivet/Z-hadronic-LEP.hepmc
    20  //	$> fads-rivet-mc-generic -nprocs=1 ./Z-hadronic-LEP.hepmc
    21  //	::: fads-rivet-mc-generic...
    22  //	app                  INFO workers done: 1/1
    23  //	app                  INFO cpu: 6.115538196s
    24  //	app                  INFO mem: alloc:          12997 kB
    25  //	app                  INFO mem: tot-alloc:     751784 kB
    26  //	app                  INFO mem: n-mallocs:    7300674
    27  //	app                  INFO mem: n-frees:      7336526
    28  //	app                  INFO mem: gc-pauses:          2 ms
    29  //	::: fads-rivet-mc-generic... [done] (time=6.11575694s)
    30  //
    31  //	$> rio2yoda rivet.rio >| rivet.yoda
    32  package main
    33  
    34  import (
    35  	"flag"
    36  	"fmt"
    37  	"log"
    38  	"os"
    39  	"reflect"
    40  	"runtime/pprof"
    41  	"time"
    42  
    43  	"go-hep.org/x/hep/fads"
    44  	"go-hep.org/x/hep/fwk"
    45  	"go-hep.org/x/hep/fwk/hbooksvc"
    46  	"go-hep.org/x/hep/fwk/job"
    47  	"go-hep.org/x/hep/hepmc"
    48  )
    49  
    50  var (
    51  	profFlag   = flag.String("profile", "", "filename of cpuprofile")
    52  	lvlFlag    = flag.String("l", "INFO", "log level (DEBUG|INFO|WARN|ERROR)")
    53  	evtmaxFlag = flag.Int("evtmax", -1, "number of events to process")
    54  	nprocsFlag = flag.Int("nprocs", -1, "number of concurrent events to process")
    55  )
    56  
    57  func main() {
    58  	flag.Usage = func() {
    59  		fmt.Fprintf(os.Stderr, `Usage: fads-rivet-mc-generic [options] <hepmc-input-file>
    60  
    61  ex:
    62   $ fads-rivet-mc-generic -l=INFO -evtmax=-1 ./testdata/hepmc.data
    63  
    64  options:
    65  `,
    66  		)
    67  		flag.PrintDefaults()
    68  	}
    69  
    70  	flag.Parse()
    71  
    72  	if flag.NArg() <= 0 {
    73  		flag.Usage()
    74  		os.Exit(2)
    75  	}
    76  
    77  	start := time.Now()
    78  
    79  	fmt.Printf("::: fads-rivet-mc-generic...\n")
    80  	if *profFlag != "" {
    81  		f, err := os.Create(*profFlag)
    82  		if err != nil {
    83  			panic(err)
    84  		}
    85  		defer f.Close()
    86  		err = pprof.StartCPUProfile(f)
    87  		if err != nil {
    88  			log.Fatalf("could not start CPU profile: %+v", err)
    89  		}
    90  		defer pprof.StopCPUProfile()
    91  	}
    92  
    93  	app := job.New(job.P{
    94  		"EvtMax":   int64(*evtmaxFlag),
    95  		"NProcs":   *nprocsFlag,
    96  		"MsgLevel": job.MsgLevel(*lvlFlag),
    97  	})
    98  
    99  	ifname := flag.Arg(0)
   100  
   101  	// create histogram service
   102  	app.Create(job.C{
   103  		Type: "go-hep.org/x/hep/fwk/hbooksvc.hsvc",
   104  		Name: "histsvc",
   105  		Props: job.P{
   106  			"Streams": map[string]hbooksvc.Stream{
   107  				"/MC_GENERIC": {
   108  					Name: "rivet.rio",
   109  					Mode: hbooksvc.Write,
   110  				},
   111  			},
   112  		},
   113  	})
   114  
   115  	// read HepMC data
   116  	app.Create(job.C{
   117  		Type: "go-hep.org/x/hep/fwk.InputStream",
   118  		Name: "hepmc-streamer",
   119  		Props: job.P{
   120  			"Ports": []fwk.Port{
   121  				{
   122  					Name: "/fads/McEvent",
   123  					Type: reflect.TypeOf(hepmc.Event{}),
   124  				},
   125  			},
   126  			"Streamer": &fads.HepMcStreamer{
   127  				Name: ifname,
   128  			},
   129  		},
   130  	})
   131  
   132  	app.Create(job.C{
   133  		Type: "main.McGeneric",
   134  		Name: "mc-generic",
   135  		Props: job.P{
   136  			"Input": "/fads/McEvent",
   137  		},
   138  	})
   139  
   140  	app.Run()
   141  	fmt.Printf("::: fads-rivet-mc-generic... [done] (time=%v)\n", time.Since(start))
   142  }