Hello Root Experts!!
I have been trying to extract data from a .root file created via simulation to a .txt file with a comma after each entry (so I can use other programs to read it easily), but I’ve hit a wall.
I am a complete beginner in ROOT and C++ so I’ve been relying on past forum posts here and in Stack Exchange for guidance. I am running ROOT 6.16 on Ubuntu18.04.
I’ve managed to do so for a single leaf (mainly based off [https://stackoverflow.com/questions/28970124/cern-root-exporting-data-to-plain-text] )
#include <iostream>
#include "TFile.h"
#include "TTree.h"
#include <iomanip>
#include <sstream>
#include <string>
#include <fstream>
using namespace std;
void Extract(const char *filename, const char *treename, const char *leafname, const char *output){
TFile *file=new TFile(filename); // opens the root file
TTree *tree=(TTree*)file->Get(treename); // creates the TTree object
Float_t val[1]; //create variables of the same type
ofstream myfile;
myfile.open (output); // open file with name to the file as inputted at start
//printing header according to leaf name and entry
myfile << leafname<<"_1";
myfile << '\n';
Int_t n_events=(Int_t)tree->GetEntries(); // define vector of number of events
for (Int_t i=0;i<n_events;i++){
// loop over the tree
tree->GetEntry(i);
val[0] = tree->GetLeaf(leafname)->GetValue(0); // get the entry number
cout << val[0];
cout << endl; //print to the screen
myfile << val[0];
myfile << '\n'; //write to file
}
myfile.close();
}
But now, I wish to access each leaf automatically through loops so that I don’t have to manually extract each leaf/variable, since the .root file may contain anywhere between 20-100 leaves/variables.
I’ve come up with the following by changing a few things from above, giving:
#include <iostream>
#include "TFile.h"
#include "TTree.h"
#include <iomanip>
#include <sstream>
#include <string>
#include <fstream>
using namespace std;
void Extract(const char *filename, const char *treename, const char *output){
TFile *file=new TFile(filename); // opens the root
TTree *tree=(TTree*)file->Get(treename); // creates the TTree object
Float_t val; //create variables of the same type as leaves to access
// Store all leaves in a string vector ??
vector<string> LeafNames;
LeafNames->GetListOfLeaves();
ofstream myfile;
myfile.open (output); // open file with name to the file as inputted at start
Int_t n_leaves=(Int_t)tree->GetListOfLeaves(); // define vector of number of leaves
for (Int_t i=0;i<n_leaves;i++){
//printing header according to extracted leaf name
TLeaf* leaf = GetLeaf(LeafName[i]);
myfile << leaf <<",";
myfile << '\n';
Int_t n_events=(Int_t)tree->GetEntries(); // define vector of number of events
for (Int_t j=0;j<n_events;j++){
// Get values for the particular leaf
tree->GetEntry(j);
val = tree->GetLeaf(leaf)->GetValue(0);
//write to file
myfile << val<<",";
myfile << '\n';
}
}
myfile.close();
}
I know that this doesn’t work, and I am stuck particularly in a method to extract each leaf in the first loop. Most leaves are of type Float_t, but some may be Int_t or UInt_t so I’m also assuming that this may be an issue as I’ve read that the declared variable must be of the same type?
Any help would be muchly appreciated
Kind Regards
ROOT Version: 6.16/00
Platform: Not Provided
Compiler: Not Provided