Esempio n. 1
0
def nonEmptyFile(f, treeName='Events'):
    logger.info("Checking file: %s" % f)
    rf = ROOT.TFile.Open(f)
    if not rf: return False
    tree = getattr(rf, treeName)
    nonEmpty = True if tree.GetEntries() else False
    if not nonEmpty: logger.info("File is empty")
    rf.Close()
    return nonEmpty
Esempio n. 2
0
def calcMETSig( event, sample ):
    if sample.isData:
        JER    = JERData
        params = paramsData
    else:
        JER    = JERMC
        params = paramsMC

    ev = Event(event, JER, isData=sample.isData, METPtVar=METPtVar, METPhiVar=METPhiVar, JetCollection=JetCollection, vetoEtaRegion=vetoEtaRegion, jetThreshold=jetThresh, pTdepMetSig=pTdependent)
    ev.calcMETSig(params)
    event.MET_significance_rec = ev.MET_sig
    if args.test:
        logger.info('MET Sig: %f, %f'%(ev.MET_sig, event.MET_significance))
    event.Jet_dpt = [ x*ev.Jet_pt[i] for i,x in enumerate(ev.Jet_dpt) ] if len(ev.Jet_dpt) > 0 else [0]
Esempio n. 3
0
    def makeTemplate(self, selection, weight='(1)'):
        logger.info(
            "Make PU profile for sample %s and selection %s and weight %s",
            self.source_sample.name, selection, weight)

        h_source = self.source_sample.get1DHistoFromDraw(
            self.draw_string,
            self.binning,
            selectionString=selection,
            weightString=weight)
        logger.info("PU histogram contains %s weighted events",
                    h_source.Integral())
        h_source.Scale(1. / h_source.Integral())
        return h_source
Esempio n. 4
0
    def minimize(self, start=None, step=None, maxSig=25):
        gmin = ROOT.Math.Factory.CreateMinimizer("Minuit2")
        gmin.SetTolerance(10.0)
        gmin.SetStrategy(0)
        gmin.SetPrintLevel(3)

        step = step if step else self.defaultStep
        start = start if start else self.defaultStart

        LL = minLL(self, pTdepMetSig=self.pTdepMetSig)

        gmin.SetFunction(LL)

        logger.info("Minimizing parameters %s" % self.variableNames)
        logger.info("With stepsize of %s" % step)
        logger.info("And starting values %s" % start)

        for i in range(self.paramRange):
            gmin.SetVariable(i, self.variableNames[i], start[i], step[i])

        gmin.Minimize()

        #filter events with high significance
        self.eventlist = filter(lambda x: x.MET_sig < maxSig, self.eventlist)

        logger.info("Now fitting after applying significance cut")
        logger.info("Total events: %i" % len(self.eventlist))

        gmin.SetStrategy(1)
        gmin.Minimize()
        gmin.Hesse()

        pars = [gmin.X()[i] for i in range(self.paramRange)]
        uncs = [gmin.Errors()[i] for i in range(self.paramRange)]
        #        pars = map( float, gmin.X() )
        #        uncs = map( float, gmin.Errors() )

        with open(self.outfile + ".txt", "w") as of:
            json.dump(pars, of)
        with open(self.outfile + "_unc.txt", "w") as of:
            json.dump(uncs, of)
Esempio n. 5
0
 def cachedTemplate(self,
                    selection,
                    weight='(1)',
                    save=True,
                    overwrite=False):
     key = {
         "selection": selection,
         "weight": weight,
         "source": self.source_sample.name
     }
     if (self.cache and self.cache.contains(key)) and not overwrite:
         result = self.cache.get(key)
         logger.info("Loaded MC PU profile from %s" %
                     (self.cache.database_file))
         logger.debug("Key used: %s result: %r" % (key, result))
     elif self.cache:
         logger.info("Obtain PU profile for %s" % (key, ))
         result = self.makeTemplate(selection=selection, weight=weight)
         if result:
             result = self.cache.addData(key, result, overwrite=save)
             logger.info("Adding PU profile to cache for %s : %r" %
                         (key, result))
         else:
             logger.warning(
                 "Couldn't create PU profile to cache for %s : %r" %
                 (key, result))
     else:
         result = self.makeTemplate(selection=selection, weight=weight)
     return result
