def buildTriggerCount(selectionParams,sampleSelectionParams,projectionParams,sampleProjectionParams,histParams,sampleHistParams):

    histParams['TriggerCount'] = {
        'count'                       : {'xVariable': '1',                              'xBinning': [1,0,2],                 }, # just a count of events passing selection
    }

    triggers = [
        'pass_Mu17_TrkIsoVVL_Mu8_TrkIsoVVL_DZ',
        'pass_Mu17_TrkIsoVVL_TkMu8_TrkIsoVVL_DZ',
        'pass_Ele23_Ele12_CaloIdL_TrackIdL_IsoVL_DZ',
        'pass_IsoMu24',
        'pass_IsoTkMu24',
        'pass_Ele27_WPTight_Gsf',
        'pass_Mu8_TrkIsoVVL_Ele23_CaloIdL_TrackIdL_IsoVL',
        'pass_Mu23_TrkIsoVVL_Ele8_CaloIdL_TrackIdL_IsoVL',
        'pass_Mu8_TrkIsoVVL_Ele23_CaloIdL_TrackIdL_IsoVL_DZ',
        'pass_Mu23_TrkIsoVVL_Ele8_CaloIdL_TrackIdL_IsoVL_DZ',
        'pass_DoubleMediumCombinedIsoPFTau35_Trk1_eta2p1_Reg',
    ]
    for trigger in triggers:
        histParams['TriggerCount'][trigger] = {'xVariable': trigger, 'xBinning': [2,0,2], }

    selectionParams['TriggerCount'] = {
        'all'      : {'args': ['1'],                   'kwargs': {'mcscalefactor': '1', }},
    }
    
    intLumi = getLumi()
    for r in ['B','C','D','E','F','G','H']:
        run = 'Run2016{0}'.format(r)
        runLumi = getLumi(run=run)
        mcscale = '{0}/{1}'.format(runLumi,intLumi)
        runRange = getRunRange(run=run)
        datacut = 'run>={0} && run<={1}'.format(*runRange)
        selectionParams['TriggerCount'][run] = {'args': [datacut], 'kwargs': {'mcscalefactor': mcscale}}
 def __init__(self,version):
     self.version = version
     self.prescales = {}
     self.lumi = {
         '76X': getLumi(version='76X'),
         '80X': getLumi(version='80X'),
     }
     self.prescales['76X'] = {
         # HLT_Ele12_CaloIdL_TrackIdL_IsoVL_v*
         'Ele17_Ele12Leg2':   4.174,
         # HLT_Ele17_CaloIdL_TrackIdL_IsoVL_v*
         'Ele17_Ele12Leg1':  45.941,
         # HLT_Mu8_TrkIsoVVL_v*
         'Mu17_Mu8Leg2'   :   1.330,
         # HLT_Mu17_TrkIsoVVL_v*
         'Mu17_Mu8Leg1'   : 197.362,
     }
     self.prescales['80X'] = {
         # HLT_Ele12_CaloIdL_TrackIdL_IsoVL_v*
         'Ele17_Ele12Leg2':   6.162,
         'Ele23_Ele12Leg2':   6.162,
         # HLT_Ele17_CaloIdL_TrackIdL_IsoVL_v*
         'Ele17_Ele12Leg1':  30.397,
         # HLT_Ele23_CaloIdL_TrackIdL_IsoVL_v*
         'Ele23_Ele12Leg1':  16.430,
         # HLT_Mu8_TrkIsoVVL_v*
         'Mu17_Mu8Leg2'   :   7.832,
         # HLT_Mu17_TrkIsoVVL_v*
         'Mu17_Mu8Leg1'   : 217.553,
     }
Beispiel #3
0
 def __init__(self, version):
     self.version = version
     self.prescales = {}
     self.lumi = {
         '76X': getLumi(version='76X'),
         '80X': getLumi(version='80X'),
     }
     self.prescales['76X'] = {
         # HLT_Ele12_CaloIdL_TrackIdL_IsoVL_v*
         'Ele17_Ele12Leg2': 4.174,
         # HLT_Ele17_CaloIdL_TrackIdL_IsoVL_v*
         'Ele17_Ele12Leg1': 45.941,
         # HLT_Mu8_TrkIsoVVL_v*
         'Mu17_Mu8Leg2': 1.330,
         # HLT_Mu17_TrkIsoVVL_v*
         'Mu17_Mu8Leg1': 197.362,
     }
     self.prescales['80X'] = {
         # HLT_Ele12_CaloIdL_TrackIdL_IsoVL_v*
         'Ele17_Ele12Leg2': 6.162,
         'Ele23_Ele12Leg2': 6.162,
         # HLT_Ele17_CaloIdL_TrackIdL_IsoVL_v*
         'Ele17_Ele12Leg1': 30.397,
         # HLT_Ele23_CaloIdL_TrackIdL_IsoVL_v*
         'Ele23_Ele12Leg1': 16.430,
         # HLT_Mu8_TrkIsoVVL_v*
         'Mu17_Mu8Leg2': 7.832,
         # HLT_Mu17_TrkIsoVVL_v*
         'Mu17_Mu8Leg1': 217.553,
     }
Beispiel #4
0
def main(argv=None):
    if argv is None:
        argv = sys.argv[1:]

    args = parse_command_line(argv)

    if args.verbose and args.analysis:
        table = PrettyTable(['Sample','xsec [pb]','entries','ratio neg.','lumi. [/pb]','eff. entries'])
    else:
        table = PrettyTable(['Sample','xsec [pb]'])
    table.align = 'r'
    table.align['Sample'] = 'l'

    ntupleDir = getAnalysisNtupleDirectory(args.analysis,True) if args.verbose and args.analysis else getNtupleDirectory(version=args.version)

    #Odd mix of local pathnames and xrootd access
#    for sample in sorted(hdfs_ls_directory(ntupleDir)):
    for sample in (glob.glob('/'.join([ntupleDir,'*']))):
        name = os.path.basename(sample)
        logging.info('Processing {0}'.format(name))
        data = isData(name)
        xsec = getXsec(name)
        if args.verbose and args.analysis:
            print sample
            fnames = get_hdfs_root_files(sample)
            # get total events, total weights
            tree = ROOT.TChain(getTreeName(args.analysis))
            summedWeights = 0.
            for f in fnames:
                tfile = ROOT.TFile.Open('/hdfs'+f)
                summedWeights += tfile.Get("summedWeights").GetBinContent(1)
                tfile.Close()
                tree.Add('/hdfs'+f)
            numEntries = tree.GetEntries(args.selection)
            weightedEntries = 0.
            negevents = 0.
            seltree = tree.CopyTree(args.selection)
            for row in seltree:
                if data:
                    weightedEntries += 1.
                else:
                    weightedEntries += row.genWeight
                    if row.genWeight<0.: negevents += 1
            if data:
                sampleLumi = getLumi()
            else:
                sampleLumi = float(summedWeights)/xsec if xsec else 0.
            negratio = float(negevents)/numEntries if numEntries else 0.
            effevents = weightedEntries*getLumi()/sampleLumi if sampleLumi else 0.
            table.add_row([name,'{0:.6f}'.format(float(xsec)),numEntries,'{0:.3}'.format(float(negratio)),'{0:.6f}'.format(float(sampleLumi)),'{0:.3f}'.format(float(effevents))])
        else:
            table.add_row([name,xsec])

    print table.get_string()
def main(argv=None):
    if argv is None:
        argv = sys.argv[1:]

    args = parse_command_line(argv)

    if args.verbose and args.analysis:
        table = PrettyTable(['Sample','xsec [pb]','entries','ratio neg.','lumi. [/pb]','eff. entries'])
    else:
        table = PrettyTable(['Sample','xsec [pb]'])
    table.align = 'r'
    table.align['Sample'] = 'l'

    ntupleDir = getAnalysisNtupleDirectory(args.analysis) if args.verbose and args.analysis else getNtupleDirectory(version=args.version)

    for sample in sorted(glob.glob(os.path.join(ntupleDir,'*'))):
        name = os.path.basename(sample)
        logging.info('Processing {0}'.format(name))
        data = isData(name)
        xsec = getXsec(name)
        if args.verbose and args.analysis:
            fnames = get_hdfs_root_files(sample)
            # get total events, total weights
            tree = ROOT.TChain(getTreeName(args.analysis))
            summedWeights = 0.
            for f in fnames:
                tfile = ROOT.TFile.Open('/hdfs'+f)
                summedWeights += tfile.Get("summedWeights").GetBinContent(1)
                tfile.Close()
                tree.Add('/hdfs'+f)
            numEntries = tree.GetEntries(args.selection)
            weightedEntries = 0.
            negevents = 0.
            seltree = tree.CopyTree(args.selection)
            for row in seltree:
                if data:
                    weightedEntries += 1.
                else:
                    weightedEntries += row.genWeight
                    if row.genWeight<0.: negevents += 1
            if data:
                sampleLumi = getLumi()
            else:
                sampleLumi = float(summedWeights)/xsec if xsec else 0.
            negratio = float(negevents)/numEntries if numEntries else 0.
            effevents = weightedEntries*getLumi()/sampleLumi if sampleLumi else 0.
            table.add_row([name,'{0:.6f}'.format(float(xsec)),numEntries,'{0:.3}'.format(float(negratio)),'{0:.6f}'.format(float(sampleLumi)),'{0:.3f}'.format(float(effevents))])
        else:
            table.add_row([name,xsec])

    print table.get_string()
def buildTriggerCount(selectionParams, sampleSelectionParams, projectionParams,
                      sampleProjectionParams, histParams, sampleHistParams):

    histParams['TriggerCount'] = {
        'count': {
            'xVariable': '1',
            'xBinning': [1, 0, 2],
        },  # just a count of events passing selection
    }

    triggers = [
        'pass_Mu17_TrkIsoVVL_Mu8_TrkIsoVVL_DZ',
        'pass_Mu17_TrkIsoVVL_TkMu8_TrkIsoVVL_DZ',
        'pass_Ele23_Ele12_CaloIdL_TrackIdL_IsoVL_DZ',
        'pass_IsoMu24',
        'pass_IsoTkMu24',
        'pass_Ele27_WPTight_Gsf',
        'pass_Mu8_TrkIsoVVL_Ele23_CaloIdL_TrackIdL_IsoVL',
        'pass_Mu23_TrkIsoVVL_Ele8_CaloIdL_TrackIdL_IsoVL',
        'pass_Mu8_TrkIsoVVL_Ele23_CaloIdL_TrackIdL_IsoVL_DZ',
        'pass_Mu23_TrkIsoVVL_Ele8_CaloIdL_TrackIdL_IsoVL_DZ',
        'pass_DoubleMediumCombinedIsoPFTau35_Trk1_eta2p1_Reg',
    ]
    for trigger in triggers:
        histParams['TriggerCount'][trigger] = {
            'xVariable': trigger,
            'xBinning': [2, 0, 2],
        }

    selectionParams['TriggerCount'] = {
        'all': {
            'args': ['1'],
            'kwargs': {
                'mcscalefactor': '1',
            }
        },
    }

    intLumi = getLumi()
    for r in ['B', 'C', 'D', 'E', 'F', 'G', 'H']:
        run = 'Run2016{0}'.format(r)
        runLumi = getLumi(run=run)
        mcscale = '{0}/{1}'.format(runLumi, intLumi)
        runRange = getRunRange(run=run)
        datacut = 'run>={0} && run<={1}'.format(*runRange)
        selectionParams['TriggerCount'][run] = {
            'args': [datacut],
            'kwargs': {
                'mcscalefactor': mcscale
            }
        }
