Exemplo n.º 1
0
  def __init__( self, outputFile, **kw ):
    Logger.__init__(self,kw)
    if not outputFile.endswith('.root'):
      outputFile += '.root'
    # Use this property to rebuild the storegate from a root file
    self._restoreStoreGate=retrieve_kw(kw,'restoreStoreGate',False)
    filterDirs=retrieve_kw(kw,'filterDirs', None)
    #Create TFile object to hold everything
    from ROOT import TFile
    outputFile = expandPath( outputFile )
    if self._restoreStoreGate:
      import os.path
      if not os.path.exists( outputFile ):
        raise ValueError("File '%s' does not exist" % outputFile)
      self._file = TFile( outputFile, "read")
    else:
      self._file = TFile( outputFile, "recreate")
    
    self._currentDir = ""
    self._objects    = dict()
    self._dirs       = list()
    import os
    self._outputFile = os.path.abspath(outputFile)

    if self._restoreStoreGate:
      retrievedObjs = self.__restore(self._file, filterDirs=filterDirs)
      for name, obj in retrievedObjs:
        self._dirs.append(name)
        self._objects[name]=obj
Exemplo n.º 2
0
 def __init__(self, w, b, **kwargs):
   Logger.__init__(self)
   self.layer     = retrieve_kw(kwargs, 'Layer'       ,0       )
   self.funcName  = retrieve_kw(kwargs, 'funcName'  ,None    )
   checkForUnusedVars(kwargs)
   self.W = np.matrix(w)
   self.b = np.transpose( np.matrix(b) )
Exemplo n.º 3
0
    def __call__(self, objList, **kwargs):

        ylimits = retrieve_kw(kwargs, 'ylimits', [(0.7, 1.1)])
        legends = retrieve_kw(kwargs, 'legends', None)
        hist_names = retrieve_kw(kwargs, 'hist_names',
                                 ['eff_et', 'eff_eta', 'eff_mu'])
        drawSame = retrieve_kw(kwargs, 'drawSame', False)
        doEffLabel = retrieve_kw(kwargs, 'doEffLabel', True)

        if 'level' in kwargs: self.level = kw.pop('level')
        #checkForUnusedVars( kwargs, self._logger.warning )

        # protection for legends list parameter
        if not legends:
            legends = []
            for o in objList:
                legends.append(o['rawInfo']['algname'])

        llabel = objList[0]['rawInfo']['outputname'].split('/')[0]

        # fix y axis size
        # protection in axis
        if len(ylimits) == 1:
            l = ylimits
            ylimits = [l for i in hist_names]
        # ready for plots...
        efficiencies_objects = self.__convert(objList)
        oname = kwargs.pop('oname')

        # TODO: make eff label for future
        eff = []
        for index, e in enumerate(efficiencies_objects['eff']):
            passed = efficiencies_objects['passed'][index]
            total = efficiencies_objects['total'][index]
            s = ('%s: %1.2f%% (%d/%d)') % (llabel, e, passed, total)
            eff.append(s)

        # Add efficiency label into the plot (only for ratio plots)
        if doEffLabel:
            kwargs['eff_label'] = eff

        if drawSame:
            self.__plot_profiles_in_same_canvas(efficiencies_objects,
                                                hist_names,
                                                legends,
                                                ylimits,
                                                oname=oname,
                                                **kwargs)
            return [oname + '.pdf']
        else:
            figures = []
            for i, eff in enumerate(hist_names):
                self.__plot_profiles(efficiencies_objects[eff],
                                     legends,
                                     ylimits[i],
                                     oname=oname + '_' + eff,
                                     **kwargs)
                figures.append(oname + '_' + eff + '.pdf')
            return figures
Exemplo n.º 4
0
    def add_quadrant(self, branch_x, branch_y, **kwargs):

        dependence_item = retrieve_kw(kwargs, 'dependence_item', 1)
        alias = retrieve_kw(kwargs, 'alias', [])
        # check correct value
        if dependence_item > 1:
            self._logger.fatal(
                'The third argument must be -1 (no NN dependence), 0 (first alg) or 1 (second alg)'
            )
        self._quadrantNames.append(
            (branch_x, branch_y, dependence_item, alias))
Exemplo n.º 5
0
 def __init__(self, **kw):
     EventBase.__init__(self, **kw)
     self._algTools = list()
     self._level = retrieve_kw(kw, 'level', LoggingLevel.INFO)
     #checkForUnusedVars(kw)
     del kw
     self._initialized = StatusTool.NOT_INITIALIZED
     self._finalized = StatusTool.NOT_FINALIZED
     self._logger.info('Created with id (%d)', self._id)
Exemplo n.º 6
0
    def gen_table(self, store, algname, subdirs, **kwargs):

        basepath = retrieve_kw(kwargs, 'basepath', 'HLT/Egamma/Expert')
        dirname = retrieve_kw(kwargs, 'dirname', 'Efficiency')

        path = basepath + '/' + algname + '/' + dirname
        self._logger.info('{:-^79}'.format((' %s ') % (algname)))
        for dir in subdirs:
            obj, eff, passed, total = self.__retrieve_efficiencies(
                store, path, dir + '/', dir + '/match_')
            eff = ('%1.2f') % (eff)
            passed = ('%d') % (passed)
            total = ('%d') % (total)
            stroutput = '| {0:<40} |{1:<10}| {2:<5} ({3:<5}, {4:<5}) |'.format(
                algname, dir, eff, passed, total)

            self._logger.info(stroutput)
        self._logger.info('{:-^79}'.format(''))
Exemplo n.º 7
0
    def __call__(self, store, algname, **kwargs):

        basepath = retrieve_kw(kwargs, 'basepath', 'HLT/Egamma/Expert')
        dirname = retrieve_kw(kwargs, 'dirname', 'Efficiency')
        input_name = retrieve_kw(kwargs, 'inputname', 'HLT/')
        output_name = retrieve_kw(kwargs, 'outputname', 'HLT/match_')

        if 'level' in kwargs: self.level = kw.pop('level')
        checkForUnusedVars(kwargs, self._logger.warning)
        input_name = input_name.replace('//', '/')
        output_name = output_name.replace('//', '/')

        # some info to hold
        rawInfo = {'inputname': input_name,\
                   'outputname' : output_name,\
                   'basepath': basepath,\
                   'dirname':dirname,\
                   'algname':algname}

        # This can be:
        # Egamma/algname/Efficiency/L1Calo/match_eta
        path = (basepath + '/' + algname + '/' + dirname).replace('//', '/')

        try:  # protection
            self._logger.debug(
                'Extracting efficiencies information for %s from %s', algname,
                path)
            obj, eff, passed, total = self.__retrieve_efficiencies(store, path,\
                                      input_name, output_name)
        except RuntimeError:
            self._logger.error(
                ('Can not extract the efficiencies for this path %s') % (path))
            raise RuntimeError(
                'loop error in retrieve_efficiencies private method')

        # hold efficiencies values
        eobj = {'eff_et':obj['eff_et'], 'eff_eta':obj['eff_eta'], 'eff_mu':obj['eff_mu'],'eff_nvtx':obj['eff_nvtx'], \
            'eff':eff, 'passed':passed, 'total':total, 'rawInfo':rawInfo}
        self._logger.debug(('%s with efficiency: %1.2f  (%d/%d)')%\
                          (algname, eff, passed, total))
        return eobj
Exemplo n.º 8
0
    def __init__(self, **kw):

        Logger.__init__(self, **kw)

        self._maxPileupLinearCorrectionValue = retrieve_kw(
            kw, 'maxPileupLinearCorrectionValue', 100)
        self._removeOutputTansigTF = retrieve_kw(kw, 'removeOutputTansigTF',
                                                 True)
        self._useEtaVar = retrieve_kw(kw, 'useEtaVar', False)
        self._useLumiVar = retrieve_kw(kw, 'useLumiVar', False)
        self._toPython = retrieve_kw(kw, 'toPython', True)
        self._doPileupCorrection = retrieve_kw(kw, 'doPileupCorrection', True)
        self._toPickle = retrieve_kw(kw, 'toPickle', False)
Exemplo n.º 9
0
    def __init__(self, **kw):

        Logger.__init__(self, kw)
        # Retrieve all information needed
        self._fList = retrieve_kw(kw, 'inputFiles', NotSet)
        self._ofile = retrieve_kw(kw, 'outputFile', "histos.root")
        self._treePath = retrieve_kw(kw, 'treePath', NotSet)
        self._dataframe = retrieve_kw(kw, 'dataframe',
                                      DataframeEnum.SkimmedNtuple)
        self._nov = retrieve_kw(kw, 'nov', -1)
        self._fList = csvStr2List(self._fList)
        self._fList = expandFolders(self._fList)

        # Loading libraries
        if ROOT.gSystem.Load('libTuningTools') < 0:
            self._fatal("Could not load TuningTools library", ImportError)

        self._containersSvc = {}
        self._storegateSvc = NotSet
        import random
        import time
        random.seed(time.time())
        # return a random number
        self._id = random.randrange(100000)
Exemplo n.º 10
0
 def __init__( self, dataCurator, d, **kw ):
   Logger.__init__( self, kw )
   d.update( kw ); del kw
   # Define discriminator type (for now we only have one discriminator type):
   # FIXME This chould be configured using coreDef
   try:
     try:
       from libTuningTools import DiscriminatorPyWrapper
     except ImportError:
       from libTuningToolsLib import DiscriminatorPyWrapper
   except ImportError:
     self._fatal("Cannot use FastNet DiscriminatorPyWrapper: library is not available!")
   self.dataCurator    = dataCurator
   # Discriminator parameters:
   self.DiscrClass           = DiscriminatorPyWrapper
   self.removeOutputTansigTF = retrieve_kw(d, 'removeOutputTansigTF', True )
   # Decision making parameters:
   self.thresEtBins       = retrieve_kw( d, 'thresEtBins',  None)
   self.thresEtaBins      = retrieve_kw( d, 'thresEtaBins', None)
   # TODO There is no need to require this from user
   self.method         = DecisionMakingMethod.retrieve( retrieve_kw(d, 'decisionMakingMethod', DecisionMakingMethod.LHLinearApproach ) )
   # Pileup limits specification
   self.pileupRef = None
   self.pileupLimits   = retrieve_kw( d, 'pileupLimits', None)
   self.maxCorr        = retrieve_kw( d, 'maxCorr', None)
   self.frMargin       = retrieve_kw( d, 'frMargin', [1.05, 1.1, 1.15, 1.25, 1.5, 2.0])
   if self.method is DecisionMakingMethod.LHLinearApproach:
     self._info("Using limits: %r", self.pileupLimits)
     if not 'pileupRef' in d:
       self._fatal("pileupRef must specified in order to correctly label the xaxis.")
   if 'pileupRef' in d:
     self.pileupRef      = PileupReference.retrieve( retrieve_kw( d, 'pileupRef' ) )
   # Linear LH Threshold Correction methods
   #self.maxFRMultipler = retrieve_kw( d, 'maxFRMultipler', None)
   #checkForUnusedVars( d )
   # TODO: Add fix fraction allowing to load a module and a table of values
   if self.method is DecisionMakingMethod.LHLinearApproach:
     try:
       import ElectronIDDevelopment
     except ImportError:
       self._warning("Using a standalone version of the pile-up fitting version which may not be the latest.")
Exemplo n.º 11
0
    def __call__(self, **kwargs):
        key = retrieve_kw(kwargs, 'key', None)
        outputdir = retrieve_kw(kwargs, 'outputdir', 'plots')
        drawSame = retrieve_kw(kwargs, 'drawsame', True)
        atlaslabel = retrieve_kw(kwargs, 'atlaslabel', 'Internal')
        isBackground = retrieve_kw(kwargs, 'isbackground', False)
        doRatio = retrieve_kw(kwargs, 'doRatio', False)
        doEffLabel = retrieve_kw(kwargs, 'doEffLabel', True)
        checkForUnusedVars(kwargs, self._logger.warning)

        import os
        localpath = os.getcwd() + '/' + outputdir
        try:
            os.mkdir(localpath)
        except:
            self._logger.warning('The director %s exist.', localpath)

        import base64
        if key:
            if not ':' in key:
                key = base64.b64decode(key)
                self._logger.info('Translate key to: %s', key)
            key = key.split(',')
            cList = self.translate(key)
        else:
            self.display()
            key = input('Write the configuration: ')
            self._logger.info(
                'Use this key %s if you want to reproduce this plot',
                base64.b64encode(key))
            key = key.split(',')
            cList = self.translate(key)

        # FIXME: The subdirs must be the same for all configuration
        subdirs = cList[0]['subdirs']

        figures = []

        for subdir in subdirs:
            eff_list = list()
            legends = list()
            algnames = None
            for index, c in enumerate(cList):
                effObj = self._effReader(self._store[c['file']],
                                         c['algname'],
                                         basepath=c['basepath'],
                                         dirname=c['dirname'],
                                         inputname=subdir + '/',
                                         outputname=subdir + '/match_')
                eff_list.append(effObj)
                legends.append(c['legend'])
                if not algnames:
                    algnames = c['algname']
                else:
                    algnames += '_' + c['algname']


            f = profile( eff_list,
                 ylimits          = [ (0.0, 1.4) , (0.0, 1.2), (0.0, 1.4), (0.0, 1.4) ] \
                     if not isBackground else [(-0.3,0.1),(-0.3,0.4),(-0.3,0.4),(-0.3, 0.4)],
                 ylabel           = 'Trigger Efficiency',
                 hist_names       = ['eff_et','eff_eta','eff_mu'],
                 legend_header    = '',
                 legend_prefix    = '',
                 legends          = legends,
                 region_label     = '',
                 oname            = localpath+'/'+subdir+'_'+ algnames,
                 drawSame         = drawSame,
                 doRatio          = doRatio,
                 atlaslabel       = atlaslabel,
               )

            figures.extend(f)

            f = profile( eff_list,
                 ylimits          = [ (0.0, 1.4) , (0.0, 1.4), (0.0, 1.4), (0.0, 1.4) ] \
                     if not isBackground else [(-0.3,0.1),(-0.3,0.4),(-0.3,0.4),(-0.3, 0.4)],
                 ylabel           = 'Trigger Efficiency',
                 hist_names       = ['eff_et','eff_eta','eff_mu'],
                 legend_header    = '',
                 legend_prefix    = '',
                 legends          = legends,
                 region_label     = '',
                 oname            = localpath+'/'+subdir+'_'+ algnames,
                 drawSame         = False,
                 doRatio          = True,
                 doEffLabel       = doEffLabel,
                 atlaslabel       = atlaslabel,
               )

            figures.extend(f)

        for c in cList:
            self._effReader.gen_table(self._store[c['file']],
                                      c['algname'],
                                      c['subdirs'],
                                      basepath=c['basepath'],
                                      dirname=c['dirname'])

        return figures
