CMS 3D CMS Logo

storageSize.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: FWLite
4 // Class : storageSize
5 //
6 // Implementation:
7 // [Notes on implementation]
8 //
9 // Original Author: Chris Jones
10 // Created: Thu Jan 20 09:50:58 CST 2011
11 //
12 
13 // system include files
14 #include <iostream>
15 #include <boost/program_options.hpp>
16 #include "TClass.h"
17 #include "TBufferFile.h"
18 
19 // user include files
22 
23 //
24 // constants, enums and typedefs
25 //
26 static char const* const kClassNameOpt = "className";
27 static char const* const kHelpOpt = "help";
28 static char const* const kHelpCommandOpt = "help,h";
29 
30 int main(int argc, char* argv[]) try {
31  std::string descString(argv[0]);
32  descString += " [options] [--";
33  descString += kClassNameOpt;
34  descString +=
35  "] class_name"
36  "\n The program dumps information about how much storage space is needed to store the class"
37  "\nAllowed options";
38  boost::program_options::options_description desc(descString);
39  desc.add_options()(kHelpCommandOpt, "show this help message")(kClassNameOpt, "name of class");
40 
41  boost::program_options::positional_options_description p;
42  p.add(kClassNameOpt, 1);
43 
44  boost::program_options::variables_map vm;
45  try {
46  store(boost::program_options::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
47  notify(vm);
48  } catch (boost::program_options::error const& iException) {
49  std::cerr << "failed to parse command line \n" << iException.what() << "\n";
50  return 1;
51  }
52 
53  if (vm.count(kHelpOpt)) {
54  std::cout << desc << std::endl;
55  return 0;
56  }
57 
58  if (!vm.count(kClassNameOpt)) {
59  std::cerr << "no class name given\n";
60  return 1;
61  }
62 
63  std::string className(vm[kClassNameOpt].as<std::string>());
64 
66 
67  TClass* cls = TClass::GetClass(className.c_str());
68  if (nullptr == cls) {
69  std::cerr << "class '" << className << "' is unknown by ROOT\n";
70  return 1;
71  }
72 
73  void* objInstance = cls->New();
74  if (nullptr == objInstance) {
75  std::cerr << "unable to create a default instance of the class " << className;
76  return 1;
77  }
78 
79  TBufferFile bf(TBuffer::kWrite);
80 
81  gDebug = 3;
82  cls->WriteBuffer(bf, objInstance);
83 
84  gDebug = 0;
85  std::cout << "Total amount stored: " << bf.Length() << " bytes" << std::endl;
86  std::cout << "\nNOTE: add 4 bytes for each 'has written' value because of a bug in ROOT's printout of the accounting"
87  << "\n Each class (inheriting or as member data) has metadata an overhead of 10 bytes" << std::endl;
88  return 0;
89 } catch (cms::Exception const& e) {
90  std::cerr << e.explainSelf() << std::endl;
91  return 1;
92 } catch (std::exception const& e) {
93  std::cerr << e.what() << std::endl;
94  return 1;
95 }
static char const *const kHelpCommandOpt
Definition: storageSize.cc:28
virtual std::string explainSelf() const
Definition: Exception.cc:108
char const * what() const override
Definition: Exception.cc:103
static char const *const kClassNameOpt
Definition: storageSize.cc:26
int main(int argc, char *argv[])
Definition: storageSize.cc:30
static void enable()
enable automatic library loading
static char const *const kHelpOpt
Definition: storageSize.cc:27
std::string className(const T &t)
Definition: ClassName.h:30