Ejemplo n.º 1
0
    def _compute_outputs(self, inputs=None):
        """Apply basic cuts and compute histograms for output channels."""

        logging.debug('Entering events_to_data._compute_outputs')

        #Hashing
        #TODO What should I hash??
        hash_property = [
            self.events_file, self.params['dataset'].value, self.output_names
        ]
        this_hash = hash_obj(hash_property, full_hash=self.full_hash)
        #if this_hash == self.sample_hash: #TODO Fix this and replace...
        #    return

        #TODO Check there are no inputs

        #Fill an events instance from a file
        events = Events(self.events_file)

        #TODO Handle nominal, etc, etc datasets?

        #Extract the neutrino data from the 'Events' instance
        nu_data = []
        flav_fidg = FlavIntDataGroup(flavint_groups=events.flavints)
        for flavint in events.present_flavints:
            flav_fidg[flavint] = {
                var: events[flavint][var]
                for var in events[flavint].keys()
            }
        nu_data.append(flav_fidg)

        #Create the data instance, including the metadata
        #Note that there is no muon or noise data  in the 'Events'
        data = Data(reduce(add, nu_data), metadata=deepcopy(events.metadata))

        #Make cuts
        if self.params['keep_criteria'].value is not None:
            self._data.applyCut(self.params['keep_criteria'].value
                                )  #TODO Shivesh says this needs testing
            self._data.update_hash()

        #Update hashes
        self.sample_hash = this_hash
        data.metadata['sample_hash'] = this_hash
        data.update_hash()

        return data
Ejemplo n.º 2
0
    def load_from_nu_file(events_file, all_flavints, weight, weight_units,
                          keep_keys, aliases):
        flav_fidg = FlavIntDataGroup(flavint_groups=all_flavints)

        events = from_file(events_file)
        sample.strip_keys(keep_keys, events)

        nu_mask = events['ptype'] > 0
        nubar_mask = events['ptype'] < 0
        cc_mask = events['interaction'] == 1
        nc_mask = events['interaction'] == 2

        if weight == 'None' or weight == '1':
            events['sample_weight'] = \
                np.ones(events['ptype'].shape) * ureg.dimensionless
        elif weight == '0':
            events['sample_weight'] = \
                np.zeros(events['ptype'].shape) * ureg.dimensionless
        else:
            events['sample_weight'] = events[weight] * \
                ureg(weight_units)
        events['pisa_weight'] = deepcopy(events['sample_weight'])

        for alias, expr in aliases:
            if alias in events:
                logging.warning(
                    'Overwriting Data key {0} with aliased expression '
                    '{1}'.format(alias, expr))
            events[alias] = eval(re.sub(r'\<(.*?)\>', r"events['\1']", expr))

        for flavint in all_flavints:
            i_mask = cc_mask if flavint.cc else nc_mask
            t_mask = nu_mask if flavint.particle else nubar_mask

            flav_fidg[flavint] = {
                var: events[var][i_mask & t_mask]
                for var in events.iterkeys()
            }
        return flav_fidg
Ejemplo n.º 3
0
    def __add__(self, other):
        muons = None
        noise = None
        assert isinstance(other, Data)

        metadata = {}
        for key in self.metadata:
            if key == 'flavints_joined':
                continue
            if key in other.metadata:
                if self.metadata[key] != other.metadata[key]:
                    raise AssertionError('Metadata mismatch, key {0}, {1} != '
                                         '{2}'.format(key, self.metadata[key],
                                                      other.metadata[key]))
                else:
                    metadata[key] = deepcopy(self.metadata[key])
            else:
                metadata[key] = deepcopy(self.metadata[key])

        for key in other.metadata:
            if key == 'flavints_joined':
                continue
            if key in self.metadata:
                if other.metadata[key] != self.metadata[key]:
                    raise AssertionError('Metadata mismatch, key {0}, {1} != '
                                         '{2}'.format(key, other.metadata[key],
                                                      self.metadata[key]))
                else:
                    metadata[key] = deepcopy(other.metadata[key])
            else:
                metadata[key] = deepcopy(other.metadata[key])

        if self.contains_muons:
            if other.contains_muons:
                muons = self._merge(deepcopy(self['muons']), other['muons'])
            else:
                muons = deepcopy(self['muons'])
        elif other.contains_muons:
            muons = deepcopy(other['muons'])

        if self.contains_noise:
            if other.contains_noise:
                noise = self._merge(deepcopy(self['noise']), other['noise'])
            else:
                noise = deepcopy(self['noise'])
        elif other.contains_noise:
            noise = deepcopy(other['noise'])

        if len(self.flavint_groups) == 0:
            if len(other.flavint_groups) == 0:
                a_fidg = FlavIntDataGroup(other)
        elif len(other.flavint_groups) == 0:
            a_fidg = FlavIntDataGroup(self)
        else:
            a_fidg = super(Data, self).__add__(other)
        metadata['flavints_joined'] = [str(f) for f in a_fidg.flavint_groups]

        if muons is not None:
            a_dict = dict(a_fidg)
            metadata['flavints_joined'] += ['muons']
            a_dict['muons'] = muons
            a_fidg = a_dict
        if noise is not None:
            a_dict = dict(a_fidg)
            metadata['flavints_joined'] += ['noise']
            a_dict['noise'] = noise
            a_fidg = a_dict
        return Data(a_fidg, metadata=metadata)