因此,我尝试并尝试遵循类似问题this one,但没有成功。
这真的很简单-我有一些.root文件,可以看到ROOT中的直方图,但希望将数据导出为.txt或类似文件,以便能够在其他程序中进行分析。
最佳答案
这是工作示例。读取具有三个分支的根文件,分别名为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();
}