如何从包含两列数据的大文件创建 ROOT 直方图?我只想从一列数据创建直方图

How to create a ROOT Histogram from a large file containing two columns of data? I only want to create a histogram from one column of data

这是我的代码。如果有一种方法可以在不显着更改代码的情况下制作直方图,请告诉我。另外,让我知道更简单的方法。谢谢。

{
TFile *f = new TFile("Data.root", "RECREATE");
TNtuple *t = new TNtuple("current_data", "Data from HV", "Unix:Current");
t->ReadFile("NP02_HVCurrent_10-09-2019_11-09-2019");
t->Write();

TH1F *h = new TH1F("Current_Hist", "Current Vs. Events", 100, -5, 5);
h->Fill("Current");
h->Draw();
}

您的代码离我们不远了。就像@PaulMcKenzie 写的那样,不需要进行动态分配(尽管它们在根使用代码中很常见,并且可以防止事情超出范围,而它们仍然隐含地需要......)

TFilet->Write() 的创建对于创建和绘制直方图来说似乎是多余的。

为了简单地绘制 TTree 的分支(TNtuple 从中继承),通常不需要创建直方图的样板。 TTree::Draw 非常通用,有多种选择:

{
TNtuple t("current_data", "Data from HV", "Unix:Current");
t.ReadFile("NP02_HVCurrent_10-09-2019_11-09-2019");

// just draw
t.Draw("Current");

// draw with custom binning
t.Draw("Current>>(100,-5,5)");

// use a manually created histogram
TH1F h("Current_Hist", "Current Vs. Events", 100, -5, 5);
t.Draw("current>>Current_Hist");
}

任何 Draw 也应该绘制到当前 canvas 或根据需要创建一个。

(对于 TTree::Draw 不能做的复杂事情,我建议在运行代码生成的 t 上调用 MakeClass。在生成的实现文件中应该有一个遍历已经实现的所有元素,您可以在循环外和循环内添加直方图创建 h.Fill(Current)。注意:Fill 不采用字符串,它采用需要作为变量的浮点数保存当前树元素的值)。