Exemplo n.º 12
0
    def plot(self, **kw):

        dirname = retrieve_kw(kw, 'dirname', 'Quadrant')
        aliasMap = retrieve_kw(
            kw, 'alias_map', {
                'xx': 'BothPassed',
                'oo': 'BothReject',
                'xo': 'OnlyPassedLH',
                'ox': 'OnlyPassedRinger'
            })
        pdftitle = retrieve_kw(kw, 'pdftitle', 'Quadrant')
        pdfoutput = retrieve_kw(kw, 'pdfoutput', 'quadrant')

        import os
        # Organize outputs (.py and .pdf)
        prefix = self._basepath.split('/')[-1]
        localpath = os.getcwd() + '/' + dirname + '/' + prefix

        try:
            if not os.path.exists(localpath):
                os.makedirs(localpath)
        except:
            self._logger.warning('The director %s exist.', localpath)

        hist_names = [
            'et', 'eta', 'mu', 'nvtx', 'reta', 'eratio', 'weta2', 'rhad',
            'rphi', 'f1', 'f3'
        ]
        hist_labels = [
            'E_{T}', "#eta", "<#mu>", 'N_{vtx}', 'R_{eta}', 'E_{ratio}',
            'W_{eta2}', 'R_{had}', 'R_{phi}', 'f_{1}', 'f_{3}'
        ]

        def sumAllRegions(histname):
            h = None
            for etBinIdx in range(len(self._etBins) - 1):
                for etaBinIdx in range(len(self._etaBins) - 1):
                    binningname = ('et%d_eta%d') % (etBinIdx, etaBinIdx)
                    path = (histname) % (binningname)
                    if h:
                        h += self.storeSvc().histogram(path).ProjectionY()
                    else:
                        h = self.storeSvc().histogram(
                            path).ProjectionY().Clone()
            return h

        collection = []

        for info in self._quadrantNames:
            for idx, hist in enumerate(hist_names):
                name = info[0] + '_X_' + info[1]
                dirname = self._basepath + '/' + name + '/%s'
                h_xx = sumAllRegions(dirname + '/passed_passed/' + hist)
                h_xo = sumAllRegions(dirname + '/passed_rejected/' + hist)
                h_ox = sumAllRegions(dirname + '/rejected_passed/' + hist)
                h_oo = sumAllRegions(dirname + '/rejected_rejected/' + hist)
                # Loop over histograms
                plotname1 = localpath + '/' + prefix + '_' + name + '_' + hist + '.pdf'
                plotname2 = localpath + '/' + prefix + '_' + name + '_' + hist + '_logscale.pdf'
                plotname3 = localpath + '/' + prefix + '_' + name + '_' + hist + '_agreement.pdf'
                plotname4 = localpath + '/' + prefix + '_' + name + '_' + hist + '_agreement_logscale.pdf'
                #self.__plot_quadrant_template1(h_oo.Clone(),h_xo.Clone(),h_ox.Clone(),h_xx.Clone(),aliasMap,hist_labels[idx],plotname1,False)
                #self.__plot_quadrant_template1(h_oo.Clone(),h_xo.Clone(),h_ox.Clone(),h_xx.Clone(),aliasMap,hist_labels[idx],plotname2,True )
                #self.__plot_quadrant_template2(h_oo.Clone(),h_xo.Clone(),h_ox.Clone(),h_xx.Clone(),aliasMap,hist_labels[idx],plotname3,False )
                #self.__plot_quadrant_template2(h_oo.Clone(),h_xo.Clone(),h_ox.Clone(),h_xx.Clone(),aliasMap,hist_labels[idx],plotname4,True )
                collection.append((h_xx, h_xo, h_ox, h_oo))

        #toPDF=False
        toPDF = True
        if toPDF:
            #from RingerCore.tex.TexAPI import *
            from RingerCore.tex.BeamerAPI import BeamerTexReportTemplate1,BeamerSection,BeamerSubSection,\
                                                 BeamerMultiFigureSlide,BeamerFigureSlide
            # apply beamer
            with BeamerTexReportTemplate1(theme='Berlin',
                                          _toPDF=True,
                                          title=pdftitle,
                                          outputFile=pdfoutput,
                                          font='structurebold'):

                for info in self._quadrantNames:

                    name = info[0] + '_X_' + info[1]
                    section_name = info[3][0].replace('_', '\_')
                    subsection_name = info[3][1].replace('_', '\_')

                    with BeamerSection(name=section_name):
                        with BeamerSubSection(name=subsection_name):
                            figures = {
                                'linear': [],
                                'log': [],
                                'diff_linear': [],
                                'diff_log': []
                            }
                            for idx, hist in enumerate(hist_names):
                                figures['linear'].append(localpath + '/' +
                                                         prefix + '_' + name +
                                                         '_' + hist + '.pdf')
                                figures['log'].append(localpath + '/' +
                                                      prefix + '_' + name +
                                                      '_' + hist +
                                                      '_logscale.pdf')
                                figures['diff_linear'].append(localpath + '/' +
                                                              prefix + '_' +
                                                              name + '_' +
                                                              hist +
                                                              '_agreement.pdf')
                                figures['diff_log'].append(
                                    localpath + '/' + prefix + '_' + name +
                                    '_' + hist + '_agreement_logscale.pdf')

                            BeamerMultiFigureSlide(
                                title='Offline Calo Variables',
                                paths=figures['linear'],
                                nDivWidth=4  # x
                                ,
                                nDivHeight=3  # y
                                ,
                                texts=None,
                                fortran=False,
                                usedHeight=0.6,
                                usedWidth=0.9)

                            BeamerMultiFigureSlide(
                                title=
                                'Offline Calo Variables (shown on log scale)',
                                paths=figures['log'],
                                nDivWidth=4  # x
                                ,
                                nDivHeight=3  # y
                                ,
                                texts=None,
                                fortran=False,
                                usedHeight=0.6,
                                usedWidth=0.9)

                            BeamerMultiFigureSlide(
                                title=
                                'Offline Calo Variables (Agreement and Disagrement)',
                                paths=figures['diff_linear'],
                                nDivWidth=4  # x
                                ,
                                nDivHeight=3  # y
                                ,
                                texts=None,
                                fortran=False,
                                usedHeight=0.6,
                                usedWidth=0.9)

                            BeamerMultiFigureSlide(
                                title=
                                'Offline Calo Variables (Agreement and Disagrement show on log scale)',
                                paths=figures['diff_log'],
                                nDivWidth=4  # x
                                ,
                                nDivHeight=3  # y
                                ,
                                texts=None,
                                fortran=False,
                                usedHeight=0.6,
                                usedWidth=0.9)

        return StatusCode.SUCCESS
