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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines