File indexing completed on 2021-06-11 04:37:10
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #include <iostream>
0015 #include <boost/program_options.hpp>
0016 #include "TClass.h"
0017 #include "TFile.h"
0018 #include "TBranch.h"
0019 #include "TBufferFile.h"
0020 #include "TTree.h"
0021
0022
0023 #include "FWCore/FWLite/interface/FWLiteEnabler.h"
0024 #include "FWCore/Utilities/interface/Exception.h"
0025
0026
0027
0028
0029 static char const* const kBranchNameOpt = "branchName";
0030 static char const* const kFileNameOpt = "fileName";
0031 static char const* const kHelpOpt = "help";
0032 static char const* const kHelpCommandOpt = "help,h";
0033 static char const* const kEntryNumberCommandOpt = "entryNumber,e";
0034 static char const* const kEntryNumberOpt = "entryNumber";
0035
0036 int main(int argc, char* argv[]) try {
0037 std::string descString(argv[0]);
0038 descString += " [options] [--";
0039 descString += kBranchNameOpt;
0040 descString += "] branch_name [--";
0041 descString += kFileNameOpt;
0042 descString +=
0043 "] file_name"
0044 "\n The program dumps information about how much storage space is needed to store a given TBranch within a ROOT "
0045 "file"
0046 "\nAllowed options";
0047 boost::program_options::options_description desc(descString);
0048 desc.add_options()(kHelpCommandOpt, "show this help message")(
0049 kEntryNumberCommandOpt,
0050 boost::program_options::value<int>(),
0051 "read branch from the given entry value (default 0)")(kBranchNameOpt, "name of branch")(kFileNameOpt,
0052 "name of file");
0053
0054 boost::program_options::positional_options_description p;
0055 p.add(kBranchNameOpt, 1);
0056 p.add(kFileNameOpt, 1);
0057
0058 boost::program_options::variables_map vm;
0059 try {
0060 store(boost::program_options::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
0061 notify(vm);
0062 } catch (boost::program_options::error const& iException) {
0063 std::cerr << "failed to parse command line \n" << iException.what() << "\n";
0064 return 1;
0065 }
0066
0067 if (vm.count(kHelpOpt)) {
0068 std::cout << desc << std::endl;
0069 return 0;
0070 }
0071 int entryNumber = 0;
0072 if (vm.count(kEntryNumberOpt)) {
0073 entryNumber = vm[kEntryNumberOpt].as<int>();
0074 }
0075
0076 if (!vm.count(kBranchNameOpt)) {
0077 std::cerr << "no branch name given\n";
0078 return 1;
0079 }
0080
0081 if (!vm.count(kFileNameOpt)) {
0082 std::cerr << "no branch name given\n";
0083 return 1;
0084 }
0085
0086 std::string branchName(vm[kBranchNameOpt].as<std::string>());
0087 std::string fileName(vm[kFileNameOpt].as<std::string>());
0088
0089 TFile* file = TFile::Open(fileName.c_str());
0090 if (nullptr == file) {
0091 std::cerr << "failed to open '" << fileName << "'";
0092 return 1;
0093 }
0094
0095 TTree* eventTree = dynamic_cast<TTree*>(file->Get("Events"));
0096
0097 if (nullptr == eventTree) {
0098 std::cerr << "The file '" << fileName << "' does not contain an 'Events' TTree";
0099 return 1;
0100 }
0101
0102 TBranch* branch = eventTree->GetBranch(branchName.c_str());
0103
0104 if (nullptr == branch) {
0105 std::cerr << "The Events TTree does not contain the branch " << branchName;
0106 return 1;
0107 }
0108
0109 FWLiteEnabler::enable();
0110
0111 TClass* cls = TClass::GetClass(branch->GetClassName());
0112 if (nullptr == cls) {
0113 std::cerr << "class '" << branch->GetClassName() << "' is unknown by ROOT\n";
0114 return 1;
0115 }
0116
0117 void* objInstance = cls->New();
0118 if (nullptr == objInstance) {
0119 std::cerr << "unable to create a default instance of the class " << branch->GetClassName();
0120 return 1;
0121 }
0122
0123
0124 void* pObjInstance = &objInstance;
0125
0126 branch->SetAddress(pObjInstance);
0127
0128 branch->GetEntry(entryNumber);
0129
0130 TBufferFile bf(TBuffer::kWrite);
0131
0132 gDebug = 3;
0133 cls->WriteBuffer(bf, objInstance);
0134
0135 gDebug = 0;
0136 std::cout << "Total amount stored: " << bf.Length() << " bytes" << std::endl;
0137 std::cout << "\nNOTE: add 4 bytes for each 'has written' value because of a bug in ROOT's printout of the accounting"
0138 << "\n Each class (inheriting or as member data) has metadata an overhead of 10 bytes" << std::endl;
0139 return 0;
0140 } catch (cms::Exception const& e) {
0141 std::cerr << e.explainSelf() << std::endl;
0142 return 1;
0143 } catch (std::exception const& e) {
0144 std::cerr << e.what() << std::endl;
0145 return 1;
0146 }