Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:09:24

0001 #include "TFile.h"
0002 #include "TH1.h"
0003 #include "TKey.h"
0004 #include "TClass.h"
0005 #include "TList.h"
0006 #include "THStack.h"
0007 #include "TLegend.h"
0008 #include "TCanvas.h"
0009 #include "TPaveStats.h"
0010 #include "TSystem.h"
0011 
0012 #include <fstream>
0013 #include <iostream>
0014 
0015 
0016 void plotScript(string file1) {
0017 
0018     ifstream infile("plotlist.txt");
0019 
0020     vector<string> plotDirectories;
0021 
0022     string s;
0023     
0024     while (getline(infile,s)) {
0025         plotDirectories.push_back(s);
0026     }
0027 
0028     
0029     ofstream outfile;
0030 
0031     string ofname = file1;
0032 
0033     while (ofname.find("/") != string::npos) {
0034         ofname.replace(ofname.find("/"), 1, "_");
0035     }
0036     while (ofname.find(".") != string::npos) {
0037         ofname.replace(ofname.find("."), 1, "_");
0038     }
0039     
0040     cout << ofname << endl;
0041     string prefix = ofname;
0042     ofname += ".html";
0043 
0044     outfile.open(ofname.c_str());
0045     outfile << "<!DOCTYPE html> <html> <head> <title>\n";
0046     outfile << file1 << "\n";
0047     outfile << "</title> </head>\n";
0048     outfile << "<body>\n";
0049     outfile << "<h1>\n";
0050     outfile << "<span style=\"color:#0404B4\">" << file1 << "</span>";
0051     outfile << "</h1>\n";
0052     outfile << "<a name=\"top\">Directory listing:</a><br>\n";
0053     int anchor = 0;
0054     for (vector<string>::iterator vecIt = plotDirectories.begin();
0055             vecIt != plotDirectories.end(); ++vecIt) {
0056         outfile << "<a href=\"#" << anchor << "\">" << *vecIt << "</a><br>\n";
0057         ++anchor;
0058     }
0059     
0060     TFile * f1 = new TFile(file1.c_str(), "READ");
0061     
0062     TDirectory * mydir; 
0063     TObject * obj;
0064     TCanvas * c0 = new TCanvas("c0","c0");
0065     anchor = 0;
0066     TLegend * leg = new TLegend(0.78,0.9,0.98,0.98);
0067     TH1F * firstH = new TH1F();
0068     firstH->SetLineWidth(2);
0069     firstH->SetLineColor(4);
0070     leg->AddEntry(firstH,file1.c_str(),"l");
0071     for (vector<string>::iterator vecIt = plotDirectories.begin();
0072             vecIt != plotDirectories.end(); ++vecIt) {
0073         outfile << "<br><a name=\"" << anchor << "\"><h2>" << *vecIt << "</h2></a> <a href=\"#top\">[Back to top]</a><br>\n";
0074         ++anchor;
0075         TH1 *h;
0076         TKey *key;
0077         mydir = f1->GetDirectory((*vecIt).c_str());
0078         TIter nextkey(mydir->GetListOfKeys());
0079         float scale = 1.;
0080         while (key = (TKey*)nextkey()) {
0081             obj = key->ReadObj();
0082             if (obj->IsA()->InheritsFrom("TH1")) {
0083                 h = (TH1*)obj; 
0084 
0085                 if (string(h->GetName()).find("convVtxYvsX") != string::npos ||
0086                        string(h->GetName()).find("convVtxRvsZ") != string::npos) {
0087                     h->Draw();
0088                     c0->Modified();
0089                     c0->Update(); 
0090                     string filename = prefix + "_" + *vecIt + "_" + h->GetName() + ".png";
0091                     while (filename.find("/") != string::npos) {
0092                         filename.replace(filename.find("/"), 1, "_");
0093                     }
0094                     while (filename.find(" ") != string::npos) {
0095                         filename.replace(filename.find(" "), 1, "_");
0096                     }
0097                     c0->SaveAs(filename.c_str());
0098                     outfile << "<a href=\"" << filename << "\"><img height=\"200\" width=\"324\" src=\"" << filename << "\"></a>\n";
0099                     continue;
0100                 }
0101 
0102                 bool is1D = true;
0103                 
0104                 if (obj->IsA()->InheritsFrom("TH2")) {
0105                     h->SetMarkerColor(4);
0106                     is1D = false;
0107                 } 
0108 
0109                 int firstBin = 1;
0110                 int lastBin  = h->GetNbinsX();
0111                     
0112                 if (h->GetEntries() == 0) {
0113                 } else {
0114                     if (is1D) {
0115                         while (h->GetBinContent(firstBin) == 0. && firstBin < lastBin) ++firstBin;
0116                         while (h->GetBinContent(lastBin)  == 0. && firstBin < lastBin) --lastBin;
0117                     }
0118 
0119 
0120                     h->SetFillStyle(0);
0121                     h->SetLineColor(4);
0122                     h->SetLineWidth(2);
0123                     scale = 1./h->GetEntries();
0124                 }
0125 
0126                 if (vecIt->find("Efficiencies") == string::npos && 
0127                         !(obj->IsA()->InheritsFrom("TProfile"))) {
0128                 }
0129                 THStack * stack = new THStack("stack",h->GetTitle());
0130                 stack->Add(h);
0131 
0132 
0133 
0134                 stack->Draw("nostack");
0135                 if (is1D) stack->GetXaxis()->SetRange(firstBin, lastBin);
0136                 stack->GetXaxis()->SetTitle(h->GetXaxis()->GetTitle());
0137                 stack->GetYaxis()->SetTitle(h->GetYaxis()->GetTitle());
0138                 stack->Draw("nostack");
0139                 leg->Draw("same");
0140                 c0->Modified();
0141                 c0->Update(); 
0142                 string filename = prefix + "_" + *vecIt + "_" + h->GetName() + ".png";
0143                 while (filename.find("/") != string::npos) {
0144                     filename.replace(filename.find("/"), 1, "_");
0145                 }
0146                 while (filename.find(" ") != string::npos) {
0147                     filename.replace(filename.find(" "), 1, "_");
0148                 }
0149                 c0->SaveAs(filename.c_str());
0150                 outfile << "<a href=\"" << filename << "\"><img height=\"200\" width=\"324\" src=\"" << filename << "\"></a>\n";
0151             }
0152         }
0153 
0154     }
0155 
0156     outfile << "</body>\n";
0157     outfile << "</html>\n";
0158     outfile.close();
0159 
0160     cout << "DONE!  HTML file created:" << endl;
0161     cout << ofname << endl;
0162 }
0163