CMS 3D CMS Logo

Functions | Variables
heppy_hadd Namespace Reference

Functions

def hadd (file, odir, idirs, appx='')
 
def haddChunks (idir, removeDestDir, cleanUp=False, odir_cmd='./')
 
def haddPck (file, odir, idirs)
 
def haddRec (odir, idirs)
 

Variables

 action
 
 args
 
 default
 
 dest
 
 dir
 
 False
 
 help
 
 MAX_ARG_STRLEN
 
 odir
 
 options
 
 parser
 
 usage
 

Function Documentation

◆ hadd()

def heppy_hadd.hadd (   file,
  odir,
  idirs,
  appx = '' 
)

Definition at line 43 of file heppy_hadd.py.

43 def hadd(file, odir, idirs, appx=''):
44  if file.endswith('.pck'):
45  try:
46  haddPck( file, odir, idirs)
47  except ImportError:
48  pass
49  return
50  elif not file.endswith('.root'):
51  return
52  haddCmd = ['hadd']
53  haddCmd.append( file.replace( idirs[0], odir ).replace('.root', appx+'.root') )
54  for dir in idirs:
55  haddCmd.append( file.replace( idirs[0], dir ) )
56  # import pdb; pdb.set_trace()
57  cmd = ' '.join(haddCmd)
58  print(cmd)
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')
63  haddCmd = ['hadd']
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)
69  os.system(cmd)
70  else:
71  os.system(cmd)
72 
73 

References haddPck(), join(), print(), and python.rootplot.root2matplotlib.replace().

Referenced by haddRec().

◆ haddChunks()

def heppy_hadd.haddChunks (   idir,
  removeDestDir,
  cleanUp = False,
  odir_cmd = './' 
)

Definition at line 100 of file heppy_hadd.py.

100 def haddChunks(idir, removeDestDir, cleanUp=False, odir_cmd='./'):
101  chunks = {}
102  for file in sorted(os.listdir(idir)):
103  filepath = '/'.join( [idir, file] )
104  # print filepath
105  if os.path.isdir(filepath):
106  compdir = file
107  try:
108  prefix,num = compdir.split('_Chunk')
109  except ValueError:
110  # ok, not a chunk
111  continue
112  # print prefix, num
113  chunks.setdefault( prefix, list() ).append(filepath)
114  if len(chunks)==0:
115  print('warning: no chunk found.')
116  return
117  for comp, cchunks in six.iteritems(chunks):
118  odir = odir_cmd+'/'+'/'.join( [idir, comp] )
119  print(odir, cchunks)
120  if removeDestDir:
121  if os.path.isdir( odir ):
122  shutil.rmtree(odir)
123  haddRec(odir, cchunks)
124  if cleanUp:
125  chunkDir = 'Chunks'
126  if os.path.isdir('Chunks'):
127  shutil.rmtree(chunkDir)
128  os.mkdir(chunkDir)
129  print(chunks)
130  for comp, chunks in six.iteritems(chunks):
131  for chunk in chunks:
132  shutil.move(chunk, chunkDir)
133 
134 

References mps_setup.append, haddRec(), join(), and print().

◆ haddPck()

def heppy_hadd.haddPck (   file,
  odir,
  idirs 
)
add pck files in directories idirs to a directory outdir.
All dirs in idirs must have the same subdirectory structure.
Each pickle file will be opened, and the corresponding objects added to a destination pickle in odir.

Definition at line 14 of file heppy_hadd.py.

14 def haddPck(file, odir, idirs):
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.
18  '''
19  sum = None
20  for dir in idirs:
21  fileName = file.replace( idirs[0], dir )
22  pckfile = open(fileName)
23  obj = pickle.load(pckfile)
24  if sum is None:
25  sum = obj
26  else:
27  try:
28  sum += obj
29  except TypeError:
30  # += not implemented, nevermind
31  pass
32 
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) )
39  txtFile.write( '\n' )
40  txtFile.close()
41 
42 

References str.

Referenced by hadd().

◆ haddRec()

def heppy_hadd.haddRec (   odir,
  idirs 
)

Definition at line 74 of file heppy_hadd.py.

74 def haddRec(odir, idirs):
75  print('adding', idirs)
76  print('to', odir)
77 
78  cmd = ' '.join( ['mkdir', odir])
79  # import pdb; pdb.set_trace()
80  # os.system( cmd )
81  try:
82  os.mkdir( odir )
83  except OSError:
84  print()
85  print('ERROR: directory in the way. Maybe you ran hadd already in this directory? Remove it and try again')
86  print()
87  raise
88  for root,dirs,files in os.walk( idirs[0] ):
89  # print root, dirs, files
90  for dir in dirs:
91  dir = '/'.join([root, dir])
92  dir = dir.replace(idirs[0], odir)
93  cmd = 'mkdir ' + dir
94  # print cmd
95  # os.system(cmd)
96  os.mkdir(dir)
97  for file in files:
98  hadd('/'.join([root, file]), odir, idirs)
99 

References hadd(), join(), and print().

Referenced by haddChunks().

Variable Documentation

◆ action

heppy_hadd.action

Definition at line 150 of file heppy_hadd.py.

◆ args

heppy_hadd.args

Definition at line 156 of file heppy_hadd.py.

◆ default

heppy_hadd.default

Definition at line 150 of file heppy_hadd.py.

◆ dest

heppy_hadd.dest

Definition at line 149 of file heppy_hadd.py.

◆ dir

heppy_hadd.dir

Definition at line 162 of file heppy_hadd.py.

◆ False

heppy_hadd.False

Definition at line 150 of file heppy_hadd.py.

◆ help

heppy_hadd.help

Definition at line 151 of file heppy_hadd.py.

◆ MAX_ARG_STRLEN

heppy_hadd.MAX_ARG_STRLEN

Definition at line 12 of file heppy_hadd.py.

◆ odir

heppy_hadd.odir

Definition at line 164 of file heppy_hadd.py.

Referenced by ClusterShape.determineShape(), and ClusterShape.processColumn().

◆ options

heppy_hadd.options

Definition at line 156 of file heppy_hadd.py.

◆ parser

heppy_hadd.parser

Definition at line 141 of file heppy_hadd.py.

◆ usage

heppy_hadd.usage

Definition at line 142 of file heppy_hadd.py.

join
static std::string join(char **cmd)
Definition: RemoteFile.cc:17
heppy_hadd.haddChunks
def haddChunks(idir, removeDestDir, cleanUp=False, odir_cmd='./')
Definition: heppy_hadd.py:100
str
#define str(s)
Definition: TestProcessor.cc:52
print
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:46
mps_setup.append
append
Definition: mps_setup.py:85
hadd
mathSSE::Vec4< double > hadd(mathSSE::Vec4< double > a, mathSSE::Vec4< double > b)
Definition: AVXVec.h:108
heppy_hadd.haddPck
def haddPck(file, odir, idirs)
Definition: heppy_hadd.py:14
heppy_hadd.haddRec
def haddRec(odir, idirs)
Definition: heppy_hadd.py:74
python.rootplot.root2matplotlib.replace
def replace(string, replacements)
Definition: root2matplotlib.py:444