CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/Validation/RecoB/scripts/plotFactory.py

Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 #######
00004 
00005 #  automatized plots generator for b-tagging performances
00006 #  Adrien Caudron, 2013, UCL
00007 
00008 #######
00009 
00010 #import all what is needed
00011 from histoStyle import *
00012 #parser options
00013 from optparse import OptionParser
00014 usage="""%prog [options]"""
00015 description="""A simple script to generate validation plots"""
00016 epilog="""Example:
00017 plotFactory.py -f BTagRelVal_TTbar_Startup_600.root -F BTagRelVal_TTbar_Startup_600gspre3.root -r 600 -R 600gspre3 -s TTbar_Startup -S TTbar_Startup 
00018 """
00019 parser = OptionParser(usage=usage,add_help_option=True,description=description,epilog=epilog)
00020 parser.add_option("-f", "--valInputFile", dest="valPath", default=fileNameVal,
00021                   help="Read input file for sample to validated", metavar="VALFILE")
00022 parser.add_option("-F", "--refInputFile", dest="refPath", default=fileNameRef,
00023                   help="Read input file for reference sample", metavar="RAFFILE")
00024 parser.add_option("-r", "--valReleaseName", dest="ValRel", default=ValRel,
00025                   help="Name to refer to the release/conditions to validate, ex: 600, GTV18 ...", metavar="VALREL")
00026 parser.add_option("-R", "--refReleaseName", dest="RefRel", default=RefRel,
00027                   help="Name to refer to the reference release/conditions, ex: 600pre11, GTV16 ...", metavar="REFREL")
00028 parser.add_option("-s", "--valSampleName", dest="ValSample", default=ValSample,
00029                   help="Name to refer to the sample name to validate, ex: TTbar_FullSim, 2012C ...", metavar="VALSAMPLE")
00030 parser.add_option("-S", "--refSampleName", dest="RefSample", default=RefSample,
00031                   help="Name to refer to the reference sample name, ex: TTbar_FullSim, 2012C ...", metavar="REFSAMPLE")
00032 parser.add_option("-b", "--batch", dest="batch", default=batch,
00033                   action="store_true", help="if False, the script will run in batch mode")
00034 parser.add_option("-l", "--drawLegend", dest="drawLegend", default=drawLegend,
00035                   action="store_true", help="if True the legend will be drawn on top of the plots")
00036 parser.add_option("-p", "--printBanner", dest="printBanner", default=printBanner,
00037                   action="store_true", help="if True, a banner will be print on top of the plots")
00038 parser.add_option("-B", "--Banner", dest="Banner", default=Banner,
00039                   help="String to write as banner on top of the plots, option -B should be used")
00040 parser.add_option("-n", "--noRatio", dest="doRatio", default=doRatio,
00041                   action="store_false", help="if True, ratios plots will be created")
00042 (options, args) = parser.parse_args()
00043 print "file for validation", options.valPath, "file for reference", options.refPath
00044 print "Validation release:", options.ValRel, "Reference release:", options.RefRel
00045 print "Validation sample:", options.ValSample, "Reference sample:", options.RefSample
00046 print "Options : batch mode ?", options.batch, "draw legend ?", options.drawLegend, "print banner ?", options.printBanner, "banner is ", options.Banner, "make ratio plots ?", options.doRatio
00047 #define the input root files                                                                                                                                                                              
00048 if options.valPath and options.refPath :
00049     fileVal = TFile(options.valPath,"READ")
00050     fileRef = TFile(options.refPath,"READ") 
00051 #batch mode ?
00052 if options.batch : ROOT.gROOT.SetBatch()
00053 # style
00054 _style = Style.Style()
00055 _style.SetStyle()
00056 #title
00057 if options.ValSample==options.RefSample : title=options.ValRel+"vs"+options.RefRel+" "+options.ValSample+" "
00058 elif options.ValRel==options.RefRel : title=options.ValRel+" "+options.ValSample+"_vs_"+options.RefSample+" "
00059 else : title=options.ValRel+"vs"+options.RefRel+" "+options.ValSample+"_vs_"+options.RefSample+" "
00060 #declaration
00061 c = {}
00062 perfAll_Val = {}
00063 perfAll_Ref = {}
00064 perfAll_keys = []
00065 valHistos = {}
00066 refHistos ={}
00067 Histos = {}
00068 ratios = {} 
00069 #loop over eta an pt bins
00070 for b in EtaPtBin :
00071     #loop over the histos
00072     for h in listHistos :
00073         for f in listFlavors :
00074             perfAll_Val[f] = {}
00075             perfAll_Ref[f] = {}
00076         #loop over the list of taggers
00077         if h.listTagger is None : h.listTagger=listTag
00078         for tag in h.listTagger :
00079             keyHisto = tag+"_"+h.name+"_"+b
00080             if h.doPerformance :
00081                 keyHisto = tag+"_performance_vs_"+h.tagFlavor
00082             #loop over the flavours
00083             h_Val = {}
00084             h_Ref = {}
00085             passH = False
00086             for f in listFlavors :
00087                 path = pathInFile+tag+"_"+b+"/"+h.name+"_"+tag+"_"+b+f
00088                 if "_B_" in path : 
00089                     path=path.replace("_B_","_"+f+"_")
00090                     path=path.replace(b+f,b)
00091                 print path
00092                 #get histos
00093                 h_Val[f] = fileVal.Get(path)
00094                 h_Ref[f] = fileRef.Get(path)
00095                 if not h_Val[f] :
00096                     print "ERROR :", path, "not found in the roofiles, please check the spelling or check if this histogram is present in the rootdile"
00097                     passH = True
00098             if passH : continue
00099             #stop if FlavEffVsBEff_?_discr plot for all the taggers
00100             if h.name=="FlavEffVsBEff_B_discr" :
00101                 for f in listFlavors :
00102                     perfAll_Val[f][tag]=h_Val[f]
00103                     perfAll_Ref[f][tag]=h_Ref[f]
00104                 perfAll_keys.append(tag)
00105                 continue
00106             #create final histos   
00107             if h.doPerformance :
00108                 valHistos[keyHisto]=graphProducer(plot=h,histos=h_Val,isVal=True)
00109                 refHistos[keyHisto]=graphProducer(plot=h,histos=h_Ref,isVal=False)
00110             else :    
00111                 valHistos[keyHisto]=histoProducer(plot=h,histos=h_Val,keys=listFlavors,isVal=True)
00112                 refHistos[keyHisto]=histoProducer(plot=h,histos=h_Ref,keys=listFlavors,isVal=False)
00113             if valHistos[keyHisto] is None or refHistos[keyHisto] is None : continue
00114             if len(valHistos[keyHisto])!=len(refHistos[keyHisto]) : print "ERROR"
00115             #compute ratios 
00116             if options.doRatio :
00117                 if h.doPerformance:
00118                     ratiosList = createRatioFromGraph(valHistos[keyHisto],refHistos[keyHisto])
00119                 else :
00120                     ratiosList = createRatio(valHistos[keyHisto],refHistos[keyHisto])
00121                 ratios[keyHisto] = ratiosList
00122             else :
00123                 ratiosList = None
00124             #set name file
00125             if options.ValSample == options.RefSample : saveName=options.ValRel+"vs"+options.RefRel+"_"+options.ValSample+"_Val_"+keyHisto+"_all"
00126             elif options.ValRel==options.RefRel : saveName=options.ValRel+"_"+options.ValSample+"_vs_"+options.RefSample+"_Val_"+keyHisto+"_all"
00127             else : saveName=options.ValRel+"vs"+options.RefRel+"_"+options.ValSample+"_vs_"+options.RefSample+"_Val_"+keyHisto+"_all"
00128             #save canvas
00129             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)
00130         #for FlavEffVsBEff_B_discr
00131         if h.name=="FlavEffVsBEff_B_discr" :
00132             for f in ["C","DUSG"] :
00133                 for isVal in [True,False] :
00134                     keyHisto=f+str(isVal)
00135                     #setup the histos
00136                     if isVal : Histos[keyHisto]=histoProducer(plot=h,histos=perfAll_Val[f],keys=perfAll_keys,isVal=isVal)
00137                     else : Histos[keyHisto]=histoProducer(plot=h,histos=perfAll_Ref[f],keys=perfAll_keys,isVal=isVal)
00138                     #set name file    
00139                     if isVal : saveName=options.ValRel+"_"+options.ValSample+"_performance_Bvs"+f+"_allTaggers"
00140                     else : saveName=options.RefRel+"_"+options.RefSample+"_performance_Bvs"+f+"_allTaggers"
00141                     #set title
00142                     if isVal : titleFlav = options.ValRel+"_"+options.ValSample+"_performance_Bvs"+f+"_allTaggers"
00143                     else : titleFlav = options.RefRel+"_"+options.RefSample+"_performanceBvs"+f+"_allTaggers"
00144                     #save canvas
00145                     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))