Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:12:41

0001 
0002 //======================================================================
0003 // This script generate a web page from a ROOT file containing histograms.
0004 // It expects the histograms being split among one level of subdirectories.
0005 // It is configured through those environment variables:
0006 //
0007 // $PUB_INPUT_FILE : name of the input ROOT file.
0008 // $PUB_INPUT_FOLDER : the top directory within the ROOT file.
0009 // $PUB_OUTPUT_DIR : name of the output directory.
0010 // $PUB_TITLE : web page title.
0011 // $PUB_COMMENT : text to be inserted at the top of the web page.
0012 //======================================================================
0013 
0014 int elePublishHistos()
0015  {
0016   TString pub_input_file = gSystem->Getenv("PUB_INPUT_FILE") ;
0017   TString pub_input_folder = gSystem->Getenv("PUB_INPUT_FOLDER") ;
0018   TString pub_output_dir = gSystem->Getenv("PUB_OUTPUT_DIR") ;
0019   TString pub_title = gSystem->Getenv("PUB_TITLE") ;
0020   TString pub_comment = gSystem->Getenv("PUB_COMMENT") ;
0021 
0022   // prepare unix output directory
0023   pub_output_dir = gSystem->ExpandPathName(pub_output_dir.Data()) ;
0024   if (gSystem->AccessPathName(pub_output_dir.Data())==kFALSE)
0025    { std::cout<<"Output directory is "<<pub_output_dir<<std::endl ; }
0026   else if (gSystem->mkdir(pub_output_dir,kTRUE)<0)
0027    { std::cerr<<"Failed to create "<<pub_output_dir<<std::endl ; exit(1) ; }
0028   else
0029    { std::cout<<"Creating "<<pub_output_dir<<std::endl ; }
0030 
0031   // open input file
0032   if (gSystem->CopyFile(pub_input_file.Data(),(pub_output_dir+"/"+pub_input_file).Data(),kTRUE)<0)
0033    { std::cerr<<"Failed to copy "<<pub_input_file<<std::endl ; exit(2) ; }
0034   else
0035    { std::cout<<"Input file is "<<pub_input_file<<std::endl ; }
0036   TFile * file = TFile::Open(pub_input_file) ;
0037   if (file!=0)
0038    {
0039     std::cout<<"Opening "<<pub_input_file<<std::endl ;
0040     if (file->cd(pub_input_folder)!=kTRUE)
0041      { std::cerr<<"Do not find "<<pub_input_folder<<std::endl ; exit(4) ; }
0042     else
0043      { std::cout<<"Input folder is "<<pub_input_folder<<std::endl ; }
0044    }
0045   else
0046    { std::cerr<<"Failed to open "<<pub_input_file<<std::endl ; exit(3) ; }
0047 
0048 
0049   // web page header
0050   std::ofstream web_page((pub_output_dir+"/index.html").Data()) ;
0051   web_page
0052     <<"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">\n"
0053     <<"<html>\n"
0054     <<"<head>\n"
0055     <<"<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\" />\n"
0056     <<"<title>"<<pub_title<<"</title>\n"
0057     <<"</head>\n"
0058     <<"<h1><a href=\"../\">"<<pub_title<<"</a></h1>\n" ;
0059   web_page<<"<p>"<<pub_comment ;
0060   web_page
0061     <<" They were made using configurations "
0062     <<"<a href=\"http://cmslxr.fnal.gov/lxr/source/DQMOffline/EGamma/test/ElectronAnalyzer_cfg.py\">"
0063     <<"DQMOffline/EGamma/test/ElectronAnalyzer_cfg.py</a> and "
0064     <<"<a href=\"http://cmslxr.fnal.gov/lxr/source/DQMOffline/EGamma/test/ElectronOfflineClient_cfg.py\">"
0065     <<"DQMOffline/EGamma/test/ElectronOfflineClient_cfg.py</a>." ;
0066   web_page
0067     <<" One can download the full <a href=\""<<pub_input_file<<"\"> histograms file</a>." ;
0068   web_page<<"</p>\n" ;
0069 
0070   // style
0071   TStyle *eleStyle = new TStyle("eleStyle","Style for electron dqm offline");
0072   eleStyle->SetCanvasBorderMode(0);
0073   eleStyle->SetCanvasColor(kWhite);
0074   eleStyle->SetCanvasDefH(600);
0075   eleStyle->SetCanvasDefW(800);
0076   eleStyle->SetCanvasDefX(0);
0077   eleStyle->SetCanvasDefY(0);
0078   eleStyle->SetPadBorderMode(0);
0079   eleStyle->SetPadColor(kWhite);
0080   eleStyle->SetPadGridX(false);
0081   eleStyle->SetPadGridY(false);
0082   eleStyle->SetGridColor(0);
0083   eleStyle->SetGridStyle(3);
0084   eleStyle->SetGridWidth(1);
0085   eleStyle->SetPadTickX(1);
0086   eleStyle->SetPadTickY(1);
0087   eleStyle->SetHistLineColor(1);
0088   eleStyle->SetHistLineStyle(0);
0089   eleStyle->SetHistLineWidth(2);
0090   eleStyle->SetEndErrorSize(2);
0091   eleStyle->SetErrorX(0.);
0092   eleStyle->SetTitleColor(1, "XYZ");
0093   eleStyle->SetTitleFont(42, "XYZ");
0094   eleStyle->SetTitleXOffset(1.0);
0095   eleStyle->SetTitleYOffset(1.0);
0096   eleStyle->SetLabelOffset(0.005, "XYZ");
0097   eleStyle->SetTitleSize(0.05, "XYZ");
0098   eleStyle->SetTitleFont(22,"X");
0099   eleStyle->SetTitleFont(22,"Y");
0100   eleStyle->SetHistLineWidth(2);
0101   eleStyle->SetPadBottomMargin(0.13);
0102   eleStyle->SetPadLeftMargin(0.15);
0103   eleStyle->SetMarkerStyle(21);
0104   eleStyle->SetMarkerSize(0.8);
0105   eleStyle->cd();
0106   gROOT->ForceStyle();
0107 
0108   // variables for the next loops
0109   int cat_num ;
0110   TList * keys1, * keys2 ;
0111   TKey * key1, * key2 ;
0112   TObject * obj1, * obj2 ;
0113   TDirectory * dir ;
0114   TH1 * histo ;
0115   TString short_histo_name, anchor_name, histo_option ;
0116   file->cd(pub_input_folder) ;
0117   keys1 = gDirectory->GetListOfKeys() ;
0118   TIter * nextKey1, * nextKey2 ;
0119 
0120   // top table
0121   std::cout<<"Writing top table"<<std::endl ;
0122   web_page
0123     <<"<br><table border=\"1\" cellpadding=\"5\" width=\"100%\">"
0124     <<"<tr valign=\"top\">\n" ;
0125   cat_num = 0 ;
0126   file->cd(pub_input_folder) ;
0127   keys1 = gDirectory->GetListOfKeys() ;
0128   nextKey1 = new TIter(keys1) ;
0129   while (key1 = (TKey *)(*nextKey1)())
0130    {
0131     obj1 = key1->ReadObj() ;
0132     if (obj1->IsA()->InheritsFrom("TDirectory")==kFALSE)
0133      { std::cout<<"Ignoring object "<<obj1->GetName()<<std::endl ; continue ; }
0134     else
0135      { std::cout<<"Processing folder "<<obj1->GetName()<<std::endl ; }
0136     dir = (TDirectory *)obj1 ;
0137     web_page<<"<td width=\"20%\"><b>"<<dir->GetName()<<"</b><br><br>\n" ;
0138     keys2 = dir->GetListOfKeys() ;
0139     nextKey2 = new TIter(keys2) ;
0140     while (key2 = (TKey *)(*nextKey2)())
0141      {
0142       obj2 = key2->ReadObj() ;
0143       if (obj2->IsA()->InheritsFrom("TH1")==kFALSE)
0144        { std::cout<<"Ignoring object "<<obj2->GetName()<<std::endl ; continue ; }
0145       short_histo_name = obj2->GetName() ;
0146       //short_histo_name.Remove(0,3) ;
0147       anchor_name = dir->GetName() ;
0148       anchor_name += "_" ;
0149       anchor_name += short_histo_name ;
0150       web_page<<"<a href=\"#"<<anchor_name<<"\">"<<short_histo_name<<"</a><br>\n" ;
0151      }
0152     web_page<<"<br></td>\n" ;
0153     cat_num++ ;
0154     if ((cat_num%5)==0)
0155      { web_page<<"</tr>\n<tr valign=\"top\">" ; }
0156    }
0157   web_page<<"</tr></table>\n" ;
0158 
0159   // histograms
0160   std::cout<<"Plotting histograms"<<std::endl ;
0161   gErrorIgnoreLevel = kWarning ;
0162   web_page<<"<br><br><table cellpadding=\"5\"><tr valign=\"top\"><td>\n" ;
0163   TCanvas * canvas ;
0164   TString left_histo_name, histo_name, gif_name, gif_path, canvas_name ;
0165   cat_num = 0 ;
0166   file->cd(pub_input_folder) ;
0167   keys1 = gDirectory->GetListOfKeys() ;
0168   nextKey1 = new TIter(keys1) ;
0169   while (key1 = (TKey *)(*nextKey1)())
0170    {
0171     obj1 = key1->ReadObj() ;
0172     if (obj1->IsA()->InheritsFrom("TDirectory")==kFALSE)
0173      { continue ; }
0174     dir = (TDirectory *)obj1 ;
0175     keys2 = dir->GetListOfKeys() ;
0176     nextKey2 = new TIter(keys2) ;
0177     while (key2 = (TKey *)(*nextKey2)())
0178      {
0179       obj2 = key2->ReadObj() ;
0180       if (obj2->IsA()->InheritsFrom("TH1")==kFALSE)
0181        { std::cout<<"Ignoring object "<<obj2->GetName()<<std::endl ; continue ; }
0182       histo = (TH1 *)obj2 ;
0183 
0184       std::cout
0185         <<dir->GetName()<<"/"<<histo->GetName()<<";"<<key2->GetCycle()
0186         <<" has "<<histo->GetEntries()<<" entries"
0187         <<" (~"<<histo->GetEffectiveEntries()<<")"
0188         <<" of mean value "<<histo->GetMean()
0189         <<std::endl ;
0190 
0191       histo_name = histo->GetName() ;
0192       if (left_histo_name.IsNull()==kFALSE)
0193        {
0194         if (histo_name.Index(left_histo_name)==0)
0195          { web_page<<"</td><td>" ; }
0196         else
0197          {
0198           left_histo_name = histo_name ;
0199           web_page<<"</td></tr>\n<tr valign=\"top\"><td>" ;
0200          }
0201        }
0202       else
0203        { left_histo_name = histo_name ; }
0204 
0205       short_histo_name = histo_name ;
0206       //short_histo_name.Remove(0,3) ;
0207       anchor_name = dir->GetName() ;
0208       anchor_name += "_" ;
0209       anchor_name += short_histo_name ;
0210       gif_name = anchor_name+".gif" ;
0211       gif_path = pub_output_dir+"/"+gif_name ;
0212       canvas_name = "c_" ;
0213       canvas_name += anchor_name ;
0214       canvas = new TCanvas(canvas_name) ;
0215       canvas->SetFillColor(10) ;
0216 
0217       histo->SetLineColor(2) ;
0218       histo->SetMarkerColor(2) ;
0219       histo->SetLineWidth(3) ;
0220 
0221       histo_option = histo->GetOption() ;
0222       if ((histo_option.Contains("ELE_LOGY")==kTRUE)&&(histo->GetEntries()>0)&&(histo->GetMaximum()>0))
0223        { canvas->SetLogy(1) ; }
0224 
0225       if (histo->IsA()->InheritsFrom("TH2")==kTRUE)
0226        {
0227         gStyle->SetPalette(1) ;
0228         gStyle->SetOptStat(111) ;
0229         histo->Draw(/*"COLZ"*/) ;
0230        }
0231       else if (histo->IsA()->InheritsFrom("TProfile")==kTRUE)
0232        {
0233         gStyle->SetOptStat(111) ;
0234         histo->Draw(/*"E1 P"*/) ;
0235        }
0236       else
0237        {
0238         gStyle->SetOptStat(111111) ;
0239         histo->Draw(/*"E1 P"*/) ;
0240        }
0241       canvas->SaveAs(gif_path.Data()) ;
0242 
0243       web_page
0244         <<"<a id=\""<<anchor_name<<"\" name=\""<<anchor_name<<"\"></a>"
0245         <<"<a href=\""<<gif_name<<"\"><img border=\"0\" class=\"image\" width=\"500\" src=\""<<gif_name<<"\"></a><br>\n" ;
0246      }
0247    }
0248   web_page<<"</td></tr></table>\n" ;
0249 
0250   // the end
0251   web_page<<"\n</html>"<<std::endl ;
0252   web_page.close() ;
0253   std::cout<<"Histos written to "<<pub_output_dir<<std::endl ;
0254   return 0 ;
0255  }