Exemplo n.º 13
0
  def __call__( self, fList, ringerOperation, **kw):
    """
      Read ntuple and return patterns and efficiencies.
      Arguments:
        - fList: The file path or file list path. It can be an argument list of
        two types:
          o List: each element is a string path to the file;
          o Comma separated string: each path is separated via a comma
          o Folders: Expand folders recursively adding also files within them to analysis
        - ringerOperation: Set Operation type. It can be both a string or the
          RingerOperation
      Optional arguments:
        - filterType [None]: whether to filter. Use FilterType enumeration
        - reference [Truth]: set reference for targets. Use Reference enumeration
        - treePath [Set using operation]: set tree name on file, this may be set to
          use different sources then the default.
            Default for:
              o Offline: Offline/Egamma/Ntuple/electron
              o L2: Trigger/HLT/Egamma/TPNtuple/e24_medium_L1EM18VH
        - l1EmClusCut [None]: Set L1 cluster energy cut if operating on the trigger
        - l2EtCut [None]: Set L2 cluster energy cut value if operating on the trigger
        - offEtCut [None]: Set Offline cluster energy cut value
        - nClusters [None]: Read up to nClusters. Use None to run for all clusters.
        - getRatesOnly [False]: Read up to nClusters. Use None to run for all clusters.
        - etBins [None]: E_T bins (GeV) where the data should be segmented
        - etaBins [None]: eta bins where the data should be segmented
        - ringConfig [100]: A list containing the number of rings available in the data
          for each eta bin.
        - crossVal [None]: Whether to measure benchmark efficiency splitting it
          by the crossVal-validation datasets
        - extractDet [None]: Which detector to export (use Detector enumeration).
          Defaults are:
            o L2Calo: Calorimetry
            o L2: Tracking
            o Offline: Calorimetry
            o Others: CaloAndTrack
        - standardCaloVariables [False]: Whether to extract standard track variables.
        - useTRT [False]: Whether to export TRT information when dumping track
          variables.
        - supportTriggers [True]: Whether reading data comes from support triggers
    """
    # Offline information branches:
    __offlineBranches = ['el_et',
                         'el_eta',
                         #'el_loose',
                         #'el_medium',
                         #'el_tight',
                         'el_lhLoose',
                         'el_lhMedium',
                         'el_lhTight',
                         'mc_hasMC',
                         'mc_isElectron',
                         'mc_hasZMother',
                         'el_nPileupPrimaryVtx',
                         ]
    # Online information branches
    __onlineBranches = []
    __l2stdCaloBranches = ['trig_L2_calo_et',
                           'trig_L2_calo_eta',
                           'trig_L2_calo_phi',
                           'trig_L2_calo_e237', # rEta
                           'trig_L2_calo_e277', # rEta
                           'trig_L2_calo_fracs1', # F1: fraction sample 1
                           'trig_L2_calo_weta2', # weta2
                           'trig_L2_calo_ehad1', # energy on hadronic sample 1
                           'trig_L2_calo_emaxs1', # eratio
                           'trig_L2_calo_e2tsts1', # eratio
                           'trig_L2_calo_wstot',] # wstot
    __l2trackBranches = [ # Do not add non patter variables on this branch list
                         #'trig_L2_el_pt',
                         #'trig_L2_el_eta',
                         #'trig_L2_el_phi',
                         #'trig_L2_el_caloEta',
                         #'trig_L2_el_charge',
                         #'trig_L2_el_nTRTHits',
                         #'trig_L2_el_nTRTHiThresholdHits',
                         'trig_L2_el_etOverPt',
                         'trig_L2_el_trkClusDeta',
                         'trig_L2_el_trkClusDphi',]
    # Retrieve information from keyword arguments
    filterType            = retrieve_kw(kw, 'filterType',            FilterType.DoNotFilter )
    reference             = retrieve_kw(kw, 'reference',             Reference.Truth        )
    l1EmClusCut           = retrieve_kw(kw, 'l1EmClusCut',           None                   )
    l2EtCut               = retrieve_kw(kw, 'l2EtCut',               None                   )
    efEtCut               = retrieve_kw(kw, 'efEtCut',               None                   )
    offEtCut              = retrieve_kw(kw, 'offEtCut',              None                   )
    treePath              = retrieve_kw(kw, 'treePath',              None                   )
    nClusters             = retrieve_kw(kw, 'nClusters',             None                   )
    getRates              = retrieve_kw(kw, 'getRates',              True                   )
    getRatesOnly          = retrieve_kw(kw, 'getRatesOnly',          False                  )
    etBins                = retrieve_kw(kw, 'etBins',                None                   )
    etaBins               = retrieve_kw(kw, 'etaBins',               None                   )
    crossVal              = retrieve_kw(kw, 'crossVal',              None                   )
    ringConfig            = retrieve_kw(kw, 'ringConfig',            100                    )
    extractDet            = retrieve_kw(kw, 'extractDet',            None                   )
    standardCaloVariables = retrieve_kw(kw, 'standardCaloVariables', False                  )
    useTRT                = retrieve_kw(kw, 'useTRT',                False                  )
    supportTriggers       = retrieve_kw(kw, 'supportTriggers',       True                   )
    monitoring            = retrieve_kw(kw, 'monitoring',            None                   )
    pileupRef             = retrieve_kw(kw, 'pileupRef',             NotSet                 )
    import ROOT, pkgutil
    #gROOT.ProcessLine (".x $ROOTCOREDIR/scripts/load_packages.C");
    #ROOT.gROOT.Macro('$ROOTCOREDIR/scripts/load_packages.C')
    if not( bool( pkgutil.find_loader( 'libTuningTools' ) ) and ROOT.gSystem.Load('libTuningTools') >= 0 ) and \
       not( bool( pkgutil.find_loader( 'libTuningToolsLib' ) ) and ROOT.gSystem.Load('libTuningToolsLib') >= 0 ):
        #ROOT.gSystem.Load('libTuningToolsPythonLib') < 0:
      self._fatal("Could not load TuningTools library", ImportError)

    if 'level' in kw: self.level = kw.pop('level')
    # and delete it to avoid mistakes:
    checkForUnusedVars( kw, self._warning )
    del kw
    ### Parse arguments
    # Mutual exclusive arguments:
    if not getRates and getRatesOnly:
      self._logger.error("Cannot run with getRates set to False and getRatesOnly set to True. Setting getRates to True.")
      getRates = True
    # Also parse operation, check if its type is string and if we can
    # transform it to the known operation enum:
    fList = csvStr2List ( fList )
    fList = expandFolders( fList )
    ringerOperation = RingerOperation.retrieve(ringerOperation)
    reference = Reference.retrieve(reference)
    if isinstance(l1EmClusCut, str):
      l1EmClusCut = float(l1EmClusCut)
    if l1EmClusCut:
      l1EmClusCut = 1000.*l1EmClusCut # Put energy in MeV
      __onlineBranches.append( 'trig_L1_emClus'  )
    if l2EtCut:
      l2EtCut = 1000.*l2EtCut # Put energy in MeV
      __onlineBranches.append( 'trig_L2_calo_et' )
    if efEtCut:
      efEtCut = 1000.*efEtCut # Put energy in MeV
      __onlineBranches.append( 'trig_EF_calo_et' )
    if offEtCut:
      offEtCut = 1000.*offEtCut # Put energy in MeV
      __offlineBranches.append( 'el_et' )
    if not supportTriggers:
      __onlineBranches.append( 'trig_L1_accept' )
    # Check if treePath is None and try to set it automatically
    if treePath is None:
      treePath = 'Offline/Egamma/Ntuple/electron' if ringerOperation < 0 else \
                 'Trigger/HLT/Egamma/TPNtuple/e24_medium_L1EM18VH'
    # Check whether using bins
    useBins=False; useEtBins=False; useEtaBins=False
    nEtaBins = 1; nEtBins = 1
    # Set the detector which we should extract the information:
    if extractDet is None:
      if ringerOperation < 0:
        extractDet = Detector.Calorimetry
      elif ringerOperation is RingerOperation.L2Calo:
        extractDet = Detector.Calorimetry
      elif ringerOperation is RingerOperation.L2:
        extractDet = Detector.Tracking
      else:
        extractDet = Detector.CaloAndTrack
    else:
      extractDet = Detector.retrieve( extractDet )

    if etaBins is None: etaBins = npCurrent.fp_array([])
    if type(etaBins) is list: etaBins=npCurrent.fp_array(etaBins)
    if etBins is None: etBins = npCurrent.fp_array([])
    if type(etBins) is list: etBins=npCurrent.fp_array(etBins)

    if etBins.size:
      etBins = etBins * 1000. # Put energy in MeV
      nEtBins  = len(etBins)-1
      if nEtBins >= np.iinfo(npCurrent.scounter_dtype).max:
        self._fatal(('Number of et bins (%d) is larger or equal than maximum '
            'integer precision can hold (%d). Increase '
            'TuningTools.coreDef.npCurrent scounter_dtype number of bytes.'), nEtBins,
            np.iinfo(npCurrent.scounter_dtype).max)
      # Flag that we are separating data through bins
      useBins=True
      useEtBins=True
      self._debug('E_T bins enabled.')

    if not type(ringConfig) is list and not type(ringConfig) is np.ndarray:
      ringConfig = [ringConfig] * (len(etaBins) - 1) if etaBins.size else 1
    if type(ringConfig) is list: ringConfig=npCurrent.int_array(ringConfig)
    if not len(ringConfig):
      self._fatal('Rings size must be specified.');

    if etaBins.size:
      nEtaBins = len(etaBins)-1
      if nEtaBins >= np.iinfo(npCurrent.scounter_dtype).max:
        self._fatal(('Number of eta bins (%d) is larger or equal than maximum '
            'integer precision can hold (%d). Increase '
            'TuningTools.coreDef.npCurrent scounter_dtype number of bytes.'), nEtaBins,
            np.iinfo(npCurrent.scounter_dtype).max)
      if len(ringConfig) != nEtaBins:
        self._fatal(('The number of rings configurations (%r) must be equal than '
                            'eta bins (%r) region config'),ringConfig, etaBins)
      useBins=True
      useEtaBins=True
      self._debug('eta bins enabled.')
    else:
      self._debug('eta/et bins disabled.')

    ### Prepare to loop:
    # Open root file
    t = ROOT.TChain(treePath)
    for inputFile in progressbar(fList, len(fList),
                                 logger = self._logger,
                                 prefix = "Creating collection tree "):

      # Check if file exists
      f  = ROOT.TFile.Open(inputFile, 'read')
      if not f or f.IsZombie():
        self._warning('Couldn''t open file: %s', inputFile)
        continue
      # Inform user whether TTree exists, and which options are available:
      self._debug("Adding file: %s", inputFile)
      obj = f.Get(treePath)
      if not obj:
        self._warning("Couldn't retrieve TTree (%s)!", treePath)
        self._info("File available info:")
        f.ReadAll()
        f.ReadKeys()
        f.ls()
        continue
      elif not isinstance(obj, ROOT.TTree):
        self._fatal("%s is not an instance of TTree!", treePath, ValueError)
      t.Add( inputFile )

    # Turn all branches off.
    t.SetBranchStatus("*", False)

    # RingerPhysVal hold the address of required branches
    event = ROOT.RingerPhysVal()

    # Add offline branches, these are always needed
    cPos = 0
    for var in __offlineBranches:
      self.__setBranchAddress(t,var,event)

    # Add online branches if using Trigger
    if ringerOperation > 0:
      for var in __onlineBranches:
        self.__setBranchAddress(t,var,event)


    ## Allocating memory for the number of entries
    entries = t.GetEntries()
    nobs = entries if (nClusters is None or nClusters > entries or nClusters < 1) \
                                                                else nClusters

    ## Retrieve the dependent operation variables:
    if useEtBins:
      etBranch = 'el_et' if ringerOperation < 0 else 'trig_L2_calo_et'
      self.__setBranchAddress(t,etBranch,event)
      self._debug("Added branch: %s", etBranch)
      if not getRatesOnly:
        npEt    = npCurrent.scounter_zeros(shape=npCurrent.shape(npat = 1, nobs = nobs))
        self._debug("Allocated npEt    with size %r", npEt.shape)

    if useEtaBins:
      etaBranch    = "el_eta" if ringerOperation < 0 else "trig_L2_calo_eta"
      self.__setBranchAddress(t,etaBranch,event)
      self._debug("Added branch: %s", etaBranch)
      if not getRatesOnly:
        npEta    = npCurrent.scounter_zeros(shape=npCurrent.shape(npat = 1, nobs = nobs))
        self._debug("Allocated npEta   with size %r", npEta.shape)

    # The base information holder, such as et, eta and pile-up
    if pileupRef is NotSet:
      if ringerOperation > 0:
        pileupRef = PileupReference.avgmu
      else:
        pileupRef = PileupReference.nvtx

    pileupRef = PileupReference.retrieve( pileupRef )

    self._info("Using '%s' as pile-up reference.", PileupReference.tostring( pileupRef ) )

    if pileupRef is PileupReference.nvtx:
      pileupBranch = 'el_nPileupPrimaryVtx'
      pileupDataType = np.uint16
    elif pileupRef is PileupReference.avgmu:
      pileupBranch = 'avgmu'
      pileupDataType = np.float32
    else:
      raise NotImplementedError("Pile-up reference %r is not implemented." % pileupRef)
    baseInfoBranch = BaseInfo((etBranch, etaBranch,  pileupBranch, 'el_phi' if ringerOperation < 0 else 'trig_L2_el_phi',),
                              (npCurrent.fp_dtype, npCurrent.fp_dtype, npCurrent.fp_dtype, pileupDataType) )
    baseInfo = [None, ] * baseInfoBranch.nInfo

    # Make sure all baseInfoBranch information is available:
    for idx in baseInfoBranch:
      self.__setBranchAddress(t,baseInfoBranch.retrieveBranch(idx),event)

    # Allocate numpy to hold as many entries as possible:
    if not getRatesOnly:
      # Retrieve the rings information depending on ringer operation
      ringerBranch = "el_ringsE" if ringerOperation < 0 else \
                     "trig_L2_calo_rings"
      self.__setBranchAddress(t,ringerBranch,event)
      if ringerOperation > 0:
        if ringerOperation is RingerOperation.L2:
          for var in __l2trackBranches:
            self.__setBranchAddress(t,var,event)
      if standardCaloVariables:
        if ringerOperation in (RingerOperation.L2, RingerOperation.L2Calo,):
          for var in __l2stdCaloBranches:
            self.__setBranchAddress(t, var, event)
        else:
          self._warning("Unknown standard calorimeters for Operation:%s. Setting operation back to use rings variables.",
                               RingerOperation.tostring(ringerOperation))
      t.GetEntry(0)
      npat = 0
      if extractDet in (Detector.Calorimetry,
                        Detector.CaloAndTrack,
                        Detector.All):
        if standardCaloVariables:
          npat+= 6
        else:
          npat += ringConfig.max()
      if extractDet in (Detector.Tracking,
                       Detector.CaloAndTrack,
                       Detector.All):
        if ringerOperation is RingerOperation.L2:
          if useTRT:
            self._info("Using TRT information!")
            npat += 2
            __l2trackBranches.append('trig_L2_el_nTRTHits')
            __l2trackBranches.append('trig_L2_el_nTRTHiThresholdHits')
          npat += 3
          for var in __l2trackBranches:
            self.__setBranchAddress(t,var,event)
          self.__setBranchAddress(t,"trig_L2_el_pt",event)
        elif ringerOperation < 0: # Offline
          self._warning("Still need to implement tracking for the ringer offline.")
      npPatterns = npCurrent.fp_zeros( shape=npCurrent.shape(npat=npat, #getattr(event, ringerBranch).size()
                                                   nobs=nobs)
                                     )
      self._debug("Allocated npPatterns with size %r", npPatterns.shape)

      # Add E_T, eta and luminosity information
      npBaseInfo = [npCurrent.zeros( shape=npCurrent.shape(npat=1, nobs=nobs ), dtype=baseInfoBranch.dtype(idx) )
                                    for idx in baseInfoBranch]
    else:
      npPatterns = npCurrent.fp_array([])
      npBaseInfo = [deepcopy(npCurrent.fp_array([])) for _ in baseInfoBranch]

    ## Allocate the branch efficiency collectors:
    if getRates:
      if ringerOperation < 0:
        benchmarkDict = OrderedDict(
          [(  RingerOperation.Offline_CutBased_Loose  , 'el_loose'            ),
           (  RingerOperation.Offline_CutBased_Medium , 'el_medium'           ),
           (  RingerOperation.Offline_CutBased_Tight  , 'el_tight'            ),
           (  RingerOperation.Offline_LH_Loose        , 'el_lhLoose'          ),
           (  RingerOperation.Offline_LH_Medium       , 'el_lhMedium'         ),
           (  RingerOperation.Offline_LH_Tight        , 'el_lhTight'          ),
          ])
      else:
        benchmarkDict = OrderedDict(
          [( RingerOperation.L2Calo                  , 'trig_L2_calo_accept' ),
           ( RingerOperation.L2                      , 'trig_L2_el_accept'   ),
           ( RingerOperation.EFCalo                  , 'trig_EF_calo_accept' ),
           ( RingerOperation.HLT                     , 'trig_EF_el_accept'   ),
          ])


      from TuningTools.CreateData import BranchEffCollector, BranchCrossEffCollector
      branchEffCollectors = OrderedDict()
      branchCrossEffCollectors = OrderedDict()
      for key, val in benchmarkDict.iteritems():
        branchEffCollectors[key] = list()
        branchCrossEffCollectors[key] = list()
        # Add efficincy branch:
        if getRates or getRatesOnly:
          self.__setBranchAddress(t,val,event)
        for etBin in range(nEtBins):
          if useBins:
            branchEffCollectors[key].append(list())
            branchCrossEffCollectors[key].append(list())
          for etaBin in range(nEtaBins):
            etBinArg = etBin if useBins else -1
            etaBinArg = etaBin if useBins else -1
            argList = [ RingerOperation.tostring(key), val, etBinArg, etaBinArg ]
            branchEffCollectors[key][etBin].append(BranchEffCollector( *argList ) )
            if crossVal:
              branchCrossEffCollectors[key][etBin].append(BranchCrossEffCollector( entries, crossVal, *argList ) )
          # etBin
        # etaBin
      # benchmark dict
      if self._logger.isEnabledFor( LoggingLevel.DEBUG ):
        self._debug( 'Retrieved following branch efficiency collectors: %r',
            [collector[0].printName for collector in traverse(branchEffCollectors.values())])
    # end of (getRates)

    etaBin = 0; etBin = 0
    step = int(entries/100) if int(entries/100) > 0 else 1
    ## Start loop!
    self._info("There is available a total of %d entries.", entries)

    for entry in progressbar(range(entries), entries,
                             step = step, logger = self._logger,
                             prefix = "Looping over entries "):

      #self._verbose('Processing eventNumber: %d/%d', entry, entries)
      t.GetEntry(entry)

      # Check if it is needed to remove energy regions (this means that if not
      # within this range, it will be ignored as well for efficiency measuremnet)
      if event.el_et < offEtCut:
        self._verbose("Ignoring entry due to offline E_T cut.")
        continue
      # Add et distribution for all events

      if not monitoring is None:
        # Book all distribtions before the event selection
        self.__fillHistograms(monitoring,filterType,event,False)

      if ringerOperation > 0:
        # Remove events which didn't pass L1_calo
        if not supportTriggers and not event.trig_L1_accept:
          #self._verbose("Ignoring entry due to L1Calo cut (trig_L1_accept = %r).", event.trig_L1_accept)
          continue
        if event.trig_L1_emClus  < l1EmClusCut:
          #self._verbose("Ignoring entry due to L1Calo E_T cut (%d < %r).", event.trig_L1_emClus, l1EmClusCut)
          continue
        if event.trig_L2_calo_et < l2EtCut:
          #self._verbose("Ignoring entry due to L2Calo E_T cut.")
          continue
        if  efEtCut is not None and event.trig_L2_calo_accept :
          # EF calo is a container, search for electrons objects with et > cut
          trig_EF_calo_et_list = stdvector_to_list(event.trig_EF_calo_et)
          found=False
          for v in trig_EF_calo_et_list:
            if v < efEtCut:  found=True
          if found:
            #self._verbose("Ignoring entry due to EFCalo E_T cut.")
            continue

      # Set discriminator target:
      target = Target.Unknown
      if reference is Reference.Truth:
        if event.mc_isElectron and event.mc_hasZMother:
          target = Target.Signal
        elif not (event.mc_isElectron and (event.mc_hasZMother or event.mc_hasWMother) ):
          target = Target.Background
      elif reference is Reference.Off_Likelihood:
        if event.el_lhTight: target = Target.Signal
        elif not event.el_lhLoose: target = Target.Background
      elif reference is Reference.AcceptAll:
        target = Target.Signal if filterType is FilterType.Signal else Target.Background
      else:
        if event.el_tight: target = Target.Signal
        elif not event.el_loose: target = Target.Background

      # Run filter if it is defined
      if filterType and \
         ( (filterType is FilterType.Signal and target != Target.Signal) or \
           (filterType is FilterType.Background and target != Target.Background) or \
           (target == Target.Unknown) ):
        #self._verbose("Ignoring entry due to filter cut.")
        continue

      # Add et distribution for all events
      if not monitoring is None:
        # Book all distributions after the event selection
        self.__fillHistograms(monitoring,filterType,event,True)

      # Retrieve base information:
      for idx in baseInfoBranch:
        lInfo = getattr(event, baseInfoBranch.retrieveBranch(idx))
        baseInfo[idx] = lInfo
        if not getRatesOnly: npBaseInfo[idx][cPos] = lInfo
      # Retrieve dependent operation region
      if useEtBins:
        etBin  = self.__retrieveBinIdx( etBins, baseInfo[0] )
      if useEtaBins:
        etaBin = self.__retrieveBinIdx( etaBins, np.fabs( baseInfo[1]) )


      # Check if bin is within range (when not using bins, this will always be true):
      if (etBin < nEtBins and etaBin < nEtaBins):
        # Retrieve patterns:
        if not getRatesOnly:
          if useEtBins:  npEt[cPos] = etBin
          if useEtaBins: npEta[cPos] = etaBin
          ## Retrieve calorimeter information:
          cPat = 0
          caloAvailable = True
          if extractDet in (Detector.Calorimetry,
                           Detector.CaloAndTrack,
                           Detector.All):
            if standardCaloVariables:
              patterns = []
              if ringerOperation is RingerOperation.L2Calo:
                from math import cosh
                cosh_eta = cosh( event.trig_L2_calo_eta )
                # second layer ratio between 3x7 7x7
                rEta = event.trig_L2_calo_e237 / event.trig_L2_calo_e277
                base = event.trig_L2_calo_emaxs1 + event.trig_L2_calo_e2tsts1
                # Ratio between first and second highest energy cells
                eRatio = ( event.trig_L2_calo_emaxs1 - event.trig_L2_calo_e2tsts1 ) / base if base > 0 else 0
                # ratio of energy in the first layer (hadronic particles should leave low energy)
                F1 = event.trig_L2_calo_fracs1 / ( event.trig_L2_calo_et * cosh_eta )
                # weta2 is calculated over the middle layer using 3 x 5
                weta2 = event.trig_L2_calo_weta2
                # wstot is calculated over the first layer using (typically) 20 strips
                wstot = event.trig_L2_calo_wstot
                # ratio between EM cluster and first hadronic layers:
                Rhad1 = ( event.trig_L2_calo_ehad1 / cosh_eta ) / event.trig_L2_calo_et
                # allocate patterns:
                patterns = [rEta, eRatio, F1, weta2, wstot, Rhad1]
                for pat in patterns:
                  npPatterns[npCurrent.access( pidx=cPat, oidx=cPos) ] = pat
                  cPat += 1
              # end of ringerOperation
            else:
              # Remove events without rings
              if getattr(event,ringerBranch).empty():
                caloAvailable = False
              # Retrieve rings:
              if caloAvailable:
                try:
                  patterns = stdvector_to_list( getattr(event,ringerBranch) )
                  lPat = len(patterns)
                  if lPat == ringConfig[etaBin]:
                    npPatterns[npCurrent.access(pidx=slice(cPat,ringConfig[etaBin]),oidx=cPos)] = patterns
                  else:
                    oldEtaBin = etaBin
                    if etaBin > 0 and ringConfig[etaBin - 1] == lPat:
                      etaBin -= 1
                    elif etaBin + 1 < len(ringConfig) and ringConfig[etaBin + 1] == lPat:
                      etaBin += 1
                    npPatterns[npCurrent.access(pidx=slice(cPat, ringConfig[etaBin]),oidx=cPos)] = patterns
                    self._warning(("Recovered event which should be within eta bin (%d: %r) "
                                          "but was found to be within eta bin (%d: %r). "
                                          "Its read eta value was of %f."),
                                          oldEtaBin, etaBins[oldEtaBin:oldEtaBin+2],
                                          etaBin, etaBins[etaBin:etaBin+2],
                                          np.fabs( getattr(event,etaBranch)))
                except ValueError:
                  self._logger.error(("Patterns size (%d) do not match expected "
                                    "value (%d). This event eta value is: %f, and ringConfig is %r."),
                                    lPat, ringConfig[etaBin], np.fabs( getattr(event,etaBranch)), ringConfig
                                    )
                  continue
              else:
                if extractDet is Detector.Calorimetry:
                  # Also display warning when extracting only calorimetry!
                  self._warning("Rings not available")
                  continue
                self._warning("Rings not available")
                continue
              cPat += ringConfig.max()
            # which calo variables
          # end of (extractDet needed calorimeter)
          # And track information:
          if extractDet in (Detector.Tracking,
                           Detector.CaloAndTrack,
                           Detector.All):
            if caloAvailable or extractDet is Detector.Tracking:
              if ringerOperation is RingerOperation.L2:
                # Retrieve nearest deta/dphi only, so we need to find each one is the nearest:
                if event.trig_L2_el_trkClusDeta.size():
                  clusDeta = npCurrent.fp_array( stdvector_to_list( event.trig_L2_el_trkClusDeta ) )
                  clusDphi = npCurrent.fp_array( stdvector_to_list( event.trig_L2_el_trkClusDphi ) )
                  bestTrackPos = int( np.argmin( clusDeta**2 + clusDphi**2 ) )
                  for var in __l2trackBranches:
                    npPatterns[npCurrent.access( pidx=cPat,oidx=cPos) ] = getattr(event, var)[bestTrackPos]
                    cPat += 1
                else:
                  #self._verbose("Ignoring entry due to track information not available.")
                  continue
                  #for var in __l2trackBranches:
                  #  npPatterns[npCurrent.access( pidx=cPat,oidx=cPos) ] = np.nan
                  #  cPat += 1
              elif ringerOperation < 0: # Offline
                pass
            # caloAvailable or only tracking
          # end of (extractDet needs tracking)
        # end of (getRatesOnly)

        ## Retrieve rates information:
        if getRates:
          for branch in branchEffCollectors.itervalues():
            if not useBins:
              branch.update(event)
            else:
              branch[etBin][etaBin].update(event)
          if crossVal:
            for branchCross in branchCrossEffCollectors.itervalues():
              if not useBins:
                branchCross.update(event)
              else:
                branchCross[etBin][etaBin].update(event)
        # end of (getRates)

        # We only increment if this cluster will be computed
        cPos += 1
      # end of (et/eta bins)

      # Limit the number of entries to nClusters if desired and possible:
      if not nClusters is None and cPos >= nClusters:
        break
    # for end

    ## Treat the rings information
    if not getRatesOnly:

      ## Remove not filled reserved memory space:
      if npPatterns.shape[npCurrent.odim] > cPos:
        npPatterns = np.delete( npPatterns, slice(cPos,None), axis = npCurrent.odim)

      ## Segment data over bins regions:
      # Also remove not filled reserved memory space:
      if useEtBins:
        npEt  = npCurrent.delete( npEt, slice(cPos,None))
      if useEtaBins:
        npEta = npCurrent.delete( npEta, slice(cPos,None))
      # Treat
      npObject = self.treatNpInfo(cPos, npEt, npEta, useEtBins, useEtaBins,
                                  nEtBins, nEtaBins, standardCaloVariables, ringConfig,
                                  npPatterns, )
      data = [self.treatNpInfo(cPos, npEt, npEta, useEtBins, useEtaBins,
                                                      nEtBins, nEtaBins, standardCaloVariables, ringConfig,
                                                      npData) for npData in npBaseInfo]
      npBaseInfo = npCurrent.array( data, dtype=np.object )
    else:
      npObject = npCurrent.array([], dtype=npCurrent.dtype)
    # not getRatesOnly

    if getRates:
      if crossVal:
        for etBin in range(nEtBins):
          for etaBin in range(nEtaBins):
            for branchCross in branchCrossEffCollectors.itervalues():
              if not useBins:
                branchCross.finished()
              else:
                branchCross[etBin][etaBin].finished()

      # Print efficiency for each one for the efficiency branches analysed:
      for etBin in range(nEtBins) if useBins else range(1):
        for etaBin in range(nEtaBins) if useBins else range(1):
          for branch in branchEffCollectors.itervalues():
            lBranch = branch if not useBins else branch[etBin][etaBin]
            self._info('%s',lBranch)
          if crossVal:
            for branchCross in branchCrossEffCollectors.itervalues():
              lBranchCross = branchCross if not useBins else branchCross[etBin][etaBin]
              lBranchCross.dump(self._debug, printSort = True,
                                 sortFcn = self._verbose)
          # for branch
        # for eta
      # for et
    # end of (getRates)

    outputs = []
    #if not getRatesOnly:
    outputs.extend((npObject, npBaseInfo))
    #if getRates:
    outputs.extend((branchEffCollectors, branchCrossEffCollectors))
    #outputs = tuple(outputs)
    return outputs
Exemplo n.º 14
0
    def __init__(self, **kw):
        Logger.__init__(self, kw)
        printArgs(kw, self._debug)
        self._nSorts = None
        self._nBoxes = None
        self._nTrain = None
        self._nValid = None
        self._nTest = None
        self._seed = None
        self._method = CrossValidMethod.retrieve(
            retrieve_kw(kw, 'method', CrossValidMethod.Standard))

        if self._method is CrossValidMethod.Standard:
            self._nSorts = retrieve_kw(kw, 'nSorts', 50)
            self._nBoxes = retrieve_kw(kw, 'nBoxes', 10)
            self._nTrain = retrieve_kw(kw, 'nTrain', 6)
            self._nValid = retrieve_kw(kw, 'nValid', 4)
            self._nTest = retrieve_kw(
                kw, 'nTest', self._nBoxes - (self._nTrain + self._nValid))
            self._seed = retrieve_kw(kw, 'seed', None)
            checkForUnusedVars(kw, self._warning)
            # Check if variables are ok:
            if (not self._nTest is None) and self._nTest < 0:
                self._fatal("Number of test clusters is lesser than zero",
                            ValueError)
            totalSum = self._nTrain + self._nValid + (self._nTest) if self._nTest else \
                       self._nTrain + self._nValid
            if totalSum != self._nBoxes:
                self._fatal(
                    "Sum of train, validation and test boxes doesn't match.",
                    ValueError)

            np.random.seed(self._seed)

            # Test number of possible combinations (N!/((N-K)!(K)!) is greater
            # than the required sorts. If number of sorts (greater than half of the
            # possible cases) is close to the number of combinations, generate all
            # possible combinations and then gather the number of needed sorts.
            # However, as calculating factorial can be heavy, we don't do this if the
            # number of boxes is large.
            self._sort_boxes_list = []
            useRandomCreation = True
            from math import factorial
            if self._nBoxes < 201:
                totalPossibilities = ( factorial( self._nBoxes ) ) / \
                    ( factorial( self._nTrain ) * \
                      factorial( self._nValid ) * \
                      factorial( self._nTest  ) )
                if self._nSorts > (totalPossibilities / 2):
                    useRandomCreation = False
            if useRandomCreation:
                count = 0
                while True:
                    random_boxes = np.random.permutation(self._nBoxes)
                    random_boxes = tuple(
                        chain(
                            sorted(random_boxes[0:self._nTrain]),
                            sorted(random_boxes[self._nTrain:self._nTrain +
                                                self._nValid]),
                            sorted(random_boxes[self._nTrain +
                                                self._nValid:])))
                    # Make sure we are not appending same sort again:
                    if not random_boxes in self._sort_boxes_list:
                        self._sort_boxes_list.append(random_boxes)
                        count += 1
                        if count == self._nSorts:
                            break
            else:
                self._sort_boxes_list = list(
                    combinations_taken_by_multiple_groups(
                        range(self._nBoxes),
                        (self._nTrain, self._nValid, self._nTest)))
                for i in range(totalPossibilities - self._nSorts):
                    self._sort_boxes_list.pop(
                        np.random.random_integers(0, totalPossibilities))
        elif self._method is CrossValidMethod.JackKnife:
            self._nBoxes = retrieve_kw(kw, 'nBoxes', 10)
            checkForUnusedVars(kw, self._warning)
            self._nSorts = self._nBoxes
            self._nTrain = self._nBoxes - 1
            self._nValid = 1
            self._nTest = 0
            self._sort_boxes_list = list(
                combinations_taken_by_multiple_groups(range(self._nBoxes), (
                    9,
                    1,
                )))
        elif self._method is CrossValidMethod.StratifiedKFold:
            self._nBoxes = retrieve_kw(kw, 'nBoxes', 10)
            self._shuffle = retrieve_kw(kw, 'shuffle', False)
            checkForUnusedVars(kw, self._logger.warning)
            self._nSorts = self._nBoxes
            self._nTrain = self._nBoxes - 1
            self._nValid = 1
            self._nTest = 0
