def skimRecoFilesParallel(thisfile):
    f = TFile(thisfile)
    t = f.Get('tree')
    newname = thisfile.rstrip('.root') + '_skim.root'
    newFile = TFile(newname, 'recreate')
    #newTree = t.CopyTree('weight!=0.')
    #newTree	= t.CopyTree('metfilters==1 && trigger==1 && onelepton==1 && btags==1 && ak4jetmult==1 && ak4jetcuts==1 && validminimization==1')
    newTree = t.CopyTree(
        'fullselection==1 || wjets_cr_selection==1 || qcd_A_SR_selection==1 || qcd_B_SR_selection==1 || qcd_C_SR_selection==1 || qcd_A_CR_selection==1 || qcd_B_CR_selection==1 || qcd_C_CR_selection==1'
    )
    #newTree = t.CopyTree('fullselection==1')
    #newTree = t.CopyTree('eventTopology==1')
    newTree.Write()
    newFile.Purge()
    newFile.Close()
Beispiel #2
0
    def produce(self):
        from ROOT import TFile, TChain, TH1
        from os.path import exists as pathExists
        from os.path import split as splitPath
        outFilePath = "%s/%s.%s.%s.root" % (self.outPath, "".join(
            self.flags), "processed", self.name)
        #if pathExists(outFilePath):
        #    return
        outFile = TFile(
            "%s/%s.%s.%s.root" %
            (self.outPath, "".join(self.flags), "processed", self.name),
            "recreate")
        for section in self.config.sections():
            trees = None
            if section.startswith("dileptonTree:"):
                treeProducerName = self.config.get(section, "treeProducerName")
                trees = self._getDileptonTrees(section)
                treeName = "DileptonTree"
                subDirName = "%s%s" % (section.split("dileptonTree:")[1],
                                       treeProducerName)
                if "PFHTHLT" in subDirName:
                    subDirName = "%sFinalTrees" % subDirName.split(
                        treeProducerName)[0]
            if section.startswith("isoTree:"):
                treeProducerName = self.config.get(section, "treeProducerName")
                trees = self._getIsoTrees(section)
                treeName = "Iso"
                subDirName = "%s%s" % (section.split("isoTree:")[1],
                                       treeProducerName)
            if not trees == None:
                outDir = None
                srcTree = {}
                for object in trees:

                    srcTree[object] = TChain("%s%s" % (object, treeName))
                    processors = self.config.get(section, "%sProcessors" %
                                                 object).split()
                    filter = " and ".join(processors)
                    if self.config.has_option(section, "%sFilter" % object):
                        filter = self.config.get(section, "%sFilter" % object)
                    for treePath in trees[object]:
                        #srcFile = TFile(filePath,"r")
                        #srcTree = srcFile.Get(treePath)
                        filePath = "%s.root" % treePath.split(".root")[0]
                        inFile = TFile(filePath, "READ")
                        makeCounterSum = eval(
                            self.config.get("general", "counterSum"))
                        print "Add counter sum: %s" % makeCounterSum
                        if not self.counterSum and makeCounterSum:
                            if not outFile.GetDirectory(
                                    "%sCounters" %
                                    section.split("dileptonTree:")[1]):
                                outFile.mkdir(
                                    "%sCounters" %
                                    section.split("dileptonTree:")[1])
                            outFile.cd("%sCounters" %
                                       section.split("dileptonTree:")[1])
                            task = None

                            if "vtxWeighter" in processors:
                                t = self.config.get("general", "tasks")

                                if ".%s." % t in splitPath(filePath)[1]:
                                    assert task == None, "unable to disambiguate tasks '%s' matches both '%s' and '%s'" (
                                        filePath, task, t)
                                    task = t
                                else:
                                    task = t
                            else:
                                for t in self.tasks:
                                    if ".%s." % t in splitPath(filePath)[1]:
                                        assert task == None, "unable to disambiguate tasks '%s' matches both '%s' and '%s'" (
                                            filePath, task, t)
                                        task = t
                            self.counterSum = inFile.Get(
                                "%sCounters/analysis paths" % task).Clone()

                            ## also add 3D weights
                            #outFile.mkdir("%sWeightSummer" % section.split("dileptonTree:")[1])
                            #outFile.cd("%sWeightSummer" % section.split("dileptonTree:")[1])
                            ## task has been defined above
                            #self.weightSum = inFile.Get("%sWeightSummer/Weights"%task).Clone()
                        else:
                            pass
                            #need to cope with different lumis :(
                            #h = inFile.Get("%sCounters/analysis paths"%task)
                            #print inFile, "%sCounters/analysis paths"%task, h
                            #self.counterSum.Add( h,1. )
                        inFile.Close()
                        srcTree[object].Add(treePath)
                        print "adding", treePath
                    srcTree[object].SetBranchStatus("*", 1)

                    ### signal code
                    if (self.treeProcessors[filter].__class__.__name__
                            == SimpleSelector.__name__
                            and self.config.has_option(section,
                                                       "%sFilter" % object)):
                        expression = self.treeProcessors[filter].getExpression(
                            object)
                        print "Cutting tree down to: '%s'" % (expression)
                        outFile.Write()
                        outFile.Close()
                        srcTree[object] = srcTree[object].CopyTree(expression)
                        outFile = TFile(
                            "%s/%s.%s.%s.root" % (self.outPath, "".join(
                                self.flags), "processed", self.name), "UPDATE")
                    #####

                    for processorName in processors:
                        if processorName == "vtxWeighter":
                            srcTree[object].SetBranchStatus("weight", 0)

                        #### default code
                        #~ if (self.treeProcessors[processorName].__class__.__name__ == SimpleSelector.__name__ and not self.config.has_option(section,"%sFilter"%object)):
                        #~ print "Requirements met, applying simple selection boosting =)"
                        #~ expression = self.treeProcessors[processorName].getExpression(object)
                        #~ print "Cutting tree down to: '%s'" % (expression)
                        #~ srcTree[object] = srcTree[object].CopyTree(expression)
                        ######

                        self.treeProcessors[processorName].prepareSrc(
                            srcTree[object], object, self.treeProcessors)
                for object in trees:
                    processors = self.config.get(section, "%sProcessors" %
                                                 object).split()
                    filter = " and ".join(processors)
                    if self.config.has_option(section, "%sFilter" % object):
                        filter = self.config.get(section, "%sFilter" % object)

                    if not outDir:
                        outDir = outFile.mkdir(subDirName)
                    outFile.cd(subDirName)

                    destTree = srcTree[object].CloneTree(0)
                    destTree.SetAutoSave(5000000000)
                    #print processors
                    for processorName in processors:
                        self.treeProcessors[processorName].prepareDest(
                            destTree, object)
                        #~ print "%s: %d" % (str(processorName), self.treeProcessors[processorName].nEntries)
                    endOfLine = 1000
                    for i in srcTree[object]:
                        if endOfLine < 1:
                            pass
                            #continue
                        endOfLine -= 1
                        processingResults = {}

                        ### default code
                        #~ for processorName in processors:
                        #~ processingResults[processorName] = self.treeProcessors[processorName].processEvent(srcTree[object], object)
                        #~ if filter == "" or eval(filter, processingResults):
                        #~ destTree.Fill()
                        ####

                        ### signal code
                        keep = False
                        for processorName in processors:
                            keep = self.treeProcessors[
                                processorName].processEvent(
                                    srcTree[object], object)

                        if keep:
                            destTree.Fill()
                        ####

                    #srcFile.Close()
                    outFile.Write()
                #from pprint import pprint
                #pprint( trees)

        outFile.Purge()
        outFile.Close()
