test_TPadManipulator.cxx
Go to the documentation of this file.
1 // test_TPadManipulator.cxx
2 
3 // David Adams
4 // September 2017
5 //
6 // This is a test and demonstration for TPadManipulator.
7 
8 #undef NDEBUG
9 
10 #include "../TPadManipulator.h"
11 #include <string>
12 #include <iostream>
13 #include <iomanip>
14 #include <cassert>
15 #include "TH1F.h"
16 #include "TGraph.h"
17 #include "TTimeStamp.h"
18 #include "TCanvas.h"
19 #include "TROOT.h"
20 #include "TFile.h"
21 
22 using std::string;
23 using std::cout;
24 using std::endl;
25 using std::setw;
26 
27 //**********************************************************************
28 
30  const string myname = "test_TPadManipulator: ";
31  cout << myname << "Starting test" << endl;
32 #ifdef NDEBUG
33  cout << myname << "NDEBUG must be off." << endl;
34  abort();
35 #endif
36  string line = "-----------------------------";
37  string scfg;
38 
39  cout << myname << line << endl;
40  cout << myname << "Test data" << endl;
41 
42  cout << myname << line << endl;
43  cout << myname << "Create histogram." << endl;
44  double xmax = 10000;
45  TH1* ph = new TH1F("h1", "My histo; X values; Y value [units]", 100, 0, xmax);
46  ph->SetStats(0);
47  ph->SetFillColor(2);
48  for ( unsigned int ibin=1; ibin<=100; ++ ibin ) {
49  ph->SetBinContent(ibin, ibin-1);
50  }
51 
52  cout << myname << line << endl;
53  cout << myname << "Create manipulator" << endl;
54  TPadManipulator* pmantop = new TPadManipulator(700, 1000, 1, 2);
55 
56  TPadManipulator* pman = pmantop->man(0);
57  TPadManipulator& man = *pman;
58  assert( man.pad() == nullptr );
59  assert( man.hist() == nullptr );
60 
61  cout << myname << line << endl;
62  cout << myname << "Add histogram." << endl;
63  man.add(ph, "H");
64  {
65  TLatex lin(0.15, 0.85, "Test of TPadManipulator");
66  lin.SetNDC();
67  man.add(&lin);
68  }
69  {
70  TLatex lin(0.15, 0.80, "Histogram");
71  lin.SetNDC();
72  man.add(&lin);
73  }
74  assert( man.hist() != nullptr );
75 
76  cout << myname << line << endl;
77  cout << myname << "Add axis." << endl;
78  man.addAxis();
79 
80  cout << myname << line << endl;
81  cout << myname << "Add vertical mod lines." << endl;
82  man.addVerticalModLines(0.2*xmax, 0.1*xmax);
83 
84  cout << myname << line << endl;
85  cout << myname << "Add sloped lines." << endl;
86  man.addSlopedLine(0.01, 5, 2);
87  man.addSlopedLine(0.01, -5, 3);
88 
89  cout << myname << line << endl;
90  cout << myname << "Add label." << endl;
91  assert( ! man.getLabel().size() );
92  man.setLabel("This is my #beta data for E_{tot} = m#timesC^{2}.");
93  assert( man.getLabel().size() );
94 
95  cout << myname << line << endl;
96  cout << myname << "Copy the pad and draw copy with time format." << endl;
97  *pmantop->man(1) = *pmantop->man(0);
98  pmantop->man(1)->setRangeX(0, 8000);
99  pmantop->man(1)->setTitle("Zoom of above");
100  pmantop->man(1)->setRangeX(2000, 8000);
101  pmantop->man(1)->setRangeY(10, 90);
102  pmantop->man(1)->setTimeFormatX("%H:%M");
103  {
104  TLatex lin(0.15, 0.75, "Zoomed");
105  lin.SetNDC();
106  pmantop->man(1)->add(&lin);
107  }
108  {
109  TLatex lin(0.15, 0.70, "Time format");
110  lin.SetNDC();
111  pmantop->man(1)->add(&lin);
112  }
113 
114  cout << myname << line << endl;
115  cout << myname << "Draw." << endl;
116  assert( man.pad() == nullptr );
117  pmantop->draw();
118  assert( man.pad() != nullptr );
119 
120  cout << myname << line << endl;
121  cout << myname << "Draw." << endl;
122  pmantop->print("test_TPadManipulator.png");
123 
124  cout << myname << line << endl;
125  cout << myname << "Copy the full plot." << endl;
126  TPadManipulator man2(*pmantop);
127  cout << myname << "Old subpad: " << pmantop->man(0) << endl;
128  cout << myname << "Old canvas: " << pmantop << endl;
129  cout << myname << "Old subpad parent: " << pmantop->man(0)->parent() << endl;
130  cout << myname << "New subpad: " << man2.man(0) << endl;
131  cout << myname << "New canvas: " << &man2 << endl;
132  cout << myname << "New subpad parent: " << man2.man(0)->parent() << endl;
133 
134  cout << myname << line << endl;
135  cout << myname << "Change title and y-label size on copy." << endl;
136  cout << myname << "Old canvas label size: " << man2.getLabelSizeY() << endl;
137  cout << myname << "Old pad label size: " << man2.man(0)->getLabelSizeY() << endl;
138  man2.setLabelSizeY(0.02);
139  man2.setTitleSize(0.025);
140  cout << myname << "New canvas label size: " << man2.getLabelSizeY() << endl;
141  // Next is zero before pad is drawn.
142  cout << myname << "New pad label size: " << man2.man(0)->getLabelSizeY() << endl;
143 
144  cout << myname << line << endl;
145  cout << myname << "Draw copy." << endl;
146  man2.print("test_TPadManipulator-copy.{png,pdf,tpad}");
147  cout << myname << "New pad label size: " << man2.man(0)->getLabelSizeY() << endl;
148 
149 
150  cout << myname << line << endl;
151  cout << myname << "Create time graph." << endl;
152  TGraph* pg = new TGraph;
153  pg->SetTitle("Clock");
154  pg->GetXaxis()->SetTitle("March 6 daytime minutes");
155  pg->GetYaxis()->SetTitle("Minute");
156  pg->SetMarkerStyle(2);
157  srand(12345);
158  TTimeStamp ts0(2019, 3, 6, 6, 0, 0);
159  for ( int i=0; i<12*60; ++i ) {
160  double tsec = ts0 + 60*i;
161  double min = fmod(i, 60);
162  pg->SetPoint(i, tsec, min);
163  }
164  TPadManipulator mant(1400, 500);
165  mant.add(pg, "P");
166  mant.addAxis();
167  double t0 = ts0.AsDouble();
168  mant.setRangeX(t0 - 7200, t0+14*3600);
169  mant.setRangeY(0, 61);
170  mant.setTimeFormatX("%H:%M%F2019-06-06 00:00:00");
171  mant.print("test_TPadManipulator-time.png");
172 
173  cout << myname << line << endl;
174  string rfnam = "test_TPadManipulator.root";
175  cout << myname << "Write pad to " << rfnam << endl;
176  //assert( pmantop->write(rfnam) == 0 );
177  assert( pmantop->print(rfnam) == 0 );
178 
179  cout << myname << line << endl;
180  cout << myname << "Read pad from " << rfnam << endl;
181  TPadManipulator* pmani = TPadManipulator::read(rfnam);
182  assert( pmani != nullptr );
183  pmani->man(1)->setTitle("My histo after read");
184  {
185  TLatex lin(0.01, 0.97, "Read from file");
186  lin.SetNDC();
187  pmani->add(&lin);
188  }
189  pmani->print("test_TPadManipulator-read.png");
190 
191  cout << myname << line << endl;
192  cout << myname << "Root canvas count: " << gROOT->GetListOfCanvases()->GetEntries() << endl;
193  cout << myname << "Deleting manipulator." << endl;
194  delete pmantop;
195  cout << myname << "Root canvas count: " << gROOT->GetListOfCanvases()->GetEntries() << endl;
196 
197  cout << myname << line << endl;
198  cout << myname << "Done." << endl;
199  return 0;
200 }
201 
202 //**********************************************************************
203 
204 int main() {
205  return test_TPadManipulator();
206 }
207 
208 //**********************************************************************
code to link reconstructed objects back to the MC truth information
TPadManipulator * parent()
int test_TPadManipulator()
int setTitleSize(double siz)
int add(unsigned int ipad, TObject *pobj, std::string sopt="", bool replace=false)
std::string string
Definition: nybbler.cc:12
int main()
std::string getLabel() const
int addSlopedLine(double slop, double yoff=0.0, int isty=1)
int setLabelSizeY(double siz)
int setLabel(std::string slab)
TH1 * hist() const
int setTitle(std::string sttl, float height=-1.0)
static TPadManipulator * read(Name fnam, Name onam="tpad")
int addAxis(bool flag=true)
TPadManipulator * man(unsigned int ipad=0)
int addVerticalModLines(double xmod, double xoff=0.0, double lenfrac=1.0, int isty=3)
Q_EXPORT QTSManip setw(int w)
Definition: qtextstream.h:331
T min(sqlite3 *const db, std::string const &table_name, std::string const &column_name)
Definition: statistics.h:55
void line(double t, double *p, double &x, double &y, double &z)
int setRangeX(double x1, double x2)
int setTimeFormatX(std::string sfmt)
TVirtualPad * pad() const
int setRangeY(double y1, double y2)
int print(std::string fname, std::string spat="{,}")
double getLabelSizeY() const
QTextStream & endl(QTextStream &s)