ATLAS Offline Software
magnifyPoolFile.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
4 
5 # @file: magnifyPoolFile.py
6 # @purpose: produce a new POOL file with N times the content of an input one.
7 # @author: Sebastien Binet <binet@cern.ch>
8 # @date: May 2007
9 #
10 # @example:
11 #
12 # magnifyPoolFile.py 1000 aod.pool
13 # magnifyPoolFile.py 1000 aod.pool my.magnified.aod.pool
14 #
15 
16 from __future__ import print_function
17 
18 __author__ = "Sebastien Binet <binet@cern.ch>"
19 
20 import sys
21 import os
22 
23 from optparse import OptionParser
24 
25 if __name__ == "__main__":
26 
27  parser = OptionParser(
28  usage = "usage: %prog [-n] nMagnify [-i] input.pool [-o output.pool]"
29  )
30 
31  parser.add_option(
32  "-n",
33  dest = "nMagnify",
34  help = "The number of times the input file will be 'replicated'"
35  )
36  parser.add_option(
37  "-i",
38  "--input",
39  dest = "inPoolFile",
40  help = "Path to the input POOL file to be 'replicated'/'magnified'"
41  )
42  parser.add_option(
43  "-o",
44  "--output",
45  dest = "outPoolFile",
46  default = None,
47  help = "Path to the output POOL file containing the replicated data"
48  )
49 
50  (options, args) = parser.parse_args()
51 
52  if len(args) > 0 and args[0][0] != "-":
53  options.nMagnify = args[0]
54  pass
55 
56  if len(args) > 1 and args[1][0] != "-":
57  options.inPoolFile = args[1]
58  pass
59 
60  if len(args) > 2 and args[2][0] != "-":
61  options.outPoolFile = args[2]
62  pass
63 
64  if not options.nMagnify or \
65  not options.inPoolFile :
66  str(parser.print_help() or "ERROR")
67  sys.exit(1)
68  pass
69 
70  nMagnify = int(options.nMagnify)
71  if nMagnify <= 1:
72  print ("ERROR: you have to give an integer > 1 for the magnifier !!")
73  str(parser.print_help() or "ERROR")
74  sys.exit(1)
75  pass
76 
77  inPoolFile = os.path.expandvars(os.path.expanduser(options.inPoolFile))
78 
79  if not options.outPoolFile:
80  options.outPoolFile = os.path.join( [
81  os.path.dirname(inPoolFile),
82  "magnified."+os.path.basename(inPoolFile)
83  ] )
84 
85  outPoolFile = os.path.expandvars(os.path.expanduser(options.outPoolFile))
86 
87 
88  print ("#"*80)
89  print ("## Magnifying POOL files...")
90  print ("## - replicator parameter:",options.nMagnify)
91  print ("## - input: ",inPoolFile)
92  print ("## - output:",outPoolFile)
93  print ("##")
94 
95  oldArgs = sys.argv
96  sys.argv = sys.argv[:1] + ['-b'] + sys.argv[1:]
97  print ("## importing ROOT...")
98  import ROOT
99  print ("## importing ROOT... [DONE]")
100  import RootUtils.PyROOTFixes # noqa: F401
101 
102  sys.argv = oldArgs
103 
104  print ("## opening input Pool file...")
105  inPoolFile = ROOT.TFile.Open( inPoolFile, "READ" )
106  assert( inPoolFile.IsOpen() )
107  print ("## opening input Pool file... [DONE]")
108 
109  trees = [ k.ReadObj() for k in inPoolFile.GetListOfKeys() ]
110 
111  print ("## creating output Pool file...")
112  outPoolFile = ROOT.TFile.Open( outPoolFile, "RECREATE" )
113  assert( outPoolFile.IsOpen() )
114  print ("## creating output Pool file... [DONE]")
115 
116  print ("## initialize input file trees' branch status...")
117  for tree in trees: tree.SetBranchStatus("*", 0)
118 
119  print ("## create output trees...")
120  outTrees = [ ]
121  for tree in trees:
122  tree.SetBranchStatus("*", 1)
123  outTrees.append( tree.CloneTree(0) )
124 
125  print ("## magnifying...")
126  for m in range( nMagnify ):
127  if nMagnify<10 or m % (nMagnify/10) == 0:
128  print (" ... %s" % str(m).zfill(8))
129  for i in range(len(trees)):
130  for j in range(trees[i].GetEntries()):
131  trees[i].GetEntry(j)
132  outTrees[i].Fill()
133  print ("## magnifying... [DONE]")
134 
135  print ("## committing output file...")
136  outPoolFile.Write()
137  print ("## committing output file... [DONE]")
138 
139  inPoolFile.Close()
140  del inPoolFile
141 
142  outPoolFile.Close()
143  del outPoolFile
144 
145  print ("## Bye.")
146  sys.exit(0)
147 
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:195
generate::GetEntries
double GetEntries(TH1D *h, int ilow, int ihi)
Definition: rmsFrac.cxx:20
str
Definition: BTagTrackIpAccessor.cxx:11