我需要汇总一个目录中的数千个直方图 [英] I need to sum up thousands of histograms from one directory

查看:108
本文介绍了我需要汇总一个目录中的数千个直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含成千上万个hists*****_blinded.root文件的目录Processed_Data.每个hists*****_blinded.root包含约15个图形和直方图.我的目标是从每个文件中重叠1个特定的直方图sc*****以获得最终的直方图finalhists_blinded.root,该直方图finalhists_blinded.root代表所有重叠在一起的所有直方图.

I have a directory Processed_Data with thousands of hists*****_blinded.root files. Each hists*****_blinded.root contains around 15 graphs and histograms in it. My goal is just to overlap 1 specific histogram sc***** from each file to get the final histogram finalhists_blinded.root which will represent all of those overlapped together.

我尝试了以下宏:

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

但是我最后得到的是空白画布.想法是通过代码运行每个hists文件,然后将每个文件逐个添加.

But I get the blank canvas at the end. The idea is to run each hists file through the code and add each one by one.

结果,我希望看到所有这些sc*****直方图重叠,以便它们中的每个峰值都会在finalhists_blinded.root文件中创建模式.

As a result, I want to see all those sc***** histograms overlapping so that the spikes in each of them will create a pattern in a finalhists_blinded.root file.

推荐答案

没有那么复杂,请尝试以下操作:

Shouldn't be that complicated, try this:

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.rootProcessed_Data/hists1_blinded.rootProcessed_Data/hists2_blinded.root,...,Processed_Data/hists99998_blinded.rootProcessed_Data/hists99999_blinded.root),打开每个文件,抓取一维sc直方图,将其添加到画布上,关闭文件并移至下一个文件.

It loops over all Processed_Data/histsXXXXXi_blinded.root files (given their names are 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), opens each of them, grabs a 1D sc histogram, adds it to the canvas, closes the file and moves to the next file.

这篇关于我需要汇总一个目录中的数千个直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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