go-hep.org/x/hep@v0.38.1/groot/rhist/hist_test.go (about)

     1  // Copyright ©2018 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 rhist_test
     6  
     7  import (
     8  	"fmt"
     9  	"os"
    10  	"path/filepath"
    11  	"reflect"
    12  	"testing"
    13  
    14  	"go-hep.org/x/hep/groot"
    15  	"go-hep.org/x/hep/groot/internal/rtests"
    16  	"go-hep.org/x/hep/groot/rhist"
    17  	"go-hep.org/x/hep/groot/riofs"
    18  	_ "go-hep.org/x/hep/groot/riofs/plugin/http"
    19  )
    20  
    21  func TestRWHist(t *testing.T) {
    22  
    23  	dir, err := os.MkdirTemp("", "groot-")
    24  	if err != nil {
    25  		t.Fatal(err)
    26  	}
    27  	defer os.RemoveAll(dir)
    28  
    29  	for i, tc := range rhist.HistoTestCases {
    30  		fname := filepath.Join(dir, fmt.Sprintf("histos-%d.root", i))
    31  		t.Run(tc.Name, func(t *testing.T) {
    32  			const kname = "my-key"
    33  
    34  			w, err := groot.Create(fname)
    35  			if err != nil {
    36  				t.Fatal(err)
    37  			}
    38  
    39  			err = w.Put(kname, tc.Want)
    40  			if err != nil {
    41  				t.Fatal(err)
    42  			}
    43  
    44  			if got, want := len(w.Keys()), 1; got != want {
    45  				t.Fatalf("invalid number of keys. got=%d, want=%d", got, want)
    46  			}
    47  
    48  			err = w.Close()
    49  			if err != nil {
    50  				t.Fatalf("error closing file: %v", err)
    51  			}
    52  
    53  			r, err := riofs.Open(fname)
    54  			if err != nil {
    55  				t.Fatal(err)
    56  			}
    57  			defer r.Close()
    58  
    59  			si := r.StreamerInfos()
    60  			if len(si) == 0 {
    61  				t.Fatalf("empty list of streamers")
    62  			}
    63  
    64  			if got, want := len(r.Keys()), 1; got != want {
    65  				t.Fatalf("invalid number of keys. got=%d, want=%d", got, want)
    66  			}
    67  
    68  			rgot, err := r.Get(kname)
    69  			if err != nil {
    70  				t.Fatal(err)
    71  			}
    72  
    73  			if got, want := rgot.(rtests.ROOTer), tc.Want; !reflect.DeepEqual(got, want) {
    74  				t.Fatalf("error reading back objstring.\ngot = %#v\nwant= %#v", got, want)
    75  			}
    76  
    77  			err = r.Close()
    78  			if err != nil {
    79  				t.Fatalf("error closing file: %v", err)
    80  			}
    81  
    82  			if !rtests.HasROOT {
    83  				t.Logf("skip test with ROOT/C++")
    84  				return
    85  			}
    86  
    87  			const rootls = `#include <iostream>
    88  #include "TFile.h"
    89  #include "TNamed.h"
    90  
    91  void rootls(const char *fname, const char *kname) {
    92  	auto f = TFile::Open(fname);
    93  	auto o = f->Get<TNamed>(kname);
    94  	if (o == NULL) {
    95  		std:cerr << "could not retrieve [" << kname << "]" << std::endl;
    96  		o->ClassName();
    97  	}
    98  	std::cout << "retrieved object: [" << o->GetName() << "]" << std::endl;
    99  }
   100  `
   101  			out, err := rtests.RunCxxROOT("rootls", []byte(rootls), fname, kname)
   102  			if err != nil {
   103  				t.Fatalf("ROOT/C++ could not open file %q:\n%s", fname, string(out))
   104  			}
   105  		})
   106  	}
   107  }
   108  
   109  func TestROOT4Hist(t *testing.T) {
   110  	f, err := groot.Open("../testdata/g4-hist.root")
   111  	if err != nil {
   112  		t.Fatalf("could not open uproot geant4 test file: %+v", err)
   113  	}
   114  	defer f.Close()
   115  
   116  	obj, err := f.Get("edep_inner")
   117  	if err != nil {
   118  		t.Fatalf("%+v", err)
   119  	}
   120  
   121  	h := obj.(*rhist.H1D)
   122  	if got, want := h.Name(), "edep_inner"; got != want {
   123  		t.Fatalf("invalid H1D name: got=%q, want=%q", got, want)
   124  	}
   125  }