-
Notifications
You must be signed in to change notification settings - Fork 1
/
runTreeChecker.py
56 lines (50 loc) · 2.05 KB
/
runTreeChecker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# Macro to check a tree in the WZgamma ntuples
# This macro takes two arguments
# The first argument is the input file, the tree that will be checked is the "ntuplizer/tree" tree
# The second argument is the output file
# Example:
# python runTreeChecker.py myWZgammaNtuple.root myOutputFile.root
# John Hakala 1/15/2016
from ROOT import *
import os, subprocess
from sys import argv
# function to compile a C/C++ macro for loading into a pyroot session
if len(argv) != 4:
print "please supply three arguments to the macro: the input ntuple, the output filename, and either 'load' or 'compile'."
exit(1)
elif not (argv[3]=="load" or argv[3]=="compile"):
print "for the third argument, please pick 'load' or 'compile'."
else:
print "\nInput file is %s\n" % argv[1]
print "\nAttempting to %s treeChecker.\n" % argv[3]
pastTense = "loaded" if argv[3]=="load" else "compiled"
def deleteLibs(macroName):
# remove the previously compiled libraries
if os.path.exists(macroName+"_C_ACLiC_dict_rdict.pcm"):
os.remove(macroName+"_C_ACLiC_dict_rdict.pcm")
if os.path.exists(macroName+"_C.d"):
os.remove(macroName+"_C.d")
if os.path.exists(macroName+"_C.so"):
os.remove(macroName+"_C.so")
# compile the macro using g++
# call the compiling function to compile the treeChecker, then run its Loop() method
if argv[3]=="compile":
deleteLibs("treeChecker")
exitCode = gSystem.CompileMacro("treeChecker.C", "gOck")
success=(exitCode==1)
elif argv[3]=="load":
exitCode = gSystem.Load('treeChecker_C')
success=(exitCode>=-1)
if not success:
print "\nError... treeChecker failed to %s. :-("%argv[3]
print "Make sure you're using an up-to-date version of ROOT by running cmsenv in a 7_4_X>=16 CMSSW area."
exit(1)
else:
print "\ntreeChecker %s successfully."%pastTense
if argv[3]=="compile":
gSystem.Load('treeChecker_C')
file = TFile(argv[1])
# get the ntuplizer/tree tree from the file specified by argument 1
tree = file.Get("ntuplizer/tree")
checker = treeChecker(tree)
checker.Loop(argv[2])