forked from root-project/root
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTPad.cxx
More file actions
7524 lines (6629 loc) · 239 KB
/
TPad.cxx
File metadata and controls
7524 lines (6629 loc) · 239 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// @(#)root/gpad:$Id$
// Author: Rene Brun 12/12/94
/*************************************************************************
* Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
* All rights reserved. *
* *
* For the licensing terms see $ROOTSYS/LICENSE. *
* For the list of contributors see $ROOTSYS/README/CREDITS. *
*************************************************************************/
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <locale>
#include <memory>
#include "TROOT.h"
#include "TBuffer.h"
#include "TError.h"
#include "TMath.h"
#include "TSystem.h"
#include "TStyle.h"
#include "TFile.h"
#include "TH1.h"
#include "TH2.h"
#include "TH3.h"
#include "TClass.h"
#include "TBaseClass.h"
#include "TClassTable.h"
#include "TVirtualPS.h"
#include "TVirtualX.h"
#include "TVirtualViewer3D.h"
#include "TView.h"
#include "TPoint.h"
#include "TGraph.h"
#include "TMultiGraph.h"
#include "THStack.h"
#include "TPaveText.h"
#include "TPaveStats.h"
#include "TGroupButton.h"
#include "TBrowser.h"
#include "TVirtualGL.h"
#include "TString.h"
#include "TDataMember.h"
#include "TMethod.h"
#include "TDataType.h"
#include "TFrame.h"
#include "TWbox.h"
#include "TExec.h"
#include "TDatime.h"
#include "TColor.h"
#include "TCanvas.h"
#include "TCanvasImp.h"
#include "TPluginManager.h"
#include "TEnv.h"
#include "TImage.h"
#include "TViewer3DPad.h"
#include "TCreatePrimitives.h"
#include "TLegend.h"
#include "TAtt3D.h"
#include "TVirtualPadPainter.h"
#include "strlcpy.h"
#include "snprintf.h"
#include "TVirtualMutex.h"
static Int_t gReadLevel = 0;
Int_t TPad::fgMaxPickDistance = 5;
/** \class TPad
\ingroup gpad
The most important graphics class in the ROOT system.
A Pad is contained in a Canvas.
A Pad may contain other pads (unlimited pad hierarchy).
A pad is a linked list of primitives of any type (graphics objects,
histograms, detectors, tracks, etc.).
Adding a new element into a pad is in general performed by the Draw
member function of the object classes.
It is important to realize that the pad is a linked list of references
to the original object.
For example, in case of a histogram, the histogram.Draw() operation
only stores a reference to the histogram object and not a graphical
representation of this histogram.
When the mouse is used to change (say the bin content), the bin content
of the original histogram is changed.
The convention used in ROOT is that a Draw operation only adds
a reference to the object. The effective drawing is performed
when the canvas receives a signal to be painted.
\image html gpad_pad1.png
This signal is generally sent when typing carriage return in the
command input or when a graphical operation has been performed on one
of the pads of this canvas.
When a Canvas/Pad is repainted, the member function Paint for all
objects in the Pad linked list is invoked.
\image html gpad_pad2.png
When the mouse is moved on the Pad, The member function DistancetoPrimitive
is called for all the elements in the pad. DistancetoPrimitive returns
the distance in pixels to this object.
When the object is within the distance window, the member function
ExecuteEvent is called for this object.
In ExecuteEvent, move, changes can be performed on the object.
For examples of DistancetoPrimitive and ExecuteEvent functions,
see classes
~~~ {.cpp}
TLine::DistancetoPrimitive, TLine::ExecuteEvent
TBox::DistancetoPrimitive, TBox::ExecuteEvent
TH1::DistancetoPrimitive, TH1::ExecuteEvent
~~~
A Pad supports linear and log scales coordinate systems.
The transformation coefficients are explained in TPad::ResizePad.
*/
////////////////////////////////////////////////////////////////////////////////
/// Pad default constructor.
TPad::TPad()
{
fModified = kTRUE;
fTip = nullptr;
fPadPointer = nullptr;
fPrimitives = nullptr;
fExecs = nullptr;
fCanvas = nullptr;
fPadPaint = 0;
fPixmapID = -1;
fGLDevice = -1;
fCopyGLDevice = kFALSE;
fEmbeddedGL = kFALSE;
fTheta = 30;
fPhi = 30;
fNumber = 0;
fAbsCoord = kFALSE;
fEditable = kTRUE;
fCrosshair = 0;
fCrosshairPos = 0;
fPadView3D = nullptr;
fMother = (TPad*)gPad;
fAbsHNDC = 0.;
fAbsPixeltoXk = 0.;
fAbsPixeltoYk = 0.;
fAbsWNDC = 0.;
fAbsXlowNDC = 0.;
fAbsYlowNDC = 0.;
fBorderMode = 0;
fBorderSize = 0;
fPixeltoX = 0;
fPixeltoXk = 0.;
fPixeltoY = 0.;
fPixeltoYk = 0.;
fUtoAbsPixelk = 0.;
fUtoPixel = 0.;
fUtoPixelk = 0.;
fVtoAbsPixelk = 0.;
fVtoPixel = 0.;
fVtoPixelk = 0.;
fXtoAbsPixelk = 0.;
fXtoPixel = 0.;
fXtoPixelk = 0.;
fYtoAbsPixelk = 0.;
fYtoPixel = 0.;
fYtoPixelk = 0.;
fXUpNDC = 0.;
fYUpNDC = 0.;
fFixedAspectRatio = kFALSE;
fAspectRatio = 0.;
fNumPaletteColor = 0;
fNextPaletteColor = 0;
fCGnx = 0;
fCGny = 0;
fLogx = 0;
fLogy = 0;
fLogz = 0;
fGridx = false;
fGridy = false;
fTickx = 0;
fTicky = 0;
fFrame = nullptr;
fView = nullptr;
fUxmin = fUymin = fUxmax = fUymax = 0;
// Set default world coordinates to NDC [0,1]
fX1 = 0;
fX2 = 1;
fY1 = 0;
fY2 = 1;
// Set default pad range
fXlowNDC = 0;
fYlowNDC = 0;
fWNDC = 1;
fHNDC = 1;
fViewer3D = nullptr;
SetBit(kMustCleanup);
// the following line is temporarily disabled. It has side effects
// when the pad is a TDrawPanelHist or a TFitPanel.
// the line was supposed to fix a problem with DrawClonePad
// gROOT->SetSelectedPad(this);
}
////////////////////////////////////////////////////////////////////////////////
/// Pad constructor.
///
/// A pad is a linked list of primitives.
/// A pad is contained in a canvas. It may contain other pads.
/// A pad has attributes. When a pad is created, the attributes
/// defined in the current style are copied to the pad attributes.
///
/// \param[in] name pad name
/// \param[in] title pad title
/// \param[in] xlow [0,1] is the position of the bottom left point of the pad
/// expressed in the mother pad reference system
/// \param[in] ylow [0,1] is the Y position of this point.
/// \param[in] xup [0,1] is the x position of the top right point of the pad
/// expressed in the mother pad reference system
/// \param[in] yup [0,1] is the Y position of this point.
/// \param[in] color pad color
/// \param[in] bordersize border size in pixels
/// \param[in] bordermode border mode
/// - bordermode = -1 box looks as it is behind the screen
/// - bordermode = 0 no special effects
/// - bordermode = 1 box looks as it is in front of the screen
TPad::TPad(const char *name, const char *title, Double_t xlow,
Double_t ylow, Double_t xup, Double_t yup,
Color_t color, Short_t bordersize, Short_t bordermode)
: TVirtualPad(name,title,xlow,ylow,xup,yup,color,bordersize,bordermode)
{
fModified = kTRUE;
fTip = nullptr;
fBorderSize = bordersize;
fBorderMode = bordermode;
if (gPad) fCanvas = gPad->GetCanvas();
else fCanvas = (TCanvas*)this;
fMother = (TPad*)gPad;
fPrimitives = new TList;
fExecs = new TList;
fPadPointer = nullptr;
fTheta = 30;
fPhi = 30;
fGridx = gStyle->GetPadGridX();
fGridy = gStyle->GetPadGridY();
fTickx = gStyle->GetPadTickX();
fTicky = gStyle->GetPadTickY();
fFrame = nullptr;
fView = nullptr;
fPadPaint = 0;
fPadView3D = nullptr;
fPixmapID = -1; // -1 means pixmap will be created by ResizePad()
fCopyGLDevice = kFALSE;
fEmbeddedGL = kFALSE;
fNumber = 0;
fAbsCoord = kFALSE;
fEditable = kTRUE;
fCrosshair = 0;
fCrosshairPos = 0;
fVtoAbsPixelk = 0.;
fVtoPixelk = 0.;
fVtoPixel = 0.;
fAbsPixeltoXk = 0.;
fPixeltoXk = 0.;
fPixeltoX = 0;
fAbsPixeltoYk = 0.;
fPixeltoYk = 0.;
fPixeltoY = 0.;
fXlowNDC = 0;
fYlowNDC = 0;
fWNDC = 1;
fHNDC = 1;
fXUpNDC = 0.;
fYUpNDC = 0.;
fAbsXlowNDC = 0.;
fAbsYlowNDC = 0.;
fAbsWNDC = 0.;
fAbsHNDC = 0.;
fXtoAbsPixelk = 0.;
fXtoPixelk = 0.;
fXtoPixel = 0.;
fYtoAbsPixelk = 0.;
fYtoPixelk = 0.;
fYtoPixel = 0.;
fUtoAbsPixelk = 0.;
fUtoPixelk = 0.;
fUtoPixel = 0.;
fUxmin = fUymin = fUxmax = fUymax = 0;
fLogx = gStyle->GetOptLogx();
fLogy = gStyle->GetOptLogy();
fLogz = gStyle->GetOptLogz();
fFixedAspectRatio = kFALSE;
fAspectRatio = 0.;
fNumPaletteColor = 0;
fNextPaletteColor = 0;
fCGnx = 0;
fCGny = 0;
fViewer3D = nullptr;
if (fCanvas) fGLDevice = fCanvas->GetGLDevice();
// Set default world coordinates to NDC [0,1]
fX1 = 0;
fX2 = 1;
fY1 = 0;
fY2 = 1;
if (!gPad) {
Error("TPad", "You must create a TCanvas before creating a TPad");
MakeZombie();
return;
}
TContext ctxt(kTRUE);
Bool_t zombie = kFALSE;
if ((xlow < 0) || (xlow > 1) || (ylow < 0) || (ylow > 1)) {
Error("TPad", "illegal bottom left position: x=%f, y=%f", xlow, ylow);
zombie = kTRUE;
} else if ((xup < 0) || (xup > 1) || (yup < 0) || (yup > 1)) {
Error("TPad", "illegal top right position: x=%f, y=%f", xup, yup);
zombie = kTRUE;
} else if (xup-xlow <= 0) {
Error("TPad", "illegal width: %f", xup-xlow);
zombie = kTRUE;
} else if (yup-ylow <= 0) {
Error("TPad", "illegal height: %f", yup-ylow);
zombie = kTRUE;
}
if (zombie) {
// error in creating pad occurred, make this pad a zombie
MakeZombie();
return;
}
fLogx = gStyle->GetOptLogx();
fLogy = gStyle->GetOptLogy();
fLogz = gStyle->GetOptLogz();
fUxmin = fUymin = fUxmax = fUymax = 0;
// Set pad parameters and Compute conversion coefficients
SetPad(name, title, xlow, ylow, xup, yup, color, bordersize, bordermode);
Range(0, 0, 1, 1);
SetBit(kMustCleanup);
SetBit(kCanDelete);
}
////////////////////////////////////////////////////////////////////////////////
/// Pad destructor.
TPad::~TPad()
{
if (ROOT::Detail::HasBeenDeleted(this)) return;
Close();
CloseToolTip(fTip);
DeleteToolTip(fTip);
auto primitives = fPrimitives;
// In some cases, fPrimitives has the kMustCleanup bit set which will lead
// its destructor to call RecursiveRemove and since this pad is still
// likely to be (indirectly) in the list of cleanups, we must set
// fPrimitives to nullptr to avoid TPad::RecursiveRemove from calling
// a member function of a partially destructed object.
fPrimitives = nullptr;
delete primitives;
SafeDelete(fExecs);
delete fViewer3D;
// Required since we overload TObject::Hash.
ROOT::CallRecursiveRemoveIfNeeded(*this);
if (this == gPad)
gPad = nullptr;
}
////////////////////////////////////////////////////////////////////////////////
/// Add an object to list of primitives with specified draw option
/// When \par modified set to kTRUE (default) pad will be marked as modified
/// Let avoid usage of gPad when drawing object(s) in canvas or in subpads.
///
/// ~~~{.cpp}
/// auto c1 = new TCanvas("c1","Canvas with subpoads", 600, 600);
/// c1->Divide(2,2);
///
/// for (Int_t n = 1; n <= 4; ++n) {
/// auto h1 = new TH1I(TString::Format("hist_%d",n), "Random hist", 100, -5, 5);
/// h1->FillRandom("gaus", 2000 + n*1000);
/// c1->GetPad(n)->Add(h1);
/// }
/// ~~~
void TPad::Add(TObject *obj, Option_t *opt, Bool_t modified)
{
if (!obj)
return;
if (!fPrimitives)
fPrimitives = new TList;
obj->SetBit(kMustCleanup);
fPrimitives->Add(obj, opt);
if (modified)
Modified();
}
////////////////////////////////////////////////////////////////////////////////
/// Add an object as first in list of primitives with specified draw option
/// When \par modified set to kTRUE (default) pad will be marked as modified
/// Let avoid usage of gPad when drawing object(s) in canvas or in subpads.
void TPad::AddFirst(TObject *obj, Option_t *opt, Bool_t modified)
{
if (!obj)
return;
if (!fPrimitives)
fPrimitives = new TList;
obj->SetBit(kMustCleanup);
fPrimitives->AddFirst(obj, opt);
if (modified)
Modified();
}
////////////////////////////////////////////////////////////////////////////////
/// Add a new TExec object to the list of Execs.
///
/// When an event occurs in the pad (mouse click, etc) the list of C++ commands
/// in the list of Execs are executed via TPad::AutoExec.
///
/// When a pad event occurs (mouse move, click, etc) all the commands
/// contained in the fExecs list are executed in the order found in the list.
///
/// This facility is activated by default. It can be deactivated by using
/// the canvas "Option" menu.
///
/// The following examples of TExec commands are provided in the tutorials:
/// macros exec1.C and exec2.C.
///
/// ### Example1 of use of exec1.C
///
/// ~~~ {.cpp}
/// Root > TFile f("hsimple.root")
/// Root > hpx.Draw()
/// Root > c1.AddExec("ex1",".x exec1.C")
/// ~~~
///
/// At this point you can use the mouse to click on the contour of
/// the histogram hpx. When the mouse is clicked, the bin number and its
/// contents are printed.
///
/// ### Example2 of use of exec1.C
///
/// ~~~ {.cpp}
/// Root > TFile f("hsimple.root")
/// Root > hpxpy.Draw()
/// Root > c1.AddExec("ex2",".x exec2.C")
/// ~~~
///
/// When moving the mouse in the canvas, a second canvas shows the
/// projection along X of the bin corresponding to the Y position
/// of the mouse. The resulting histogram is fitted with a gaussian.
/// A "dynamic" line shows the current bin position in Y.
/// This more elaborated example can be used as a starting point
/// to develop more powerful interactive applications exploiting the C++
/// interpreter as a development engine.
void TPad::AddExec(const char *name, const char *command)
{
if (!fExecs) fExecs = new TList;
TExec *ex = new TExec(name,command);
fExecs->Add(ex);
}
////////////////////////////////////////////////////////////////////////////////
/// Execute the list of Execs when a pad event occurs.
void TPad::AutoExec()
{
if (GetCrosshair())
DrawCrosshair();
if (!fExecs)
return;
TIter next(fExecs);
while (auto exec = (TExec*)next())
exec->Exec();
}
////////////////////////////////////////////////////////////////////////////////
/// Browse pad.
void TPad::Browse(TBrowser *b)
{
cd();
if (fPrimitives) fPrimitives->Browse(b);
}
////////////////////////////////////////////////////////////////////////////////
/// Build a legend from the graphical objects in the pad.
///
/// A simple method to build automatically a TLegend from the primitives in a TPad.
///
/// Only those deriving from TAttLine, TAttMarker and TAttFill are added, excluding
/// TPave and TFrame derived classes.
///
/// \return The built TLegend
///
/// \param[in] x1, y1, x2, y2 The TLegend coordinates
/// \param[in] title The legend title. By default it is " "
/// \param[in] option The TLegend option
///
/// The caller program owns the returned TLegend.
///
/// If the pad contains some TMultiGraph or THStack the individual
/// graphs or histograms in them are added to the TLegend.
///
/// ### Automatic placement of the legend
/// If `x1` is equal to `x2` and `y1` is equal to `y2` the legend will be automatically
/// placed to avoid overlapping with the existing primitives already displayed.
/// `x1` is considered as the width of the legend and `y1` the height. By default
/// the legend is automatically placed with width = `x1`= `x2` = 0.3 and
/// height = `y1`= `y2` = 0.21.
TLegend *TPad::BuildLegend(Double_t x1, Double_t y1, Double_t x2, Double_t y2,
const char* title, Option_t *option)
{
TList *lop = GetListOfPrimitives();
if (!lop) return nullptr;
TList *lof = nullptr;
TLegend *leg = nullptr;
TObject *obj = nullptr;
TIter next(lop);
TString mes;
TString opt;
auto AddEntryFromListOfFunctions = [&]() {
TIter nextobj(lof);
while ((obj = nextobj())) {
if (obj->InheritsFrom(TNamed::Class())) {
if (strlen(obj->GetTitle()))
mes = obj->GetTitle();
else
mes = obj->GetName();
} else {
mes = obj->ClassName();
}
leg->AddEntry(obj, mes.Data(), "lpf");
}
};
while(auto o = next()) {
if ((o->InheritsFrom(TAttLine::Class()) || o->InheritsFrom(TAttMarker::Class()) ||
o->InheritsFrom(TAttFill::Class())) &&
( !(o->InheritsFrom(TFrame::Class())) && !(o->InheritsFrom(TPave::Class())) )) {
if (!leg)
leg = new TLegend(x1, y1, x2, y2, title);
if (o->InheritsFrom(TNamed::Class()) && strlen(o->GetTitle()))
mes = o->GetTitle();
else if (strlen(o->GetName()))
mes = o->GetName();
else
mes = o->ClassName();
if (option && strlen(option)) {
opt = option;
} else {
if (o->InheritsFrom(TAttLine::Class()))
opt += "l";
if (o->InheritsFrom(TAttMarker::Class()))
opt += "p";
if (o->InheritsFrom(TAttFill::Class()))
opt += "f";
}
leg->AddEntry(o,mes.Data(), opt.Data());
if (o->InheritsFrom(TH1::Class())) {
lof = ((TH1 *)o)->GetListOfFunctions();
AddEntryFromListOfFunctions();
}
if (o->InheritsFrom(TGraph::Class())) {
lof = ((TGraph *)o)->GetListOfFunctions();
AddEntryFromListOfFunctions();
}
} else if (o->InheritsFrom(TMultiGraph::Class())) {
if (!leg)
leg = new TLegend(x1, y1, x2, y2, title);
TList * grlist = ((TMultiGraph *)o)->GetListOfGraphs();
TIter nextgraph(grlist);
TGraph *gr = nullptr;
while ((obj = nextgraph())) {
gr = (TGraph*) obj;
if (strlen(gr->GetTitle()))
mes = gr->GetTitle();
else if (strlen(gr->GetName()))
mes = gr->GetName();
else
mes = gr->ClassName();
if (option && strlen(option))
opt = option;
else
opt = "lpf";
leg->AddEntry(obj, mes.Data(), opt);
}
lof = ((TMultiGraph *)o)->GetListOfFunctions();
AddEntryFromListOfFunctions();
} else if (o->InheritsFrom(THStack::Class())) {
if (!leg)
leg = new TLegend(x1, y1, x2, y2, title);
TList * hlist = ((THStack *)o)->GetHists();
TIter nexthist(hlist);
while ((obj = nexthist())) {
TH1 *hist = (TH1*) obj;
if (strlen(hist->GetTitle()))
mes = hist->GetTitle();
else if (strlen(hist->GetName()))
mes = hist->GetName();
else
mes = hist->ClassName();
if (option && strlen(option))
opt = option;
else
opt = "lpf";
leg->AddEntry( obj, mes.Data(), opt );
}
}
opt = "";
}
if (leg) {
TContext ctxt(this, kTRUE);
leg->Draw();
} else {
Info("BuildLegend", "No object(s) to build a TLegend.");
}
return leg;
}
////////////////////////////////////////////////////////////////////////////////
/// Set Current pad.
///
/// When a canvas/pad is divided via TPad::Divide, one can directly
/// set the current path to one of the subdivisions.
/// See TPad::Divide for the convention to number sub-pads.
///
/// Returns the new current pad, or 0 in case of failure.
///
/// For example:
/// ~~~ {.cpp}
/// c1.Divide(2,3); // create 6 pads (2 divisions along x, 3 along y).
/// ~~~
/// To set the current pad to the bottom right pad, do
/// ~~~ {.cpp}
/// c1.cd(6);
/// ~~~
/// Note1: c1.cd() is equivalent to c1.cd(0) and sets the current pad
/// to c1 itself.
///
/// Note2: after a statement like c1.cd(6), the global variable gPad
/// points to the current pad. One can use gPad to set attributes
/// of the current pad.
///
/// Note3: One can get a pointer to one of the sub-pads of pad with:
/// TPad *subpad = (TPad*)pad->GetPad(subpadnumber);
TVirtualPad *TPad::cd(Int_t subpadnumber)
{
if (!subpadnumber) {
gPad = this;
if (!gPad->IsBatch() && GetPainter()) GetPainter()->SelectDrawable(fPixmapID);
if (!fPrimitives) fPrimitives = new TList;
return gPad;
}
if (!fPrimitives) fPrimitives = new TList;
TIter next(fPrimitives);
while (auto obj = next()) {
if (obj->InheritsFrom(TPad::Class())) {
Int_t n = ((TPad*)obj)->GetNumber();
if (n == subpadnumber) {
return ((TPad*)obj)->cd();
}
}
}
return nullptr;
}
////////////////////////////////////////////////////////////////////////////////
/// Delete all pad primitives.
///
/// If the bit kClearAfterCR has been set for this pad, the Clear function
/// will execute only after having pressed a CarriageReturn
/// Set the bit with `mypad->SetBit(TPad::kClearAfterCR)`
void TPad::Clear(Option_t *option)
{
if (!IsEditable()) return;
R__LOCKGUARD(gROOTMutex);
if (!fPadPaint) {
SafeDelete(fView);
if (fPrimitives) fPrimitives->Clear(option);
if (fFrame) {
if (! ROOT::Detail::HasBeenDeleted(fFrame)) delete fFrame;
fFrame = nullptr;
}
}
if (fCanvas) fCanvas->Cleared(this);
cd();
if (TestBit(kClearAfterCR)) {
// Intentional do not use the return value of getchar,
// we just want to get it and forget it
getchar();
}
auto pp = GetPainter();
// If pad painter uses PS, ClearDrawable() start new page
if (pp) {
if (pp->IsNative())
pp->ClearDrawable();
else if (this == GetCanvas())
pp->NewPage();
}
PaintBorder(GetFillColor(), kTRUE);
fCrosshairPos = 0;
fNumPaletteColor = 0;
fCollideGrid.clear();
fCGnx = 0;
fCGny = 0;
ResetBit(TGraph::kClipFrame);
}
////////////////////////////////////////////////////////////////////////////////
/// Clipping routine: Cohen Sutherland algorithm.
///
/// - If Clip ==2 the segment is outside the boundary.
/// - If Clip ==1 the segment has one point outside the boundary.
/// - If Clip ==0 the segment is inside the boundary.
///
/// \param[inout] x[],y[] Segment coordinates (2 points)
/// \param[in] xclipl,yclipb,xclipr,yclipt Clipping boundary
Int_t TPad::Clip(Float_t *x, Float_t *y, Float_t xclipl, Float_t yclipb, Float_t xclipr, Float_t yclipt)
{
const Float_t kP=10000;
Int_t clip = 0;
for (Int_t i=0;i<2;i++) {
if (TMath::Abs(xclipl-x[i]) <= TMath::Abs(xclipr-xclipl)/kP) x[i] = xclipl;
if (TMath::Abs(xclipr-x[i]) <= TMath::Abs(xclipr-xclipl)/kP) x[i] = xclipr;
if (TMath::Abs(yclipb-y[i]) <= TMath::Abs(yclipt-yclipb)/kP) y[i] = yclipb;
if (TMath::Abs(yclipt-y[i]) <= TMath::Abs(yclipt-yclipb)/kP) y[i] = yclipt;
}
// Compute the first endpoint codes.
Int_t code1 = ClippingCode(x[0],y[0],xclipl,yclipb,xclipr,yclipt);
Int_t code2 = ClippingCode(x[1],y[1],xclipl,yclipb,xclipr,yclipt);
Double_t xt=0, yt=0;
Int_t clipped = 0; //this variable could be used in a future version
while(code1 + code2) {
clipped = 1;
// The line lies entirely outside the clipping boundary
if (code1&code2) {
clip = 2;
return clip;
}
// The line is subdivided into several parts
Int_t ic = code1;
if (ic == 0) ic = code2;
if (ic & 0x1) {
yt = y[0] + (y[1]-y[0])*(xclipl-x[0])/(x[1]-x[0]);
xt = xclipl;
}
if (ic & 0x2) {
yt = y[0] + (y[1]-y[0])*(xclipr-x[0])/(x[1]-x[0]);
xt = xclipr;
}
if (ic & 0x4) {
xt = x[0] + (x[1]-x[0])*(yclipb-y[0])/(y[1]-y[0]);
yt = yclipb;
}
if (ic & 0x8) {
xt = x[0] + (x[1]-x[0])*(yclipt-y[0])/(y[1]-y[0]);
yt = yclipt;
}
if (ic == code1) {
x[0] = xt;
y[0] = yt;
code1 = ClippingCode(xt,yt,xclipl,yclipb,xclipr,yclipt);
} else {
x[1] = xt;
y[1] = yt;
code2 = ClippingCode(xt,yt,xclipl,yclipb,xclipr,yclipt);
}
}
clip = clipped;
return clip;
}
/// @copydoc TPad::Clip(Float_t*,Float_t*,Float_t,Float_t,Float_t,Float_t)
Int_t TPad::Clip(Double_t *x, Double_t *y, Double_t xclipl, Double_t yclipb, Double_t xclipr, Double_t yclipt)
{
const Double_t kP = 10000;
Int_t clip = 0;
for (Int_t i=0;i<2;i++) {
if (TMath::Abs(xclipl-x[i]) <= TMath::Abs(xclipr-xclipl)/kP) x[i] = xclipl;
if (TMath::Abs(xclipr-x[i]) <= TMath::Abs(xclipr-xclipl)/kP) x[i] = xclipr;
if (TMath::Abs(yclipb-y[i]) <= TMath::Abs(yclipt-yclipb)/kP) y[i] = yclipb;
if (TMath::Abs(yclipt-y[i]) <= TMath::Abs(yclipt-yclipb)/kP) y[i] = yclipt;
}
// Compute the first endpoint codes.
Int_t code1 = 0;
if (x[0] < xclipl) code1 = code1 | 0x1;
if (x[0] > xclipr) code1 = code1 | 0x2;
if (y[0] < yclipb) code1 = code1 | 0x4;
if (y[0] > yclipt) code1 = code1 | 0x8;
Int_t code2 = 0;
if (x[1] < xclipl) code2 = code2 | 0x1;
if (x[1] > xclipr) code2 = code2 | 0x2;
if (y[1] < yclipb) code2 = code2 | 0x4;
if (y[1] > yclipt) code2 = code2 | 0x8;
Double_t xt=0, yt=0;
Int_t clipped = 0; //this variable could be used in a future version
while(code1 + code2) {
clipped = 1;
// The line lies entirely outside the clipping boundary
if (code1&code2) {
clip = 2;
return clip;
}
// The line is subdivided into several parts
Int_t ic = code1;
if (ic == 0) ic = code2;
if (ic & 0x1) {
yt = y[0] + (y[1]-y[0])*(xclipl-x[0])/(x[1]-x[0]);
xt = xclipl;
}
if (ic & 0x2) {
yt = y[0] + (y[1]-y[0])*(xclipr-x[0])/(x[1]-x[0]);
xt = xclipr;
}
if (ic & 0x4) {
xt = x[0] + (x[1]-x[0])*(yclipb-y[0])/(y[1]-y[0]);
yt = yclipb;
}
if (ic & 0x8) {
xt = x[0] + (x[1]-x[0])*(yclipt-y[0])/(y[1]-y[0]);
yt = yclipt;
}
if (ic == code1) {
x[0] = xt;
y[0] = yt;
code1 = ClippingCode(xt,yt,xclipl,yclipb,xclipr,yclipt);
} else {
x[1] = xt;
y[1] = yt;
code2 = ClippingCode(xt,yt,xclipl,yclipb,xclipr,yclipt);
}
}
clip = clipped;
return clip;
}
////////////////////////////////////////////////////////////////////////////////
/// Compute the endpoint codes for TPad::Clip.
Int_t TPad::ClippingCode(Double_t x, Double_t y, Double_t xcl1, Double_t ycl1, Double_t xcl2, Double_t ycl2)
{
Int_t code = 0;
if (x < xcl1) code = code | 0x1;
if (x > xcl2) code = code | 0x2;
if (y < ycl1) code = code | 0x4;
if (y > ycl2) code = code | 0x8;
return code;
}
////////////////////////////////////////////////////////////////////////////////
/// Clip polygon using the Sutherland-Hodgman algorithm.
///
/// \param[in] n Number of points in the polygon to
/// be clipped
/// \param[in] x,y Polygon x[n], y[n] do be clipped vertices
/// \param[in] xclipl,yclipb,xclipr,yclipt Clipping boundary
/// \param[out] nn Number of points in xc and yc
/// \param[out] xc,yc Clipped polygon vertices. The Int_t
/// returned by this function is
/// the number of points in the clipped
/// polygon. These vectors must
/// be allocated by the calling function.
/// A size of 2*n for each is
/// enough.
///
/// Sutherland and Hodgman's polygon-clipping algorithm uses a divide-and-conquer
/// strategy: It solves a series of simple and identical problems that, when
/// combined, solve the overall problem. The simple problem is to clip a polygon
/// against a single infinite clip edge. Four clip edges, each defining one boundary
/// of the clip rectangle, successively clip a polygon against a clip rectangle.
///
/// Steps of Sutherland-Hodgman's polygon-clipping algorithm:
///
/// * Polygons can be clipped against each edge of the window one at a time.
/// Windows/edge intersections, if any, are easy to find since the X or Y coordinates
/// are already known.
/// * Vertices which are kept after clipping against one window edge are saved for
/// clipping against the remaining edges.
/// * Note that the number of vertices usually changes and will often increases.
///
/// The clip boundary determines a visible and invisible region. The edges from
/// vertex i to vertex i+1 can be one of four types:
///
/// * Case 1 : Wholly inside visible region - save endpoint
/// * Case 2 : Exit visible region - save the intersection
/// * Case 3 : Wholly outside visible region - save nothing
/// * Case 4 : Enter visible region - save intersection and endpoint
Int_t TPad::ClipPolygon(Int_t n, Double_t *x, Double_t *y, Int_t nn, Double_t *xc, Double_t *yc, Double_t xclipl, Double_t yclipb, Double_t xclipr, Double_t yclipt)
{
if (n <= 0)
return 0;
Int_t nc, nc2;
Double_t x1, y1, x2, y2, slope; // Segment to be clipped
std::vector<Double_t> xc2(nn), yc2(nn);
// Clip against the left boundary
x1 = x[n - 1];
y1 = y[n - 1];
nc2 = 0;
Int_t i;
for (i = 0; i < n; i++) {
x2 = x[i]; y2 = y[i];
if (x1 == x2) {
slope = 0;
} else {
slope = (y2-y1)/(x2-x1);
}
if (x1 >= xclipl) {
if (x2 < xclipl) {
xc2[nc2] = xclipl; yc2[nc2++] = slope*(xclipl-x1)+y1;
} else {
xc2[nc2] = x2; yc2[nc2++] = y2;
}
} else {
if (x2 >= xclipl) {
xc2[nc2] = xclipl; yc2[nc2++] = slope*(xclipl-x1)+y1;
xc2[nc2] = x2; yc2[nc2++] = y2;
}
}
x1 = x2; y1 = y2;
}
// Clip against the top boundary
if (nc2 > 0) {
x1 = xc2[nc2 - 1];
y1 = yc2[nc2 - 1];
}
nc = 0;
for (i = 0; i < nc2; i++) {
x2 = xc2[i]; y2 = yc2[i];