ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/OSUT3Analysis/Configuration/scripts/makeBNTreePlot.py
(Generate patch)

Comparing UserCode/OSUT3Analysis/Configuration/scripts/makeBNTreePlot.py (file contents):
Revision 1.1 by jbrinson, Fri May 31 14:19:23 2013 UTC vs.
Revision 1.14 by wulsin, Thu Jun 13 09:35:44 2013 UTC

# Line 1 | Line 1
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 TCut, TFile, TH1D, TTree, TStopwatch, TChain
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  
14 watch = TStopwatch()
70  
71   parser = OptionParser()
72   parser = set_commandline_arguments(parser)
73 +
74 + ###  Only used by makeBNTreePlot.py (maybe move to another file?)        
75 + parser.add_option("-D", "--dataset", dest="datasetName",        
76 +                  help="Name of dataset (overrides value from local configuration file)")        
77 + parser.add_option("-C", "--runOnCondor",  action="store_true", dest="runOnCondor", default=False,        
78 +                  help="Run on condor instead of interactively")        
79 +
80   (arguments, args) = parser.parse_args()
81 +
82  
83   if not arguments.localConfig:
84      sys.exit(" You must specify a localOptions.py file with -l")
85 < if arguments.localConfig:
85 > if arguments.localConfig:  
86      sys.path.append(os.getcwd())
87      exec("from " + arguments.localConfig.rstrip('.py') + " import *")
88   if not arguments.condorDir:
# Line 27 | Line 90 | if not arguments.condorDir:
90   if arguments.condorDir:
91      condor_dir = "condor/%s" % arguments.condorDir
92  
93 < for dataset in datasets:
94 <    for hist in input_histograms:
95 <        ch = TChain("OSUAnalysis/"+hist['channel']+"/BNTree_"+hist['channel'])
96 <        ch.Add(condor_dir + "/" + dataset + "/hist_*.root")
97 <        inputFile = TFile(condor_dir + "/" + dataset + ".root", "UPDATE")
98 <        h = TH1D(hist['histName'], hist['histName'], hist['nbins'], hist['xMin'], hist['xMax'])
99 <        
100 <        cut = TCut(hist['cutString'])
101 <
102 <        ch.Draw(hist['varToPlot']+">>"+hist['histName'], cut)
103 <
104 <        inputFile.cd("OSUAnalysis/"+hist['channel'])
105 <        h.Write()
106 <        inputFile.Close()
107 <        print "Histogram " + hist['histName'] + " has been added to " + condor_dir + "/"+ dataset + ".root"
108 <    
93 > if arguments.datasetName:  # If datasetName is specified on command line, then override the value from localConfig  
94 >    datasets = [
95 >        arguments.datasetName,
96 >    ]
97 >
98 > #save a list of composite datasets
99 > composite_datasets = get_composite_datasets(datasets, composite_dataset_definitions)
100 > #save a list of datasets with composite datasets split up
101 > split_datasets = split_composite_datasets(datasets, composite_dataset_definitions)
102 >
103 > if arguments.quickMerge and arguments.runOnCondor:
104 >    print "Cannot use -q (--quickMerge) and -C (--runOnCondor) options simultaneously.  Please choose one or the other."
105 >    exit()  
106 >
107 > if arguments.runOnCondor:
108 >    RunOnCondor(arguments, split_datasets)
109 >    exit()    
110 >
111 > #write new histogram to dataset
112 > if not arguments.quickMerge:
113 >    for dataset in split_datasets:
114 >        if BNTreeUseScript:
115 >            chainName = "OSUAnalysis/" + BNTreeChannel + "/BNTree_" + BNTreeChannel
116 >            command = "root -l -b -q '" + BNTreeScript + "+(\"" + condor_dir + "\",\"" + dataset + "\",\"" + chainName + "\")'"  
117 >            print "About to execute command:  " + command  
118 >            os.system(command)
119 >        else:
120 >            for hist in input_histograms:
121 >                #chain trees together
122 >                ch = TChain("OSUAnalysis/"+hist['channel']+"/BNTree_"+hist['channel'])
123 >                ch.Add(condor_dir + "/" + dataset + "/hist_*.root")
124 >                print ("Looping over chain with # entries = %f; split time = " % ch.GetEntries()),
125 >                watch1.Stop(); watch1.Print(); watch1.Start()
126 >
127 >                outputFile = TFile(condor_dir + "/" + dataset + ".root", "UPDATE")
128 >                if not outputFile or outputFile.IsZombie():  print "Could not open file: %s/%s.root" % (condor_dir, dataset)  
129 >                outputFile.cd("OSUAnalysis/"+hist['channel'])
130 >
131 >                deleteString = hist['histName'] + ";*"  # delete all existing instances of the object
132 >                currentDir = outputFile.GetDirectory("OSUAnalysis/"+hist['channel']);
133 >                if not currentDir: print "Could not find directory OSUAnalysis/%s in file %s" % (hist['channel'], outputFile.GetName())  
134 >                currentDir.Delete(deleteString);
135 >                if 'nbinsY' in hist:  # only make a 2D histogram if the key "nbinsY" is defined  
136 >                    h = TH2D(hist['histName'], hist['histName'],
137 >                             hist['nbins'],  hist['xMin'], hist['xMax'],
138 >                             hist['nbinsY'], hist['yMin'], hist['yMax'])
139 >                else:            
140 >                    h = TH1D(hist['histName'], hist['histName'], hist['nbins'], hist['xMin'], hist['xMax'])
141 >                h.Sumw2()  # Needed to get weights correct.  
142 >                cut = TCut(hist['cutString'])
143 >                ch.Draw(hist['varToPlot']+">>"+hist['histName'], cut)  
144 >                h.Write()
145 >                outputFile.Close()
146 >                print "Histogram " + hist['histName'] + " has been added to " + condor_dir + "/"+ dataset + ".root"
147 >
148 > #merge output if composite dataset
149 > for composite_dataset in composite_datasets:
150 >    component_datasets_list = ""
151 >    component_dataset_file_path = ""
152 >    for component_dataset in composite_dataset_definitions[composite_dataset]:
153 >        component_dataset_dir = "%s/%s" % (condor_dir,component_dataset)
154 >        component_dataset_file_path = component_dataset_dir + ".root"
155 >        if os.path.isfile(component_dataset_file_path):
156 >            component_datasets_list += " " + component_dataset_file_path
157 >    composite_dataset_dir = "%s/%s" % (condor_dir,composite_dataset)
158 >    command = "mergeHists -p %s %s" % (composite_dataset_dir, component_datasets_list)
159 >    print "Merging output for composite dataset: " + composite_dataset
160 >    os.system(command)
161 >      
162 > print ("Total time: "),
163   watch.Stop()
164   watch.Print()
165 <
165 >            
166  
167  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines