{
"cells": [
{
"cell_type": "markdown",
"id": "101bc26e",
"metadata": {},
"source": [
"# float16\n",
"Tutorial illustrating use and precision of the Float16_t data type.\n",
"See the double32.C tutorial for all the details.\n",
"\n",
"\n",
"\n",
"**Author:** Danilo Piparo \n",
"This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Tuesday, May 19, 2026 at 08:14 PM."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0b7eea77",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%%cpp -d\n",
"\n",
"#include \"ROOT/TSeq.hxx\"\n",
"#include \"TCanvas.h\"\n",
"#include \"TFile.h\"\n",
"#include \"TGraph.h\"\n",
"#include \"TH1.h\"\n",
"#include \"TLegend.h\"\n",
"#include \"TMath.h\"\n",
"#include \"TRandom3.h\"\n",
"#include \"TTree.h\"\n",
"\n",
"class DemoFloat16 {\n",
"private:\n",
" float fF32; // reference member with full single precision\n",
" Float16_t fF16; // saved as a 16 bit floating point number\n",
" Float16_t fI16; //[-pi,pi] saved as a 16 bit unsigned int\n",
" Float16_t fI14; //[-pi,pi,14] saved as a 14 bit unsigned int\n",
" Float16_t fI10; //[-pi,pi,10] saved as a 10 bit unsigned int\n",
" Float16_t fI8; //[-pi,pi,8] saved as a 8 bit unsigned int\n",
" Float16_t fI6; //[-pi,pi,6] saved as a 6 bit unsigned int\n",
" Float16_t fI4; //[-pi,pi,4] saved as a 4 bit unsigned int\n",
" Float16_t fR8; //[0, 0, 8] saved as a 16 bit float with a 8 bits mantissa\n",
" Float16_t fR6; //[0, 0, 6] saved as a 16 bit float with a 6 bits mantissa\n",
" Float16_t fR4; //[0, 0, 4] saved as a 16 bit float with a 4 bits mantissa\n",
" Float16_t fR2; //[0, 0, 2] saved as a 16 bit float with a 2 bits mantissa\n",
"\n",
"public:\n",
" DemoFloat16() = default;\n",
" void Set(float ref) { fF32 = fF16 = fI16 = fI14 = fI10 = fI8 = fI6 = fI4 = fR8 = fR6 = fR4 = fR2 = ref; }\n",
"};"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7ee06842",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"const auto nEntries = 200000;\n",
"const auto xmax = TMath::Pi();\n",
"const auto xmin = -xmax;"
]
},
{
"cell_type": "markdown",
"id": "514db242",
"metadata": {},
"source": [
"create a Tree with nEntries objects DemoFloat16"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "158be865",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"TFile::Open(\"DemoFloat16.root\", \"recreate\");\n",
"TTree tree(\"tree\", \"DemoFloat16\");\n",
"DemoFloat16 demoInstance;\n",
"auto demoInstanceBranch = tree.Branch(\"d\", \"DemoFloat16\", &demoInstance, 4000);\n",
"TRandom3 r;\n",
"for (auto i : ROOT::TSeqI(nEntries)) {\n",
" demoInstance.Set(r.Uniform(xmin, xmax));\n",
" tree.Fill();\n",
"}\n",
"tree.Write();"
]
},
{
"cell_type": "markdown",
"id": "f079341f",
"metadata": {},
"source": [
"Now we can proceed with the analysis of the sizes on disk of all branches"
]
},
{
"cell_type": "markdown",
"id": "22771c86",
"metadata": {},
"source": [
"Create the frame histogram and the graphs"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "12c4b71a",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"auto branches = demoInstanceBranch->GetListOfBranches();\n",
"const auto nb = branches->GetEntries();\n",
"auto br = static_cast(branches->At(0));\n",
"const Long64_t zip64 = br->GetZipBytes();\n",
"\n",
"auto h = new TH1F(\"h\", \"Float16_t compression and precision\", nb, 0, nb);\n",
"h->SetMaximum(18);\n",
"h->SetStats(false);\n",
"\n",
"auto gcx = new TGraph();\n",
"gcx->SetName(\"gcx\");\n",
"gcx->SetMarkerStyle(kFullSquare);\n",
"gcx->SetMarkerColor(kBlue);\n",
"\n",
"auto gdrange = new TGraph();\n",
"gdrange->SetName(\"gdrange\");\n",
"gdrange->SetMarkerStyle(kFullCircle);\n",
"gdrange->SetMarkerColor(kRed);\n",
"\n",
"auto gdval = new TGraph();\n",
"gdval->SetName(\"gdval\");\n",
"gdval->SetMarkerStyle(kFullTriangleUp);\n",
"gdval->SetMarkerColor(kBlack);"
]
},