5 from __future__
import print_function
12 MAX_ARG_STRLEN = 131072
15 '''add pck files in directories idirs to a directory outdir.
16 All dirs in idirs must have the same subdirectory structure.
17 Each pickle file will be opened, and the corresponding objects added to a destination pickle in odir.
21 fileName = file.replace( idirs[0], dir )
22 pckfile = open(fileName)
23 obj = pickle.load(pckfile)
33 oFileName = file.replace( idirs[0], odir )
34 pckfile = open(oFileName,
'w')
35 pickle.dump(sum, pckfile)
36 txtFileName = oFileName.replace(
'.pck',
'.txt')
37 txtFile = open(txtFileName,
'w')
38 txtFile.write(
str(sum) )
43 def hadd(file, odir, idirs, appx=''):
44 if file.endswith(
'.pck'):
50 elif not file.endswith(
'.root'):
53 haddCmd.append( file.replace( idirs[0], odir ).
replace(
'.root', appx+
'.root') )
55 haddCmd.append( file.replace( idirs[0], dir ) )
57 cmd =
' '.
join(haddCmd)
59 if len(cmd) > MAX_ARG_STRLEN:
60 print(
'Command longer than maximum unix string length; dividing into 2')
61 hadd(file, odir, idirs[:len(idirs)/2],
'1')
62 hadd(file.replace(idirs[0], idirs[len(idirs)/2]), odir, idirs[len(idirs)/2:],
'2')
64 haddCmd.append( file.replace( idirs[0], odir ).
replace(
'.root', appx+
'.root') )
65 haddCmd.append( file.replace( idirs[0], odir ).
replace(
'.root',
'1.root') )
66 haddCmd.append( file.replace( idirs[0], odir ).
replace(
'.root',
'2.root') )
67 cmd =
' '.
join(haddCmd)
68 print(
'Running merge cmd:', cmd)
75 print(
'adding', idirs)
78 cmd =
' '.
join( [
'mkdir', odir])
85 print(
'ERROR: directory in the way. Maybe you ran hadd already in this directory? Remove it and try again')
88 for root,dirs,files
in os.walk( idirs[0] ):
91 dir =
'/'.
join([root, dir])
92 dir = dir.replace(idirs[0], odir)
98 hadd(
'/'.
join([root, file]), odir, idirs)
100 def haddChunks(idir, removeDestDir, cleanUp=False, odir_cmd='./'):
102 for file
in sorted(os.listdir(idir)):
103 filepath =
'/'.
join( [idir, file] )
105 if os.path.isdir(filepath):
108 prefix,num = compdir.split(
'_Chunk')
113 chunks.setdefault( prefix,
list() ).
append(filepath)
115 print(
'warning: no chunk found.')
117 for comp, cchunks
in six.iteritems(chunks):
118 odir = odir_cmd+
'/'+
'/'.
join( [idir, comp] )
121 if os.path.isdir( odir ):
126 if os.path.isdir(
'Chunks'):
127 shutil.rmtree(chunkDir)
130 for comp, chunks
in six.iteritems(chunks):
132 shutil.move(chunk, chunkDir)
135 if __name__ ==
'__main__':
139 from optparse
import OptionParser
141 parser = OptionParser()
144 Find chunks in dir, and run recursive hadd to group all chunks.
146 DYJets_Chunk0/, DYJets_Chunk1/ ... -> hadd -> DYJets/
147 WJets_Chunk0/, WJets_Chunk1/ ... -> hadd -> WJets/
149 parser.add_option(
"-r",
"--remove", dest=
"remove",
150 default=
False,action=
"store_true",
151 help=
"remove existing destination directories.")
152 parser.add_option(
"-c",
"--clean", dest=
"clean",
153 default=
False,action=
"store_true",
154 help=
"move chunks to Chunks/ after processing.")
156 (options,args) = parser.parse_args()
159 print(
'provide at most 2 directory as arguments: first the source, then the destination (optional)')
168 haddChunks(dir, options.remove, options.clean, odir)