def main(): """Main function""" # get the options optmgr = MyOptionParser() opts = optmgr.get_opt() # build the result from user input result = Result(unicode(opts.path)) result.description = unicode(opts.desc) result.author = unicode(opts.author) result.creation_time = opts.datetime result.elog = unicode(opts.elog) result.analysis_id = opts.ana # connect to the MySQL database using default credentials dbstore = DbStore() # unless the source is set, prompt the user and present a list to make a choice if opts.inputSamples is None: inputSamples = prompt_samples(dbstore) else: inputSamples = parse_samples(opts.inputSamples) # create and store the relations samples = dbstore.find(Sample, Sample.sample_id.is_in(inputSamples)) if samples.is_empty(): dbstore.add(result) else: for sample in samples: sample.results.add(result) # flush (populates the analysis if needed) dbstore.flush() # print the resulting object and ask for confirmation print result if confirm(prompt="Insert into the database?", resp=True): dbstore.commit()
def main(): """Main function""" # get the options optmgr = MyOptionParser() opts = optmgr.get_opt() # build the sample from user input sample = Sample(unicode(opts.name), unicode(opts.path), unicode(opts.sampletype), opts.nevents_processed) sample.nevents = opts.nevents sample.normalization = opts.normalization sample.event_weight_sum = opts.weight_sum sample.luminosity = opts.luminosity sample.code_version = unicode(opts.code_version) sample.user_comment = unicode(opts.user_comment) sample.source_dataset_id = opts.source_dataset_id sample.source_sample_id = opts.source_sample_id sample.author = unicode(opts.author) sample.creation_time = opts.datetime # connect to the MySQL database using default credentials dbstore = DbStore() # unless the source is set, prompt the user and present a list to make a choice if sample.source_dataset_id is None: prompt_dataset(sample,dbstore) if sample.source_sample_id is None: prompt_sample(sample,dbstore) # check that source sample and dataset exist if sample.source_dataset_id is not None: checkExisting = dbstore.find(Dataset,Dataset.dataset_id==sample.source_dataset_id) if checkExisting.is_empty(): raise IndexError("No dataset with such index: %d"%sample.source_dataset_id) if sample.source_sample_id is not None: checkExisting = dbstore.find(Sample,Sample.sample_id==sample.source_sample_id) if checkExisting.is_empty(): raise IndexError("No sample with such index: %d"%sample.source_sample_id) # if opts.nevents is not set, take #events from source sample (if set) or from source dataset (if set) in that order if sample.nevents_processed is None and sample.source_sample_id is not None: sample.nevents_processed = dbstore.find(Sample,Sample.sample_id==sample.source_sample_id).one().nevents_processed if sample.nevents_processed is None and sample.source_dataset_id is not None: sample.nevents_processed = dbstore.find(Dataset,Dataset.dataset_id==sample.source_dataset_id).one().nevents if sample.nevents_processed is None: print "Warning: Number of processed events not given, and no way to guess it." # List input files files = [] if opts.files == "": files = glob.glob(os.path.join(sample.path, '*.root')) else: files = unicode(opts.files).split(",") if len(files) == 0: print "Warning: no root files found in %r" % sample.path # Try to guess the number of events stored into the file, as well as the weight sum for f in files: (weight_sum, entries) = get_file_data_(f) sample.files.add(File(f, f, weight_sum, entries)) # check that there is no existing entry checkExisting = dbstore.find(Sample,Sample.name==sample.name) if checkExisting.is_empty(): print sample if confirm(prompt="Insert into the database?", resp=True): dbstore.add(sample) # compute the luminosity, if possible if sample.luminosity is None: dbstore.flush() sample.luminosity = sample.getLuminosity() else: existing = checkExisting.one() prompt = "Replace existing " prompt += str(existing) prompt += "\nby new " prompt += str(sample) prompt += "\n?" if confirm(prompt, resp=False): existing.replaceBy(sample) if existing.luminosity is None: dbstore.flush() existing.luminosity = existing.getLuminosity() # commit dbstore.commit()
def main(): """Main function""" # get the options optmgr = MyOptionParser() opts = optmgr.get_opt() # build the sample from user input sample = Sample(unicode(opts.name), unicode(opts.path), unicode(opts.sampletype), opts.nevents_processed) sample.nevents = opts.nevents sample.normalization = opts.normalization sample.event_weight_sum = opts.weight_sum sample.luminosity = opts.luminosity sample.code_version = unicode(opts.code_version) sample.user_comment = unicode(opts.user_comment) sample.source_dataset_id = opts.source_dataset_id sample.source_sample_id = opts.source_sample_id sample.author = unicode(opts.author) sample.creation_time = opts.datetime # connect to the MySQL database using default credentials dbstore = DbStore() # unless the source is set, prompt the user and present a list to make a choice if sample.source_dataset_id is None: prompt_dataset(sample, dbstore) if sample.source_sample_id is None: prompt_sample(sample, dbstore) # check that source sample and dataset exist if sample.source_dataset_id is not None: checkExisting = dbstore.find( Dataset, Dataset.dataset_id == sample.source_dataset_id) if checkExisting.is_empty(): raise IndexError("No dataset with such index: %d" % sample.source_dataset_id) if sample.source_sample_id is not None: checkExisting = dbstore.find( Sample, Sample.sample_id == sample.source_sample_id) if checkExisting.is_empty(): raise IndexError("No sample with such index: %d" % sample.source_sample_id) # if opts.nevents is not set, take #events from source sample (if set) or from source dataset (if set) in that order if sample.nevents_processed is None and sample.source_sample_id is not None: sample.nevents_processed = dbstore.find( Sample, Sample.sample_id == sample.source_sample_id).one().nevents_processed if sample.nevents_processed is None and sample.source_dataset_id is not None: sample.nevents_processed = dbstore.find( Dataset, Dataset.dataset_id == sample.source_dataset_id).one().nevents if sample.nevents_processed is None: print "Warning: Number of processed events not given, and no way to guess it." # List input files files = [] if opts.files == "": files = glob.glob(os.path.join(sample.path, '*.root')) else: files = unicode(opts.files).split(",") if len(files) == 0: print "Warning: no root files found in %r" % sample.path # Try to guess the number of events stored into the file, as well as the weight sum for f in files: (weight_sum, entries) = get_file_data_(f) sample.files.add(File(f, f, weight_sum, entries)) # check that there is no existing entry checkExisting = dbstore.find(Sample, Sample.name == sample.name) if checkExisting.is_empty(): print sample if confirm(prompt="Insert into the database?", resp=True): dbstore.add(sample) # compute the luminosity, if possible if sample.luminosity is None: dbstore.flush() sample.luminosity = sample.getLuminosity() else: existing = checkExisting.one() prompt = "Replace existing " prompt += str(existing) prompt += "\nby new " prompt += str(sample) prompt += "\n?" if confirm(prompt, resp=False): existing.replaceBy(sample) if existing.luminosity is None: dbstore.flush() existing.luminosity = existing.getLuminosity() # commit dbstore.commit()