Esempio n. 6
0
        revCumBkg = [totalBkg] * p.binning[0]

        for i in range(p.binning[0]):
            revCumSignal[i] += -cumulativeSignal[i]
            revCumBkg[i] += -cumulativeBkg[i]

        tmp = ROOT.TGraph()
        for i in range(p.binning[0]):
            tmp.SetPoint(i, revCumSignal[i] / totalSignal,
                         1 - revCumBkg[i] / totalBkg)
        tmp.SetPoint(p.binning[0], 0, 1)
        #tmp.style          = styles.lineStyle(colors[j])
        tmp.SetLineColor(colors[j])
        tmp.SetLineWidth(2)
        tmp.SetMarkerSize(0)
        metSigRoc.append(copy.deepcopy(tmp))

    plotting.draw(
        plot=dummyPlot,
        plot_directory=os.path.join(plot_directory, 'analysis_plots',
                                    str(year), args.plot_directory, mode,
                                    args.selection),
        logX=False,
        logY=False,
        legend=None,
        yRange=(0.0, 1.0),
        drawObjects=drawObjects(not args.noData, 1.00, lumi_scale) + metSigRoc,
    )

logger.info("Done.")
Esempio n. 7
0
    ov = options.overwrite
    if options.update:
        ov = 'update'
else:
    import logging
    logger = logging.getLogger(__name__)
    ov = False

# Redirector
from nanoMET.tools.user import redirector_global as redirector

# DB
from nanoMET.tools.user import dbDir
dbFile = dbDir + "/samples/DB_Summer16_central_legacy.sql"

logger.info("Using db file: %s", dbFile)

## DY
DYJetsToLL_M50_LO_ext1 = Sample.nanoAODfromDAS(
    "DYJetsToLL_M50_LO_ext1",
    "/DYJetsToLL_M-50_TuneCUETP8M1_13TeV-madgraphMLM-pythia8/RunIISummer16NanoAODv7-PUMoriond17_Nano02Apr2020_102X_mcRun2_asymptotic_v8_ext1-v1/NANOAODSIM",
    dbFile=dbFile,
    redirector=redirector,
    instance="phys03",
    overwrite=ov,
    xSection=2075.14 * 3)

DY = [
    DYJetsToLL_M50_LO_ext1,
]
Esempio n. 8
0
if year == 2016:
    from nanoMET.nanoAOD.Summer16_private_legacy_v1 import allSamples as Summer16_private_legacy_v1
    from nanoMET.nanoAOD.Run2016_17Jul2018_private import allSamples as Run2016_17Jul2018_private
    allSamples = Summer16_private_legacy_v1 + Run2016_17Jul2018_private
elif year == 2017:
    from nanoMET.nanoAOD.Fall17_private_legacy_v1 import allSamples as Fall17_private_legacy_v1
    from nanoMET.nanoAOD.Run2017_31Mar2018_private import allSamples as Run2017_31Mar2018_private
    allSamples = Fall17_private_legacy_v1 + Run2017_31Mar2018_private
elif year == 2018:
    #from nanoMET.nanoAOD.Autumn18_private_legacy_v1 import allSamples as Autumn18_private_legacy_v1
    #from nanoMET.nanoAOD.Run2018_17Sep2018_private import allSamples as Run2018_17Sep2018_private
    from nanoMET.nanoAOD.Run2018_02Apr2020_private import allSamples as Run2018_02Apr2020_private  #LOR added it to Switch to nanoAODv7 02Apr2020
    from nanoMET.nanoAOD.Autumn18_central_legacy_v1 import allSamples as Autumn18_central_legacy_v1
    allSamples = Autumn18_central_legacy_v1 + Run2018_02Apr2020_private

logger.info("Searching for sample %s" % options.samples[0])

samples = []
for selectedSample in options.samples:
    for sample in allSamples:
        if selectedSample == sample.name:
            samples.append(sample)
            logger.info("Adding sample %s", sample.name)
            logger.info("Sample has normalization %s", sample.normalization)
            sample.normalization = float(sample.normalization)

if len(samples) == 0:
    logger.info("No samples found. Was looking for %s. Exiting" %
                options.samples)
    sys.exit(-1)
