--- UserCode/OSUT3Analysis/Configuration/scripts/makeBNTreePlot.py 2013/06/12 09:15:32 1.12 +++ UserCode/OSUT3Analysis/Configuration/scripts/makeBNTreePlot.py 2013/06/14 16:08:18 1.15 @@ -1,7 +1,13 @@ #!/usr/bin/env python -# Must specify options with -l argument, e.g.: -# > makeBNTreePlot.py -l sampleBNTreePlotConfig.py +# Must specify options with -l argument, and condor directory with -c argument, e.g.: +# > makeBNTreePlot.py -l sampleBNTreePlotConfig.py -c myCondorDir + +# Additional arugments specify the running mode: +# -D : Run on a single dataset (typically for testing) +# -C: Submit jobs to run on condor over all datasets +# BNTreeUseScript=True (set in sampleBNTreePlotConfig.py): run BNTreeScript root macro (also set in sampleBNTreePlotConfig.py), +# which must take as arguments the condor directory, dataset, and channel import sys @@ -21,21 +27,45 @@ gROOT.SetBatch(True) # This is to preve def MakeCondorSubmitFile(arguments, dataset): + outdir = "condor/"+arguments.condorDir+"/"+dataset+"/" p = subprocess.Popen(["which", "makeBNTreePlot.py"], stdout=subprocess.PIPE) - executable = p.communicate()[0] # read the stdout of the command + executable = p.communicate()[0] # read the stdout of the command workdir = os.getcwd() + "/" - outdir = "condor/"+arguments.condorDir+"/"+dataset+"/" + + # Count the number of hist*root files in the specified directory + p = subprocess.Popen("ls " + workdir + outdir + "hist*root | wc", shell=True, stdout=subprocess.PIPE) + out = p.communicate()[0] + outSplit = out.split() + totalJobs = int(outSplit[0]) # The first element of the list is the number of files + + ## print "Debug: outSplit = " + ## print outSplit + ## out, err = p.communicate() + ## print "Debug: Size of out:" + ## print len(out) + ## print "out = " + ## print out + ## print "err = " + ## print err + ## print "Debug: p.communicate()[0] = " + p.communicate()[0] + ## totalJobs = len(out) out = open(outdir+"/condorBNTree.sub", "w") out.write("Executable = " + executable + " \n") out.write("Universe = vanilla \n") out.write("Getenv = True \n") - out.write("Arguments = -D " + dataset + " -l " + arguments.localConfig + " -c " + arguments.condorDir + " \n") + argCondorProcess = "" + if arguments.splitCondorJobs: + argCondorProcess = " -p $(Process) " + out.write("Arguments = -D " + dataset + " -l " + arguments.localConfig + " -c " + arguments.condorDir + argCondorProcess + " \n") out.write("Output = " + workdir + outdir + "condorBNTree_$(Process).out \n") out.write("Error = " + workdir + outdir + "condorBNTree_$(Process).err \n") out.write("Log = " + workdir + outdir + "condorBNTree_$(Process).log \n") out.write("+IsLocalJob = true \n") out.write("Rank = TARGET.IsLocalSlot \n") - out.write("Queue 1 \n") + if arguments.splitCondorJobs: + out.write("Queue " + str(totalJobs) + " \n") + else: + out.write("Queue 1 \n") out.close() @@ -64,7 +94,21 @@ watch1 = TStopwatch() parser = OptionParser() parser = set_commandline_arguments(parser) + +### Only used by makeBNTreePlot.py (maybe move to another file?) +parser.remove_option("-p") +parser.add_option("-D", "--dataset", dest="datasetName", + help="Name of dataset (overrides value from local configuration file)") +parser.add_option("-C", "--runOnCondor", action="store_true", dest="runOnCondor", default=False, + help="Run on condor instead of interactively") +parser.add_option("-S", "--splitCondorJobs", action="store_true", dest="splitCondorJobs", default=False, + help="Split condor jobs to have one for each file, rather than one for each dataset") +parser.add_option("-p", "--condorProcessNum", dest="condorProcessNum", default=-1, + help="Specify which condor process to run (default is to run over all).") + + (arguments, args) = parser.parse_args() + if not arguments.localConfig: sys.exit(" You must specify a localOptions.py file with -l") @@ -97,33 +141,39 @@ if arguments.runOnCondor: #write new histogram to dataset if not arguments.quickMerge: for dataset in split_datasets: - for hist in input_histograms: - #chain trees together - ch = TChain("OSUAnalysis/"+hist['channel']+"/BNTree_"+hist['channel']) - ch.Add(condor_dir + "/" + dataset + "/hist_*.root") - print ("Looping over chain with # entries = %f; split time = " % ch.GetEntries()), - watch1.Stop(); watch1.Print(); watch1.Start() - - outputFile = TFile(condor_dir + "/" + dataset + ".root", "UPDATE") - if not outputFile or outputFile.IsZombie(): print "Could not open file: %s/%s.root" % (condor_dir, dataset) - outputFile.cd("OSUAnalysis/"+hist['channel']) - - deleteString = hist['histName'] + ";*" # delete all existing instances of the object - currentDir = outputFile.GetDirectory("OSUAnalysis/"+hist['channel']); - if not currentDir: print "Could not find directory OSUAnalysis/%s in file %s" % (hist['channel'], outputFile.GetName()) - currentDir.Delete(deleteString); - if 'nbinsY' in hist: # only make a 2D histogram if the key "nbinsY" is defined - h = TH2D(hist['histName'], hist['histName'], - hist['nbins'], hist['xMin'], hist['xMax'], - hist['nbinsY'], hist['yMin'], hist['yMax']) - else: - h = TH1D(hist['histName'], hist['histName'], hist['nbins'], hist['xMin'], hist['xMax']) - h.Sumw2() # Needed to get weights correct. - cut = TCut(hist['cutString']) - ch.Draw(hist['varToPlot']+">>"+hist['histName'], cut) - h.Write() - outputFile.Close() - print "Histogram " + hist['histName'] + " has been added to " + condor_dir + "/"+ dataset + ".root" + if BNTreeUseScript: + chainName = "OSUAnalysis/" + BNTreeChannel + "/BNTree_" + BNTreeChannel + command = "root -l -b -q '" + BNTreeScript + "+(\"" + condor_dir + "\",\"" + dataset + "\",\"" + chainName + "\"," + str(arguments.condorProcessNum) + ")'" + print "About to execute command: " + command + os.system(command) + else: + for hist in input_histograms: + #chain trees together + ch = TChain("OSUAnalysis/"+hist['channel']+"/BNTree_"+hist['channel']) + ch.Add(condor_dir + "/" + dataset + "/hist_*.root") + print ("Looping over chain with # entries = %f; split time = " % ch.GetEntries()), + watch1.Stop(); watch1.Print(); watch1.Start() + + outputFile = TFile(condor_dir + "/" + dataset + ".root", "UPDATE") + if not outputFile or outputFile.IsZombie(): print "Could not open file: %s/%s.root" % (condor_dir, dataset) + outputFile.cd("OSUAnalysis/"+hist['channel']) + + deleteString = hist['histName'] + ";*" # delete all existing instances of the object + currentDir = outputFile.GetDirectory("OSUAnalysis/"+hist['channel']); + if not currentDir: print "Could not find directory OSUAnalysis/%s in file %s" % (hist['channel'], outputFile.GetName()) + currentDir.Delete(deleteString); + if 'nbinsY' in hist: # only make a 2D histogram if the key "nbinsY" is defined + h = TH2D(hist['histName'], hist['histName'], + hist['nbins'], hist['xMin'], hist['xMax'], + hist['nbinsY'], hist['yMin'], hist['yMax']) + else: + h = TH1D(hist['histName'], hist['histName'], hist['nbins'], hist['xMin'], hist['xMax']) + h.Sumw2() # Needed to get weights correct. + cut = TCut(hist['cutString']) + ch.Draw(hist['varToPlot']+">>"+hist['histName'], cut) + h.Write() + outputFile.Close() + print "Histogram " + hist['histName'] + " has been added to " + condor_dir + "/"+ dataset + ".root" #merge output if composite dataset for composite_dataset in composite_datasets: @@ -139,7 +189,7 @@ for composite_dataset in composite_datas print "Merging output for composite dataset: " + composite_dataset os.system(command) -print ("Total time: "), +print ("Total time to run makeBNTreePlot.py: "), watch.Stop() watch.Print()