|
1 | 1 | /// \file |
2 | 2 | /// \ingroup tutorial_dataframe |
3 | 3 | /// \notebook |
4 | | -/// Fill and draw a bar chart from a categorical/string column with RDataFrame. |
| 4 | +/// Fill and draw a bar chart from a categorical column with RDataFrame. |
5 | 5 | /// |
6 | 6 | /// RDataFrame has no dedicated action for categorical/string columns, but the |
7 | 7 | /// generic RDataFrame::Fill() action can fill any user-provided object that |
8 | 8 | /// exposes Fill() and Merge() methods. This shows how to use it to build and |
9 | | -/// draw a bar chart from a string column, reusing the "Nation" column of the |
10 | | -/// classic CERN staff dataset also used in hist006_TH1_bar_charts.C. |
11 | | -/// |
12 | | -/// Classic ROOT trees often store short strings in fixed-size char[] branches |
13 | | -/// (leaf type "C"), which RDataFrame reads as ROOT::VecOps::RVec<char> rather |
14 | | -/// than std::string. A one-line Define() converts such a column into a real |
15 | | -/// std::string column before filling. |
| 9 | +/// draw a bar chart from a string column. |
16 | 10 | /// |
17 | 11 | /// \macro_image |
18 | 12 | /// \macro_code |
|
27 | 21 | // const char*, so RDataFrame can't call it directly on a plain TH1D. Wrapping |
28 | 22 | // it in a class with our own Fill(const std::string&) sidesteps both issues. |
29 | 23 | struct AlphaNumHist { |
30 | | - TH1D h; |
| 24 | + TH1D histo; |
31 | 25 |
|
32 | | - AlphaNumHist(const char *name, const char *title) : h(name, title, 1, 0, 1) |
| 26 | + AlphaNumHist(const char *name, const char *title) : histo(name, title, 1, 0, 1) |
33 | 27 | { |
34 | | - h.GetXaxis()->SetAlphanumeric(true); |
35 | | - h.SetCanExtend(TH1::kAllAxes); |
| 28 | + histo.GetXaxis()->SetAlphanumeric(true); |
| 29 | + histo.SetCanExtend(TH1::kAllAxes); |
36 | 30 | } |
37 | 31 |
|
38 | | - void Fill(const std::string &s) { h.Fill(s.c_str(), 1.); } |
| 32 | + void Fill(const std::string &s) { histo.Fill(s.c_str(), 1.); } |
39 | 33 |
|
40 | 34 | // Required so RDataFrame can merge partial per-slot results when run with |
41 | 35 | // implicit multi-threading enabled. |
42 | 36 | void Merge(const std::vector<AlphaNumHist *> &others) |
43 | 37 | { |
44 | 38 | TList l; |
45 | 39 | for (auto *o : others) |
46 | | - l.Add(&o->h); |
47 | | - h.Merge(&l); |
| 40 | + l.Add(&o->histo); |
| 41 | + histo.Merge(&l); |
48 | 42 | } |
49 | 43 | }; |
50 | 44 |
|
51 | | -void df041_alphanumericHistograms() |
| 45 | +void writeData(std::string_view datasetName, std::string_view fileName) |
52 | 46 | { |
53 | | - // Reuse the same "cernstaff.root" dataset as hist006_TH1_bar_charts.C, |
54 | | - // generating it first if it doesn't already exist. |
55 | | - TString filedir = gROOT->GetTutorialDir(); |
56 | | - filedir += TString("/io/tree/"); |
57 | | - TString filename = "cernstaff.root"; |
58 | | - bool fileNotFound = gSystem->AccessPathName(filename); |
59 | | - if (fileNotFound) { |
60 | | - TString macroName = filedir + "tree500_cernbuild.C"; |
61 | | - if (!gInterpreter->IsLoaded(macroName)) |
62 | | - gInterpreter->LoadMacro(macroName); |
63 | | - gROOT->ProcessLineFast("tree500_cernbuild()"); |
64 | | - } |
| 47 | + std::random_device rd; |
| 48 | + std::mt19937 gen{rd()}; |
| 49 | + std::uniform_int_distribution<int> distrib{0, 2}; |
65 | 50 |
|
66 | | - ROOT::RDataFrame df0("T", filename.Data()); |
| 51 | + const std::vector<std::string> colours{"RED", "GREEN", "BLUE"}; |
| 52 | + |
| 53 | + ROOT::RDataFrame df{100}; |
| 54 | + df.Define("colour", [&]() { return colours[distrib(gen)]; }).Snapshot(datasetName, fileName); |
| 55 | +} |
| 56 | + |
| 57 | +auto canvas = std::make_unique<TCanvas>("c"); |
| 58 | + |
| 59 | +void df041_alphanumericHistograms() |
| 60 | +{ |
| 61 | + writeData("tree", "df041_alphanumericHistograms.root"); |
67 | 62 |
|
68 | | - // "Nation" is a classic char[] branch, read by RDataFrame as |
69 | | - // ROOT::VecOps::RVec<char>. Converting it to a real std::string column |
70 | | - // makes it fillable through the AlphaNumHist adapter above. |
71 | | - auto df = df0.Define("NationStr", [](const ROOT::VecOps::RVec<char> &c) { return std::string(c.begin(), c.end()); }, |
72 | | - {"Nation"}); |
| 63 | + ROOT::RDataFrame df("tree", "df041_alphanumericHistograms.root"); |
73 | 64 |
|
74 | | - AlphaNumHist model("hNation", "Staff by nation;Nation;Count"); |
75 | | - auto h = df.Fill<std::string>(model, {"NationStr"}); |
76 | | - h->h.LabelsDeflate("X"); |
| 65 | + AlphaNumHist model("hColour", "Entries by colour;Colour;Count"); |
| 66 | + auto result = df.Fill<std::string>(model, {"colour"}); |
| 67 | + result->histo.LabelsDeflate("X"); |
77 | 68 |
|
78 | | - auto c1 = new TCanvas("c1", "Bar chart from RDataFrame", 700, 500); |
79 | | - h->h.SetFillColor(45); |
80 | | - h->h.DrawCopy("bar2"); |
| 69 | + result->histo.SetFillColor(45); |
| 70 | + result->histo.DrawCopy("bar2"); |
81 | 71 | } |
0 commit comments