-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathskim_edepsim_tree.py
More file actions
133 lines (108 loc) · 4.68 KB
/
skim_edepsim_tree.py
File metadata and controls
133 lines (108 loc) · 4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import ROOT
import numpy as np
import json
import argparse
import edep_tree
def create_default_config_file():
# Create a configuration dictionary
config = {
"GeoCuts": {
"Vertex_min_x": -800,
"Vertex_max_x": 800,
"Vertex_min_y": -800,
"Vertex_max_y": 800,
"Vertex_min_z": -400,
"Vertex_max_z": 400
},
"TrackCuts": {
"Min_number": 1,
"Max_number": 9999,
"Min_length_mm": 0,
"Min_en_deposit_MeV": 0
},
"InteractionCuts": {
"Allowed_interactions": ""
}
}
# Write the configuration to a file
with open('edepsim_tree_cuts.json', 'w') as configfile:
json.dump(config, configfile, indent=4)
def read_config_file(config_name):
# Read the configuration file
with open(config_name, 'r') as configfile:
config = json.load(configfile)
return config
def get_transform_to_local_coord(file_name):
ROOT.gSystem.Load("libGeom")
ROOT.TGeoManager.Import(file_name)
ROOT.gGeoManager.cd('/volWorld_PV/rockBox_lv_PV_0/volDetEnclosure_PV_0/volSAND_PV_0/MagIntVol_volume_PV_0/sand_inner_volume_PV_0/GRAIN_lv_PV_0/GRAIN_LAr_lv_PV_0')
local = np.array([0.,0.,0.])
master = np.array([0.,0.,0.])
ROOT.gGeoManager.LocalToMaster(local, master)
return master
def skim_tree(tree, cuts, master_transform):
new_tree = tree.CloneTree(0)
primaries_tot_lengths = {}
primaries_tot_en_deposits = {}
for entry in tree:
# InteractionCuts
if not cuts["InteractionCuts"]["Allowed_interactions"] == "":
interaction_list = cuts["InteractionCuts"]["Allowed_interactions"].split('-')
evt_interaction = entry.Event.Primaries[0].GetReaction().split("proc:")[1]
if not any(interaction in evt_interaction for interaction in interaction_list):
continue
# GeoCuts
vertex = entry.Event.Primaries[0].GetPosition()
if not cuts["GeoCuts"]["Vertex_min_x"] <= vertex.X() - master_transform[0] <= cuts["GeoCuts"]["Vertex_max_x"]:
continue
if not cuts["GeoCuts"]["Vertex_min_y"] <= vertex.Y() - master_transform[1] <= cuts["GeoCuts"]["Vertex_max_y"]:
continue
if not cuts["GeoCuts"]["Vertex_min_z"] <= vertex.Z() - master_transform[2] <= cuts["GeoCuts"]["Vertex_max_z"]:
continue
# TrackCuts
segments = entry.Event.SegmentDetectors
count_tracks = 0
for s in segments:
if s.first == "LArHit":
for v in s.second:
if v.GetPrimaryId() in primaries_tot_lengths:
primaries_tot_lengths[v.GetPrimaryId()] += v.GetTrackLength()
primaries_tot_en_deposits[v.GetPrimaryId()] += v.GetEnergyDeposit()
else:
primaries_tot_lengths[v.GetPrimaryId()] = v.GetTrackLength()
primaries_tot_en_deposits[v.GetPrimaryId()] = v.GetEnergyDeposit()
for l in primaries_tot_lengths:
if primaries_tot_lengths[l] >= cuts["TrackCuts"]["Min_length_mm"] and primaries_tot_en_deposits[l] >= cuts["TrackCuts"]["Min_en_deposit_MeV"]:
count_tracks += 1
primaries_tot_lengths.clear()
primaries_tot_en_deposits.clear()
if not cuts["TrackCuts"]["Min_number"] <= count_tracks <= cuts["TrackCuts"]["Max_number"]:
continue
new_tree.Fill()
return new_tree
def create_skimmed_file(file_name, output_name, cuts):
# Disable pop-ups
ROOT.gROOT.SetBatch(True)
# Open file
file = ROOT.TFile.Open(file_name)
tree = file.Get("EDepSimEvents")
master_transform = get_transform_to_local_coord(file_name)
output_file = ROOT.TFile(output_name, "RECREATE")
# Get skimmed tree
new_tree = skim_tree(tree, cuts, master_transform)
# Write the new tree to the new file and close the files
new_tree.AutoSave()
edep_tree.copy_detsim_trees(file, output_file)
edep_tree.copy_geometry(file, output_file)
output_file.Close()
file.Close()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("input_file", type=str, help="EDepSim input file")
parser.add_argument("--config_file", type=str, default="edepsim_tree_cuts.json", help="json configuration file")
parser.add_argument("--output_file", type=str, default="output/skimmed.root", help="Output file name")
args = parser.parse_args()
#create_default_config_file()
cuts = read_config_file(args.config_file)
print(cuts)
create_skimmed_file(args.input_file, args.output_file, cuts)