def flatten(analysis,sample,**kwargs):
    histParams = kwargs.pop('histParams',{})
    histSelections = kwargs.pop('histSelections',{})
    inputFileList = kwargs.pop('inputFileList','')
    outputFile = kwargs.pop('outputFile','')
    shift = kwargs.pop('shift','')
    countOnly = kwargs.pop('countOnly',False)
    njobs = kwargs.pop('njobs',1)
    job = kwargs.pop('job',0)
    multi = kwargs.pop('multi',False)
    useProof = kwargs.pop('useProof',False)
    intLumi = kwargs.pop('intLumi',float(getLumi()))
    if hasProgress and multi:
        pbar = kwargs.pop('progressbar',ProgressBar(widgets=['{0}: '.format(sample),' ',SimpleProgress(),' histograms ',Percentage(),' ',Bar(),' ',ETA()]))
    else:
        pbar = None

    if outputFile:
        flat = outputFile
        proj = outputFile.replace('.root','_projection.root')
        flattener = FlattenTree(analysis,sample,inputFileList=inputFileList,flat=flat,proj=proj,shift=shift,countOnly=countOnly,useProof=useProof,intLumi=intLumi)
    else:
        flattener = FlattenTree(analysis,sample,inputFileList=inputFileList,shift=shift,countOnly=countOnly,useProof=useProof,intLumi=intLumi)

    for histName, params in histParams.iteritems():
        flattener.addHistogram(histName,**params)

    for selName, sel in histSelections.iteritems():
        if sel: flattener.addSelection(selName,**sel['kwargs'])

    flattener.flattenAll(progressbar=pbar,njobs=njobs,job=job,multi=multi)
 def __initializeNtuple(self):
     tchain = ROOT.TChain(self.treeName)
     if self.inputFileList:  # reading from a passed list of inputfiles
         allFiles = []
         with open(self.inputFileList, 'r') as f:
             for line in f.readlines():
                 allFiles += [line.strip()]
     else:  # reading from an input directory (all files in directory will be processed)
         allFiles = glob.glob('{0}/*.root'.format(self.ntupleDirectory))
     if len(allFiles) == 0:
         logging.error('No files found for sample {0}'.format(self.sample))
     summedWeights = 0.
     for f in allFiles:
         tfile = ROOT.TFile.Open(f)
         summedWeights += tfile.Get("summedWeights").GetBinContent(1)
         tfile.Close()
         tchain.Add(f)
     if not summedWeights and not isData(self.sample):
         logging.warning('No events for sample {0}'.format(self.sample))
     self.intLumi = float(getLumi())
     self.xsec = getXsec(self.sample)
     self.sampleLumi = float(summedWeights) / self.xsec if self.xsec else 0.
     self.sampleTree = tchain
     self.files = allFiles
     self.initialized = True
     logging.debug(
         'Initialized {0}: summedWeights = {1}; xsec = {2}; sampleLumi = {3}; intLumi = {4}'
         .format(self.sample, summedWeights, self.xsec, self.sampleLumi,
                 self.intLumi))
Beispiel #9
0
 def __init__(self,analysis,sample,**kwargs):
     # default to access via sample/analysis
     self.analysis = analysis
     self.sample = sample
     self.shift = kwargs.pop('shift','')
     self.skipHists = kwargs.pop('skipHists',False)
     self.isData = isData(self.sample)
     self.intLumi = kwargs.get('intLumi',float(getLumi()))
     logging.debug('Initializing {0} {1} {2}'.format(self.analysis,self.sample,self.shift))
     # backup passing custom parameters
     self.ntupleDirectory = kwargs.pop('ntupleDirectory','{0}/{1}'.format(getNtupleDirectory(self.analysis,shift=self.shift),self.sample))
     self.inputFileList = kwargs.pop('inputFileList','')
     self.outputFile = kwargs.pop('outputFile',getNewFlatHistograms(self.analysis,self.sample,shift=self.shift))
     if os.path.dirname(self.outputFile): python_mkdir(os.path.dirname(self.outputFile))
     self.treeName = kwargs.pop('treeName',getTreeName(self.analysis))
     if hasProgress:
         self.pbar = kwargs.pop('progressbar',ProgressBar(widgets=['{0}: '.format(sample),' ',SimpleProgress(),' ',Percentage(),' ',Bar(),' ',ETA()]))
     else:
         self.pbar = None
     # get stuff needed to flatten
     self.infile = 0
     self.tchain = 0
     self.initialized = False
     self.hists = {}
     self.datasets = {}
Beispiel #10
0
 def __init__(self, analysis, **kwargs):
     '''Initialize the plotter'''
     # plot directory
     self.analysis = analysis
     self.intLumi = kwargs.get('intLumi', float(getLumi()))
     self.outputDirectory = kwargs.pop('outputDirectory',
                                       'plots/{0}'.format(self.analysis))
     self.outputDirectoryCSV = kwargs.pop('outputDirectoryCSV',
                                          'csv/{0}'.format(self.analysis))
