Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-11-26 02:34:37

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