Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:32:57

0001 #!/usr/bin/env python3
0002 
0003 #######
0004 
0005 #  automatized plots generator for b-tagging performances
0006 #  Adrien Caudron, 2013, UCL
0007 
0008 #######
0009 
0010 #import all what is needed
0011 from __future__ import print_function
0012 from histoStyle import *
0013 #parser options
0014 from optparse import OptionParser
0015 usage="""%prog [options]"""
0016 description="""A simple script to generate validation plots"""
0017 epilog="""Example:
0018 plotFactory.py -f BTagRelVal_TTbar_Startup_600.root -F BTagRelVal_TTbar_Startup_600gspre3.root -r 600 -R 600gspre3 -s TTbar_Startup -S TTbar_Startup 
0019 """
0020 parser = OptionParser(usage=usage,add_help_option=True,description=description,epilog=epilog)
0021 parser.add_option("-f", "--valInputFile", dest="valPath", default=fileNameVal,
0022                   help="Read input file for sample to validated", metavar="VALFILE")
0023 parser.add_option("-F", "--refInputFile", dest="refPath", default=fileNameRef,
0024                   help="Read input file for reference sample", metavar="RAFFILE")
0025 parser.add_option("-r", "--valReleaseName", dest="ValRel", default=ValRel,
0026                   help="Name to refer to the release/conditions to validate, ex: 600, GTV18 ...", metavar="VALREL")
0027 parser.add_option("-R", "--refReleaseName", dest="RefRel", default=RefRel,
0028                   help="Name to refer to the reference release/conditions, ex: 600pre11, GTV16 ...", metavar="REFREL")
0029 parser.add_option("-s", "--valSampleName", dest="ValSample", default=ValSample,
0030                   help="Name to refer to the sample name to validate, ex: TTbar_FullSim, 2012C ...", metavar="VALSAMPLE")
0031 parser.add_option("-S", "--refSampleName", dest="RefSample", default=RefSample,
0032                   help="Name to refer to the reference sample name, ex: TTbar_FullSim, 2012C ...", metavar="REFSAMPLE")
0033 parser.add_option("-b", "--batch", dest="batch", default=batch,
0034                   action="store_true", help="if False, the script will run in batch mode")
0035 parser.add_option("-l", "--drawLegend", dest="drawLegend", default=drawLegend,
0036                   action="store_true", help="if True the legend will be drawn on top of the plots")
0037 parser.add_option("-p", "--printBanner", dest="printBanner", default=printBanner,
0038                   action="store_true", help="if True, a banner will be print on top of the plots")
0039 parser.add_option("-B", "--Banner", dest="Banner", default=Banner,
0040                   help="String to write as banner on top of the plots, option -B should be used")
0041 parser.add_option("-n", "--noRatio", dest="doRatio", default=doRatio,
0042                   action="store_false", help="if True, ratios plots will be created")
0043 (options, args) = parser.parse_args()
0044 print("file for validation", options.valPath, "file for reference", options.refPath)
0045 print("Validation release:", options.ValRel, "Reference release:", options.RefRel)
0046 print("Validation sample:", options.ValSample, "Reference sample:", options.RefSample)
0047 print("Options : batch mode ?", options.batch, "draw legend ?", options.drawLegend, "print banner ?", options.printBanner, "banner is ", options.Banner, "make ratio plots ?", options.doRatio)
0048 #define the input root files                                                                                                                                                                              
0049 if options.valPath and options.refPath :
0050     fileVal = TFile(options.valPath,"READ")
0051     fileRef = TFile(options.refPath,"READ") 
0052 #batch mode ?
0053 if options.batch : ROOT.gROOT.SetBatch()
0054 # style
0055 _style = Style.Style()
0056 _style.SetStyle()
0057 #title
0058 if options.ValSample==options.RefSample : title=options.ValRel+"vs"+options.RefRel+" "+options.ValSample+" "
0059 elif options.ValRel==options.RefRel : title=options.ValRel+" "+options.ValSample+"_vs_"+options.RefSample+" "
0060 else : title=options.ValRel+"vs"+options.RefRel+" "+options.ValSample+"_vs_"+options.RefSample+" "
0061 #declaration
0062 c = {}
0063 perfAll_Val = {}
0064 perfAll_Ref = {}
0065 perfAll_keys = []
0066 valHistos = {}
0067 refHistos ={}
0068 Histos = {}
0069 ratios = {} 
0070 #loop over eta an pt bins
0071 for b in EtaPtBin :
0072     #loop over the histos
0073     for h in listHistos :
0074         for f in listFlavors :
0075             perfAll_Val[f] = {}
0076             perfAll_Ref[f] = {}
0077         #loop over the list of taggers
0078         if h.listTagger is None : h.listTagger=listTag
0079         for tag in h.listTagger :
0080             keyHisto = tag+"_"+h.name+"_"+b
0081             if h.doPerformance :
0082                 keyHisto = tag+"_performance_vs_"+h.tagFlavor
0083             #loop over the flavours
0084             h_Val = {}
0085             h_Ref = {}
0086             passH = False
0087             for f in listFlavors :
0088                 path = pathInFile+tag+"_"+b+"/"+h.name+"_"+tag+"_"+b+f
0089                 if "_B_" in path : 
0090                     path=path.replace("_B_","_"+f+"_")
0091                     path=path.replace(b+f,b)
0092                 print(path)
0093                 #get histos
0094                 h_Val[f] = fileVal.Get(path)
0095                 h_Ref[f] = fileRef.Get(path)
0096                 if not h_Val[f] :
0097                     print("ERROR :", path, "not found in the roofiles, please check the spelling or check if this histogram is present in the rootdile")
0098                     passH = True
0099             if passH : continue
0100             #stop if FlavEffVsBEff_?_discr plot for all the taggers
0101             if h.name=="FlavEffVsBEff_B_discr" :
0102                 for f in listFlavors :
0103                     perfAll_Val[f][tag]=h_Val[f]
0104                     perfAll_Ref[f][tag]=h_Ref[f]
0105                 perfAll_keys.append(tag)
0106                 continue
0107             #create final histos   
0108             if h.doPerformance :
0109                 valHistos[keyHisto]=graphProducer(plot=h,histos=h_Val,isVal=True)
0110                 refHistos[keyHisto]=graphProducer(plot=h,histos=h_Ref,isVal=False)
0111             else :    
0112                 valHistos[keyHisto]=histoProducer(plot=h,histos=h_Val,keys=listFlavors,isVal=True)
0113                 refHistos[keyHisto]=histoProducer(plot=h,histos=h_Ref,keys=listFlavors,isVal=False)
0114             if valHistos[keyHisto] is None or refHistos[keyHisto] is None : continue
0115             if len(valHistos[keyHisto])!=len(refHistos[keyHisto]) : print("ERROR")
0116             #compute ratios 
0117             if options.doRatio :
0118                 if h.doPerformance:
0119                     ratiosList = createRatioFromGraph(valHistos[keyHisto],refHistos[keyHisto])
0120                 else :
0121                     ratiosList = createRatio(valHistos[keyHisto],refHistos[keyHisto])
0122                 ratios[keyHisto] = ratiosList
0123             else :
0124                 ratiosList = None
0125             #set name file
0126             if options.ValSample == options.RefSample : saveName=options.ValRel+"vs"+options.RefRel+"_"+options.ValSample+"_Val_"+keyHisto+"_all"
0127             elif options.ValRel==options.RefRel : saveName=options.ValRel+"_"+options.ValSample+"_vs_"+options.RefSample+"_Val_"+keyHisto+"_all"
0128             else : saveName=options.ValRel+"vs"+options.RefRel+"_"+options.ValSample+"_vs_"+options.RefSample+"_Val_"+keyHisto+"_all"
0129             #save canvas
0130             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)
0131         #for FlavEffVsBEff_B_discr
0132         if h.name=="FlavEffVsBEff_B_discr" :
0133             for f in ["C","DUSG"] :
0134                 for isVal in [True,False] :
0135                     keyHisto=f+str(isVal)
0136                     #setup the histos
0137                     if isVal : Histos[keyHisto]=histoProducer(plot=h,histos=perfAll_Val[f],keys=perfAll_keys,isVal=isVal)
0138                     else : Histos[keyHisto]=histoProducer(plot=h,histos=perfAll_Ref[f],keys=perfAll_keys,isVal=isVal)
0139                     #set name file    
0140                     if isVal : saveName=options.ValRel+"_"+options.ValSample+"_performance_Bvs"+f+"_allTaggers"
0141                     else : saveName=options.RefRel+"_"+options.RefSample+"_performance_Bvs"+f+"_allTaggers"
0142                     #set title
0143                     if isVal : titleFlav = options.ValRel+"_"+options.ValSample+"_performance_Bvs"+f+"_allTaggers"
0144                     else : titleFlav = options.RefRel+"_"+options.RefSample+"_performanceBvs"+f+"_allTaggers"
0145                     #save canvas
0146                     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))