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.12 by wulsin, Wed Jun 12 09:15:32 2013 UTC vs.
Revision 1.15 by wulsin, Fri Jun 14 16:08:18 2013 UTC

# Line 1 | Line 1
1   #!/usr/bin/env python
2  
3 < # Must specify options with -l argument, e.g.:  
4 < # > makeBNTreePlot.py -l sampleBNTreePlotConfig.py  
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
# Line 21 | Line 27 | gROOT.SetBatch(True)  # This is to preve
27  
28  
29   def MakeCondorSubmitFile(arguments, dataset):
30 +    outdir = "condor/"+arguments.condorDir+"/"+dataset+"/"  
31      p = subprocess.Popen(["which", "makeBNTreePlot.py"], stdout=subprocess.PIPE)
32 <    executable = p.communicate()[0]  # read the stdout of the command  
32 >    executable = p.communicate()[0]  # read the stdout of the command
33      workdir = os.getcwd() + "/"
34 <    outdir = "condor/"+arguments.condorDir+"/"+dataset+"/"  
34 >
35 >    # Count the number of hist*root files in the specified directory
36 >    p = subprocess.Popen("ls " + workdir + outdir + "hist*root | wc", shell=True, stdout=subprocess.PIPE)  
37 >    out = p.communicate()[0]
38 >    outSplit = out.split()
39 >    totalJobs = int(outSplit[0])  # The first element of the list is the number of files
40 >
41 >    ## print "Debug: outSplit = "
42 >    ## print outSplit
43 >    ## out, err = p.communicate()
44 >    ## print "Debug: Size of out:"
45 >    ## print len(out)
46 >    ## print "out = "
47 >    ## print out
48 >    ## print "err = "
49 >    ## print err
50 >    ## print "Debug:  p.communicate()[0] = " + p.communicate()[0]
51 >    ## totalJobs = len(out)
52      out = open(outdir+"/condorBNTree.sub", "w")
53      out.write("Executable              = " + executable + " \n")
54      out.write("Universe                = vanilla \n")
55      out.write("Getenv                  = True \n")
56 <    out.write("Arguments               = -D " + dataset + " -l " + arguments.localConfig + " -c " + arguments.condorDir + " \n")
56 >    argCondorProcess = ""  
57 >    if arguments.splitCondorJobs:
58 >        argCondorProcess = " -p $(Process) "
59 >    out.write("Arguments               = -D " + dataset + " -l " + arguments.localConfig + " -c " + arguments.condorDir + argCondorProcess + " \n")
60      out.write("Output                  = " + workdir + outdir + "condorBNTree_$(Process).out \n")
61      out.write("Error                   = " + workdir + outdir + "condorBNTree_$(Process).err \n")
62      out.write("Log                     = " + workdir + outdir + "condorBNTree_$(Process).log \n")
63      out.write("+IsLocalJob             = true \n")
64      out.write("Rank                    = TARGET.IsLocalSlot \n")
65 <    out.write("Queue 1 \n")
65 >    if arguments.splitCondorJobs:
66 >        out.write("Queue " + str(totalJobs) + " \n")
67 >    else:
68 >        out.write("Queue 1 \n")
69      out.close()  
70  
71  
# Line 64 | Line 94 | watch1 = TStopwatch()
94  
95   parser = OptionParser()
96   parser = set_commandline_arguments(parser)
97 +
98 + ###  Only used by makeBNTreePlot.py (maybe move to another file?)        
99 + parser.remove_option("-p")
100 + parser.add_option("-D", "--dataset", dest="datasetName",        
101 +                  help="Name of dataset (overrides value from local configuration file)")        
102 + parser.add_option("-C", "--runOnCondor",  action="store_true", dest="runOnCondor", default=False,        
103 +                  help="Run on condor instead of interactively")        
104 + parser.add_option("-S", "--splitCondorJobs", action="store_true", dest="splitCondorJobs", default=False,                
105 +                  help="Split condor jobs to have one for each file, rather than one for each dataset")  
106 + parser.add_option("-p", "--condorProcessNum", dest="condorProcessNum", default=-1,      
107 +                  help="Specify which condor process to run (default is to run over all).")      
108 +
109 +
110   (arguments, args) = parser.parse_args()
111 +
112  
113   if not arguments.localConfig:
114      sys.exit(" You must specify a localOptions.py file with -l")
# Line 97 | Line 141 | if arguments.runOnCondor:
141   #write new histogram to dataset
142   if not arguments.quickMerge:
143      for dataset in split_datasets:
144 <        for hist in input_histograms:
145 <            #chain trees together
146 <            ch = TChain("OSUAnalysis/"+hist['channel']+"/BNTree_"+hist['channel'])
147 <            ch.Add(condor_dir + "/" + dataset + "/hist_*.root")
148 <            print ("Looping over chain with # entries = %f; split time = " % ch.GetEntries()),
149 <            watch1.Stop(); watch1.Print(); watch1.Start()
150 <            
151 <            outputFile = TFile(condor_dir + "/" + dataset + ".root", "UPDATE")
152 <            if not outputFile or outputFile.IsZombie():  print "Could not open file: %s/%s.root" % (condor_dir, dataset)  
153 <            outputFile.cd("OSUAnalysis/"+hist['channel'])
154 <                
155 <            deleteString = hist['histName'] + ";*"  # delete all existing instances of the object
156 <            currentDir = outputFile.GetDirectory("OSUAnalysis/"+hist['channel']);
157 <            if not currentDir: print "Could not find directory OSUAnalysis/%s in file %s" % (hist['channel'], outputFile.GetName())  
158 <            currentDir.Delete(deleteString);
159 <            if 'nbinsY' in hist:  # only make a 2D histogram if the key "nbinsY" is defined  
160 <                h = TH2D(hist['histName'], hist['histName'],
161 <                         hist['nbins'],  hist['xMin'], hist['xMax'],
162 <                         hist['nbinsY'], hist['yMin'], hist['yMax'])
163 <            else:            
164 <                h = TH1D(hist['histName'], hist['histName'], hist['nbins'], hist['xMin'], hist['xMax'])
165 <            h.Sumw2()  # Needed to get weights correct.  
166 <            cut = TCut(hist['cutString'])
167 <            ch.Draw(hist['varToPlot']+">>"+hist['histName'], cut)  
168 <            h.Write()
169 <            outputFile.Close()
170 <            print "Histogram " + hist['histName'] + " has been added to " + condor_dir + "/"+ dataset + ".root"
144 >        if BNTreeUseScript:
145 >            chainName = "OSUAnalysis/" + BNTreeChannel + "/BNTree_" + BNTreeChannel
146 >            command = "root -l -b -q '" + BNTreeScript + "+(\"" + condor_dir + "\",\"" + dataset + "\",\"" + chainName + "\"," + str(arguments.condorProcessNum) + ")'"  
147 >            print "About to execute command:  " + command  
148 >            os.system(command)
149 >        else:
150 >            for hist in input_histograms:
151 >                #chain trees together
152 >                ch = TChain("OSUAnalysis/"+hist['channel']+"/BNTree_"+hist['channel'])
153 >                ch.Add(condor_dir + "/" + dataset + "/hist_*.root")
154 >                print ("Looping over chain with # entries = %f; split time = " % ch.GetEntries()),
155 >                watch1.Stop(); watch1.Print(); watch1.Start()
156 >
157 >                outputFile = TFile(condor_dir + "/" + dataset + ".root", "UPDATE")
158 >                if not outputFile or outputFile.IsZombie():  print "Could not open file: %s/%s.root" % (condor_dir, dataset)  
159 >                outputFile.cd("OSUAnalysis/"+hist['channel'])
160 >
161 >                deleteString = hist['histName'] + ";*"  # delete all existing instances of the object
162 >                currentDir = outputFile.GetDirectory("OSUAnalysis/"+hist['channel']);
163 >                if not currentDir: print "Could not find directory OSUAnalysis/%s in file %s" % (hist['channel'], outputFile.GetName())  
164 >                currentDir.Delete(deleteString);
165 >                if 'nbinsY' in hist:  # only make a 2D histogram if the key "nbinsY" is defined  
166 >                    h = TH2D(hist['histName'], hist['histName'],
167 >                             hist['nbins'],  hist['xMin'], hist['xMax'],
168 >                             hist['nbinsY'], hist['yMin'], hist['yMax'])
169 >                else:            
170 >                    h = TH1D(hist['histName'], hist['histName'], hist['nbins'], hist['xMin'], hist['xMax'])
171 >                h.Sumw2()  # Needed to get weights correct.  
172 >                cut = TCut(hist['cutString'])
173 >                ch.Draw(hist['varToPlot']+">>"+hist['histName'], cut)  
174 >                h.Write()
175 >                outputFile.Close()
176 >                print "Histogram " + hist['histName'] + " has been added to " + condor_dir + "/"+ dataset + ".root"
177  
178   #merge output if composite dataset
179   for composite_dataset in composite_datasets:
# Line 139 | Line 189 | for composite_dataset in composite_datas
189      print "Merging output for composite dataset: " + composite_dataset
190      os.system(command)
191        
192 < print ("Total time: "),
192 > print ("Total time to run makeBNTreePlot.py: "),
193   watch.Stop()
194   watch.Print()
195              

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines