I need to sum up thousands of histograms from one directory

╄→尐↘猪︶ㄣ 提交于 2019-12-24 19:10:55

问题


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.

I have tried the following macro:

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

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.

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.


回答1:


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

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.



来源:https://stackoverflow.com/questions/56855059/i-need-to-sum-up-thousands-of-histograms-from-one-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!