#include #include #include "TFile.h" #include "TH1D.h" int CollectHist ( TH1D *hist1d_input, int ncummulative, string name_hist, string path_input) { TH1::AddDirectory(kFALSE); TFile *file_input = new TFile (path_input.data(), "read"); TH1D *hist1d_tmp = (TH1D*)file_input -> Get (name_hist.data()); if (ncummulative<=0) { hist1d_input = (TH1D*)hist1d_tmp -> Clone(); } else { hist1d_input -> Add (hist1d_tmp); } file_input -> Close(); delete hist1d_tmp; delete file_input; printf ( " | |-- Integral before exist: %.2f\n", hist1d_input->Integral() ); return 1; } void example () { TH1::AddDirectory(kFALSE); TH1D *hist; int ncum = 0; string list_file[3] = { "file1.root", "file2.root", "file3.root" }; printf (" [+] Read files\n"); for (int i=0; i<3; i++) { printf (" |-- File number %d\n", i+1); int stt = CollectHist ( hist, ncum, "histname", list_file[i]); printf ( " | |-- Integral after exist: %.2f\n", hist->Integral() ); ncum += stt; } printf (" [+] Accumulated histogram from %d files\n", ncum); TCanvas *canvas = new TCanvas ("canvas", "", 600, 600); canvas -> cd(); hist -> SetLineColor (kOrange); hist -> SetLineWidth (2); hist -> Draw ("hist"); canvas -> SaveAs ("accumulatedhistogram.png"); delete canvas; delete hist; }