rootpy/rootpy — pull request #286 by schmitts (2013-05-23 09:21:58+00:00)
Title: Proposal for a fast object search with regular expression

I would like to propose a fast object search with regular expression. It is optimized for speed by performing the regular expression search only over the "minimal" number of paths/objects. For this, a lookup or cache is populated. I use this technique in my analysis and it scales at least up to ~100k histograms per file.

Please find an example and the expected output below.

```python
import itertools

from rootpy.io import TemporaryFile
from rootpy.plotting import Hist

with TemporaryFile() as f:

h = Hist(10, 0, 1, name="inroot")
h.Write()

f.mkdir("a/b/c")

f.a.cd()
h = Hist(10, 0, 1, name="ina")
h.Write()

h = Hist(10, 0, 1, name="ina2")
h.Write()

f.a.b.cd()
h = Hist(10, 0, 1, name="inab")
h.Write()

f.a.b.c.cd()
h = Hist(10, 0, 1, name="inabc")
h.Write()

res = ["^/(?P[^/]*)$",
"/a/(?Pina.*)",
]

for r, negre in itertools.product(res, [False, True]):

print "testing regular expression", r, "with negate_regexp="+str(negre),":"

for path, result in f.ReSearch(r, class_pattern="TH*", negate_regexp=negre):
print path, result.groupdict() if not negre else "no groupdict, negate_regexp=True"

print
```

```
testing regular expression ^/(?P[^/]*)$ with negate_regexp=False :
/inroot {'h': 'inroot'}

testing regular expression ^/(?P[^/]*)$ with negate_regexp=True :
/a/ina2 no groupdict, negate_regexp=True
/a/ina no groupdict, negate_regexp=True
/a/b/inab no groupdict, negate_regexp=True
/a/b/c/inabc no groupdict, negate_regexp=True

testing regular expression /a/(?Pina.*) with negate_regexp=False :
/a/ina2 {'h': 'ina2'}
/a/ina {'h': 'ina'}

testing regular expression /a/(?Pina.*) with negate_regexp=True :
/a/b/inab no groupdict, negate_regexp=True
/a/b/c/inabc no groupdict, negate_regexp=True
```