CMS 3D CMS Logo

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