Exemplo n.º 15
0
  def plot(self, **kw):

    dirname      = retrieve_kw(kw, 'dirname'     , 'correction'   )
    pdftitle     = retrieve_kw(kw, 'pdftitle'    , 'Distributions')
    pdfoutput    = retrieve_kw(kw, 'pdfoutput'   , 'distributions')
    atlaslabel   = retrieve_kw(kw, 'atlaslabel'  , 'Internal'     )
    selectorDict = retrieve_kw(kw, 'selectorDict', None           )
    dovertical   = retrieve_kw(kw, 'dovertical'  , False          )

    import os
    # Organize outputs (.py and .pdf)
    from datetime import datetime
    localpath = os.getcwd()+'/'+dirname
    
    try:
      if not os.path.exists(localpath):
        os.makedirs(localpath)
    except:
      self._logger.warning('The director %s exist.', localpath)

    # create skeleton
    thrDict = {'version':1, 'type': ['Hypo']  , 'date':0, 'metadata':dict(), 'tuning':dict(), 'name':[self._alias]}
    thrDict['metadata'] = self._metadata

    self._logger.info('Applying correction...')
    for pidname, pair in self._algDictNames.iteritems():
      # get the real reference values
      refValues = self.__retrieveTargetValues(pidname)
      algname = pair[0]
      tgtname = pair[1]
      # retrive the correct et/eta information

      if selectorDict: # retrieve from the expert input
        try:
          selector = selectorDict[algname]
        except KeyError:
          self._logger.fatal('Can not retrieve the algname %s',algname)
      else: # Retrieve from the framework
        if self._doTrigger: # Online
          obj = self.retrieve( "FastCalo" ); selector=obj.getDecor(algname+'_selector')
        else: # Offline
          obj = self.retrieve( "Electron" ); selector=obj.getDecor(algname+'_selector')
      
      # create new grid of thresholds objects
      selector.reset( self._thresholdEtBins, self._thresholdEtaBins )
      useNoActivationFunctionInTheLastLayer=selector.useNoActivationFunctionInTheLastLayer
      
      for etBinIdx in range( len(self._thresholdEtBins)-1 ):
        for etaBinIdx in range( len(self._thresholdEtaBins)-1 ):
          binningname = ('et%d_eta%d') % (etBinIdx,etaBinIdx)
          ref = refValues[etBinIdx][etaBinIdx]
          # relax detection if needed
          ref = ref + ((1-ref)*self._percentage[pidname])
          # Retrive histograms
          if self._doTrigger:
          	path = self._basepath+'/probes/'+pidname+'/'+algname+'/'+binningname
          	sgnHist2D = self.storeSvc().histogram(path+'/discriminantVsMu')
          	path = self._basepath+'/fakes/'+pidname+'/'+algname+'/'+binningname
          	bkgHist2D = self.storeSvc().histogram(path+'/discriminantVsMu')
          else:					
          	path = self._basepath+'/probes/'+pidname+'/'+algname+'/'+binningname
          	sgnHist2D = self.storeSvc().histogram(path+'/discriminantVsNvtx')
          	path = self._basepath+'/fakes/'+pidname+'/'+algname+'/'+binningname
          	bkgHist2D = self.storeSvc().histogram(path+'/discriminantVsNvtx')
   
          plotname = localpath+'/eff_corr_'+algname+'_'+binningname
          b, a , b0 = self.__applyThresholdCorrection(ref,sgnHist2D.Clone(),bkgHist2D.Clone(),binningname, plotname, 
                                                      draw=True , 
                                                      xname = '<#mu>' if self._doTrigger else 'N_{vtx}',
                                                      useNoActivationFunctionInTheLastLayer=useNoActivationFunctionInTheLastLayer,
                                                      limits = self._limits, dolumi=False, 
                                                      # FIXME: need to add spaces here. Fix in AtlasStyle for future
                                                      atlaslabel = '      '+atlaslabel,
                                                      dovertical = dovertical
                                                      )
          selector.configureThresholdParam(binningname, a, b, b0) 
      # Hold the thresholds objects
      thrDict['tuning'][selector.pidname] = {}
      thresholds = selector.thresholds
      for key in sorted(thresholds.keys()):
        thrDict['tuning'][selector.pidname][key] = thresholds[key].toRaw()

    # Create the athena/python conf file
    name=localpath+'/TrigL2CaloRingerThresholds.py'
    pyfile = open(name,'w')
    pyfile.write('def ThresholdsMap():\n')
    pyfile.write('  s=dict()\n')
    for key in thrDict.keys():
      pyfile.write('  s["%s"]=%s\n' % (key, thrDict[key]))
    pyfile.write('  return s\n')


    from RingerCore.tex.BeamerAPI import BeamerTexReportTemplate1,BeamerSection,BeamerMultiFigureSlide,BeamerFigureSlide
   
    if not dovertical:
      self._logger.info('Do pdf maker...')
      # Slide maker
      with BeamerTexReportTemplate1( theme = 'Berlin'
                                   , _toPDF = True
                                   , title = pdftitle
                                   , outputFile = pdfoutput
                                   , font = 'structurebold' ):

   
        for pidname, pair in self._algDictNames.iteritems():
          factor = self._percentage[pidname]
          with BeamerSection( name = pidname ):
            # get the real reference values
            refValues = self.__retrieveTargetValues(pidname)
            algname = pair[0]
            tgtname = pair[1]
            for etBinIdx in range( len(self._thresholdEtBins)-1 ):
              for etaBinIdx in range( len(self._thresholdEtaBins)-1 ):
                binningname = ('et%d_eta%d') % (etBinIdx,etaBinIdx)
                plotname = localpath+'/eff_corr_'+algname+'_'+binningname+'.pdf'
                BeamerMultiFigureSlide( title = ("%s, factor = %1.2f, (%s)")%(algname.replace('_','\_'), 
                                                factor ,binningname.replace('_','\_') )
                              , paths = [plotname]
                              , nDivWidth = 1 # x
                              , nDivHeight = 1 # y
                              , texts=None
                              , fortran = False
                              , usedHeight = 0.8
                              , usedWidth = 1.1
                              )
 



    return StatusCode.SUCCESS 
Exemplo n.º 16
0
  def __applyThresholdCorrection( self, refValue, sgn_hist2D, bkg_hist2D, partition_name, output_name, **kwargs):

    legend_position = retrieve_kw( kwargs, 'legend_position', (0.36,0.20,0.66,0.40))
    useNoActivationFunctionInTheLastLayer=retrieve_kw(kwargs,'useNoActivationFunctionInTheLastLayer',False)
    xname           = retrieve_kw( kwargs, 'xname', 'n_{vtx}' )
    draw            = retrieve_kw( kwargs, 'draw', False)
    limits          = retrieve_kw( kwargs, 'limits', [0,10,20])
    dovertical      = retrieve_kw( kwargs, 'dovertical'  , False)
    
    mumin = limits[0]; mumax=limits[-1]
    mubins = mumax-mumin
    from TrigEgammaDevelopments.helper.util import *
    sgn_hist2D = copy2DRegion(sgn_hist2D,1000,-12,7,mubins,mumin,mumax)
    bkg_hist2D = copy2DRegion(bkg_hist2D,1000,-12,7,mubins,mumin,mumax)



    from copy import deepcopy
    refValue_requested = refValue 
    false_alarm = 1.0
    false_alarm_limit = 0.20
    while false_alarm > false_alarm_limit:
      # Calculate the original threshold
      b0, error = find_threshold(sgn_hist2D.ProjectionX(), refValue )
      # Take eff points using uncorrection threshold
      discr_points, nvtx_points, error_points = calculate_dependent_discr_points(sgn_hist2D , refValue )
      # Calculate eff without correction
      sgn_histNum, sgn_histDen, sgn_histEff, det0   = calculate_efficiency(sgn_hist2D, refValue, b0, 0,  doCorrection=False)
      # Generate correction parameters and produce fixed plots
      sgn_histNum_corr, sgn_histDen_corr, sgn_histEff_corr, detection ,b, a = calculate_efficiency( sgn_hist2D, 
                                                          refValue, b0, 0, limits = limits, doCorretion=True)
      
      # Calculate eff without correction
      bkg_histNum, bkg_histDen, bkg_histEff, _  = calculate_efficiency(bkg_hist2D, refValue, b0, 0,  doCorrection=False)
      # Calculate eff using the correction from signal
      bkg_histNum_corr, bkg_histDen_corr, bkg_histEff_corr, false_alarm = calculate_efficiency(bkg_hist2D, refValue, b, a,  doCorrection=False)

      if false_alarm > false_alarm_limit:
        refValue-=0.025

    # To np.array
    discr_points = np.array(discr_points)

    # Plot correction 
    if draw:
      # Retrieve some usefull information
      y_max = sgn_hist2D.GetYaxis().GetXmax()
      y_min = sgn_hist2D.GetYaxis().GetXmin()
      x_min = y_min; x_max = y_max

      from ROOT import TCanvas, gStyle, TLegend, kRed, kBlue, kBlack,TLine
      from ROOT import TGraphErrors,TF1
      gStyle.SetPalette(107)
      if dovertical:
        canvas = TCanvas('canvas','canvas',1600,2000)
        canvas.Divide(2,3)
      else:
        canvas = TCanvas('canvas','canvas',2800,1600)
        canvas.Divide(3,2)

      pad1= canvas.cd(1)
      sgn_histEff.SetTitle('Signal Efficiency in: '+partition_name)
      sgn_histEff.SetLineColor(kRed)
      sgn_histEff.SetMarkerColor(kRed)
      sgn_histEff.GetYaxis().SetTitle('#epsilon('+xname+')')
      sgn_histEff.GetXaxis().SetTitle(xname)
      sgn_histEff.GetYaxis().SetRangeUser( 0.6, 1.1 ) 
      sgn_histEff.Draw()
      sgn_histEff_corr.SetLineColor(kBlack)
      sgn_histEff_corr.SetMarkerColor(kBlack)
      sgn_histEff_corr.Draw('sames')


      l0 = TLine(x_min,refValue_requested,x_max,refValue_requested)
      l0.SetLineColor(kBlack)
      l0.Draw()
 
      l1 = TLine(x_min,refValue,x_max,refValue)
      l1.SetLineColor(kBlack)
      l1.SetLineStyle(9)
      l1.Draw()
      
      leg1 = TLegend(legend_position[0],legend_position[1], legend_position[2],legend_position[3])
      setLegend1(leg1)
      leg1.SetHeader('Signal efficiency in '+partition_name)
      leg1.AddEntry(sgn_histEff,('Old: d = %1.3f')%(b0),'p' )
      lg1 = leg1.AddEntry(sgn_histEff_corr,('New: d = %1.3f + %s %1.3f')%(b,xname,a),'p' )
      lg1.SetTextColor(kRed)
      leg1.AddEntry(l1,('Reference: %1.3f')%(refValue) ,'l')

      leg1.SetTextSize(0.03)
      leg1.SetBorderSize(0)
      leg1.Draw()
      atlas_template(pad1,**kwargs)

      pad2 = canvas.cd(2) if dovertical else canvas.cd(4)

      bkg_histEff.SetTitle('Background rejection in: '+partition_name)
      bkg_histEff.SetLineColor(kRed)
      bkg_histEff.SetMarkerColor(kRed)
      bkg_histEff.GetYaxis().SetTitle('#epsilon('+xname+')')
      bkg_histEff.GetXaxis().SetTitle(xname)
      bkg_histEff.Draw()
      bkg_histEff_corr.SetLineColor(kBlack)
      bkg_histEff_corr.SetMarkerColor(kBlack)
      bkg_histEff_corr.Draw('sames')
      l0.Draw()
      l1.Draw()
      leg2 = TLegend(legend_position[0],legend_position[1]+0.4, legend_position[2],legend_position[3]+0.4)
      setLegend1(leg2)
      leg2.SetHeader('Background rejection in '+partition_name)
      leg2.AddEntry(sgn_histEff,('Old: d = %1.3f')%(b0),'p' )
      lg2 = leg2.AddEntry(sgn_histEff_corr,('New: d = %1.3f + %s %1.3f')%(b,xname,a),'p' )
      lg2.SetTextColor(kRed)
      leg2.SetTextSize(0.03)
      leg2.SetBorderSize(0)
      leg2.Draw()
      atlas_template(pad2,**kwargs)

      pad3 = canvas.cd(3) if dovertical else canvas.cd(2)
      sgn_hist2D.SetTitle('Neural Network output as a function fo nvtx, '+partition_name)
      sgn_hist2D.GetXaxis().SetTitle('Neural Network output (Discriminant)')
      sgn_hist2D.GetYaxis().SetTitle(xname)
      sgn_hist2D.GetZaxis().SetTitle('')
      if not useNoActivationFunctionInTheLastLayer: sgn_hist2D.SetAxisRange(-1,1, 'X' )
      sgn_hist2D.Draw('colz')
      pad3.SetLogz()
      # Add points

      
      g1 = TGraphErrors(len(discr_points), discr_points, np.array(nvtx_points)+limits[0], np.array(error_points), np.zeros(discr_points.shape))
      g1.SetLineWidth(1)
      g1.SetLineColor(kBlue)
      g1.SetMarkerColor(kBlue) 
      g1.Draw("P same")
      # Old threshold line
      l2 = TLine(b0,y_min,b0,y_max)
      l2.SetLineColor(kRed)
      l2.SetLineWidth(2)
      l2.Draw()
      # New threshold line
      l3 = TLine(b,y_min, a*y_max+b, y_max)
      l3.SetLineColor(kBlack)
      l3.SetLineWidth(2)
      l3.Draw()

      atlas_template(pad3,**kwargs)

      pad4 = canvas.cd(4) if dovertical else canvas.cd(5)
      bkg_hist2D.SetTitle('Neural Network output as a function fo nvtx, '+partition_name)
      bkg_hist2D.GetXaxis().SetTitle('Neural Network output (Discriminant)')
      bkg_hist2D.GetYaxis().SetTitle(xname)
      bkg_hist2D.GetZaxis().SetTitle('')
      if not useNoActivationFunctionInTheLastLayer: bkg_hist2D.SetAxisRange(-1,1, 'X' )
      #sgn_hist2D.SetAxisRange(b+y_max*a-0.2,1, 'X' )
      bkg_hist2D.Draw('colz')
      pad4.SetLogz()
      # Add points
      g2 = TGraphErrors(len(discr_points), discr_points, np.array(nvtx_points)+limits[0], np.array(error_points), np.zeros(discr_points.shape))
      g2.SetLineWidth(1)
      g2.SetLineColor(kBlue)
      g2.SetMarkerColor(kBlue) 
      g2.Draw("P same")
      # Old threshold line
      l4 = TLine(b0,y_min,b0,y_max)
      l4.SetLineColor(kRed)
      l4.SetLineWidth(2)
      l4.Draw()
      # New threshold line
      l5 = TLine(b,y_min, a*y_max+b, y_max)
      l5.SetLineColor(kBlack)
      l5.SetLineWidth(2)
      l5.Draw()
      atlas_template(pad4,**kwargs)
      from ROOT import TH1D, kAzure
      from TrigEgammaDevelopments.plots import AutoFixAxes
      pad5 = canvas.cd(5) if dovertical else canvas.cd(3)
      #pad5.SetLogy()
      h5 = TH1D(sgn_hist2D.ProjectionX())
      h6 = TH1D(bkg_hist2D.ProjectionX())
      if max(h5.GetMaximum(),h6.GetMaximum()) > 10*(min(h5.GetMaximum(),h6.GetMaximum())):
        pad5.SetLogy()

      h5.Rebin(10)
      h6.Rebin(10)
      h5.SetLineColor(kAzure+6)
      h5.SetFillColor(kAzure+6)
      h6.SetLineColor(kRed-7)
      h5.Draw()
      h6.Draw('sames')
      AutoFixAxes(pad5,False,False,1.5)
      atlas_template(pad5,**kwargs)

      pad6 = canvas.cd(6)
      #pad6.SetLogy()
      h7 = TH1D(bkg_hist2D.ProjectionX())
      h8 = TH1D(sgn_hist2D.ProjectionX())
      if max(h7.GetMaximum(),h8.GetMaximum()) > 10*(min(h7.GetMaximum(),h8.GetMaximum())):
        pad6.SetLogy()
      
      h7.Rebin(10)
      h8.Rebin(10)
      h7.SetLineColor(kRed-7)
      h7.SetFillColor(kRed-7)
      h8.SetLineColor(kAzure+6)
      h7.Draw()
      h8.Draw('sames')
      AutoFixAxes(pad6,False,False,1.5)
      atlas_template(pad6,**kwargs)
      canvas.SaveAs(output_name+'.pdf')    

    return b,a,b0
