test_RootPalette.cxx
Go to the documentation of this file.
1 // test_RootPalette.cxx
2 
3 // David Adams
4 // March 2018
5 //
6 // This is a test and demonstration for RootPalette.
7 
8 #undef NDEBUG
9 
11 #include <string>
12 #include <iostream>
13 #include <iomanip>
14 #include <cassert>
15 #include <vector>
16 #include "TH2F.h"
17 #include "TCanvas.h"
18 #include "TColor.h" // To get Root palette indices
19 #include "TStyle.h"
20 #include "TRandom.h"
21 
22 using std::string;
23 using std::cout;
24 using std::endl;
25 using std::setw;
26 using std::vector;
27 
28 //**********************************************************************
29 
31  const string myname = "test_RootPalette: ";
32  cout << myname << "Starting test" << endl;
33 #ifdef NDEBUG
34  cout << myname << "NDEBUG must be off." << endl;
35  abort();
36 #endif
37  string line = "-----------------------------";
38  string scfg;
39 
40  cout << myname << line << endl;
41  cout << myname << "Test data" << endl;
42 
43  vector<double> peak1 = { 1.5, 6.0, 22.3, 50.3, 74.1, 92.3, 100.0, 95.1, 89.2, 64.3, 40.2, 18.0, 4.8, 0.6 };
44  vector<double> peak2 = { 1.5, 6.0, 22.3, 50.3, 74.1, 92.3, 100.0, 95.1, 89.2, 64.3, 40.2, 18.0, 4.8,
45  -12.0, -26.1, -51.2, -93.7, -95.8, -80.6, -64.3, -49.3, -35.0, -22.1, -15.3,
46  -8.6, -5.1, -2.8};
47  vector<double> wf1(100, 0.0);
48  vector<double> wf2(100, 0.0);
49  for ( unsigned int ipk=0; ipk<peak1.size(); ++ipk ) {
50  wf1[ipk] = 0.80*peak1[ipk];
51  }
52  for ( unsigned int ipk=0; ipk<peak2.size(); ++ipk ) {
53  wf2[ipk] = 0.80*peak2[ipk];
54  }
55 
56  cout << myname << line << endl;
57  cout << myname << "Create histogram." << endl;
58  TH1* ph1 = new TH2F("h2", "My histo; Tick; Channel", 100, 0, 100, 50, 0, 50);
59  TH1* ph2 = new TH2F("h2", "My histo; Tick; Channel", 100, 0, 100, 50, 0, 50);
60  for ( TH1* ph : {ph1, ph2} ) {
61  ph->SetStats(0);
62  ph->SetFillColor(2);
63  ph->SetContour(40);
64  ph->SetMinimum(-100);
65  ph->SetMaximum(100);
66  }
67  ph1->SetMinimum(0.0);
68  ph1->SetContour(20);
69  for ( unsigned int jbin=1; jbin<=100; ++jbin ) {
70  for ( unsigned int ibin=1; ibin<=100; ++ibin ) {
71  int iwf = ( 190 + ibin - jbin)%100;
72  double z1 = wf1[iwf];
73  z1 += gRandom->Gaus(0.0, 5.0);
74  ph1->SetBinContent(ibin, jbin, z1);
75  double z2 = wf2[iwf];
76  z2 += gRandom->Gaus(0.0, 5.0);
77  ph2->SetBinContent(ibin, jbin, z2);
78  }
79  }
80 
81  TCanvas* pcan = new TCanvas;
82  pcan->SetGridx();
83  pcan->SetGridy();
84  int ncol = 0;
85  TH1* ph = ph2;
86 
87  cout << myname << line << endl;
88  cout << myname << "Draw with default palette." << endl;
89  ph->SetTitle("Default palette");
90  ncol = gStyle->GetNumberOfColors();
91  cout << myname << " Color count: " << ncol << endl;
92  ph->Draw("colz");
93  pcan->Print("test_RootPalette_default.png");
94 
95  RootPalette oldPalette;
96 
97  cout << myname << line << endl;
98  cout << myname << "Draw with palette 1010." << endl;
99  ph = ph1;
100  assert( RootPalette::set(1010) );
101  ncol = gStyle->GetNumberOfColors();
102  cout << myname << " Color count: " << ncol << endl;
103  ph->SetTitle("Palette 1010");
104  ph->Draw("colz");
105  pcan->Print("test_RootPalette_1010.png");
106 
107  cout << myname << line << endl;
108  cout << myname << "Draw with palette 1016." << endl;
109  ph = ph1;
110  assert( RootPalette::set(1016) );
111  ncol = gStyle->GetNumberOfColors();
112  cout << myname << " Color count: " << ncol << endl;
113  ph->SetTitle("Palette 1016");
114  ph->Draw("colz");
115  pcan->Print("test_RootPalette_1016.png");
116 
117  cout << myname << line << endl;
118  cout << myname << "Draw with palette 1017." << endl;
119  ph = ph1;
120  assert( RootPalette::set(1017) );
121  ncol = gStyle->GetNumberOfColors();
122  cout << myname << " Color count: " << ncol << endl;
123  ph->SetTitle("Palette 1017");
124  ph->Draw("colz");
125  pcan->Print("test_RootPalette_1017.png");
126 
127  cout << myname << line << endl;
128  cout << myname << "Draw with palette 1019." << endl;
129  ph = ph1;
130  assert( RootPalette::set(1019) );
131  ncol = gStyle->GetNumberOfColors();
132  cout << myname << " Color count: " << ncol << endl;
133  ph->SetTitle("Palette 1019");
134  ph->Draw("colz");
135  pcan->Print("test_RootPalette_1019.png");
136 
137  cout << myname << line << endl;
138  cout << myname << "Draw with palette 1020." << endl;
139  ph = ph2;
140  assert( RootPalette::set(1020) );
141  ncol = gStyle->GetNumberOfColors();
142  cout << myname << " Color count: " << ncol << endl;
143  ph->SetTitle("Palette 1020");
144  ph->Draw("colz");
145  pcan->Print("test_RootPalette_1020.png");
146 
147  cout << myname << line << endl;
148  cout << myname << "Draw with palette 1026." << endl;
149  ph = ph2;
150  assert( RootPalette::set(1026) );
151  ncol = gStyle->GetNumberOfColors();
152  cout << myname << " Color count: " << ncol << endl;
153  ph->SetTitle("Palette 1026");
154  ph->Draw("colz");
155  pcan->Print("test_RootPalette_1026.png");
156 
157  cout << myname << line << endl;
158  cout << myname << "Draw with palette 1027." << endl;
159  ph = ph2;
160  assert( RootPalette::set(1027) );
161  ncol = gStyle->GetNumberOfColors();
162  cout << myname << " Color count: " << ncol << endl;
163  ph->SetTitle("Palette 1027");
164  ph->Draw("colz");
165  pcan->Print("test_RootPalette_1027.png");
166 
167  cout << myname << line << endl;
168  cout << myname << "Draw with palette 1029." << endl;
169  ph = ph2;
170  assert( RootPalette::set(1029) );
171  ncol = gStyle->GetNumberOfColors();
172  cout << myname << " Color count: " << ncol << endl;
173  ph->SetTitle("Palette 1029");
174  ph->Draw("colz");
175  pcan->Print("test_RootPalette_1029.png");
176 
177  cout << myname << line << endl;
178  cout << myname << "Draw with rainbow palette." << endl;
179  ph = ph2;
180  assert( RootPalette::set(kRainBow) );
181  ncol = gStyle->GetNumberOfColors();
182  cout << myname << " Color count: " << ncol << endl;
183  ph->SetTitle("Palette kRainBow");
184  ph->Draw("colz");
185  pcan->Print("test_RootPalette_rainbow.png");
186 
187  cout << myname << line << endl;
188  cout << myname << "Draw with palette zero." << endl;
189  assert( RootPalette::set(0) );
190  ncol = gStyle->GetNumberOfColors();
191  cout << myname << " Color count: " << ncol << endl;
192  ph->SetTitle("Palette zero (default)");
193  ph->Draw("colz");
194  pcan->Print("test_RootPalette_zero.png");
195 
196  cout << myname << line << endl;
197  cout << myname << "Draw with invalid palette 720." << endl;
198  assert( RootPalette::set(720) );
199  ncol = gStyle->GetNumberOfColors();
200  cout << myname << " Color count: " << ncol << endl;
201  ph->SetTitle("Palette 720 (invalid)");
202  ph->Draw("colz");
203  pcan->Print("test_RootPalette_0720.png");
204 
205  cout << myname << line << endl;
206  cout << myname << "Draw with invalid palette 12." << endl;
207  ph = ph1;
208  assert( RootPalette::set(12) );
209  ncol = gStyle->GetNumberOfColors();
210  cout << myname << " Color count: " << ncol << endl;
211  ph->SetTitle("Palette 12 (invalid)");
212  ph->Draw("colz");
213  pcan->Print("test_RootPalette_0012.png");
214 
215  cout << myname << line << endl;
216  cout << myname << "Draw with kGistEarthlette inverted." << endl;
217  ph = ph1;
218  assert( RootPalette::set(-kGistEarth) );
219  ncol = gStyle->GetNumberOfColors();
220  cout << myname << " Color count: " << ncol << endl;
221  ph->SetTitle("Palette kGistEarth inverted)");
222  ph->Draw("colz");
223  pcan->Print("test_RootPalette_gistearth.png");
224 
225  cout << myname << line << endl;
226  cout << myname << "Draw with back body palette." << endl;
227  ph = ph2;
228  assert( RootPalette::set(kBlackBody) );
229  ncol = gStyle->GetNumberOfColors();
230  cout << myname << " Color count: " << ncol << endl;
231  ph->SetTitle("Palette kBlackBody");
232  ph->Draw("colz");
233  pcan->Print("test_RootPalette_blackbody.png");
234 
235  cout << myname << line << endl;
236  cout << myname << "Draw with inverted deep sea palette." << endl;
237  ph = ph1;
238  assert( RootPalette::set(kDeepSea) );
239  TColor::InvertPalette();
240  ncol = gStyle->GetNumberOfColors();
241  cout << myname << " Color count: " << ncol << endl;
242  ph->SetTitle("Palette kDeepSea inverted");
243  ph->Draw("colz");
244  pcan->Print("test_RootPalette_deepsea_inv.png");
245 
246  cout << myname << line << endl;
247  cout << myname << "Draw with inverted cherry palette." << endl;
248  ph = ph1;
249  assert( RootPalette::set(-kCherry) );
250  ncol = gStyle->GetNumberOfColors();
251  cout << myname << " Color count: " << ncol << endl;
252  ph->SetTitle("Palette kCherry inverted");
253  ph->Draw("colz");
254  pcan->Print("test_RootPalette_cherry_inv.png");
255 
256  cout << myname << line << endl;
257  cout << myname << "Draw with default palette positive." << endl;
258  ph = ph1;
259  assert( RootPalette::defaultPalette()->setRootPalette() == 0 );
260  ncol = gStyle->GetNumberOfColors();
261  cout << myname << " Color count: " << ncol << endl;
262  ph->SetTitle("Palette default pos");
263  ph->Draw("colz");
264  pcan->Print("test_RootPalette_default_pos.png");
265 
266  cout << myname << line << endl;
267  cout << myname << "Done." << endl;
268  return 0;
269 }
270 
271 //**********************************************************************
272 
273 int main() {
274  return test_RootPalette();
275 }
276 
277 //**********************************************************************
static bool set(int ipal)
static const RootPalette * defaultPalette()
Definition: RootPalette.cxx:23
std::string string
Definition: nybbler.cc:12
struct vector vector
size_t size
Definition: lodepng.cpp:55
int test_RootPalette()
Q_EXPORT QTSManip setw(int w)
Definition: qtextstream.h:331
void line(double t, double *p, double &x, double &y, double &z)
int main()
QTextStream & endl(QTextStream &s)