]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGLF/FORWARD/analysis2/qa/QABase.h
Fixes for pA indenfication of events
[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 pwglf_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 pwglf_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   /** 
121    * Make a tree 
122    * 
123    * @param read If true, read from file 
124    * 
125    * @return True on success 
126    */
127   virtual Bool_t MakeTree(bool read)
128   {
129     if (!fSingle) { 
130       fOutput = new TFile(OutputName(), (read ? "READ" : "RECREATE"));
131       if (!fOutput) { 
132         Error("MakeTree", "Failed to open output file");
133         return false;
134       }
135       if (read) fTree   = static_cast<TTree*>(fOutput->Get("T"));
136       else      fTree   = new TTree("T", "T");
137     }
138     if (!fTree) { 
139       Error("MakeTree", "No tree defined!");
140       return false;
141     }
142
143     return true;
144   }
145   /** 
146    * Initialize 
147    * 
148    * @param read If true initialise for reading tree file
149    *
150    * @return True on success
151    */
152   Bool_t Init(bool read=false)
153   {
154     if (!MakeTree(read)) return false;
155
156     if (read) fGlobal = Global::SetBranch(fTree);
157     else      fGlobal = Global::MakeBranch(fTree);
158
159     fToDelete = "";
160       
161     fFMD1i->Init(fTree, read);
162     fFMD2i->Init(fTree, read);
163     fFMD2o->Init(fTree, read);
164     fFMD3i->Init(fTree, read);
165     fFMD3o->Init(fTree, read);
166
167     return true;
168   }
169   /** 
170    * Make a canvas, LaTeX file, and storage file 
171    * 
172    * @param title Title of canvas and files
173    */
174   void MakeCanvas(const char* title)
175   {
176     if (fCanvas) { 
177       delete fCanvas;
178       fCanvas = 0;
179     }
180     const char* base = fTeXName.Data();
181     gSystem->Exec(Form("rm -f %s.tex %s.html %s.root %s.pdf", 
182                        base, base, base, base));
183
184     fTeX = new std::ofstream(Form("%s.tex", base));
185     fHtml = new std::ofstream(Form("%s.html", base));
186
187     *fTeX << "\\documentclass[landscape,a4paper,12pt]{article}\n"
188           << "\\usepackage[margin=2cm,a4paper]{geometry}\n"
189           << "\\usepackage{graphicx}\n"
190           << "\\title{{\\Huge\\bf " << title << "}}\n"
191           << "\\author{{\\LARGE FMD Team}}\n"
192           << "\\date{{\\Large \\today}}\n"
193           << "\\begin{document}\n"
194           << "\\thispagestyle{empty}\n"
195           << "\\maketitle" << std::endl;
196
197     *fHtml << "<html>\n"
198            << " <head>\n"
199            << "  <title>QA information - "  << title << "</title>\n"
200            << "  <link rel='stylesheet' href='style.css'>\n" 
201            << " </head>\n"
202            << "<body>\n" 
203            << " <h1>" << title << "</h1>\n"
204            << " <table>" 
205            << std::endl;
206
207     gStyle->SetPalette(1);
208     gStyle->SetOptFit(0);
209     gStyle->SetOptStat(0);
210     gStyle->SetOptTitle(1);
211     gStyle->SetTitleW(.4);
212     gStyle->SetTitleH(.1);
213     // gStyle->SetTitleColor(0);
214     gStyle->SetTitleStyle(0);
215     gStyle->SetTitleBorderSize(0);
216     gStyle->SetTitleX(.6);
217
218     fCanvas = new TCanvas("qa", title, 900, 700);
219     fCanvas->SetFillColor(0);
220     fCanvas->SetBorderSize(0);
221     fCanvas->SetLeftMargin(0.15);
222     fCanvas->SetRightMargin(0.02);
223     fCanvas->SetTopMargin(0.10);
224     fCanvas->SetBottomMargin(0.10);
225
226     fStore = TFile::Open(Form("%s.root", fTeXName.Data()), "RECREATE");
227     if (!fStore) 
228       Warning("MakeCanvas", "Failed to make store %s.root", fTeXName.Data());
229     
230     fToDelete = "";
231   }
232   /** 
233    * Put a title on the canvas.  Also clears page in LaTeX file
234    * 
235    * @param title Title
236    */
237   void CanvasTitle(const char* title)
238   {
239     if (!fCanvas) return;
240
241     fCanvas->cd();
242     fCanvas->Clear();
243     fCanvas->SetBorderSize(0);
244     fCanvas->SetLeftMargin(0.15);
245     fCanvas->SetRightMargin(0.02);
246     fCanvas->SetTopMargin(0.10);
247     fCanvas->SetBottomMargin(0.10);
248
249     *fTeX << "\\clearpage\n"
250           << "%% -----------------------------------------------------"
251           << std::endl;
252
253     TString tit(title);
254     tit.ReplaceAll("#LT", "&lt;");
255     tit.ReplaceAll("#GT", "&gt;");
256     tit.ReplaceAll("#Delta", "&Delta;");
257     tit.ReplaceAll("#xi", "&xi;");
258     tit.ReplaceAll("#sigma", "&sigma;");
259     tit.ReplaceAll("#chi", "&chi;");
260     tit.ReplaceAll("#nu", "&nu;");
261     tit.ReplaceAll("_{p}", "<sub>p</sub>");
262     tit.ReplaceAll("_{z}", "<sub>z</sub>");
263     tit.ReplaceAll("^{2}", "<sup>2</sup>");
264     *fHtml << "<tr><td>" << tit << "</td>" << std::flush;
265
266     PutCanvasTitle(title);
267   }
268   /** 
269    * Put a title on the canvas
270    * 
271    * @param title Title 
272    */
273   void PutCanvasTitle(const char* title)
274   {
275     // Put title on top 
276     TLatex* topText = new TLatex(.5, .99, title);
277     topText->SetTextAlign(23);
278     topText->SetTextSize(.038);
279     topText->SetTextFont(42);
280     topText->SetTextColor(kBlue+3);
281     topText->SetNDC();
282     topText->Draw();
283   }
284   /** 
285    * Print the canvas to PNG and include it into LaTeX file
286    * 
287    * @param pngName Base name of PNG
288    * @param runNo   Run number to append
289    */
290   void PrintCanvas(const char* pngName, UInt_t runNo)
291   {
292     TString s(Form("%s_%09d", pngName, runNo));
293     PrintCanvas(s.Data());
294   }
295   /** 
296    * Print the canvas to PNG and include it into LaTeX file.  Stores
297    * canvas in storage file 
298    * 
299    * @param pngName Base name of PNG
300    */
301   void PrintCanvas(const char* pngName)
302   {
303     gSystem->Exec(Form("rm -f %s.png %s.html", pngName, pngName));
304     fCanvas->SaveAs(Form("%s.png", pngName));
305     *fTeX << "\\begin{center}\n"
306           << "\\includegraphics[keepaspectratio,height=\\textheight]{"
307           << pngName << "}\n" 
308           << "\\end{center}" << std::endl;
309     *fHtml << "<td><a href='" << pngName << ".html'>Plot</a></td></tr>" 
310            << std::endl;
311
312     std::ofstream img(Form("%s.html", pngName));
313     img << "<html>\n"
314         << " <head>\n"
315         << "  <title>" << pngName << "</title>\n"
316         << "  <link rel='stylesheet' href='style.css'>\n"
317         << " </head>\n"
318         << " <body>\n"
319         << "  <h1>" << pngName << "</h1>\n"
320         << "  <div class='img'>\n"
321         << "    <img src='" << pngName << ".png'>\n"
322         << "  </div>\n" << std::endl;
323     WriteImageFooter(img, pngName);
324     img << " </body>\n" 
325         << "</html>" << std::endl;
326     img.close();
327     gSystem->Exec(Form("chmod g+rw %s.html", pngName));
328
329     fToDelete.Append(Form(" %s.png", pngName));
330     TDirectory* d = gDirectory;
331     fStore->cd();
332     fCanvas->Write();
333     d->cd();
334   }
335   /** 
336    * Write out image footer 
337    * 
338    * @param o Output stream
339    */
340   virtual void WriteImageFooter(std::ostream& o, const char* /*pngName*/)
341   {
342     TDatime now;
343     o << "<div class='back'>\n"
344       << "<a href='" << fTeXName << ".html'>Back</a>\n"
345       << "</div>\n"
346       << "<div class='change'>\n"
347       << "  Last update: " << now.AsString() << "\n"
348       << "</div>" << std::endl;
349   }
350   /** 
351    * Close the LaTeX and storage files. Runs PDFLaTeX on LaTeX
352    * file. Makes sure that the files are group writable.
353    * 
354    * @param deletePNGs  If true, delete intermident PNG files 
355    */
356   void Close(bool deletePNGs=true)
357   {
358     const char* base = fTeXName.Data();
359     if (fTeX) {
360       *fTeX << "\\end{document}\n"
361             << "%% EOF" << std::endl;
362       fTeX->close();
363       fTeX = 0;
364       
365       gSystem->Exec(Form("pdflatex %s.tex > /dev/null 2>&1", base));
366       Info("Close", "PDF file %s.pdf generated", base);
367     }
368     if (fHtml) {
369       TDatime now;
370       *fHtml << "</table>" << std::endl;
371       WriteLinks();
372       WriteFooter();
373       *fHtml << "</body></html>" << std::endl;
374       fHtml->close();
375       fHtml = 0;
376       gSystem->Exec(Form("chmod g+rw %s.html", fTeXName.Data()));
377     }
378     if (fStore) {
379       fStore->Write();
380       fStore->Close();
381       gSystem->Exec(Form("chmod 664 %s.root", fTeXName.Data()));
382     }
383     TString cmd(Form("rm -f %s.log %s.aux %s.tex %s", 
384                      base, base, base, 
385                      deletePNGs ? fToDelete.Data() : ""));
386     gSystem->Exec(cmd.Data());
387     gSystem->Exec(Form("chmod g+rw %s.pdf %s", base, 
388                        deletePNGs ? "" : fToDelete.Data()));
389   }
390   /** 
391    * Output links 
392    * 
393    */
394   virtual void WriteLinks() 
395   {
396     *fHtml << "<h3>Collection of plots</h3>\n" 
397            << "<ul>\n"
398            << "  <li><a href='" << fTeXName << ".pdf'>PDF</a></li>\n"
399            << "  <li><a href='" << fTeXName << ".root'>ROOT</a></li>\n"
400            << "</ul>" << std::endl;
401   }
402   /** 
403    * Write full job footer 
404    * 
405    */
406   virtual void WriteFooter() 
407   {
408     TDatime now;
409     *fHtml << "<div class='back'>\n"
410            << "<a href='index.html'>Back</a>\n"
411            << "</div>\n"
412            << "<div class='change'>\n"
413            << "  Last update: " << now.AsString() << "\n"
414            << "</div>" << std::endl;
415   }
416   // --- Members -----------------------------------------------------
417   QARing*        fFMD1i;        // Pointer to ring object
418   QARing*        fFMD2i;        // Pointer to ring object
419   QARing*        fFMD2o;        // Pointer to ring object
420   QARing*        fFMD3i;        // Pointer to ring object
421   QARing*        fFMD3o;        // Pointer to ring object
422   Global*        fGlobal;       // Pointer to global run object
423   TTree*         fTree;         // Pointer to tree object
424   TFile*         fOutput;       // Pointer to tree file 
425   TFile*         fStore;        // Pointer to storage file
426   std::ofstream* fTeX;          // pointer to LaTeX stream
427   std::ofstream* fHtml;         // pointer to HTML stream
428   TString        fTeXName;      // Base name of LaTeX file 
429   TString        fToDelete;     // List of files to possibly delete
430   TCanvas*       fCanvas;       // Pointer to canvas object
431   Bool_t         fSingle;       // Whether we're processing one run only
432   TString        fOutputName;   // Output tree file name 
433 };
434
435 #endif
436
437
438 // Local Variables:
439 //  mode: C++
440 // End: