25 from __future__
import absolute_import
26 from __future__
import print_function
28 from larbatch_utilities
import ifdh_cp
29 import project_utilities
52 if not project_utilities.safeexist(path):
57 json_path = os.path.join(logdir, os.path.basename(path) +
'.json')
58 if project_utilities.safeexist(json_path):
63 lines = project_utilities.saferead(json_path)
78 if len(list(md.keys())) > 0:
82 nevroot =
int(md[
'events'])
83 if 'data_stream' in md:
84 stream = md[
'data_stream']
85 result = (nevroot, stream)
113 print(
'Checking root files in directory %s.' % outdir)
114 filenames = os.listdir(outdir)
115 for filename
in filenames:
116 name, ext = os.path.splitext(filename)
117 if len(ext) > 0
and ext[1:]
in data_file_types:
118 path = os.path.join(outdir, filename)
124 roots.append((os.path.join(outdir, filename), nevroot, stream))
130 hists.append(os.path.join(outdir, filename))
137 print(
'Warning: File %s in directory %s is not a valid root file.' % (filename, outdir))
141 return (nev, roots, hists)
148 global extractor_dict
152 samweb = project_utilities.samweb()
153 from extractor_dict
import expMetaData
171 maintain_parentage = 0
176 if args[0] ==
'--dir' and len(args) > 1:
179 elif args[0] ==
'--logfiledir' and len(args) > 1:
182 elif args[0] ==
'--outdir' and len(args) > 1:
185 elif args[0] ==
'--declare' and len(args) > 1:
186 declare_file =
int(args[1])
188 elif args[0] ==
'--copy' and len(args) > 1:
189 copy_to_dropbox =
int(args[1])
191 elif args[0] ==
'--maintain_parentage' and len(args) > 1:
192 maintain_parentage =
int(args[1])
194 elif args[0] ==
'--data_file_type' and len(args) > 1:
195 data_file_types.append(args[1])
198 print(
'Unknown option %s' % args[0])
203 if len(data_file_types) == 0:
204 data_file_types.append(
'root')
208 print(
"Do decleration in job: %d" % declare_file)
211 stat_filename = os.path.join(logdir,
'lar.stat')
212 if project_utilities.safeexist(stat_filename):
214 status =
int(project_utilities.saferead(stat_filename)[0].strip())
216 print(
'Job in subdirectory %s ended with non-zero exit status %d.' % (checkdir, status))
220 print(
'Bad file lar.stat in subdirectory %s.' % checkdir)
224 print(
'No directory specified (use the --dir option.) Exiting.')
227 print(
'No log file directory specified (use the --logfiledir option.) Exiting.')
230 nevts,rootfiles,hists =
check_root(checkdir, logdir, data_file_types)
237 if len(hists) > 0
and len(rootfiles) == 0
and nevts <= 0:
241 if len(rootfiles) == 0
or nevts < 0:
242 print(
'Problem with root file(s) in %s.' % checkdir)
246 elif nevts < -1
or len(hists) == 0:
247 print(
'Problem with analysis root file(s) in %s.' % checkdir)
256 validate_list =
open(
'validate.list',
'w')
257 file_list =
open(
'files.list',
'w')
258 ana_file_list =
open(
'filesana.list',
'w')
260 events_list =
open(
'events.list',
'w')
263 bad_list =
open(
'bad.list',
'w')
264 missing_list =
open(
'missing_files.list',
'w')
269 print(
"%d processes completed successfully." % nproc)
270 print(
"%d total good histogram files." % len(hists))
273 print(
"%d total good events." % nevts)
274 print(
"%d total good root files." % len(rootfiles))
275 print(
"%d total good histogram files." % len(hists))
277 file_list_stream = {}
281 for rootfile
in rootfiles:
283 rootpath = rootfile[0]
285 streamname = rootfile[2]
288 rootname = os.path.basename(rootpath)
289 if len(rootname) >= 200:
290 print(
'Filename %s in subdirectory %s is longer than 200 characters.' % (
294 if streamname
not in file_list_stream:
295 file_list_stream[streamname] =
open(
'files_%s.list' % streamname,
'w')
296 validate_list.write(rootpath +
'\n')
297 file_on_scratch = os.path.join(outdir, os.path.basename(rootpath))
298 file_list.write(file_on_scratch +
'\n')
299 file_list_stream[streamname].
write(file_on_scratch +
'\n')
300 events_list.write(
'%s %d \n' % (file_on_scratch, nev) )
304 for histfile
in hists:
305 validate_list.write(histfile +
'\n')
306 file_on_scratch = os.path.join(outdir, os.path.basename(histfile))
307 ana_file_list.write(file_on_scratch +
'\n')
311 validate_list.close()
313 ana_file_list.close()
314 for streamname
in list(file_list_stream.keys()):
315 file_list_stream[streamname].
close()
319 missing_list.write(
'%d \n' %status)
330 for rootfile
in rootfiles:
332 rootpath = rootfile[0]
333 fn = os.path.basename(rootpath)
341 md = samweb.getMetadata(fn)
344 print(
'File %s is already declared.' % fn)
349 print(
'Declaring %s' % fn)
350 expSpecificMetaData = expMetaData(project_utilities.get_experiment(), rootpath)
351 md = expSpecificMetaData.getmetadata()
355 if maintain_parentage == 1:
364 jobs_parents = os.getenv(
'JOBS_PARENTS',
'').
split(
" ")
365 jobs_aunts = os.getenv(
'JOBS_AUNTS',
'').
split(
" ")
366 if(jobs_parents[0] !=
'' ):
367 md[
'parents'] = [{
'file_name': parent}
for parent
in jobs_parents]
368 if(jobs_aunts[0] !=
'' ):
369 for aunt
in jobs_aunts:
370 mixparent_dict = {
'file_name': aunt}
371 if 'parents' not in md:
373 md[
'parents'].append(mixparent_dict)
376 project_utilities.test_kca()
382 samweb.declareFile(md=md)
385 except samweb_cli.exceptions.SAMWebHTTPError
as e:
387 print(
'SAM declare failed.')
391 print(
'SAM declare failed.')
395 print(
'No sam metadata found for %s.' % fn)
399 if copy_to_dropbox == 1
and declare_ok:
400 print(
"Copying to Dropbox")
401 dropbox_dir = project_utilities.get_dropbox(fn)
402 rootPath = os.path.join(dropbox_dir, fn)
403 jsonPath = rootPath +
".json" 408 for histpath
in hists:
411 fn = os.path.basename(histpath)
418 md = samweb.getMetadata(fn)
421 print(
'File %s is already declared.' % fn)
426 print(
'Declaring %s' % fn)
427 json_file = os.path.join(logdir, fn +
'.json')
432 if project_utilities.safeexist(json_file):
433 mdlines = project_utilities.saferead(json_file)
436 mdtext = mdtext + line
438 md = json.loads(mdtext)
443 if maintain_parentage == 1:
452 jobs_parents = os.getenv(
'JOBS_PARENTS',
'').
split(
" ")
453 jobs_aunts = os.getenv(
'JOBS_AUNTS',
'').
split(
" ")
454 if(jobs_parents[0] !=
'' ):
455 md[
'parents'] = [{
'file_name': parent}
for parent
in jobs_parents]
456 if(jobs_aunts[0] !=
'' ):
457 for aunt
in jobs_aunts:
458 mixparent_dict = {
'file_name': aunt}
459 if 'parents' not in md:
461 md[
'parents'].append(mixparent_dict)
463 if len(md) > 0
and 'file_type' in md:
464 project_utilities.test_kca()
470 samweb.declareFile(md=md)
473 except samweb_cli.exceptions.SAMWebHTTPError
as e:
475 print(
'SAM declare failed.')
479 print(
'SAM declare failed.')
483 print(
'No sam metadata found for %s.' % fn)
486 if copy_to_dropbox == 1
and declare_ok:
487 print(
"Copying to Dropbox")
488 dropbox_dir = project_utilities.get_dropbox(fn)
489 rootPath = dropbox_dir +
"/" + fn
490 jsonPath = rootPath +
".json" 498 dir_on_scratch = os.path.basename(outdir)
499 print(
'Dir on scratch ' + dir_on_scratch)
500 bad_list.write(
'%s \n' % dir_on_scratch)
505 if __name__ ==
'__main__' :
def check_root_file(path, logdir)
int open(const char *, int)
Opens a file descriptor.
size_t write(int, const char *, size_t)
Writes count bytes from buf to the filedescriptor fd.
def ifdh_cp(source, destination)
int close(int)
Closes the file descriptor fd.
def check_root(outdir, logdir, data_file_types)
void split(std::string const &s, char c, OutIter dest)