Convert TH1 to .txt file


_ROOT Version:6.08
Platform: Not Provided
Compiler: Not Provided


i try to use the following program to convert a .root file to a .txt file

void dump(const char *fname = “ICSD.root”,
const char *nname = “ntuple_1”)
{
if (!fname || !(*fname) || !nname || !(*nname)) return; // just a precaution

TFile *f = TFile::Open(fname, “READ”);
if (!f) return; // just a precaution

TTree *t; f->GetObject(nname, t);
if (!t) { delete f; return; } // just a precaution

// See:
// http://root.cern.ch/root/html/TTreePlayer.html#TTreePlayer:Scan 9
// http://root.cern.ch/root/html/TTree.html#TTree:Scan 5
t->SetScanField(0);
t->Scan("*");

// See:
// http://root.cern.ch/root/html/TObject.html#TObject:SaveAs 3
t->SaveAs(“dump.xml”);
// t->SaveAs(TString::Format("%s.xml", nname));

delete f; // no longer needed (automatically deletes “t”)
}

my situation is shown in the following picture

i just want to get the detailed information in ICSD.root/ntuple_1/ionisations.
After i named the program above as ‘‘dump.cxx’’ and save it in the same folder of ICSD.root, i use it as : root [0] .> dump.txt root [1] .x dump.cxx root [2] .>
however only to find that:


and no tesult in dump.txt

So, could anyone tell me that any mistake in the program ?or any mistake in my operation?

Replace the fancy UTF double quotes with regular ASCII double quotes, such as:

"ntuple"

Thank you for your reply.
After replacing the fancy UTF double quotes with regular ASCII double quotes, i got the result in dump.txt.

However, the result does not seem to be what I wanted. What i want to get is just the information shown in the histogram below (for example: among 100000 Entries, 94000 entries are with zero ionisations, 3000 entries are with 1 ionisations, 200 entries are with 2 ionisations and so on).


however the result shown in the dump.txt is the number of ionisations for each entry(first entry 0 ionisations, second entry 0 ionisations,…tenth entry 1 ionisations). And this will make the dump.txt file very large when entries get larger.

so i would like to know, how can I change something in the program to get the result I want(i.e. just the information of the histogram in the first picture).

You can right-click the histogram and select “SaveAs”. Enter e.g. “hist.C” and you’ll get a C++ version of the file, which contains the bin counts. Is that a good start?

Thank you for your reply!

if you want to dump the content of an n-tuple into a text file, this program may help:

here are binaries for a few platforms:

and if you have Go installed (apt-get install -y golang), you just have to run this to install it:

$> go get go-hep.org/x/hep/cmd/root2csv

hth,
-s

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