CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
plotFactory.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 #######
4 
5 # automatized plots generator for b-tagging performances
6 # Adrien Caudron, 2013, UCL
7 
8 #######
9 
10 #import all what is needed
11 from histoStyle import *
12 #parser options
13 from optparse import OptionParser
14 usage="""%prog [options]"""
15 description="""A simple script to generate validation plots"""
16 epilog="""Example:
17 plotFactory.py -f BTagRelVal_TTbar_Startup_600.root -F BTagRelVal_TTbar_Startup_600gspre3.root -r 600 -R 600gspre3 -s TTbar_Startup -S TTbar_Startup
18 """
19 parser = OptionParser(usage=usage,add_help_option=True,description=description,epilog=epilog)
20 parser.add_option("-f", "--valInputFile", dest="valPath", default=fileNameVal,
21  help="Read input file for sample to validated", metavar="VALFILE")
22 parser.add_option("-F", "--refInputFile", dest="refPath", default=fileNameRef,
23  help="Read input file for reference sample", metavar="RAFFILE")
24 parser.add_option("-r", "--valReleaseName", dest="ValRel", default=ValRel,
25  help="Name to refer to the release/conditions to validate, ex: 600, GTV18 ...", metavar="VALREL")
26 parser.add_option("-R", "--refReleaseName", dest="RefRel", default=RefRel,
27  help="Name to refer to the reference release/conditions, ex: 600pre11, GTV16 ...", metavar="REFREL")
28 parser.add_option("-s", "--valSampleName", dest="ValSample", default=ValSample,
29  help="Name to refer to the sample name to validate, ex: TTbar_FullSim, 2012C ...", metavar="VALSAMPLE")
30 parser.add_option("-S", "--refSampleName", dest="RefSample", default=RefSample,
31  help="Name to refer to the reference sample name, ex: TTbar_FullSim, 2012C ...", metavar="REFSAMPLE")
32 parser.add_option("-b", "--batch", dest="batch", default=batch,
33  action="store_true", help="if False, the script will run in batch mode")
34 parser.add_option("-l", "--drawLegend", dest="drawLegend", default=drawLegend,
35  action="store_true", help="if True the legend will be drawn on top of the plots")
36 parser.add_option("-p", "--printBanner", dest="printBanner", default=printBanner,
37  action="store_true", help="if True, a banner will be print on top of the plots")
38 parser.add_option("-B", "--Banner", dest="Banner", default=Banner,
39  help="String to write as banner on top of the plots, option -B should be used")
40 parser.add_option("-n", "--noRatio", dest="doRatio", default=doRatio,
41  action="store_false", help="if True, ratios plots will be created")
42 (options, args) = parser.parse_args()
43 print "file for validation", options.valPath, "file for reference", options.refPath
44 print "Validation release:", options.ValRel, "Reference release:", options.RefRel
45 print "Validation sample:", options.ValSample, "Reference sample:", options.RefSample
46 print "Options : batch mode ?", options.batch, "draw legend ?", options.drawLegend, "print banner ?", options.printBanner, "banner is ", options.Banner, "make ratio plots ?", options.doRatio
47 #define the input root files
48 if options.valPath and options.refPath :
49  fileVal = TFile(options.valPath,"READ")
50  fileRef = TFile(options.refPath,"READ")
51 #batch mode ?
52 if options.batch : ROOT.gROOT.SetBatch()
53 # style
54 _style = Style.Style()
55 _style.SetStyle()
56 #title
57 if options.ValSample==options.RefSample : title=options.ValRel+"vs"+options.RefRel+" "+options.ValSample+" "
58 elif options.ValRel==options.RefRel : title=options.ValRel+" "+options.ValSample+"_vs_"+options.RefSample+" "
59 else : title=options.ValRel+"vs"+options.RefRel+" "+options.ValSample+"_vs_"+options.RefSample+" "
60 #declaration
61 c = {}
62 perfAll_Val = {}
63 perfAll_Ref = {}
64 perfAll_keys = []
65 valHistos = {}
66 refHistos ={}
67 Histos = {}
68 ratios = {}
69 #loop over eta an pt bins
70 for b in EtaPtBin :
71  #loop over the histos
72  for h in listHistos :
73  for f in listFlavors :
74  perfAll_Val[f] = {}
75  perfAll_Ref[f] = {}
76  #loop over the list of taggers
77  if h.listTagger is None : h.listTagger=listTag
78  for tag in h.listTagger :
79  keyHisto = tag+"_"+h.name+"_"+b
80  if h.doPerformance :
81  keyHisto = tag+"_performance_vs_"+h.tagFlavor
82  #loop over the flavours
83  h_Val = {}
84  h_Ref = {}
85  passH = False
86  for f in listFlavors :
87  path = pathInFile+tag+"_"+b+"/"+h.name+"_"+tag+"_"+b+f
88  if "_B_" in path :
89  path=path.replace("_B_","_"+f+"_")
90  path=path.replace(b+f,b)
91  print path
92  #get histos
93  h_Val[f] = fileVal.Get(path)
94  h_Ref[f] = fileRef.Get(path)
95  if not h_Val[f] :
96  print "ERROR :", path, "not found in the roofiles, please check the spelling or check if this histogram is present in the rootdile"
97  passH = True
98  if passH : continue
99  #stop if FlavEffVsBEff_?_discr plot for all the taggers
100  if h.name=="FlavEffVsBEff_B_discr" :
101  for f in listFlavors :
102  perfAll_Val[f][tag]=h_Val[f]
103  perfAll_Ref[f][tag]=h_Ref[f]
104  perfAll_keys.append(tag)
105  continue
106  #create final histos
107  if h.doPerformance :
108  valHistos[keyHisto]=graphProducer(plot=h,histos=h_Val,isVal=True)
109  refHistos[keyHisto]=graphProducer(plot=h,histos=h_Ref,isVal=False)
110  else :
111  valHistos[keyHisto]=histoProducer(plot=h,histos=h_Val,keys=listFlavors,isVal=True)
112  refHistos[keyHisto]=histoProducer(plot=h,histos=h_Ref,keys=listFlavors,isVal=False)
113  if valHistos[keyHisto] is None or refHistos[keyHisto] is None : continue
114  if len(valHistos[keyHisto])!=len(refHistos[keyHisto]) : print "ERROR"
115  #compute ratios
116  if options.doRatio :
117  if h.doPerformance:
118  ratiosList = createRatioFromGraph(valHistos[keyHisto],refHistos[keyHisto])
119  else :
120  ratiosList = createRatio(valHistos[keyHisto],refHistos[keyHisto])
121  ratios[keyHisto] = ratiosList
122  else :
123  ratiosList = None
124  #set name file
125  if options.ValSample == options.RefSample : saveName=options.ValRel+"vs"+options.RefRel+"_"+options.ValSample+"_Val_"+keyHisto+"_all"
126  elif options.ValRel==options.RefRel : saveName=options.ValRel+"_"+options.ValSample+"_vs_"+options.RefSample+"_Val_"+keyHisto+"_all"
127  else : saveName=options.ValRel+"vs"+options.RefRel+"_"+options.ValSample+"_vs_"+options.RefSample+"_Val_"+keyHisto+"_all"
128  #save canvas
129  c[keyHisto] = savePlots(title=title+tag,saveName=saveName,listFromats=listFromats,plot=h,Histos=valHistos[keyHisto]+refHistos[keyHisto],options=options,ratios=ratiosList,keyHisto=keyHisto,listLegend=listFlavors,legendName=h.legend)
130  #for FlavEffVsBEff_B_discr
131  if h.name=="FlavEffVsBEff_B_discr" :
132  for f in ["C","DUSG"] :
133  for isVal in [True,False] :
134  keyHisto=f+str(isVal)
135  #setup the histos
136  if isVal : Histos[keyHisto]=histoProducer(plot=h,histos=perfAll_Val[f],keys=perfAll_keys,isVal=isVal)
137  else : Histos[keyHisto]=histoProducer(plot=h,histos=perfAll_Ref[f],keys=perfAll_keys,isVal=isVal)
138  #set name file
139  if isVal : saveName=options.ValRel+"_"+options.ValSample+"_performance_Bvs"+f+"_allTaggers"
140  else : saveName=options.RefRel+"_"+options.RefSample+"_performance_Bvs"+f+"_allTaggers"
141  #set title
142  if isVal : titleFlav = options.ValRel+"_"+options.ValSample+"_performance_Bvs"+f+"_allTaggers"
143  else : titleFlav = options.RefRel+"_"+options.RefSample+"_performanceBvs"+f+"_allTaggers"
144  #save canvas
145  c[keyHisto] = savePlots(title=titleFlav,saveName=saveName,listFromats=listFromats,plot=h,Histos=Histos[keyHisto],keyHisto=keyHisto,listLegend=h.listTagger,options=options,legendName=h.legend.replace("FLAV",f))
def savePlots
Definition: histoStyle.py:313
def createRatio
Definition: histoStyle.py:392
def graphProducer
Definition: histoStyle.py:239
def createRatioFromGraph
Definition: histoStyle.py:407
def histoProducer
Definition: histoStyle.py:183