Beispiel #3
0
    def produce(self):
        from ROOT import TFile, TChain, TH1
        from os.path import exists as pathExists
        from os.path import split as splitPath
        outFilePath = "%s/%s.%s.%s.root" % (self.outPath, "".join(
            self.flags), "processed", self.name)
        #if pathExists(outFilePath):
        #    return
        outFile = TFile(
            "%s/%s.%s.%s.root" %
            (self.outPath, "".join(self.flags), "processed", self.name),
            "recreate")
        for section in self.config.sections():
            trees = None
            if section.startswith("dileptonTree:"):
                treeProducerName = self.config.get(section, "treeProducerName")
                trees = self._getDileptonTrees(section)
                treeName = "DileptonTree"
                subDirName = "%s%s" % (section.split("dileptonTree:")[1],
                                       treeProducerName)

            if not trees == None:
                outDir = None
                srcTree = {}
                for object in trees:
                    srcTree[object] = TChain("%s%s" % (object, treeName))
                    processors = self.config.get(section, "%sProcessors" %
                                                 object).split()
                    filter = " and ".join(processors)
                    if self.config.has_option(section, "%sFilter" % object):
                        filter = self.config.get(section, "%sFilter" % object)

                    ### Quick and dirty workaround to prefer certain primary datasets (double muon > double electron > emu)
                    ### To be in synch with ETH group
                    ### Remove duplication of loops and conditions on path if this is to be removed again

                    for treePath in trees[object]:
                        ### Use events from dimuon primary dataset first
                        if not "DoubleMuon" in treePath:
                            continue
                        #srcFile = TFile(filePath,"r")
                        #srcTree = srcFile.Get(treePath)
                        filePath = "%s.root" % treePath.split(".root")[0]
                        inFile = TFile(filePath, "READ")
                        makeCounterSum = eval(
                            self.config.get("general", "counterSum"))
                        print "Add counter sum: %s" % makeCounterSum
                        if not self.counterSum and makeCounterSum:
                            if not outFile.GetDirectory(
                                    "%sCounters" %
                                    section.split("dileptonTree:")[1]):
                                outFile.mkdir(
                                    "%sCounters" %
                                    section.split("dileptonTree:")[1])
                            outFile.cd("%sCounters" %
                                       section.split("dileptonTree:")[1])
                            task = None

                            if "vtxWeighter" in processors:
                                t = self.config.get("general", "tasks")

                                if ".%s." % t in splitPath(filePath)[1]:
                                    assert task == None, "unable to disambiguate tasks '%s' matches both '%s' and '%s'" (
                                        filePath, task, t)
                                    task = t
                                else:
                                    task = t
                            else:
                                for t in self.tasks:
                                    if ".%s." % t in splitPath(filePath)[1]:
                                        assert task == None, "unable to disambiguate tasks '%s' matches both '%s' and '%s'" (
                                            filePath, task, t)
                                        task = t
                            self.counterSum = inFile.Get(
                                "%sCounters/analysis paths" % task).Clone()

                        else:
                            pass

                        inFile.Close()
                        srcTree[object].Add(treePath)
                        print "adding", treePath

                    for treePath in trees[object]:
                        ### Then take ee primary dataset
                        if not "DoubleElectron" in treePath:
                            continue
                        #srcFile = TFile(filePath,"r")
                        #srcTree = srcFile.Get(treePath)
                        filePath = "%s.root" % treePath.split(".root")[0]
                        inFile = TFile(filePath, "READ")
                        makeCounterSum = eval(
                            self.config.get("general", "counterSum"))
                        print "Add counter sum: %s" % makeCounterSum
                        if not self.counterSum and makeCounterSum:
                            if not outFile.GetDirectory(
                                    "%sCounters" %
                                    section.split("dileptonTree:")[1]):
                                outFile.mkdir(
                                    "%sCounters" %
                                    section.split("dileptonTree:")[1])
                            outFile.cd("%sCounters" %
                                       section.split("dileptonTree:")[1])
                            task = None

                            if "vtxWeighter" in processors:
                                t = self.config.get("general", "tasks")

                                if ".%s." % t in splitPath(filePath)[1]:
                                    assert task == None, "unable to disambiguate tasks '%s' matches both '%s' and '%s'" (
                                        filePath, task, t)
                                    task = t
                                else:
                                    task = t
                            else:
                                for t in self.tasks:
                                    if ".%s." % t in splitPath(filePath)[1]:
                                        assert task == None, "unable to disambiguate tasks '%s' matches both '%s' and '%s'" (
                                            filePath, task, t)
                                        task = t
                            self.counterSum = inFile.Get(
                                "%sCounters/analysis paths" % task).Clone()

                        else:
                            pass

                        inFile.Close()
                        srcTree[object].Add(treePath)
                        print "adding", treePath

                    for treePath in trees[object]:
                        ### Now the rest, but avoid taking a tree twice
                        if "DoubleElectron" in treePath or "DoubleMuon" in treePath:
                            continue
                        #srcFile = TFile(filePath,"r")
                        #srcTree = srcFile.Get(treePath)
                        filePath = "%s.root" % treePath.split(".root")[0]
                        inFile = TFile(filePath, "READ")
                        makeCounterSum = eval(
                            self.config.get("general", "counterSum"))
                        print "Add counter sum: %s" % makeCounterSum
                        if not self.counterSum and makeCounterSum:
                            if not outFile.GetDirectory(
                                    "%sCounters" %
                                    section.split("dileptonTree:")[1]):
                                outFile.mkdir(
                                    "%sCounters" %
                                    section.split("dileptonTree:")[1])
                            outFile.cd("%sCounters" %
                                       section.split("dileptonTree:")[1])
                            task = None

                            if "vtxWeighter" in processors:
                                t = self.config.get("general", "tasks")

                                if ".%s." % t in splitPath(filePath)[1]:
                                    assert task == None, "unable to disambiguate tasks '%s' matches both '%s' and '%s'" (
                                        filePath, task, t)
                                    task = t
                                else:
                                    task = t
                            else:
                                for t in self.tasks:
                                    if ".%s." % t in splitPath(filePath)[1]:
                                        assert task == None, "unable to disambiguate tasks '%s' matches both '%s' and '%s'" (
                                            filePath, task, t)
                                        task = t
                            self.counterSum = inFile.Get(
                                "%sCounters/analysis paths" % task).Clone()

                        else:
                            pass

                        inFile.Close()
                        srcTree[object].Add(treePath)
                        print "adding", treePath

                    srcTree[object].SetBranchStatus("*", 1)

                    for processorName in processors:
                        if processorName == "vtxWeighter":
                            srcTree[object].SetBranchStatus("weight", 0)

                        #### old code
                        if (self.treeProcessors[processorName].__class__.
                                __name__ == SimpleSelector.__name__
                                and not self.config.has_option(
                                    section, "%sFilter" % object)):
                            print "Requirements met, applying simple selection boosting =)"
                            expression = self.treeProcessors[
                                processorName].getExpression(object)
                            print "Cutting tree down to: '%s'" % (expression)
                            srcTree[object] = srcTree[object].CopyTree(
                                expression)
                        ######

                        self.treeProcessors[processorName].prepareSrc(
                            srcTree[object], object, self.treeProcessors)
                for object in trees:
                    processors = self.config.get(section, "%sProcessors" %
                                                 object).split()
                    filter = " and ".join(processors)
                    if self.config.has_option(section, "%sFilter" % object):
                        filter = self.config.get(section, "%sFilter" % object)

                    if not outDir:
                        outDir = outFile.mkdir(subDirName)
                    outFile.cd(subDirName)

                    destTree = srcTree[object].CloneTree(0)
                    destTree.SetAutoSave(5000000000)
                    #print processors
                    for processorName in processors:
                        self.treeProcessors[processorName].prepareDest(
                            destTree, object)
                        #~ print "%s: %d" % (str(processorName), self.treeProcessors[processorName].nEntries)
                    endOfLine = 1000
                    for i in srcTree[object]:
                        if endOfLine < 1:
                            pass
                            #continue
                        endOfLine -= 1
                        processingResults = {}

                        ### old code
                        for processorName in processors:
                            processingResults[
                                processorName] = self.treeProcessors[
                                    processorName].processEvent(
                                        srcTree[object], object)
                        if filter == "" or eval(filter, processingResults):
                            destTree.Fill()
                        ####

                    #srcFile.Close()
                    outFile.Write()
                #from pprint import pprint
                #pprint( trees)

        outFile.Purge()
        outFile.Close()