All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
experiment_utilities.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # ----------------------------------------------------------------------
3 #
4 # Name: project_utilities.py
5 #
6 # Purpose: A python module containing various experiment-specific
7 # python utility functions.
8 #
9 # Created: 28-Oct-2013 H. Greenlee
10 # Modified: 10-Jul-2017 T. Brooks (tbrooks@fnal.gov) - now works with
11 # LArSoft v06
12 #
13 # Modified: 17-Feb-2020 I. de Icaza Astiz (icaza@fnal.gov)
14 # Python3 compliant and Python2 backwards compatible.
15 # ----------------------------------------------------------------------
16 
17 import os
18 from io import StringIO
19 
20 # Don't fail (on import) if samweb is not available.
21 
22 try:
23  import samweb_cli
24 except ImportError:
25  pass
26 
27 
28 def get_dropbox(filename):
29 
30  # Get metadata.
31 
32  md = {}
33  exp = 'sbnd'
34  if 'SAM_EXPERIMENT' in os.environ:
35  exp = os.environ['SAM_EXPERIMENT']
36  samweb = samweb_cli.SAMWebClient(experiment=exp)
37  try:
38  md = samweb.getMetadata(filenameorid=filename)
39  except:
40  pass
41 
42  # Extract the metadata fields that we need.
43 
44  file_type = ''
45  group = ''
46  data_tier = ''
47 
48  if 'file_type' in md:
49  file_type = md['file_type']
50  if 'group' in md:
51  group = md['group']
52  if 'data_tier' in md:
53  data_tier = md['data_tier']
54 
55  if not file_type or not group or not data_tier:
56  raise RuntimeError(
57  'Missing or invalid metadata for file %s.' % filename)
58 
59  # Construct dropbox path.
60 
61  #path = '/sbnd/data/sbndsoft/dropbox/%s/%s/%s' % (file_type, group, data_tier)
62  if 'FTS_DROPBOX' in os.environ:
63  dropbox_root = os.environ['FTS_DROPBOX']
64  else:
65  dropbox_root = '/pnfs/sbnd/scratch/sbndpro/dropbox'
66  path = '%s/%s/%s/%s' % (dropbox_root, file_type, group, data_tier)
67  return path
68 
69 # Return fcl configuration for experiment-specific sam metadata.
70 
71 
72 def get_sam_metadata(project, stage):
73  result = 'services.FileCatalogMetadataSBN: {\n'
74  if type(stage.fclname) == type('') or type(stage.fclname) == type(''):
75  result = result + ' FCLName: "%s"\n' % os.path.basename(stage.fclname)
76  else:
77  result = result + ' FCLName: "'
78  for fcl in stage.fclname:
79  result = result + '%s/' % os.path.basename(fcl)
80  result = result[:-1]
81  result = result + '"\n'
82  result = result + ' FCLVersion: "%s"\n' % project.release_tag
83  result = result + ' ProjectName: "%s"\n' % project.name
84  result = result + ' ProjectStage: "%s"\n' % stage.name
85  result = result + ' ProjectVersion: "%s"\n' % project.release_tag
86  result = result + '}\n'
87 
88  return result
89 
90 # Function to return path to the setup_sbnd.sh script
91 
92 
94 
95  CVMFS_DIR = "/cvmfs/sbnd.opensciencegrid.org/products/sbnd/"
96  FERMIAPP_DIR = "/grid/fermiapp/products/sbnd/"
97 
98  if os.path.isfile(FERMIAPP_DIR+"setup_sbnd.sh"):
99  setup_script = FERMIAPP_DIR+"setup_sbnd.sh"
100  elif os.path.isfile(CVMFS_DIR+"setup_sbnd.sh"):
101  setup_script = CVMFS_DIR+"setup_sbnd.sh"
102  else:
103  raise RuntimeError("Could not find setup script at " +
104  FERMIAPP_DIR+" or "+CVMFS_DIR)
105 
106  return setup_script
107 
108 # Construct dimension string for project, stage.
109 
110 
111 def dimensions(project, stage):
112 
113  data_tier = ''
114  if ana:
115  data_tier = stage.ana_data_tier
116  else:
117  data_tier = stage.data_tier
118  dim = 'file_type %s' % project.file_type
119  dim = dim + ' and data_tier %s' % data_tier
120  dim = dim + ' and ub_project.name %s' % project.name
121  dim = dim + ' and ub_project.stage %s' % stage.name
122  dim = dim + ' and ub_project.version %s' % project.release_tag
123  if stage.pubs_output:
124  first_subrun = True
125  for subrun in stage.output_subruns:
126  if first_subrun:
127  dim = dim + \
128  ' and run_number %d.%d' % (stage.output_run, subrun)
129  first_subrun = False
130 
131  # Kluge to pick up mc files with wrong run number.
132 
133  if stage.output_run > 1 and stage.output_run < 100:
134  dim = dim + ',1.%d' % subrun
135  else:
136  dim = dim + ',%d.%d' % (stage.output_run, subrun)
137  elif project.run_number != 0:
138  dim = dim + ' and run_number %d' % project.run_number
139  dim = dim + ' and availability: anylocation'
140  return dim
141 
142 
144  return 'sbndcode'