Exemplo n.º 17
0
    def __plot_profiles_in_same_canvas(self, hist_objs, hist_names, legends,
                                       y_limits, **kwargs):

        legend_position = retrieve_kw(kwargs, 'legend_position',
                                      (0.36, 0.20, 0.66, 0.36))
        legend_prefix = retrieve_kw(kwargs, 'legend_prefix',
                                    'Z#rightarrowee, ')
        legend_header = retrieve_kw(kwargs, 'legend_header', 'Trigger step')
        ylabel = retrieve_kw(kwargs, 'ylabel', 'Trigger Efficiency')
        title = retrieve_kw(kwargs, 'title', 'Trigger Efficiency')
        oname = retrieve_kw(kwargs, 'oname', 'plot_efficiencys')
        column = retrieve_kw(kwargs, 'column', 2)
        doRatio = retrieve_kw(kwargs, 'doRatio', False)
        canvas_size = retrieve_kw(kwargs, 'canvas_size', (1800, 1500))
        # FIXME: This must be disable for now, there is some problem into the xaxis scale
        # The top and bot axis must be match!
        tobject_collector = []
        ratio_size_as_fraction = 0.35

        from ROOT import TCanvas, TLegend, TProfile, TPad
        rows = int(round(len(hist_objs) / float(column)))
        canvas = TCanvas('canvas', 'canvas', canvas_size[0], canvas_size[1])
        canvas.Divide(rows, column)
        leg_holder = []
        for index, hist_str in enumerate(hist_names):
            hists = hist_objs[hist_str]
            pad = canvas.cd(index + 1)
            # Force disable if != of 2
            if len(hists) != 2:
                doRatio = False

            if doRatio:
                drawopt = 'pE1'
                pad.cd()
                top = TPad("pad_top", "This is the top pad", 0.0,
                           ratio_size_as_fraction, 1.0, 1.0)
                top.SetBottomMargin(0.0)
                top.SetBottomMargin(0.06 / float(top.GetHNDC()))
                #top.SetTopMargin   (0.04/float(top.GetHNDC()))
                top.SetRightMargin(0.05)
                top.SetLeftMargin(0.16)
                top.SetFillColor(0)
                top.Draw(drawopt)
                tobject_collector.append(top)
                pad.cd()
                bot = TPad("pad_bot", "This is the bottom pad", 0.0, 0.0, 1.0,
                           ratio_size_as_fraction)
                bot.SetBottomMargin(0.10 / float(bot.GetHNDC()))
                #bot.SetTopMargin   (0.02/float(bot.GetHNDC()))
                bot.SetTopMargin(0.0)
                bot.SetRightMargin(0.05)
                bot.SetLeftMargin(0.16)
                bot.SetFillColor(0)
                bot.Draw(drawopt)
                tobject_collector.append(bot)

            if type(legend_prefix) is not list:
                legend_prefix = [legend_prefix] * len(legends)

            if doRatio:
                top.cd()

            leg = TLegend(legend_position[0], legend_position[1],
                          legend_position[2], legend_position[3])

            from AtlasStyle import setLegend1, atlas_template
            setLegend1(leg)
            leg.SetHeader(legend_header)

            for i, eff in enumerate(hists):
                eff.SetLineColor(self._curve_color[i])
                eff.SetMarkerColor(self._curve_color[i])
                eff.SetMarkerStyle(self._marker_style[i])
                if type(eff) is TProfile: eff.SetStats(0)
                leg.AddEntry(eff, legend_prefix[i] + legends[i], 'p')
                if i is 0: eff.Draw()
                else: eff.Draw('same')

            leg.SetTextSize(0.03)
            leg.SetBorderSize(0)
            leg.Draw()

            if doRatio:
                top.Modified()
                top.Update

            pad.Modified()
            pad.Update()

            from ROOT import TProfile, TEfficiency, kFALSE
            xmin = 0
            xmax = 999

            if type(hists[0]) is TProfile:
                hists[0].GetYaxis().SetRangeUser(y_limits[index][0],
                                                 y_limits[index][1])
            elif type(hists[0]) is TEfficiency:
                hists[0].GetPaintedGraph().GetYaxis().SetRangeUser(
                    y_limits[index][0], y_limits[index][1])
                hists[0].GetPaintedGraph().GetYaxis().SetTitle('A')

                # Fix the X axis
                nbins = hists[0].GetTotalHistogram().GetNbinsX()
                xmin = hists[0].GetTotalHistogram().GetXaxis().GetBinLowEdge(1)
                xmax = hists[0].GetTotalHistogram().GetXaxis().GetBinLowEdge(
                    nbins + 1)
                hists[0].GetPaintedGraph().GetXaxis().SetLimits(xmin, xmax)
            else:
                hists[0].GetYaxis().SetRangeUser(y_limits[index][0],
                                                 y_limits[index][1])

            pad.Modified()
            pad.Update()
            tobject_collector.append(leg)

            if doRatio:
                atlas_template(top, **kwargs)
                top.Update()
                from ROOT import TH1
                divide = ""
                drawopt = 'pE1'
                bot.cd()
                ref = hists[0].GetPassedHistogram().Clone()
                h = hists[1].GetPassedHistogram().Clone()
                #ref = hists[0].Clone()
                #h = hists[1].Clone()
                ref.Sumw2()
                h.Sumw2()
                ref.Divide(hists[0].GetTotalHistogram().Clone())
                h.Divide(hists[1].GetTotalHistogram().Clone())
                #ratioplot = TEfficiency(h,ref)
                ratioplot = h.Clone()
                ratioplot.Sumw2()
                ratioplot.SetName(h.GetName() + '_ratio')
                ratioplot.Divide(h, ref, 1., 1., '')
                #ratioplot.Divide(ref)
                ratioplot.SetFillColor(0)
                ratioplot.SetFillStyle(0)
                ratioplot.SetMarkerColor(1)
                ratioplot.SetLineColor(kBlack)
                ratioplot.SetMarkerStyle(24)
                ratioplot.SetMarkerSize(1.2)
                ratioplot.GetYaxis().SetTitle('trigger / ref')
                ratioplot.GetYaxis().SetTitleSize(0.10)
                ratioplot.GetXaxis().SetTitleSize(0.10)
                ratioplot.GetXaxis().SetLabelSize(0.10)
                ratioplot.GetYaxis().SetLabelSize(0.10)
                ratioplot.GetYaxis().SetRangeUser(0.9, 1.07)
                ratioplot.Draw(drawopt)
                tobject_collector.append(ratioplot)
                #atlas_template(bot, **kwargs)
                from ROOT import TLine
                l1 = TLine(xmin, 1, xmax, 1)
                l1.SetLineColor(kRed)
                l1.SetLineStyle(2)
                l1.Draw()
                tobject_collector.append(l1)
                bot.Update()
            else:
                atlas_template(pad, **kwargs)
            pad.Update()

        canvas.SaveAs(oname + '.pdf')
