PlottI
 All Classes Files Functions Variables Enumerations Enumerator Pages
Legend.h
1 
2 // ~~ Legend CLASS ~~
3 
4 // ----------------------------------------------------------------------------
5 //
6 // This file contains a basic legend class inheriting from TLegend,
7 // providing additional constructors, allowing easy legend generation from
8 // - TObjArrays
9 // - plain text (information)
10 // - descriptions of dummy markers
11 // - other legends
12 //
13 // ----------------------------------------------------------------------------
14 
15 #define LEGEND_H
16 
18 
19 class Legend : public TLegend
20 {
21 public:
22 
23  Legend();
24  Legend(TObjArray* array, std::string entries, std::string opt, std::string title="", Int_t nEntries = 0, std::string name="");
25  Legend(std::string obj, std::string entries, std::string opt, Int_t nEntries, std::string name="");
26  Legend(std::string entries, Int_t nEntries, std::string name="");
27  Legend(Legend& lgnd, std::string name="");
28  Legend(Legend* lgnd, std::string name="");
29  ~Legend() {}
30 
31  Legend* GetLegendPointer() {return this;}
32  const Legend* GetLegendPointer() const {return this;}
33  static void SetPosition(TLegend* l, Float_t x1, Float_t x2, Float_t y1, Float_t y2);
34  void SetPosition(Float_t x1, Float_t x2, Float_t y1, Float_t y2);
35  void SetPositionAuto();
36 
37  std::vector<TH1*> dummy;
38 
39 };
40 
41 // ---- Constructors ----------------------------------------------------------
42 
44 Legend::Legend(): TLegend(),
45  dummy(0)
46 {
47 }
48 
50 Legend::Legend(TObjArray* array, std::string entr, std::string opt, std::string title, Int_t nEntries, std::string name): TLegend(0.1, 0.7, 0.3, 0.9),
51  dummy(0)
52 {
53 
54  if (!array) {
55  std::cout << "\033[1;31mERROR:\033[0m Array is empty! Try again!" << std::endl;
56  return;
57  }
58 
59  std::istringstream entries(entr);
60  std::istringstream options(opt);
61 
62  TString* option = new TString();
63  TString* entryName = new TString();
64 
65  if (title != "") AddEntry((TObject*)0x0, title.data(), "");
66  if (name != "") fName = name;
67 
68  TIter iArray(array);
69  while (TObject* obj = iArray()) {
70 
71  if (obj->InheritsFrom("TPave")) continue;
72 
73  option->ReadToken(options);
74  entryName->ReadLine(entries);
75 
76  AddEntry(obj, entryName->Data(), option->Data());
77 
78  if (array->IndexOf(obj) == nEntries-1) break;
79 
80  }
81 
82  array->Add(this);
83 
84 }
85 
87 Legend::Legend(std::string obj, std::string entr, std::string opt, Int_t nEntries, std::string name): TLegend(0.1, 0.7, 0.3, 0.9),
88 dummy(nEntries)
89 {
90 
91  if (name != "") fName = name;
92 
93  std::istringstream objects(obj);
94  std::istringstream entries(entr);
95  std::istringstream options(opt);
96 
97  TString* option = new TString();
98  TString* entryName = new TString();
99  TString* object = new TString();
100 
101  TString* color = new TString();
102  TString* marker = new TString();
103  TString* size = new TString();
104 
105  for(Int_t entry = 0; entry < nEntries; entry++){
106 
107  object->ReadLine(objects);
108  std::istringstream token(object->Data());
109 
110  color ->ReadToken(token);
111  marker->ReadToken(token);
112  size ->ReadToken(token);
113 
114  option->ReadToken(options);
115  entryName->ReadLine(entries);
116 
117  dummy[entry] = new TH1C();
118  Plot::SetPlottjectProperties(dummy[entry], color->Atoi(), marker->Atoi(), size->Atof());
119 
120  AddEntry(dummy[entry], entryName->Data(), option->Data());
121 
122  }
123 
124 }
125 
127 Legend::Legend(std::string entr, Int_t nEntries, std::string name): TLegend(0.1, 0.7, 0.3, 0.9),
128 dummy(nEntries)
129 {
130 
131  if (name != "") fName = name;
132 
133  std::istringstream entries(entr);
134  TString* entryName = new TString();
135 
136  for(Int_t entry = 0; entry < nEntries; entry++){
137 
138  entryName->ReadLine(entries);
139 
140  AddEntry((TObject*)0x0, entryName->Data(), "");
141 
142  }
143 
144 }
145 
147 Legend::Legend(Legend& lgnd, std::string name): TLegend(0.1, 0.7, 0.3, 0.9),
148  dummy(0)
149 {
150  if (name != "") fName = name;
151  fPrimitives = new TList();
152  TIter prim(lgnd.fPrimitives);
153  while(TObject* entry = prim()){
154  fPrimitives->Add(entry);
155  }
156 }
157 
159 Legend::Legend(Legend* lgnd, std::string name): TLegend(0.1, 0.7, 0.3, 0.9),
160  dummy(0)
161 {
162  if (name != "") fName = name;
163  fPrimitives = new TList();
164  TIter prim(lgnd->fPrimitives);
165  while(TObject* entry = prim()){
166  fPrimitives->Add(entry);
167  }
168 }
169 
170 // ---- Member Functions ------------------------------------------------------
171 
172 void Legend::SetPosition(TLegend* l, Float_t x1, Float_t x2, Float_t y1, Float_t y2){
173 
176  l->SetX1(x1);
177  l->SetX2(x2);
178  l->SetY1(y1);
179  l->SetY2(y2);
180 
181 }
182 
183 void Legend::SetPosition(Float_t x1, Float_t x2, Float_t y1, Float_t y2){
184 
187  fX1 = x1;
188  fX2 = x2;
189  fY1 = y1;
190  fY2 = y2;
191 
192 }
193 
195 
201 }
202 
203 
204 
205 //
void SetPositionAuto()
Definition: Legend.h:194
const Legend * GetLegendPointer() const
Return pointer to class object.
Definition: Legend.h:32
Structure for saving RGB colors.
Definition: Color.h:18
std::vector< TH1 * > dummy
Vector for holding dummy markers.
Definition: Legend.h:37
Legend()
Default constructor.
Definition: Legend.h:44
static void SetPosition(TLegend *l, Float_t x1, Float_t x2, Float_t y1, Float_t y2)
Definition: Legend.h:172
static void SetPlottjectProperties(PO *pobj, Color_t color, Style_t mstyle, Size_t msize=3., Style_t lstyle=1, Size_t lwid=2., std::string title="")
Definition: PlotBase.h:231
Class for additional legend functionality, mainly additional constructors.
Definition: Legend.h:19
Legend * GetLegendPointer()
Return pointer to class object.
Definition: Legend.h:31