]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGLF/FORWARD/analysis2/qa/QABase.h
Transition PWG2/FORWARD -> PWGLF
[u/mrichter/AliRoot.git] / PWGLF / FORWARD / analysis2 / qa / QABase.h
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  * 
8  * @ingroup pwg2_forward_qa_scripts
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
24 class TTree;
25 class TFile;
26 class QARing;
27 class Global;
28 class 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  * 
35  * @ingroup pwg2_forward_qa_scripts
36  */
37 struct QABase 
38 {
39   /** 
40    * Constructor
41    * 
42    * @param single  If true, only process one file. 
43    */
44   QABase(Bool_t single=false) 
45     : fFMD1i(0),
46       fFMD2i(0),
47       fFMD2o(0),
48       fFMD3i(0),
49       fFMD3o(0),
50       fGlobal(0),
51       fTree(0), 
52       fOutput(0), 
53       fStore(0),
54       fTeX(0), 
55       fHtml(0),
56       fTeXName(""),
57       fToDelete(""),
58       fCanvas(0), 
59       fSingle(single), 
60       fOutputName("forward_trend.root")
61   {}
62   /** 
63    * Copy constructor 
64    * 
65    * @param o Object to copy from 
66    */
67   QABase(const QABase& o)
68     : fFMD1i(o.fFMD1i),
69       fFMD2i(o.fFMD2i),
70       fFMD2o(o.fFMD2o),
71       fFMD3i(o.fFMD3i),
72       fFMD3o(o.fFMD3o),
73       fGlobal(o.fGlobal),
74       fTree(o.fTree), 
75       fOutput(o.fOutput),
76       fStore(o.fStore),
77       fTeX(o.fTeX), 
78       fHtml(o.fHtml),
79       fTeXName(o.fTeXName),
80       fToDelete(o.fToDelete),
81       fCanvas(o.fCanvas),
82       fSingle(o.fSingle), 
83       fOutputName(o.fOutputName)
84   {}
85   /** 
86    * Assignment operator
87    * 
88    * @return Reference to this object
89    */
90   QABase& operator=(const QABase&) { return *this; }
91   /** 
92    * Desctructor 
93    */
94   virtual ~QABase()
95   {
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; }
107   }
108   /**
109    * Set the output file name 
110    *
111    * @param name Name of output (tree) file 
112    */
113   void SetOutputName(const char* name) { fOutputName = name; }
114   /** 
115    * The name of the TTree output file
116    * 
117    * @return Output file name 
118    */
119   const char* OutputName() const { return fOutputName.Data(); }
120   virtual Bool_t MakeTree(bool read)
121   {
122     if (!fSingle) { 
123       fOutput = new TFile(OutputName(), (read ? "READ" : "RECREATE"));
124       if (!fOutput) { 
125         Error("MakeTree", "Failed to open output file");
126         return false;
127       }
128       if (read) fTree   = static_cast<TTree*>(fOutput->Get("T"));
129       else      fTree   = new TTree("T", "T");
130     }
131     if (!fTree) { 
132       Error("MakeTree", "No tree defined!");
133       return false;
134     }
135
136     return true;
137   }
138   /** 
139    * Initialize 
140    * 
141    * @param read If true initialise for reading tree file
142    *
143    * @return True on success
144    */
145   Bool_t Init(bool read=false)
146   {
147     if (!MakeTree(read)) return false;
148
149     if (read) fGlobal = Global::SetBranch(fTree);
150     else      fGlobal = Global::MakeBranch(fTree);
151
152     fToDelete = "";
153       
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);
159
160     return true;
161   }
162   /** 
163    * Make a canvas, LaTeX file, and storage file 
164    * 
165    * @param title Title of canvas and files
166    */
167   void MakeCanvas(const char* title)
168   {
169     if (fCanvas) { 
170       delete fCanvas;
171       fCanvas = 0;
172     }
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));
176
177     fTeX = new std::ofstream(Form("%s.tex", base));
178     fHtml = new std::ofstream(Form("%s.html", base));
179
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;
189
190     *fHtml << "<html>\n"
191            << " <head>\n"
192            << "  <title>QA information - "  << title << "</title>\n"
193            << "  <link rel='stylesheet' href='style.css'>\n" 
194            << " </head>\n"
195            << "<body>\n" 
196            << " <h1>" << title << "</h1>\n"
197            << " <table>" 
198            << std::endl;
199
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);
210
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);
218
219     fStore = TFile::Open(Form("%s.root", fTeXName.Data()), "RECREATE");
220     if (!fStore) 
221       Warning("MakeCanvas", "Failed to make store %s.root", fTeXName.Data());
222     
223     fToDelete = "";
224   }
225   /** 
226    * Put a title on the canvas.  Also clears page in LaTeX file
227    * 
228    * @param title Title
229    */
230   void CanvasTitle(const char* title)
231   {
232     if (!fCanvas) return;
233
234     fCanvas->cd();
235     fCanvas->Clear();
236     fCanvas->SetBorderSize(0);
237     fCanvas->SetLeftMargin(0.15);
238     fCanvas->SetRightMargin(0.02);
239     fCanvas->SetTopMargin(0.10);
240     fCanvas->SetBottomMargin(0.10);
241
242     *fTeX << "\\clearpage\n"
243           << "%% -----------------------------------------------------"
244           << std::endl;
245
246     TString tit(title);
247     tit.ReplaceAll("#LT", "&lt;");
248     tit.ReplaceAll("#GT", "&gt;");
249     tit.ReplaceAll("#Delta", "&Delta;");
250     tit.ReplaceAll("#xi", "&xi;");
251     tit.ReplaceAll("#sigma", "&sigma;");
252     tit.ReplaceAll("#chi", "&chi;");
253     tit.ReplaceAll("#nu", "&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;
258
259     PutCanvasTitle(title);
260   }
261   void PutCanvasTitle(const char* title)
262   {
263     // Put title on top 
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);
269     topText->SetNDC();
270     topText->Draw();
271   }
272   /** 
273    * Print the canvas to PNG and include it into LaTeX file
274    * 
275    * @param pngName Base name of PNG
276    * @param runNo   Run number to append
277    */
278   void PrintCanvas(const char* pngName, UInt_t runNo)
279   {
280     TString s(Form("%s_%09d", pngName, runNo));
281     PrintCanvas(s.Data());
282   }
283   /** 
284    * Print the canvas to PNG and include it into LaTeX file.  Stores
285    * canvas in storage file 
286    * 
287    * @param pngName Base name of PNG
288    */
289   void PrintCanvas(const char* pngName)
290   {
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]{"
295           << pngName << "}\n" 
296           << "\\end{center}" << std::endl;
297     *fHtml << "<td><a href='" << pngName << ".html'>Plot</a></td></tr>" 
298            << std::endl;
299
300     std::ofstream img(Form("%s.html", pngName));
301     img << "<html>\n"
302         << " <head>\n"
303         << "  <title>" << pngName << "</title>\n"
304         << "  <link rel='stylesheet' href='style.css'>\n"
305         << " </head>\n"
306         << " <body>\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);
312     img << " </body>\n" 
313         << "</html>" << std::endl;
314     img.close();
315     gSystem->Exec(Form("chmod g+rw %s.html", pngName));
316
317     fToDelete.Append(Form(" %s.png", pngName));
318     TDirectory* d = gDirectory;
319     fStore->cd();
320     fCanvas->Write();
321     d->cd();
322   }
323   virtual void WriteImageFooter(std::ostream& o, const char* /*pngName*/)
324   {
325     TDatime now;
326     o << "<div class='back'>\n"
327       << "<a href='" << fTeXName << ".html'>Back</a>\n"
328       << "</div>\n"
329       << "<div class='change'>\n"
330       << "  Last update: " << now.AsString() << "\n"
331       << "</div>" << std::endl;
332   }
333   /** 
334    * Close the LaTeX and storage files. Runs PDFLaTeX on LaTeX
335    * file. Makes sure that the files are group writable.
336    * 
337    * @param deletePNGs  If true, delete intermident PNG files 
338    */
339   void Close(bool deletePNGs=true)
340   {
341     const char* base = fTeXName.Data();
342     if (fTeX) {
343       *fTeX << "\\end{document}\n"
344             << "%% EOF" << std::endl;
345       fTeX->close();
346       fTeX = 0;
347       
348       gSystem->Exec(Form("pdflatex %s.tex > /dev/null 2>&1", base));
349       Info("Close", "PDF file %s.pdf generated", base);
350     }
351     if (fHtml) {
352       TDatime now;
353       *fHtml << "</table>" << std::endl;
354       WriteLinks();
355       WriteFooter();
356       *fHtml << "</body></html>" << std::endl;
357       fHtml->close();
358       fHtml = 0;
359       gSystem->Exec(Form("chmod g+rw %s.html", fTeXName.Data()));
360     }
361     if (fStore) {
362       fStore->Write();
363       fStore->Close();
364       gSystem->Exec(Form("chmod 664 %s.root", fTeXName.Data()));
365     }
366     TString cmd(Form("rm -f %s.log %s.aux %s.tex %s", 
367                      base, base, base, 
368                      deletePNGs ? fToDelete.Data() : ""));
369     gSystem->Exec(cmd.Data());
370     gSystem->Exec(Form("chmod g+rw %s.pdf %s", base, 
371                        deletePNGs ? "" : fToDelete.Data()));
372   }
373   virtual void WriteLinks() 
374   {
375     *fHtml << "<h3>Collection of plots</h3>\n" 
376            << "<ul>\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;
380   }
381   /** 
382    * Write full job footer 
383    * 
384    */
385   virtual void WriteFooter() 
386   {
387     TDatime now;
388     *fHtml << "<div class='back'>\n"
389            << "<a href='index.html'>Back</a>\n"
390            << "</div>\n"
391            << "<div class='change'>\n"
392            << "  Last update: " << now.AsString() << "\n"
393            << "</div>" << std::endl;
394   }
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 
412 };
413
414 #endif
415
416
417 // Local Variables:
418 //  mode: C++
419 // End: