3 * @author Christian Holm Christensen <cholm@nbi.dk>
4 * @date Thu Nov 17 11:58:11 2011
6 * @brief Base class for QA active classes
8 * @ingroup pwg2_forward_qa_scripts
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
35 * @ingroup pwg2_forward_qa_scripts
42 * @param single If true, only process one file.
44 QABase(Bool_t single=false)
60 fOutputName("forward_trend.root")
65 * @param o Object to copy from
67 QABase(const QABase& o)
80 fToDelete(o.fToDelete),
83 fOutputName(o.fOutputName)
88 * @return Reference to this object
90 QABase& operator=(const QABase&) { return *this; }
96 if (fFMD1i) { delete fFMD1i; }
97 if (fFMD2i) { delete fFMD2i; }
98 if (fFMD2o) { delete fFMD2o; }
99 if (fFMD3i) { delete fFMD3i; }
100 if (fFMD3o) { delete fFMD3o; }
101 if (fGlobal) { delete fGlobal; }
102 if (fTree) { delete fTree; }
103 if (fOutput) { delete fOutput; }
104 if (fStore) { delete fStore; }
105 if (fTeX) { fTeX->close(); fTeX = 0; }
106 if (fHtml) { fHtml->close(); fHtml = 0; }
109 * Set the output file name
111 * @param name Name of output (tree) file
113 void SetOutputName(const char* name) { fOutputName = name; }
115 * The name of the TTree output file
117 * @return Output file name
119 const char* OutputName() const { return fOutputName.Data(); }
120 virtual Bool_t MakeTree(bool read)
123 fOutput = new TFile(OutputName(), (read ? "READ" : "RECREATE"));
125 Error("MakeTree", "Failed to open output file");
128 if (read) fTree = static_cast<TTree*>(fOutput->Get("T"));
129 else fTree = new TTree("T", "T");
132 Error("MakeTree", "No tree defined!");
141 * @param read If true initialise for reading tree file
143 * @return True on success
145 Bool_t Init(bool read=false)
147 if (!MakeTree(read)) return false;
149 if (read) fGlobal = Global::SetBranch(fTree);
150 else fGlobal = Global::MakeBranch(fTree);
154 fFMD1i->Init(fTree, read);
155 fFMD2i->Init(fTree, read);
156 fFMD2o->Init(fTree, read);
157 fFMD3i->Init(fTree, read);
158 fFMD3o->Init(fTree, read);
163 * Make a canvas, LaTeX file, and storage file
165 * @param title Title of canvas and files
167 void MakeCanvas(const char* title)
173 const char* base = fTeXName.Data();
174 gSystem->Exec(Form("rm -f %s.tex %s.html %s.root %s.pdf",
175 base, base, base, base));
177 fTeX = new std::ofstream(Form("%s.tex", base));
178 fHtml = new std::ofstream(Form("%s.html", base));
180 *fTeX << "\\documentclass[landscape,a4paper,12pt]{article}\n"
181 << "\\usepackage[margin=2cm,a4paper]{geometry}\n"
182 << "\\usepackage{graphicx}\n"
183 << "\\title{{\\Huge\\bf " << title << "}}\n"
184 << "\\author{{\\LARGE FMD Team}}\n"
185 << "\\date{{\\Large \\today}}\n"
186 << "\\begin{document}\n"
187 << "\\thispagestyle{empty}\n"
188 << "\\maketitle" << std::endl;
192 << " <title>QA information - " << title << "</title>\n"
193 << " <link rel='stylesheet' href='style.css'>\n"
196 << " <h1>" << title << "</h1>\n"
200 gStyle->SetPalette(1);
201 gStyle->SetOptFit(0);
202 gStyle->SetOptStat(0);
203 gStyle->SetOptTitle(1);
204 gStyle->SetTitleW(.4);
205 gStyle->SetTitleH(.1);
206 // gStyle->SetTitleColor(0);
207 gStyle->SetTitleStyle(0);
208 gStyle->SetTitleBorderSize(0);
209 gStyle->SetTitleX(.6);
211 fCanvas = new TCanvas("qa", title, 900, 700);
212 fCanvas->SetFillColor(0);
213 fCanvas->SetBorderSize(0);
214 fCanvas->SetLeftMargin(0.15);
215 fCanvas->SetRightMargin(0.02);
216 fCanvas->SetTopMargin(0.10);
217 fCanvas->SetBottomMargin(0.10);
219 fStore = TFile::Open(Form("%s.root", fTeXName.Data()), "RECREATE");
221 Warning("MakeCanvas", "Failed to make store %s.root", fTeXName.Data());
226 * Put a title on the canvas. Also clears page in LaTeX file
230 void CanvasTitle(const char* title)
232 if (!fCanvas) return;
236 fCanvas->SetBorderSize(0);
237 fCanvas->SetLeftMargin(0.15);
238 fCanvas->SetRightMargin(0.02);
239 fCanvas->SetTopMargin(0.10);
240 fCanvas->SetBottomMargin(0.10);
242 *fTeX << "\\clearpage\n"
243 << "%% -----------------------------------------------------"
247 tit.ReplaceAll("#LT", "<");
248 tit.ReplaceAll("#GT", ">");
249 tit.ReplaceAll("#Delta", "Δ");
250 tit.ReplaceAll("#xi", "ξ");
251 tit.ReplaceAll("#sigma", "σ");
252 tit.ReplaceAll("#chi", "χ");
253 tit.ReplaceAll("#nu", "ν");
254 tit.ReplaceAll("_{p}", "<sub>p</sub>");
255 tit.ReplaceAll("_{z}", "<sub>z</sub>");
256 tit.ReplaceAll("^{2}", "<sup>2</sup>");
257 *fHtml << "<tr><td>" << tit << "</td>" << std::flush;
259 PutCanvasTitle(title);
261 void PutCanvasTitle(const char* title)
264 TLatex* topText = new TLatex(.5, .99, title);
265 topText->SetTextAlign(23);
266 topText->SetTextSize(.038);
267 topText->SetTextFont(42);
268 topText->SetTextColor(kBlue+3);
273 * Print the canvas to PNG and include it into LaTeX file
275 * @param pngName Base name of PNG
276 * @param runNo Run number to append
278 void PrintCanvas(const char* pngName, UInt_t runNo)
280 TString s(Form("%s_%09d", pngName, runNo));
281 PrintCanvas(s.Data());
284 * Print the canvas to PNG and include it into LaTeX file. Stores
285 * canvas in storage file
287 * @param pngName Base name of PNG
289 void PrintCanvas(const char* pngName)
291 gSystem->Exec(Form("rm -f %s.png %s.html", pngName, pngName));
292 fCanvas->SaveAs(Form("%s.png", pngName));
293 *fTeX << "\\begin{center}\n"
294 << "\\includegraphics[keepaspectratio,height=\\textheight]{"
296 << "\\end{center}" << std::endl;
297 *fHtml << "<td><a href='" << pngName << ".html'>Plot</a></td></tr>"
300 std::ofstream img(Form("%s.html", pngName));
303 << " <title>" << pngName << "</title>\n"
304 << " <link rel='stylesheet' href='style.css'>\n"
307 << " <h1>" << pngName << "</h1>\n"
308 << " <div class='img'>\n"
309 << " <img src='" << pngName << ".png'>\n"
310 << " </div>\n" << std::endl;
311 WriteImageFooter(img, pngName);
313 << "</html>" << std::endl;
315 gSystem->Exec(Form("chmod g+rw %s.html", pngName));
317 fToDelete.Append(Form(" %s.png", pngName));
318 TDirectory* d = gDirectory;
323 virtual void WriteImageFooter(std::ostream& o, const char* /*pngName*/)
326 o << "<div class='back'>\n"
327 << "<a href='" << fTeXName << ".html'>Back</a>\n"
329 << "<div class='change'>\n"
330 << " Last update: " << now.AsString() << "\n"
331 << "</div>" << std::endl;
334 * Close the LaTeX and storage files. Runs PDFLaTeX on LaTeX
335 * file. Makes sure that the files are group writable.
337 * @param deletePNGs If true, delete intermident PNG files
339 void Close(bool deletePNGs=true)
341 const char* base = fTeXName.Data();
343 *fTeX << "\\end{document}\n"
344 << "%% EOF" << std::endl;
348 gSystem->Exec(Form("pdflatex %s.tex > /dev/null 2>&1", base));
349 Info("Close", "PDF file %s.pdf generated", base);
353 *fHtml << "</table>" << std::endl;
356 *fHtml << "</body></html>" << std::endl;
359 gSystem->Exec(Form("chmod g+rw %s.html", fTeXName.Data()));
364 gSystem->Exec(Form("chmod 664 %s.root", fTeXName.Data()));
366 TString cmd(Form("rm -f %s.log %s.aux %s.tex %s",
368 deletePNGs ? fToDelete.Data() : ""));
369 gSystem->Exec(cmd.Data());
370 gSystem->Exec(Form("chmod g+rw %s.pdf %s", base,
371 deletePNGs ? "" : fToDelete.Data()));
373 virtual void WriteLinks()
375 *fHtml << "<h3>Collection of plots</h3>\n"
377 << " <li><a href='" << fTeXName << ".pdf'>PDF</a></li>\n"
378 << " <li><a href='" << fTeXName << ".root'>ROOT</a></li>\n"
379 << "</ul>" << std::endl;
382 * Write full job footer
385 virtual void WriteFooter()
388 *fHtml << "<div class='back'>\n"
389 << "<a href='index.html'>Back</a>\n"
391 << "<div class='change'>\n"
392 << " Last update: " << now.AsString() << "\n"
393 << "</div>" << std::endl;
395 // --- Members -----------------------------------------------------
396 QARing* fFMD1i; // Pointer to ring object
397 QARing* fFMD2i; // Pointer to ring object
398 QARing* fFMD2o; // Pointer to ring object
399 QARing* fFMD3i; // Pointer to ring object
400 QARing* fFMD3o; // Pointer to ring object
401 Global* fGlobal; // Pointer to global run object
402 TTree* fTree; // Pointer to tree object
403 TFile* fOutput; // Pointer to tree file
404 TFile* fStore; // Pointer to storage file
405 std::ofstream* fTeX; // pointer to LaTeX stream
406 std::ofstream* fHtml; // pointer to HTML stream
407 TString fTeXName; // Base name of LaTeX file
408 TString fToDelete; // List of files to possibly delete
409 TCanvas* fCanvas; // Pointer to canvas object
410 Bool_t fSingle; // Whether we're processing one run only
411 TString fOutputName; // Output tree file name