CMS 3D CMS Logo

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