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.
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";
}
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”.
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 ***