3 from __future__
import print_function
7 from subprocess
import Popen, PIPE
10 import project_utilities, root_metadata
26 Purpose: To extract metadata from output file on worker node, generate JSON file 31 """Base class to hold / interpret general metadata""" 32 __metaclass__ = abc.ABCMeta
39 """Extract metadata from inputfile into a pipe for further processing.""" 40 local = project_utilities.path_to_local(self.
inputfile)
42 proc = Popen([
"sam_metadata_dumper", local], stdout=PIPE,
45 url = project_utilities.path_to_url(inputfile)
46 proc = Popen([
"sam_metadata_dumper", url], stdout=PIPE,
48 if len(local) > 0
and local != self.
inputfile:
53 """Run the proc in a 60-sec timeout queue, return stdout, stderr""" 57 thread.join(timeout=60)
59 print(
'Terminating subprocess because of timeout.')
66 raise RuntimeError(
'sam_metadata_dumper returned nonzero exit status {}.'.
format(rc))
71 """Run jobinfo, put the return code, stdout, and stderr into a queue""" 72 jobout, joberr = jobinfo.communicate()
74 for item
in (rc, jobout, joberr):
80 """Take Jobout and Joberr (in jobtuple) and return mdart object from that""" 81 mdtext =
''.join(line.replace(
", ,",
",")
for line
in jobtuple[0].
split(
'\n')
if line[-3:-1] !=
' ,')
82 mdtop = json.JSONDecoder().
decode(mdtext)
83 if len(mdtop.keys()) == 0:
84 print(
'No top-level key in extracted metadata.')
86 file_name = mdtop.keys()[0]
87 return mdtop[file_name]
91 """If there's no application key in md dict, create the key with a blank dictionary. 92 Then return md['application'], along with mdval""" 93 if 'application' not in md:
94 md[
'application'] = {}
95 return md[
'application']
99 """Class to hold/interpret experiment-specific metadata""" 101 MetaData.__init__(self, inputfile)
106 from experiment_utilities
import MetaDataKey
108 print(
"You have not defined an experiment-specific metadata and key-translating module in experiment_utilities. Exiting")
111 metaDataModule = MetaDataKey()
112 self.metadataList, self.
translateKeyf = metaDataModule.metadataList(), metaDataModule.translateKey
115 """Returns the output of the imported translateKey function (as translateKeyf) called on key""" 119 """Loop through art metdata, generate metadata dictionary""" 126 for mdkey
in mdart.keys():
131 if mdkey ==
'file_format_version':
133 elif mdkey ==
'file_format_era':
139 elif mdkey ==
'run_type':
144 elif mdkey ==
'data_stream':
149 elif mdkey ==
'process_name':
154 elif mdkey ==
'applicationFamily':
155 if not md.has_key(
'application'):
156 md[
'application'] = {}
157 md[
'application'][
'family'] = mdval
158 elif mdkey ==
'StageName':
159 if not md.has_key(
'application'):
160 md[
'application'] = {}
161 md[
'application'][
'name'] = mdval
162 elif mdkey ==
'applicationVersion':
163 if not md.has_key(
'application'):
164 md[
'application'] = {}
165 md[
'application'][
'version'] = mdval
169 elif mdkey ==
'parents':
172 parent_dict = {
'file_name': parent}
173 mdparents.append(parent_dict)
174 md[
'parents'] = mdparents
178 elif mdkey ==
'first_event':
180 elif mdkey ==
'last_event':
182 elif mdkey ==
'lbneMCGenerators':
183 md[
'lbne_MC.generators'] = mdval
184 elif mdkey ==
'lbneMCOscillationP':
185 md[
'lbne_MC.oscillationP'] = mdval
186 elif mdkey ==
'lbneMCTriggerListVersion':
187 md[
'lbne_MC.trigger-list-version'] = mdval
188 elif mdkey ==
'lbneMCBeamEnergy':
189 md[
'lbne_MC.beam_energy'] = mdval
190 elif mdkey ==
'lbneMCBeamFluxID':
191 md[
'lbne_MC.beam_flux_ID'] = mdval
192 elif mdkey ==
'lbneMCName':
193 md[
'lbne_MC.name'] = mdval
194 elif mdkey ==
'lbneMCDetectorType':
195 md[
'lbne_MC.detector_type'] = mdval
196 elif mdkey ==
'lbneMCNeutrinoFlavors':
197 md[
'lbne_MC.neutrino_flavors'] = mdval
198 elif mdkey ==
'lbneMCMassHierarchy':
199 md[
'lbne_MC.mass_hierarchy'] = mdval
200 elif mdkey ==
'lbneMCMiscellaneous':
201 md[
'lbne_MC.miscellaneous'] = mdval
202 elif mdkey ==
'lbneMCGeometryVersion':
203 md[
'lbne_MC.geometry_version'] = mdval
204 elif mdkey ==
'lbneMCOverlay':
205 md[
'lbne_MC.overlay'] = mdval
206 elif mdkey ==
'lbneDataRunMode':
207 md[
'lbne_data.run_mode'] = mdval
208 elif mdkey ==
'lbneDataDetectorType':
209 md[
'lbne_data.detector_type'] = mdval
210 elif mdkey ==
'lbneDataName':
211 md[
'lbne_data.name'] = mdval
217 md[mdkey] = mdart[mdkey]
221 md[
'file_name'] = self.inputfile.split(
"/")[-1]
222 if 'file_size' in md0:
223 md[
'file_size'] = md0[
'file_size']
225 md[
'file_size'] = os.path.getsize(self.
inputfile)
227 md[
'crc'] = md0[
'crc']
229 md[
'crc'] = root_metadata.fileEnstoreChecksum(self.
inputfile)
236 """ Get metadata from input file and return as python dictionary. 237 Calls other methods in class and returns metadata dictionary""" 241 return self.
md_gen(mdart, md0)
245 expSpecificMetadata =
expMetaData(os.environ[
'SAM_EXPERIMENT'],
str(sys.argv[1]))
247 print(
'You have not implemented a defineMetaData function by providing an experiment.')
248 print(
'No metadata keys will be saved')
250 mdtext = json.dumps(expSpecificMetadata.getmetadata(), indent=2, sort_keys=
True)
256 if __name__ ==
"__main__":
static bool format(QChar::Decomposition tag, QString &str, int index, int len)
void decode(std::any const &a, Hep2Vector &result)
void split(std::string const &s, char c, OutIter dest)