TkN 2.1
Toolkit for Nuclei
tknuclear_chart.cpp
1#include "tknuclear_chart.h"
2#include <iostream>
3
4#include "TLine.h"
5#include "TLatex.h"
6#include "TGraph.h"
7#include "TStyle.h"
8
9namespace tkn {
17}
18
19using namespace tkn;
20
21tknuclear_chart::tknuclear_chart(const char *_title, int type, bool _autofit):
22 mgrn(Form("%s_N",_title),""),
23 mgrz(Form("%s_Z",_title),""),
24 chart_title(_title),
25 autofit(_autofit)
26{
27 cc = nullptr;
28 ndivx = ndivy = 10;
29 ndivz = 6;
30
31 hzn.SetNameTitle(Form("%s_ZN",chart_title.Data()), chart_title.Data());
32 hz.SetNameTitle(Form("%s_Z",chart_title.Data()), chart_title.Data());
33 hn.SetNameTitle(Form("%s_N",chart_title.Data()), chart_title.Data());
34
35 switch(type){
36 case kAll:
37 hzn.SetBins(201,-.5,200.5,121,-.5,120.5);
38 hz.SetBins(100,0,1,121,-.5,120.5);
39 hn.SetBins(201,-.5,200.5,100,0,1);
40 break;
41 case kEven :
42 hzn.SetBins(80,1,161,55,1,111);
43 hz.SetBins(100,0,1,55,1,111);
44 hn.SetBins(80,1,161,100,0,1);
45 break;
46 case kOdd :
47 hzn.SetBins(80,0,160,55,0,110);
48 hz.SetBins(100,0,1,55,0,110);
49 hn.SetBins(80,0,160,100,0,1);
50 break;
51 }
52}
53
54tknuclear_chart::tknuclear_chart(const char *_title, int type, double _min_val_histo, bool _autofit): tknuclear_chart(_title, type, _autofit)
55{
56 min_val_histo = _min_val_histo;
57 hzn.SetMinimum(min_val_histo);
58 fset_min_val = true;
59}
60
61void tknuclear_chart::fill(int _zz, int _nn, double _val)
62{
63 if(std::isnan(_val)) return;
64 hzn.Fill(_nn,_zz,_val);
65}
66
67double tknuclear_chart::get_value(int _zz, int _nn)
68{
69 return hzn.GetBinContent(_nn,_zz);
70}
71
72void tknuclear_chart::set_value(int _zz, int _nn, double _val)
73{
74 if(std::isnan(_val)) return;
75 if(_val==0.)_val=-9999999;
76 hzn.SetBinContent(hzn.FindBin(_nn,_zz),_val);
77}
78
79void tknuclear_chart::calculate_projections()
80{
81 if(mgrz.GetListOfGraphs()) mgrz.GetListOfGraphs()->Clear();
82 if(mgrn.GetListOfGraphs()) mgrn.GetListOfGraphs()->Clear();
83
84 min_val = 9999999.;
85 max_val = -9999999.;
86 min_z = 9999999.;
87 min_n = 9999999.;
88 max_z = -9999999.;
89 max_n = -9999999.;
90
91 for(int i=1 ; i<=hzn.GetNbinsX() ; i++) {
92 for(int j=1 ; j<=hzn.GetNbinsY() ; j++) {
93 int _nn = hzn.GetXaxis()->GetBinCenter(i);
94 int _zz = hzn.GetYaxis()->GetBinCenter(j);
95 double _val = hzn.GetBinContent(i,j);
96 if(_val==0 || _val==-9999999.) continue;
97
98 if(_val>max_val) max_val = _val;
99 if(_val<min_val) min_val = _val;
100
101 if(_zz>max_z) max_z = _zz;
102 if(_zz<min_z) min_z = _zz;
103
104 if(_nn>max_n) max_n = _nn;
105 if(_nn<min_n) min_n = _nn;
106
107 TGraph* gg = nullptr;
108 if(!mgrz.GetListOfGraphs()||!(gg = (TGraph*)mgrz.GetListOfGraphs()->FindObject(Form("%s_N%d",chart_title.Data(),_nn)))){
109 gg = new TGraph;
110 gg->SetNameTitle(Form("%s_N%d",chart_title.Data(),_nn),Form("%d",_nn));
111 gg->SetMarkerStyle(7);
112 gg->SetMarkerColor((_nn%2)?odd_col:even_col);
113 gg->SetLineColor((_nn%2)?odd_col:even_col);
114 mgrz.Add(gg,"lp");
115 }
116 gg->SetPoint(gg->GetN(),_val,_zz);
117
118 gg = nullptr;
119 if(!mgrn.GetListOfGraphs()||!(gg = (TGraph*)mgrn.GetListOfGraphs()->FindObject(Form("%s_Z%d",chart_title.Data(),_zz)))){
120 gg = new TGraph;
121 gg->SetNameTitle(Form("%s_Z%d",chart_title.Data(),_zz),Form("%d",_nn));
122 gg->SetMarkerStyle(7);
123 gg->SetMarkerColor((_zz%2)?odd_col:even_col);
124 gg->SetLineColor((_zz%2)?odd_col:even_col);
125 mgrn.Add(gg,"lp");
126 }
127 gg->SetPoint(gg->GetN(),_nn,_val);
128 }
129 }
130 if(hzn.GetEntries()==0) {
131 min_val=0;max_val=1.;
132 }
133
134// if(min_val==(-9999999)) min_val=1;
135}
136
138{
139 hzn.Reset();
140}
141
142void tknuclear_chart::draw(bool _update_only)
143{
144 gStyle->SetOptTitle(0);
145 gStyle->SetOptStat(0);
146
147 set_editables(true);
148
149 calculate_projections();
150 if(!_update_only) {
151 draw_canvas();
152
153 if(autofit && hzn.GetEntries()){
154 hzn.GetXaxis()->SetRangeUser(min_n-2,max_n+2);
155 hzn.GetYaxis()->SetRangeUser(min_z-2,max_z+2);
156
157 hn.GetXaxis()->SetRangeUser(min_n-2,max_n+2);
158 hz.GetYaxis()->SetRangeUser(min_z-2,max_z+2);
159 }
160 }
161
162 p1->cd();
163 hzn.Draw("col");
164 set_axis_att(hzn.GetXaxis());
165 set_axis_att(hzn.GetYaxis());
166
167 draw_lines(2);
168 draw_lines(8);
169 draw_lines(20);
170 draw_lines(28);
171 draw_lines(50);
172 draw_lines(82);
173 draw_lines(126);
174 hzn.Draw("col,same");
175 if(!fset_min_val) hzn.GetZaxis()->SetRangeUser(min_val,max_val);
176 else hzn.GetZaxis()->SetRangeUser(min_val_histo,max_val);
177
178 p2->cd();
179 hz.GetXaxis()->SetLimits(min_val*0.9,max_val*1.1);
180
181 hz.Draw();
182 TAxis* ax = hz.GetXaxis();
183 double min = ax->GetXmin();
184 double max = ax->GetXmax();
185
186 draw_line(2, min, max);
187 draw_line(8, min, max);
188 draw_line(20, min, max);
189 draw_line(28, min, max);
190 draw_line(50, min, max);
191 draw_line(82, min, max);
192
193 mgrz.Draw("pl");
194
195 ax = hz.GetYaxis();
196 ax->SetTitle("proton number");
197 ax->SetTitleOffset(1.2);
198 set_axis_att(ax);
199 ax->SetNdivisions(ndivy);
200
201 ax = hz.GetXaxis();
202 set_axis_att(ax);
203
204 p3->cd();
205 hn.GetYaxis()->SetLimits(min_val*0.9,max_val*1.1);
206 hn.Draw();
207
208 ax = hn.GetYaxis();
209 min = ax->GetXmin();
210 max = ax->GetXmax();
211
212 draw_line(2, min, max,0);
213 draw_line(8, min, max,0);
214 draw_line(20, min, max,0);
215 draw_line(28, min, max,0);
216 draw_line(50, min, max,0);
217 draw_line(82, min, max,0);
218 draw_line(126, min, max,0);
219
220 mgrn.Draw("pl");
221
222 ax = hn.GetXaxis();
223 ax->SetTitle("neutron number");
224 ax->SetTitleOffset(.9);
225 set_axis_att(ax);
226 ax->SetNdivisions(ndivx);
227
228 ax = hn.GetYaxis();
229 set_axis_att(ax);
230
231 p4->cd();
232 draw_axes();
233
234 delete ftitletex;
235 ftitletex = new TLatex(0.625,0.98,chart_title);
236 ftitletex->SetTextAlign(22);
237 ftitletex->SetTextSize(.03);
238 ftitletex->SetTextFont(42);
239 ftitletex->Draw();
240
241 if(cc&&p1&&p2&&p3){}
242 else return;
243
244 p1->Modified(); p1->Update();
245 p2->Modified(); p2->Update();
246 p3->Modified(); p3->Update();
247 cc->Modified();
248 cc->Update();
249
250 set_editables(false);
251}
252
254{
255 if(cc&&p1&&p2&&p3){}
256 else return;
257
258 p1->SetLogz(_log);
259 p2->SetLogx(_log);
260 p3->SetLogy(_log);
261}
262
263
264void tknuclear_chart::draw_lines(int nz)
265{
266 double xxmin = hzn.GetXaxis()->GetBinLowEdge(hzn.GetXaxis()->GetFirst());
267 double xxmax = hzn.GetXaxis()->GetBinUpEdge(hzn.GetXaxis()->GetLast());
268 double yymin = hzn.GetYaxis()->GetBinLowEdge(hzn.GetYaxis()->GetFirst());
269 double yymax = hzn.GetYaxis()->GetBinUpEdge(hzn.GetYaxis()->GetLast());
270
271 if(nz>xxmax||nz<xxmin) {}
272 else{
273 TLine *ll = new TLine(nz,yymin,nz,yymax);
274 ll->SetLineColor(kGray);
275 ll->SetLineWidth(kLineWidth);
276 ll->Draw();}
277
278 if(nz>yymax||nz<yymin) {}
279 else if(nz<120) {
280 TLine *ll = new TLine(xxmin,nz,xxmax,nz);
281 ll->SetLineColor(kGray);
282 ll->SetLineWidth(kLineWidth);
283 ll->Draw();
284 }
285}
286
287void tknuclear_chart::draw_line(int nz, double min, double max, bool onx)
288{
289
290 TLine* ll = 0;
291 if(onx) {
292 double xxmin = hz.GetYaxis()->GetBinLowEdge(hzn.GetYaxis()->GetFirst());
293 double xxmax = hz.GetYaxis()->GetBinUpEdge(hzn.GetYaxis()->GetLast());
294 if(nz>xxmax||nz<xxmin) return;
295
296 ll = new TLine(min,nz,max,nz);
297 ll->SetLineColor(kGray);
298 ll->SetLineWidth(kLineWidth);
299 ll->Draw();
300 }
301 else {
302 double xxmin = hn.GetXaxis()->GetBinLowEdge(hzn.GetXaxis()->GetFirst());
303 double xxmax = hn.GetXaxis()->GetBinUpEdge(hzn.GetXaxis()->GetLast());
304 if(nz>xxmax||nz<xxmin) return;
305
306 ll = new TLine(nz,min,nz,max);
307 ll->SetLineColor(kGray);
308 ll->SetLineWidth(kLineWidth);
309 ll->Draw();
310 }
311
312}
313
314void tknuclear_chart::set_size(int _width, int _height)
315{
316 width = _width;
317 height = _height;
318 xratio = ((double)height)/((double)width);
319 xoffset = 0.7 - (1-(1.-0.7-.08)*xratio-.08);
320}
321
322
323void tknuclear_chart::draw_canvas()
324{
325 if(cc&&p1&&p2&&p3){return;}
326
327 cc = new TCanvas(Form("canvas_%s",chart_title.Data()),chart_title.Data(),width,height);
328 cc->cd();
329
330 p1 = new TPad(Form("chartzn_%s",chart_title.Data()),"chartzn",0,0,1,1);
331 p1->SetFillStyle(0);
332 p1->SetTickx();
333 p1->SetTicky();
334 p1->SetTopMargin(0.05);
335 p1->SetRightMargin(0.05);
336 p1->SetBottomMargin(0.3);
337 p1->SetLeftMargin(0.3+xoffset);
338 p1->SetBit(TObject::kCannotPick);
339 p1->Draw();
340
341 p2 = new TPad(Form("chartz_%s",chart_title.Data()),"chartz",0.,0,1,1);
342 p2->SetFillStyle(0);
343 p2->SetTickx();
344 p2->SetTicky();
345 p2->SetTopMargin(0.05);
346 p2->SetBottomMargin(0.3);
347 p2->SetRightMargin(0.7-xoffset);
348 p2->SetLeftMargin(.08);
349 p2->SetBit(TObject::kCannotPick);
350 p2->Draw();
351
352 p3 = new TPad(Form("chartn_%s",chart_title.Data()),"chartn",0.,0,1,1);
353 p3->SetFillStyle(0);
354 p3->SetTickx();
355 p3->SetTicky();
356 p3->SetTopMargin(0.7);
357 p3->SetLeftMargin(0.3+xoffset);
358 p3->SetRightMargin(.05);
359 p3->SetBottomMargin(.08);
360 p3->SetBit(TObject::kCannotPick);
361 p3->Draw();
362
363 p4 = new TPad(Form("control_%s",chart_title.Data()),"chartn",0.,0,1,1);
364 p4->SetFillStyle(0);
365 p4->SetBit(TObject::kCannotPick);
366 p4->Draw();
367}
368
369void tknuclear_chart::draw_axes()
370{
371 delete axX;delete axY;delete axZn;delete axZz;
372 axX=nullptr;axY=nullptr;axZn=nullptr;axZz=nullptr;
373 if(hzn.GetEntries()==0) return;
374
375 // X-axes
376 axX = new TGaxis(.3+xoffset,0.08,.95,0.08,
377 hn.GetXaxis()->GetBinLowEdge(hn.GetXaxis()->GetFirst()),
378 hn.GetXaxis()->GetBinUpEdge(hn.GetXaxis()->GetLast()),
379 ndivx,"S");
380 axX->SetTitle("neutron number");
381 axX->SetName("axis1");
382 style_axis(axX);
383 axX->SetTitleOffset(.9);
384 axX->Draw();
385
386 axY = new TGaxis(.08,0.3,.08,0.95,
387 hz.GetYaxis()->GetBinLowEdge(hz.GetYaxis()->GetFirst()),
388 hz.GetYaxis()->GetBinUpEdge(hz.GetYaxis()->GetLast()),
389 ndivy,"S");
390 axY->SetTitle("proton number");
391 axY->SetName("axis2");
392 style_axis(axY);
393 axY->SetTitleOffset(1.2*xratio);
394 axY->Draw();
395
396 // Y-axes
397 axZn = new TGaxis(.95,0.08,.95,0.3,
398 hn.GetYaxis()->GetBinLowEdge(hn.GetYaxis()->GetFirst()),
399 hn.GetYaxis()->GetBinUpEdge(hn.GetYaxis()->GetLast()),
400 ndivz,"+L");
401 axZn->SetName("axis3");
402 style_axis(axZn);
403 axZn->Draw();
404
405 axZz = new TGaxis(.08,0.95,.3+xoffset,0.95,
406 hz.GetXaxis()->GetBinLowEdge(hz.GetXaxis()->GetFirst()),
407 hz.GetXaxis()->GetBinUpEdge(hz.GetXaxis()->GetLast()),
408 ndivz,"-");
409 axZz->SetName("axis4");
410 axZz->SetLabelOffset(0.01/xratio);
411 style_axis(axZz);
412 axZz->Draw();
413}
414
415void tknuclear_chart::style_axis(TGaxis* _ax)
416{
417 _ax->SetTitleSize(.03);
418 _ax->SetLabelSize(.025);
419 _ax->SetTickLength(0);
420 _ax->SetTickSize(0);
421 _ax->SetTextFont(42);
422 _ax->SetLabelFont(42);
423
424}
425
426void tknuclear_chart::set_axis_att(TAxis *_ax, bool _show_labels)
427{
428 _ax->SetNdivisions(4);
429 _ax->SetTitleSize(.03);
430 _ax->SetLabelSize(.025);
431
432 _ax->SetAxisColor(kWhite);
433
434 if(!_show_labels){ _ax->SetLabelColor(kWhite); _ax->SetLabelSize(0); _ax->SetTitle("");}
435}
436
438{
439 if(cc) cc->SetEditable(_ed);
440 if(p1) p1->SetEditable(_ed);
441 if(p2) p2->SetEditable(_ed);
442 if(p3) p3->SetEditable(_ed);
443 if(p4) p4->SetEditable(_ed);
444}
445
447{
448 ndivx = _ndiv;
449 if(cc) {
450 axX->SetNdivisions(ndivx);
451 p3->Modified(); p3->Update();
452 }
453}
454
456{
457 ndivy = _ndiv;
458 if(cc) {
459 axY->SetNdivisions(ndivy);
460 p2->Modified(); p2->Update();
461 }
462
463}
464
466{
467 ndivz = _ndiv;
468 if(cc) {
469 axZn->SetNdivisions(ndivz);
470 axZz->SetNdivisions(ndivz);
471 p2->Modified(); p2->Update();
472 p3->Modified(); p3->Update();
473 }
474}
475
476void tknuclear_chart::save_as(const char *file_name)
477{
478 if(cc) cc->SaveAs(file_name);
479}
480
481
483{
484 for(int i=0 ; i<=hzn.GetNbinsX() ; i++) {
485 for(int j=0 ; j<=hzn.GetNbinsY() ; j++) {
486 if(hzn.GetBinContent(i,j) == -9999999.) {
487 hzn.SetBinContent(i,j,0);
488 }
489 else if(hzn.GetBinContent(i,j) == 0) {
490 hzn.SetBinContent(i,j,-9999999.);
491 }
492 }
493 }
494 update();
495}
496
497
498
499ClassImp(tknuclear_chart)
500
501
nuclear chart plot with ROOT
tknuclear_chart(const char *title, int type=kAll, bool autofit=false)
default constructor
void fill(int _zz, int _nn, double _val)
fill the bin for a given nucleus (increment by _val)
void set_ndivisions_y(int _ndiv)
define the number of tics on the y axis
void set_ndivisions_x(int _ndiv)
define the number of tics on the x axis
void draw(bool _update_only=false)
draw the tknucleus chart
void save_as(const char *file_name)
save the nuclear chart in the file file_name
void set_ndivisions_z(int _ndiv)
define the number of tics on the z axis
void set_value(int _zz, int _nn, double _val)
set the bin value for a given nucleus
void set_size(int _width, int _height)
set the canvas size in pixels (default 900x900)
void update()
update the tknucleus chart
double get_value(int _zz, int _nn)
get the bin content for a given nucleus
void reset()
reset the tknuclear chart
void set_log_scale(bool _log=true)
print the tknuclear chart in log scale
void set_editables(bool _ed=true)
allow to edit the canvases
void hide_empty_bins()
remove color on empty bins (usefull for plot with negative and positive values)
Definition: tklog.cpp:39