Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-06-11 04:40:30

0001 /** measure branch sizes

0002  *

0003  *

0004  */
0005 
0006 #include "PerfTools/EdmEvent/interface/EdmEventSize.h"
0007 
0008 #include <boost/program_options.hpp>
0009 #include <string>
0010 #include <iostream>
0011 #include <fstream>
0012 
0013 #include <TROOT.h>
0014 #include <TSystem.h>
0015 #include <TError.h>
0016 #include "FWCore/FWLite/interface/FWLiteEnabler.h"
0017 
0018 static const char* const kHelpOpt = "help";
0019 static const char* const kHelpCommandOpt = "help,h";
0020 static const char* const kDataFileOpt = "data-file";
0021 static const char* const kDataFileCommandOpt = "data-file,d";
0022 static const char* const kTreeNameOpt = "tree-name";
0023 static const char* const kTreeNameCommandOpt = "tree-name,n";
0024 static const char* const kOutputOpt = "output";
0025 static const char* const kOutputCommandOpt = "output,o";
0026 static const char* const kAutoLoadOpt = "auto-loader";
0027 static const char* const kAutoLoadCommandOpt = "auto-loader,a";
0028 static const char* const kPlotOpt = "plot";
0029 static const char* const kPlotCommandOpt = "plot,p";
0030 static const char* const kSavePlotOpt = "save-plot";
0031 static const char* const kSavePlotCommandOpt = "save-plot,s";
0032 static const char* const kPlotTopOpt = "plot-top";
0033 static const char* const kPlotTopCommandOpt = "plot-top,t";
0034 static const char* const kVerboseOpt = "verbose";
0035 static const char* const kVerboseCommandOpt = "verbose,v";
0036 static const char* const kAlphabeticOrderOpt = "alphabetic-order";
0037 static const char* const kAlphabeticOrderCommandOpt = "alphabetic-order,A";
0038 static const char* const kFormatNamesOpt = "format-names";
0039 static const char* const kFormatNamesCommandOpt = "format-names,F";
0040 
0041 int main(int argc, char* argv[]) {
0042   using namespace boost::program_options;
0043   using namespace std;
0044 
0045   string programName(argv[0]);
0046   string descString(programName);
0047   descString += " [options] ";
0048   descString += "data_file \nAllowed options";
0049   options_description desc(descString);
0050 
0051   // clang-format off

0052   desc.add_options()(kHelpCommandOpt, "produce help message")(kAutoLoadCommandOpt,
0053                                                               "automatic library loading (avoid root warnings)")(
0054       kDataFileCommandOpt, value<string>(), "data file")(
0055       kTreeNameCommandOpt, value<string>(), "tree name (default \"Events\")")(
0056       kOutputCommandOpt, value<string>(), "output file")(kAlphabeticOrderCommandOpt,
0057                                                          "sort by alphabetic order (default: sort by size)")(
0058       kFormatNamesCommandOpt, "format product name as \"product:label (type)\" (default: use full branch name)")(
0059       kPlotCommandOpt, value<string>(), "produce a summary plot")(
0060       kPlotTopCommandOpt, value<int>(), "plot only the <arg> top size branches")(
0061       kSavePlotCommandOpt, value<string>(), "save plot into root file <arg>")(kVerboseCommandOpt, "verbose printout");
0062   // clang-format on

0063 
0064   positional_options_description p;
0065 
0066   p.add(kDataFileOpt, -1);
0067 
0068   variables_map vm;
0069   try {
0070     store(command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
0071     notify(vm);
0072   } catch (const error&) {
0073     return 7000;
0074   }
0075 
0076   if (vm.count(kHelpOpt)) {
0077     cout << desc << std::endl;
0078     return 0;
0079   }
0080 
0081   if (!vm.count(kDataFileOpt)) {
0082     cerr << programName << ": no data file given" << endl;
0083     return 7001;
0084   }
0085 
0086   gROOT->SetBatch();
0087 
0088   if (vm.count(kAutoLoadOpt) != 0) {
0089     gSystem->Load("libFWCoreFWLite");
0090     FWLiteEnabler::enable();
0091   } else
0092     gErrorIgnoreLevel = kError;
0093 
0094   bool verbose = vm.count(kVerboseOpt) > 0;
0095 
0096   std::string fileName = vm[kDataFileOpt].as<string>();
0097 
0098   std::string treeName = "Events";
0099   if (vm.count(kTreeNameOpt))
0100     treeName = vm[kTreeNameOpt].as<string>();
0101 
0102   perftools::EdmEventSize me;
0103 
0104   try {
0105     me.parseFile(fileName, treeName);
0106   } catch (perftools::EdmEventSize::Error const& error) {
0107     std::cerr << programName << ":" << error.descr << std::endl;
0108     return error.code;
0109   }
0110 
0111   if (vm.count(kFormatNamesOpt))
0112     me.formatNames();
0113 
0114   if (vm.count(kAlphabeticOrderOpt))
0115     me.sortAlpha();
0116 
0117   if (verbose) {
0118     std::cout << std::endl;
0119     me.dump(std::cout);
0120     std::cout << std::endl;
0121   }
0122 
0123   if (vm.count(kOutputOpt)) {
0124     std::ofstream of(vm[kOutputOpt].as<std::string>().c_str());
0125     me.dump(of);
0126     of << std::endl;
0127   }
0128 
0129   bool plot = (vm.count(kPlotOpt) > 0);
0130   bool save = (vm.count(kSavePlotOpt) > 0);
0131   if (plot || save) {
0132     std::string plotName;
0133     std::string histName;
0134     if (plot)
0135       plotName = vm[kPlotOpt].as<string>();
0136     if (save)
0137       histName = vm[kSavePlotOpt].as<string>();
0138     int top = 0;
0139     if (vm.count(kPlotTopOpt) > 0)
0140       top = vm[kPlotTopOpt].as<int>();
0141     me.produceHistos(plotName, histName, top);
0142   }
0143 
0144   return 0;
0145 }