Values of data plotted

Hi everyone! I need help!
I can visualise “output.root” file structure by use of TBrowser and see related diagrams-such as energy spectrum in my simulation- by double click on the related tree.
How can I extract the values of data plotted in a graph to a .txt. file.

Sincerely,
Pat

Your question is a bit vague. And depending on the kind of objects in your root file the answer may vary. What are these object you are looking at ? TH1 ? TH2 ? TTrree ? TGraph ? …

The only way that comes to my mind right now is iterating over all tree entries and streaming your wanted variables into txt file one by one. I assume that you have a TTree object with variables.

#include <iostream>
#include <fstream>
using namespace std;

ofstream myfile;
myfile.open ("example.txt");

TFile * inputfile = new TFile::Open( "DIRECTORY");
TTree * yourtree = (TTree*) inputfile->Get("TREENAME");
double Energy; // EDIT:you may actually want to change double to float because that is usually the branch type
yourtree->SetBranchAddress("EnergyBranchName", &Energy);
for (int i=0;i<yourtree->GetEntries();++i)
{
yourtree->GetEntry(i);
myfile << std::to_string(Energy)+"\n";
}
1 Like

Dear Couet,
Thanks for your attention.
I am a new user.
For example, it is possible to me to extract the values of data plotted in Canvas_1, see attach image, to a .txt. file.

Dear mwojtas,
Thanks.
I am a new user.
I attached image of my root file in the previous reply.
I have some question about your code;
What is “EnergyBranchName” or “Energy” terns in your code?
Is “TREENAME” is “singles;553” in my example, as shown in the attached image?

Hello,
TREENAME in your example is “Singles” (case sensitive).
“EnergyBranchName” is “energy” (name of the branch with energy values).
Energy is a variable that will be updated every time that the loop will load new entry with GetEntry(i).
SetBranchAddress function assigns the address of this variable to the branch with name “energy”.

1 Like

Dear mwojtas,
I am so sorry for bothering you.
I received this error:

And so, “energy” is exist both in Singles;533 and Singles;532. While i want to energy values only for Singles;533 branch.

Oh yes, I think I made a small mistake in my code, you should change this:

TFile * inputfile = new TFile::Open( "DIRECTORY");

to this

TFile * inputfile = File::Open( "DIRECTORY", "READ" );

I received this:

Sorry again, but there was still a small typo -I must have deleted T in TFile when i was deleting “new”. So what you have to do is:

TFile * inputfile = TFile::Open( "DIRECTORY", "READ" );

I also ran it and checked that it works now, just to be sure :slight_smile:

Thanks,
But, I have following error; after typing " .x tt.cxx(); > tt.txt" command in terminal.

root [0] .x tt.cxx(); > tt.txt
Error: Missing one of ‘,)’ expected at or after line 13.
Error: Unexpected end of file (G__fgetstream_template():2) tt.cxx:18:
Error: Missing one of ‘,;{(’ expected at or after line 18.
Error: Unexpected end of file (G__fgetstream_template():2) tt.cxx:18:
Error: Missing one of ‘{’ expected at or after line 18.
Error: Unexpected end of file (G__fignorestream():3) tt.cxx:18:
*** Interpreter error recovered ***

It looks like it works for me:


And the txt output:

Maybe try just directly pasting it into the root command line?

1 Like

Thanks,
I received this:

#include <string>

See the documentation for std::to_string.

More importantly it is unnecessary to convert to string as the operator << can handle a multitude of types including float types.

myfile << Energy << "\n";

See the documentation for ostream::operator<<.

PS: It is easier to read if you copy the code and output into code blocks or preformatted blocks instead of screenshots.

1 Like

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