]> git.uio.no Git - u/mrichter/AliRoot.git/blame - PWGLF/FORWARD/analysis2/qa/QABase.h
Some new script (ROOT and shell) for new centralized QA
[u/mrichter/AliRoot.git] / PWGLF / FORWARD / analysis2 / qa / QABase.h
CommitLineData
2dbde04b 1/**
2 * @file QABase.h
3 * @author Christian Holm Christensen <cholm@nbi.dk>
4 * @date Thu Nov 17 11:58:11 2011
5 *
6 * @brief Base class for QA active classes
7 *
bd6f5206 8 * @ingroup pwglf_forward_qa_scripts
2dbde04b 9 *
10 */
11#ifndef QABASE_H
12#define QABASE_H
13#ifndef __CINT__
14# include <TTree.h>
15# include <TFile.h>
16# include <TError.h>
17# include <TCanvas.h>
18# include <TSystem.h>
19# include <fstream>
20# include <TLatex.h>
21# include <TStyle.h>
22# include "QARing.h"
23#else
24class TTree;
25class TFile;
26class QARing;
27class Global;
28class TCanvas;
29#endif
30
31/**
32 * Base class for active QA classes. This manages the I/O files, like
33 * the tree file, the LaTeX file, and the storage file
34 *
bd6f5206 35 * @ingroup pwglf_forward_qa_scripts
2dbde04b 36 */
37struct QABase
38{
27fcc3c7 39 QABase(const TString& dataType,
40 Int_t prodYear,
41 const TString& period,
42 const TString& pass)
2dbde04b 43 : fFMD1i(0),
44 fFMD2i(0),
45 fFMD2o(0),
46 fFMD3i(0),
47 fFMD3o(0),
48 fGlobal(0),
49 fTree(0),
50 fOutput(0),
51 fStore(0),
52 fTeX(0),
3405bb49 53 fHtml(0),
27fcc3c7 54 fTeXName("index"),
2dbde04b 55 fToDelete(""),
56 fCanvas(0),
27fcc3c7 57 fOutputName("trending.root"),
58 fDataType(dataType),
7fbaa8da 59 fYear(prodYear),
27fcc3c7 60 fPeriod(period),
61 fPass(pass)
2dbde04b 62 {}
63 /**
64 * Copy constructor
65 *
66 * @param o Object to copy from
67 */
68 QABase(const QABase& o)
69 : fFMD1i(o.fFMD1i),
70 fFMD2i(o.fFMD2i),
71 fFMD2o(o.fFMD2o),
72 fFMD3i(o.fFMD3i),
73 fFMD3o(o.fFMD3o),
74 fGlobal(o.fGlobal),
75 fTree(o.fTree),
76 fOutput(o.fOutput),
77 fStore(o.fStore),
78 fTeX(o.fTeX),
3405bb49 79 fHtml(o.fHtml),
2dbde04b 80 fTeXName(o.fTeXName),
81 fToDelete(o.fToDelete),
82 fCanvas(o.fCanvas),
27fcc3c7 83 fOutputName(o.fOutputName),
84 fDataType(o.fDataType),
85 fYear(o.fYear),
86 fPeriod(o.fPeriod),
87 fPass(o.fPass)
2dbde04b 88 {}
89 /**
90 * Assignment operator
91 *
92 * @return Reference to this object
93 */
94 QABase& operator=(const QABase&) { return *this; }
95 /**
96 * Desctructor
97 */
98 virtual ~QABase()
99 {
100 if (fFMD1i) { delete fFMD1i; }
101 if (fFMD2i) { delete fFMD2i; }
102 if (fFMD2o) { delete fFMD2o; }
103 if (fFMD3i) { delete fFMD3i; }
104 if (fFMD3o) { delete fFMD3o; }
105 if (fGlobal) { delete fGlobal; }
106 if (fTree) { delete fTree; }
107 if (fOutput) { delete fOutput; }
108 if (fStore) { delete fStore; }
109 if (fTeX) { fTeX->close(); fTeX = 0; }
3405bb49 110 if (fHtml) { fHtml->close(); fHtml = 0; }
2dbde04b 111 }
bae153e5 112 /**
113 * Set the output file name
114 *
115 * @param name Name of output (tree) file
116 */
117 void SetOutputName(const char* name) { fOutputName = name; }
2dbde04b 118 /**
119 * The name of the TTree output file
120 *
121 * @return Output file name
122 */
bae153e5 123 const char* OutputName() const { return fOutputName.Data(); }
60db3344 124 /**
125 * Make a tree
126 *
127 * @param read If true, read from file
128 *
129 * @return True on success
130 */
bae153e5 131 virtual Bool_t MakeTree(bool read)
2dbde04b 132 {
27fcc3c7 133 fOutput = new TFile(OutputName(), (read ? "READ" : "RECREATE"));
134 if (!fOutput) {
135 Error("MakeTree", "Failed to open output file");
136 return false;
2dbde04b 137 }
27fcc3c7 138 if (read) fTree = static_cast<TTree*>(fOutput->Get("T"));
139 else fTree = new TTree("T", "T");
bae153e5 140 if (!fTree) {
141 Error("MakeTree", "No tree defined!");
142 return false;
143 }
144
145 return true;
146 }
147 /**
148 * Initialize
149 *
150 * @param read If true initialise for reading tree file
151 *
152 * @return True on success
153 */
154 Bool_t Init(bool read=false)
155 {
156 if (!MakeTree(read)) return false;
157
2dbde04b 158 if (read) fGlobal = Global::SetBranch(fTree);
159 else fGlobal = Global::MakeBranch(fTree);
bae153e5 160
161 fToDelete = "";
2dbde04b 162
163 fFMD1i->Init(fTree, read);
164 fFMD2i->Init(fTree, read);
165 fFMD2o->Init(fTree, read);
166 fFMD3i->Init(fTree, read);
167 fFMD3o->Init(fTree, read);
168
169 return true;
170 }
171 /**
172 * Make a canvas, LaTeX file, and storage file
173 *
174 * @param title Title of canvas and files
175 */
176 void MakeCanvas(const char* title)
177 {
dc8aa359 178 if (fCanvas) {
179 delete fCanvas;
180 fCanvas = 0;
181 }
182 const char* base = fTeXName.Data();
183 gSystem->Exec(Form("rm -f %s.tex %s.html %s.root %s.pdf",
184 base, base, base, base));
185
186 fTeX = new std::ofstream(Form("%s.tex", base));
187 fHtml = new std::ofstream(Form("%s.html", base));
3405bb49 188
2dbde04b 189 *fTeX << "\\documentclass[landscape,a4paper,12pt]{article}\n"
190 << "\\usepackage[margin=2cm,a4paper]{geometry}\n"
191 << "\\usepackage{graphicx}\n"
192 << "\\title{{\\Huge\\bf " << title << "}}\n"
193 << "\\author{{\\LARGE FMD Team}}\n"
194 << "\\date{{\\Large \\today}}\n"
195 << "\\begin{document}\n"
196 << "\\thispagestyle{empty}\n"
197 << "\\maketitle" << std::endl;
198
7fbaa8da 199 *fHtml << "<!DOCTYPE html>\n"
200 << "<html>\n"
3405bb49 201 << " <head>\n"
202 << " <title>QA information - " << title << "</title>\n"
b288a8e6 203 << " <link rel='stylesheet' href='style.css'>\n"
7fbaa8da 204 << " <link rel='shortcut icon' href='fmd_favicon.png' "
205 << "type='image/x-png'>\n"
3405bb49 206 << " </head>\n"
207 << "<body>\n"
208 << " <h1>" << title << "</h1>\n"
209 << " <table>"
210 << std::endl;
211
2dbde04b 212 gStyle->SetPalette(1);
213 gStyle->SetOptFit(0);
214 gStyle->SetOptStat(0);
215 gStyle->SetOptTitle(1);
216 gStyle->SetTitleW(.4);
217 gStyle->SetTitleH(.1);
218 // gStyle->SetTitleColor(0);
219 gStyle->SetTitleStyle(0);
220 gStyle->SetTitleBorderSize(0);
221 gStyle->SetTitleX(.6);
222
223 fCanvas = new TCanvas("qa", title, 900, 700);
224 fCanvas->SetFillColor(0);
225 fCanvas->SetBorderSize(0);
226 fCanvas->SetLeftMargin(0.15);
227 fCanvas->SetRightMargin(0.02);
228 fCanvas->SetTopMargin(0.10);
229 fCanvas->SetBottomMargin(0.10);
230
231 fStore = TFile::Open(Form("%s.root", fTeXName.Data()), "RECREATE");
232 if (!fStore)
233 Warning("MakeCanvas", "Failed to make store %s.root", fTeXName.Data());
234
235 fToDelete = "";
236 }
237 /**
238 * Put a title on the canvas. Also clears page in LaTeX file
239 *
240 * @param title Title
241 */
242 void CanvasTitle(const char* title)
243 {
244 if (!fCanvas) return;
245
246 fCanvas->cd();
247 fCanvas->Clear();
248 fCanvas->SetBorderSize(0);
249 fCanvas->SetLeftMargin(0.15);
250 fCanvas->SetRightMargin(0.02);
251 fCanvas->SetTopMargin(0.10);
252 fCanvas->SetBottomMargin(0.10);
253
254 *fTeX << "\\clearpage\n"
255 << "%% -----------------------------------------------------"
256 << std::endl;
257
3405bb49 258 TString tit(title);
259 tit.ReplaceAll("#LT", "&lt;");
260 tit.ReplaceAll("#GT", "&gt;");
261 tit.ReplaceAll("#Delta", "&Delta;");
262 tit.ReplaceAll("#xi", "&xi;");
263 tit.ReplaceAll("#sigma", "&sigma;");
264 tit.ReplaceAll("#chi", "&chi;");
265 tit.ReplaceAll("#nu", "&nu;");
dc8aa359 266 tit.ReplaceAll("_{p}", "<sub>p</sub>");
267 tit.ReplaceAll("_{z}", "<sub>z</sub>");
268 tit.ReplaceAll("^{2}", "<sup>2</sup>");
3405bb49 269 *fHtml << "<tr><td>" << tit << "</td>" << std::flush;
270
5e8eab11 271 PutCanvasTitle(title);
272 }
60db3344 273 /**
274 * Put a title on the canvas
275 *
276 * @param title Title
277 */
5e8eab11 278 void PutCanvasTitle(const char* title)
279 {
2dbde04b 280 // Put title on top
281 TLatex* topText = new TLatex(.5, .99, title);
282 topText->SetTextAlign(23);
283 topText->SetTextSize(.038);
284 topText->SetTextFont(42);
285 topText->SetTextColor(kBlue+3);
286 topText->SetNDC();
287 topText->Draw();
288 }
289 /**
290 * Print the canvas to PNG and include it into LaTeX file
291 *
292 * @param pngName Base name of PNG
293 * @param runNo Run number to append
294 */
27fcc3c7 295 void PrintCanvas(const char* pngName, UInt_t /*runNo*/)
2dbde04b 296 {
27fcc3c7 297 // TString s(Form("%s_%09d", pngName, runNo));
298 TString s(pngName);
2dbde04b 299 PrintCanvas(s.Data());
300 }
301 /**
302 * Print the canvas to PNG and include it into LaTeX file. Stores
303 * canvas in storage file
304 *
305 * @param pngName Base name of PNG
33438b4c 306 * @param areas Areas to print
2dbde04b 307 */
7fbaa8da 308 void PrintCanvas(const char* pngName, TCollection* areas=0)
2dbde04b 309 {
dc8aa359 310 gSystem->Exec(Form("rm -f %s.png %s.html", pngName, pngName));
2dbde04b 311 fCanvas->SaveAs(Form("%s.png", pngName));
312 *fTeX << "\\begin{center}\n"
313 << "\\includegraphics[keepaspectratio,height=\\textheight]{"
314 << pngName << "}\n"
315 << "\\end{center}" << std::endl;
3405bb49 316 *fHtml << "<td><a href='" << pngName << ".html'>Plot</a></td></tr>"
317 << std::endl;
318
319 std::ofstream img(Form("%s.html", pngName));
7fbaa8da 320 img << "<!DOCTYPE html>\n"
321 << "<html>\n"
3405bb49 322 << " <head>\n"
323 << " <title>" << pngName << "</title>\n"
7fbaa8da 324 << " <link rel='stylesheet' href='style.css'></link>\n"
325 << " <link rel='shortcut icon' href='fmd_favicon.png' "
326 << "type='image/x-png'>\n"
3405bb49 327 << " </head>\n"
328 << " <body>\n"
329 << " <h1>" << pngName << "</h1>\n"
7fbaa8da 330 << " <div id=\"imap\">\n"
331 << " <img src=\"" << pngName << ".png\">\n";
332 if (areas) {
333 TIter next(areas);
334 TObject* o = 0;
335 while ((o = next()))
336 img << " " << o->GetName() << "\n";
337 }
338 img << " </div>\n" << std::endl;
3405bb49 339 WriteImageFooter(img, pngName);
340 img << " </body>\n"
341 << "</html>" << std::endl;
342 img.close();
343 gSystem->Exec(Form("chmod g+rw %s.html", pngName));
344
2dbde04b 345 fToDelete.Append(Form(" %s.png", pngName));
346 TDirectory* d = gDirectory;
347 fStore->cd();
348 fCanvas->Write();
349 d->cd();
350 }
60db3344 351 /**
352 * Write out image footer
353 *
354 * @param o Output stream
355 */
3405bb49 356 virtual void WriteImageFooter(std::ostream& o, const char* /*pngName*/)
357 {
358 TDatime now;
359 o << "<div class='back'>\n"
360 << "<a href='" << fTeXName << ".html'>Back</a>\n"
361 << "</div>\n"
b288a8e6 362 << "<div class='change'>\n"
3405bb49 363 << " Last update: " << now.AsString() << "\n"
364 << "</div>" << std::endl;
365 }
2dbde04b 366 /**
367 * Close the LaTeX and storage files. Runs PDFLaTeX on LaTeX
368 * file. Makes sure that the files are group writable.
369 *
370 * @param deletePNGs If true, delete intermident PNG files
371 */
372 void Close(bool deletePNGs=true)
373 {
3405bb49 374 const char* base = fTeXName.Data();
2dbde04b 375 if (fTeX) {
376 *fTeX << "\\end{document}\n"
377 << "%% EOF" << std::endl;
378 fTeX->close();
379 fTeX = 0;
380
2dbde04b 381 gSystem->Exec(Form("pdflatex %s.tex > /dev/null 2>&1", base));
2dbde04b 382 Info("Close", "PDF file %s.pdf generated", base);
383 }
3405bb49 384 if (fHtml) {
385 TDatime now;
386 *fHtml << "</table>" << std::endl;
9a684059 387 WriteLinks();
3405bb49 388 WriteFooter();
389 *fHtml << "</body></html>" << std::endl;
390 fHtml->close();
391 fHtml = 0;
392 gSystem->Exec(Form("chmod g+rw %s.html", fTeXName.Data()));
393 }
2dbde04b 394 if (fStore) {
395 fStore->Write();
396 fStore->Close();
3405bb49 397 gSystem->Exec(Form("chmod 664 %s.root", fTeXName.Data()));
2dbde04b 398 }
3405bb49 399 TString cmd(Form("rm -f %s.log %s.aux %s.tex %s",
400 base, base, base,
401 deletePNGs ? fToDelete.Data() : ""));
3405bb49 402 gSystem->Exec(cmd.Data());
403 gSystem->Exec(Form("chmod g+rw %s.pdf %s", base,
404 deletePNGs ? "" : fToDelete.Data()));
405 }
60db3344 406 /**
407 * Output links
408 *
409 */
9a684059 410 virtual void WriteLinks()
3405bb49 411 {
9a684059 412 *fHtml << "<h3>Collection of plots</h3>\n"
413 << "<ul>\n"
3405bb49 414 << " <li><a href='" << fTeXName << ".pdf'>PDF</a></li>\n"
415 << " <li><a href='" << fTeXName << ".root'>ROOT</a></li>\n"
9a684059 416 << "</ul>" << std::endl;
417 }
418 /**
419 * Write full job footer
420 *
421 */
422 virtual void WriteFooter()
423 {
424 TDatime now;
425 *fHtml << "<div class='back'>\n"
3405bb49 426 << "<a href='index.html'>Back</a>\n"
427 << "</div>\n"
b288a8e6 428 << "<div class='change'>\n"
3405bb49 429 << " Last update: " << now.AsString() << "\n"
430 << "</div>" << std::endl;
2dbde04b 431 }
2dbde04b 432 // --- Members -----------------------------------------------------
433 QARing* fFMD1i; // Pointer to ring object
434 QARing* fFMD2i; // Pointer to ring object
435 QARing* fFMD2o; // Pointer to ring object
436 QARing* fFMD3i; // Pointer to ring object
437 QARing* fFMD3o; // Pointer to ring object
438 Global* fGlobal; // Pointer to global run object
439 TTree* fTree; // Pointer to tree object
440 TFile* fOutput; // Pointer to tree file
441 TFile* fStore; // Pointer to storage file
442 std::ofstream* fTeX; // pointer to LaTeX stream
3405bb49 443 std::ofstream* fHtml; // pointer to HTML stream
2dbde04b 444 TString fTeXName; // Base name of LaTeX file
445 TString fToDelete; // List of files to possibly delete
446 TCanvas* fCanvas; // Pointer to canvas object
bae153e5 447 TString fOutputName; // Output tree file name
27fcc3c7 448 TString fDataType; // Data type
7fbaa8da 449 Int_t fYear; // Production year
27fcc3c7 450 TString fPeriod; // Period identifier
451 TString fPass; // Pass identifier
2dbde04b 452};
453
454#endif
455
456
457// Local Variables:
458// mode: C++
459// End: