Fill and draw a bar chart from a categorical column with RDataFrame.
RDataFrame has no dedicated action for categorical/string columns, but the generic RDataFrame::Fill() action can fill any user-provided object that exposes Fill() and Merge() methods. This shows how to use it to build and draw a bar chart from a string column.
struct AlphaNumHist {
AlphaNumHist(
const char *
name,
const char *title) : histo(
name, title, 1, 0, 1)
{
}
void Fill(
const std::string &s) { histo.
Fill(s.c_str(), 1.); }
void Merge(const std::vector<AlphaNumHist *> &others)
{
for (auto *o : others)
}
};
void writeData(std::string_view datasetName, std::string_view fileName)
{
std::random_device rd;
std::mt19937 gen{rd()};
std::uniform_int_distribution<int> distrib{0, 2};
const std::vector<std::string> colours{"RED", "GREEN", "BLUE"};
df.Define("colour", [&]() { return colours[distrib(gen)]; }).Snapshot(datasetName, fileName);
}
auto canvas = std::make_unique<TCanvas>("c");
void df041_alphanumericHistograms()
{
writeData("tree", "df041_alphanumericHistograms.root");
AlphaNumHist model("hColour", "Entries by colour;Colour;Count");
result->histo.LabelsDeflate(
"X");
result->histo.SetFillColor(45);
result->histo.DrawCopy(
"bar2");
}
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
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.
1-D histogram with a double per channel (see TH1 documentation)
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
virtual UInt_t SetCanExtend(UInt_t extendBitMask)
Make the histogram axes extendable / not extendable according to the bit mask returns the previous bi...
virtual Long64_t Merge(TCollection *list)
TMatrixT< Element > & Add(TMatrixT< Element > &target, Element scalar, const TMatrixT< Element > &source)
Modify addition: target += scalar * source.
- Date
- July 2026
- Author
- Mehkaan Khan
Definition in file df041_alphanumericHistograms.C.