Last active
October 30, 2019 13:20
-
-
Save riga/3f77bb4500cb622fbd205a840885fc92 to your computer and use it in GitHub Desktop.
Read PDF values from MiniAOD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
import os | |
def fwlite_loop(path, handle_data=None, start=0, end=-1, object_type="Event"): | |
""" | |
Opens one or more ROOT files defined by *path* and yields the FWLite event. When *handle_data* | |
is not *None*, it is supposed to be a dictionary ``key -> {"type": ..., "label": ...}``. In that | |
case, the handle products are yielded as well in a dictionary, mapped to the key, as | |
``(event, objects dict)``. | |
""" | |
import ROOT | |
ROOT.PyConfig.IgnoreCommandLineOptions = True | |
ROOT.gROOT.SetBatch() | |
ROOT.gSystem.Load("libFWCoreFWLite.so") | |
ROOT.gSystem.Load("libDataFormatsFWLite.so") | |
ROOT.FWLiteEnabler.enable() | |
from DataFormats.FWLite import Events, Runs, Handle | |
paths = path if isinstance(path, (list, tuple)) else [path] | |
handles = {} | |
if handle_data: | |
for key, data in handle_data.items(): | |
handles[key] = Handle(data["type"]) | |
objects = locals()[object_type + "s"](paths) | |
if start > 0: | |
objects.to(start) | |
for i, obj in enumerate(objects): | |
if end >= 0 and (start + i) >= end: | |
break | |
if handle_data: | |
products = {} | |
for key, data in handle_data.items(): | |
obj.getByLabel(data["label"], handles[key]) | |
products[key] = handles[key].product() | |
yield obj, products | |
else: | |
yield obj | |
def read_pdf(path, event=0): | |
handle_data = { | |
"gen": { | |
"type": "GenEventInfoProduct", | |
"label": "generator", | |
}, | |
} | |
for event, data in fwlite_loop(path, handle_data, start=event, end=event + 1): | |
return data["gen"].pdf() | |
if __name__ == "__main__": | |
import sys | |
if len(sys.argv) > 1: | |
path = sys.argv[1] | |
else: | |
path = "/store/mc/RunIIAutumn18MiniAOD/ST_tW_top_5f_inclusiveDecays_TuneCP5_13TeV-powheg-pythia8/MINIAODSIM/102X_upgrade2018_realistic_v15_ext1-v1/20000/E9DED20D-BAB0-4A40-9A4D-77344642CF6B.root" | |
if path.startswith("/store/") and not os.path.exists(path): | |
path = "root://xrootd-cms.infn.it/" + path | |
print("reading PDF values\nfile : {}\nevent: 0\n".format(path)) | |
pdf = read_pdf(path) | |
print("id1: {}".format(pdf.id.first)) | |
print("id2: {}".format(pdf.id.second)) | |
print("x1 : {}".format(pdf.x.first)) | |
print("x2 : {}".format(pdf.x.second)) | |
print("xf1: {}".format(pdf.xPDF.first)) | |
print("xf2: {}".format(pdf.xPDF.second)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run with