ATLAS Offline Software
Loading...
Searching...
No Matches
DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py
Go to the documentation of this file.
1#!/usr/bin/env python
2
3# Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
4
5#----------------------------------------------------------------------
6#stand-alone script to merge specific directories of NTUP_PHYSVAL files
7#author: philipp.mogg@cern.ch
8#16 May 2016
9#----------------------------------------------------------------------
10
11import os,glob,argparse,ROOT,time
12
13start = time.clock()
14
15parser = argparse.ArgumentParser(description='Merge specific folder(s) in root files.')
16parser.add_argument("-i", "--input", help="path to the folder holding the samples (default: ./)", default=os.getcwd())
17parser.add_argument("-o", "--output", help="path for the output (default: ./merge.root", default=os.getcwd()+"/merge.root")
18parser.add_argument("-d", "--dir", nargs='+', help="ROOT directory to be merged, multiple arguments are possible (default: Summary)", default=["Summary"])
19parser.add_argument("-p", "--pattern", help='pattern of files to merge (default: "*PHYSVAL*")', default="*PHYSVAL*")
20args = parser.parse_args()
21folder = os.path.abspath(args.input)
22mergeDirs = args.dir
23origDir = os.getcwd()
24out = args.output
25pattern = args.pattern
26output_file = os.path.abspath(args.output)
27os.chdir(folder)
28
29files = glob.glob(folder + "/" + pattern)
30
31f = ROOT.TFile(output_file, "recreate")
32folder = os.getcwd()
33f2 = ROOT.TFile(files[1])
34
35print("Target file: " + output_file)
36for infile in files:
37 print("Found input file: " + infile)
38 if os.path.samefile(output_file, infile):
39 print("Please make sure that the output file is not part of the input files! Stopping.")
40 quit()
41
42errors = []
43
44def mergeFolder(path) :
45 print("Merging folder " + path)
46 d = f2.Get(path)
47 if not d:
48 error = "ERROR: Cannot find directory " + path + ". Omitting."
49 print(error)
50 errors.append(error)
51 return
52 dirlist = d.GetListOfKeys()
53 for subdir in dirlist:
54 obj = subdir.ReadObj()
55 if obj.IsA().InheritsFrom(ROOT.TH1.Class()):
56 print("Now merging "+obj.GetName())
57 h1 = obj
58 hpath = d.GetPath()
59 hname = hpath[hpath.find(":")+2:]+"/"+obj.GetName()
60 print("Path: "+hname)
61 for tup in files:
62 if tup==files[1]: continue
63 nextfile = ROOT.TFile(tup)
64 h2 = nextfile.Get(hname)
65 if not h2:
66 error = "ERROR: Cannot find " + hname + " in file " + tup + ". Omitting."
67 print(error)
68 errors.append(error)
69 continue
70 h1.Add(h2)
71 subfolder = f.Get(hpath[hpath.find(":")+2:])
72 subfolder.cd()
73 h1.Write()
74 if obj.IsA().InheritsFrom(ROOT.TDirectory.Class()):
75 print("Found subdirectory "+obj.GetName())
76 hpath = obj.GetPath()
77 subfolder = f.mkdir(hpath[hpath.find(":")+2:],obj.GetTitle())
78 subfolder.cd()
79 mergeFolder(hpath[hpath.find(":")+2:])
80
81for mergeDir in mergeDirs:
82 newfolder = f.mkdir(mergeDir,mergeDir)
83 ROOT.TH1.AddDirectory(False)
84 mergeFolder(mergeDir)
85
86f.Close()
87if len(errors)>0:
88 print("Summary of all errors:")
89 for phrase in errors:
90 print(phrase)
91
92end = time.clock()
93print("Wall time used: %s sec" % (end - start))
void print(char *figname, TCanvas *c1)