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