Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:12:31

0001 // -*- C++ -*-
0002 //
0003 // Package:     FWLite
0004 // Class  :     storageSize
0005 //
0006 // Implementation:
0007 //     [Notes on implementation]
0008 //
0009 // Original Author:  Chris Jones
0010 //         Created:  Thu Jan 20 09:50:58 CST 2011
0011 //
0012 
0013 // system include files
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 // user include files
0023 #include "FWCore/FWLite/interface/FWLiteEnabler.h"
0024 #include "FWCore/Utilities/interface/Exception.h"
0025 
0026 //
0027 // constants, enums and typedefs
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")(kEntryNumberCommandOpt,
0049                                                                 boost::program_options::value<int>(),
0050                                                                 "read branch from the given entry value (default 0)")(
0051       kBranchNameOpt, "name of branch")(kFileNameOpt, "name of file");
0052 
0053   boost::program_options::positional_options_description p;
0054   p.add(kBranchNameOpt, 1);
0055   p.add(kFileNameOpt, 1);
0056 
0057   boost::program_options::variables_map vm;
0058   try {
0059     store(boost::program_options::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
0060     notify(vm);
0061   } catch (boost::program_options::error const& iException) {
0062     std::cerr << "failed to parse command line \n" << iException.what() << "\n";
0063     return 1;
0064   }
0065 
0066   if (vm.count(kHelpOpt)) {
0067     std::cout << desc << std::endl;
0068     return 0;
0069   }
0070   int entryNumber = 0;
0071   if (vm.count(kEntryNumberOpt)) {
0072     entryNumber = vm[kEntryNumberOpt].as<int>();
0073   }
0074 
0075   if (!vm.count(kBranchNameOpt)) {
0076     std::cerr << "no branch name given\n";
0077     return 1;
0078   }
0079 
0080   if (!vm.count(kFileNameOpt)) {
0081     std::cerr << "no branch name given\n";
0082     return 1;
0083   }
0084 
0085   std::string branchName(vm[kBranchNameOpt].as<std::string>());
0086   std::string fileName(vm[kFileNameOpt].as<std::string>());
0087 
0088   TFile* file = TFile::Open(fileName.c_str());
0089   if (nullptr == file) {
0090     std::cerr << "failed to open '" << fileName << "'";
0091     return 1;
0092   }
0093 
0094   TTree* eventTree = dynamic_cast<TTree*>(file->Get("Events"));
0095 
0096   if (nullptr == eventTree) {
0097     std::cerr << "The file '" << fileName << "' does not contain an 'Events' TTree";
0098     return 1;
0099   }
0100 
0101   TBranch* branch = eventTree->GetBranch(branchName.c_str());
0102 
0103   if (nullptr == branch) {
0104     std::cerr << "The Events TTree does not contain the branch " << branchName;
0105     return 1;
0106   }
0107 
0108   FWLiteEnabler::enable();
0109 
0110   TClass* cls = TClass::GetClass(branch->GetClassName());
0111   if (nullptr == cls) {
0112     std::cerr << "class '" << branch->GetClassName() << "' is unknown by ROOT\n";
0113     return 1;
0114   }
0115 
0116   void* objInstance = cls->New();
0117   if (nullptr == objInstance) {
0118     std::cerr << "unable to create a default instance of the class " << branch->GetClassName();
0119     return 1;
0120   }
0121 
0122   //associate this with the branch
0123   void* pObjInstance = &objInstance;
0124 
0125   branch->SetAddress(pObjInstance);
0126 
0127   branch->GetEntry(entryNumber);
0128 
0129   TBufferFile bf(TBuffer::kWrite);
0130 
0131   gDebug = 3;
0132   cls->WriteBuffer(bf, objInstance);
0133 
0134   gDebug = 0;
0135   std::cout << "Total amount stored: " << bf.Length() << " bytes" << std::endl;
0136   std::cout << "\nNOTE: add 4 bytes for each 'has written' value because of a bug in ROOT's printout of the accounting"
0137             << "\n  Each class (inheriting or as member data) has metadata an overhead of 10 bytes" << std::endl;
0138   return 0;
0139 } catch (cms::Exception const& e) {
0140   std::cerr << e.explainSelf() << std::endl;
0141   return 1;
0142 } catch (std::exception const& e) {
0143   std::cerr << e.what() << std::endl;
0144   return 1;
0145 }