subruns.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 ######################################################################
3 #
4 # Name: subruns.py
5 #
6 # Purpose: Extract (run, subrun) pairs from an artroot file or certain
7 # root tuple files. Output one (run, subrun) pair per line
8 # of output.
9 #
10 # Created: 13-Oct-2015 H. Greenlee
11 #
12 # Command line usage:
13 #
14 # subruns.py <artroot file>
15 #
16 ######################################################################
17 
18 from __future__ import absolute_import
19 from __future__ import print_function
20 
21 # Import stuff.
22 
23 import sys, os, project_utilities, larbatch_posix
24 
25 # Import ROOT (hide command line arguments).
26 
27 myargv = sys.argv
28 sys.argv = myargv[0:1]
29 sys.argv.append('-n')
30 # Prevent root from printing garbage on initialization.
31 if 'TERM' in os.environ:
32  del os.environ['TERM']
33 import ROOT
34 ROOT.gErrorIgnoreLevel = ROOT.kError
35 sys.argv = myargv
36 
37 # Filter warnings.
38 
39 import warnings
40 warnings.filterwarnings('ignore', category = RuntimeWarning, message = 'creating converter.*')
41 
42 # This function opens an artroot file and extracts the list of runs and subruns
43 # from the SubRuns TTree.
44 # A list of (run, subrun) pairs is returned as a list of 2-tuples.
45 
46 def get_subruns(inputfile):
47 
48  # Initialize return value to empty list.
49 
50  result = []
51 
52  # Check whether this file exists.
53  if not os.path.exists(inputfile) or not inputfile.endswith('.root'):
54  return result
55 
56  # Root checks.
57 
58  file = ROOT.TFile.Open(larbatch_posix.root_stream(inputfile))
59  if file and file.IsOpen() and not file.IsZombie():
60 
61  # Root file opened successfully.
62  # Get runs and subruns fro SubRuns tree.
63 
64  subrun_tree = file.Get('SubRuns')
65  if subrun_tree and subrun_tree.InheritsFrom('TTree'):
66  nsubruns = subrun_tree.GetEntriesFast()
67  tfr = ROOT.TTreeFormula('runs',
68  'SubRunAuxiliary.id_.run_.run_',
69  subrun_tree)
70  tfs = ROOT.TTreeFormula('subruns',
71  'SubRunAuxiliary.id_.subRun_',
72  subrun_tree)
73  for entry in range(nsubruns):
74  subrun_tree.GetEntry(entry)
75  run = tfr.EvalInstance64()
76  subrun = tfs.EvalInstance64()
77  run_subrun = (run, subrun)
78  result.append(run_subrun)
79 
80  # If previous section didn't find anything, try extracting
81  # from beam data trees.
82 
83  if len(result) == 0:
84  tdir = file.Get('beamdata')
85  if tdir and tdir.InheritsFrom('TDirectory'):
86 
87  # Look for bnb tree.
88 
89  bnb_tree = tdir.Get('bnb')
90  if bnb_tree and bnb_tree.InheritsFrom('TTree'):
91  nsubruns = bnb_tree.GetEntriesFast()
92  tfr = ROOT.TTreeFormula('runs', 'run', bnb_tree)
93  tfs = ROOT.TTreeFormula('subruns', 'subrun', bnb_tree)
94  for entry in range(nsubruns):
95  bnb_tree.GetEntry(entry)
96  run = tfr.EvalInstance64()
97  subrun = tfs.EvalInstance64()
98  run_subrun = (run, subrun)
99  if run_subrun not in result:
100  result.append(run_subrun)
101 
102  # Look for numi tree.
103 
104  numi_tree = tdir.Get('numi')
105  if numi_tree and numi_tree.InheritsFrom('TTree'):
106  nsubruns = numi_tree.GetEntriesFast()
107  tfr = ROOT.TTreeFormula('runs', 'run', numi_tree)
108  tfs = ROOT.TTreeFormula('subruns', 'subrun', numi_tree)
109  for entry in range(nsubruns):
110  numi_tree.GetEntry(entry)
111  run = tfr.EvalInstance64()
112  subrun = tfs.EvalInstance64()
113  run_subrun = (run, subrun)
114  if run_subrun not in result:
115  result.append(run_subrun)
116 
117  else:
118 
119  # Root file could not be opened.
120 
121  result = []
122 
123  # Sort in order of increasing run, then increasing subrun.
124 
125  result.sort()
126 
127  # Done.
128 
129  return result
130 
131 if __name__ == "__main__":
132  run_subruns = get_subruns(str(sys.argv[1]))
133  for run_subrun in run_subruns:
134  print(run_subrun[0], run_subrun[1])
135  sys.exit(0)
def get_subruns(inputfile)
Definition: subruns.py:46
static QCString str