Esempio n. 9
0
    def __init__(self,
                 samples,
                 selection,
                 jetResolution,
                 outfile="results/tune",
                 METPtVar="MET_pt",
                 METPhiVar="MET_phi",
                 JetCollection="Jet_pt",
                 maxN=1e6,
                 vetoEtaRegion=(10, 10),
                 jetThreshold=15.,
                 puWeight="puWeight",
                 ttbarModifier=1,
                 pTdepMetSig=True,
                 nvtxHist=None):
        # Need fill a list in order to do the minimization, reading from the tree is too slow

        self.eventlist = []
        if samples[0].isData:
            self.variables = map(TreeVariable.fromString, [
                "nJet/I", "fixedGridRhoFastjetAll/F",
                "%s/F" % METPtVar,
                "%s/F" % METPhiVar, "MET_sumPtUnclustered/F"
            ])
        else:
            self.variables = map(TreeVariable.fromString, [
                "weight/F", "puWeight/F", "puWeightUp/F", "puWeightDown/F",
                "nJet/I", "fixedGridRhoFastjetAll/F", "PV_npvsGood/I",
                "%s/F" % METPtVar,
                "%s/F" % METPhiVar, "MET_sumPtUnclustered/F"
            ])
        self.variables += [
            VectorTreeVariable.fromString(
                "Jet[pt/F,eta/F,phi/F,cleanmask/O,jetId/I,cleanmaskMETSig/I,cleanmaskMETSigRec/I]"
            )
        ]
        if JetCollection == "Jet_pt_nom":
            self.variables += [VectorTreeVariable.fromString("Jet[pt_nom/F]")]
        elif JetCollection == "Jet_pt_jerUp":
            self.variables += [
                VectorTreeVariable.fromString("Jet[pt_jerUp/F]")
            ]

        self.outfile = os.path.abspath(outfile)
        self.outdir = os.path.dirname(self.outfile)

        if not os.path.isdir(self.outdir):
            os.makedirs(self.outdir)

        self.pTdepMetSig = pTdepMetSig
        self.paramRange = 12 if self.pTdepMetSig else 7

        self.variableNames = ["a%i" % i for i in range(self.paramRange - 2)
                              ] + ["u1", "u2"]
        self.defaultStep = [0.05] * self.paramRange
        self.defaultStart = [1.5] * (self.paramRange - 2) + [0., .5]

        for s in samples:
            logger.info("Now working on sample: %s" % s.name)
            s.setSelectionString(selection)
            logger.info("Getting number of events")
            nEvents = s.getYieldFromDraw(selectionString="(1)",
                                         weightString="(1)")
            logger.info("Running over %s events for sample %s." %
                        (nEvents["val"], s.name))
            nEvents = nEvents["val"]
            if nEvents > maxN:
                weightModifier = nEvents / float(maxN)
                fracToKeep = float(maxN) / nEvents
                logger.info(
                    "Preliminary factor for modifying the event weight: %s" %
                    weightModifier)
            else:
                weightModifier = 1
                fracToKeep = 1

            if "top" in s.name.lower():
                weightModifier *= ttbarModifier
            logger.info("Filling the eventlist")
            reader = s.treeReader(variables=self.variables)
            reader.start()
            i = 0
            tmp_eventlist = []
            while reader.run():
                i += 1
                nvtxWeight = 1
                if random.random() < fracToKeep:
                    if nvtxHist:
                        nvtxWeight = getNVtxWeight(nvtxHist,
                                                   reader.event.PV_npvsGood)
                    tmp_eventlist += [
                        Event(reader.event,
                              jetResolution,
                              weightModifier=weightModifier * nvtxWeight,
                              isData=s.isData,
                              METPtVar=METPtVar,
                              METPhiVar=METPhiVar,
                              JetCollection=JetCollection,
                              vetoEtaRegion=vetoEtaRegion,
                              jetThreshold=jetThreshold,
                              puWeight=puWeight,
                              pTdepMetSig=self.pTdepMetSig)
                    ]
                update_progress(i / nEvents)

            self.eventlist += tmp_eventlist
Esempio n. 10
0
import nanoMET.tools.logger as logger
import RootTools.core.logger as logger_rt
logger = logger.get_logger(args.logLevel, logFile=None)
logger_rt = logger_rt.get_logger(args.logLevel, logFile=None)


def chunks(l, n):
    n = max(1, n)
    return (l[i:i + n] for i in xrange(0, len(l), n))


filePath = str(options.dir[0]) + '/*.root'
files = glob.glob(filePath)