Beispiel #11
0
 def __init__(self,**kwargs):
     self.ntupleDirectory = kwargs.pop('ntupleDirectory','ntuples/Analysis')
     self.treeName = kwargs.pop('treeName','AnalysisTree')
     # dictionaries to hold the sample information
     self.intLumi = getLumi()
     self.sampleLumi = 0
     self.sampleTree = None
     self.histParameters = {}
     self.histParameters2D = {}
     self.outfile = 0
     self.preinitialized = False
     self.selections = []
     self.sample = ''
     self.currVal = 0
Beispiel #12
0
 def __init__(self, **kwargs):
     self.ntupleDirectory = kwargs.pop('ntupleDirectory',
                                       'ntuples/Analysis')
     self.treeName = kwargs.pop('treeName', 'AnalysisTree')
     # dictionaries to hold the sample information
     self.intLumi = getLumi()
     self.sampleLumi = 0
     self.sampleTree = None
     self.histParameters = {}
     self.histParameters2D = {}
     self.outfile = 0
     self.preinitialized = False
     self.selections = []
     self.sample = ''
     self.currVal = 0
def parse_command_line(argv):
    parser = argparse.ArgumentParser(description='Flatten Tree')

    parser.add_argument('analysis', type=str, help='Analysis to process')
    parser.add_argument('shift', type=str, default='', nargs='?', help='Shift to apply to scale factors')
    parser.add_argument('countOnly', type=int, default=0, nargs='?', help='Only do counts, no distributions')
    parser.add_argument('--samples', nargs='+', type=str, default=['*'], help='Samples to flatten. Supports unix style wildcards.')
    parser.add_argument('--hists', nargs='+', type=str, default=['all'], help='Histograms to flatten.')
    parser.add_argument('--selections', nargs='+', type=str, default=['all'], help='Selections to flatten.')
    parser.add_argument('--channels', nargs='+', type=str, default=['all'], help='Channels to project.')
    parser.add_argument('--skipProjection', action='store_true', help='Skip projecting')
    parser.add_argument('--intLumi', type=float, default=float(getLumi()), nargs='?', help='Override luminosity from full run')
    #parser.add_argument('--useProof', action='store_true', help='Use PROOF')
    parser.add_argument('-j',type=int,default=1,help='Number of cores to use')

    return parser.parse_args(argv)
Beispiel #14
0
 def __setStyle(self,pad,position=11,preliminary=True):
     '''Set style for plots based on the CMS TDR style guidelines.
        https://twiki.cern.ch/twiki/bin/view/CMS/Internal/PubGuidelines#Figures_and_tables
        https://ghm.web.cern.ch/ghm/plots/'''
     # set period (used in CMS_lumi)
     # period : sqrts
     # 1 : 7, 2 : 8, 3 : 7+8, 4 : 13, ... 7 : 7+8+13
     period_int = 4
     # set position
     # 11: upper left, 33 upper right
     CMS_lumi.wrtieExtraText = preliminary
     CMS_lumi.extraText = "Preliminary"
     CMS_lumi.lumi_13TeV = "%0.1f fb^{-1}" % (float(getLumi())/1000.)
     if getLumi < 1000:
         CMS_lumi.lumi_13TeV = "%0.1f pb^{-1}" % (float(getLumi))
     CMS_lumi.CMS_lumi(pad,period_int,position)
Beispiel #15
0
 def _setStyle(self,pad,position=11,preliminary=True,personal=False,period_int=4):
     '''Set style for plots based on the CMS TDR style guidelines.
        https://twiki.cern.ch/twiki/bin/view/CMS/Internal/PubGuidelines#Figures_and_tables
        https://ghm.web.cern.ch/ghm/plots/'''
     # set period (used in CMS_lumi)
     # period : sqrts
     # 1 : 7, 2 : 8, 3 : 7+8, 4 : 13, ... 7 : 7+8+13
     # set position
     # 11: upper left, 33 upper right
     CMS_lumi.cmsText = 'CMS' if not personal else 'Devin N. Taylor'
     CMS_lumi.writeExtraText = preliminary if not personal else True
     CMS_lumi.extraText = "Preliminary" if not personal else 'Analysis in Progress'
     CMS_lumi.lumi_13TeV = "%0.1f fb^{-1}" % (float(getLumi())/1000.)
     if getLumi < 1000:
         CMS_lumi.lumi_13TeV = "%0.1f pb^{-1}" % (float(getLumi))
     CMS_lumi.CMS_lumi(pad,period_int,position)
