ROOT里往tree里添加branch

#include "TFile.h"
#include "TTree.h"
#include "TString.h"
#include "TROOT.h"
#include "TBenchmark.h"
#include "TH2.h"
#include "TH1.h"
#include "TRandom.h"
#include "TStyle.h"
#include "TSystem.h"
#include <fstream>
#include <iostream>

void treeAddBranch()
 {
  TFile *file=new TFile("data_R0193.root", "update"); 
  UShort_t evte;
  double evter;
  TBranch *b_evte;
  TBranch *newBranch;
  TRandom *ran=new TRandom(0);
 TTree *tree = (TTree*)file->Get("tree");
 newBranch = tree->Branch("evter",&evter,"evter/D");
 tree->SetBranchAddress("evte",&evte,&b_evte);
 
  Long64_t nentries = tree->GetEntries(); // read the number of entries in the t3 
  for (Long64_t i = 0; i < nentries; i++) {  
    
      file->cd();
      tree->GetEvent(i);
      evter=evte+ran->Rndm();
      // std::cout<<evte<<"  "<<evter<<std::endl;
  newBranch->Fill();
  }
  //tree->Write();
  tree->Write("", TObject::kOverwrite); // save only the new version of the tree
  }

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CERN ROOT是一个开源的软件框架,主要用于高能物理数据分析。可以使用CERN ROOT提供的TTree类将文本文件(如.txt)转换为ROOT文件(.root)。 下面是一个简单的例子,演示如何使用TTree类将.txt文件转换为.root文件: ```c++ #include <iostream> #include "TFile.h" #include "TTree.h" using namespace std; int main() { // 打开输入文件 ifstream infile("input.txt"); // 创建输出文件 TFile *outfile = new TFile("output.root", "RECREATE"); // 创建TTree对象 TTree *tree = new TTree("tree", "My Tree"); // 声明变量 int x; double y; // 将变量与TTreebranch关联 tree->Branch("x", &x, "x/I"); tree->Branch("y", &y, "y/D"); // 读取输入文件,将数据写入TTree while (infile >> x >> y) { tree->Fill(); } // 写入TTree到输出文件 outfile->Write(); // 关闭文件 infile.close(); outfile->Close(); return 0; } ``` 在上面的例子中,我们打开了一个名为input.txt的文本文件,然后创建了一个名为output.rootROOT文件。我们创建了一个TTree对象,并声明了两个变量x和y,它们分别对应文本文件中的两列数据。我们将这些变量与TTreebranch关联,然后读取输入文件并将数据写入TTree。最后,我们将TTree写入输出文件,关闭文件并结束程序。 当然,这只是一个简单的例子,实际应用中可能需要更复杂的数据结构和转换操作。CERN ROOT提供了丰富的类和工具,可以满足不同的需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值