Exemplo n.º 18
0
    def __call__(self, fList, ringerOperation, **kw):
        """
      Read ntuple and return patterns and efficiencies.
      Arguments:
        - fList: The file path or file list path. It can be an argument list of 
        two types:
          o List: each element is a string path to the file;
          o Comma separated string: each path is separated via a comma
          o Folders: Expand folders recursively adding also files within them to analysis
        - ringerOperation: Set Operation type. It can be both a string or the
          RingerOperation
      Optional arguments:
        - filterType [None]: whether to filter. Use FilterType enumeration
        - reference [Truth]: set reference for targets. Use Reference enumeration
        - treePath [Set using operation]: set tree name on file, this may be set to
          use different sources then the default.
            Default for:
              o Offline: Offline/Egamma/Ntuple/electron
              o L2: Trigger/HLT/Egamma/TPNtuple/e24_medium_L1EM18VH
        - l1EmClusCut [None]: Set L1 cluster energy cut if operating on the trigger
        - l2EtCut [None]: Set L2 cluster energy cut value if operating on the trigger
        - offEtCut [None]: Set Offline cluster energy cut value
        - nClusters [None]: Read up to nClusters. Use None to run for all clusters.
        - getRatesOnly [False]: Read up to nClusters. Use None to run for all clusters.
        - etBins [None]: E_T bins (GeV) where the data should be segmented
        - etaBins [None]: eta bins where the data should be segmented
        - ringConfig [100]: A list containing the number of rings available in the data
          for each eta bin.
        - crossVal [None]: Whether to measure benchmark efficiency splitting it
          by the crossVal-validation datasets
        - extractDet [None]: Which detector to export (use Detector enumeration).
          Defaults are:
            o L2Calo: Calorimetry
            o L2: Tracking
            o Offline: Calorimetry
            o Others: CaloAndTrack
        - standardCaloVariables [False]: Whether to extract standard track variables.
        - useTRT [False]: Whether to export TRT information when dumping track
          variables.
        - supportTriggers [True]: Whether reading data comes from support triggers
    """

        __eventBranches = [
            'EventNumber', 'RunNumber', 'RandomRunNumber', 'MCChannelNumber',
            'RandomLumiBlockNumber', 'MCPileupWeight', 'VertexZPosition',
            'Zcand_M', 'Zcand_pt', 'Zcand_eta', 'Zcand_phi', 'Zcand_y',
            'isTagTag'
        ]

        __trackBranches = [
            'elCand2_deltaeta1', 'elCand2_DeltaPOverP',
            'elCand2_deltaphiRescaled', 'elCand2_d0significance',
            'elCand2_trackd0pvunbiased', 'elCand2_eProbabilityHT'
        ]

        __monteCarloBranches = [
            'type',
            'origin',
            'originbkg',
            'typebkg',
            'isTruthElectronFromZ',
            'TruthParticlePdgId',
            'firstEgMotherPdgId',
            'TruthParticleBarcode',
            'firstEgMotherBarcode',
            'MotherPdgId',
            'MotherBarcode',
            'FirstEgMotherTyp',
            'FirstEgMotherOrigin',
            'dRPdgId',
        ]

        __onlineBranches = ['match', 'ringerMatch', 'ringer_rings']

        __offlineBranches = ['et', 'eta']

        # The current pid map used as offline reference
        pidConfigs = {
            key: value
            for key, value in RingerOperation.efficiencyBranches().iteritems()
            if key in (RingerOperation.Offline_LH_Tight,
                       RingerOperation.Offline_LH_Medium,
                       RingerOperation.Offline_LH_Loose,
                       RingerOperation.Offline_LH_VeryLoose)
        }

        # Retrieve information from keyword arguments
        filterType = retrieve_kw(kw, 'filterType', FilterType.DoNotFilter)
        reference = retrieve_kw(kw, 'reference', Reference.AcceptAll)
        offEtCut = retrieve_kw(kw, 'offEtCut', None)
        l2EtCut = retrieve_kw(kw, 'l2EtCut', None)
        treePath = retrieve_kw(kw, 'treePath', 'ZeeCandidate')
        nClusters = retrieve_kw(kw, 'nClusters', None)
        etBins = retrieve_kw(kw, 'etBins', None)
        etaBins = retrieve_kw(kw, 'etaBins', None)
        crossVal = retrieve_kw(kw, 'crossVal', None)
        ringConfig = retrieve_kw(kw, 'ringConfig', 100)
        monitoring = retrieve_kw(kw, 'monitoring', None)
        pileupRef = retrieve_kw(kw, 'pileupRef', NotSet)
        getRates = retrieve_kw(kw, 'getRates', True)
        getRatesOnly = retrieve_kw(kw, 'getRatesOnly', False)
        getTagsOnly = retrieve_kw(kw, 'getTagsOnly', False)
        extractDet = retrieve_kw(kw, 'extractDet', None)

        import ROOT
        #gROOT.ProcessLine (".x $ROOTCOREDIR/scripts/load_packages.C");
        #ROOT.gROOT.Macro('$ROOTCOREDIR/scripts/load_packages.C')
        if ROOT.gSystem.Load('libTuningTools') < 0:
            self._fatal("Could not load TuningTools library", ImportError)

        if 'level' in kw: self.level = kw.pop('level')
        # and delete it to avoid mistakes:
        checkForUnusedVars(kw, self._warning)
        del kw

        ### Parse arguments
        # Also parse operation, check if its type is string and if we can
        # transform it to the known operation enum:
        fList = csvStr2List(fList)
        fList = expandFolders(fList)
        ringerOperation = RingerOperation.retrieve(ringerOperation)
        reference = Reference.retrieve(reference)

        # Offline E_T cut
        if offEtCut:
            offEtCut = 1000. * offEtCut  # Put energy in MeV

        # Check whether using bins
        useBins = False
        useEtBins = False
        useEtaBins = False
        nEtaBins = 1
        nEtBins = 1

        if etaBins is None: etaBins = npCurrent.fp_array([])
        if type(etaBins) is list: etaBins = npCurrent.fp_array(etaBins)
        if etBins is None: etBins = npCurrent.fp_array([])
        if type(etBins) is list: etBins = npCurrent.fp_array(etBins)

        if etBins.size:
            etBins = etBins * 1000.  # Put energy in MeV
            nEtBins = len(etBins) - 1
            if nEtBins >= np.iinfo(npCurrent.scounter_dtype).max:
                self._fatal((
                    'Number of et bins (%d) is larger or equal than maximum '
                    'integer precision can hold (%d). Increase '
                    'TuningTools.coreDef.npCurrent scounter_dtype number of bytes.'
                ), nEtBins,
                            np.iinfo(npCurrent.scounter_dtype).max)
            # Flag that we are separating data through bins
            useBins = True
            useEtBins = True
            self._debug('E_T bins enabled.')

        if not type(ringConfig) is list and not type(ringConfig) is np.ndarray:
            ringConfig = [ringConfig] * (len(etaBins) -
                                         1) if etaBins.size else 1
        if type(ringConfig) is list:
            ringConfig = npCurrent.int_array(ringConfig)
        if not len(ringConfig):
            self._fatal('Rings size must be specified.')

        if etaBins.size:
            nEtaBins = len(etaBins) - 1
            if nEtaBins >= np.iinfo(npCurrent.scounter_dtype).max:
                self._fatal((
                    'Number of eta bins (%d) is larger or equal than maximum '
                    'integer precision can hold (%d). Increase '
                    'TuningTools.coreDef.npCurrent scounter_dtype number of bytes.'
                ), nEtaBins,
                            np.iinfo(npCurrent.scounter_dtype).max)
            if len(ringConfig) != nEtaBins:
                self._fatal((
                    'The number of rings configurations (%r) must be equal than '
                    'eta bins (%r) region config'), ringConfig, etaBins)
            useBins = True
            useEtaBins = True
            self._debug('eta bins enabled.')
        else:
            self._debug('eta/et bins disabled.')

        # The base information holder, such as et, eta and pile-up
        if pileupRef is NotSet:
            if ringerOperation > 0:
                pileupRef = PileupReference.avgmu
            else:
                pileupRef = PileupReference.nvtx

        pileupRef = PileupReference.retrieve(pileupRef)
        self._info("Using '%s' as pile-up reference.",
                   PileupReference.tostring(pileupRef))

        # Candidates: (1) is tags and (2) is probes. Default is probes
        self._candIdx = 1 if getTagsOnly else 2

        # Mutual exclusive arguments:
        if not getRates and getRatesOnly:
            self._logger.error(
                "Cannot run with getRates set to False and getRatesOnly set to True. Setting getRates to True."
            )
            getRates = True

        ### Prepare to loop:
        t = ROOT.TChain(treePath)
        for inputFile in progressbar(fList,
                                     len(fList),
                                     logger=self._logger,
                                     prefix="Creating collection tree "):
            # Check if file exists
            f = ROOT.TFile.Open(inputFile, 'read')
            if not f or f.IsZombie():
                self._warning('Couldn' 't open file: %s', inputFile)
                continue
            # Inform user whether TTree exists, and which options are available:
            self._debug("Adding file: %s", inputFile)
            obj = f.Get(treePath)
            if not obj:
                self._warning("Couldn't retrieve TTree (%s)!", treePath)
                self._info("File available info:")
                f.ReadAll()
                f.ReadKeys()
                f.ls()
                continue
            elif not isinstance(obj, ROOT.TTree):
                self._fatal("%s is not an instance of TTree!", treePath,
                            ValueError)
            t.Add(inputFile)
        # Turn all branches off.
        t.SetBranchStatus("*", False)
        # RingerPhysVal hold the address of required branches
        event = ROOT.SkimmedNtuple()
        # Ready to retrieve the total number of events
        t.GetEntry(0)
        ## Allocating memory for the number of entries
        entries = t.GetEntries()
        nobs = entries if (nClusters is None or nClusters > entries or nClusters < 1) \
                                                                    else nClusters
        ## Retrieve the dependent operation variables:
        if useEtBins:
            etBranch = ('elCand%d_et') % (
                self._candIdx) if ringerOperation < 0 else ('fcCand%d_et') % (
                    self._candIdx)
            self.__setBranchAddress(t, etBranch, event)
            self._debug("Added branch: %s", etBranch)
            npEt = npCurrent.scounter_zeros(
                shape=npCurrent.shape(npat=1, nobs=nobs))
            self._debug("Allocated npEt    with size %r", npEt.shape)

        if useEtaBins:
            etaBranch = ('elCand%d_eta') % (
                self._candIdx) if ringerOperation < 0 else ('fcCand%d_eta') % (
                    self._candIdx)
            self.__setBranchAddress(t, etaBranch, event)
            self._debug("Added branch: %s", etaBranch)
            npEta = npCurrent.scounter_zeros(
                shape=npCurrent.shape(npat=1, nobs=nobs))
            self._debug("Allocated npEta   with size %r", npEta.shape)

        if reference is Reference.Truth:
            self.__setBranchAddress(t, ('elCand%d_isTruthElectronFromZ') %
                                    (self._candIdx), event)

        for var in __offlineBranches:
            self.__setBranchAddress(t, ('elCand%d_%s') % (self._candIdx, var),
                                    event)
        #for var in pidConfigs.values():
        #  self.__setBranchAddress(t,var,event)

        for var in __trackBranches:
            self.__setBranchAddress(t, var, event)

        # Add online branches if using Trigger
        if ringerOperation > 0:
            for var in __onlineBranches:
                self.__setBranchAddress(t,
                                        ('fcCand%d_%s') % (self._candIdx, var),
                                        event)
        else:
            self.__setBranchAddress(t, ('elCand%d_%s') %
                                    (self._candIdx, 'ringer_rings'), event)

        if pileupRef is PileupReference.nvtx:
            pileupBranch = 'Nvtx'
            pileupDataType = np.uint16
        elif pileupRef is PileupReference.avgmu:
            pileupBranch = 'averageIntPerXing'
            pileupDataType = np.float32
        else:
            raise NotImplementedError(
                "Pile-up reference %r is not implemented." % pileupRef)

        #for var in __eventBranches +
        for var in [pileupBranch]:
            self.__setBranchAddress(t, var, event)

        ### Allocate memory
        if extractDet == (Detector.Calorimetry):
            npat = ringConfig.max()
        elif extractDet == (Detector.Tracking):
            npat = len(__trackBranches)
        # NOTE: Check if pat is correct for both Calo and track data
        elif extractDet in (Detector.CaloAndTrack, Detector.All):
            npat = ringConfig.max() + len(__trackBranches)

        npPatterns = npCurrent.fp_zeros(shape=npCurrent.shape(
            npat=npat,  #getattr(event, ringerBranch).size()
            nobs=nobs))
        self._debug("Allocated npPatterns with size %r", npPatterns.shape)

        baseInfoBranch = BaseInfo(
            (etBranch, etaBranch, pileupBranch),
            (npCurrent.fp_dtype, npCurrent.fp_dtype, pileupDataType))

        baseInfo = [
            None,
        ] * baseInfoBranch.nInfo
        # Add E_T, eta and luminosity information
        npBaseInfo = [
            npCurrent.zeros(shape=npCurrent.shape(npat=1, nobs=nobs),
                            dtype=baseInfoBranch.dtype(idx))
            for idx in baseInfoBranch
        ]

        from TuningTools.CreateData import BranchEffCollector, BranchCrossEffCollector
        branchEffCollectors = OrderedDict()
        branchCrossEffCollectors = OrderedDict()

        if ringerOperation < 0:
            from operator import itemgetter
            benchmarkDict = OrderedDict(
                sorted([(key, value) for key, value in
                        RingerOperation.efficiencyBranches().iteritems()
                        if key < 0 and not (isinstance(value, (list, tuple)))],
                       key=itemgetter(0)))
        else:
            benchmarkDict = OrderedDict()

        for key, val in benchmarkDict.iteritems():
            branchEffCollectors[key] = list()
            branchCrossEffCollectors[key] = list()
            # Add efficincy branch:
            if ringerOperation < 0:
                self.__setBranchAddress(t, val, event)

            for etBin in range(nEtBins):
                if useBins:
                    branchEffCollectors[key].append(list())
                    branchCrossEffCollectors[key].append(list())
                for etaBin in range(nEtaBins):
                    etBinArg = etBin if useBins else -1
                    etaBinArg = etaBin if useBins else -1
                    argList = [
                        RingerOperation.tostring(key), val, etBinArg, etaBinArg
                    ]
                    branchEffCollectors[key][etBin].append(
                        BranchEffCollector(*argList))
                    if crossVal:
                        branchCrossEffCollectors[key][etBin].append(
                            BranchCrossEffCollector(entries, crossVal,
                                                    *argList))
                # etBin
            # etaBin
        # benchmark dict

        if self._logger.isEnabledFor(LoggingLevel.DEBUG):
            self._debug(
                'Retrieved following branch efficiency collectors: %r', [
                    collector[0].printName
                    for collector in traverse(branchEffCollectors.values())
                ])

        etaBin = 0
        etBin = 0
        step = int(entries / 100) if int(entries / 100) > 0 else 1

        ## Start loop!
        self._info("There is available a total of %d entries.", entries)
        cPos = 0

        ### Loop over entries
        for entry in progressbar(range(entries),
                                 entries,
                                 step=step,
                                 logger=self._logger,
                                 prefix="Looping over entries "):

            self._verbose('Processing eventNumber: %d/%d', entry, entries)
            t.GetEntry(entry)

            #print self.__getEt(event)
            if event.elCand2_et < offEtCut:
                self._debug(
                    "Ignoring entry due to offline E_T cut. E_T = %1.3f < %1.3f MeV",
                    event.elCand2_et, offEtCut)
                continue
            # Add et distribution for all events

            if ringerOperation > 0:
                if event.fcCand2_et < l2EtCut:
                    self._debug("Ignoring entry due Fast Calo E_T cut.")
                    continue
                # Add et distribution for all events

            # Set discriminator target:
            target = Target.Unknown
            # Monte Carlo cuts
            if reference is Reference.Truth:
                if getattr(event, ('elCand%d_isTruthElectronFromZ') %
                           (self._candIdx)):
                    target = Target.Signal
                elif not getattr(event, ('elCand%d_isTruthElectronFromZ') %
                                 (self._candIdx)):
                    target = Target.Background
            # Offline Likelihood cuts
            elif reference is Reference.Off_Likelihood:
                if getattr(event,
                           pidConfigs[RingerOperation.Offline_LH_Tight]):
                    target = Target.Signal
                elif not getattr(
                        event,
                        pidConfigs[RingerOperation.Offline_LH_VeryLoose]):
                    target = Target.Background
            # By pass everything (Default)
            elif reference is Reference.AcceptAll:
                target = Target.Signal if filterType is FilterType.Signal else Target.Background

            # Run filter if it is defined
            if filterType and \
               ( (filterType is FilterType.Signal and target != Target.Signal) or \
                 (filterType is FilterType.Background and target != Target.Background) or \
                 (target == Target.Unknown) ):
                #self._verbose("Ignoring entry due to filter cut.")
                continue

            ## Retrieve base information and rings:
            for idx in baseInfoBranch:
                lInfo = getattr(event, baseInfoBranch.retrieveBranch(idx))
                baseInfo[idx] = lInfo
            # Retrieve dependent operation region
            if useEtBins:
                etBin = self.__retrieveBinIdx(etBins, baseInfo[0])
            if useEtaBins:
                etaBin = self.__retrieveBinIdx(etaBins, np.fabs(baseInfo[1]))

            # Check if bin is within range (when not using bins, this will always be true):
            if (etBin < nEtBins and etaBin < nEtaBins):

                if useEtBins: npEt[cPos] = etBin
                if useEtaBins: npEta[cPos] = etaBin
                # Online operation
                cPat = 0
                caloAvailable = True
                if ringerOperation > 0 and self.__get_ringer_onMatch(
                        event) < 1:
                    continue
                # TODO Treat case where we don't use rings energy
                # Check if the rings empty
                if self.__get_rings_energy(event, ringerOperation).empty():
                    self._debug(
                        'No rings available in this event. Skipping...')
                    caloAvailable = False

                # Retrieve rings:
                if extractDet in (Detector.Calorimetry, Detector.CaloAndTrack,
                                  Detector.All):
                    if caloAvailable:
                        try:
                            pass
                            patterns = stdvector_to_list(
                                self.__get_rings_energy(
                                    event, ringerOperation))
                            lPat = len(patterns)
                            if lPat == ringConfig[etaBin]:
                                npPatterns[npCurrent.access(
                                    pidx=slice(cPat, ringConfig[etaBin]),
                                    oidx=cPos)] = patterns
                            else:
                                oldEtaBin = etaBin
                                if etaBin > 0 and ringConfig[etaBin -
                                                             1] == lPat:
                                    etaBin -= 1
                                elif etaBin + 1 < len(
                                        ringConfig) and ringConfig[etaBin +
                                                                   1] == lPat:
                                    etaBin += 1
                                npPatterns[npCurrent.access(
                                    pidx=slice(cPat, ringConfig[etaBin]),
                                    oidx=cPos)] = patterns
                                self._warning((
                                    "Recovered event which should be within eta bin (%d: %r) "
                                    "but was found to be within eta bin (%d: %r). "
                                    "Its read eta value was of %f."),
                                              oldEtaBin,
                                              etaBins[oldEtaBin:oldEtaBin + 2],
                                              etaBin,
                                              etaBins[etaBin:etaBin + 2],
                                              np.fabs(getattr(
                                                  event, etaBranch)))
                        except ValueError:
                            self._logger.error((
                                "Patterns size (%d) do not match expected "
                                "value (%d). This event eta value is: %f, and ringConfig is %r."
                            ), lPat, ringConfig[etaBin],
                                               np.fabs(
                                                   getattr(event, etaBranch)),
                                               ringConfig)
                            continue
                        cPat += ringConfig[etaBin]
                    else:
                        # Also display warning when extracting only calorimetry!
                        self._warning("Rings not available")
                        continue

                if extractDet in (Detector.Tracking, Detector.CaloAndTrack,
                                  Detector.All):
                    for var in __trackBranches:
                        npPatterns[npCurrent.access(pidx=cPat,
                                                    oidx=cPos)] = getattr(
                                                        event, var)
                        if var == 'elCand2_eProbabilityHT':
                            from math import log
                            TRT_PID = npPatterns[npCurrent.access(pidx=cPat,
                                                                  oidx=cPos)]
                            epsilon = 1e-99
                            if TRT_PID >= 1.0: TRT_PID = 1.0 - 1.e-15
                            elif TRT_PID <= 0.0: TRT_PID = epsilon
                            tau = 15.0
                            TRT_PID = -(1 / tau) * log((1.0 / TRT_PID) - 1.0)
                            npPatterns[npCurrent.access(pidx=cPat,
                                                        oidx=cPos)] = TRT_PID
                        cPat += 1

                ## Retrieve rates information:
                if getRates and ringerOperation < 0:
                    #event.elCand2_isEMVerLoose2015 = not( event.elCand2_isEMVeryLoose2015 & 34896 )
                    event.elCand2_isEMLoose2015 = not (
                        event.elCand2_isEMLoose2015 & 34896)
                    event.elCand2_isEMMedium2015 = not (
                        event.elCand2_isEMMedium2015 & 276858960)
                    event.elCand2_isEMTight2015 = not (
                        event.elCand2_isEMTight2015 & 281053264)

                    for branch in branchEffCollectors.itervalues():
                        if not useBins:
                            branch.update(event)
                        else:
                            branch[etBin][etaBin].update(event)
                    if crossVal:
                        for branchCross in branchCrossEffCollectors.itervalues(
                        ):
                            if not useBins:
                                branchCross.update(event)
                            else:
                                branchCross[etBin][etaBin].update(event)
                # end of (getRates)

                if not monitoring is None:
                    self.__fillHistograms(monitoring, filterType, pileupRef,
                                          pidConfigs, event)

                # We only increment if this cluster will be computed
                cPos += 1
            # end of (et/eta bins)

            # Limit the number of entries to nClusters if desired and possible:
            if not nClusters is None and cPos >= nClusters:
                break
        # for end

        ## Treat the rings information
        ## Remove not filled reserved memory space:
        if npPatterns.shape[npCurrent.odim] > cPos:
            npPatterns = np.delete(npPatterns,
                                   slice(cPos, None),
                                   axis=npCurrent.odim)

        ## Segment data over bins regions:
        # Also remove not filled reserved memory space:
        if useEtBins:
            npEt = npCurrent.delete(npEt, slice(cPos, None))
        if useEtaBins:
            npEta = npCurrent.delete(npEta, slice(cPos, None))

        # Treat
        standardCaloVariables = False
        npObject = self.treatNpInfo(
            cPos,
            npEt,
            npEta,
            useEtBins,
            useEtaBins,
            nEtBins,
            nEtaBins,
            standardCaloVariables,
            ringConfig,
            npPatterns,
        )

        data = [
            self.treatNpInfo(cPos, npEt, npEta, useEtBins, useEtaBins, nEtBins,
                             nEtaBins, standardCaloVariables, ringConfig,
                             npData) for npData in npBaseInfo
        ]
        npBaseInfo = npCurrent.array(data, dtype=np.object)

        if getRates:
            if crossVal:
                for etBin in range(nEtBins):
                    for etaBin in range(nEtaBins):
                        for branchCross in branchCrossEffCollectors.itervalues(
                        ):
                            if not useBins:
                                branchCross.finished()
                            else:
                                branchCross[etBin][etaBin].finished()

            # Print efficiency for each one for the efficiency branches analysed:
            for etBin in range(nEtBins) if useBins else range(1):
                for etaBin in range(nEtaBins) if useBins else range(1):
                    for branch in branchEffCollectors.itervalues():
                        lBranch = branch if not useBins else branch[etBin][
                            etaBin]
                        self._info('%s', lBranch)
                    if crossVal:
                        for branchCross in branchCrossEffCollectors.itervalues(
                        ):
                            lBranchCross = branchCross if not useBins else branchCross[
                                etBin][etaBin]
                            lBranchCross.dump(self._debug,
                                              printSort=True,
                                              sortFcn=self._verbose)
                    # for branch
                # for eta
            # for et
        else:
            branchEffCollectors = None
            branchCrossEffCollectors = None
        # end of (getRates)

        outputs = []
        outputs.extend((npObject, npBaseInfo))
        if getRates:
            outputs.extend((branchEffCollectors, branchCrossEffCollectors))

        return outputs
Exemplo n.º 19
0
  def __call__(self, **kw):
    """
      Create a collection of tuning job configuration files at the output
      folder.
    """

    # Cross validation configuration
    outputFolder   = retrieve_kw( kw, 'outputFolder',       'jobConfig'       )
    neuronBounds   = retrieve_kw( kw, 'neuronBounds', SeqLoopingBounds(5, 20) )
    sortBounds     = retrieve_kw( kw, 'sortBounds',   PythonLoopingBounds(50) )
    nInits         = retrieve_kw( kw, 'nInits',                100            )
    # Output configuration
    nNeuronsPerJob = retrieve_kw( kw, 'nNeuronsPerJob',         1             )
    nSortsPerJob   = retrieve_kw( kw, 'nSortsPerJob',           1             )
    nInitsPerJob   = retrieve_kw( kw, 'nInitsPerJob',          100            )
    compress       = retrieve_kw( kw, 'compress',              True           )
    if 'level' in kw: self.level = kw.pop('level')
    # Make sure that bounds variables are LoopingBounds objects:
    if not isinstance( neuronBounds, SeqLoopingBounds ):
      neuronBounds = SeqLoopingBounds(neuronBounds)
    if not isinstance( sortBounds, SeqLoopingBounds ):
      sortBounds   = PythonLoopingBounds(sortBounds)
    # and delete it to avoid mistakes:
    checkForUnusedVars( kw, self._logger.warning )
    del kw

    if nInits < 1:
      self._logger.fatal(("Cannot require zero or negative initialization "
          "number."), ValueError)

    # Do some checking in the arguments:
    nNeurons = len(neuronBounds)
    nSorts = len(sortBounds)
    if not nSorts:
      self._logger.fatal("Sort bounds is empty.")
    if nNeuronsPerJob > nNeurons:
      self._logger.warning(("The number of neurons per job (%d) is "
        "greater then the total number of neurons (%d), changing it "
        "into the maximum possible value."), nNeuronsPerJob, nNeurons )
      nNeuronsPerJob = nNeurons
    if nSortsPerJob > nSorts:
      self._logger.warning(("The number of sorts per job (%d) is "
        "greater then the total number of sorts (%d), changing it "
        "into the maximum possible value."), nSortsPerJob, nSorts )
      nSortsPerJob = nSorts

    # Create the output folder:
    mkdir_p(outputFolder)

    # Create the windows in which each job will loop upon:
    neuronJobsWindowList = \
        CreateTuningJobFiles._retrieveJobLoopingBoundsCol( neuronBounds, 
                                                           nNeuronsPerJob )
    sortJobsWindowList = \
        CreateTuningJobFiles._retrieveJobLoopingBoundsCol( sortBounds, 
                                                           nSortsPerJob )
    initJobsWindowList = \
        CreateTuningJobFiles._retrieveJobLoopingBoundsCol( \
          PythonLoopingBounds( nInits ), \
          nInitsPerJob )

    # Loop over windows and create the job configuration
    for neuronWindowBounds in neuronJobsWindowList():
      for sortWindowBounds in sortJobsWindowList():
        for initWindowBounds in initJobsWindowList():
          self._logger.debug(('Retrieved following job configuration '
              '(bounds.vec) : '
              '[ neuronBounds=%s, sortBounds=%s, initBounds=%s]'),
              neuronWindowBounds.formattedString('hn'), 
              sortWindowBounds.formattedString('s'), 
              initWindowBounds.formattedString('i'))
          fulloutput = '{outputFolder}/job.{neuronStr}.{sortStr}.{initStr}'.format( 
                        outputFolder = outputFolder, 
                        neuronStr = neuronWindowBounds.formattedString('hn'), 
                        sortStr = sortWindowBounds.formattedString('s'),
                        initStr = initWindowBounds.formattedString('i') )
          savedFile = TuningJobConfigArchieve( fulloutput,
                                               neuronBounds = neuronWindowBounds,
                                               sortBounds = sortWindowBounds,
                                               initBounds = initWindowBounds ).save( compress )
          self._logger.info('Saved job option configuration at path: %s',
                            savedFile )