Beispiel #16
0
 def __init__(self,analysis,sample,**kwargs):
     # default to access via sample/analysis
     self.analysis = analysis
     self.sample = sample
     self.shift = kwargs.pop('shift','')
     self.intLumi = kwargs.get('intLumi',float(getLumi()))
     logging.debug('Initializing {0} {1} {2}'.format(self.analysis,self.sample,self.shift))
     # backup passing custom parameters
     self.ntupleDirectory = kwargs.pop('ntupleDirectory','{0}/{1}'.format(getNtupleDirectory(self.analysis,shift=self.shift),self.sample))
     self.inputFileList = kwargs.pop('inputFileList','')
     self.outputFile = kwargs.pop('outputFile','')
     self.json = kwargs.pop('json',getSkimJson(self.analysis,self.sample))
     self.pickle = kwargs.pop('pickle',getSkimPickle(self.analysis,self.sample))
     self.treeName = kwargs.pop('treeName',getTreeName(self.analysis))
     if hasProgress:
         self.pbar = kwargs.pop('progressbar',ProgressBar(widgets=['{0}: '.format(sample),' ',SimpleProgress(),' events ',Percentage(),' ',Bar(),' ',ETA()]))
     else:
         self.pbar = None
     # get stuff needed to flatten
     self.infile = 0
     self.tchain = 0
     self.initialized = False
     self.counts = {}
