ROOT-Tree实用示例

@[TOC](ROOT-Tree示例 (详细) )

注:

  • 参考资源部分来源于ROOT官网
  • 欢迎交流,有误请留言
  • 如有侵权请联系删除

写入.root数据

新建文件Newtree.C

#include <iostream> 
#include "TFile.h"
#include "TH1.h"
#include "TRandom.h"
#include "TTree.h"

//----------------------------------------------------------------------------------------------- 
int main(int argc, char **argv){
//int Newtree(){
	// RECREATE-overwrite;NEW-new file;OLD-read only
	TFile *hfile = TFile::Open("htree.root","RECREATE","ROOT file with histograms & trees");
	// Create some histograms and a profile histogram
	TH1F *hpx = new TH1F("hpx","This is the px distribution",100,-4,4);
	// Define some simple structures
	typedef struct {Float_t x,y,z;} POINT;
	typedef struct {
		Int_t ntrack;
		UInt_t flag;
  		Float_t temperature;
	} EVENTN;
	POINT point;
	EVENTN eventn;
	//An array of floats
	Float_t E[5];
    	                                                                                            
	// Create a ROOT Tree
	TTree *tree = new TTree("T","An example of ROOT tree with a few branches");
	// Structures
	tree->Branch("point",&point,"x:y:z");
	tree->Branch("eventn",&eventn,"ntrack/I:flag/i:temperature/F");
	tree->Branch("hpx","TH1F",&hpx,128000,0);
	tree->Branch("E",E,"E[5]/F");                                                                                                    
	Float_t px,py,pz;                          

	for ( Int_t i=0; i<1000; i++) {
		//generate a pair of Gaussian random numbers with mu=0 and sigma=1
		//gRandom-global static varible
		gRandom->Rannor(px,py);
    		pz = px*px + py*py;
		//generate a uniform random numbers between 0 and 1
    		const Double_t random = gRandom->Rndm(1); 

		// Fill histograms
		hpx->Fill(px);

		// Fill structures
    		point.x = 2*random;
    		point.y = 5*random-10;
    		point.z = 20*random;
    		eventn.ntrack  = Int_t(100*random);
   		eventn.flag    = Int_t(random+0.5);
    		eventn.temperature = 20+random;

		for(Int_t j=0;j<5;j++)	E[j]=random*j;
		//
		// Fill the tree. For each event, save the 2 structures, 1 objects and 1 array
		tree->Fill();
	}

	//Print the Tree Structure with TTree::Print 
   	tree->Print();
	//Browser the content
	hfile->ls(); 

	// Save all objects in this file
	// save only the new version of the tree
   	hfile->Write("", TObject::kOverwrite);
    	// Close the file.
	hfile->Close();
	return 0;
}

运行及结果

#使用g++编译
g++ Newtree.C `root-config --libs` `root-config --cflags` -o Newtree
#运行可执行文件
./Newtree

显示如下
该文件包含一个tree, 两个struct,一个直方图,一个数组,分别对应了其名称

******************************************************************************
*Tree    :T         : An example of ROOT tree with a few branches            *
*Entries :     1000 : Total =          991751 bytes  File  Size =      62265 *
*        :          : Tree compression factor =  14.23                       *
******************************************************************************
*Br    0 :point     : x:y:z
*Entries :     1000 : Total  Size=      12750 bytes  One basket in memory    *
*Baskets :        0 : Basket Size=      32000 bytes  Compression=   1.00     *
*............................................................................*
*Br    1 :eventn    : ntrack/I:flag/i:temperature/F                          *
*Entries :     1000 : Total  Size=      12820 bytes  One basket in memory    *
*Baskets :        0 : Basket Size=      32000 bytes  Compression=   1.00     *
*............................................................................*
*Br    2 :hpx       : TH1F                                                   *
*Entries :     1000 : Total  Size=     945177 bytes  File Size  =      62265 *
*Baskets :        7 : Basket Size=     128000 bytes  Compression=  14.23     *
*............................................................................*
*Br    3 :E         : E[5]/F                                                 *
*Entries :     1000 : Total  Size=      20610 bytes  One basket in memory    *
*Baskets :        0 : Basket Size=      32000 bytes  Compression=   1.00     *
*............................................................................*
TFile**		htree.root	ROOT file with histograms & trees
 TFile*		htree.root	ROOT file with histograms & trees
  OBJ: TH1F	hpx	This is the px distribution : 0 at: 0x1764e90
  OBJ: TTree	T	An example of ROOT tree with a few branches : 0 at: 0x1765500

