Line Code
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
#!/usr/bin/env python3
import ROOT
import argparse
from collections import defaultdict

parser = argparse.ArgumentParser(description="Output name and number of entries (or value) for MEs in a DQMIO file.")

parser.add_argument('inputfile', help='DQMIO ROOT file name.')
parser.add_argument('-r', '--run', help='Run number of run to process', default=1, type=int)
parser.add_argument('-l', '--lumi', help='Lumisection to process', default=0, type=int)
parser.add_argument('-s', '--summary', help='Only show values and how often they appeared.', action='store_true')
args = parser.parse_args()

treenames = {
    0: "Ints",
    1: "Floats",
    2: "Strings",
    3: "TH1Fs",
    4: "TH1Ss",
    5: "TH1Ds",
    6: "TH2Fs",
    7: "TH2Ss",
    8: "TH2Ds",
    9: "TH2Polys",
    10: "TH3Fs",
    11: "TProfiles",
    12: "TProfile2Ds",
    13: "TH1Is",
    14: "TH2Is"
}

f = ROOT.TFile.Open(args.inputfile)
idxtree = getattr(f, "Indices")

summary = defaultdict(lambda: 0)

for i in range(idxtree.GetEntries()):
    idxtree.GetEntry(i)
    run, lumi, metype = idxtree.Run, idxtree.Lumi, idxtree.Type
    if run != args.run or lumi != args.lumi:
        continue

    # inclusive range -- for 0 entries, row is left out
    firstidx, lastidx = idxtree.FirstIndex, idxtree.LastIndex
    metree = getattr(f, treenames[metype])
    # this GetEntry is only to make sure the TTree is initialized correctly
    metree.GetEntry(0)
    metree.SetBranchStatus("*",0)
    metree.SetBranchStatus("FullName",1)

    for x in range(firstidx, lastidx+1):
        metree.GetEntry(x)
        mename = str(metree.FullName)
        metree.GetEntry(x, 1)
        value = metree.Value
        
        
        if treenames[metype] in ["Ints", "Floats", "Strings"]:
          result = str(value)
        else:
          result = "%d" % value.GetEntries()

        if args.summary:
          summary[result] += 1
        else:
          print("%s: %s" % (mename, result))

if args.summary:
  keys = sorted(summary.keys())
  summaryitems = ["%s: %d" % (k, summary[k]) for k in keys]
  print(", ".join(summaryitems))