Beispiel #17
0
    def __init__(self, **kwargs):
        inputTreeName = kwargs.pop('inputTreeName', 'WZTree')
        super(WZTrainer, self).__init__(**kwargs)

        sampleDir = 'ntuples/WZ'
        sampleMap = {
            "dy10":
            "DYJetsToLL_M-10to50_TuneCUETP8M1_13TeV-amcatnloFXFX-pythia8",
            "dy50": "DYJetsToLL_M-50_TuneCUETP8M1_13TeV-amcatnloFXFX-pythia8",
            "ggzz2e2m": "GluGluToContinToZZTo2e2mu_13TeV_MCFM701_pythia8",
            "ggzz2e2t": "GluGluToContinToZZTo2e2tau_13TeV_MCFM701_pythia8",
            "ggzz2m2t": "GluGluToContinToZZTo2mu2tau_13TeV_MCFM701_pythia8",
            "ggzz4e": "GluGluToContinToZZTo4e_13TeV_MCFM701_pythia8",
            "ggzz4m": "GluGluToContinToZZTo4mu_13TeV_MCFM701_pythia8",
            "ggzz4t": "GluGluToContinToZZTo4tau_13TeV_MCFM701_pythia8",
            "tt": "TTJets_TuneCUETP8M1_13TeV-amcatnloFXFX-pythia8",
            "ttw":
            "TTWJetsToLNu_TuneCUETP8M1_13TeV-amcatnloFXFX-madspin-pythia8",
            "w": "WJetsToLNu_TuneCUETP8M1_13TeV-amcatnloFXFX-pythia8",
            "ww": "WWTo2L2Nu_13TeV-powheg",
            "wz3lnu": "WZTo3LNu_TuneCUETP8M1_13TeV-powheg-pythia8",
            "wz2l2q": "WZTo2L2Q_13TeV_amcatnloFXFX_madspin_pythia8",
            "wzz": "WZZ_TuneCUETP8M1_13TeV-amcatnlo-pythia8",
            "zg": "ZGTo2LG_TuneCUETP8M1_13TeV-amcatnloFXFX-pythia8",
            "zz2l2n": "ZZTo2L2Nu_13TeV_powheg_pythia8",
            "zz2l2q": "ZZTo2L2Q_13TeV_amcatnloFXFX_madspin_pythia8",
            "zz4l": "ZZTo4L_13TeV_powheg_pythia8",
            #"tzq"      : "tZq_ll_4f_13TeV-amcatnlo-pythia8_TuneCUETP8M1",
        }

        # get the trees
        intLumis = {}
        for s in sampleMap:
            summedWeights = 0.
            for f in glob.glob('{0}/{1}/*.root'.format(sampleDir,
                                                       sampleMap[s])):
                tfile = ROOT.TFile.Open(f)
                hist = tfile.Get('summedWeights')
                summedWeights += hist.GetBinContent(1)
                tfile.Close()
            intLumis[s] = float(summedWeights) / getXsec(sampleMap[s])
        sigTrees = {}
        for sig in ['wz3lnu']:
            sigTrees[sig] = ROOT.TChain('WZTree')
            for f in glob.glob('{0}/{1}/*.root'.format(sampleDir,
                                                       sampleMap[sig])):
                sigTrees[sig].Add(f)
        bgTrees = {}
        for bg in [
                'dy10', 'dy50', 'ggzz2e2m', 'ggzz2m2t', 'ggzz4e', 'ggzz4m',
                'ggzz4t', 'tt', 'ttw', 'wzz', 'zg', 'zz2l2n', 'zz2l2q', 'zz4l'
        ]:
            bgTrees[bg] = ROOT.TChain('WZTree')
            for f in glob.glob('{0}/{1}/*.root'.format(sampleDir,
                                                       sampleMap[bg])):
                bgTrees[bg].Add(f)

        intLumi = getLumi()

        # add to factory
        for sig in sigTrees:
            self.factory.AddSignalTree(sigTrees[sig], intLumi / intLumis[sig])
        for bg in bgTrees:
            self.factory.AddBackgroundTree(bgTrees[bg], intLumi / intLumis[bg])

        # per event weight
        weight = 'genWeight'
        self.factory.SetWeightExpression(weight)

        # variables
        self.factory.AddVariable('z1_pt', 'F')
        self.factory.AddVariable('z2_pt', 'F')
        self.factory.AddVariable('w1_pt', 'F')
        self.factory.AddVariable('z_mass', 'F')
        self.factory.AddVariable('met_pt', 'F')
        self.factory.AddVariable('numBjetsTight30', 'I')

        # preselection cut
        passCut = ROOT.TCut(
            'z1_passMedium==1 && z2_passMedium==1 && w1_passTight==1')
        self.factory.PrepareTrainingAndTestTree(
            passCut, ":".join([
                "nTrain_Signal=0", "nTrain_Background=0", "SplitMode=Random",
                "NormMode=NumEvents", "!V"
            ]))

        # options:
        # H : display help
        # V : turn on verbosity
        # IgnoreNegWeightsInTraining : ignore events with negative weights for training, keep for testing

        # book method
        method = self.factory.BookMethod(
            ROOT.TMVA.Types.kBDT, "BDT", ":".join([
                "NTrees=850",
                "MaxDepth=3",
                "BoostType=AdaBoost",
                "AdaBoostBeta=0.5",
                "SeparationType=GiniIndex",
                "nCuts=20",
                "PruneMethod=NoPruning",
            ]))
