#include <iostream>
#include <fstream>
using namespace std;
void root2txt(const char *rootfilename,const char *txtfilename)
{
TFile *file = new TFile(rootfilename,"READ");//"RECREATE" "READ"
if(!file->IsOpen())
{
std::cout<<"Can't open root file"<<std::endl;
}
TH1I *hEner_00 = (TH1I*)file->Get("hEner_00");
// write txt file
int count[5000];
for (int i = 1; i <= 4096; ++i)
{
count[i] = int(hEner_00->GetBinContent(i));
}
ofstream myfile;
myfile.open(txtfilename,ios::out);
if(myfile.is_open())
{
cout<<"file open successful"<<endl;
for(int i=1;i<=4096;++i)
{
myfile<<i<<" "<<count[i]<<endl;
}
}
myfile.close();
file->Close();
}
用法:root
.L tran.C
root2txt("rootname","txtname")