Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-02-13 02:58:24

0001 #ifndef PerfTools_EdmEventSize_H
0002 #define PerfTools_EdmEventSize_H
0003 
0004 #include <string>
0005 #include <vector>
0006 #include <iosfwd>
0007 
0008 namespace perftools {
0009 
0010   /** \class EdmEventSize
0011    *  Measure the size of each product in an edm::event
0012    *  Provides the output as an ascii table, json file or root histograms
0013 
0014    *  Based on the original implementation by Luca Lista
0015    *
0016    *  Algorithm:
0017    *  Measure the size of each branch in a tree as the sum of the sizes of 
0018    *  all its baskets
0019    *  Estimate the "size in memory" multipling the actual branch size 
0020    *  by its compression factor
0021    *  Optionally, measure the size of each leaf in a branch and calculate
0022    *  the overhead of the branch size with respect to the sum of the leaf sizes
0023    *
0024    *  \author Vincenzo Innocente
0025    *  \author Simone Rossi Tisbeni
0026    */
0027 
0028   enum class EdmEventMode { Branches, Leaves };
0029 
0030   template <EdmEventMode M>
0031   class EdmEventSize {
0032   public:
0033     enum class Format { text, json };
0034 
0035     /// generic exception
0036     struct Error {
0037       Error(std::string const& idescr, int icode) : descr(idescr), code(icode) {}
0038       std::string descr;
0039       int code;
0040     };
0041 
0042     /// the information for each branch
0043     struct Record {
0044       Record() : compr_size(0.), uncompr_size(0.) {}
0045       Record(std::string const& iname, size_t inEvents, size_t compr, size_t uncompr)
0046           : name(iname), nEvents(inEvents), compr_size(compr), uncompr_size(uncompr) {
0047         if constexpr (M == EdmEventMode::Branches) {
0048           type = name;
0049         } else if constexpr (M == EdmEventMode::Leaves) {
0050           if (name.find('.') == std::string::npos) {
0051             type = name;
0052             label = "";
0053           } else {
0054             type = name.substr(0, name.find('.'));
0055             label = name.substr(name.find('.') + 1);
0056           }
0057         }
0058       }
0059       std::string name;
0060       std::string type;
0061       std::string label;
0062       size_t nEvents;
0063       size_t compr_size;
0064       size_t uncompr_size;
0065     };
0066 
0067     typedef std::vector<Record> Records;
0068 
0069     /// Constructor
0070     EdmEventSize();
0071     /// Constructor and parse
0072     explicit EdmEventSize(std::string const& fileName, std::string const& treeName = "Events");
0073 
0074     /// read file, compute branch size, sort by size
0075     void parseFile(std::string const& fileName, std::string const& treeName = "Events");
0076 
0077     /// sort by name
0078     void sortAlpha();
0079 
0080     /// transform Branch names in "formatted" prodcut identifiers
0081     void formatNames();
0082 
0083     /// dump the ascii table on "co"
0084     void dump(std::ostream& co, bool header = true) const;
0085 
0086     /// dump the json table on "co"
0087     void dumpJson(std::ostream& co) const;
0088 
0089     /// produce histograms and optionally write them in "file" or as "plot"
0090     void produceHistos(std::string const& plot, std::string const& file, int top = 0) const;
0091 
0092   private:
0093     std::string m_fileName;
0094     int m_nEvents;
0095     Records m_records;
0096   };
0097 
0098   extern template class perftools::EdmEventSize<perftools::EdmEventMode::Leaves>;
0099   extern template class perftools::EdmEventSize<perftools::EdmEventMode::Branches>;
0100 }  // namespace perftools
0101 
0102 #endif  // PerfTools_EdmEventSize_H