Logo ROOT  
Reference Guide
float16.C File Reference

Detailed Description

View in nbviewer Open in SWAN Tutorial illustrating use and precision of the Float16_t data type. See the double32.C tutorial for all the details.

#include "ROOT/TSeq.hxx"
#include "TCanvas.h"
#include "TFile.h"
#include "TGraph.h"
#include "TH1.h"
#include "TLegend.h"
#include "TMath.h"
#include "TRandom3.h"
#include "TTree.h"
class DemoFloat16 {
private:
float fF32; // reference member with full single precision
Float16_t fF16; // saved as a 16 bit floating point number
Float16_t fI16; //[-pi,pi] saved as a 16 bit unsigned int
Float16_t fI14; //[-pi,pi,14] saved as a 14 bit unsigned int
Float16_t fI10; //[-pi,pi,10] saved as a 10 bit unsigned int
Float16_t fI8; //[-pi,pi,8] saved as a 8 bit unsigned int
Float16_t fI6; //[-pi,pi,6] saved as a 6 bit unsigned int
Float16_t fI4; //[-pi,pi,4] saved as a 4 bit unsigned int
Float16_t fR8; //[0, 0, 8] saved as a 16 bit float with a 8 bits mantissa
Float16_t fR6; //[0, 0, 6] saved as a 16 bit float with a 6 bits mantissa
Float16_t fR4; //[0, 0, 4] saved as a 16 bit float with a 4 bits mantissa
Float16_t fR2; //[0, 0, 2] saved as a 16 bit float with a 2 bits mantissa
public:
DemoFloat16() = default;
void Set(float ref) { fF32 = fF16 = fI16 = fI14 = fI10 = fI8 = fI6 = fI4 = fR8 = fR6 = fR4 = fR2 = ref; }
};
void float16()
{
const auto nEntries = 200000;
const auto xmax = TMath::Pi();
const auto xmin = -xmax;
// create a Tree with nEntries objects DemoFloat16
TFile::Open("DemoFloat16.root", "recreate");
TTree tree("tree", "DemoFloat16");
DemoFloat16 demoInstance;
auto demoInstanceBranch = tree.Branch("d", "DemoFloat16", &demoInstance, 4000);
for (auto i : ROOT::TSeqI(nEntries)) {
demoInstance.Set(r.Uniform(xmin, xmax));
tree.Fill();
}
tree.Write();
// Now we can proceed with the analysis of the sizes on disk of all branches
// Create the frame histogram and the graphs
auto branches = demoInstanceBranch->GetListOfBranches();
const auto nb = branches->GetEntries();
auto br = static_cast<TBranch *>(branches->At(0));
const Long64_t zip64 = br->GetZipBytes();
auto h = new TH1F("h", "Float16_t compression and precision", nb, 0, nb);
h->SetMaximum(18);
h->SetStats(0);
auto gcx = new TGraph();
gcx->SetName("gcx");
gcx->SetMarkerStyle(kFullSquare);
gcx->SetMarkerColor(kBlue);
auto gdrange = new TGraph();
gdrange->SetName("gdrange");
gdrange->SetMarkerStyle(kFullCircle);
gdrange->SetMarkerColor(kRed);
auto gdval = new TGraph();
gdval->SetName("gdval");
gdval->SetMarkerStyle(kFullTriangleUp);
gdval->SetMarkerColor(kBlack);
// loop on branches to get the precision and compression factors
for (auto i : ROOT::TSeqI(nb)) {
br = static_cast<TBranch *>(branches->At(i));
const auto brName = br->GetName();
h->GetXaxis()->SetBinLabel(i + 1, brName);
auto const cx = double(zip64) / br->GetZipBytes();
gcx->SetPoint(i, i + 0.5, cx);
if (i == 0) continue;
tree.Draw(Form("(fF32-%s)/(%g)", brName, xmax - xmin), "", "goff");
const auto rmsDrange = TMath::RMS(nEntries, tree.GetV1());
const auto drange = TMath::Max(0., -TMath::Log10(rmsDrange));
gdrange->SetPoint(i - 1, i + 0.5, drange);
tree.Draw(Form("(fF32-%s)/fF32 >> hdval_%s", brName, brName), "", "goff");
const auto rmsDval = TMath::RMS(nEntries, tree.GetV1());
const auto dval = TMath::Max(0., -TMath::Log10(rmsDval));
gdval->SetPoint(i - 1, i + 0.5, dval);
tree.Draw(Form("(fF32-%s) >> hdvalabs_%s", brName, brName), "", "goff");
auto hdval = gDirectory->Get<TH1F>(Form("hdvalabs_%s", brName));
hdval->GetXaxis()->SetTitle("Difference wrt reference value");
auto c = new TCanvas(brName, brName, 800, 600);
c->SetGrid();
c->SetLogy();
hdval->DrawClone();
}
auto c1 = new TCanvas("c1", "c1", 800, 600);
c1->SetGrid();
h->Draw();
h->GetXaxis()->LabelsOption("v");
gcx->Draw("lp");
gdrange->Draw("lp");
gdval->Draw("lp");
// Finally build a legend
auto legend = new TLegend(0.3, 0.6, 0.9, 0.9);
legend->SetHeader(Form("%d entries within the [-#pi, #pi] range", nEntries));
legend->AddEntry(gcx, "Compression factor", "lp");
legend->AddEntry(gdrange, "Log of precision wrt range: p = -Log_{10}( RMS( #frac{Ref - x}{range} ) ) ", "lp");
legend->AddEntry(gdval, "Log of precision wrt value: p = -Log_{10}( RMS( #frac{Ref - x}{Ref} ) ) ", "lp");
legend->Draw();
}
Author
Danilo Piparo

