makeTrainCVSamples.py
Go to the documentation of this file.
1 import os
2 import argparse
3 import subprocess
4 
5 
6 parser = argparse.ArgumentParser(description='Dump a tree to text.')
7 parser.add_argument('--fluxswapdir', '-f', type=str, required=True,
8  help="""Directory with fluxswap files.""")
9 parser.add_argument('--nonswapdir', '-n', type=str, required=True,
10  help="""Directory with nonswap files.""")
11 
12 parser.add_argument('--ntrain', type=int, required=True,
13  help="""Number of pairs of fluxswap and nonswap
14  files for training sample.""")
15 
16 parser.add_argument('--ncv', type=int, required=True,
17  help="""Number of pairs of fluxswap and nonswap files for CV smaple .""")
18 
19 parser.add_argument('--artext', type=str, default=".cvn.root",
20  help="""Extension of art-ROOT files.""")
21 
22 parser.add_argument('--histext', type=str, default=".cvnevents.root",
23  help="""Extension of hist-ROOT files with CVN events for training.""")
24 
25 
26 parser.add_argument('--haddpath', type=str,
27  help="""Location to place hadd files for training and lists for cv sample""")
28 
29 
30 args = parser.parse_args()
31 
32 
33 
34 
35 
36 class FilePair:
37  def __init__(self, artPath, histPath):
38  self.artPath = artPath
39  self.histPath = histPath
40 
41  def __repr__(self):
42  return "File pair:\n %s\n %s\n" %(self.artPath, self.histPath)
43 
44  def __lt__(self, other):
45  return self.artPath < other.artPath
46 
47 def haddFiles(files, outputName):
48  cmdList = ["hadd", "-f", outputName] + files
49 
50  subprocess.call(cmdList)
51 
52 
53 dirs = {}
54 dirs["fluxswap"] = args.fluxswapdir
55 dirs["nonswap"] = args.nonswapdir
56 
57 
58 filePairs = {}
59 
60 for key, dir in dirs.items():
61  fileList = os.listdir(dir)
62  artFiles = [f for f in fileList if f.endswith(args.artext)]
63  histFiles = [f for f in fileList if f.endswith(args.histext)]
64  filePairs[key] = []
65  for artFile in artFiles:
66  histFileMatches = [f for f in histFiles \
67  if f.replace(args.histext, "") == artFile.replace(args.artext, "")]
68  if len(histFileMatches) > 1:
69  raise Exception("too many hist file matches for this file:" + artFile)
70  elif len(histFileMatches) == 0:
71  print "Warning: failed to find hist match for this art file: ", artFile
72  continue
73  filePairs[key].append(FilePair(os.path.join(dir, artFile),
74  os.path.join(dir, histFileMatches[0])))
75  filePairs[key].sort()
76 
77 
78 
79 
80 if args.haddpath:
81  trainFilePairs = filePairs["fluxswap"][0:args.ntrain] + \
82  filePairs["nonswap" ][0:args.ntrain]
83  haddName = os.path.join(args.haddpath, "train_%s_hadd.root" %(args.ntrain))
84  trainListPath = os.path.join(args.haddpath, "train_%s.txt" %(args.ntrain))
85  trainList = [f.histPath for f in trainFilePairs]
86  trainArtList = [f.artPath for f in trainFilePairs]
87  haddFiles(trainList, haddName)
88  open(trainListPath, 'w').write("\n".join(trainArtList ))
89 
90  cvFilePairs = filePairs["fluxswap"][-args.ncv:] + \
91  filePairs["nonswap" ][-args.ncv:]
92  cvArtList = [f.artPath for f in cvFilePairs]
93  cvListPath = os.path.join(args.haddpath, "cv_%s_train_%s.txt" %(args.ncv,
94  args.ntrain))
95  open(cvListPath, 'w').write("\n".join(cvArtList))
96 
97 
98 
99 
100 
101 
102 
103 
104 
105 
106 
107 
108 
109 
110 
111 
112 
113 
114 
115 
def haddFiles(files, outputName)
void append()
Definition: append.C:24
write
Run ND cosmics.
def __init__(self, artPath, histPath)
procfile open("FD_BRL_v0.txt")
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66