Conversion of root file

I want to convert the given root file into the ASCII data file,i tried but,seems to make mistake somewhere,suppose its abc.root, then how should i convert it into suppose abc.txt?

Hi nice,
there is no conversion tool as far as I know, you have to read in the data from the root file (several ways to do it are described here) and write it to a text file as you would normally write things to file in c++, e.g. with a std::ofstream.

Thanks for the reply. I am trying the following and it gives the error as: illegal pointer to class object tr 0x0 3116

void dumpTreeTotxt()
{
  TFile *f=new TFile("rigidity.root"); // opens the root file
  TTree *tr=(TTree)*f->Get("tree"); // creates the TTree object
  tr->Scan(); // prints the content on the screen

  float a,b; // create variables of the same type as the branches you want to access

  tr->SetBranchAddress("TS",&a); // for all the TTree branches you need this
  tr->SetBranchAddress("ns",&b);
  

  ofstream myfile;
  myfile.open ("example.txt");
  myfile << "TS ns\n";

  for (int i=0;i<tr->GetEntries();i++){
    // loop over the tree
    tr->GetEntry(i);
    cout << a << " " << b << endl; //print to the screen
    myfile << a << " " << b << "\n"; //write to file
  }
  myfile.close();
}

You can do that with a single command TTree::Scan. See the first example here:

https://root.cern/doc/master/classTTreePlayer.html#aa0149b416e4b812a8762ec1e389ba2db

1 Like

you have a typo:

TTree *tr=(TTree)*f->Get("tree");

should be

TTree *tr=(TTree*)f->Get("tree");

or (safer)

TTree *tr = nullptr;
f->GetObject("tree", tr);

now its showing that function convert() is not defined in current scope (0) ,i don’t know now how this occurs after the above correction?

convert() ? I do not see any convert() function in the code you posted …

The filename of a ROOT macro must be the same as the name of the contained function.
Did you name the file that contains the snippet above convert.C? You should name it dumpTreeTotxt.C.

@eguiraud is right ! also consider doing what I suggested you i.e. : use Scan

I tried it,again it stating that : illegal pointer to class object tr 0x0 3116

Yeah,i am also trying to do that,not yet worked properly,lets see,i still need to figure it out what’s going wrong in this one actually.

I any case you need to access tr . It is difficult to help you further without having access to the file rigidity.root

ok,i am attaching that,you can have a look through that. rigidity.root (63.6 KB)

Your file does not contain the TTree “tree”

root [0]    TFile *f=new TFile("rigidity.root"); // opens the root file
root [1] f->ls()
TFile**		rigidity.root	
 TFile*		rigidity.root	
  KEY: TH1F	rigidity;1	
root [2] 

oh,i see . so how should i proceed now? thanks for bearing with me.

Your file does not contain a TTree with data, it just contains a histogram.
At this point it’s very hard to tell what you want to achieve exactly.

Its like,the graph is there with the root file and now i want to get the data in the text format like ASCII one from the given above plot.

i tried now doing like below,now its saying (void) 1 after i call this file in root.

void dumpTreeTotxt()
{
TFile *f=new TFile(“rigidity.root”); // opens the root file
TH1F tr=(TH1F)f->Get(“rigidity”);
tr->Scan(“a:b”); // prints the content on the screen

float a,b; // create variables of the same type as the branches you want to access

ofstream myfile;
myfile.open (“example.txt”);
myfile << “TS ns\n”;

for (int i=0;iGetEntries();i++){
// loop over the tree
tr->GetEntries(i);
cout << a << " " << b << endl; //print to the screen
myfile << a << " " << b << “\n”; //write to file
}
myfile.close();
}

Hi,

Scan is a method of TTree and not of TH1F. Also, you get pointers out of files when using TFile::Get and not concrete classes.
Perhaps you just need to loop over the bins of your histogram and write their content to a file?
In this case, what is needed is:

// these two lines are here for the sake of this example: you already have an histogram
TH1F h("h","h",64,0,4)
h.FillRandom("gaus")
// Here print on screen the content of each bin
for (auto idx : ROOT::TSeqI(h.GetNbinsX())) cout << h.GetBinContent(idx) << endl;

Cheers,
D

ok,btw, what does this error means and how to rectify it?
Error: memory allocation for () size=4 pinc=1 FILE: LINE:0