ROOT  6.06/09
Reference Guide
matrixOperations.C
Go to the documentation of this file.
1 #include "TGraphErrors.h"
2 #include "TMultiGraph.h"
3 #include "TCanvas.h"
4 #include "TLegend.h"
5 #include "TPaveText.h"
6 #include "TFile.h"
7 #include "TProfile.h"
8 #include "TStyle.h"
9 #include "TMath.h"
10 #include "TFrame.h"
11 
12 #include <string>
13 #include <iostream>
14 
15 int fillCol=20;
16 std::string systemName;
17 bool drawSingleGraph = true;
18 
19 int topX=10;
20 int topY=50;
21 
22 const Int_t N=10;
23 
24 
25 void matrixOperations_do(std::string type = "", bool clhep=false, bool drawSingleGraph = false );
26 
27 void matrixOperations(std::string type = "",bool clhep=false, bool drawSingleGraph = false ) {
28 
30 // matrixOperations_do("slc3_ia32_gcc323");
31 // matrixOperations_do("win32_vc71");
32 
33 }
34 
35 
36 void DrawData(char * title, TGraphErrors * h1, TGraphErrors * h2, TGraphErrors * h3 = 0, TGraphErrors * h4 = 0, TGraphErrors * h5 = 0, TGraphErrors * h6 = 0) {
37 
38 
39  gPad->SetFillColor(fillCol);
40  gPad->SetGrid();
41  gPad->SetLogy();
42  gPad->SetLogx();
43  //gStyle->SetFillColor(30);
44 
45  TMultiGraph * mg = new TMultiGraph();
46 
47 h1->SetLineColor(3);
48 h1->SetMarkerColor(3);
49 h1->SetMarkerStyle(20);
50 h1->SetLineWidth(2);
51 mg->Add(h1);
52 
53 h2->SetLineColor(4);
54 h2->SetMarkerColor(4);
55 h2->SetMarkerStyle(21);
56 h2->SetLineWidth(2);
57  mg->Add(h2);
58 
59  if (h3 != 0 ) {
60 h3->SetLineColor(2);
61 h3->SetMarkerColor(2);
62 h3->SetMarkerStyle(22);
63 h3->SetLineWidth(2);
64  mg->Add(h3);
65  }
66 if (h4!= 0) {
67  h4->SetLineColor(5);
68  h4->SetMarkerColor(5);
69  h4->SetMarkerStyle(23);
70  h4->SetLineWidth(2);
71  mg->Add(h4);
72  }
73 if (h5!= 0) {
74  h5->SetLineColor(6);
75  h5->SetMarkerColor(6);
76  h5->SetMarkerStyle(29);
77  h5->SetLineWidth(2);
78  mg->Add(h5);
79  }
80 if (h6!= 0) {
81  h6->SetLineColor(7);
82  h6->SetMarkerColor(7);
83  h6->SetMarkerStyle(3);
84  h6->SetLineWidth(2);
85  mg->Add(h6);
86  }
87 
88 
89 TGraphErrors * hd = h1;
90 // hd->SetLineWidth(2);
91 //hd->GetXaxis()->SetLabelSize(0);
92 //hd->GetXaxis()->SetNdivisions(510);
93  hd->GetYaxis()->SetTitleSize(0.05);
94  hd->GetXaxis()->SetTitle("Matrix size ");
95  hd->GetYaxis()->SetTitle("CPU Time ");
96  hd->GetYaxis()->SetTitleOffset(0.7);
97  hd->SetTitle("");
98 // h1->SetMaximum(20);
99 // h1->SetMinimum(0.0);
100 
101  gStyle->SetOptStat(0);
102 
103 
104  mg->Draw("alp");
105 
106  // ned to do after drawing
107  mg->GetXaxis()->SetLimits(1.8,32);
108  mg->GetXaxis()->SetTitle("Matrix size ");
109  mg->GetXaxis()->SetMoreLogLabels(1);
110  mg->GetXaxis()->SetNoExponent(1);
111  mg->GetYaxis()->SetTitle("CPU Time ");
112  mg->GetYaxis()->SetTitleOffset(1.25);
113 
114  TLegend * tleg = new TLegend(0.78, 0.25, 0.97 ,0.45);
115  tleg->AddEntry(h1, "SMatrix", "p");
116  tleg->AddEntry(h2, "TMatrix", "p");
117  if (h3 != 0) tleg->AddEntry(h3, "SMatrix_sym", "p");
118  if (h4 != 0) tleg->AddEntry(h4, "TMatrix_sym", "p");
119  if (h5 != 0) tleg->AddEntry(h5, "HepMatrix", "p");
120  if (h6 != 0) tleg->AddEntry(h6, "HepMatrix_sym", "p");
121  tleg->Draw();
122 
123  {
124  TPaveText *pt1 = new TPaveText(0.78,0.15,0.97,0.2,"brNDC");
125  pt1->AddText(systemName.c_str());
126  pt1->SetBorderSize(1);
127  pt1->Draw();
128  pt1->SetFillColor(0);
129  }
130 
131  {
132  TPaveText *pt1 = new TPaveText(0.3,0.91,0.7,0.98,"brNDC");
133  pt1->AddText(title);
134  pt1->SetBorderSize(1);
135  pt1->Draw();
136  pt1->SetFillColor(0);
137  }
138 
139 }
140 
141 void GetData(std::string s,double * x, double * y, double * ey) {
142  std::string fileName;
143  if (systemName != "")
144  fileName="testOperations_" + systemName + ".root";
145  else
146  fileName="testOperations.root";
147 
148  TFile * f = new TFile(fileName.c_str());
149  TProfile * h1 = (TProfile * ) f->Get(s.c_str() );
150  if (h1 ==0) {
151  std::cout << "Profile " << s << " not found !!! " << std::endl;
152  return;
153  }
154  for (int i = 0; i < N; ++i) {
155  y[i] = h1->GetBinContent(int(x[i] + 0.1) );
156  ey[i] = h1->GetBinError(int(x[i] + 0.1) );
157  }
158 }
159 
160 
161 
162 void matrixOperations_do(std::string type, bool clhep, bool drawSingleGraph) {
163 
164 
165  systemName = type;
166  std::string cName = "c1_" + type;
167  std::string cTitle = "Matrix operations " + type;
168 
169  TCanvas * c1 = new TCanvas(cName.c_str(),cTitle.c_str(),topX,topY,800*sqrt(2),800);
170  topX+=20;
171  topY+=20;
172 
173  c1->Divide(3,2);
174 
175  const int nb = N;
176  //double x[N] = { 2.,3.,4.,5.,7.,10,15,20,50,75,100};
177  double x[N] = { 2.,3.,4.,5.,6,7.,10,15,20,30};
178  // timings
179  double smat[N]; // = { 1., 2., 3., 4,5,10,100,300,1000 };
180  double tmat[N]; // = { 1.4, 2.4, 3.4, 5,6,20,200,500,2000 };
181  double cmat[N]; // = { 2., 3., 4., 5,8,10,300,800,5000 };
182  double ymat[N]; // = { 2., 3., 4., 5,8,10,300,800,5000 };
183  double wmat[N]; // = { 2., 3., 4., 5,8,10,300,800,5000 };
184  double zmat[N]; // = { 2., 3., 4., 5,8,10,300,800,5000 };
185  double es[N];
186  double et[N];
187  double ec[N];
188  double ey[N];
189  double ew[N];
190  double ez[N];
191 
192 
193  c1->cd(1);
194 
195  GetData("SMatrix_dot",x,smat,es);
196  GetData("TMatrix_dot",x,tmat,et);
197  if (clhep) GetData("HepMatrix_dot",x,cmat,ec);
198  TGraphErrors * g10 = new TGraphErrors(nb,x, smat,0, es);
199  TGraphErrors * g20 = new TGraphErrors(nb,x, tmat,0, et);
200  TGraphErrors * g30 = 0;
201  TGraphErrors * g40 = 0;
202  TGraphErrors * g50 = 0;
203  if (clhep) g50 = new TGraphErrors(nb,x, cmat,0, ec);
204  DrawData("#vec{v} #upoint #vec{w}",g10,g20,g30,g40,g50);
205 
206  c1->cd(2);
207  GetData("SMatrix_M*V+",x,smat,es);
208  GetData("TMatrix_M*V+",x,tmat,et);
209  GetData("SMatrix_sym_M*V+",x,ymat,ey);
210  GetData("TMatrix_sym_M*V+",x,wmat,ew);
211  if (clhep) GetData("HepMatrix_M*V+",x,cmat,ec);
212  if (clhep) GetData("HepMatrix_sym_M*V+",x,zmat,ez);
213  TGraphErrors * g11 = new TGraphErrors(nb,x, smat,0,es);
214  TGraphErrors * g21 = new TGraphErrors(nb,x, tmat,0,et);
215  TGraphErrors * g31 = new TGraphErrors(nb,x, ymat,0,ey);
216  TGraphErrors * g41 = new TGraphErrors(nb,x, wmat,0,ew);
217  TGraphErrors * g51 = 0;
218  TGraphErrors * g61 = 0;
219  if (clhep) g51 = new TGraphErrors(nb,x, cmat,0, ec);
220  if (clhep) g61 = new TGraphErrors(nb,x, zmat,0, ez);
221  DrawData("M #upoint #vec{v} + #vec{w}",g11,g21,g31,g41,g51,g61);
222 
223  c1->cd(3);
224  GetData("SMatrix_prod",x,smat,es);
225  GetData("TMatrix_prod",x,tmat,et);
226  GetData("SMatrix_sym_prod",x,ymat,ey);
227  GetData("TMatrix_sym_prod",x,wmat,ew);
228  if (clhep) {
229  GetData("HepMatrix_M*V+",x,cmat,ec);
230  GetData("HepMatrix_sym_M*V+",x,zmat,ez);
231  }
232  TGraphErrors * g12 = new TGraphErrors(nb,x, smat,0,es);
233  TGraphErrors * g22 = new TGraphErrors(nb,x, tmat,0,et);
234  TGraphErrors * g32 = new TGraphErrors(nb,x, ymat,0,ey);
235  TGraphErrors * g42 = new TGraphErrors(nb,x, wmat,0,ew);
236  TGraphErrors * g52 = 0;
237  TGraphErrors * g62 = 0;
238  if (clhep) g52 = new TGraphErrors(nb,x, cmat,0, ec);
239  if (clhep) g62 = new TGraphErrors(nb,x, zmat,0, ez);
240  DrawData("v^{T} * M * v",g12,g22,g32,g42,g52,g62);
241 
242 
243  c1->cd(4);
244  GetData("SMatrix_M*M",x,smat,es);
245  GetData("TMatrix_M*M",x,tmat,et);
246  GetData("SMatrix_sym_M*M",x,ymat,ey);
247  GetData("TMatrix_sym_M*M",x,wmat,ew);
248  if (clhep) {
249  GetData("HepMatrix_M*M",x,cmat,ec);
250  GetData("HepMatrix_sym_M*M",x,zmat,ez);
251  }
252  TGraphErrors * g14 = new TGraphErrors(nb,x, smat,0,es);
253  TGraphErrors * g24 = new TGraphErrors(nb,x, tmat,0,et);
254  TGraphErrors * g34 = new TGraphErrors(nb,x, ymat,0,ey);
255  TGraphErrors * g44 = new TGraphErrors(nb,x, wmat,0,ew);
256  TGraphErrors * g54 = 0;
257  TGraphErrors * g64 = 0;
258  if (clhep) g54 = new TGraphErrors(nb,x, cmat,0, ec);
259  if (clhep) g64 = new TGraphErrors(nb,x, zmat,0, ez);
260  DrawData("A * B + C",g14,g24,g34,g44,g54,g64);
261 
262  c1->cd(5);
263  GetData("SMatrix_At*M*A",x,smat,es);
264  GetData("TMatrix_At*M*A",x,tmat,et);
265  GetData("SMatrix_sym_At*M*A",x,ymat,ey);
266  GetData("TMatrix_sym_At*M*A",x,wmat,ew);
267  if (clhep) {
268  GetData("HepMatrix_At*M*A",x,cmat,ec);
269  GetData("HepMatrix_sym_At*M*A",x,zmat,ez);
270  }
271  TGraphErrors * g15 = new TGraphErrors(nb,x, smat,0,es);
272  TGraphErrors * g25 = new TGraphErrors(nb,x, tmat,0,et);
273  TGraphErrors * g35 = new TGraphErrors(nb,x, ymat,0,ey);
274  TGraphErrors * g45 = new TGraphErrors(nb,x, wmat,0,ew);
275  TGraphErrors * g55 = 0;
276  TGraphErrors * g65 = 0;
277  if (clhep) g55 = new TGraphErrors(nb,x, cmat,0, ec);
278  if (clhep) g65 = new TGraphErrors(nb,x, zmat,0, ez);
279  DrawData("A * M * A^{T}",g15,g25,g35,g45,g55,g65);
280 
281  c1->cd(6);
282  GetData("SMatrix_inv",x,smat,es);
283  GetData("TMatrix_inv",x,tmat,et);
284  GetData("SMatrix_sym_inv",x,ymat,ey);
285  GetData("TMatrix_sym_inv",x,wmat,ew);
286  if (clhep) {
287  GetData("HepMatrix_inv",x,cmat,ec);
288  GetData("HepMatrix_sym_inv",x,zmat,ez);
289  }
290  TGraphErrors * g16 = new TGraphErrors(nb,x, smat,0,es);
291  TGraphErrors * g26 = new TGraphErrors(nb,x, tmat,0,et);
292  TGraphErrors * g36 = new TGraphErrors(nb,x, ymat,0,ey);
293  TGraphErrors * g46 = new TGraphErrors(nb,x, wmat,0,ew);
294  TGraphErrors * g56 = 0;
295  TGraphErrors * g66 = 0;
296  if (clhep) g56 = new TGraphErrors(nb,x, cmat,0, ec);
297  if (clhep) g66 = new TGraphErrors(nb,x, zmat,0, ez);
298  DrawData("A^{-1}",g16,g26,g36,g46,g56,g66);
299 
300 
301  // TCanvas::Update() draws the frame, after which one can change it
302  c1->Update();
303  c1->SetFillColor(fillCol);
304  c1->GetFrame()->SetFillColor(21);
305  c1->GetFrame()->SetBorderSize(12);
306  c1->Modified();
307 
308  if (drawSingleGraph) {
309  std::string c2Name = "c2_" + type;
310  TCanvas * c2 = new TCanvas(c2Name.c_str(),"Matrix Operations",200,10,700,600);
311  DrawData("A * M * A^{T}",g15,g25,g35,g45,g55,g65);
312  c2->SetRightMargin(0.028);
313  c2->Update();
314  }
315 
316 
317 }
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title Offset is a correction factor with respect to the "s...
Definition: TAttAxis.cxx:244
virtual void SetLineWidth(Width_t lwidth)
Definition: TAttLine.h:57
virtual void Draw(Option_t *option="")
Draw this pavetext with its current attributes.
Definition: TPaveText.cxx:211
This class displays a legend box (TPaveText) containing several legend entries.
Definition: TLegend.h:35
virtual void SetLimits(Double_t xmin, Double_t xmax)
Definition: TAxis.h:154
TCanvas * c1
Definition: legend1.C:2
const Int_t N
R__EXTERN TStyle * gStyle
Definition: TStyle.h:423
virtual void Draw(Option_t *option="")
Draw this legend with its current attributes.
Definition: TLegend.cxx:373
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:45
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:659
A TMultiGraph is a collection of TGraph (or derived) objects.
Definition: TMultiGraph.h:37
virtual TText * AddText(Double_t x1, Double_t y1, const char *label)
Add a new Text line to this pavetext at given coordinates.
Definition: TPaveText.cxx:160
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
bool drawSingleGraph
int Int_t
Definition: RtypesCore.h:41
virtual void SetTitle(const char *title="")
Set graph title.
Definition: TGraph.cxx:2153
Profile Historam.
Definition: TProfile.h:34
double sqrt(double)
Double_t x[n]
Definition: legend1.C:17
TFrame * GetFrame()
Get frame.
Definition: TPad.cxx:2733
virtual void SetMarkerColor(Color_t mcolor=1)
Definition: TAttMarker.h:51
virtual void Draw(Option_t *chopt="")
Draw this multigraph with its current attributes.
TH1F * h1
Definition: legend1.C:5
TAxis * GetXaxis() const
Get x axis of the graph.
std::string systemName
virtual void SetBorderSize(Short_t bordersize)
Definition: TWbox.h:63
virtual void SetLineColor(Color_t lcolor)
Definition: TAttLine.h:54
TAxis * GetXaxis() const
Get x axis of the graph.
Definition: TGraph.cxx:1563
virtual void SetFillColor(Color_t fcolor)
Definition: TAttFill.h:50
virtual void SetMarkerStyle(Style_t mstyle=1)
Definition: TAttMarker.h:53
int topY
virtual void SetTitleSize(Float_t size=0.04)
Set size of axis title The size is expressed in per cent of the pad width.
Definition: TAttAxis.cxx:254
The Canvas class.
Definition: TCanvas.h:48
void SetNoExponent(Bool_t noExponent=kTRUE)
Set the NoExponent flag By default, an exponent of the form 10^N is used when the label value are eit...
Definition: TAxis.h:223
return c2
Definition: legend2.C:14
A Pave (see TPave) with text, lines or/and boxes inside.
Definition: TPaveText.h:35
double f(double x)
void DrawData(char *title, TGraphErrors *h1, TGraphErrors *h2, TGraphErrors *h3=0, TGraphErrors *h4=0, TGraphErrors *h5=0, TGraphErrors *h6=0)
TLegendEntry * AddEntry(const TObject *obj, const char *label="", Option_t *option="lpf")
Add a new entry to this legend.
Definition: TLegend.cxx:280
int type
Definition: TGX11.cxx:120
Double_t y[n]
Definition: legend1.C:17
int topX
Double_t ey[n]
Definition: legend1.C:17
void matrixOperations_do(std::string type="", bool clhep=false, bool drawSingleGraph=false)
virtual void SetRightMargin(Float_t rightmargin)
Set Pad right margin in fraction of the pad width.
Definition: TAttPad.cxx:117
TAxis * GetYaxis() const
Get y axis of the graph.
Definition: TGraph.cxx:1573
void matrixOperations(std::string type="", bool clhep=false, bool drawSingleGraph=false)
virtual void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0)
Automatic pad generation by division.
Definition: TPad.cxx:1077
void SetMoreLogLabels(Bool_t more=kTRUE)
Set the kMoreLogLabels bit flag When this option is selected more labels are drawn when in log scale ...
Definition: TAxis.h:213
A TGraphErrors is a TGraph with error bars.
Definition: TGraphErrors.h:28
void SetOptStat(Int_t stat=1)
The type of information printed in the histogram statistics box can be selected via the parameter mod...
Definition: TStyle.cxx:1252
#define gPad
Definition: TVirtualPad.h:288
int fillCol
virtual void Add(TGraph *graph, Option_t *chopt="")
Add a new graph to the list of graphs.
virtual void Update()
Update canvas pad buffers.
Definition: TCanvas.cxx:2179
void GetData(std::string s, double *x, double *y, double *ey)
virtual void SetTitle(const char *title="")
Change (i.e. set) the title of the TNamed.
Definition: TNamed.cxx:152
TAxis * GetYaxis() const
Get y axis of the graph.
void Modified(Bool_t flag=1)
Definition: TPad.h:407
virtual void SetBorderSize(Int_t bordersize=4)
Definition: TPave.h:82