--- UserCode/OSUT3Analysis/Configuration/scripts/makeCutFlows.py 2013/02/06 00:28:41 1.6 +++ UserCode/OSUT3Analysis/Configuration/scripts/makeCutFlows.py 2013/07/16 09:05:34 1.19 @@ -10,24 +10,53 @@ from OSUT3Analysis.Configuration.process parser = OptionParser() parser = set_commandline_arguments(parser) -(options, args) = parser.parse_args() +parser.remove_option("-d") +parser.remove_option("-n") +parser.remove_option("-u") +parser.remove_option("-e") +parser.remove_option("-R") +parser.remove_option("-b") +parser.remove_option("-y") +parser.remove_option("-p") +parser.remove_option("-r") +parser.remove_option("-o") +parser.remove_option("--2D") + +parser.add_option("-d", "--diff", action="store_true", dest="makeDiffPlots", default=False, + help="add a column for data-MC to the tables") +parser.add_option("-r", "--ratio", action="store_true", dest="makeRatioPlots", default=False, + help="add a colunn for (data-MC)/MC to the tables") +parser.add_option("-s", "--sToB", action="store_true", dest="signalToBackground", default=False, + help="add a column for S/sqrt(S+B) to the tables") +parser.add_option("-o", "--output-file", dest="outputFileName", + help="specify an output file base name for the cutflow table (suffix will be appended), default is 'cutFlow'") -if options.localConfig: + +(arguments, args) = parser.parse_args() + +if arguments.localConfig: sys.path.append(os.getcwd()) - exec("from " + options.localConfig.rstrip('.py') + " import *") + exec("from " + arguments.localConfig.rstrip('.py') + " import *") -condor_dir = set_condor_output_dir(options) +condor_dir = set_condor_output_dir(arguments) from ROOT import TFile, gROOT, gStyle, gDirectory, TKey channels = [] processed_datasets = [] -texfile = condor_dir + "/cutFlow.tex" + +outputFileName = "cutFlow" +if arguments.outputFileName: + outputFileName = arguments.outputFileName +outputFileName = outputFileName.partition(".")[0] # If the input filename contains a period, take only the part before the period. + +texfile = condor_dir + "/" + outputFileName + ".tex" replacements = { ">":"$>$", "<":"$<$", - "eta ":"$\\eta$ ", + " eta ":" $\\eta$ ", + " abs(eta) ":" $|\\eta|$ ", "#":"Num", "\\rightarrow":"{\\rightarrow}", @@ -36,40 +65,31 @@ replacements = { "BCtoE QCD":"BCtoE$ $QCD", - "Pt ":"pt ", - "PT ":"pt ", - "pT ":"pt ", - "pt ":"$p_{T}$ ", - - "Ht ":"HT ", - "ht ":"HT ", - "hT ":"HT ", - "HT ":"$H_{T}$ ", - "tig$H_{T}$ ":"tight ", + " pt ":" $p_{T}$ ", + " ht ":"$H_{T}$ ", - "D0":"d0", "d0":"$d_{0}$", - "MET ":"Met ", - "MEt ":"Met ", - "met ":"Met ", - "Met ":"$\\not\\!\\!{E}_{T}$ ", - - "MHT ":"Mht ", - "MHt ":"Mht ", - "mht ":"Mht ", - "Mht ":"$\\not\\!\\!{H}_{T}$ ", + " MET ":" Met ", + " MEt ":" Met ", + " met ":" Met ", + " Met ":"$\\not\\!\\!{E}_{T}$ ", + + " MHT ":" Mht ", + " MHt ":" Mht ", + " mht ":" Mht ", + " Mht ":"$\\not\\!\\!{H}_{T}$ ", "M_Z" : "$M_{Z}$", "M_mumu" : "$M_{\\mu\\mu}$", "M_ee" : "$M_{ee}$", "M_ll" : "$M_{ll}$", - "|" : "$|$" } secondary_replacements = { "$$<$":"$<" + } @@ -100,7 +120,7 @@ for key in testFile.GetListOfKeys(): channels.append(key2.GetName()) fout = open (texfile, "w") -fout.write ("\\documentclass{article}\n\n") +fout.write ("\\documentclass[a2paper,8pt]{article}\n\n") fout.write ("\\usepackage[landscape,margin=0.15cm]{geometry}\n\n") fout.write ("\\usepackage{multirow}\n\n") fout.write ("\\begin{document}\n\n") @@ -109,32 +129,45 @@ fout.close () firstChannel = True -weight = intLumi / 10000.0 -for dataset in processed_datasets: - dataset_file = "%s/%s.root" % (condor_dir,dataset) - os.system("mergeTFileServiceHistograms -i %s -o %s -w %g" % (dataset_file, dataset_file + "_tmp", weight)) +#weight = intLumi / 10000.0 +#for dataset in processed_datasets: +# dataset_file = "%s/%s.root" % (condor_dir,dataset) +# fin = TFile (dataset_file) +# flags = fin.Get ("flags") +# noWeights = flags and flags.GetBinContent (1) +# fin.Close () +# if types[dataset] != "data" and not noWeights: +# os.system("mergeTFileServiceHistograms -i %s -o %s -w %g" % (dataset_file, dataset_file + "_tmp", weight)) +# else: +# os.system("mergeTFileServiceHistograms -i %s -o %s -w %g" % (dataset_file, dataset_file + "_tmp", 1.0)) -for channel in channels: # loop over final states, which each have their own directory - fout = open (texfile, "a") - if not firstChannel: - fout.write ("\\pagebreak\n\n") - firstChannel = False - fout.write ("\\section{" + channel + " channel}\n\n") - fout.close () +for channel in channels: # loop over final states, which each have their own directory + formatted_channel = channel.replace("_"," ") cutFlowArgs = "" selectionArgs = "" minusOneArgs = "" #print hist + for dataset in processed_datasets: - dataset_file = "%s/%s.root_tmp" % (condor_dir,dataset) + dataset_file = "%s/%s.root" % (condor_dir,dataset) #print dataset_file + if arguments.makeDiffPlots or arguments.makeRatioPlots: + if types[dataset] == "data": + dataset_file = "\"<" + dataset_file + "\"" + elif types[dataset] == "bgMC": + dataset_file = "\">" + dataset_file + "\"" + if arguments.signalToBackground: + if types[dataset] == "signalMC": + dataset_file = "\"<" + dataset_file + "\"" + elif types[dataset] == "bgMC": + dataset_file = "\">" + dataset_file + "\"" cutFlowArgs = cutFlowArgs + " " + dataset_file selectionArgs = selectionArgs + " " + dataset_file minusOneArgs = minusOneArgs + " " + dataset_file - cutFlowArgs = cutFlowArgs + " " + channel + "CutFlow" - selectionArgs = selectionArgs + " " + channel + "Selection" - minusOneArgs = minusOneArgs + " " + channel + "MinusOne" + cutFlowArgs = cutFlowArgs + " '" + channel + "CutFlow'" + selectionArgs = selectionArgs + " '" + channel + "Selection'" + minusOneArgs = minusOneArgs + " '" + channel + "MinusOne'" rawlabel = "$" + labels[dataset] + "$" label = rawlabel.replace("#","\\") @@ -144,26 +177,39 @@ for channel in channels: # loop over fin selectionArgs = selectionArgs + " " + label minusOneArgs = minusOneArgs + " " + label - #make cutFlowTable objects fout = open (texfile, "a") - fout.write ("\\subsection{Cut Flow}\n\n") + if not firstChannel: + fout.write ("\\pagebreak\n\n") + cutFlowTable = "cutFlowTable" + if arguments.makeDiffPlots: + cutFlowTable += " -d \"Data - MC\"" + if arguments.makeRatioPlots: + cutFlowTable += " -r \"(Data - MC) / MC\"" + if arguments.signalToBackground: + cutFlowTable += " -s \"$\\mathrm{S} / \\sqrt{\\mathrm{S} + \\mathrm{B}}$\"" + firstChannel = False + fout.write ("\\section*{" + formatted_channel + " channel}\n\n") + fout.write ("\\subsection*{Cut flow}\n\n") fout.close () - os.system("cutFlowTable -l %g %s >> %s" % (intLumi,cutFlowArgs,texfile)) + # print "Debug: running command: %s -l %g -m %s >> %s" % (cutFlowTable, intLumi,cutFlowArgs,texfile) + os.system("%s -l %g -m %s >> %s" % (cutFlowTable, intLumi,cutFlowArgs,texfile)) fout = open (texfile, "a") fout.write ("\\pagebreak\n\n") - fout.write ("\\subsection{Individual Selection}\n\n") + fout.write ("\\section*{" + formatted_channel + " channel}\n\n") + fout.write ("\\subsection*{Individual selection}\n\n") fout.close () - os.system("cutFlowTable -l %g %s >> %s" % (intLumi,selectionArgs,texfile)) + os.system("%s -l %g %s >> %s" % (cutFlowTable, intLumi,selectionArgs,texfile)) fout = open (texfile, "a") fout.write ("\\pagebreak\n\n") - fout.write ("\\subsection{Minus One}\n\n") + fout.write ("\\section*{" + formatted_channel + " channel}\n\n") + fout.write ("\\subsection*{Minus one}\n\n") fout.close () - os.system("cutFlowTable -l %g %s >> %s" % (intLumi,minusOneArgs,texfile)) + os.system("%s -l %g %s >> %s" % (cutFlowTable, intLumi,minusOneArgs,texfile)) -for dataset in processed_datasets: - dataset_file = "%s/%s.root_tmp" % (condor_dir,dataset) - os.remove(dataset_file) +#for dataset in processed_datasets: +# dataset_file = "%s/%s.root_tmp" % (condor_dir,dataset) +# os.remove(dataset_file) #reformat tex files for line in fileinput.FileInput(texfile,inplace=1): @@ -187,3 +233,6 @@ os.system(command) #os.system("rm %s" % texfile) os.system("rm %saux" % (texfile.rstrip("tex"))) os.system("rm %slog" % (texfile.rstrip("tex"))) + +print "Finished writing cutFlow to " + condor_dir + "/" + outputFileName + "{.tex,.pdf}" +