CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Functions | Variables
heppy_hadd Namespace Reference

Functions

def hadd
 
def haddChunks
 
def haddPck
 
def haddRec
 

Variables

string default = "store_true"
 
list dir = args[0]
 
string help = "remove existing destination directories."
 
int MAX_ARG_STRLEN = 131072
 
list odir = args[1]
 
tuple parser = OptionParser()
 

Function Documentation

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

Definition at line 42 of file heppy_hadd.py.

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

Referenced by haddRec().

42 
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 
def haddPck
Definition: heppy_hadd.py:13
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
static std::string join(char **cmd)
Definition: RemoteFile.cc:19
def heppy_hadd.haddChunks (   idir,
  removeDestDir,
  cleanUp = False,
  odir_cmd = './' 
)

Definition at line 99 of file heppy_hadd.py.

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

99 
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 chunks.items():
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 chunks.items():
131  for chunk in chunks:
132  shutil.move(chunk, chunkDir)
133 
def haddChunks
Definition: heppy_hadd.py:99
boost::dynamic_bitset append(const boost::dynamic_bitset<> &bs1, const boost::dynamic_bitset<> &bs2)
this method takes two bitsets bs1 and bs2 and returns result of bs2 appended to the end of bs1 ...
def haddRec
Definition: heppy_hadd.py:73
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
static std::string join(char **cmd)
Definition: RemoteFile.cc:19
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 13 of file heppy_hadd.py.

References str.

Referenced by hadd().

13 
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 
def haddPck
Definition: heppy_hadd.py:13
#define str(s)
def heppy_hadd.haddRec (   odir,
  idirs 
)

Definition at line 73 of file heppy_hadd.py.

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

Referenced by haddChunks().

73 
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)
def haddRec
Definition: heppy_hadd.py:73
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
static std::string join(char **cmd)
Definition: RemoteFile.cc:19

Variable Documentation

string heppy_hadd.default = "store_true"

Definition at line 149 of file heppy_hadd.py.

list heppy_hadd.dir = args[0]

Definition at line 161 of file heppy_hadd.py.

string heppy_hadd.help = "remove existing destination directories."

Definition at line 150 of file heppy_hadd.py.

int heppy_hadd.MAX_ARG_STRLEN = 131072

Definition at line 11 of file heppy_hadd.py.

string heppy_hadd.odir = args[1]

Definition at line 163 of file heppy_hadd.py.

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

tuple heppy_hadd.parser = OptionParser()

Definition at line 140 of file heppy_hadd.py.