我有一个目录Processed_Data
数以千计的hists*****_blinded.root
文件。每个hists*****_blinded.root
其中包含大约 15 个图表和直方图。我的目标只是重叠 1 个特定的直方图 sc*****
从每个文件得到最终的直方图 finalhists_blinded.root
这将代表所有重叠在一起的对象。
我试过下面的宏:
void final()
{
TCanvas *time = new TCanvas("c1","overlap" ,600,1000);
time ->Divide(1,1);
time ->cd(1);
TH1F *h1 = new TH1F("h1","time" ,4096,0,4096);
ifstream in;
Float_t t;
Int_t nlines= 0;
in.open("Processed_Data", ios::in);
while (1) {
in >> t;
if (!in.good()) break;
h1->Fill(t);
nlines++;
}
in.close();
但最后我得到的是空白 Canvas 。这个想法是运行每个 hists
通过代码文件,逐一添加。
因此,我想看到所有这些 sc*****
直方图重叠,因此每个直方图中的尖峰将在 finalhists_blinded.root
中创建一个模式文件。
最佳答案
不应该那么复杂,试试这个:
void overlap()
{
TCanvas *time = new TCanvas("c1", "overlap", 0, 0, 800, 600);
const char* histoname = "sc";
const int NFiles = 100000;
for (int fileNumber = 0; fileNumber < NFiles; fileNumber++)
{
TFile* myFile = TFile::Open(Form("Processed_Data/hists%i_blinded.root", fileNumber));
if (!myFile)
{
printf("Nope, no such file!\n");
return;
}
TH1* h1 = (TH1*)myFile->Get(histoname);
if (!h1)
{
printf("Nope, no such histogram!\n");
return;
}
h1->SetDirectory(gROOT);
h1->Draw("same");
myFile->Close();
}
}
它遍历所有 Processed_Data/histsXXXXXi_blinded.root
文件(假设它们的名称是 Processed_Data/hists0_blinded.root
、Processed_Data/hists1_blinded.root
, Processed_Data/hists2_blinded.root
, ..., Processed_Data/hists99998_blinded.root
, Processed_Data/hists99999_blinded.root
), 打开它们中的每一个,抓取一维 sc
直方图,将其添加到 Canvas ,关闭文件并移至下一个文件。
关于c++ - 我需要从一个目录中汇总数千个直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56855059/