O2-DQ User Interface 1.0.0
Loading...
Searching...
No Matches
getTTrees.py
Go to the documentation of this file.
1#!/usr/bin/env python
2# PYTHON_ARGCOMPLETE_OK
3# -*- coding: utf-8 -*-
4
5# Copyright 2019-2020 CERN and copyright holders of ALICE O2.
6# See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
7# All rights not expressly granted are reserved.
8#
9# This software is distributed under the terms of the GNU General Public
10# License v3 (GPL Version 3), copied verbatim in the file "COPYING".
11#
12# In applying this license CERN does not waive the privileges and immunities
13# granted to it by virtue of its status as an Intergovernmental Organization
14# or submit itself to any jurisdiction.
15
16# \Author: ionut.cristian.arsene@cern.ch
17# \Interface: cevat.batuhan.tolon@cern.ch
18
19import ROOT
20import logging
21
22
23def Map(tf, browsable_to, tpath = None):
24 """
25 Maps objets as dict[obj_name][0] using a TFile (tf) and TObject to browse.
26 """
27 m = {}
28 for k in browsable_to.GetListOfKeys():
29 n = k.GetName()
30 if tpath == None:
31 m[n] = [tf.Get(n)]
32 else:
33 m[n] = [tf.Get(tpath + "/" + n)]
34 return m
35
36
37def ExpandDeepTDirs(tf, to_map):
38 """
39 A deep-mapping function for one TDirectory
40 """
41
42 tpath = None # dummy param
43 names = sorted(to_map.keys())
44 # print("names = ",names)
45 for n in names:
46 # print("n =", n)
47 if len(to_map[n]) != 1:
48 continue
49 if tpath == None:
50 tpath_ = n
51 else:
52 tpath_ = tpath + "/" + n
53
54 tobject = to_map[n][0]
55 if type(tobject) is ROOT.TDirectoryFile:
56 m = Map(tf, tobject, tpath_)
57 #print(m)
58 to_map[n].append(m)
59 # print("TO MAP = ", to_map[n])
60 return to_map[n] #
61
62
63def MappingTFile(filename):
64 """
65 Get TTree names from one Data Frame
66 """
67 if filename.endswith("txt") or filename.endswith("text"):
68 with open(filename) as f:
69 for line in f:
70 line = line.strip()
71 if line.endswith(".root"):
72 logging.info("Converter manager will use this file from text list : %s", line)
73 filename = line
74 break
75
76 f = ROOT.TFile(filename)
77 m = Map(f, f)
78 # print("MAP = ",m)
79 # print("FİLE = ",f)
80 return ExpandDeepTDirs(f, m)
81
82
83def getTTrees(aod: str):
84 """ Get TTrees from one DF
85
86 Args:
87 aod (CLI Argument): CLI Argument for AO2D.root File
88
89 Returns:
90 list: list of all ttres names in provided AO2D.root File
91 """
92 textAodList = aod.startswith("@")
93 endsWithTxt = aod.endswith("txt") or aod.endswith("text")
94 ttreeList = []
95
96 # Management for text aod list files
97 if textAodList and endsWithTxt:
98 aod = aod.replace("@", "")
99 with open(aod) as f:
100 for line in f:
101 if line.endswith(".root"):
102 # print(line)
103 aod = line # get one AO2D.root file from text
104 break
105
106 tFile = (MappingTFile(aod)) # Input as aod
107 for i in tFile:
108 if isinstance(i, dict):
109 for key in i.keys():
110 ttreeList.append(key)
111 return ttreeList
def Map(tf, browsable_to, tpath=None)
Definition: getTTrees.py:23
def MappingTFile(filename)
Definition: getTTrees.py:63
def ExpandDeepTDirs(tf, to_map)
Definition: getTTrees.py:37