CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/IOPool/Input/src/RootTree.h

Go to the documentation of this file.
00001 #ifndef IOPool_Input_RootTree_h
00002 #define IOPool_Input_RootTree_h
00003 
00004 /*----------------------------------------------------------------------
00005 
00006 RootTree.h // used by ROOT input sources
00007 
00008 ----------------------------------------------------------------------*/
00009 
00010 #include "DataFormats/Provenance/interface/ConstBranchDescription.h"
00011 #include "DataFormats/Provenance/interface/ProvenanceFwd.h"
00012 #include "FWCore/Framework/interface/Frameworkfwd.h"
00013 
00014 #include "Rtypes.h"
00015 #include "TBranch.h"
00016 
00017 #include "boost/scoped_ptr.hpp"
00018 #include "boost/shared_ptr.hpp"
00019 #include "boost/utility.hpp"
00020 
00021 #include <memory>
00022 #include <map>
00023 #include <string>
00024 #include <vector>
00025 
00026 class TBranch;
00027 class TClass;
00028 class TTree;
00029 class TTreeCache;
00030 
00031 namespace edm {
00032   struct BranchKey;
00033   class DelayedReader;
00034   class FileFormatVersion;
00035   class InputFile;
00036   class RootTree;
00037 
00038   namespace roottree {
00039     unsigned int const defaultCacheSize = 20U * 1024 * 1024;
00040     unsigned int const defaultNonEventCacheSize = 1U * 1024 * 1024;
00041     unsigned int const defaultLearningEntries = 20U;
00042     unsigned int const defaultNonEventLearningEntries = 1U;
00043     typedef Long64_t EntryNumber;
00044     struct BranchInfo {
00045       BranchInfo(ConstBranchDescription const& prod) :
00046         branchDescription_(prod),
00047         productBranch_(0),
00048         provenanceBranch_(0),
00049         classCache_(0) {}
00050       ConstBranchDescription branchDescription_;
00051       TBranch* productBranch_;
00052       TBranch* provenanceBranch_; // For backward compatibility
00053       mutable TClass* classCache_;
00054     };
00055     typedef std::map<BranchKey const, BranchInfo> BranchMap;
00056     Int_t getEntry(TBranch* branch, EntryNumber entryNumber);
00057     Int_t getEntry(TTree* tree, EntryNumber entryNumber);
00058     std::auto_ptr<TTreeCache> trainCache(TTree* tree, InputFile& file, unsigned int cacheSize, char const* branchNames);
00059   }
00060 
00061   class RootTree : private boost::noncopyable {
00062   public:
00063     typedef roottree::BranchMap BranchMap;
00064     typedef roottree::EntryNumber EntryNumber;
00065     RootTree(boost::shared_ptr<InputFile> filePtr,
00066              FileFormatVersion const& fileFormatVersion,
00067              BranchType const& branchType,
00068              unsigned int maxVirtualSize,
00069              unsigned int cacheSize,
00070              unsigned int learningEntries);
00071     ~RootTree();
00072 
00073     bool isValid() const;
00074     void addBranch(BranchKey const& key,
00075                    BranchDescription const& prod,
00076                    std::string const& oldBranchName);
00077     void dropBranch(std::string const& oldBranchName);
00078     void getEntry(TBranch *branch, EntryNumber entry) const;
00079     void setPresence(BranchDescription const& prod);
00080     bool next() {return ++entryNumber_ < entries_;}
00081     bool previous() {return --entryNumber_ >= 0;}
00082     bool current() {return entryNumber_ < entries_ && entryNumber_ >= 0;}
00083     void rewind() {entryNumber_ = 0;}
00084     void close();
00085     EntryNumber const& entryNumber() const {return entryNumber_;}
00086     EntryNumber const& entries() const {return entries_;}
00087     void setEntryNumber(EntryNumber theEntryNumber);
00088     std::vector<std::string> const& branchNames() const {return branchNames_;}
00089     DelayedReader* rootDelayedReader() const;
00090     template <typename T>
00091     void fillAux(T*& pAux) {
00092       auxBranch_->SetAddress(&pAux);
00093       getEntry(auxBranch_, entryNumber_);
00094     }
00095     template <typename T>
00096     void fillBranchEntryMeta(TBranch* branch, T*& pbuf) {
00097       if (metaTree_ != 0) {
00098         // Metadata was in separate tree.  Not cached.
00099         branch->SetAddress(&pbuf);
00100         roottree::getEntry(branch, entryNumber_);
00101       } else {
00102         fillBranchEntry<T>(branch, pbuf);
00103       }
00104     }
00105 
00106     template <typename T>
00107     void fillBranchEntry(TBranch* branch, T*& pbuf) {
00108       branch->SetAddress(&pbuf);
00109       getEntry(branch, entryNumber_);
00110     }
00111 
00112     TTree const* tree() const {return tree_;}
00113     TTree* tree() {return tree_;}
00114     TTree const* metaTree() const {return metaTree_;}
00115     BranchMap const& branches() const;
00116 
00117     //For backwards compatibility
00118     TBranch* const branchEntryInfoBranch() const {return branchEntryInfoBranch_;}
00119     
00120     void trainCache(char const* branchNames);
00121     void resetTraining() {trainNow_ = true;}
00122 
00123     BranchType branchType() const {return branchType_;}
00124   private:
00125     void setCacheSize(unsigned int cacheSize);
00126     void setTreeMaxVirtualSize(int treeMaxVirtualSize);
00127     void startTraining();
00128     void stopTraining();
00129 
00130     boost::shared_ptr<InputFile> filePtr_;
00131 // We use bare pointers for pointers to some ROOT entities.
00132 // Root owns them and uses bare pointers internally.
00133 // Therefore,using smart pointers here will do no good.
00134     TTree* tree_;
00135     TTree* metaTree_;
00136     BranchType branchType_;
00137     TBranch* auxBranch_;
00138 // We use a smart pointer to own the TTreeCache.
00139 // Unfortunately, ROOT owns it when attached to a TFile, but not after it is detached.
00140 // So, we make sure to it is detached before closing the TFile so there is no double delete.
00141     boost::shared_ptr<TTreeCache> treeCache_;
00142     boost::shared_ptr<TTreeCache> rawTreeCache_;
00143     EntryNumber entries_;
00144     EntryNumber entryNumber_;
00145     std::vector<std::string> branchNames_;
00146     boost::shared_ptr<BranchMap> branches_;
00147     bool trainNow_;
00148     EntryNumber switchOverEntry_;
00149     unsigned int learningEntries_;
00150     unsigned int cacheSize_;
00151     boost::scoped_ptr<DelayedReader> rootDelayedReader_;
00152 
00153     TBranch* branchEntryInfoBranch_; //backwards compatibility
00154     // below for backward compatibility
00155     TTree* infoTree_; // backward compatibility
00156     TBranch* statusBranch_; // backward compatibility
00157   };
00158 }
00159 #endif