Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
df041_alphanumericHistograms.C File Reference

Detailed Description

View in nbviewer Open in SWAN
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.

// A small adapter around TH1D that lets RDataFrame's generic Fill() action
// build a histogram from a std::string column: TH1D::Fill(const char*, Double_t)
// takes no default weight and std::string has no implicit conversion to
// const char*, so RDataFrame can't call it directly on a plain TH1D. Wrapping
// it in a class with our own Fill(const std::string&) sidesteps both issues.
struct AlphaNumHist {
TH1D histo;
AlphaNumHist(const char *name, const char *title) : histo(name, title, 1, 0, 1)
{
histo.GetXaxis()->SetAlphanumeric(true);
}
void Fill(const std::string &s) { histo.Fill(s.c_str(), 1.); }
// Required so RDataFrame can merge partial per-slot results when run with
// implicit multi-threading enabled.
void Merge(const std::vector<AlphaNumHist *> &others)
{
for (auto *o : others)
l.Add(&o->histo);
histo.Merge(&l);
}
};
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");
{
writeData("tree", "df041_alphanumericHistograms.root");
ROOT::RDataFrame df("tree", "df041_alphanumericHistograms.root");
AlphaNumHist model("hColour", "Entries by colour;Colour;Count");
auto result = df.Fill<std::string>(model, {"colour"});
result->histo.LabelsDeflate("X");
result->histo.SetFillColor(45);
result->histo.DrawCopy("bar2");
}
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
TMatrixT< Element > & Add(TMatrixT< Element > &target, Element scalar, const TMatrixT< Element > &source)
Modify addition: target += scalar * source.
TLine l
Definition textangle.C:4
Date
July 2026
Author
Mehkaan Khan

Definition in file df041_alphanumericHistograms.C.