for i, c in enumerate(chunks(files, options.nFiles)):

    outFile = "%s/merged_nanoAOD_%s.root" % (options.dir[0], i)

    nCorrupted = 0
    for f in c:
        if not helpers.checkRootFile(f):
            logger.info("File corrupted? %s", f)
            nCorrupted += 1

    logger.info("Number of corrupted files %s", nCorrupted)

    inFiles = ' '.join(c)

    logger.info("Files: %s" % inFiles)
    os.system("haddnano.py %s %s" % (outFile, inFiles))
Esempio n. 11
0
                       default=2016,
                       choices=[2016, 2017, 2018],
                       help='Which year?')
args = argParser.parse_args()

# Logger
import nanoMET.tools.logger as logger
import RootTools.core.logger as logger_rt

logger = logger.get_logger(args.logLevel, logFile=None)
logger_rt = logger_rt.get_logger(args.logLevel, logFile=None)

jetSelection = "nJetSelected"
bJetSelectionM = "nBTag"

logger.info("Starting")

# Systematics to run over
jet_systematics = ['jesTotalUp', 'jesTotalDown']
if not args.skipJER:
    jet_systematics += ['jerUp', 'jerDown']
met_systematics = ['unclustEnUp', 'unclustEnDown'
                   ] if not args.skipUnclEn else []
jme_systematics = jet_systematics + met_systematics
weight_systematics = ['puWeightUp', 'puWeightDown']

if args.selectSys != "all" and args.selectSys != "combine":
    all_systematics = [args.selectSys if args.selectSys != 'None' else None]
else:
    all_systematics = [None] + weight_systematics + jme_systematics
Esempio n. 12
0
allSamples = []
if year == 2016:
    from Samples.nanoAOD.Summer16_private_legacy_v1 import allSamples as Summer16_private_legacy_v1
    from Samples.nanoAOD.Run2016_17Jul2018_private import allSamples as Run2016_17Jul2018_private
    allSamples = Summer16_private_legacy_v1 + Run2016_17Jul2018_private
elif year == 2017:
    from Samples.nanoAOD.Fall17_private_legacy_v1 import allSamples as Fall17_private_legacy_v1
    from Samples.nanoAOD.Run2017_31Mar2018_private import allSamples as Run2017_31Mar2018_private
    allSamples = Fall17_private_legacy_v1 + Run2017_31Mar2018_private
elif year == 2018:
    from Samples.nanoAOD.Autumn18_private_legacy_v1 import allSamples as Autumn18_private_legacy_v1
    from Samples.nanoAOD.Run2018_17Sep2018_private import allSamples as Run2018_17Sep2018_private
    allSamples = Autumn18_private_legacy_v1 + Run2018_17Sep2018_private

logger.info("Searching for sample %s" % options.samples[0])

samples = []
for selectedSample in options.samples:
    for sample in allSamples:
        if selectedSample == sample.name:
            samples.append(sample)
            logger.info("Adding sample %s", sample.name)
            logger.info("Sample has normalization %s", sample.normalization)
            sample.normalization = float(sample.normalization)

if len(samples) == 0:
    logger.info("No samples found. Was looking for %s. Exiting" %
                options.samples)
    sys.exit(-1)
Esempio n. 13
0
            weightString=weight)
        logger.info("PU histogram contains %s weighted events",
                    h_source.Integral())
        h_source.Scale(1. / h_source.Integral())
        return h_source


if __name__ == "__main__":

    # Get all NanoAOD tuples for caching
    from nanoMET.nanoAOD.Fall17_private_legacy_v1 import *
    from nanoMET.tools.user import plot_directory

    if args.overwrite: os.remove(dbDir + "/puProfiles/puProfiles_v2.sql")
    for sample in allSamples:
        logger.info("Working on samples %s", sample.name)
        puProfiles = puProfile(source_sample=sample,
                               cacheDir=dbDir + "/puProfiles/")

        # reweighting selection
        selection = "( 1 )"
        profile = puProfiles.cachedTemplate(selection,
                                            weight='genWeight',
                                            overwrite=False)

        # plot the MC PU profile
        if args.makePlots:
            profilePlot = Plot.fromHisto(sample.name,
                                         texX="nTrueInt",
                                         histos=[[profile]])
            plotting.draw(profilePlot,