CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_2_9_HLT1_bphpatch4/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/shared_ptr.hpp"
00018 #include "boost/utility.hpp"
00019 
00020 #include <memory>
00021 #include <map>
00022 #include <string>
00023 #include <vector>
00024 
00025 class TBranch;
00026 class TClass;
00027 class TTree;
00028 class TTreeCache;
00029 
00030 namespace edm {
00031   struct BranchKey;
00032   class FileFormatVersion;
00033   class InputFile;
00034   class RootDelayedReader;
00035   class RootFile;
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         offsetToEDProduct_(0) {}
00051       ConstBranchDescription branchDescription_;
00052       TBranch* productBranch_;
00053       TBranch* provenanceBranch_; // For backward compatibility
00054       mutable TClass* classCache_;
00055       mutable Int_t offsetToEDProduct_;
00056     };
00057     typedef std::map<BranchKey const, BranchInfo> BranchMap;
00058     Int_t getEntry(TBranch* branch, EntryNumber entryNumber);
00059     Int_t getEntry(TTree* tree, EntryNumber entryNumber);
00060     std::auto_ptr<TTreeCache> trainCache(TTree* tree, InputFile& file, unsigned int cacheSize, char const* branchNames);
00061   }
00062 
00063   class RootTree : private boost::noncopyable {
00064   public:
00065     typedef roottree::BranchMap BranchMap;
00066     typedef roottree::EntryNumber EntryNumber;
00067     RootTree(boost::shared_ptr<InputFile> filePtr,
00068              BranchType const& branchType,
00069              unsigned int maxVirtualSize,
00070              unsigned int cacheSize,
00071              unsigned int learningEntries);
00072     ~RootTree();
00073 
00074     bool isValid() const;
00075     void addBranch(BranchKey const& key,
00076                    BranchDescription const& prod,
00077                    std::string const& oldBranchName);
00078     void dropBranch(std::string const& oldBranchName);
00079     void getEntry(TBranch *branch, EntryNumber entry) const;
00080     void setPresence(BranchDescription const& prod);
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     boost::shared_ptr<DelayedReader> makeDelayedReader(FileFormatVersion const& fileFormatVersion, boost::shared_ptr<RootFile> rootFilePtr = boost::shared_ptr<RootFile>()) 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     std::vector<ProductStatus> const& productStatuses() const {return productStatuses_;} // backward compatibility
00118 
00119     // below for backward compatibility
00120     void fillStatus() { // backward compatibility
00121       statusBranch_->SetAddress(&pProductStatuses_); // backward compatibility
00122       roottree::getEntry(statusBranch_, entryNumber_); // backward compatibility
00123     } // backward compatibility
00124 
00125     TBranch* const branchEntryInfoBranch() const {return branchEntryInfoBranch_;}
00126     void trainCache(char const* branchNames);
00127     void resetTraining() {trainNow_ = true;}
00128 
00129   private:
00130     void setCacheSize(unsigned int cacheSize);
00131     void setTreeMaxVirtualSize(int treeMaxVirtualSize);
00132     void startTraining();
00133     void stopTraining();
00134 
00135     boost::shared_ptr<InputFile> filePtr_;
00136 // We use bare pointers for pointers to some ROOT entities.
00137 // Root owns them and uses bare pointers internally.
00138 // Therefore,using smart pointers here will do no good.
00139     TTree* tree_;
00140     TTree* metaTree_;
00141     BranchType branchType_;
00142     TBranch* auxBranch_;
00143     TBranch* branchEntryInfoBranch_;
00144 // We use a smart pointer to own the TTreeCache.
00145 // Unfortunately, ROOT owns it when attached to a TFile, but not after it is detached.
00146 // So, we make sure to it is detached before closing the TFile so there is no double delete.
00147     boost::shared_ptr<TTreeCache> treeCache_;
00148     boost::shared_ptr<TTreeCache> rawTreeCache_;
00149     EntryNumber entries_;
00150     EntryNumber entryNumber_;
00151     std::vector<std::string> branchNames_;
00152     boost::shared_ptr<BranchMap> branches_;
00153     bool trainNow_;
00154     EntryNumber switchOverEntry_;
00155     unsigned int learningEntries_;
00156     unsigned int cacheSize_;
00157 
00158     // below for backward compatibility
00159     std::vector<ProductStatus> productStatuses_; // backward compatibility
00160     std::vector<ProductStatus>* pProductStatuses_; // backward compatibility
00161     TTree* infoTree_; // backward compatibility
00162     TBranch* statusBranch_; // backward compatibility
00163   };
00164 }
00165 #endif