CERN ROOT exporting data to plain text

Emil picture Emil · Mar 10, 2015 · Viewed 10.6k times · Source

So I have tried and tried to follow similar questions asked like this one, but to no success.

It's really simple - I have some .root files and can see the histograms in ROOT but want to export the data as a .txt or similar to be able to analyse it in other programs.

Answer

Asen Christov picture Asen Christov · Mar 19, 2015

Here is working example. Reads in a root file with three branches, named TS, ns, and nserr.

#include <iostream>
#include "TFile.h"
#include "TTree.h"
#include <fstream>
using namespace std;

void dumpTreeTotxt(){
  TFile *f=new TFile("TS0.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,c; // 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);
  tr->SetBranchAddress("nserr",&c);

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

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