CERN ROOT将数据导出为纯文本 [英] CERN ROOT exporting data to plain text

查看:1751
本文介绍了CERN ROOT将数据导出为纯文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我尝试并试图按照类似的问题提出问题,例如这一个,但没有成功。

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

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

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.

推荐答案

这里是工作示例。在根文件中读取三个分支,名为TS,ns和nserr。

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

这篇关于CERN ROOT将数据导出为纯文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