diff --git a/analysis/batch/__init__.py b/analysis/batch/__init__.py index 4fcc269dfc2a9fb23f2153fd08929d4ccff43ff5..a8b9a8dde1a925719c89c7cf8bab8257d001130c 100644 --- a/analysis/batch/__init__.py +++ b/analysis/batch/__init__.py @@ -27,7 +27,7 @@ def get_batch_system(name=None): """ from analysis.batch.batch_system import BATCH_SYSTEMS if name: - batch_system = BATCH_SYSTEMS.get(name, None) + batch_system = BATCH_SYSTEMS.get(name) if batch_system: return batch_system else: diff --git a/analysis/batch/batch_system.py b/analysis/batch/batch_system.py index 7e520d621ec0169c112906231f47bc27cd0f591b..4445e584bd7db7a224823951839a69e02523870f 100644 --- a/analysis/batch/batch_system.py +++ b/analysis/batch/batch_system.py @@ -113,7 +113,7 @@ echo "------------------------------------------------------------------------" if log_file == err_file: header.append(self.DIRECTIVES['mergelogs']) for batch_option, batch_value in batch_config.items(): - directive = self.DIRECTIVES.get(batch_option, None) + directive = self.DIRECTIVES.get(batch_option) if directive is None: logger.warning("Ignoring directive %s -> %s", batch_option, batch_value) continue diff --git a/analysis/data/__init__.py b/analysis/data/__init__.py index a8c2bf299363f8105206a6de650786435bf6926c..1c1ff234c729a8763d70958bcbed60442af29953 100644 --- a/analysis/data/__init__.py +++ b/analysis/data/__init__.py @@ -113,7 +113,7 @@ def get_data(data_config, **kwargs): # Optional: output-type, cuts, branches input_ext = os.path.splitext(file_name)[1] try: - input_type = data_config.get('input-type', None) + input_type = data_config.get('input-type') if not input_type: input_type = get_global_var('FILE_TYPES')[input_ext] except KeyError: diff --git a/analysis/data/loaders.py b/analysis/data/loaders.py index 9a193b77ec8b90acb38016af960ccffe2c4729ad..74d9ad76f7828023a994e7f513093cd1e7d9822f 100644 --- a/analysis/data/loaders.py +++ b/analysis/data/loaders.py @@ -169,7 +169,7 @@ def get_root_from_pandas_file(file_name, tree_name, kwargs): except KeyError: raise KeyError("Badly specified weights") # Variables - var_list = kwargs.get('variables', None) + var_list = kwargs.get('variables') # Raise an error if some weights are not loaded. if not (set(weights_to_normalize+weights_not_to_normalize).issubset(set(var_list))) : raise ValueError("Missing weights in the list of variables read from input file.") @@ -202,7 +202,7 @@ def get_root_from_pandas_file(file_name, tree_name, kwargs): # Load the data frame = _load_pandas(file_name, tree_name, var_list, - kwargs.get('selection', None)) + kwargs.get('selection')) if acc_var: from analysis.efficiency import get_acceptance try: @@ -228,7 +228,7 @@ def get_root_from_pandas_file(file_name, tree_name, kwargs): return dataset_from_pandas(frame, name, title, var_list=var_list, weight_var=weight_var, - categories=kwargs.get('categories', None)) + categories=kwargs.get('categories')) ############################################################################### @@ -300,7 +300,7 @@ def get_root_from_root_file(file_name, tree_name, kwargs): # Crosscheck leaves if variables - leaves: raise ValueError("Cannot find leaves in input -> {}".format(variables - leaves)) - selection = kwargs.get('selection', None) + selection = kwargs.get('selection') leave_set = ROOT.RooArgSet() leave_list = [] if selection: @@ -385,8 +385,8 @@ def get_pandas_from_root_file(file_name, tree_name, kwargs): file_name, tree_name) if not os.path.exists(file_name): raise OSError("Cannot find input file -> {}".format(file_name)) - selection = kwargs.get('selection', None) - variables = kwargs.get('variables', None) + selection = kwargs.get('selection') + variables = kwargs.get('variables') if selection: output_data = read_root(file_name, tree_name).query(selection) if variables: diff --git a/analysis/efficiency/__init__.py b/analysis/efficiency/__init__.py index fab71aa00df41f9a2900af33443d1af613485e09..044528c10a9343bf72e7cdaf43c4423021970f6e 100644 --- a/analysis/efficiency/__init__.py +++ b/analysis/efficiency/__init__.py @@ -60,7 +60,7 @@ def get_efficiency_model_class(model_name): `Efficiency`: Efficiency class, non-instantiated. """ - return get_global_var('EFFICIENCY_MODELS').get(model_name.lower(), None) + return get_global_var('EFFICIENCY_MODELS').get(model_name.lower()) def load_efficiency_model(model_name, **extra_parameters): diff --git a/analysis/efficiency/legendre.py b/analysis/efficiency/legendre.py index 408db420b8d5b7673c0953e32e8dd15b8b692642..42a10d97aeea3f5f32a653b291ddf9742119e5cf 100644 --- a/analysis/efficiency/legendre.py +++ b/analysis/efficiency/legendre.py @@ -116,8 +116,8 @@ class LegendreEfficiency(Efficiency): var_name2: [min_var2, max_var2]}, 'symmetric-variables': [var_1]} - The range is used to rescale the data in the `fit` method. If it's not - given, it's assumed it is [-1, 1]. + The range is used to rescale the data in the `fit` method. If no range + is given, the data is assumed to be already in the range [-1, 1]. Arguments: var_list (list): List of observables to apply the efficiency to. diff --git a/analysis/fit/__init__.py b/analysis/fit/__init__.py index 5590369acda0b7fa3b1e333980f9ece2346691ce..a974f37f1b209b0a94bc2d1d11963866aac7047c 100644 --- a/analysis/fit/__init__.py +++ b/analysis/fit/__init__.py @@ -77,8 +77,7 @@ def fit(factory, pdf_name, strategy, dataset, verbose=False, **kwargs): fit_config = [ROOT.RooFit.Save(True), ROOT.RooFit.PrintLevel(2 if verbose else -1)] - if 'Range' not in kwargs: - kwargs['Range'] = 'Full' + kwargs.setdefault('Range', 'Full') for command, val in kwargs.items(): roo_cmd = getattr(ROOT.RooFit, command, None) if not roo_cmd: diff --git a/analysis/physics/__init__.py b/analysis/physics/__init__.py index 54397d9e6f681b54c8774802937fee2d2ade3d0e..5f87f94910f570cbbefeca9cfc36686cebbcc198 100644 --- a/analysis/physics/__init__.py +++ b/analysis/physics/__init__.py @@ -182,7 +182,7 @@ def configure_model(config, shared_vars=None, external_vars=None): if pdf_name not in yields: yields[pdf_name] = sanitize_parameter(yield_, 'Yield', 'Yield') # yields[pdf_name][0].setStringAttribute('shared', 'true') - if isinstance(pdf_config.get('pdf', None), str): + if isinstance(pdf_config.get('pdf'), str): factories[pdf_name] = configure_model({pdf_name: pdf_config}, shared_vars) else: factories[pdf_name] = configure_model(pdf_config, shared_vars[pdf_name]) diff --git a/analysis/physics/factory.py b/analysis/physics/factory.py index 9e0ef93d6ac23a049eb71b413f10c4c291149053..372a750d42027d1c1dc6cbb121117f996c5613e6 100644 --- a/analysis/physics/factory.py +++ b/analysis/physics/factory.py @@ -145,7 +145,7 @@ class BaseFactory(object): if recursive: obj = self._find_object(key) else: - obj = self._objects.get(key, None) + obj = self._objects.get(key) return obj if obj else default def __getitem__(self, key): @@ -330,9 +330,7 @@ class BaseFactory(object): raise ValueError("Requested non-extended PDF, " "but the factory needs to be extended") pdf_name = 'pdf_{}'.format(name) - return self.get(pdf_name) \ - if pdf_name in self \ - else self.set(pdf_name, self.get_unbound_pdf(name, title)) + return self.get(pdf_name, self.set(pdf_name, self.get_unbound_pdf(name, title))) def get_unbound_pdf(self, name, title): """Get the physics PDF. @@ -362,9 +360,7 @@ class BaseFactory(object): logger.warning("Specified yield value but it's already defined. Ignoring.") # Avoid name clashes pdf_name = 'pdfext_{}'.format(name) - return self.get(pdf_name) \ - if pdf_name in self \ - else self.set(pdf_name, self.get_unbound_extended_pdf(name, title)) + return self.get(pdf_name, self.set(pdf_name, self.get_unbound_extended_pdf(name, title))) def get_unbound_extended_pdf(self, name, title): """Get an extedned physics PDF.""" @@ -380,14 +376,13 @@ class BaseFactory(object): """Get the physics observables. """ - return tuple((self.get(obs_id) - if obs_id in self - else self.set(obs_id, execute_and_return_self(ROOT.RooRealVar(obs_name, obs_title, - obs_min, obs_max, - unit), - 'setStringAttribute', - 'originalName', - obs_id))) + return tuple((self.get(obs_id, + self.set(obs_id, execute_and_return_self(ROOT.RooRealVar(obs_name, obs_title, + obs_min, obs_max, + unit), + 'setStringAttribute', + 'originalName', + obs_id)))) for obs_id, (obs_name, obs_title, obs_min, obs_max, unit) in self.OBSERVABLES.items()) @@ -450,7 +445,7 @@ class BaseFactory(object): raise NotImplementedError() def get_yield_var(self): - return self._objects.get('Yield', None) + return self._objects.get('Yield') def set_yield_var(self, yield_): raise NotImplementedError() @@ -552,7 +547,7 @@ class PhysicsFactory(BaseFactory): return ROOT.RooExtendPdf(name, title, self.get_pdf(name+'_{noext}', title+'_{noext}'), - self._objects['Yield']) + self['Yield']) def has_to_be_extended(self): return False @@ -567,7 +562,7 @@ class PhysicsFactory(BaseFactory): params = self.PARAMETERS[:] if extended and 'Yield' in self: params.append('Yield') - return tuple(self.get(param_name) for param_name in params) + return tuple(self.get(param_name) for param_name in params) # Remove default? should fail if not found def get_gen_parameters(self): """Get all the necessary generation parameters. @@ -586,13 +581,13 @@ class PhysicsFactory(BaseFactory): else: if isinstance(yield_, (list, tuple)): yield_ = yield_[0] - if isinstance(self._objects['Yield'], ROOT.RooRealVar): + if isinstance(self['Yield'], ROOT.RooRealVar): if isinstance(yield_, ROOT.RooRealVar): - self._objects['Yield'].setVal(yield_.getVal()) - self._objects['Yield'].SetName(yield_.GetName()) - self._objects['Yield'].SetTitle(yield_.GetTitle()) + self['Yield'].setVal(yield_.getVal()) + self['Yield'].SetName(yield_.GetName()) + self['Yield'].SetTitle(yield_.GetTitle()) elif isinstance(yield_, (float, int)): - self._objects['Yield'].setVal(yield_) + self['Yield'].setVal(yield_) else: logger.warning("Trying to set a yield that cannot be overriden") @@ -638,7 +633,7 @@ class ProductPhysicsFactory(BaseFactory): return ROOT.RooExtendPdf(name, title, self.get_pdf(name+'_{noext}', title+'_{noext}'), - self._objects['Yield']) + self['Yield']) def has_to_be_extended(self): return False @@ -701,14 +696,14 @@ class ProductPhysicsFactory(BaseFactory): if isinstance(yield_, (list, tuple)): yield_, constraint = yield_ if 'Yield' not in self._objects: - self._objects['Yield'] = yield_ + self['Yield'] = yield_ if constraint: self._constraints.add(constraint) else: - if isinstance(self._objects['Yield'], ROOT.RooRealVar): - self._objects['Yield'].setVal(yield_.getVal()) - self._objects['Yield'].SetName(yield_.GetName()) - self._objects['Yield'].SetTitle(yield_.GetTitle()) + if isinstance(self['Yield'], ROOT.RooRealVar): + self['Yield'].setVal(yield_.getVal()) + self['Yield'].SetName(yield_.GetName()) + self['Yield'].SetTitle(yield_.GetTitle()) else: logger.warning("Trying to set a yield that cannot be overriden") @@ -769,7 +764,7 @@ class SumPhysicsFactory(BaseFactory): if len(factories) == len(children_yields): # Extended if yield_ is not None: raise KeyError("Specified yield on a sum of RooExtendPdf") - self._objects['Yield'] = ROOT.RooAddition("Yield", "Yield", list_to_rooarglist(yield_values)) + self['Yield'] = ROOT.RooAddition("Yield", "Yield", list_to_rooarglist(yield_values)) self._constraints.update({constraint for _, constraint in children_yields.values()}) for child_name, child in self._children.items(): child.set_yield_var(children_yields[child_name]) @@ -786,7 +781,7 @@ class SumPhysicsFactory(BaseFactory): if yield_val.getStringAttribute('shared') != 'true': yield_val.SetName(yield_val.GetName().replace('Yield', 'Fraction')) yield_val.SetTitle(yield_val.GetTitle().replace('Yield', 'Fraction')) - self._objects['Fractions'] = yield_values + self['Fractions'] = yield_values for child_name, child in self._children.items(): if child_name in children_yields.keys(): child_yield, child_constraint = children_yields[child_name] @@ -821,7 +816,7 @@ class SumPhysicsFactory(BaseFactory): pdfs.add(child.get_pdf(new_name, new_name)) return ROOT.RooAddPdf(name, title, pdfs, - list_to_rooarglist(self._objects['Fractions'])) + list_to_rooarglist(self['Fractions'])) def get_unbound_extended_pdf(self, name, title): if 'Fractions' in self: @@ -829,7 +824,7 @@ class SumPhysicsFactory(BaseFactory): return ROOT.RooExtendPdf(name, title, self.get_pdf(name+'_{noext}', title+'_{noext}'), - self._objects['Yield']) + self['Yield']) else: pdfs = ROOT.RooArgList() for child_name, child in self._children.items(): @@ -897,13 +892,13 @@ class SumPhysicsFactory(BaseFactory): child['Fraction'].GetTitle().replace('Fraction', 'Yield'), list_to_rooarglist([yield_, child['Fraction']]))) else: - if isinstance(self._objects['Yield'], ROOT.RooRealVar): + if isinstance(self['Yield'], ROOT.RooRealVar): if isinstance(yield_, ROOT.RooRealVar): - self._objects['Yield'].setVal(yield_.getVal()) - self._objects['Yield'].SetName(yield_.GetName()) - self._objects['Yield'].SetTitle(yield_.GetTitle()) + self['Yield'].setVal(yield_.getVal()) + self['Yield'].SetName(yield_.GetName()) + self['Yield'].SetTitle(yield_.GetTitle()) elif isinstance(yield_, (float, int)): - self._objects['Yield'].setVal(yield_) + self['Yield'].setVal(yield_) else: logger.warning("Trying to set a yield that cannot be overriden") @@ -962,7 +957,7 @@ class SimultaneousPhysicsFactory(BaseFactory): if category.count(';') > 0 else category) yields.add(child.get_yield_var()) - self._objects['Yield'] = ROOT.RooAddition('Yield', 'Yield', yields) + self['Yield'] = ROOT.RooAddition('Yield', 'Yield', yields) return sim_pdf def is_extended(self): @@ -996,7 +991,7 @@ class SimultaneousPhysicsFactory(BaseFactory): if child_obs.GetName() not in self._objects: obs_list[child_obs.GetName()] = self.set(child_obs.GetName(), child_obs) elif child_obs.GetName() not in obs_list: - obs_list[child_obs.GetName()] = self.get(child_obs.GetName()) + obs_list[child_obs.GetName()] = self[child_obs.GetName()] # Should fail if not there? return tuple(obs_list.values()) def set_observable(self, obs_id, obs=None, name=None, title=None, limits=None, units=None): diff --git a/analysis/physics/pdf_models.py b/analysis/physics/pdf_models.py index 46c1e3042b871f76175df9966c9da22129893154..007e97df25fff76973e22f37df826c3ad76adac0 100644 --- a/analysis/physics/pdf_models.py +++ b/analysis/physics/pdf_models.py @@ -353,7 +353,7 @@ class RooWorkspaceMixin(object): var = self._workspace.var(obs_name) if not var: raise KeyError("Observable {} not present in RooWorkspace".format(obs_name)) - if obs_id not in self._objects or var != self._objects[obs_id]: + if obs_id not in self or var != self[obs_id]: self.set(obs_id, var) self.set_observable(obs_id, title=obs_title, limits=(obs_min, obs_max), units=unit) return super(RooWorkspaceMixin, self).get_observables() diff --git a/analysis/toys/fit_toys.py b/analysis/toys/fit_toys.py index 99c550512b8633d48342ccac2926f7dcf8cbc93f..da14fdf5787fdf5db5d867a76d0238927536bce2 100644 --- a/analysis/toys/fit_toys.py +++ b/analysis/toys/fit_toys.py @@ -160,7 +160,7 @@ def run(config_files, link_from, verbose): logger.error("Missing model configuration -> %s", str(error)) raise KeyError("Missing model configuration") if not models: - logger.error("No model was specified in the config file!") + logger.error("Empty list specified in the config file under 'fit/models'!") raise KeyError() fit_strategies = config['fit'].get('strategies', ['simple']) if not fit_strategies: @@ -192,10 +192,10 @@ def run(config_files, link_from, verbose): 'source-type': 'toy', 'tree': 'data', 'output-format': 'pandas', - 'selection': data_source.get('selection', None)}), + 'selection': data_source.get('selection')}), data_source['nevents'], - data_source.get('poisson', None), - data_source.get('category', None)) + data_source.get('poisson'), + data_source.get('category')) # Generator values toy_info = get_data({'source': source_toy, 'source-type': 'toy', @@ -228,7 +228,7 @@ def run(config_files, link_from, verbose): # pylint: disable=E1101 with _paths.work_on_file(config['name'], _paths.get_toy_fit_path, - config.get('link-from', None)) as toy_fit_file: + config.get('link-from')) as toy_fit_file: with modify_hdf(toy_fit_file) as hdf_file: logger.debug("Checking generator values") test_gen = [('gen_{}'.format(data_source)) in hdf_file @@ -307,8 +307,8 @@ def run(config_files, link_from, verbose): fit_strategy, dataset, verbose, - Extended=config['fit'].get('extended', True), - Minos=config['fit'].get('minos', True)) + Extended=config['fit'].get('extended', False), + Minos=config['fit'].get('minos', False)) except ValueError: raise RuntimeError() # Now results are in fit_parameters @@ -351,7 +351,7 @@ def run(config_files, link_from, verbose): # pylint: disable=E1101 with _paths.work_on_file(config['name'], path_func=_paths.get_toy_fit_path, - link_from=config.get('link-from', None)) as toy_fit_file: + link_from=config.get('link-from')) as toy_fit_file: with modify_hdf(toy_fit_file) as hdf_file: # First fit results hdf_file.append('fit_results', fit_result_frame) diff --git a/analysis/toys/generate_toys.py b/analysis/toys/generate_toys.py index 0669579d068675b4cdf077ce34a8511a993e2774..a0c8cde33238493837891de14822ec7de94f3ded 100644 --- a/analysis/toys/generate_toys.py +++ b/analysis/toys/generate_toys.py @@ -74,7 +74,7 @@ def generate(physics_factory, n_events): raise ValueError("Generation of a simultaneous requires a dictionary for the number of events.") output_dataset = None for label, n_events_label in n_events.items(): - label_factory = physics_factory.get_children().get(label, None) + label_factory = physics_factory.get_children().get(label) if not label_factory: raise KeyError("Unknown label -> {}".format(label)) label_df = generate_events(label_factory.get_pdf("GenPdf_{}".format(label), @@ -156,7 +156,7 @@ def run(config_files, link_from): logger.warning("Generating a RooAddPdf or a RooSimultaneous: " "yields will be generated at a fixed value") try: - dataset = generate(physics, config['gen'].get('nevents-per-job', config['gen']['nevents'])) # TODO: catch config error? + dataset = generate(physics, config['gen'].get('nevents-per-job', config['gen']['nevents'])) except ValueError as error: logger.exception("Exception on generation") raise RuntimeError(str(error)) @@ -173,7 +173,7 @@ def run(config_files, link_from): # Save with work_on_file(config['name'], path_func=get_toy_path, - link_from=config.get('link-from', None)) as toy_file: + link_from=config.get('link-from')) as toy_file: with modify_hdf(toy_file) as hdf_file: hdf_file.append('data', dataset.assign(jobid=job_id)) hdf_file.append('toy_info', pd.DataFrame(toy_info)) diff --git a/analysis/toys/submitter.py b/analysis/toys/submitter.py index beab3ffc91c0761d128046774b1f990e6b549f0a..89678c9423cb873107e5c7f879eb7e1970172099 100644 --- a/analysis/toys/submitter.py +++ b/analysis/toys/submitter.py @@ -91,7 +91,7 @@ class ToySubmitter(object): # Store infotmation self.config = config # Assign link-from giving priority to the argument - self.config['link-from'] = link_from if link_from else config.get('link-from', None) + self.config['link-from'] = link_from if link_from else config.get('link-from') self.link_from = link_from self.extend = extend self.overwrite = overwrite