
{
"cell_type": "markdown",
"id": "5b4c83b8",
"metadata": {},
"source": [
"loop on branches to get the precision and compression factors"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "396f1c79",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"for (auto i : ROOT::TSeqI(nb)) {\n",
" br = static_cast(branches->At(i));\n",
" const auto brName = br->GetName();\n",
" h->GetXaxis()->SetBinLabel(i + 1, brName);\n",
" auto const cx = double(zip64) / br->GetZipBytes();\n",
" gcx->SetPoint(i, i + 0.5, cx);\n",
" if (i == 0) continue;\n",
"\n",
" tree.Draw(Form(\"(fF32-%s)/(%g)\", brName, xmax - xmin), \"\", \"goff\");\n",
" const auto rmsDrange = TMath::RMS(nEntries, tree.GetV1());\n",
" const auto drange = TMath::Max(0., -TMath::Log10(rmsDrange));\n",
" gdrange->SetPoint(i - 1, i + 0.5, drange);\n",
"\n",
" tree.Draw(Form(\"(fF32-%s)/fF32 >> hdval_%s\", brName, brName), \"\", \"goff\");\n",
" const auto rmsDval = TMath::RMS(nEntries, tree.GetV1());\n",
" const auto dval = TMath::Max(0., -TMath::Log10(rmsDval));\n",
" gdval->SetPoint(i - 1, i + 0.5, dval);\n",
"\n",
" tree.Draw(Form(\"(fF32-%s) >> hdvalabs_%s\", brName, brName), \"\", \"goff\");\n",
" auto hdval = gDirectory->Get(Form(\"hdvalabs_%s\", brName));\n",
" hdval->GetXaxis()->SetTitle(\"Difference wrt reference value\");\n",
" auto c = new TCanvas(brName, brName, 800, 600);\n",
" c->SetGrid();\n",
" c->SetLogy();\n",
" hdval->DrawClone();\n",
"}\n",
"\n",
"auto c1 = new TCanvas(\"c1\", \"c1\", 800, 600);\n",
"c1->SetGrid();\n",
"\n",
"h->Draw();\n",
"h->GetXaxis()->LabelsOption(\"v\");\n",
"gcx->Draw(\"lp\");\n",
"gdrange->Draw(\"lp\");\n",
"gdval->Draw(\"lp\");"
]
},
{
"cell_type": "markdown",
"id": "8aafc927",
"metadata": {},
"source": [
"Finally build a legend"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8055a413",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"auto legend = new TLegend(0.3, 0.6, 0.9, 0.9);\n",
"legend->SetHeader(Form(\"%d entries within the [-#pi, #pi] range\", nEntries));\n",
"legend->AddEntry(gcx, \"Compression factor\", \"lp\");\n",
"legend->AddEntry(gdrange, \"Log of precision wrt range: p = -Log_{10}( RMS( #frac{Ref - x}{range} ) ) \", \"lp\");\n",
"legend->AddEntry(gdval, \"Log of precision wrt value: p = -Log_{10}( RMS( #frac{Ref - x}{Ref} ) ) \", \"lp\");\n",
"legend->Draw();"
]
},
{
"cell_type": "markdown",
"id": "45c102d0",
"metadata": {},
"source": [
"Draw all canvases "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8d76a7ab",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%jsroot on\n",
"gROOT->GetListOfCanvases()->Draw()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "ROOT C++",
"language": "c++",
"name": "root"
},
"language_info": {
"codemirror_mode": "text/x-c++src",
"file_extension": ".C",
"mimetype": " text/x-c++src",
"name": "c++"
}
},
"nbformat": 4,
"nbformat_minor": 5
}