Logo ROOT   6.08/07
Reference Guide
double32.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_io
3 /// \notebook -js
4 /// Tutorial illustrating use and precision of the Double32_t data type
5 /// You must run this tutorial with ACLIC: a dictionary will be automatically
6 /// created.
7 /// ~~~{.bash}
8 /// root > .x double32.C+
9 /// ~~~
10 /// The following cases are supported for streaming a Double32_t type
11 /// depending on the range declaration in the comment field of the data member:
12 ///
13 /// Case | Declaration
14 /// -----|------------
15 /// A | Double32_t fNormal;
16 /// B | Double32_t fTemperature; //[0,100]
17 /// C | Double32_t fCharge; //[-1,1,2]
18 /// D | Double32_t fVertex[3]; //[-30,30,10]
19 /// E | Double32_t fChi2; //[0,0,6]
20 /// F | Int_t fNsp;<br>Double32_t* fPointValue; //[fNsp][0,3]
21 ///
22 /// * Case A fNormal is converted from a Double_t to a Float_t
23 /// * Case B fTemperature is converted to a 32 bit unsigned integer
24 /// * Case C fCharge is converted to a 2 bits unsigned integer
25 /// * Case D the array elements of fVertex are converted to an unsigned 10 bits integer
26 /// * Case E fChi2 is converted to a Float_t with truncated precision at 6 bits
27 /// * 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.
28 ///
29 /// Case B has more precision than case A: 9 to 10 significative digits and 6 to 7 digits respectively.
30 /// The range specifier has the general format: [xmin,xmax] or [xmin,xmax,nbits]. Examples
31 /// * [0,1]
32 /// * [-10,100];
33 /// * [-pi,pi], [-pi/2,pi/4],[-2pi,2*pi]
34 /// * [-10,100,16]
35 /// * [0,0,8]
36 /// Note that:
37 /// * If nbits is not specified, or nbits <2 or nbits>32 it is set to 32
38 /// * 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.
39 ///
40 /// ## IMPORTANT NOTE
41 /// Lets assume an original variable double x.
42 /// When using the format [0,0,8] (i.e. range not specified) you get the best
43 /// relative precision when storing and reading back the truncated x, say xt.
44 /// The variance of (x-xt)/x will be better than when specifying a range
45 /// for the same number of bits. However the precision relative to the
46 /// range (x-xt)/(xmax-xmin) will be worst, and vice-versa.
47 /// The format [0,0,8] is also interesting when the range of x is infinite
48 /// or unknown.
49 ///
50 /// \macro_image
51 /// \macro_code
52 ///
53 /// \author Rene Brun
54 
55 #include "TFile.h"
56 #include "TCanvas.h"
57 #include "TTree.h"
58 #include "TH1.h"
59 #include "TMath.h"
60 #include "TRandom3.h"
61 #include "TGraph.h"
62 #include "TLegend.h"
63 #include "TFrame.h"
64 #include "TPaveLabel.h"
65 
66 class DemoDouble32 {
67 private:
68  Double_t fD64; //reference member with full double precision
69  Double32_t fF32; //saved as a 32 bit Float_t
70  Double32_t fI32; //[-pi,pi] saved as a 32 bit unsigned int
71  Double32_t fI30; //[-pi,pi,30] saved as a 30 bit unsigned int
72  Double32_t fI28; //[-pi,pi,28] saved as a 28 bit unsigned int
73  Double32_t fI26; //[-pi,pi,26] saved as a 26 bit unsigned int
74  Double32_t fI24; //[-pi,pi,24] saved as a 24 bit unsigned int
75  Double32_t fI22; //[-pi,pi,22] saved as a 22 bit unsigned int
76  Double32_t fI20; //[-pi,pi,20] saved as a 20 bit unsigned int
77  Double32_t fI18; //[-pi,pi,18] saved as a 18 bit unsigned int
78  Double32_t fI16; //[-pi,pi,16] saved as a 16 bit unsigned int
79  Double32_t fI14; //[-pi,pi,14] saved as a 14 bit unsigned int
80  Double32_t fI12; //[-pi,pi,12] saved as a 12 bit unsigned int
81  Double32_t fI10; //[-pi,pi,10] saved as a 10 bit unsigned int
82  Double32_t fI8; //[-pi,pi, 8] saved as a 8 bit unsigned int
83  Double32_t fI6; //[-pi,pi, 6] saved as a 6 bit unsigned int
84  Double32_t fI4; //[-pi,pi, 4] saved as a 4 bit unsigned int
85  Double32_t fI2; //[-pi,pi, 2] saved as a 2 bit unsigned int
86  Double32_t fR14; //[0, 0, 14] saved as a 32 bit float with a 14 bits mantissa
87  Double32_t fR12; //[0, 0, 12] saved as a 32 bit float with a 12 bits mantissa
88  Double32_t fR10; //[0, 0, 10] saved as a 32 bit float with a 10 bits mantissa
89  Double32_t fR8; //[0, 0, 8] saved as a 32 bit float with a 8 bits mantissa
90  Double32_t fR6; //[0, 0, 6] saved as a 32 bit float with a 6 bits mantissa
91  Double32_t fR4; //[0, 0, 4] saved as a 32 bit float with a 4 bits mantissa
92  Double32_t fR2; //[0, 0, 2] saved as a 32 bit float with a 2 bits mantissa
93 
94 public:
95  DemoDouble32() {;}
96  void Set(Double_t ref);
97 };
98 
99 void DemoDouble32::Set(Double_t ref) {
100  fD64 = fF32 = fI32 = fI30 = fI28 = fI26 = fI24 = fI22 = fI20 = ref;
101  fI18 = fI16 = fI14 = fI12 = fI10 = fI8 = fI6 = fI4 = fI2 = ref;
102  fR14 = fR12 = fR10 = fR8 = fR6 = fR4 = fR2 = ref;
103 }
104 
105 void double32() {
106  // show the use and precision of the Double32_t data type
107 
108  DemoDouble32 *d = new DemoDouble32();
109 
110  //create a Tree with 40000 objects DemoDouble32
111  TFile::Open("DemoDouble32.root","recreate");
112  TTree *T = new TTree("T","DemoDouble32");
113  TBranch *bd = T->Branch("d","DemoDouble32",&d,4000);
114  TRandom3 r;
115  Double_t xmax = TMath::Pi();
116  Double_t xmin = -xmax;
117  Int_t i, n = 40000;
118  for (i=0;i<n;i++) {
119  d->Set(r.Uniform(xmin,xmax));
120  T->Fill();
121  }
122  T->Write();
123 
124  //Create the frame histogram and the graphs
126  Int_t nb = branches->GetEntries();
127  TBranch *br = (TBranch*)branches->At(0);
128  Long64_t zip64 = br->GetZipBytes();
129  Double_t cx = 1;
130  Double_t drange = 15;
131  Double_t dval = 15;
132  TCanvas *c1 = new TCanvas("c1","c1",800,600);
133  c1->SetGrid();
134  c1->SetHighLightColor(0);
135  c1->SetFillColor(17);
136  c1->SetFrameFillColor(20);
137  c1->SetFrameBorderSize(10);
138  TH1F *h = new TH1F("h","",nb,0,nb);
139  h->SetMaximum(16);
140  h->SetStats(0);
141  h->Draw();
142  c1->GetFrame()->SetFillColor(21);
143  c1->GetFrame()->SetBorderSize(12);
144  TGraph *gcx = new TGraph(nb); gcx->SetName("gcx");
145  gcx->SetMarkerStyle(21);
146  gcx->SetMarkerColor(kBlue);
147  TGraph *gdrange = new TGraph(nb); gdrange->SetName("gdrange");
148  gdrange->SetMarkerStyle(20);
149  gdrange->SetMarkerColor(kRed);
150  TGraph *gdval = new TGraph(nb); gdval->SetName("gdval");
151  gdval->SetMarkerStyle(20);
152  gdval->SetMarkerColor(kBlack);
153  TPaveLabel *title = new TPaveLabel(.15,.92,.85,.97,"Double32_t compression and precision","brNDC");
154  title->Draw();
155 
156  //loop on branches to get the precision and compression factors
157  for (i=0;i<nb;i++) {
158  br = (TBranch*)branches->At(i);
159  h->GetXaxis()->SetBinLabel(i+1,br->GetName());
160  cx = Double_t(zip64)/Double_t(br->GetZipBytes());
161  gcx->SetPoint(i,i+0.5,cx);
162  if (i > 0) {
163  T->Draw(Form("(fD64-%s)/(%g)",br->GetName(),xmax-xmin),"","goff");
164  Double_t rms = TMath::RMS(n,T->GetV1());
165  drange = TMath::Max(0.,-TMath::Log10(rms));
166  }
167  gdrange->SetPoint(i,i+0.5,drange);
168  if (i > 0) {
169  T->Draw(Form("(fD64-%s)/(fD64+0.01)",br->GetName()),"","goff");
170  Double_t rms = TMath::RMS(n,T->GetV1());
171  dval = TMath::Max(0.,-TMath::Log10(rms));
172  }
173  gdval->SetPoint(i,i+0.5,dval);
174  }
175  gcx->Draw("lp");
176  gdrange->Draw("lp");
177  gdval->Draw("lp");
178  TLegend *legend = new TLegend(0.2,0.7,0.7,0.85);
179  legend->SetTextFont(72);
180  legend->SetTextSize(0.04);
181  legend->AddEntry(gcx,"Compression factor","lp");
182  legend->AddEntry(gdrange,"Log of precision wrt range","lp");
183  legend->AddEntry(gdval,"Log of precision wrt value","lp");
184  legend->Draw();
185  TPaveLabel *rang = new TPaveLabel(.75,.75,.88,.80,"[-pi,pi]","brNDC");
186  rang->Draw();
187 }
188 
189 
190 
191 
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
An array of TObjects.
Definition: TObjArray.h:39
float xmin
Definition: THbookFile.cxx:93
Random number generator class based on M.
Definition: TRandom3.h:29
long long Long64_t
Definition: RtypesCore.h:69
virtual void SetMaximum(Double_t maximum=-1111)
Definition: TH1.h:399
This class displays a legend box (TPaveText) containing several legend entries.
Definition: TLegend.h:27
return c1
Definition: legend1.C:41
Definition: Rtypes.h:61
double T(double x)
Definition: ChebyshevPol.h:34
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:1647
virtual Int_t Fill()
Fill all branches.
Definition: TTree.cxx:4375
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition: TNamed.cxx:131
virtual void Draw(Option_t *option="")
Draw this legend with its current attributes.
Definition: TLegend.cxx:373
THist< 1, float, THistStatContent, THistStatUncertainty > TH1F
Definition: THist.hxx:302
Definition: Rtypes.h:60
TH1 * h
Definition: legend2.C:5
tomato 1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:575
int Int_t
Definition: RtypesCore.h:41
TObject * At(Int_t idx) const
Definition: TObjArray.h:167
virtual void Draw(Option_t *chopt="")
Draw this graph with its current attributes.
Definition: TGraph.cxx:747
TLegend * legend
Definition: pirndm.C:35
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=1, Int_t netopt=0)
Create / open a file.
Definition: TFile.cxx:3907
Double_t RMS(Long64_t n, const T *a, const Double_t *w=0)
Definition: TMath.h:903
virtual void SetTextFont(Font_t tfont=62)
Set the text font.
Definition: TAttText.h:51
TFrame * GetFrame()
Get frame.
Definition: TPad.cxx:2746
virtual void SetGrid(Int_t valuex=1, Int_t valuey=1)
Definition: TPad.h:318
Double_t Log10(Double_t x)
Definition: TMath.h:529
virtual void SetMarkerColor(Color_t mcolor=1)
Set the marker color.
Definition: TAttMarker.h:43
TObjArray * GetListOfBranches()
Definition: TBranch.h:178
double Double32_t
Definition: RtypesCore.h:56
virtual void SetBorderSize(Short_t bordersize)
Definition: TWbox.h:54
A Pave (see TPave) with a text centered in the Pave.
Definition: TPaveLabel.h:24
virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0)
Write this object to the current directory.
Definition: TTree.cxx:9042
TRandom2 r(17)
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2851
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition: TAttFill.h:42
char * Form(const char *fmt,...)
virtual void SetMarkerStyle(Style_t mstyle=1)
Set the marker style.
Definition: TAttMarker.h:45
float xmax
Definition: THbookFile.cxx:93
Double_t Pi()
Definition: TMath.h:44
The Canvas class.
Definition: TCanvas.h:41
virtual void Draw(Option_t *option="")
Draw this pavelabel with its current attributes.
Definition: TPaveLabel.cxx:77
double Double_t
Definition: RtypesCore.h:55
TLegendEntry * AddEntry(const TObject *obj, const char *label="", Option_t *option="lpf")
Add a new entry to this legend.
Definition: TLegend.cxx:280
virtual void Draw(Option_t *opt)
Default Draw method for all objects.
Definition: TTree.h:367
virtual void SetBinLabel(Int_t bin, const char *label)
Set label for bin.
Definition: TAxis.cxx:808
virtual Double_t Uniform(Double_t x1=1)
Returns a uniform deviate on the interval (0, x1).
Definition: TRandom.cxx:606
virtual Int_t Branch(TCollection *list, Int_t bufsize=32000, Int_t splitlevel=99, const char *name="")
Create one branch for each element in the collection.
Definition: TTree.cxx:1652
virtual void SetPoint(Int_t i, Double_t x, Double_t y)
Set x and y values for point number i.
Definition: TGraph.cxx:2150
void SetFrameBorderSize(Width_t size=1)
Definition: TAttPad.h:80
Short_t Max(Short_t a, Short_t b)
Definition: TMathBase.h:202
A Graph is a graphics object made of two arrays X and Y with npoints each.
Definition: TGraph.h:53
Int_t GetEntries() const
Return the number of objects in array (i.e.
Definition: TObjArray.cxx:494
A TTree object has a header with a name and a title.
Definition: TTree.h:98
Definition: Rtypes.h:61
void SetFrameFillColor(Color_t color=1)
Definition: TAttPad.h:75
virtual void SetTextSize(Float_t tsize=1)
Set the text size.
Definition: TAttText.h:52
A TTree is a list of TBranches.
Definition: TBranch.h:58
const Int_t n
Definition: legend1.C:16
virtual Double_t * GetV1()
Definition: TTree.h:462
virtual void SetStats(Bool_t stats=kTRUE)
Set statistics option on/off.
Definition: TH1.cxx:8101
TAxis * GetXaxis()
Definition: TH1.h:324
void SetHighLightColor(Color_t col)
Definition: TCanvas.h:216