Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
df041_alphanumericHistograms.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_dataframe
3/// \notebook
4/// Fill and draw a bar chart from a categorical column with RDataFrame.
5///
6/// RDataFrame has no dedicated action for categorical/string columns, but the
7/// generic RDataFrame::Fill() action can fill any user-provided object that
8/// exposes Fill() and Merge() methods. This shows how to use it to build and
9/// draw a bar chart from a string column.
10///
11/// \macro_image
12/// \macro_code
13/// \macro_output
14///
15/// \date July 2026
16/// \author Mehkaan Khan
17
18// A small adapter around TH1D that lets RDataFrame's generic Fill() action
19// build a histogram from a std::string column: TH1D::Fill(const char*, Double_t)
20// takes no default weight and std::string has no implicit conversion to
21// const char*, so RDataFrame can't call it directly on a plain TH1D. Wrapping
22// it in a class with our own Fill(const std::string&) sidesteps both issues.
23struct AlphaNumHist {
24 TH1D histo;
25
26 AlphaNumHist(const char *name, const char *title) : histo(name, title, 1, 0, 1)
27 {
28 histo.GetXaxis()->SetAlphanumeric(true);
30 }
31
32 void Fill(const std::string &s) { histo.Fill(s.c_str(), 1.); }
33
34 // Required so RDataFrame can merge partial per-slot results when run with
35 // implicit multi-threading enabled.
36 void Merge(const std::vector<AlphaNumHist *> &others)
37 {
38 TList l;
39 for (auto *o : others)
40 l.Add(&o->histo);
41 histo.Merge(&l);
42 }
43};
44
45void writeData(std::string_view datasetName, std::string_view fileName)
46{
47 std::random_device rd;
48 std::mt19937 gen{rd()};
49 std::uniform_int_distribution<int> distrib{0, 2};
50
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
57auto canvas = std::make_unique<TCanvas>("c");
58
60{
61 writeData("tree", "df041_alphanumericHistograms.root");
62
63 ROOT::RDataFrame df("tree", "df041_alphanumericHistograms.root");
64
65 AlphaNumHist model("hColour", "Entries by colour;Colour;Count");
66 auto result = df.Fill<std::string>(model, {"colour"});
67 result->histo.LabelsDeflate("X");
68
69 result->histo.SetFillColor(45);
70 result->histo.DrawCopy("bar2");
71}
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
char name[80]
Definition TGX11.cxx:148
ROOT's RDataFrame offers a modern, high-level interface for analysis of data stored in TTree ,...
void SetAlphanumeric(Bool_t alphanumeric=kTRUE)
Set axis alphanumeric.
Definition TAxis.cxx:847
1-D histogram with a double per channel (see TH1 documentation)
Definition TH1.h:926
TAxis * GetXaxis()
Definition TH1.h:571
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition TH1.cxx:3489
@ kAllAxes
Definition TH1.h:126
virtual UInt_t SetCanExtend(UInt_t extendBitMask)
Make the histogram axes extendable / not extendable according to the bit mask returns the previous bi...
Definition TH1.cxx:6860
virtual Long64_t Merge(TCollection *list)
Definition TH1.h:592
A doubly linked list.
Definition TList.h:38
void Fill(float *output, float value, int size)
TMatrixT< Element > & Add(TMatrixT< Element > &target, Element scalar, const TMatrixT< Element > &source)
Modify addition: target += scalar * source.
TLine l
Definition textangle.C:4