Skip to content

Commit

Permalink
Merge pull request #1 from GaelTouquet/master
Browse files Browse the repository at this point in the history
convertrootfile update
  • Loading branch information
YohannFaure committed Jun 19, 2018
2 parents 8a996ec + 5b7eaad commit 57ba84a
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 74 deletions.
116 changes: 116 additions & 0 deletions converttorecnnfiles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
"""
Preprocessing our files to a usable format for the recnn program.
input format :
[ [[ p1, p2, p3, ... ]] , [[ p1, p2, p3, ... ]] , [[ p1, p2, p3, ... ]] , ...]
pi = [E,px,py,pz,id ...]
with "fake particles", i.e. pi=[0.,0.,0.,0.,...]
output format :
[ [ p1, p2, ... ] , ...]
pi = [E,px,py,pz,id=0]
without fake particles
"""


import numpy as np
import sys
import os
import multiprocessing as mp
from ROOT import TFile, TLorentzVector
from root_numpy import tree2array
from functools import partial



def multithreadmap(f,X,ncores=20, **kwargs):
"""
multithreading map of a function, default on 20 cpu cores.
"""
func = partial(f, **kwargs)
p=mp.Pool(ncores)
Xout = p.map(func,X)
p.terminate()
return(Xout)


#now useless
def extract_first_element(jet):
"""
extract first element of the jet : its components.
"""
return(jet[0])


def find_first_non_particle(jet):
"""
dichotomy search of first non particle of the jet
"""
n=len(jet)
found = False
first = 0
last = n-1
while not found and first <= last :
m = (last+first)//2
if jet[m][0] == 0. and jet[m-1][0] != 0. :
found = True
elif jet[m][0] != 0. :
first = m+1
else :
last = m-1
return(m)

def select_particle_features(jet, addID=False):
"""
select E,px,py,pz of the particle.
"""
if addID:
return(jet[:,[3,0,1,2,0]])
else:
return(jet[:,[3,0,1,2]])


def etatrimtree(tree):
newtree = tree.CloneTree(0)
for event in tree:
vect = TLorentzVector()
vect.SetPxPyPzE(event.Jet[0],event.Jet[1],event.Jet[2],event.Jet[3])
if vect.Eta()<2.6:
newtree.Fill()
return newtree

def converttorecnnfiles(input_file, addID=False, etacut=True):
#load data
f = TFile(input_file)
tree = f.Get('tree')
if etacut:
tmpfile = TFile('tmp.root','recreate')
tree = etatrimtree(tree)
jets_array = tree2array(tree,'ptcs')
if etacut:
os.remove('tmp.root')

#process
indexes = multithreadmap(find_first_non_particle, jets_array)

jets_array = list(jets_array)

for i in range(len(jets_array)):
jets_array[i] = jets_array[i][:indexes[i]]

jets_array = multithreadmap(select_particle_features,jets_array, addID=addID)

np.save(input_file[:input_file.find('.')],
jets_array)



if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description='Convert Dataformated ROOTfiles to numpy files usable in the notebooks.')
parser.add_argument("rootfile", help="path to the ROOTfile to be converted",type=str)
# parser.add_argument("--branchlist", help="list of branches to keep", nargs='+') # for now respect naming scheme used to produce trees
parser.add_argument("--addID", help="wether or not to add the pdgID in the output", action="store_true")
# parser.add_argument("--cut", help="what cut to apply to the rootfile events e.g. ",type=str)
args = parser.parse_args()

converttorecnnfiles(args.rootfile, addID=args.addID)
1 change: 0 additions & 1 deletion data

This file was deleted.

5 changes: 0 additions & 5 deletions model.pickle

This file was deleted.

38 changes: 10 additions & 28 deletions notebooks/01-load-events.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,7 @@
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"import copy\n",
Expand Down Expand Up @@ -251,7 +249,6 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [],
Expand Down Expand Up @@ -343,9 +340,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"# jet 4 momenta\n",
Expand Down Expand Up @@ -379,9 +374,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"# all original 4 momenta\n",
Expand Down Expand Up @@ -467,9 +460,7 @@
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"data = open(\"../data/events/antikt-kt-train.pickle\", \"rb\")\n",
Expand All @@ -494,9 +485,7 @@
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [
{
"data": {
Expand Down Expand Up @@ -589,9 +578,7 @@
{
"cell_type": "code",
"execution_count": 86,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"%%cython -f -+ -I/usr/local/include --link-args=-Wl,-rpath,/usr/local/lib -lm -L/usr/local/lib -lfastjettools -lfastjet -lfastjetplugins -lsiscone_spherical -lsiscone\n",
Expand Down Expand Up @@ -775,7 +762,6 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"scrolled": false
},
"outputs": [],
Expand Down Expand Up @@ -867,9 +853,7 @@
{
"cell_type": "code",
"execution_count": 90,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"data = open(\"../data/events/antikt-kt-towers-test.pickle\", \"rb\")\n",
Expand All @@ -894,9 +878,7 @@
{
"cell_type": "code",
"execution_count": 91,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [
{
"data": {
Expand Down Expand Up @@ -939,9 +921,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.11"
"version": "2.7.14"
}
},
"nbformat": 4,
"nbformat_minor": 0
"nbformat_minor": 1
}

0 comments on commit 57ba84a

Please sign in to comment.