Skip to content

Commit 6dacc74

Browse files
committed
implement MUCH faster file filtering in assignFileNames and more verbose output
1 parent d61528f commit 6dacc74

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

mctools/fluka/fluka2root.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# alias fluka2root-dir="parallel 'cd {} && fluka2root *.inp' ::: *"
44

55
import sys, re, os, argparse
6-
import glob
6+
import glob, fnmatch
77
from tempfile import NamedTemporaryFile
88
from shutil import which
99

@@ -84,6 +84,8 @@ def __init__(self, args):
8484
print("output ROOT file:", self.root)
8585

8686
def Clean(self):
87+
if self.verbose:
88+
print("Cleaning tmp files...")
8789
v = "-v" if self.verbose else ""
8890
for f in self.inp:
8991
n = "[0-9][0-9][0-9]"
@@ -114,11 +116,13 @@ def getROOTFileName(self):
114116
return os.path.basename(root)
115117

116118
def checkInputFiles(self):
117-
"""Does some checks of the input files
119+
"""Do some checks of the input files
118120
119121
- check if all input files exist
120122
- check whether input follows the standard Fluka format (free format is not supported)
121123
"""
124+
if self.verbose:
125+
print("Checking input files...")
122126

123127
for f in self.inp:
124128
if not os.path.isfile(f):
@@ -182,6 +186,9 @@ def getOpenedUnits(self):
182186
def assignUnits(self):
183187
"""Assigns units to estimators
184188
"""
189+
if self.verbose:
190+
print("Assigning units to estimators...")
191+
185192
self.opened = self.getOpenedUnits()
186193
if len(self.opened):
187194
sys.exit("Opened units not yet supported")
@@ -220,18 +227,24 @@ def assignUnits(self):
220227
def assignFileNames(self):
221228
"""Assign file names to units
222229
"""
230+
if self.verbose:
231+
print("Assigning file names to units...")
232+
233+
all_files = glob.glob("*[0-9][0-9][0-9]_fort.*")
234+
223235
for e in self.estimators:
224236
for u in e.units:
225237
for inp in self.inp:
226-
for f in glob.glob("%s[0-9][0-9][0-9]_fort.%d" % (os.path.splitext(inp)[0], abs(u))):
227-
if f not in e.units[u]: # TODO: this can be done smarter
238+
pattern = "%s[0-9][0-9][0-9]_fort.%d" % (os.path.splitext(inp)[0], abs(u))
239+
for f in fnmatch.filter(all_files, pattern):
240+
if f not in e.units[u]: # TODO: why do we need this check?
228241
e.addFile(u,f)
229242

230243
def Merge(self):
231244
""" Merge all data with standard FLUKA tools
232245
"""
233246
if self.verbose:
234-
print("Merging...")
247+
print("Merging data files...")
235248

236249
tmpfiles=[]
237250
for e in self.estimators:
@@ -279,7 +292,7 @@ def Convert(self):
279292
"""Convert merged files into ROOT
280293
"""
281294
if self.verbose:
282-
print("Converting...")
295+
print("Converting to ROOT...")
283296

284297
v = "-v" if self.verbose else ""
285298

0 commit comments

Comments
 (0)