Definition in file float16.C.

c
#define c(i)
Definition: RSha256.hxx:119
TBranch::GetZipBytes
Long64_t GetZipBytes(Option_t *option="") const
Return total number of zip bytes in the branch if option ="*" includes all sub-branches of this branc...
Definition: TBranch.cxx:2071
TMath::Max
Short_t Max(Short_t a, Short_t b)
Definition: TMathBase.h:212
tree
Definition: tree.py:1
Form
char * Form(const char *fmt,...)
r
ROOT::R::TRInterface & r
Definition: Object.C:4
TGraph.h
xmax
float xmax
Definition: THbookFile.cxx:95
Long64_t
long long Long64_t
Definition: RtypesCore.h:73
TLegend.h
TTree
Definition: TTree.h:79
TFile::Open
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
Definition: TFile.cxx:3946
TLegend::SetHeader
virtual void SetHeader(const char *header="", Option_t *option="")
Sets the header, which is the "title" that appears at the top of the legend.
Definition: TLegend.cxx:1099
TCanvas.h
TTree.h
TFile.h
TBranch
Definition: TBranch.h:89
kBlack
@ kBlack
Definition: Rtypes.h:65
Float16_t
float Float16_t
Definition: RtypesCore.h:58
TMath::Pi
constexpr Double_t Pi()
Definition: TMath.h:43
kFullCircle
@ kFullCircle
Definition: TAttMarker.h:51
TMath::Log10
Double_t Log10(Double_t x)
Definition: TMath.h:764
kFullTriangleUp
@ kFullTriangleUp
Definition: TAttMarker.h:51
xmin
float xmin
Definition: THbookFile.cxx:95
TRandom3
Definition: TRandom3.h:27
h
#define h(i)
Definition: RSha256.hxx:124
TSeq.hxx
TLegend::AddEntry
TLegendEntry * AddEntry(const TObject *obj, const char *label="", Option_t *option="lpf")
Add a new entry to this legend.
Definition: TLegend.cxx:330
gDirectory
#define gDirectory
Definition: TDirectory.h:236
TRandom3.h
double
double
Definition: Converters.cxx:921
TMath::RMS
Double_t RMS(Long64_t n, const T *a, const Double_t *w=0)
Return the Standard Deviation of an array a with length n.
Definition: TMath.h:1167
kRed
@ kRed
Definition: Rtypes.h:66
TNamed::SetTitle
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition: TNamed.cxx:164
kFullSquare
@ kFullSquare
Definition: TAttMarker.h:51
TGraph
Definition: TGraph.h:41
TCanvas
Definition: TCanvas.h:23
TH1F
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:572
kBlue
@ kBlue
Definition: Rtypes.h:66
TLegend::Draw
virtual void Draw(Option_t *option="")
Draw this legend with its current attributes.
Definition: TLegend.cxx:423
TLegend
Definition: TLegend.h:23
ROOT::TSeq
A pseudo container class which is a generator of indices.
Definition: TSeq.hxx:66
TNamed::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:53
TH1::GetXaxis
TAxis * GetXaxis()
Get the behaviour adopted by the object about the statoverflows. See EStatOverflows for more informat...
Definition: TH1.h:317
TH1.h
TMath.h
c1
return c1
Definition: legend1.C:41