CERN ROOT exporting data to plain text

耗尽温柔 提交于 2019-12-24 03:59:20

问题


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.


回答1:


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();
}


来源:https://stackoverflow.com/questions/28970124/cern-root-exporting-data-to-plain-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!