from ROOT import TFile,TH1F import os,sys ### GLOBALS FILENAME = "hd_root.root" DIRECTORY = "." histnames = [ "Independent/Hist_DetectedParticleKinematics/Proton/Momentum", "Independent/Hist_DetectedParticleKinematics/Pi+/Momentum", "Independent/Hist_DetectedParticleKinematics/Pi-/Momentum" ] if __name__ == "__main__": ## set up data structures out_histnames = {} out_hists = {} event_counts = {} for hn in histnames: out_histnames[hn] = hn.replace("/", "-") event_counts[hn] = [] #print hn + " " + out_histnames[hn] ## process the files if len(sys.argv) > 1: DIRECTORY = str(sys.argv[1]) for root, dirs, files in os.walk(DIRECTORY): for f in files: if f == FILENAME: filenamepath = os.path.join(root, f) print "Processing " + filenamepath + "..." try: f = TFile(filenamepath) for hn in histnames: n = f.Get(hn).Integral() event_counts[hn].append(n) #print hn + " " + str(n) f.Close() except: print "Error processing file!" ## histogram number of counts for hn in histnames: out_hists[hn] = TH1F(out_histnames[hn], hn, 100, 0.9*min(event_counts[hn]), 1.1*max(event_counts[hn])) for x in event_counts[hn]: out_hists[hn].Fill(x) ## output the histograms outf = TFile("monitor.root", "recreate") for h in out_hists.values(): h.Write() outf.Close()