go-hep.org/x/hep@v0.38.1/lcio/cmd/lcio-ls/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  // lcio-ls displays the content of a LCIO file.
     6  //
     7  // The default behaviour is to only display RunHeaders and EventHeaders.
     8  // Events' contents can be printed out with the --print-event flag.
     9  package main
    10  
    11  import (
    12  	"flag"
    13  	"fmt"
    14  	"io"
    15  	"log"
    16  	"os"
    17  	"reflect"
    18  
    19  	"go-hep.org/x/hep/lcio"
    20  )
    21  
    22  func main() {
    23  	log.SetPrefix("lcio-ls: ")
    24  	log.SetFlags(0)
    25  
    26  	var (
    27  		fname      = ""
    28  		nevts      = flag.Int64("n", -1, "number of events to inspect")
    29  		printEvent = flag.Bool("print-event", false, "enable event(s) printout")
    30  	)
    31  
    32  	flag.Parse()
    33  
    34  	if flag.NArg() > 0 {
    35  		fname = flag.Arg(0)
    36  	}
    37  
    38  	if fname == "" {
    39  		flag.Usage()
    40  		os.Exit(1)
    41  	}
    42  	inspect(os.Stdout, fname, *nevts, *printEvent)
    43  }
    44  
    45  func inspect(w io.Writer, fname string, nevts int64, printEvent bool) {
    46  	log.Printf("inspecting file [%s]...", fname)
    47  	r, err := lcio.Open(fname)
    48  	if err != nil {
    49  		log.Fatal(err)
    50  	}
    51  	defer r.Close()
    52  
    53  	rhdr := r.RunHeader()
    54  	ehdr := r.EventHeader()
    55  
    56  	evts := 0
    57  	for ievt := int64(0); r.Next() && (nevts < 0 || ievt < nevts); ievt++ {
    58  		if hdr := r.RunHeader(); !reflect.DeepEqual(hdr, rhdr) {
    59  			fmt.Fprintf(w, "%v\n", &hdr)
    60  			rhdr = hdr
    61  		}
    62  		if hdr := r.EventHeader(); !reflect.DeepEqual(hdr, ehdr) {
    63  			if !printEvent {
    64  				fmt.Fprintf(w, "%v\n", &hdr)
    65  			}
    66  			ehdr = hdr
    67  		}
    68  		if printEvent {
    69  			evt := r.Event()
    70  			fmt.Fprintf(w, "%v\n", &evt)
    71  		}
    72  		evts++
    73  	}
    74  	err = r.Err()
    75  	if err == io.EOF && evts == 0 {
    76  		if hdr := r.RunHeader(); !reflect.DeepEqual(hdr, rhdr) {
    77  			fmt.Fprintf(w, "%v\n", &hdr)
    78  		}
    79  		if hdr := r.EventHeader(); !reflect.DeepEqual(hdr, ehdr) {
    80  			fmt.Fprintf(w, "%v\n", &hdr)
    81  		}
    82  	}
    83  	if err != nil && err != io.EOF {
    84  		log.Fatal(err)
    85  	}
    86  }