# MultiCell histograms

Usually, one has to deal with `n` events to histogram by sorting each event into bins based on some variable `x` and by adding the value of a `weight` to the value stored in that bin.

However, there are also cases where one has to create multiple histograms for multiple independent weights that are all sorted into the same bin per event. Each event has one variable `x` and a list `weights` with a fixed number of `k` weights. A usage example could be to study a nominal weight and some systematic effects that manifest in variations of this weight without changing the the variable for selecting the bin (without changing `x`).

In such a case, one option would be to create `k` histograms (one for each weight) and sort each event into these `k` histograms (each time providing a different weight). The downsides of this approach is, that one has to do the sorting of the events into the bins `k` times whereas one time would be sufficient. One also has to create `k` histogram objects.

A more efficient approach would be to have only one histogram that stores a list of `k` weights per bin. This is achieved by using the `boost_histogram.storage.MultiCell` storage.

```none
[1]:
```

A `MultiCell` histogram `h` has to be provided with `k`, the fixed number of cells (in this example the weights) per bin, at construction time:

```none
[2]:
```

To fill the `MultiCell` histogram with `n` events, one has to provide a 2-dimensional array of weights with the shape `(n, k)` via the `weight` keyword of the `fill()` method:

```none
[ ]:
```

Now, each bin of `h` stores a list of `k` weights:

```none
[31]:
```

```none
[31]:
```

One can interact with a `MultiCell` identical to any other histogram with a different storage type.

However, 2 important keypoints to remember:

1. A `view` of a `MultiCell` histogram has the cells (weights) as first dimension. In our case, `h.view()` will have the shape `(k, 7)` with `7` being the number of bins from the `bh.axis.Integer(0, 7)` axis.:

```none
[32]:
```

```none
[32]:
```

To get the histogram corresponding to the weight with index 3 one can use `h.view()[3]`

1. `MultiCell` histograms do NOT track the variances of their cell contents (weights) (in contrast to histograms with the `Weight` storage). If required, one can track the variances by adding them as another weight to the histogram:

```none
[ ]:
```

Now, the weight with index `k + 1` is the square of the weight with index `0` and therefore tracks the sum-of-weights-squared of the first weight.

## Benchmarks

Let’s estimate how much time one can save with `MultiCell` histograms in a ‘real world’ example in contrast to using multiple histograms with `Weight` and `Double` storage.

For a precicsion analysis we have to process 150 million events. We can fill 10000 events per iteration. Each event has 2000 different weights due to systematic variations. We also have some reweighting that increases the number of weights by a factor 10 to 20000 weights. The events are sorted based on two variables into a `8 x 8` histogram that uses `variable` axis.

We are only interested in the sum-of-weights-squared for the nominal weight (which would be 10 out of these 20000 in this case), therefore we will use histograms with a `Double` storage type as comparison to the `MultiCell` histogram.

```none
[ ]:
```

```none
[51]:
```

Filling the `Double` type histograms:

```none
[53]:
```

Filling the `MultiCell` histogram:

```none
[ ]:
```

On my test laptop, filling the `k` `Double` type histograms took `26.7 +- 1.6 s` whereas filling the `MultiCell` histogram only took `325 +- 60 ms` which is `82` times faster.

Therefore, using `MultiCell` histograms instead of multiple `Double` type histograms for filling all 150 million events would save us `110` hours (`1.4 h` for the `MultiCell` filling vs. `111 h` for iterated `Double` type filling)
