{ "cells": [ { "cell_type": "markdown", "id": "2c7383c7", "metadata": {}, "source": [ "# double32\n", "Tutorial illustrating use and precision of the Double32_t data type\n", "You should run this tutorial with ACLIC: a dictionary will be automatically\n", "created.\n", "```bash\n", " root > .x double32.C+\n", "```\n", "The following cases are supported for streaming a Double32_t type\n", "depending on the range declaration in the comment field of the data member:\n", "\n", "Case | Declaration\n", "-----|------------\n", " A | Double32_t fNormal;\n", " B | Double32_t fTemperature; //[0,100]\n", " C | Double32_t fCharge; //[-1,1,2]\n", " D | Double32_t fVertex[3]; //[-30,30,10]\n", " E | Double32_t fChi2; //[0,0,6]\n", " F | Int_t fNsp;
Double32_t* fPointValue; //[fNsp][0,3]\n", "\n", " * Case A fNormal is converted from a Double_t to a Float_t\n", " * Case B fTemperature is converted to a 32 bit unsigned integer\n", " * Case C fCharge is converted to a 2 bits unsigned integer\n", " * Case D the array elements of fVertex are converted to an unsigned 10 bits integer\n", " * Case E fChi2 is converted to a Float_t with truncated precision at 6 bits\n", " * Case F the fNsp elements of array fPointvalue are converted to an unsigned 32 bit integer. Note that the range specifier must follow the dimension specifier.\n", "\n", "Case B has more precision than case A: 9 to 10 significative digits and 6 to 7 digits respectively.\n", "The range specifier has the general format: [xmin,xmax] or [xmin,xmax,nbits]. Examples\n", " * [0,1]\n", " * [-10,100];\n", " * [-pi,pi], [-pi/2,pi/4],[-2pi,2*pi]\n", " * [-10,100,16]\n", " * [0,0,8]\n", "Note that:\n", " * If nbits is not specified, or nbits <2 or nbits>32 it is set to 32\n", " * If (xmin==0 and xmax==0 and nbits <=14) the double word will be converted to a float and its mantissa truncated to nbits significative bits.\n", "\n", "## IMPORTANT NOTE\n", "Lets assume an original variable double x.\n", "When using the format [0,0,8] (i.e. range not specified) you get the best\n", "relative precision when storing and reading back the truncated x, say xt.\n", "The variance of (x-xt)/x will be better than when specifying a range\n", "for the same number of bits. However the precision relative to the\n", "range (x-xt)/(xmax-xmin) will be worse, and vice-versa.\n", "The format [0,0,8] is also interesting when the range of x is infinite\n", "or unknown.\n", "\n", "\n", "\n", "\n", "**Author:** Rene Brun \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:13 PM." ] }, { "cell_type": "code", "execution_count": null, "id": "476171c9", "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 DemoDouble32 {\n", "private:\n", " Double_t fD64; // reference member with full double precision\n", " Double32_t fF32; // saved as a 32 bit Float_t\n", " Double32_t fI32; //[-pi,pi] saved as a 32 bit unsigned int\n", " Double32_t fI30; //[-pi,pi,30] saved as a 30 bit unsigned int\n", " Double32_t fI28; //[-pi,pi,28] saved as a 28 bit unsigned int\n", " Double32_t fI26; //[-pi,pi,26] saved as a 26 bit unsigned int\n", " Double32_t fI24; //[-pi,pi,24] saved as a 24 bit unsigned int\n", " Double32_t fI22; //[-pi,pi,22] saved as a 22 bit unsigned int\n", " Double32_t fI20; //[-pi,pi,20] saved as a 20 bit unsigned int\n", " Double32_t fI18; //[-pi,pi,18] saved as a 18 bit unsigned int\n", " Double32_t fI16; //[-pi,pi,16] saved as a 16 bit unsigned int\n", " Double32_t fI14; //[-pi,pi,14] saved as a 14 bit unsigned int\n", " Double32_t fI12; //[-pi,pi,12] saved as a 12 bit unsigned int\n", " Double32_t fI10; //[-pi,pi,10] saved as a 10 bit unsigned int\n", " Double32_t fI8; //[-pi,pi, 8] saved as a 8 bit unsigned int\n", " Double32_t fI6; //[-pi,pi, 6] saved as a 6 bit unsigned int\n", " Double32_t fI4; //[-pi,pi, 4] saved as a 4 bit unsigned int\n", " Double32_t fI2; //[-pi,pi, 2] saved as a 2 bit unsigned int\n", " Double32_t fR14; //[0, 0, 14] saved as a 32 bit float with a 14 bits mantissa\n", " Double32_t fR12; //[0, 0, 12] saved as a 32 bit float with a 12 bits mantissa\n", " Double32_t fR10; //[0, 0, 10] saved as a 32 bit float with a 10 bits mantissa\n", " Double32_t fR8; //[0, 0, 8] saved as a 32 bit float with a 8 bits mantissa\n", " Double32_t fR6; //[0, 0, 6] saved as a 32 bit float with a 6 bits mantissa\n", " Double32_t fR4; //[0, 0, 4] saved as a 32 bit float with a 4 bits mantissa\n", " Double32_t fR2; //[0, 0, 2] saved as a 32 bit float with a 2 bits mantissa\n", "\n", "public:\n", " DemoDouble32() = default;\n", " void Set(Double_t ref)\n", " {\n", " fD64 = fF32 = fI32 = fI30 = fI28 = fI26 = fI24 = fI22 = fI20 = fI18 = fI16 = fI14 = fI12 = fI10 = fI8 = fI6 =\n", " fI4 = fI2 = fR14 = fR12 = fR10 = fR8 = fR6 = fR4 = fR2 = ref;\n", " }\n", "};" ] }, { "cell_type": "code", "execution_count": null, "id": "eb4cd3ec", "metadata": { "collapsed": false }, "outputs": [], "source": [ "const auto nEntries = 40000;\n", "const auto xmax = TMath::Pi();\n", "const auto xmin = -xmax;" ] }, { "cell_type": "markdown", "id": "67c61eda", "metadata": {}, "source": [ "create a Tree with nEntries objects DemoDouble32" ] }, { "cell_type": "code", "execution_count": null, "id": "47ff5627", "metadata": { "collapsed": false }, "outputs": [], "source": [ "TFile::Open(\"DemoDouble32.root\", \"recreate\");\n", "TTree tree(\"tree\", \"DemoDouble32\");\n", "DemoDouble32 demoInstance;\n", "auto demoInstanceBranch = tree.Branch(\"d\", \"DemoDouble32\", &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": "82fe0dfd", "metadata": {}, "source": [ "Now we can proceed with the analysis of the sizes on disk of all branches" ] }, { "cell_type": "markdown", "id": "dd6b6d89", "metadata": {}, "source": [ "Create the frame histogram and the graphs" ] }, { "cell_type": "code", "execution_count": null, "id": "2a6d88f7", "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\", \"Double32_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": "2bc0bfed", "metadata": {}, "source": [ "loop on branches to get the precision and compression factors" ] }, { "cell_type": "code", "execution_count": null, "id": "1419a86c", "metadata": { "collapsed": false }, "outputs": [], "source": [ "for (auto i : ROOT::TSeqI(nb)) {\n", " auto br = static_cast(branches->At(i));\n", " const auto brName = br->GetName();\n", "\n", " h->GetXaxis()->SetBinLabel(i + 1, brName);\n", " const auto cx = double(zip64) / br->GetZipBytes();\n", " gcx->SetPoint(i, i + 0.5, cx);\n", " if (i == 0 ) continue;\n", "\n", " tree.Draw(Form(\"(fD64-%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(\"(fD64-%s)/fD64\", 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(\"(fD64-%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": "3a98bf6e", "metadata": {}, "source": [ "Finally build a legend" ] }, { "cell_type": "code", "execution_count": null, "id": "5a91fd9e", "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": "184154d6", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": null, "id": "259950b2", "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 }