HtmlCosmic.C
Go to the documentation of this file.
1 /// Macro to export histogram content of a ROOT file to a html page
2 /// Based on $ROOTSYS/bin/hadd.C
3 /// Raphael Schroeter, May 2011
4 
5 // C/C++ includes
6 #include <iostream>
7 #include <string.h>
8 
9 // ROOT includes
10 #include "Riostream.h"
11 #include "TCanvas.h"
12 #include "TFile.h"
13 #include "TH1.h"
14 #include "TH2.h"
15 #include "TH3.h"
16 #include "TImage.h"
17 #include "TKey.h"
18 #include "TString.h"
19 #include "TSystem.h"
20 
21 TString htmlpath; ///< Path to html page and local location of saved images
22 TString subDir; ///< Name of current subdirectory
23 
24 ofstream *histhtml; ///< Stream to send html code/text/commands to html file
25 
26 double NormPOTCurrent; ///< Amount of POT used to generate input file
27 
28 /// Write the initial portion of the html file and set up top level directory
29 void InitializeHtml(char* file);
30 
31 /// Set up subdirectory structure of html file and local area
32 void WriteDirectoryStruct(TString inPath, TFile* inFile);
33 
34 /// Get the amount of POT from input file
35 void GetPOTInfo(TString inPath, TFile* inFile);
36 
37 /// Recursively look for each histogram in the inPath directory
38 void ScanRootfile(TString inPath, TFile* inFile);
39 
40 /// Write the final lines of the html file
41 void Finalize();
42 
43 //.............................................................................
44 void HtmlCosmic(char* file)
45 {
46  TString topPath = "/"; // Path to top directory of histogram locations/subdirectories
47 
48  // Open the file
49  TFile* inFile = new TFile(file);
50 
51  // Get the base file name
52  std::string baseFile(file);
53  baseFile.erase(0, baseFile.rfind("/"));
54 
55  InitializeHtml((char*)baseFile.c_str());
56  WriteDirectoryStruct(topPath, inFile);
57  GetPOTInfo("cosmicana", inFile);
58  ScanRootfile(topPath, inFile);
59  Finalize();
60 
61  return;
62 }
63 
64 //.............................................................................
65 void InitializeHtml(char* file)
66 {
67  // Set up html files for ease of image viewing
68  gSystem->Exec("mkdir html_out/");
69 
70  char commandHelper[256];
71  sprintf(commandHelper, "mkdir html_out/%s/", file);
72  gSystem->Exec(commandHelper);
73 
74  sprintf(commandHelper, "html_out/%s/", file);
75  htmlpath = commandHelper;
76  histhtml = new ofstream(htmlpath + "index.html", ios::out);
77 
78  *histhtml << "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n"
79  << "<html>\n <head>\n"
80  << "<title>" << file << "</title>\n"
81  << "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">\n"
82  << "</head>\n<body>\n" << std::endl;
83 
84  *histhtml << "<a name=\"pagetop\" ></a>\n"
85  << "<h1>" << file << "</h1>\n" << std::endl;
86 
87  *histhtml << "Skip to:\n" << std::endl;
88 
89  return;
90 }
91 
92 //.............................................................................
93 void WriteDirectoryStruct(TString inPath, TFile* inFile)
94 {
95  inFile->cd(inPath);
96  TDirectory* current_sourcedir = gDirectory;
97 
98  // Loop over all keys in this directory and write the subdir index on the html file
99  TIter nextkey(current_sourcedir->GetListOfKeys());
100  TKey* key;
101  TKey* oldkey = 0;
102 
103  while((key = (TKey*)nextkey())) {
104  // Keep only the highest cycle number for each key
105  if(oldkey && !strcmp(oldkey->GetName(),key->GetName())) { continue; }
106 
107  // Read object from the input file
108  inFile->cd(inPath);
109  TObject* obj = key->ReadObj();
110 
111  if(obj->IsA()->InheritsFrom(TDirectory::Class())) { // It's a subdirectory
112  subDir = obj->GetName();
113  *histhtml << "<li><a href=\"#" << subDir << "\">" << subDir << "</a></li>\n" << std::endl;
114  }
115 
116  delete obj;
117  } // end of while loop over TKeys
118 
119  return;
120 }
121 
122 //.............................................................................
123 void GetPOTInfo(TString inPath, TFile* inFile)
124 {
125  // Name of the histogram to get POT information from
126  std::string histPOT = "fSampleTime";
127 
128  if(inFile->cd(inPath)) {
129  TKey* keyPOT = (TKey*)gDirectory->GetListOfKeys()->FindObject(histPOT.c_str());
130 
131  if(keyPOT) {
132  TH1* hPOT = (TH1*)keyPOT->ReadObj();
133  NormPOTCurrent = hPOT->Integral();
134  std::cout << "Total sample time in current release " << NormPOTCurrent << std::endl;
135  }
136  else {
137  NormPOTCurrent = 1.;
138  }
139  }
140  else {
141  std::cout << "POT info not available!" << std::endl;
142  NormPOTCurrent = 1.;
143  }
144 
145  return;
146 }
147 
148 //.............................................................................
149 void ScanRootfile(TString inPath, TFile* inFile)
150 {
151  TString saveName;
152 
153  inFile->cd(inPath);
154  TDirectory* current_sourcedir = gDirectory;
155 
156  // Loop over all keys in this directory and save all histos found as PNG images
157  TIter nextkey(current_sourcedir->GetListOfKeys());
158  TKey* key;
159  TKey* oldkey = 0;
160 
161  while((key = (TKey*)nextkey())) {
162  // Keep only the highest cycle number for each key
163  if(oldkey && !strcmp(oldkey->GetName(),key->GetName())) { continue; }
164 
165  // Read object from the input file
166  inFile->cd(inPath);
167  TObject* obj = key->ReadObj();
168 
169  if(obj->IsA()->InheritsFrom(TH1::Class())) { // Could be TH1, TH2, or TH3
170  std::cout << "Exporting histogram " << obj->GetName() << std::endl;
171  TH1* hist = (TH1*)obj;
172 
173  TCanvas* c = new TCanvas("c", "c", 800, 600);
174  saveName = subDir + "/";
175 
176  TImage* img = TImage::Create();
177 
178  // Add POT scale to energy histogram axis label
179  if(strstr(hist->GetName(), "Enu")) {
180  char yAxisLabel[256];
181  sprintf(yAxisLabel, "Events / %.2E POT", NormPOTCurrent);
182  hist->GetYaxis()->SetTitle(yAxisLabel);
183  }
184 
185  // Use colz option for TH2 objects
186  if(obj->IsA()->InheritsFrom(TH2::Class())) {
187  hist->Draw("colz");
188  }
189  else {
190  hist->Draw();
191  }
192 
193  img->FromPad(c);
194  img->WriteImage(htmlpath + saveName + hist->GetName() + ".png");
195 
196  *histhtml << "<p>" << subDir << "-"<< hist->GetName() << std::endl;
197  *histhtml << "<p><p><img src=\"" << saveName + hist->GetName() << ".png\">" << std::endl;
198  *histhtml << "<a href=\"#pagetop\" >Back to top</a>\n" << std::endl;
199 
200  delete c;
201  delete img;
202  } // end of conditonal if object is TH1
203  else if(obj->IsA()->InheritsFrom(TDirectory::Class())) { // It's a subdirectory
204  subDir = obj->GetName();
205  std::cout << "Found subdirectory " << subDir << std::endl;
206 
207  // Make local directories for the images
208  gSystem->Exec("mkdir " + htmlpath + subDir);
209 
210  *histhtml << "<a name=\"" << subDir << "\">\n"
211  << "<h2>" << subDir << "</h2>\n" << std::endl;
212 
213  // Create a new subdirectory
214  TString newDir = obj->GetName();
215 
216  ScanRootfile(newDir, inFile);
217  }
218  else { // Object is of no type that this macro can handle
219  std::cout << "Unknown object type, name: " << obj->GetName()
220  << " title: " << obj->GetTitle() << std::endl;
221  }
222 
223  delete obj;
224  } // end of while loop over TKeys
225 
226  return;
227 }
228 
229 //.............................................................................
230 void Finalize()
231 {
232  *histhtml << "</body></html>" << std::endl;
233  histhtml->close();
234 
235  return;
236 }
ofstream * histhtml
Stream to send html code/text/commands to html file.
Definition: HtmlCosmic.C:24
double NormPOTCurrent
Amount of POT used to generate input file.
Definition: HtmlCosmic.C:26
TString subDir
Name of current subdirectory.
Definition: HtmlCosmic.C:22
ifstream inFile
Definition: AnaPlotMaker.h:34
void HtmlCosmic(char *file)
Definition: HtmlCosmic.C:44
void WriteDirectoryStruct(TString inPath, TFile *inFile)
Set up subdirectory structure of html file and local area.
Definition: HtmlCosmic.C:93
def img(path, mouseover="")
Definition: HTMLTools.py:36
void GetPOTInfo(TString inPath, TFile *inFile)
Get the amount of POT from input file.
Definition: HtmlCosmic.C:123
TString htmlpath
Path to html page and local location of saved images.
Definition: HtmlCosmic.C:21
OStream cout
Definition: OStream.cxx:6
void Finalize()
Write the final lines of the html file.
Definition: HtmlCosmic.C:230
TFile * file
Definition: cellShifts.C:17
void InitializeHtml(char *file)
Write the initial portion of the html file and set up top level directory.
Definition: HtmlCosmic.C:65
void ScanRootfile(TString inPath, TFile *inFile)
Recursively look for each histogram in the inPath directory.
Definition: HtmlCosmic.C:149
enum BeamMode string