Exemplo n.º 20
0
  def __call__(self, **kw):
    """
      Create a collection of tuning job configuration files at the output
      folder.
    """

    # Cross validation configuration
    outputFolder   = retrieve_kw( kw, 'outputFolder',       'jobConfig'       )
    neuronBounds   = retrieve_kw( kw, 'neuronBounds', SeqLoopingBounds(5, 20) )
    sortBounds     = retrieve_kw( kw, 'sortBounds',   PythonLoopingBounds(50) )
    nInits         = retrieve_kw( kw, 'nInits',                100            )
    # Output configuration
    nNeuronsPerJob = retrieve_kw( kw, 'nNeuronsPerJob',         1             )
    nSortsPerJob   = retrieve_kw( kw, 'nSortsPerJob',           1             )
    nInitsPerJob   = retrieve_kw( kw, 'nInitsPerJob',          100            )
    compress       = retrieve_kw( kw, 'compress',              True           )
    prefix         = retrieve_kw( kw, 'prefix'  ,             'job'           )
  
    if 'level' in kw: self.level = kw.pop('level')
    # Make sure that bounds variables are LoopingBounds objects:
    if not isinstance( neuronBounds, SeqLoopingBounds ):
      neuronBounds = SeqLoopingBounds(neuronBounds)
    if not isinstance( sortBounds, SeqLoopingBounds ):
      sortBounds   = PythonLoopingBounds(sortBounds)
    # and delete it to avoid mistakes:
    checkForUnusedVars( kw, self._warning )
    del kw

    if nInits < 1:
      self._fatal(("Cannot require zero or negative initialization "
          "number."), ValueError)

    # Do some checking in the arguments:
    nNeurons = len(neuronBounds)
    nSorts = len(sortBounds)
    if not nSorts:
      self._fatal("Sort bounds is empty.")
    if nNeuronsPerJob > nNeurons:
      self._warning(("The number of neurons per job (%d) is "
        "greater then the total number of neurons (%d), changing it "
        "into the maximum possible value."), nNeuronsPerJob, nNeurons )
      nNeuronsPerJob = nNeurons
    if nSortsPerJob > nSorts:
      self._warning(("The number of sorts per job (%d) is "
        "greater then the total number of sorts (%d), changing it "
        "into the maximum possible value."), nSortsPerJob, nSorts )
      nSortsPerJob = nSorts

    # Create the output folder:
    mkdir_p(outputFolder)

    # Create the windows in which each job will loop upon:
    neuronJobsWindowList = \
        CreateTuningJobFiles._retrieveJobLoopingBoundsCol( neuronBounds, 
                                                           nNeuronsPerJob )
    sortJobsWindowList = \
        CreateTuningJobFiles._retrieveJobLoopingBoundsCol( sortBounds, 
                                                           nSortsPerJob )
    initJobsWindowList = \
        CreateTuningJobFiles._retrieveJobLoopingBoundsCol( \
          PythonLoopingBounds( nInits ), \
          nInitsPerJob )

    # Loop over windows and create the job configuration
    for neuronWindowBounds in neuronJobsWindowList():
      for sortWindowBounds in sortJobsWindowList():
        for initWindowBounds in initJobsWindowList():
          self._debug(('Retrieved following job configuration '
              '(bounds.vec) : '
              '[ neuronBounds=%s, sortBounds=%s, initBounds=%s]'),
              neuronWindowBounds.formattedString('hn'), 
              sortWindowBounds.formattedString('s'), 
              initWindowBounds.formattedString('i'))
          fulloutput = '{outputFolder}/{prefix}.{neuronStr}.{sortStr}.{initStr}'.format( 
                        outputFolder = outputFolder, 
                        prefix = prefix,
                        neuronStr = neuronWindowBounds.formattedString('hn'), 
                        sortStr = sortWindowBounds.formattedString('s'),
                        initStr = initWindowBounds.formattedString('i') )
          savedFile = TuningJobConfigArchieve( fulloutput,
                                               neuronBounds = neuronWindowBounds,
                                               sortBounds = sortWindowBounds,
                                               initBounds = initWindowBounds ).save( compress )
          self._info('Saved job option configuration at path: %s',
                            savedFile )
Exemplo n.º 21
0
    def plot(self, **kw):

        from ROOT import kRed
        dirname = retrieve_kw(kw, 'dirname', 'Distribution')
        basecolor = retrieve_kw(kw, 'basecolor', kRed - 7)
        pdftitle = retrieve_kw(kw, 'pdftitle', 'Distributions')
        pdfoutput = retrieve_kw(kw, 'pdfoutput', 'distributions')

        import os
        # Organize outputs (.py and .pdf)
        prefix = self._basepath.split('/')[-1]
        localpath = os.getcwd() + '/' + dirname + '/' + prefix

        try:
            if not os.path.exists(localpath):
                os.makedirs(localpath)
        except:
            self._logger.warning('The director %s exist.', localpath)

        hist_names = [
            'et', 'eta', 'mu', 'nvtx', 'reta', 'eratio', 'weta2', 'rhad',
            'rphi', 'f1', 'f3'
        ]
        hist_labels = [
            'E_{T}', "#eta", "<#mu>", 'N_{vtx}', 'R_{eta}', 'E_{ratio}',
            'W_{eta2}', 'R_{had}', 'R_{phi}', 'f_{1}', 'f_{3}'
        ]

        from ROOT import TCanvas, TH1F, gStyle, TLegend, TPad
        from ROOT import kGreen, kRed, kBlue, kBlack, kGray, gPad, kAzure
        from TrigEgammaDevelopments.plots.AtlasStyle import AtlasStyle, atlas_template, setLegend1

        canvas1 = TCanvas('canvas1', 'canvas1', 2500, 1600)
        canvas1.Divide(4, 3)

        # Concatenate distributions for all regions
        def sumAllRegions(histname):
            h = None
            for etBinIdx in range(len(self._etBins) - 1):
                for etaBinIdx in range(len(self._etaBins) - 1):
                    binningname = ('et%d_eta%d') % (etBinIdx, etaBinIdx)
                    path = self._basepath + '/' + self.currentDir(
                    ) + '/' + binningname
                    if h:
                        h += self.storeSvc().histogram(path + '/' + histname)
                    else:
                        h = self.storeSvc().histogram(path + '/' +
                                                      histname).Clone()
            return h

        collector = []
        figures = {
            'rings': [],
            'rnnOutput': [],
            'ringer_profile': str(),
            'shower_shapes': str()
        }
        """
      Plot all shower shapes distributins
    """
        for idx, histname in enumerate(hist_names):
            self.setDir('Data')
            h_data = sumAllRegions(histname)
            self.setDir('MonteCarlo')
            h_mc = sumAllRegions(histname)
            #h_mc, h_data =  self.__scale_histograms(h_mc, h_data, 100, 0.01, 0.01)

            pad = canvas1.cd(idx + 1)
            gStyle.SetOptStat(110011)
            collector.append(pad)

            h_mc.SetFillColor(basecolor)
            h_mc.SetLineColor(basecolor)
            h_data.SetLineColor(kBlack)
            h_mc.Scale(1. / h_mc.GetMaximum())
            h_data.Scale(1. / h_data.GetMaximum())
            h_mc.Draw()
            h_data.Draw('same')
            leg1 = TLegend(0.2, 0.75, 0.5, 0.95)
            setLegend1(leg1)
            leg1.AddEntry(h_mc, 'MC')
            leg1.AddEntry(h_data, 'Data')
            leg1.Draw()
            collector[-1].Update()
            collector.append(h_mc)
            collector.append(h_data)
            collector.append(leg1)

        canvas1.SaveAs(localpath + '/shower_shapes_distributions.pdf')
        figures[
            'shower_shapes'] = localpath + '/shower_shapes_distributions.pdf'
        """
      Plot all shower ringer shapes for each ring
    """
        ratio_size_as_fraction = 0.35
        from RingerCore import progressbar

        rings_localpath = []

        for r in progressbar(range(100),
                             100,
                             step=1,
                             logger=self._logger,
                             prefix="Looping over rings (Plotting...) "):
            canvas2 = TCanvas('canvas2', 'canvas2', 2500, 1600)
            drawopt = 'pE1'
            canvas2.cd()
            top = TPad("pad_top", "This is the top pad", 0.0,
                       ratio_size_as_fraction, 1.0, 1.0)
            top.SetBottomMargin(0.0)
            top.SetBottomMargin(0.06 / float(top.GetHNDC()))
            #top.SetTopMargin   (0.04/float(top.GetHNDC()))
            top.SetRightMargin(0.05)
            top.SetLeftMargin(0.16)
            top.SetFillColor(0)
            top.Draw(drawopt)

            canvas2.cd()
            bot = TPad("pad_bot", "This is the bottom pad", 0.0, 0.0, 1.0,
                       ratio_size_as_fraction)
            bot.SetBottomMargin(0.10 / float(bot.GetHNDC()))
            #bot.SetTopMargin   (0.02/float(bot.GetHNDC()))
            bot.SetTopMargin(0.0)
            bot.SetRightMargin(0.05)
            bot.SetLeftMargin(0.16)
            bot.SetFillColor(0)
            bot.Draw(drawopt)

            self.setDir('MonteCarlo')
            h_mc = sumAllRegions('rings/ring_' + str(r))
            self.setDir('Data')
            h_data = sumAllRegions('rings/ring_' + str(r))
            gStyle.SetOptStat(000000)

            h_mc, h_data = self.__scale_histograms(h_mc, h_data, 100, 0.0001,
                                                   0.025)
            h_mc.Scale(1. / h_mc.GetMaximum())
            h_data.Scale(1. / h_data.GetMaximum())

            from ROOT import TH1, kGray
            divide = ""
            drawopt = 'pE1'
            bot.cd()
            ref = h_mc.Clone()
            h = h_data.Clone()

            ref.Sumw2()
            h.Sumw2()
            ratioplot = h.Clone()
            ratioplot.Sumw2()
            ratioplot.SetName(h.GetName() + '_ratio')
            ratioplot.Divide(h, ref, 1., 1., '')
            ratioplot.SetFillColor(0)
            ratioplot.SetFillStyle(0)
            ratioplot.SetMarkerColor(1)
            ratioplot.SetLineColor(kGray)
            ratioplot.SetMarkerStyle(24)
            ratioplot.SetMarkerSize(1.2)
            ratioplot.GetYaxis().SetTitleSize(0.10)
            ratioplot.GetXaxis().SetTitleSize(0.10)
            ratioplot.GetXaxis().SetLabelSize(0.10)
            ratioplot.GetYaxis().SetLabelSize(0.10)
            ratioplot.GetYaxis().SetRangeUser(-1.6, 3.7)
            ratioplot.GetYaxis().SetTitleOffset(0.7)
            ratioplot.GetYaxis().SetTitle('Data/MC')
            ratioplot.GetXaxis().SetTitle('Ring #' + str(r + 1) + ' [MeV]')
            ratioplot.Draw(drawopt)
            from ROOT import TLine

            nbins = h_data.GetNbinsX()
            xmin = h_data.GetXaxis().GetBinLowEdge(1)
            xmax = h_data.GetXaxis().GetBinLowEdge(nbins + 1)
            l1 = TLine(xmin, 1, xmax, 1)
            l1.SetLineColor(kRed)
            l1.SetLineStyle(2)
            l1.Draw()
            bot.Update()

            top.cd()

            h_mc.SetFillColor(basecolor)
            h_mc.SetLineWidth(1)
            h_mc.SetLineColor(basecolor)
            h_data.SetLineColor(kBlack)
            h_data.SetLineWidth(1)
            h_mc.GetYaxis().SetTitle('Count')
            h_mc.Draw()
            h_data.Draw('same')

            leg1 = TLegend(0.8, 0.70, 0.95, 0.95)
            setLegend1(leg1)
            leg1.AddEntry(h_mc, 'MC')
            leg1.AddEntry(h_data, 'Data')
            leg1.Draw()
            atlas_template(top)
            top.Update()
            canvas2.SaveAs(localpath + '/distribution_ring_' + str(r + 1) +
                           '.pdf')
            figures['rings'].append(localpath + '/distribution_ring_' +
                                    str(r + 1) + '.pdf')
        """
      Plot ringer mean shapes
    """
        h_mean_data = TH1F('h_mean_data', '', 100, 0, 100)
        h_mean_mc = TH1F('h_mean_mc', '', 100, 0, 100)

        for bin in range(100):
            self.setDir('MonteCarlo')
            h_mc = sumAllRegions('rings/ring_' + str(bin))
            self.setDir('Data')
            h_data = sumAllRegions('rings/ring_' + str(bin))
            h_mean_data.SetBinContent(bin + 1, h_data.GetMean())
            h_mean_mc.SetBinContent(bin + 1, h_mc.GetMean())
        canvas3 = TCanvas('canvas3', 'canvas3', 2500, 1600)

        drawopt = 'pE1'
        canvas3.cd()
        top = TPad("pad_top", "This is the top pad", 0.0,
                   ratio_size_as_fraction, 1.0, 1.0)
        top.SetBottomMargin(0.0)
        top.SetBottomMargin(0.06 / float(top.GetHNDC()))
        #top.SetTopMargin   (0.04/float(top.GetHNDC()))
        top.SetRightMargin(0.05)
        top.SetLeftMargin(0.16)
        top.SetFillColor(0)
        top.Draw(drawopt)

        canvas3.cd()
        bot = TPad("pad_bot", "This is the bottom pad", 0.0, 0.0, 1.0,
                   ratio_size_as_fraction)
        bot.SetBottomMargin(0.10 / float(bot.GetHNDC()))
        #bot.SetTopMargin   (0.02/float(bot.GetHNDC()))
        bot.SetTopMargin(0.0)
        bot.SetRightMargin(0.05)
        bot.SetLeftMargin(0.16)
        bot.SetFillColor(0)
        bot.Draw(drawopt)

        gStyle.SetOptStat(000000)
        from ROOT import TH1, kGray
        divide = ""
        drawopt = 'pE1'
        bot.cd()
        ref = h_mean_mc.Clone()
        h = h_mean_data.Clone()
        ref.Sumw2()
        h.Sumw2()
        ratioplot = h.Clone()
        ratioplot.Sumw2()
        ratioplot.SetName(h.GetName() + '_ratio')
        ratioplot.Divide(h, ref, 1., 1., '')
        ratioplot.SetFillColor(0)
        ratioplot.SetFillStyle(0)
        ratioplot.SetMarkerColor(1)
        ratioplot.SetLineColor(kGray)
        ratioplot.SetMarkerStyle(24)
        ratioplot.SetMarkerSize(1.2)
        ratioplot.GetYaxis().SetTitleSize(0.10)
        ratioplot.GetXaxis().SetTitleSize(0.10)
        ratioplot.GetXaxis().SetLabelSize(0.10)
        ratioplot.GetYaxis().SetLabelSize(0.10)
        ratioplot.GetYaxis().SetRangeUser(-1.6, 3.7)
        ratioplot.GetYaxis().SetTitleOffset(0.7)
        ratioplot.GetYaxis().SetTitle('Data/MC')
        ratioplot.GetXaxis().SetTitle('Rings')
        ratioplot.Draw(drawopt)
        from ROOT import TLine

        nbins = h_mean_data.GetNbinsX()
        xmin = h_mean_data.GetXaxis().GetBinLowEdge(1)
        xmax = h_mean_data.GetXaxis().GetBinLowEdge(nbins + 1)
        l1 = TLine(xmin, 1, xmax, 1)
        l1.SetLineColor(kRed)
        l1.SetLineStyle(2)
        l1.Draw()
        bot.Update()

        top.cd()
        h_mean_mc.SetFillColor(basecolor)
        h_mean_mc.SetLineWidth(1)
        h_mean_mc.SetLineColor(basecolor)
        h_mean_data.SetLineColor(kBlack)
        h_mean_data.SetLineWidth(1)
        #h_mean_mc.Scale( 1./h_mean_mc.GetEntries() )
        #h_mean_data.Scale( 1./h_mean_data.GetEntries() )

        if h_mean_mc.GetMaximum() > h_mean_data.GetMaximum():
            ymin = h_mean_mc.GetMinimum()
            ymax = h_mean_mc.GetMaximum()
            h_mean_mc.Draw()
            h_mean_mc.GetYaxis().SetTitle('E[Ring] MeV')
            h_mean_data.Draw('same')
        else:
            ymin = h_mean_data.GetMinimum()
            ymax = h_mean_data.GetMaximum()
            h_mean_data.GetYaxis().SetTitle('E[Ring] MeV')
            h_mean_data.Draw()
            h_mean_mc.Draw('same')

        h_mean_data.Draw('same')

        # prepare ringer lines
        def gen_line_90(x, ymin, ymax, text):
            from ROOT import TLine, TLatex
            ymax = 1.05 * ymax
            l = TLine(x, ymin, x, ymax)
            l.SetLineStyle(2)
            l.Draw()
            txt = TLatex()
            txt.SetTextFont(12)
            txt.SetTextAngle(90)
            txt.SetTextSize(0.04)
            txt.DrawLatex(x - 1, (ymax - ymin) / 2., text)
            return l, txt

        l_ps, t_ps = gen_line_90(8, ymin, ymax, 'presampler')
        l_em1, t_em1 = gen_line_90(72, ymin, ymax, 'EM.1')
        l_em2, t_em2 = gen_line_90(80, ymin, ymax, 'EM.2')
        l_em3, t_em3 = gen_line_90(88, ymin, ymax, 'EM.3')
        l_had1, t_had1 = gen_line_90(92, ymin, ymax, 'Had.1')
        l_had2, t_had2 = gen_line_90(96, ymin, ymax, 'Had.2')
        l_had3, t_had3 = gen_line_90(100, ymin, ymax, 'Had.3')

        leg1 = TLegend(0.8, 0.70, 0.95, 0.95)
        setLegend1(leg1)
        leg1.AddEntry(h_mean_mc, 'MC')
        leg1.AddEntry(h_mean_data, 'Data')
        leg1.Draw()
        atlas_template(top)
        top.Update()

        canvas3.SaveAs(localpath + '/ringer_profile.pdf')
        figures['ringer_profile'] = localpath + '/ringer_profile.pdf'
        """
      Plot all NN distributions for each calo region
    """
        for algname in self._discrList:
            for etBinIdx in range(len(self._etBins) - 1):
                for etaBinIdx in range(len(self._etaBins) - 1):

                    binningname = ('et%d_eta%d') % (etBinIdx, etaBinIdx)
                    path = self._basepath + '/MonteCarlo/' + binningname
                    h_mc = self.storeSvc().histogram(
                        path + '/' + algname +
                        '/discriminantVsMu').ProjectionX().Clone()
                    path = self._basepath + '/Data/' + binningname
                    h_data = self.storeSvc().histogram(
                        path + '/' + algname +
                        '/discriminantVsMu').ProjectionX().Clone()
                    h_mc.Rebin(10)
                    h_data.Rebin(10)
                    h_mc.Scale(1. / h_mc.GetMaximum())
                    h_data.Scale(1. / h_data.GetMaximum())

                    canvas4 = TCanvas('canvas4', 'canvas4', 2500, 1600)
                    drawopt = 'pE1'
                    canvas4.cd()
                    top = TPad("pad_top", "This is the top pad", 0.0,
                               ratio_size_as_fraction, 1.0, 1.0)
                    top.SetBottomMargin(0.0)
                    top.SetBottomMargin(0.06 / float(top.GetHNDC()))
                    #top.SetTopMargin   (0.04/float(top.GetHNDC()))
                    top.SetRightMargin(0.05)
                    top.SetLeftMargin(0.16)
                    top.SetFillColor(0)
                    top.Draw(drawopt)

                    canvas4.cd()
                    bot = TPad("pad_bot", "This is the bottom pad", 0.0, 0.0,
                               1.0, ratio_size_as_fraction)
                    bot.SetBottomMargin(0.10 / float(bot.GetHNDC()))
                    bot.SetTopMargin(0.0)
                    bot.SetRightMargin(0.05)
                    bot.SetLeftMargin(0.16)
                    bot.SetFillColor(0)
                    bot.Draw(drawopt)

                    gStyle.SetOptStat(000000)

                    from ROOT import TH1, kGray
                    divide = ""
                    drawopt = 'pE1'
                    bot.cd()
                    ref = h_mc.Clone()
                    h = h_data.Clone()
                    ref.Sumw2()
                    h.Sumw2()
                    ratioplot = h.Clone()
                    ratioplot.Sumw2()
                    ratioplot.SetName(h.GetName() + '_ratio')
                    ratioplot.Divide(h, ref, 1., 1., '')
                    ratioplot.SetFillColor(0)
                    ratioplot.SetFillStyle(0)
                    ratioplot.SetMarkerColor(1)
                    ratioplot.SetLineColor(kGray)
                    ratioplot.SetMarkerStyle(24)
                    ratioplot.SetMarkerSize(1.2)
                    ratioplot.GetYaxis().SetTitleSize(0.10)
                    ratioplot.GetXaxis().SetTitleSize(0.10)
                    ratioplot.GetXaxis().SetLabelSize(0.10)
                    ratioplot.GetYaxis().SetLabelSize(0.10)
                    ratioplot.GetYaxis().SetRangeUser(-1.6, 3.7)
                    ratioplot.GetYaxis().SetTitleOffset(0.7)
                    ratioplot.GetYaxis().SetTitle('Data/MC')
                    ratioplot.GetXaxis().SetTitle(
                        'Neural Network (Discriminant)')
                    ratioplot.Draw(drawopt)
                    from ROOT import TLine

                    nbins = h_data.GetNbinsX()
                    xmin = h_data.GetXaxis().GetBinLowEdge(1)
                    xmax = h_data.GetXaxis().GetBinLowEdge(nbins + 1)
                    l1 = TLine(xmin, 1, xmax, 1)
                    l1.SetLineColor(kRed)
                    l1.SetLineStyle(2)
                    l1.Draw()
                    bot.Update()

                    top.cd()
                    h_mc.SetFillColor(basecolor)
                    h_mc.SetLineWidth(1)
                    h_mc.SetLineColor(basecolor)
                    h_data.SetLineColor(kBlack)
                    h_data.SetLineWidth(1)
                    h_mc.Scale(1. / h_mc.GetMaximum())
                    h_data.Scale(1. / h_data.GetMaximum())
                    h_mc.GetYaxis().SetTitle(
                        ('Counts (%s)') % (binningname.replace('_', ',')))
                    h_mc.Draw()
                    h_data.Draw('same')

                    leg1 = TLegend(0.8, 0.70, 0.95, 0.95)
                    setLegend1(leg1)
                    leg1.AddEntry(h_mc, 'MC')
                    leg1.AddEntry(h_data, 'Data')
                    leg1.Draw()
                    atlas_template(top)
                    top.Update()
                    canvas4.SaveAs(localpath + '/' + algname + '_rnnOutput_' +
                                   binningname + '.pdf')
                    figures['rnnOutput'].append(localpath + '/' + algname +
                                                '_rnnOutput_' + binningname +
                                                '.pdf')

        #from RingerCore.tex.TexAPI import *
        from RingerCore.tex.BeamerAPI import BeamerTexReportTemplate1, BeamerSection, BeamerMultiFigureSlide, BeamerFigureSlide
        with BeamerTexReportTemplate1(theme='Berlin',
                                      _toPDF=True,
                                      title=pdftitle,
                                      outputFile=pdfoutput,
                                      font='structurebold'):

            with BeamerSection(name='Shower Shapes'):
                BeamerMultiFigureSlide(
                    title='Shower Shapes (MC and Data)',
                    paths=[figures['shower_shapes']],
                    nDivWidth=1  # x
                    ,
                    nDivHeight=1  # y
                    ,
                    texts=None,
                    fortran=False,
                    usedHeight=0.8,
                    usedWidth=1.1)

            with BeamerSection(name='Ringer Shapes Profile'):
                BeamerMultiFigureSlide(
                    title='Ringer Profile (MC and Data)',
                    paths=[figures['ringer_profile']],
                    nDivWidth=1  # x
                    ,
                    nDivHeight=1  # y
                    ,
                    texts=None,
                    fortran=False,
                    usedHeight=0.8,
                    usedWidth=1.1)

            with BeamerSection(name='Ringer Shapes'):
                for s in range(4):
                    paths1 = [
                        path for path in figures['rings'][s * 25:s * 25 + 25]
                    ]
                    BeamerMultiFigureSlide(
                        title='Ringer Shapes (MC and Data)',
                        paths=paths1,
                        nDivWidth=5  # x
                        ,
                        nDivHeight=5  # y
                        ,
                        texts=None,
                        fortran=False,
                        usedHeight=0.8,
                        usedWidth=1.1)

            for algname in self._discrList:
                with BeamerSection(name=('%s Neural Network Output') %
                                   (algname.replace('_', '\_'))):
                    paths2 = []
                    for etBinIdx in range(len(self._etBins) - 1):
                        for etaBinIdx in range(len(self._etaBins) - 1):
                            binningname = ('et%d_eta%d') % (etBinIdx,
                                                            etaBinIdx)
                            paths2.append(localpath + '/' + algname +
                                          '_rnnOutput_' + binningname + '.pdf')
                    BeamerMultiFigureSlide(
                        title=algname.replace('_', '\_'),
                        paths=paths2,
                        nDivWidth=len(self._etaBins)  # x
                        ,
                        nDivHeight=len(self._etBins)  # y
                        ,
                        texts=None,
                        fortran=False,
                        usedHeight=1.0,
                        usedWidth=1.1)

        return StatusCode.SUCCESS
