go-hep.org/x/hep@v0.38.1/groot/cmd/root-dump/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  // root-dump dumps the content of a ROOT file, including the content of
     6  // the Trees (for all entries), if any.
     7  //
     8  // Example:
     9  //
    10  //	$> root-dump ./testdata/small-flat-tree.root
    11  //	>>> file[./testdata/small-flat-tree.root]
    12  //	key[000]: tree;1 "my tree title" (TTree)
    13  //	[000][Int32]: 0
    14  //	[000][Int64]: 0
    15  //	[000][UInt32]: 0
    16  //	[000][UInt64]: 0
    17  //	[000][Float32]: 0
    18  //	[000][Float64]: 0
    19  //	[000][Str]: evt-000
    20  //	[000][ArrayInt32]: [0 0 0 0 0 0 0 0 0 0]
    21  //	[000][ArrayInt64]: [0 0 0 0 0 0 0 0 0 0]
    22  //	[000][ArrayInt32]: [0 0 0 0 0 0 0 0 0 0]
    23  //	[000][ArrayInt64]: [0 0 0 0 0 0 0 0 0 0]
    24  //	[000][ArrayFloat32]: [0 0 0 0 0 0 0 0 0 0]
    25  //	[000][ArrayFloat64]: [0 0 0 0 0 0 0 0 0 0]
    26  //	[000][N]: 0
    27  //	[000][SliceInt32]: []
    28  //	[000][SliceInt64]: []
    29  //	[...]
    30  //
    31  //	$> root-dump -h
    32  //	Usage: root-dump [options] f0.root [f1.root [...]]
    33  //
    34  //	ex:
    35  //	 $> root-dump ./testdata/small-flat-tree.root
    36  //	 $> root-dump -deep=0 ./testdata/small-flat-tree.root
    37  //
    38  //	options:
    39  //	  -cpu-profile string
    40  //	    	path to CPU profile output file
    41  //	  -deep
    42  //	    	enable deep dumping of values (including Trees' entries) (default true)
    43  //	  -name string
    44  //	    	regex of object names to dump
    45  package main // import "go-hep.org/x/hep/groot/cmd/root-dump"
    46  
    47  import (
    48  	"bufio"
    49  	"flag"
    50  	"fmt"
    51  	"io"
    52  	"log"
    53  	"os"
    54  	"regexp"
    55  	"runtime/pprof"
    56  
    57  	"go-hep.org/x/hep/groot/rcmd"
    58  	_ "go-hep.org/x/hep/groot/riofs/plugin/http"
    59  	_ "go-hep.org/x/hep/groot/riofs/plugin/xrootd"
    60  )
    61  
    62  var (
    63  	deepFlag = flag.Bool("deep", true, "enable deep dumping of values (including Trees' entries)")
    64  	nameFlag = flag.String("name", "", "regex of object names to dump")
    65  	cpuFlag  = flag.String("cpu-profile", "", "path to CPU profile output file")
    66  )
    67  
    68  func main() {
    69  	log.SetPrefix("root-dump: ")
    70  	log.SetFlags(0)
    71  
    72  	flag.Usage = func() {
    73  		fmt.Fprintf(os.Stderr, `Usage: root-dump [options] f0.root [f1.root [...]]
    74  
    75  ex:
    76   $> root-dump ./testdata/small-flat-tree.root
    77   $> root-dump -deep=0 ./testdata/small-flat-tree.root
    78  
    79  options:
    80  `,
    81  		)
    82  		flag.PrintDefaults()
    83  	}
    84  
    85  	flag.Parse()
    86  
    87  	if *nameFlag != "" {
    88  		reName = regexp.MustCompile(*nameFlag)
    89  	}
    90  
    91  	if flag.NArg() == 0 {
    92  		flag.Usage()
    93  		log.Fatalf("need at least one input ROOT file")
    94  	}
    95  
    96  	if *cpuFlag != "" {
    97  		f, err := os.Create(*cpuFlag)
    98  		if err != nil {
    99  			log.Fatalf("%+v", err)
   100  		}
   101  		err = pprof.StartCPUProfile(f)
   102  		if err != nil {
   103  			log.Fatalf("could not start CPU profiling: %+v", err)
   104  		}
   105  		defer pprof.StopCPUProfile()
   106  	}
   107  
   108  	out := bufio.NewWriter(os.Stdout)
   109  	defer out.Flush()
   110  
   111  	for _, fname := range flag.Args() {
   112  		err := dump(out, fname, *deepFlag)
   113  		if err != nil {
   114  			out.Flush()
   115  			log.Fatalf("error dumping file %q: %+v", fname, err)
   116  		}
   117  	}
   118  }
   119  
   120  func dump(w io.Writer, fname string, deep bool) error {
   121  	fmt.Fprintf(w, ">>> file[%s]\n", fname)
   122  	return rcmd.Dump(w, fname, deep, match)
   123  }
   124  
   125  var reName *regexp.Regexp
   126  
   127  func match(name string) bool {
   128  	if reName == nil {
   129  		return true
   130  	}
   131  	return reName.MatchString(name)
   132  }