CMS 3D CMS Logo

storageSizeForBranch.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 "TFile.h"
18 #include "TBranch.h"
19 #include "TBufferFile.h"
20 #include "TTree.h"
21 
22 // user include files
25 
26 //
27 // constants, enums and typedefs
28 //
29 static char const* const kBranchNameOpt = "branchName";
30 static char const* const kFileNameOpt = "fileName";
31 static char const* const kHelpOpt = "help";
32 static char const* const kHelpCommandOpt = "help,h";
33 static char const* const kEntryNumberCommandOpt = "entryNumber,e";
34 static char const* const kEntryNumberOpt = "entryNumber";
35 
36 int main(int argc, char* argv[]) try {
37  std::string descString(argv[0]);
38  descString += " [options] [--";
39  descString += kBranchNameOpt;
40  descString += "] branch_name [--";
41  descString += kFileNameOpt;
42  descString +=
43  "] file_name"
44  "\n The program dumps information about how much storage space is needed to store a given TBranch within a ROOT "
45  "file"
46  "\nAllowed options";
47  boost::program_options::options_description desc(descString);
48  desc.add_options()(kHelpCommandOpt, "show this help message")(kEntryNumberCommandOpt,
49  boost::program_options::value<int>(),
50  "read branch from the given entry value (default 0)")(
51  kBranchNameOpt, "name of branch")(kFileNameOpt, "name of file");
52 
53  boost::program_options::positional_options_description p;
54  p.add(kBranchNameOpt, 1);
55  p.add(kFileNameOpt, 1);
56 
57  boost::program_options::variables_map vm;
58  try {
59  store(boost::program_options::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
60  notify(vm);
61  } catch (boost::program_options::error const& iException) {
62  std::cerr << "failed to parse command line \n" << iException.what() << "\n";
63  return 1;
64  }
65 
66  if (vm.count(kHelpOpt)) {
67  std::cout << desc << std::endl;
68  return 0;
69  }
70  int entryNumber = 0;
71  if (vm.count(kEntryNumberOpt)) {
72  entryNumber = vm[kEntryNumberOpt].as<int>();
73  }
74 
75  if (!vm.count(kBranchNameOpt)) {
76  std::cerr << "no branch name given\n";
77  return 1;
78  }
79 
80  if (!vm.count(kFileNameOpt)) {
81  std::cerr << "no branch name given\n";
82  return 1;
83  }
84 
85  std::string branchName(vm[kBranchNameOpt].as<std::string>());
86  std::string fileName(vm[kFileNameOpt].as<std::string>());
87 
88  TFile* file = TFile::Open(fileName.c_str());
89  if (nullptr == file) {
90  std::cerr << "failed to open '" << fileName << "'";
91  return 1;
92  }
93 
94  TTree* eventTree = dynamic_cast<TTree*>(file->Get("Events"));
95 
96  if (nullptr == eventTree) {
97  std::cerr << "The file '" << fileName << "' does not contain an 'Events' TTree";
98  return 1;
99  }
100 
101  TBranch* branch = eventTree->GetBranch(branchName.c_str());
102 
103  if (nullptr == branch) {
104  std::cerr << "The Events TTree does not contain the branch " << branchName;
105  return 1;
106  }
107 
109 
110  TClass* cls = TClass::GetClass(branch->GetClassName());
111  if (nullptr == cls) {
112  std::cerr << "class '" << branch->GetClassName() << "' is unknown by ROOT\n";
113  return 1;
114  }
115 
116  void* objInstance = cls->New();
117  if (nullptr == objInstance) {
118  std::cerr << "unable to create a default instance of the class " << branch->GetClassName();
119  return 1;
120  }
121 
122  //associate this with the branch
123  void* pObjInstance = &objInstance;
124 
125  branch->SetAddress(pObjInstance);
126 
127  branch->GetEntry(entryNumber);
128 
129  TBufferFile bf(TBuffer::kWrite);
130 
131  gDebug = 3;
132  cls->WriteBuffer(bf, objInstance);
133 
134  gDebug = 0;
135  std::cout << "Total amount stored: " << bf.Length() << " bytes" << std::endl;
136  std::cout << "\nNOTE: add 4 bytes for each 'has written' value because of a bug in ROOT's printout of the accounting"
137  << "\n Each class (inheriting or as member data) has metadata an overhead of 10 bytes" << std::endl;
138  return 0;
139 } catch (cms::Exception const& e) {
140  std::cerr << e.explainSelf() << std::endl;
141  return 1;
142 } catch (std::exception const& e) {
143  std::cerr << e.what() << std::endl;
144  return 1;
145 }
int main(int argc, char *argv[])
virtual std::string explainSelf() const
Definition: Exception.cc:108
static char const *const kEntryNumberOpt
static char const *const kFileNameOpt
char const * what() const override
Definition: Exception.cc:103
static void enable()
enable automatic library loading
static char const *const kBranchNameOpt
static char const *const kEntryNumberCommandOpt
static char const *const kHelpCommandOpt
static char const *const kHelpOpt