Conversion of root file

Hi,

perhaps it’s better not to pile up too many things here. Let’s leave the error aside for a moment: did you manage to print to file what you wanted?
By the way, are you using root5?

D

No,Actually trying to execute that only,i am getting the above error.Yeah,its version is 5.34/36. below is the given steps:

void dumpTreeTotxt()
{
TFile *f=new TFile(“rigidity.root”); // opens the root file
TH1F tr=(TH1F)f->Get(“rigidity”);

float a,b;

ofstream myfile;
myfile.open (“example.txt”);
myfile << “a b\n”;

for (auto idx : ROOT::TSeqI(f.GetNbinsX())) cout << f.GetBinContent(idx) << endl;

cout << a << " " << b << endl; //print to the screen
myfile << a << " " << b << "\n"; //write to file

myfile.close();
}

Hi,

the reason is that TSeq is not supported by 5.34 (we now concentrate all development on ROOT6 and 7 release series). You need a c++98 style loop there, i.e.:

for (int idx=0; i<f.GetNbinsX();++i) 

Note that the code you have will still not work. What are a and b? You need to fill them. Not sure with what, perhaps you want to put there bin content and error? Bin center and bin content? It’s easy to do either way in any case…

D

i want to get the values of x axis and corresponding y axis in the text file format from the above graph.

Hi,

you then need to get the bin centre and content via the appropriate getter methods.
(you still did not follow the advice given about the nature of the objects extracted from a file btw)

void dumpTreeTotxt()
{
TFile *f=new TFile("rigidity.root"); // opens the root file
TH1F tr=(TH1F*)f->Get("rigidity");

float a,b;

ofstream myfile;
myfile.open ("example.txt");
myfile << "a b\n";

for (int idx=0; i<tr.GetNbinsX();++idx) {
   a = tr.GetBinCenter(idx);
   b = tr.GetBinCenter(idx);
   cout << a << " " << b << endl; //print to the screen
   myfile << a << " " << b << "\n"; //write to file
}

myfile.close();
}

i am sorry,i am going through that now but,again i am getting the following error while doing like above as you stated
error: Can’t call TH1F::TH1F((class TH1F*)0x307dd70) in current scope

Hi nice,

As a general tip it is very helpful during development of ROOT scripts and C++ programs to have a good understanding of how types work and how to navigate the ROOT documentation.

In the docs we can find that the TDirectoryFile::Get function returns a pointer to the object, thus the type of tr needs to reflect this. (And then the syntax needs to change from tr.something to tr->something because of how c and c++ work.)

void dumpTreeTotxt()
{
TFile *f=new TFile("rigidity.root"); // opens the root file
TH1F * tr=(TH1F *)f->Get("rigidity");

float a,b;

ofstream myfile;
myfile.open ("example.txt");
myfile << "a b\n";

for (int idx=0; i<tr->GetNbinsX();++idx) {
   a = tr->GetBinCenter(idx);
   b = tr->GetBinContent(idx);
   cout << a << " " << b << endl; //print to the screen
   myfile << a << " " << b << "\n"; //write to file
}

myfile.close();
}

Cheers,
Kim

When i am doing the above,its giving the error that symbol i is not defined in current scope and when i change it to idx<tr in the above for loop,then after execution its just displaying as: (void)1.

Hi,

yes, there is a typo. Congrats for fixing that. Without the input file we cannot help further.

Cheers,
D

i have uploaded the input file earlier although,still,i am uploading it if you want.rigidity.root (63.6 KB)

I just ran the script on my machine and it seems to be working. I run the script as root -l dumpTreeTotxt.C and get output as follows:

a b
-0.05 0
0.05 0
0.15 0
0.25 0
...snip...

Did you check if the output file example.txt is created?

Yeah Thanks! its done eventually :slight_smile:

one last doubt,from ROOT when i just called like .x dumpTreeTotxt.c,then why it didn’t worked? although it used to work for other files.

Glad to hear it!

I wouldn’t know. Perhaps something was messed up in your root environment and a restart cleared that up :slight_smile:

FYI,

here is how you’d do it in Go, with the go-hep/rootio package:

package main

import (
        "fmt"
        "log"

        "go-hep.org/x/hep/rootio"
)

func main() {
        f, err := rootio.Open("rigidity.root")
        if err != nil {
                log.Fatal(err)
        }
        defer f.Close()

        obj, ok := f.Get("rigidity")
        if !ok {
                log.Fatalf("no object 'rigidity' in %s", f.Name())
        }

        h1 := obj.(*rootio.H1F)
        raw, err := h1.MarshalYODA()
        if err != nil {
                log.Fatal(err)
        }

        fmt.Printf("%s\n", string(raw))
}

ie:

$> go get go-hep.org/x/hep/rootio
$> go run ./main.go
BEGIN YODA_HISTO1D /rigidity
Path=/rigidity
Title=
Type=Histo1D
# Mean: NaN
# Area: NaN
# ID     ID      sumw    sumw2   sumwx   sumwx2  numEntries
Total           Total           1.352525e+07    1.300015e+10    3.472089e+09    7.880877e+12    900000
Underflow       Underflow       0.000000e+00    0.000000e+00    0.000000e+00    0.000000e+00    0
Overflow        Overflow        0.000000e+00    0.000000e+00    0.000000e+00    0.000000e+00    0
# xlow   xhigh   sumw    sumw2   sumwx   sumwx2  numEntries
0.000000e+00    1.000000e-01    0.000000e+00    0.000000e+00    0.000000e+00    0.000000e+00    0
1.000000e-01    2.000000e-01    0.000000e+00    0.000000e+00    0.000000e+00    0.000000e+00    0
2.000000e-01    3.000000e-01    0.000000e+00    0.000000e+00    0.000000e+00    0.000000e+00    0
3.000000e-01    4.000000e-01    0.000000e+00    0.000000e+00    0.000000e+00    0.000000e+00    0
[...]
9.999100e+03    9.999200e+03    0.000000e+00    0.000000e+00    0.000000e+00    0.000000e+00    0
9.999200e+03    9.999300e+03    0.000000e+00    0.000000e+00    0.000000e+00    0.000000e+00    0
9.999300e+03    9.999400e+03    0.000000e+00    0.000000e+00    0.000000e+00    0.000000e+00    0
9.999400e+03    9.999500e+03    0.000000e+00    0.000000e+00    0.000000e+00    0.000000e+00    0
9.999500e+03    9.999600e+03    1.020000e+02    0.000000e+00    0.000000e+00    0.000000e+00    102
9.999600e+03    9.999700e+03    0.000000e+00    0.000000e+00    0.000000e+00    0.000000e+00    0
9.999700e+03    9.999800e+03    0.000000e+00    0.000000e+00    0.000000e+00    0.000000e+00    0
9.999800e+03    9.999900e+03    0.000000e+00    0.000000e+00    0.000000e+00    0.000000e+00    0
9.999900e+03    1.000000e+04    0.000000e+00    0.000000e+00    0.000000e+00    0.000000e+00    0
END YODA_HISTO1D

hth,
-s

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.