显示数据

root[] TFile f("htree.root")
root[] T->Show(11)  //显示第11个event各个数据
======> EVENT:11
 x               = 1.27426
 y               = -6.81434
 z               = 12.7426
 ntrack          = 63
 flag            = 1
 temperature     = 20.6371
 hpx             = (TH1F*)0x25c6930
 E               = 0, 
                  0.637131, 1.27426, 1.91139, 2.54852
root[] T->Scan("E[2]:E[1]")  //显示其中几个变量的所有值
************************************
*    Row   *      E[2] *      E[1] *
************************************
*        0 * 0.5652356 * 0.2826178 *
*        1 * 0.9699472 * 0.4849736 *
*        2 * 1.0800873 * 0.5400436 *
*        3 * 1.3172732 * 0.6586366 *
*        4 * 1.0393441 * 0.5196720 *
*        5 * 0.7846279 * 0.3923139 *
*        6 * 0.0606704 * 0.0303352 *
*        7 * 1.8874335 * 0.9437167 *
*        8 * 1.3311278 * 0.6655639 *
*        9 * 0.3645692 * 0.1822846 *
*       10 * 0.1258353 * 0.0629176 *
*       11 * 1.2742623 * 0.6371311 *
*       12 * 1.3985344 * 0.6992672 *
*       13 * 1.0048060 * 0.5024030 *
*       14 * 0.1663495 * 0.0831747 *
*       15 * 0.1646392 * 0.0823196 *
*       16 * 0.4542356 * 0.2271178 *
*       17 * 0.8007842 * 0.4003921 *
*       18 * 0.8816417 * 0.4408208 *
*       19 * 1.0123621 * 0.5061810 *
*       20 * 1.4815764 * 0.7407882 *
*       21 * 0.9202135 * 0.4601067 *
*       22 * 1.3963930 * 0.6981965 *
*       23 * 0.6774570 * 0.3387285 *
*       24 * 0.9493467 * 0.4746733 *
Type <CR> to continue or q to quit ==>

使用browser打开,可显示完整结构

root[] new TBrowser()

在这里插入图片描述

画图

root[] T->Draw("point.x")	//一维直方图

在这里插入图片描述

root[] T->Draw("point.x:point.y>>(100,-10,-5,100,0,2)","","colz") //二维直方图(热力图形式),()内为变量区间及bin数,第二个""可以设置第三个变量范围,"colz"为热力图形式

在这里插入图片描述

读取.root数据

新建Rtree.C

typedef struct {
        Int_t tr;
        UInt_t fl;
        Float_t te;
} event;


void Rtree(){
        event e;

        TFile *f =new TFile("htree.root");
        TTree *mytree =(TTree*)f->Get("T");

        //Gets three leaf for a branch
        mytree->SetBranchAddress("eventn",&e.tr);
        Long64_t n =mytree->GetEntries();
        cout<<"n="<<n<<endl;
        for(Long64_t i=0;i<n;i++){
                //read a event
                mytree->GetEntry(i);
                if(i<10)
                        cout<<e.tr<<","<<e.fl<<","<<std::setprecision(4)<<e.te<<endl;
                /*
                processing data
                */

        }

}
#运行
root -l Rtree.C
root [] 
Processing Rtree.C...
n=1000
28,0,20.28
48,0,20.48
54,1,20.54
65,1,20.66
51,1,20.52
39,0,20.39
3,0,20.03
94,1,20.94
66,1,20.67
18,0,20.18

参考及推荐资料
https://root.cern/doc/master/classTTree.html

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值