Exemplo n.º 22
0
 def __init__(self, **kw):
     Logger.__init__(self, kw)
     self.references = ReferenceBenchmarkCollection([])
     coreframe = coreConf.core_framework()
     self.doPerf = retrieve_kw(kw, 'doPerf', True)
     self.batchMethod           = BatchSizeMethod.retrieve(
                                retrieve_kw( kw, 'batchMethod', BatchSizeMethod.MinClassSize \
         if not 'batchSize' in kw or kw['batchSize'] is NotSet else BatchSizeMethod.Manual         )
                                  )
     self.batchSize = retrieve_kw(kw, 'batchSize', NotSet)
     epochs = retrieve_kw(kw, 'epochs', 10000)
     maxFail = retrieve_kw(kw, 'maxFail', 50)
     self.useTstEfficiencyAsRef = retrieve_kw(kw, 'useTstEfficiencyAsRef',
                                              False)
     self._merged = retrieve_kw(kw, 'merged', False)
     self.networks = retrieve_kw(kw, 'networks', NotSet)
     self.sortIdx = None
     if coreConf() is TuningToolCores.FastNet:
         seed = retrieve_kw(kw, 'seed', None)
         self._core = coreframe(level=LoggingLevel.toC(self.level),
                                seed=seed)
         self._core.trainFcn = retrieve_kw(kw, 'algorithmName', 'trainrp')
         self._core.showEvo = retrieve_kw(kw, 'showEvo', 50)
         self._core.multiStop = retrieve_kw(kw, 'doMultiStop', True)
         self._core.epochs = epochs
         self._core.maxFail = maxFail
         # TODO Add properties
     elif coreConf() is TuningToolCores.keras:
         self._core = coreframe
         from keras import callbacks
         from keras.optimizers import RMSprop, SGD
         from TuningTools.keras_util.callbacks import PerformanceHistory
         self.trainOptions = dict()
         self.trainOptions['optmin_alg'] = retrieve_kw(
             kw, 'optmin_alg', RMSprop(lr=0.001, rho=0.9, epsilon=1e-08))
         #self.trainOptions['optmin_alg']    = retrieve_kw( kw, 'optmin_alg',    SGD(lr=0.1, decay=1e-6, momentum=0.7)  )
         self.trainOptions['costFunction'] = retrieve_kw(
             kw, 'binary_crossentropy', 'mean_squared_error'
         )  # 'binary_crossentropy' #'mean_squared_error' #
         self.trainOptions['metrics'] = retrieve_kw(kw, 'metrics', [
             'accuracy',
         ])
         self.trainOptions['shuffle'] = retrieve_kw(kw, 'shuffle', True)
         self._multiStop = retrieve_kw(kw, 'doMultiStop', True)
         self.trainOptions['nEpochs'] = epochs
         self.trainOptions['nFails'] = maxFail
         #self._earlyStopping = callbacks.EarlyStopping( monitor='val_Tuning_L2Calo_SP_sp_value' #  val_loss, self.trainOptions['metrics'][0] FIXME This must change
         #                                             , patience=self.trainOptions['nFails']
         #                                             , verbose=0
         #                                             , mode='max')
         self._earlyStopping = callbacks.EarlyStopping(
             monitor='val_loss'  # val_acc
             ,
             patience=self.trainOptions['nFails'],
             verbose=0,
             mode='auto')
         self._historyCallback = PerformanceHistory(
             display=retrieve_kw(kw, 'showEvo', 50))
     else:
         self._fatal("TuningWrapper not implemented for %s", coreConf)
     checkForUnusedVars(kw, self._debug)
     del kw
     # Set default empty values:
     if coreConf() is TuningToolCores.keras:
         self._emptyData = npCurrent.fp_array([])
     elif coreConf() is TuningToolCores.FastNet:
         self._emptyData = list()
     self._emptyHandler = None
     if coreConf() is TuningToolCores.keras:
         self._emptyTarget = npCurrent.fp_array([[]]).reshape(
             npCurrent.access(pidx=1, oidx=0))
     elif coreConf() is TuningToolCores.FastNet:
         self._emptyTarget = None
     # Set holders:
     self._trnData = self._emptyData
     self._valData = self._emptyData
     self._tstData = self._emptyData
     self._trnHandler = self._emptyHandler
     self._valHandler = self._emptyHandler
     self._tstHandler = self._emptyHandler
     self._trnTarget = self._emptyTarget
     self._valTarget = self._emptyTarget
     self._tstTarget = self._emptyTarget
Exemplo n.º 23
0
  def __init__(self, **kw):

    Logger.__init__(self,**kw)
    from TuningTools.export import TrigMultiVarHypo_v2
    # hold the export tool used to transform the dict data to some format
    self._exportModel = retrieve_kw( kw, 'model', TrigMultiVarHypo_v2() )
Exemplo n.º 24
0
  def __init__(self, **kw ):
    Logger.__init__( self, kw  )
    printArgs( kw, self._logger.debug  )
    self._nSorts = None
    self._nBoxes = None
    self._nTrain = None
    self._nValid = None
    self._nTest  = None
    self._seed   = None
    self._method = CrossValidMethod.retrieve( retrieve_kw( kw, 'method', CrossValidMethod.Standard ) )

    if self._method is CrossValidMethod.Standard:
      self._nSorts = retrieve_kw( kw, 'nSorts', 50 )
      self._nBoxes = retrieve_kw( kw, 'nBoxes', 10 )
      self._nTrain = retrieve_kw( kw, 'nTrain', 6  )
      self._nValid = retrieve_kw( kw, 'nValid', 4  )
      self._nTest  = retrieve_kw( kw, 'nTest',  self._nBoxes - ( self._nTrain + self._nValid ) )
      self._seed   = retrieve_kw( kw, 'seed',   None )
      checkForUnusedVars( kw, self._logger.warning )
      # Check if variables are ok:
      if (not self._nTest is None) and self._nTest < 0:
        self._logger.fatal("Number of test clusters is lesser than zero", ValueError)
      totalSum = self._nTrain + self._nValid + (self._nTest) if self._nTest else \
                 self._nTrain + self._nValid
      if totalSum != self._nBoxes:
        self._logger.fatal("Sum of train, validation and test boxes doesn't match.", ValueError)

      np.random.seed(self._seed)

      # Test number of possible combinations (N!/((N-K)!(K)!) is greater
      # than the required sorts. If number of sorts (greater than half of the
      # possible cases) is close to the number of combinations, generate all
      # possible combinations and then gather the number of needed sorts.
      # However, as calculating factorial can be heavy, we don't do this if the
      # number of boxes is large.
      self._sort_boxes_list = []
      useRandomCreation = True
      from math import factorial
      if self._nBoxes < 201:
        totalPossibilities = ( factorial( self._nBoxes ) ) / \
            ( factorial( self._nTrain ) * \
              factorial( self._nValid ) * \
              factorial( self._nTest  ) )
        if self._nSorts > (totalPossibilities / 2):
          useRandomCreation = False
      if useRandomCreation:
        count = 0
        while True:
          random_boxes = np.random.permutation(self._nBoxes)
          random_boxes = tuple(chain(sorted(random_boxes[0:self._nTrain]),
                          sorted(random_boxes[self._nTrain:self._nTrain+self._nValid]),
                          sorted(random_boxes[self._nTrain+self._nValid:])))
          # Make sure we are not appending same sort again:
          if not random_boxes in self._sort_boxes_list:
            self._sort_boxes_list.append( random_boxes )
            count += 1
            if count == self._nSorts:
              break
      else:
        self._sort_boxes_list = list(
            combinations_taken_by_multiple_groups(range(self._nBoxes),
                                                  (self._nTrain, 
                                                   self._nVal, 
                                                   self._nTest)))
        for i in range(totalPossibilities - self._nSorts):
          self._sort_boxes_list.pop( np.random_integers(0, totalPossibilities) )
    elif self._method is CrossValidMethod.JackKnife:
      self._nBoxes = retrieve_kw( kw, 'nBoxes', 10 )
      checkForUnusedVars( kw, self._logger.warning )
      self._nSorts = self._nBoxes
      self._nTrain = self._nBoxes - 1
      self._nValid = 1
      self._nTest  = 0
      self._sort_boxes_list = list(
          combinations_taken_by_multiple_groups(range(self._nBoxes), (9, 1,)) )
Exemplo n.º 25
0
 def __init__( self, **kw ):
   Logger.__init__( self, kw )
   self.references = ReferenceBenchmarkCollection( [] )
   self.doPerf                = retrieve_kw( kw, 'doPerf',                True                   )
   self.batchMethod           = BatchSizeMethod.retrieve(
                              retrieve_kw( kw, 'batchMethod', BatchSizeMethod.MinClassSize \
       if not 'batchSize' in kw or kw['batchSize'] is NotSet else BatchSizeMethod.Manual         ) 
                                )
   self.batchSize             = retrieve_kw( kw, 'batchSize',             NotSet                 )
   epochs                     = retrieve_kw( kw, 'epochs',                10000                  )
   maxFail                    = retrieve_kw( kw, 'maxFail',               50                     )
   self.useTstEfficiencyAsRef = retrieve_kw( kw, 'useTstEfficiencyAsRef', False                  )
   self._core, self._coreEnum = retrieve_core()
   self.sortIdx = None
   if self._coreEnum is TuningToolCores.ExMachina:
     self.trainOptions = dict()
     self.trainOptions['algorithmName'] = retrieve_kw( kw, 'algorithmName', 'rprop'       )
     self.trainOptions['print']         = retrieve_kw( kw, 'showEvo',       True          )
     self.trainOptions['networkArch']   = retrieve_kw( kw, 'networkArch',   'feedforward' )
     self.trainOptions['costFunction']  = retrieve_kw( kw, 'costFunction',  'sp'          )
     self.trainOptions['shuffle']       = retrieve_kw( kw, 'shuffle',       True          )
     self.trainOptions['nEpochs']       = epochs
     self.trainOptions['nFails']        = maxFail
     self.doMultiStop                   = False
   elif self._coreEnum is TuningToolCores.FastNet:
     seed = retrieve_kw( kw, 'seed', None )
     self._core = self._core( level = LoggingLevel.toC(self.level), seed = seed )
     self._core.trainFcn    = retrieve_kw( kw, 'algorithmName', 'trainrp' )
     self._core.showEvo     = retrieve_kw( kw, 'showEvo',       50        )
     self._core.multiStop   = retrieve_kw( kw, 'doMultiStop',   True      )
     self._core.epochs      = epochs
     self._core.maxFail     = maxFail
   else:
     self._logger.fatal("TuningWrapper not implemented for %s" % TuningToolCores.tostring(self._coreEnum))
   checkForUnusedVars(kw, self._logger.debug )
   del kw
   # Set default empty values:
   if self._coreEnum is TuningToolCores.ExMachina:
     self._emptyData  = npCurrent.fp_array([])
   elif self._coreEnum is TuningToolCores.FastNet:
     self._emptyData = list()
   self._emptyHandler = None
   if self._coreEnum is TuningToolCores.ExMachina:
     self._emptyTarget = npCurrent.fp_array([[]]).reshape( 
             npCurrent.access( pidx=1,
                               oidx=0 ) )
   elif self._coreEnum is TuningToolCores.FastNet:
     self._emptyTarget = None
   # Set holders:
   self._trnData    = self._emptyData
   self._valData    = self._emptyData
   self._tstData    = self._emptyData
   self._trnHandler = self._emptyHandler
   self._valHandler = self._emptyHandler
   self._tstHandler = self._emptyHandler
   self._trnTarget  = self._emptyTarget
   self._valTarget  = self._emptyTarget
   self._tstTarget  = self._emptyTarget