Beispiel #18
0
    def __init__(self,**kwargs):
        inputTreeName = kwargs.pop('inputTreeName','WZTree')
        super(WZTrainer,self).__init__(**kwargs)
        
        sampleDir = 'ntuples/WZ'
        sampleMap = {
            "dy10"     : "DYJetsToLL_M-10to50_TuneCUETP8M1_13TeV-amcatnloFXFX-pythia8",
            "dy50"     : "DYJetsToLL_M-50_TuneCUETP8M1_13TeV-amcatnloFXFX-pythia8",
            "ggzz2e2m" : "GluGluToContinToZZTo2e2mu_13TeV_MCFM701_pythia8",
            "ggzz2e2t" : "GluGluToContinToZZTo2e2tau_13TeV_MCFM701_pythia8",
            "ggzz2m2t" : "GluGluToContinToZZTo2mu2tau_13TeV_MCFM701_pythia8",
            "ggzz4e"   : "GluGluToContinToZZTo4e_13TeV_MCFM701_pythia8",
            "ggzz4m"   : "GluGluToContinToZZTo4mu_13TeV_MCFM701_pythia8",
            "ggzz4t"   : "GluGluToContinToZZTo4tau_13TeV_MCFM701_pythia8",
            "tt"       : "TTJets_TuneCUETP8M1_13TeV-amcatnloFXFX-pythia8",
            "ttw"      : "TTWJetsToLNu_TuneCUETP8M1_13TeV-amcatnloFXFX-madspin-pythia8",
            "w"        : "WJetsToLNu_TuneCUETP8M1_13TeV-amcatnloFXFX-pythia8",
            "ww"       : "WWTo2L2Nu_13TeV-powheg",
            "wz3lnu"   : "WZTo3LNu_TuneCUETP8M1_13TeV-powheg-pythia8",
            "wz2l2q"   : "WZTo2L2Q_13TeV_amcatnloFXFX_madspin_pythia8",
            "wzz"      : "WZZ_TuneCUETP8M1_13TeV-amcatnlo-pythia8",
            "zg"       : "ZGTo2LG_TuneCUETP8M1_13TeV-amcatnloFXFX-pythia8",
            "zz2l2n"   : "ZZTo2L2Nu_13TeV_powheg_pythia8",
            "zz2l2q"   : "ZZTo2L2Q_13TeV_amcatnloFXFX_madspin_pythia8",
            "zz4l"     : "ZZTo4L_13TeV_powheg_pythia8",
            #"tzq"      : "tZq_ll_4f_13TeV-amcatnlo-pythia8_TuneCUETP8M1",
        }

        # get the trees
        intLumis = {}
        for s in sampleMap:
            summedWeights = 0.
            for f in glob.glob('{0}/{1}/*.root'.format(sampleDir, sampleMap[s])):
                tfile = ROOT.TFile.Open(f)
                hist = tfile.Get('summedWeights')
                summedWeights += hist.GetBinContent(1)
                tfile.Close()
            intLumis[s] = float(summedWeights)/getXsec(sampleMap[s])
        sigTrees = {}
        for sig in ['wz3lnu']:
            sigTrees[sig] = ROOT.TChain('WZTree')
            for f in glob.glob('{0}/{1}/*.root'.format(sampleDir,sampleMap[sig])):
                sigTrees[sig].Add(f)
        bgTrees = {}
        for bg in ['dy10','dy50','ggzz2e2m','ggzz2m2t','ggzz4e','ggzz4m','ggzz4t','tt','ttw','wzz','zg','zz2l2n','zz2l2q','zz4l']:
            bgTrees[bg] = ROOT.TChain('WZTree')
            for f in glob.glob('{0}/{1}/*.root'.format(sampleDir,sampleMap[bg])):
                bgTrees[bg].Add(f)

        intLumi = getLumi()

        # add to factory
        for sig in sigTrees:
            self.factory.AddSignalTree(sigTrees[sig],intLumi/intLumis[sig])
        for bg in bgTrees:
            self.factory.AddBackgroundTree(bgTrees[bg],intLumi/intLumis[bg])

        # per event weight
        weight = 'genWeight'
        self.factory.SetWeightExpression(weight)

        # variables
        self.factory.AddVariable('z1_pt','F')
        self.factory.AddVariable('z2_pt','F')
        self.factory.AddVariable('w1_pt','F')
        self.factory.AddVariable('z_mass','F')
        self.factory.AddVariable('met_pt','F')
        self.factory.AddVariable('numBjetsTight30','I')

        # preselection cut
        passCut = ROOT.TCut('z1_passMedium==1 && z2_passMedium==1 && w1_passTight==1')
        self.factory.PrepareTrainingAndTestTree(
            passCut,
            ":".join(
                [
                "nTrain_Signal=0",
                "nTrain_Background=0",
                "SplitMode=Random",
                "NormMode=NumEvents",
                "!V"
                ]
            )
        )

        # options:
        # H : display help
        # V : turn on verbosity
        # IgnoreNegWeightsInTraining : ignore events with negative weights for training, keep for testing

        # book method
        method = self.factory.BookMethod(
            ROOT.TMVA.Types.kBDT,
            "BDT",
            ":".join(
                [
                    "NTrees=850",
                    "MaxDepth=3",
                    "BoostType=AdaBoost",
                    "AdaBoostBeta=0.5",
                    "SeparationType=GiniIndex",
                    "nCuts=20",
                    "PruneMethod=NoPruning",
                ]
            )
        )