5 from __future__
import print_function
11 MAX_ARG_STRLEN = 131072
14 '''add pck files in directories idirs to a directory outdir.
15 All dirs in idirs must have the same subdirectory structure.
16 Each pickle file will be opened, and the corresponding objects added to a destination pickle in odir.
20 fileName = file.replace( idirs[0], dir )
21 pckfile = open(fileName)
22 obj = pickle.load(pckfile)
32 oFileName = file.replace( idirs[0], odir )
33 pckfile = open(oFileName,
'w')
34 pickle.dump(sum, pckfile)
35 txtFileName = oFileName.replace(
'.pck',
'.txt')
36 txtFile = open(txtFileName,
'w')
37 txtFile.write(
str(sum) )
42 def hadd(file, odir, idirs, appx=''):
43 if file.endswith(
'.pck'):
49 elif not file.endswith(
'.root'):
52 haddCmd.append( file.replace( idirs[0], odir ).
replace(
'.root', appx+
'.root') )
54 haddCmd.append( file.replace( idirs[0], dir ) )
56 cmd =
' '.
join(haddCmd)
58 if len(cmd) > MAX_ARG_STRLEN:
59 print(
'Command longer than maximum unix string length; dividing into 2')
60 hadd(file, odir, idirs[:len(idirs)/2],
'1')
61 hadd(file.replace(idirs[0], idirs[len(idirs)/2]), odir, idirs[len(idirs)/2:],
'2')
63 haddCmd.append( file.replace( idirs[0], odir ).
replace(
'.root', appx+
'.root') )
64 haddCmd.append( file.replace( idirs[0], odir ).
replace(
'.root',
'1.root') )
65 haddCmd.append( file.replace( idirs[0], odir ).
replace(
'.root',
'2.root') )
66 cmd =
' '.
join(haddCmd)
67 print(
'Running merge cmd:', cmd)
74 print(
'adding', idirs)
77 cmd =
' '.
join( [
'mkdir', odir])
84 print(
'ERROR: directory in the way. Maybe you ran hadd already in this directory? Remove it and try again')
87 for root,dirs,files
in os.walk( idirs[0] ):
90 dir =
'/'.
join([root, dir])
91 dir = dir.replace(idirs[0], odir)
97 hadd(
'/'.
join([root, file]), odir, idirs)
99 def haddChunks(idir, removeDestDir, cleanUp=False, odir_cmd='./'):
101 for file
in sorted(os.listdir(idir)):
102 filepath =
'/'.
join( [idir, file] )
104 if os.path.isdir(filepath):
107 prefix,num = compdir.split(
'_Chunk')
112 chunks.setdefault( prefix, list() ).
append(filepath)
114 print(
'warning: no chunk found.')
116 for comp, cchunks
in chunks.items():
117 odir = odir_cmd+
'/'+
'/'.
join( [idir, comp] )
120 if os.path.isdir( odir ):
125 if os.path.isdir(
'Chunks'):
126 shutil.rmtree(chunkDir)
129 for comp, chunks
in chunks.items():
131 shutil.move(chunk, chunkDir)
134 if __name__ ==
'__main__':
138 from optparse
import OptionParser
140 parser = OptionParser()
143 Find chunks in dir, and run recursive hadd to group all chunks.
145 DYJets_Chunk0/, DYJets_Chunk1/ ... -> hadd -> DYJets/
146 WJets_Chunk0/, WJets_Chunk1/ ... -> hadd -> WJets/
148 parser.add_option(
"-r",
"--remove", dest=
"remove",
149 default=
False,action=
"store_true",
150 help=
"remove existing destination directories.")
151 parser.add_option(
"-c",
"--clean", dest=
"clean",
152 default=
False,action=
"store_true",
153 help=
"move chunks to Chunks/ after processing.")
155 (options,args) = parser.parse_args()
158 print(
'provide at most 2 directory as arguments: first the source, then the destination (optional)')
167 haddChunks(dir, options.remove, options.clean, odir)