CMS 3D CMS Logo

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