CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
heppy_hadd.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # Copyright (C) 2014 Colin Bernet
3 # https://github.com/cbernet/heppy/blob/master/LICENSE
4 
5 import os
6 import pprint
7 import pickle
8 import shutil
9 
10 def haddPck(file, odir, idirs):
11  '''add pck files in directories idirs to a directory outdir.
12  All dirs in idirs must have the same subdirectory structure.
13  Each pickle file will be opened, and the corresponding objects added to a destination pickle in odir.
14  '''
15  sum = None
16  for dir in idirs:
17  fileName = file.replace( idirs[0], dir )
18  pckfile = open(fileName)
19  obj = pickle.load(pckfile)
20  if sum is None:
21  sum = obj
22  else:
23  try:
24  sum += obj
25  except TypeError:
26  # += not implemented, nevermind
27  pass
28 
29  oFileName = file.replace( idirs[0], odir )
30  pckfile = open(oFileName, 'w')
31  pickle.dump(sum, pckfile)
32  txtFileName = oFileName.replace('.pck','.txt')
33  txtFile = open(txtFileName, 'w')
34  txtFile.write( str(sum) )
35  txtFile.write( '\n' )
36  txtFile.close()
37 
38 
39 def hadd(file, odir, idirs):
40  if file.endswith('.pck'):
41  try:
42  haddPck( file, odir, idirs)
43  except ImportError:
44  pass
45  return
46  elif not file.endswith('.root'):
47  return
48  haddCmd = ['hadd']
49  haddCmd.append( file.replace( idirs[0], odir ) )
50  for dir in idirs:
51  haddCmd.append( file.replace( idirs[0], dir ) )
52  # import pdb; pdb.set_trace()
53  cmd = ' '.join(haddCmd)
54  print cmd
55  os.system(cmd)
56 
57 
58 def haddRec(odir, idirs):
59  print 'adding', idirs
60  print 'to', odir
61 
62  cmd = ' '.join( ['mkdir', odir])
63  # import pdb; pdb.set_trace()
64  # os.system( cmd )
65  try:
66  os.mkdir( odir )
67  except OSError:
68  print
69  print 'ERROR: directory in the way. Maybe you ran hadd already in this directory? Remove it and try again'
70  print
71  raise
72  for root,dirs,files in os.walk( idirs[0] ):
73  # print root, dirs, files
74  for dir in dirs:
75  dir = '/'.join([root, dir])
76  dir = dir.replace(idirs[0], odir)
77  cmd = 'mkdir ' + dir
78  # print cmd
79  # os.system(cmd)
80  os.mkdir(dir)
81  for file in files:
82  hadd('/'.join([root, file]), odir, idirs)
83 
84 def haddChunks(idir, removeDestDir, cleanUp=False, odir_cmd='./'):
85  chunks = {}
86  for file in sorted(os.listdir(idir)):
87  filepath = '/'.join( [idir, file] )
88  # print filepath
89  if os.path.isdir(filepath):
90  compdir = file
91  try:
92  prefix,num = compdir.split('_Chunk')
93  except ValueError:
94  # ok, not a chunk
95  continue
96  # print prefix, num
97  chunks.setdefault( prefix, list() ).append(filepath)
98  if len(chunks)==0:
99  print 'warning: no chunk found.'
100  return
101  for comp, cchunks in chunks.iteritems():
102  odir = odir_cmd+'/'+'/'.join( [idir, comp] )
103  print odir, cchunks
104  if removeDestDir:
105  if os.path.isdir( odir ):
106  shutil.rmtree(odir)
107  haddRec(odir, cchunks)
108  if cleanUp:
109  chunkDir = 'Chunks'
110  if os.path.isdir('Chunks'):
111  shutil.rmtree(chunkDir)
112  os.mkdir(chunkDir)
113  print chunks
114  for comp, chunks in chunks.iteritems():
115  for chunk in chunks:
116  shutil.move(chunk, chunkDir)
117 
118 
119 if __name__ == '__main__':
120 
121  import os
122  import sys
123  from optparse import OptionParser
124 
125  parser = OptionParser()
126  parser.usage = """
127  %prog <dir>
128  Find chunks in dir, and run recursive hadd to group all chunks.
129  For example:
130  DYJets_Chunk0/, DYJets_Chunk1/ ... -> hadd -> DYJets/
131  WJets_Chunk0/, WJets_Chunk1/ ... -> hadd -> WJets/
132  """
133  parser.add_option("-r","--remove", dest="remove",
134  default=False,action="store_true",
135  help="remove existing destination directories.")
136  parser.add_option("-c","--clean", dest="clean",
137  default=False,action="store_true",
138  help="move chunks to Chunks/ after processing.")
139 
140  (options,args) = parser.parse_args()
141 
142  if len(args)>2:
143  print 'provide at most 2 directory as arguments: first the source, then the destination (optional)'
144  sys.exit(1)
145 
146  dir = args[0]
147  if(len(args)>1):
148  odir = args[1]
149  else:
150  odir='./'
151 
152  haddChunks(dir, options.remove, options.clean, odir)
153 
def haddChunks
Definition: heppy_hadd.py:84
def haddRec
Definition: heppy_hadd.py:58
def haddPck
Definition: heppy_hadd.py:10
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
if(conf.exists("allCellsPositionCalc"))
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger list("!*","!HLTx*"if it matches 2 triggers or more) will accept the event if all the matching triggers are FAIL.It will reject the event if any of the triggers are PASS or EXCEPTION(this matches the behavior of"!*"before the partial wildcard feature was incorporated).Triggers which are in the READY state are completely ignored.(READY should never be returned since the trigger paths have been run