CMS 3D CMS Logo

Functions | Variables
storageSizeForBranch.cc File Reference
#include <iostream>
#include <boost/program_options.hpp>
#include "TClass.h"
#include "TFile.h"
#include "TBranch.h"
#include "TBufferFile.h"
#include "TTree.h"
#include "FWCore/FWLite/interface/FWLiteEnabler.h"
#include "FWCore/Utilities/interface/Exception.h"

Go to the source code of this file.

Functions

int main (int argc, char *argv[])
 

Variables

static char const *const kBranchNameOpt = "branchName"
 
static char const *const kEntryNumberCommandOpt = "entryNumber,e"
 
static char const *const kEntryNumberOpt = "entryNumber"
 
static char const *const kFileNameOpt = "fileName"
 
static char const *const kHelpCommandOpt = "help,h"
 
static char const *const kHelpOpt = "help"
 

Function Documentation

int main ( int  argc,
char *  argv[] 
)

Definition at line 36 of file storageSizeForBranch.cc.

References MicroEventContent_cff::branch, electrons_cff::branchName, MessageLogger_cfi::cerr, gather_cfg::cout, MillePedeFileConverter_cfg::e, FWLiteEnabler::enable(), relativeConstraints::error, cppFunctionSkipper::exception, cms::Exception::explainSelf(), FrontierConditions_GlobalTag_cff::file, MillePedeFileConverter_cfg::fileName, kBranchNameOpt, kEntryNumberCommandOpt, kEntryNumberOpt, kFileNameOpt, kHelpCommandOpt, kHelpOpt, AlcaSiPixelAliHarvester0T_cff::options, AlCaHLTBitMon_ParallelJobs::p, writedatasetfile::run, AlCaHLTBitMon_QueryRunRegistry::string, and cms::Exception::what().

36  {
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 }
virtual std::string explainSelf() const
Definition: Exception.cc:108
static char const *const kEntryNumberOpt
static char const *const kFileNameOpt
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

Variable Documentation

char const* const kBranchNameOpt = "branchName"
static

Definition at line 29 of file storageSizeForBranch.cc.

Referenced by main().

char const* const kEntryNumberCommandOpt = "entryNumber,e"
static

Definition at line 33 of file storageSizeForBranch.cc.

Referenced by main().

char const* const kEntryNumberOpt = "entryNumber"
static

Definition at line 34 of file storageSizeForBranch.cc.

Referenced by main().

char const* const kFileNameOpt = "fileName"
static

Definition at line 30 of file storageSizeForBranch.cc.

Referenced by main().

char const* const kHelpCommandOpt = "help,h"
static

Definition at line 32 of file storageSizeForBranch.cc.

Referenced by main().

char const* const kHelpOpt = "help"
static

Definition at line 31 of file storageSizeForBranch.cc.

Referenced by main().