Root2yoda error

Dear expert,
I am trying to convert few root histograms in a root file to yoda scatter 2D hist format using root2yoda script.
But I get the following error :
Traceback (most recent call last):
*** File “./root2yoda”, line 46, in ***
*** analysisobjects = [to_yoda(path, ro) for (path, ro) in rootobjects]***
*** File “./root2yoda”, line 43, in to_yoda***
*** ao.path = path***
AttributeError: ‘yoda.core.Scatter2D’ object attribute ‘path’ is read-only

I have tried both root V6.16 and V6.22 and end up with same error

I am attaching the root file which I want to convert.
Data_Unfolded_Run2.root (68.2 KB)

Please suggest any solution. Thanks in advance.

it seems you are using the python version of the root2yoda script, shipped with the yoda project.

the root2yoda program shipped with the go-hep.org project doesn’t seem to have issues with the file you’ve attached:

$> curl -O -L https://go-hep.org/dist/latest/root2yoda-linux_amd64.exe
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 18.4M  100 18.4M    0     0  20.8M      0 --:--:-- --:--:-- --:--:-- 20.8M

$> chmod +x ./root2yoda-linux_amd64.exe 
$> ./root2yoda-linux_amd64.exe -o o.yoda.gz ./Data_Unfolded_Run2.root 
root2yoda: processing ./Data_Unfolded_Run2.root

$> ll
total 19M
-rw-r--r-- 1 binet binet 69K Oct 18 09:54 Data_Unfolded_Run2.root
-rw-r--r-- 1 binet binet 21K Oct 18 09:59 o.yoda.gz
-rwxr-xr-x 1 binet binet 19M Oct 18 09:58 root2yoda-linux_amd64.exe

$> file o.yoda.gz 
o.yoda.gz: gzip compressed data, original size modulo 2^32 123104

$> ./root2yoda-linux_amd64.exe  ./Data_Unfolded_Run2.root > o.yoda
root2yoda: processing ./Data_Unfolded_Run2.root

$> ll
total 19M
-rw-r--r-- 1 binet binet  69K Oct 18 09:54 Data_Unfolded_Run2.root
-rw-r--r-- 1 binet binet 121K Oct 18 09:59 o.yoda
-rw-r--r-- 1 binet binet  21K Oct 18 09:59 o.yoda.gz
-rwxr-xr-x 1 binet binet  19M Oct 18 09:58 root2yoda-linux_amd64.exe

$> head o.yoda
BEGIN YODA_HISTO1D_V2 /Edd_TUnfold_NoReg_typ_1_eta0_24_pt4
Path: /Edd_TUnfold_NoReg_typ_1_eta0_24_pt4
Title: 'HT2 Unfolded No Reg Charged Particles 2.4 #rho^{T}_{Tot}   pt4'
Type: Histo1D
---
# Mean: -3.702428e+00
# Area: 1.581013e+08
# ID	 ID	 sumw	 sumw2	 sumwx	 sumwx2	 numEntries
Total   	Total   	1.581013e+08	6.598662e+10	-5.853587e+08	2.401912e+09	3.788040e+05
Underflow	Underflow	0.000000e+00	0.000000e+00	0.000000e+00	0.000000e+00	0.000000e+00

hth,
-s

Thank you, so much for prompt reply.
But one issue still there, The type of the yoda file format is “Type: Histo1D”. But in Rivet, we need “Type=Scatter2D”.
Can it possible to get “Type=Scatter2D” format with this script?

it’s relatively easy with Go-HEP to convert a H1 histogram into a 2D scatter points:

but one would need to actually write the code that loops over your file (be it the YODA file or the ROOT one), convert each H1 into its scatter point equivalent, and save it (into the YODA or ROOT format).
I suppose one could do the same in ROOT (TH1D to TGraph{,Errors}) and run a root2yoda script on it.

as this is a rather specific task, I don’t think there is an off-the-shelf script or binary that does that.

you’ll have to write code :slight_smile:

FYI, here is what it would look like with Go-HEP:

package main

import (
	"flag"
	"log"
	"os"

	"go-hep.org/x/hep/groot"
	"go-hep.org/x/hep/groot/rhist"
	"go-hep.org/x/hep/hbook"
	"go-hep.org/x/hep/hbook/rootcnv"
	"go-hep.org/x/hep/hbook/yodacnv"
)

func main() {
	flag.Parse()

	f, err := groot.Open(flag.Arg(0))
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()

	w, err := os.Create("o.yoda")
	if err != nil {
		log.Fatalf("could not create output YODA file: %+v", err)
	}
	defer w.Close()

	for _, k := range f.Keys() {
		o, err := f.Get(k.Name())
		if err != nil {
			log.Fatal(err)
		}
		th1, ok := o.(rhist.H1)
		if !ok {
			log.Printf("discarding %q...", k)
			continue
		}

		h1 := rootcnv.H1D(th1)
		s2 := hbook.NewS2DFromH1D(h1)
		err = yodacnv.Write(w, s2)
		if err != nil {
			log.Fatalf("could not write %q: %+v", k.Name(), err)
		}
	}

	err = w.Close()
	if err != nil {
		log.Fatalf("could not close output YODA file: %+v", err)
	}
}

ex:

#> go build -o cnv ./main.go
#> ./cnv ./Data_Unfolded_Run2.root
#> head o.yoda
BEGIN YODA_SCATTER2D_V2 /Edd_TUnfold_NoReg_typ_0_eta0_3_pt0
Path: /Edd_TUnfold_NoReg_typ_0_eta0_3_pt0
Title: 'HT2 Unfolded No Reg Jets 2.4 #tau_{_{#perp}}   pt0'
Type: Scatter2D
---
# xval	 xerr-	 xerr+	 yval	 yerr-	 yerr+	
-6.410000e+00	3.000000e-01	3.000000e-01	6.368566e+09	8.675885e+07	8.675885e+07
-5.835000e+00	2.750000e-01	2.750000e-01	8.022018e+09	9.263745e+07	9.263745e+07
-5.305000e+00	2.550000e-01	2.550000e-01	8.259488e+09	8.544773e+07	8.544773e+07
-4.805000e+00	2.450000e-01	2.450000e-01	8.548090e+09	7.729565e+07	7.729565e+07

Thanks, get It.