Skip to content
Snippets Groups Projects
Commit a36b4f46 authored by Johannes Schumann's avatar Johannes Schumann
Browse files

Event tables as pandas df and weight calculator

parent 3116a2d6
No related branches found
No related tags found
1 merge request!1Merge python environment
Pipeline #14165 failed
...@@ -13,31 +13,31 @@ __email__ = "jschumann@km3net.de" ...@@ -13,31 +13,31 @@ __email__ = "jschumann@km3net.de"
__status__ = "Development" __status__ = "Development"
import re import re
import mmap
import numpy as np import numpy as np
import pylhe
import pandas as pd import pandas as pd
from io import StringIO from io import StringIO
from os import listdir from os import listdir
from os.path import isfile, join, abspath from os.path import isfile, join, abspath
from tempfile import TemporaryDirectory from tempfile import TemporaryDirectory
from collections import defaultdict
import awkward import awkward
import itertools
import uproot import uproot
from scipy.interpolate import UnivariateSpline
from scipy.spatial.transform import Rotation from scipy.spatial.transform import Rotation
from .jobcard import Jobcard, read_jobcard, PDGID_LOOKUP from .jobcard import Jobcard, read_jobcard, PDGID_LOOKUP
EVENT_FILENAME = "FinalEvents.dat" EVENT_FILENAME = "FinalEvents.dat"
LHE_PERT_FILENAME = "EventOutput.Pert.[0-9]{8}.lhe"
LHE_REAL_FILENAME = "EventOutput.Real.[0-9]{8}.lhe"
ROOT_PERT_FILENAME = "EventOutput.Pert.[0-9]{8}.root" ROOT_PERT_FILENAME = "EventOutput.Pert.[0-9]{8}.root"
ROOT_REAL_FILENAME = "EventOutput.Real.[0-9]{8}.root" ROOT_REAL_FILENAME = "EventOutput.Real.[0-9]{8}.root"
FLUXDESCR_FILENAME = "neutrino_initialized_energyFlux.dat" FLUXDESCR_FILENAME = "neutrino_initialized_energyFlux.dat"
XSECTION_FILENAMES = {"all": "neutrino_absorption_cross_section_ALL.dat"} XSECTION_FILENAMES = {"all": "neutrino_absorption_cross_section_ALL.dat"}
PARTICLE_COLUMNS = ["E", "Px", "Py", "Pz", "barcode"] PARTICLE_COLUMNS = ["E", "Px", "Py", "Pz", "barcode"]
EVENTINFO_COLUMNS = [
"weight", "evType", "lepIn_E", "lepIn_Px", "lepIn_Py", "lepIn_Pz",
"lepOut_E", "lepOut_Px", "lepOut_Py", "lepOut_Pz", "nuc_E", "nuc_Px",
"nuc_Py", "nuc_Pz"
]
LHE_NU_INFO_DTYPE = np.dtype([ LHE_NU_INFO_DTYPE = np.dtype([
("type", np.int), ("type", np.int),
...@@ -151,17 +151,27 @@ class GiBUUOutput: ...@@ -151,17 +151,27 @@ class GiBUUOutput:
def _read_flux_file(self): def _read_flux_file(self):
fpath = join(self._data_path, FLUXDESCR_FILENAME) fpath = join(self._data_path, FLUXDESCR_FILENAME)
if isfile(fpath): self.flux_data = np.loadtxt(fpath, dtype=FLUX_INFORMATION_DTYPE)
self.flux_flat = False self.flux_interpolation = UnivariateSpline(self.flux_data["energy"],
self.flux_data = np.loadtxt(fpath, dtype=FLUX_INFORMATION_DTYPE) self.flux_data["events"])
else:
self.flux_flat = True @property
def event_weights(self):
event_df = self.event_info_df
gibuu_wgt = event_df["weight"]
flux = self.flux_interpolation(event_df["lepIn_E"])
energy_min = np.min(self.flux_data["energy"])
energy_max = np.max(self.flux_data["energy"])
total_events = self.flux_interpolation.integral(energy_min, energy_max)
n_files = len(self.root_pert_files)
wgt = np.divide(total_events * gibuu_wgt, flux * n_files)
return wgt
@property @property
def particle_df(self): def particle_df(self):
df = None df = None
for fname in self.root_pert_files: for fname in self.root_pert_files:
fobj = uproot.open(fname) fobj = uproot.open(join(self._data_path, fname))
file_df = None file_df = None
for col in PARTICLE_COLUMNS: for col in PARTICLE_COLUMNS:
tmp = awkward.topandas(fobj["RootTuple"][col].array(), tmp = awkward.topandas(fobj["RootTuple"][col].array(),
...@@ -182,6 +192,19 @@ class GiBUUOutput: ...@@ -182,6 +192,19 @@ class GiBUUOutput:
df = df.rename(columns=dict(enumerate(PARTICLE_COLUMNS))) df = df.rename(columns=dict(enumerate(PARTICLE_COLUMNS)))
return df return df
@property
def event_info_df(self):
df = None
for fname in self.root_pert_files:
fobj = uproot.open(join(self._data_path, fname))
event_data = fobj["RootTuple"]
dct = {k: event_data[k].array() for k in EVENTINFO_COLUMNS}
if df is None:
df = pd.DataFrame(dct)
else:
df = df.append(pd.DataFrame(dct), ignore_index=True)
return df
def write_detector_file(gibuu_output, def write_detector_file(gibuu_output,
ofile="gibuu.aanet.root", ofile="gibuu.aanet.root",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment