CERN ROOT将数据导出为纯文本

时间:2015-03-10 17:20:31

标签: export analysis root-framework

所以我尝试过并尝试像this one那样提出类似的问题,但没有成功。

这很简单 - 我有一些.root文件,可以看到ROOT中的直方图,但想要将数据导出为.txt或类似的,以便能够在其他程序中进行分析。

1 个答案:

答案 0 :(得分:4)

这是一个有效的例子。读取具有三个分支的根文件,名为TS,ns和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();
}