3 """Script for building ROOT trees.
5 Thomas Burton, BNL, 5th September 2013, tpb@bnl.gov
12 -- ROOT installed with Python support (active by default for recent versions)
13 -- ROOT.py should be accessible via $PYTHONPATH
14 -- libeicsmear should be accessible vi $LD_LIBRARY_PATH
16 If in doubt, adding the ROOT library directory (e.g. $ROOTSYS/lib)
17 to both LD_LIBRARY_PATH and PYTHONPATH should suffice.
25 ZIP = {
'.gz':
'gzip',
'.bz2':
'bzip2'}
26 UNZIP = {
'.gz':
'gunzip',
'.bz2':
'bunzip2'}
29 """Processes an input file into a ROOT tree file."""
36 supported_extensions = {
'.txt',
'.dat'}
38 def __init__(self, filename, outdir, nevents, rezip):
41 Initialise with the input file and output information.
42 Determines whether the input file is zipped.
45 name, extension = os.path.splitext(filename)
52 name, extension = os.path.splitext(name)
62 """Build the tree for the input the File was initialised with.
64 If the file is zipped, unzip it first and rezip it after making
65 the ROOT tree (unless requested not to rezip).
69 if self.
ext not in File.supported_extensions:
71 fullname =
''.join([self.
name, self.
ext])
74 zipped_name =
''.join([fullname, self.
zipext])
76 unzipped =
not subprocess.call(
77 [UNZIP[self.
zipext],
'-v', zipped_name])
85 print 'Error encountered building tree'
87 print 'Rezipping', fullname
88 subprocess.call([ZIP[self.
zipext],
'-v', fullname])
92 """Function run by each thread.
94 Pops Files from the queue and processes them until there are no files
99 file = File.queue.get()
101 File.queue.task_done()
103 def build_list(filelist, outdir='.', nevents=-1, nthreads = 1, rezip=True):
104 """Build ROOT tree files from all the listed files.
107 filelist -- a list of file names
108 outdir -- the directory to which to write ROOT files [current directory]
109 nevents -- the maximum number of events per file to process [all]
110 nthreads -- the maximum number of threads permitted to run [1]
112 Zipped files (either gzip or bzip2) are unzipped then rezipped
113 after the ROOT file has been made.
118 for i
in range(nthreads):
119 t = threading.Thread(target=processor)
124 File.queue.put(
File(i, outdir, nevents, rezip))
129 """Process command line arguments and return argparse Namespace."""
130 from argparse
import ArgumentParser, ArgumentDefaultsHelpFormatter
131 parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter,
132 description=
'build trees from files in a list or directory')
133 parser.add_argument(
'source', help=
'name of file list or directory')
134 parser.add_argument(
'-o',
'--outdir', default=
'.', help=
'output directory')
135 parser.add_argument(
'-e',
'--events', type=int, default=-1,
136 help=
'number of events per file')
137 parser.add_argument(
'-p',
'--processes', type=int, default=1,
138 help=
'number of parallel processes')
139 parser.add_argument(
'--norezip', action=
'store_true',
140 help=
'do not rezip files')
141 return parser.parse_args()
145 if __name__ ==
"__main__":
146 """Executes buildlist on all the files in a list or directory."""
158 ROOT.PyConfig.IgnoreCommandLineOptions =
True
159 except AttributeError:
162 if ROOT.gSystem.Load(
'libeicsmear') < 0:
163 raise IOError(
'libeicsmear could not be located')
165 except Exception
as error:
166 print 'Error:', error
169 if os.path.isfile(options.source):
170 with
open(options.source)
as file:
171 files = file.read().splitlines()
173 elif os.path.isdir(options.source):
174 files = [os.path.join(options.source, file)
175 for file
in os.listdir(options.source)]
178 print options.source,
'not recognized... quitting'
181 build_list(files, options.outdir, options.events, options.processes,