Skip to content

Commit

Permalink
Merge pull request #71 from henryiii/henryiii/fix/modern
Browse files Browse the repository at this point in the history
fix: modernize and correct some parts
  • Loading branch information
jpivarski authored Jul 23, 2024
2 parents cebc9fd + f1516f1 commit 78b83eb
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 17 deletions.
4 changes: 2 additions & 2 deletions _episodes/02-uproot.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ Uproot doesn't do any plotting or histogram manipulation, so the most useful met
h.to_hist().plot()
```

Uproot histograms also satisfy the [UHI plotting protocol](https://uhi.readthedocs.io/en/latest/plotting.html), so they have methods like `values` (bin contents), `variances` (errors squared), and `edges`.
Uproot histograms also satisfy the [UHI plotting protocol](https://uhi.readthedocs.io/en/latest/plotting.html), so they have methods like `values` (bin contents), `variances` (errors squared), and `axes`.

```python
h.values()
h.variances()
h.axis("x").edges() # "x", "y", "z" or 0, 1, 2
list(h.axes[0]) # "x", "y", "z" or 0, 1, 2
```

## Reading a TTree
Expand Down
14 changes: 7 additions & 7 deletions _episodes/03-ttree-details.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ However, it takes a long time because a lot of data have to be sent over the net
To limit the amount of data read, set `entry_start` and `entry_stop` to the range you want. The `entry_start` is inclusive, `entry_stop` exclusive, and the first entry would be indexed by `0`, just like slices in an array interface (first lesson). Uproot only reads as many TBaskets as are needed to provide these entries.

```python
tree["nMuon"].array(entry_start=1000, entry_stop=2000)
tree["nMuon"].array(entry_start=1_000, entry_stop=2_000)
```

These are the building blocks of a parallel data reader: each is responsible for a different slice. (See also [uproot.TTree.num_entries_for](https://uproot.readthedocs.io/en/latest/uproot.behaviors.TTree.TTree.html#num-entries-for) and [uproot.TTree.common_entry_offsets](https://uproot.readthedocs.io/en/latest/uproot.behaviors.TTree.TTree.html#common-entry-offsets), which can be used to pick `entry_start`/`entry_stop` in optimal ways.)
Expand All @@ -83,7 +83,7 @@ Suppose you know that you will need all of the muon TBranches. Asking for them i

```python
muons = tree.arrays(
["Muon_pt", "Muon_eta", "Muon_phi", "Muon_mass", "Muon_charge"], entry_stop=1000
["Muon_pt", "Muon_eta", "Muon_phi", "Muon_mass", "Muon_charge"], entry_stop=1_000
)
muons
```
Expand Down Expand Up @@ -114,7 +114,7 @@ Suppose you have many muon TBranches and you don't want to list them all. The [u
```python
tree.keys(filter_name="Muon_*")
tree.arrays(filter_name="Muon_*", entry_stop=1000)
tree.arrays(filter_name="Muon_*", entry_stop=1_000)
```
(There are also `filter_typename` and `filter_branch` for more options.)
Expand All @@ -124,7 +124,7 @@ tree.arrays(filter_name="Muon_*", entry_stop=1000)
The best way to figure out what you're doing is to tinker with small datasets, and then scale them up. Here, we take 1000 events and compute dimuon masses.

```python
muons = tree.arrays(entry_stop=1000)
muons = tree.arrays(entry_stop=1_000)
cut = muons["nMuon"] == 2

pt0 = muons["Muon_pt", cut, 0]
Expand Down Expand Up @@ -178,11 +178,11 @@ In all of the above examples, the `array`, `arrays`, and `iterate` methods retur
Use `library="np"` or `library="pd"` to get NumPy or Pandas, respectively.

```python
tree["nMuon"].array(library="np", entry_stop=10000)
tree["nMuon"].array(library="np", entry_stop=10_000)

tree.arrays(library="np", entry_stop=10000)
tree.arrays(library="np", entry_stop=10_000)

tree.arrays(library="pd", entry_stop=10000)
tree.arrays(library="pd", entry_stop=10_000)
```

NumPy is great for non-jagged data like the `"nMuon"` branch, but it has to represent an unknown number of muons per event as an array of NumPy arrays (i.e. Python objects).
Expand Down
20 changes: 14 additions & 6 deletions _episodes/05-histograms.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,31 @@ h[10:110].plot()
but often you want to select bins by coordinate value

```python
# Explicit version
h[hist.loc(90) :].plot()

# Short version
h[90j:].plot()
```

or rebin by a factor,

```python
# Explicit version
h[:: hist.rebin(2)].plot()

# Short version
h[::2j].plot()
```

or sum over a range.

```python
# Explicit version
h[hist.loc(80) : hist.loc(100) : sum]

# Short version
h[90j:100j:sum]
```

Things get more interesting when a histogram has multiple dimensions.
Expand Down Expand Up @@ -123,13 +135,9 @@ vertexhist.fill(
)

vertexhist[:, :, sum].plot2d_full()
vertexhist[
hist.loc(-0.25) : hist.loc(0.25), hist.loc(-0.25) : hist.loc(0.25), sum
].plot2d_full()
vertexhist[-0.25j:0.25j, -0.25j:0.25j, sum].plot2d_full()
vertexhist[sum, sum, :].plot()
vertexhist[
hist.loc(-0.25) : hist.loc(0.25) : sum, hist.loc(-0.25) : hist.loc(0.25) : sum, :
].plot()
vertexhist[-0.25j:0.25j:sum, -0.25j:0.25j:sum, :].plot()
```

A histogram object can have more dimensions than you can reasonably visualize—you can slice, rebin, and project it into something visual later.
Expand Down
2 changes: 1 addition & 1 deletion _episodes/06-lorentz-vectors.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ from hepunits import GeV

particle.Particle.findall("pi")

z_boson = particle.Particle.from_name("Z0")
z_boson = particle.Particle.from_string("Z0")
z_boson.mass / GeV, z_boson.width / GeV

print(z_boson.describe())
Expand Down
2 changes: 1 addition & 1 deletion _episodes/07-scaling-up.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ However, the [Coffea project](https://github.com/CoffeaTeam) ([documentation](ht

and finally

- cookie [GitHub](https://github.com/scikit-hep/cookie), [documentation](https://scikit-hep.org/developer), a template for making your own...
- cookie [GitHub](https://github.com/scientific-python/cookie), [documentation](https://learn.scientific-python.org/development), a template for making your own...

{% include links.md %}

0 comments on commit 78b83eb

Please sign in to comment.