ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/OSUT3Analysis/Configuration/scripts/makeBNTreePlot.py
Revision: 1.13
Committed: Thu Jun 13 08:34:22 2013 UTC (11 years, 10 months ago) by wulsin
Content type: text/x-python
Branch: MAIN
Changes since 1.12: +41 -29 lines
Log Message:
Add BNTreeUseScript option to run root script

File Contents

# Content
1 #!/usr/bin/env python
2
3 # Must specify options with -l argument, and condor directory with -c argument, e.g.:
4 # > makeBNTreePlot.py -l sampleBNTreePlotConfig.py -c myCondorDir
5
6 # Additional arugments specify the running mode:
7 # -D <dataset>: Run on a single dataset (typically for testing)
8 # -C: Submit jobs to run on condor over all datasets
9 # BNTreeUseScript=True (set in sampleBNTreePlotConfig.py): run BNTreeScript root macro (also set in sampleBNTreePlotConfig.py),
10 # which must take as arguments the condor directory, dataset, and channel
11
12
13 import sys
14 import os
15 import re
16 from optparse import OptionParser
17 from array import *
18 from decimal import *
19 import subprocess
20
21 from OSUT3Analysis.Configuration.configurationOptions import *
22 from OSUT3Analysis.Configuration.processingUtilities import *
23
24 from ROOT import TChain, TCut, TDirectory, TFile, TH1D, TH2D, TStopwatch, TTree, gROOT
25
26 gROOT.SetBatch(True) # This is to prevent pop-up graphical windows
27
28
29 def MakeCondorSubmitFile(arguments, dataset):
30 p = subprocess.Popen(["which", "makeBNTreePlot.py"], stdout=subprocess.PIPE)
31 executable = p.communicate()[0] # read the stdout of the command
32 workdir = os.getcwd() + "/"
33 outdir = "condor/"+arguments.condorDir+"/"+dataset+"/"
34 out = open(outdir+"/condorBNTree.sub", "w")
35 out.write("Executable = " + executable + " \n")
36 out.write("Universe = vanilla \n")
37 out.write("Getenv = True \n")
38 out.write("Arguments = -D " + dataset + " -l " + arguments.localConfig + " -c " + arguments.condorDir + " \n")
39 out.write("Output = " + workdir + outdir + "condorBNTree_$(Process).out \n")
40 out.write("Error = " + workdir + outdir + "condorBNTree_$(Process).err \n")
41 out.write("Log = " + workdir + outdir + "condorBNTree_$(Process).log \n")
42 out.write("+IsLocalJob = true \n")
43 out.write("Rank = TARGET.IsLocalSlot \n")
44 out.write("Queue 1 \n")
45 out.close()
46
47
48 def RunOnCondor(arguments, split_datasets):
49 print "Running jobs on condor, instead of interactively."
50 ## print "Found condorDir = %s" % (condorDir)
51 ## print "Found split_datasets = "
52 ## print split_datasets
53
54 for dataset in split_datasets:
55 MakeCondorSubmitFile(arguments, dataset)
56 cmd = "condor_submit condor/"+arguments.condorDir+"/"+dataset+"/condorBNTree.sub"
57 os.system(cmd)
58 print "Submitting job: %s " % cmd
59
60 print "Once condor jobs have finished, merge the composite datasets and then make plots with:"
61 print " makeBNTreePlot.py -q -l " + arguments.localConfig + " -c " + arguments.condorDir
62 print " makePlots.py -l " + arguments.localConfig + " -c " + arguments.condorDir
63
64 return
65
66
67 watch = TStopwatch()
68 watch1 = TStopwatch()
69
70
71 parser = OptionParser()
72 parser = set_commandline_arguments(parser)
73 (arguments, args) = parser.parse_args()
74
75 if not arguments.localConfig:
76 sys.exit(" You must specify a localOptions.py file with -l")
77 if arguments.localConfig:
78 sys.path.append(os.getcwd())
79 exec("from " + arguments.localConfig.rstrip('.py') + " import *")
80 if not arguments.condorDir:
81 sys.exit(" You must specify a condor directory with -c")
82 if arguments.condorDir:
83 condor_dir = "condor/%s" % arguments.condorDir
84
85 if arguments.datasetName: # If datasetName is specified on command line, then override the value from localConfig
86 datasets = [
87 arguments.datasetName,
88 ]
89
90 #save a list of composite datasets
91 composite_datasets = get_composite_datasets(datasets, composite_dataset_definitions)
92 #save a list of datasets with composite datasets split up
93 split_datasets = split_composite_datasets(datasets, composite_dataset_definitions)
94
95 if arguments.quickMerge and arguments.runOnCondor:
96 print "Cannot use -q (--quickMerge) and -C (--runOnCondor) options simultaneously. Please choose one or the other."
97 exit()
98
99 if arguments.runOnCondor:
100 RunOnCondor(arguments, split_datasets)
101 exit()
102
103 #write new histogram to dataset
104 if not arguments.quickMerge:
105 for dataset in split_datasets:
106 if BNTreeUseScript:
107 chainName = "OSUAnalysis/" + BNTreeChannel + "/BNTree_" + BNTreeChannel
108 command = "root -l -b -q '" + BNTreeScript + "+(\"" + condor_dir + "\",\"" + dataset + "\",\"" + chainName + "\")'"
109 print "About to execute command: " + command
110 os.system(command)
111 else:
112 for hist in input_histograms:
113 #chain trees together
114 ch = TChain("OSUAnalysis/"+hist['channel']+"/BNTree_"+hist['channel'])
115 ch.Add(condor_dir + "/" + dataset + "/hist_*.root")
116 print ("Looping over chain with # entries = %f; split time = " % ch.GetEntries()),
117 watch1.Stop(); watch1.Print(); watch1.Start()
118
119 outputFile = TFile(condor_dir + "/" + dataset + ".root", "UPDATE")
120 if not outputFile or outputFile.IsZombie(): print "Could not open file: %s/%s.root" % (condor_dir, dataset)
121 outputFile.cd("OSUAnalysis/"+hist['channel'])
122
123 deleteString = hist['histName'] + ";*" # delete all existing instances of the object
124 currentDir = outputFile.GetDirectory("OSUAnalysis/"+hist['channel']);
125 if not currentDir: print "Could not find directory OSUAnalysis/%s in file %s" % (hist['channel'], outputFile.GetName())
126 currentDir.Delete(deleteString);
127 if 'nbinsY' in hist: # only make a 2D histogram if the key "nbinsY" is defined
128 h = TH2D(hist['histName'], hist['histName'],
129 hist['nbins'], hist['xMin'], hist['xMax'],
130 hist['nbinsY'], hist['yMin'], hist['yMax'])
131 else:
132 h = TH1D(hist['histName'], hist['histName'], hist['nbins'], hist['xMin'], hist['xMax'])
133 h.Sumw2() # Needed to get weights correct.
134 cut = TCut(hist['cutString'])
135 ch.Draw(hist['varToPlot']+">>"+hist['histName'], cut)
136 h.Write()
137 outputFile.Close()
138 print "Histogram " + hist['histName'] + " has been added to " + condor_dir + "/"+ dataset + ".root"
139
140 #merge output if composite dataset
141 for composite_dataset in composite_datasets:
142 component_datasets_list = ""
143 component_dataset_file_path = ""
144 for component_dataset in composite_dataset_definitions[composite_dataset]:
145 component_dataset_dir = "%s/%s" % (condor_dir,component_dataset)
146 component_dataset_file_path = component_dataset_dir + ".root"
147 if os.path.isfile(component_dataset_file_path):
148 component_datasets_list += " " + component_dataset_file_path
149 composite_dataset_dir = "%s/%s" % (condor_dir,composite_dataset)
150 command = "mergeHists -p %s %s" % (composite_dataset_dir, component_datasets_list)
151 print "Merging output for composite dataset: " + composite_dataset
152 os.system(command)
153
154 print ("Total time: "),
155 watch.Stop()
156 watch.Print()
157
158
159