go-hep.org/x/hep@v0.38.1/groot/cmd/root-diff/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-diff compares the content of 2 ROOT files, including the content of
     6  // their Trees (for all entries), if any.
     7  //
     8  // Example:
     9  //
    10  //	$> root-diff ./ref.root ./chk.root
    11  //	$> root-diff -k=key1,tree,my-tree ./ref.root ./chk.root
    12  //
    13  //	$> root-diff -h
    14  //	Usage: root-diff [options] a.root b.root
    15  //
    16  //	ex:
    17  //	 $> root-diff ./testdata/small-flat-tree.root ./testdata/small-flat-tree.root
    18  //
    19  //	options:
    20  //	  -k string
    21  //	    	comma-separated list of keys to inspect and compare (default=all common keys)
    22  package main // import "go-hep.org/x/hep/groot/cmd/root-diff"
    23  
    24  import (
    25  	"flag"
    26  	"fmt"
    27  	"log"
    28  	"os"
    29  	"strings"
    30  
    31  	"go-hep.org/x/hep/groot"
    32  	"go-hep.org/x/hep/groot/rcmd"
    33  	_ "go-hep.org/x/hep/groot/riofs/plugin/http"
    34  	_ "go-hep.org/x/hep/groot/riofs/plugin/xrootd"
    35  )
    36  
    37  func main() {
    38  	keysFlag := flag.String("k", "", "comma-separated list of keys to inspect and compare (default=all common keys)")
    39  
    40  	log.SetPrefix("root-diff: ")
    41  	log.SetFlags(0)
    42  
    43  	flag.Usage = func() {
    44  		fmt.Fprintf(os.Stderr, `Usage: root-diff [options] a.root b.root
    45  
    46  ex:
    47   $> root-diff ./testdata/small-flat-tree.root ./testdata/small-flat-tree.root
    48  
    49  options:
    50  `,
    51  		)
    52  		flag.PrintDefaults()
    53  	}
    54  
    55  	flag.Parse()
    56  
    57  	if flag.NArg() != 2 {
    58  		flag.Usage()
    59  		log.Fatalf("need 2 input ROOT files to compare")
    60  	}
    61  
    62  	err := rootdiff(flag.Arg(0), flag.Arg(1), *keysFlag)
    63  	if err != nil {
    64  		log.Fatalf("%+v", err)
    65  	}
    66  }
    67  
    68  func rootdiff(ref, chk string, keysFlag string) error {
    69  	fref, err := groot.Open(ref)
    70  	if err != nil {
    71  		return fmt.Errorf("could not open reference file: %w", err)
    72  	}
    73  	defer fref.Close()
    74  
    75  	fchk, err := groot.Open(chk)
    76  	if err != nil {
    77  		return fmt.Errorf("could not open check file: %w", err)
    78  	}
    79  	defer fchk.Close()
    80  
    81  	var keys []string
    82  	if keysFlag != "" {
    83  		keys = strings.Split(keysFlag, ",")
    84  	}
    85  
    86  	err = rcmd.Diff(nil, fchk, fref, keys)
    87  	if err != nil {
    88  		return fmt.Errorf("files differ: %w", err)
    89  	}
    90  
    91  	return nil
    92  }