CMS 3D CMS Logo

BranchMapReader.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:     FWLite
00004 // Class  :     BranchMapReader
00005 // 
00006 // Implementation:
00007 //     <Notes on implementation>
00008 //
00009 // Original Author:  Dan Riley
00010 //         Created:  Tue May 20 10:31:32 EDT 2008
00011 // $Id: BranchMapReader.cc,v 1.4 2008/09/24 15:25:31 dsr Exp $
00012 //
00013 
00014 // system include files
00015 
00016 #include "TFile.h"
00017 #include "TTree.h"
00018 #include "TBranch.h"
00019 #include "TClass.h"
00020 #include "Reflex/Type.h"
00021 #include "TROOT.h"
00022 
00023 // user include files
00024 #include "FWCore/FWLite/interface/BranchMapReader.h"
00025 #include "DataFormats/Provenance/interface/ProductRegistry.h"
00026 #include "DataFormats/Provenance/interface/FileFormatVersion.h"
00027 #include "DataFormats/Provenance/interface/BranchType.h"
00028 #include "DataFormats/Provenance/interface/ProductStatus.h"
00029 #include "FWCore/Utilities/interface/WrappedClassName.h"
00030 #include "FWCore/Utilities/interface/Exception.h"
00031 
00032 namespace fwlite {
00033   static BranchMapReader::eeiMap emptyMapper;
00034 
00035   namespace internal {
00036     class BranchMapReaderStrategyV1 : public fwlite::BranchMapReader::Strategy {
00037     public:
00038       BranchMapReaderStrategyV1(TFile* file, int fileVersion, BranchMapReader::eeiMap& eventInfoMap, 
00039                                 BranchMapReader::bidToDesc& branchDescriptionMap);
00040       virtual bool updateFile(TFile* file);
00041       virtual bool updateMap();
00042     private:
00043     };
00044 
00045     BranchMapReaderStrategyV1::BranchMapReaderStrategyV1(TFile* file, int fileVersion,
00046       BranchMapReader::eeiMap& eventInfoMap,
00047       BranchMapReader::bidToDesc& branchDescriptionMap)
00048       : Strategy(file, fileVersion, eventInfoMap, branchDescriptionMap)
00049     {
00050       updateFile(file);
00051     }
00052 
00053     bool BranchMapReaderStrategyV1::updateFile(TFile* file)
00054     {
00055       if (BranchMapReader::Strategy::updateFile(file)) {
00056         mapperFilled_ = false;
00057         return true;        
00058       }
00059       return false;
00060     }
00061     
00062     bool BranchMapReaderStrategyV1::updateMap()
00063     {
00064       if (mapperFilled_) {
00065         return true;
00066       }
00067 
00068       eventInfoMap_ = emptyMapper;
00069       branchDescriptionMap_.clear();
00070       
00071       edm::ProductRegistry reg;
00072       edm::ProductRegistry* pReg = &reg;
00073       TBranch* br = getBranchRegistry(&pReg);
00074 
00075       if (0 != br) {
00076         const edm::ProductRegistry::ProductList& prodList = reg.productList();
00077 
00078         for(edm::ProductRegistry::ProductList::const_iterator it = prodList.begin(), itEnd = prodList.end(); it != itEnd; ++it) {
00079           if (edm::InEvent == it->second.branchType()) {
00080             edm::ProductStatus status = edm::productstatus::uninitialized();
00081             // call to regenerate branchName
00082             it->second.init();
00083                 edm::EventEntryInfo entry(it->second.branchID(), status, it->second.oldProductID());
00084                 eventInfoMap_.insert(entry);
00085             branchDescriptionMap_.insert(BranchMapReader::bidToDesc::value_type(it->second.branchID(), it->second));
00086             // std::cout << "v1 updatemap " << it->second.branchID() << std::endl;
00087                 }
00088               }
00089         mapperFilled_ = true;
00090       }
00091       return 0 != br;
00092     }
00093 
00094     // v7 has differences in product status that are not implemented in BranchMapReader yet
00095     class BranchMapReaderStrategyV7 : public BranchMapReaderStrategyV1 {
00096     public:
00097       BranchMapReaderStrategyV7(TFile* file, int fileVersion, BranchMapReader::eeiMap& eventInfoMap,
00098                                 BranchMapReader::bidToDesc& branchDescriptionMap);
00099     };
00100 
00101     BranchMapReaderStrategyV7::BranchMapReaderStrategyV7(TFile* file, int fileVersion, BranchMapReader::eeiMap& eventInfoMap,
00102       BranchMapReader::bidToDesc& branchDescriptionMap)
00103     : BranchMapReaderStrategyV1(file, fileVersion, eventInfoMap, branchDescriptionMap)
00104     {
00105       updateFile(file);
00106     }
00107 
00108     class BranchMapReaderStrategyV8 : public fwlite::BranchMapReader::Strategy {
00109     public:
00110       BranchMapReaderStrategyV8(TFile* file, int fileVersion, BranchMapReader::eeiMap& eventInfoMap,
00111                                 BranchMapReader::bidToDesc& branchDescriptionMap);
00112       virtual bool updateFile(TFile* file);
00113       virtual bool updateEvent(Long_t eventEntry);
00114       virtual bool updateMap();
00115     private:
00116       TBranch* entryInfoBranch_;
00117       edm::EventEntryInfoVector  eventEntryInfoVector_;
00118       edm::EventEntryInfoVector* pEventEntryInfoVector_;
00119     };
00120 
00121     BranchMapReaderStrategyV8::BranchMapReaderStrategyV8(TFile* file, int fileVersion,
00122       BranchMapReader::eeiMap& eventInfoMap, BranchMapReader::bidToDesc& branchDescriptionMap)
00123     : Strategy(file, fileVersion, eventInfoMap, branchDescriptionMap),
00124       eventEntryInfoVector_(), pEventEntryInfoVector_(&eventEntryInfoVector_)
00125     {
00126       updateFile(file);
00127     }
00128 
00129     bool BranchMapReaderStrategyV8::updateEvent(Long_t newevent)
00130     {
00131       // std::cout << "v8 updateevent " << newevent << std::endl;
00132       if (newevent != eventEntry_) {
00133         eventEntry_ = newevent;
00134         mapperFilled_ = false;
00135       }
00136       return true;
00137     }
00138 
00139     bool BranchMapReaderStrategyV8::updateFile(TFile* file)
00140     {
00141       BranchMapReader::Strategy::updateFile(file);
00142       mapperFilled_ = false;
00143       entryInfoBranch_ = 0;
00144       TTree* metaDataTree = dynamic_cast<TTree*>(currentFile_->Get(edm::poolNames::eventMetaDataTreeName().c_str()) );
00145       // std::cout << "metaDataTree " << metaDataTree << std::endl;
00146       if (0 != metaDataTree) {
00147         entryInfoBranch_ = metaDataTree->GetBranch(BranchTypeToBranchEntryInfoBranchName(edm::InEvent).c_str());
00148         // std::cout << "entryInfoBranch for " << BranchTypeToBranchEntryInfoBranchName(edm::InEvent) << " " << entryInfoBranch_ << std::endl;
00149       } else {
00150         return false;
00151       }
00152       pEventEntryInfoVector_ = &eventEntryInfoVector_;
00153       entryInfoBranch_->SetAddress(&pEventEntryInfoVector_);
00154 
00155       branchDescriptionMap_.clear();
00156       
00157       edm::ProductRegistry reg;
00158       edm::ProductRegistry* pReg = &reg;
00159       TBranch *br = getBranchRegistry(&pReg);
00160 
00161       if (0 != br) {
00162         const edm::ProductRegistry::ProductList& prodList = reg.productList();
00163 
00164         for(edm::ProductRegistry::ProductList::const_iterator it = prodList.begin(), itEnd = prodList.end(); it != itEnd; ++it) {
00165           if (edm::InEvent == it->second.branchType()) {
00166             // call to regenerate branchName
00167             it->second.init();
00168             branchDescriptionMap_.insert(BranchMapReader::bidToDesc::value_type(it->second.branchID(), it->second));
00169             // std::cout << "v8 updatefile " << it->second.branchID() << std::endl;
00170                 }
00171               }
00172       }
00173       return 0 != br;
00174     }
00175 
00176     bool BranchMapReaderStrategyV8::updateMap()
00177     {
00178       if (mapperFilled_) {
00179         return true;
00180       }
00181 
00182       eventInfoMap_ = emptyMapper;
00183       assert (entryInfoBranch_);
00184 
00185       entryInfoBranch_->GetEntry(eventEntry_);
00186 
00187       for (std::vector<edm::EventEntryInfo>::const_iterator it = pEventEntryInfoVector_->begin(), 
00188            itEnd = pEventEntryInfoVector_->end();
00189            it != itEnd; ++it) {
00190         eventInfoMap_.insert(*it);
00191         // std::cout << "v8 updatemap " << it->productID() << " " << it->branchID() << std::endl;
00192       }
00193       mapperFilled_ = true;
00194       return true;
00195     }
00196   }
00197 
00198 
00199 //
00200 // constants, enums and typedefs
00201 //
00202 
00203 //
00204 // static data member definitions
00205 //
00206 
00207 //
00208 // constructors and destructor
00209 //
00210 
00211 BranchMapReader::BranchMapReader(TFile* file)
00212 {
00213   strategy_ = newStrategy(file, getFileVersion(file));
00214 }
00215 
00216 BranchMapReader::Strategy::Strategy(TFile* file, int fileVersion, eeiMap& eventInfoMap,
00217   BranchMapReader::bidToDesc& branchDescriptionMap)
00218   : currentFile_(file), fileVersion_(fileVersion), eventEntry_(-1), eventInfoMap_(eventInfoMap),
00219     branchDescriptionMap_(branchDescriptionMap), mapperFilled_(false)
00220 {
00221   // do in derived obects
00222   // updateFile(file);
00223 }
00224 
00225 BranchMapReader::Strategy::~Strategy()
00226 {
00227 }
00228 
00229 //
00230 // member functions
00231 //
00232 
00233 bool BranchMapReader::Strategy::updateFile(TFile* file)
00234 {
00235   currentFile_ = file;
00236   eventTree_ = dynamic_cast<TTree*>(currentFile_->Get(edm::poolNames::eventTreeName().c_str()));
00237   fileUUID_ = currentFile_->GetUUID();
00238   return 0 != eventTree_;
00239 }
00240 
00241 TBranch* BranchMapReader::Strategy::getBranchRegistry(edm::ProductRegistry** ppReg)
00242 {
00243   TBranch* bReg(0);
00244 
00245   TTree* metaDataTree = dynamic_cast<TTree*>(currentFile_->Get(edm::poolNames::metaDataTreeName().c_str()) );
00246   if ( 0 != metaDataTree) {
00247     bReg = metaDataTree->GetBranch(edm::poolNames::productDescriptionBranchName().c_str());
00248     bReg->SetAddress(ppReg);
00249     bReg->GetEntry(0);
00250     (*ppReg)->setFrozen();
00251   }
00252   return bReg;
00253 }
00254 
00255 int BranchMapReader::getFileVersion(TFile* file) const
00256 {
00257   TTree* metaDataTree = dynamic_cast<TTree*>(file->Get(edm::poolNames::metaDataTreeName().c_str()) );
00258   if (0==metaDataTree) {
00259     return 0;
00260   }
00261     
00262   edm::FileFormatVersion v;
00263   edm::FileFormatVersion* pV=&v;
00264   TBranch* bVer = metaDataTree->GetBranch(edm::poolNames::fileFormatVersionBranchName().c_str());
00265   bVer->SetAddress(&pV);
00266   bVer->GetEntry(0);
00267   return v.value_;
00268 }
00269 
00270 bool BranchMapReader::updateEvent(Long_t newevent)
00271 {
00272   return strategy_->updateEvent(newevent);
00273 }
00274 
00275 bool BranchMapReader::updateFile(TFile* file)
00276 {
00277   if (0 == strategy_.get()) {
00278     strategy_ = newStrategy(file, getFileVersion(file));
00279     bDesc_.clear();
00280     return true;
00281   }
00282 
00283   TFile* currentFile(strategy_->currentFile_);
00284   bool isNew(file != currentFile);
00285 
00286   if (!isNew) {
00287     //could still have a new TFile which just happens to share the same memory address as the previous file
00288     //will assume that if the Event tree's address and UUID are the same as before then we do not have
00289     // to treat this like a new file
00290     TTree* eventTreeTemp = dynamic_cast<TTree*>(currentFile->Get(edm::poolNames::eventTreeName().c_str()));
00291     isNew = eventTreeTemp != strategy_->eventTree_ || strategy_->fileUUID_ != currentFile->GetUUID();
00292   }
00293   if (isNew) {
00294     bDesc_.clear();
00295     int fileVersion = getFileVersion(file);
00296     if (fileVersion != strategy_->fileVersion_) {
00297       strategy_ = newStrategy(file, fileVersion);
00298     } else {
00299       strategy_->updateFile(file);
00300     }
00301   }
00302   return isNew;
00303 }
00304 
00305 const std::vector<edm::BranchDescription>&
00306 BranchMapReader::getBranchDescriptions()
00307 {
00308   if (bDesc_.empty()) {
00309     for (bidToDesc::const_iterator i = branchDescriptionMap_.begin(); i != branchDescriptionMap_.end(); ++i) {
00310       bDesc_.push_back(i->second);
00311     }
00312   }
00313   return bDesc_;
00314 }
00315 
00316 
00317 std::auto_ptr<BranchMapReader::Strategy>
00318 BranchMapReader::newStrategy(TFile* file, int fileVersion)
00319 {
00320   std::auto_ptr<Strategy> s;
00321 
00322   if (fileVersion >= 8) {
00323     s = std::auto_ptr<Strategy>(new internal::BranchMapReaderStrategyV8(file, fileVersion, eventInfoMap_, branchDescriptionMap_));
00324   } else if (fileVersion >= 7) {
00325     s = std::auto_ptr<Strategy>(new internal::BranchMapReaderStrategyV7(file, fileVersion, eventInfoMap_, branchDescriptionMap_));
00326   } else {
00327     s = std::auto_ptr<Strategy>(new internal::BranchMapReaderStrategyV1(file, fileVersion, eventInfoMap_, branchDescriptionMap_));
00328   }
00329   return s;
00330 }
00331 }

Generated on Tue Jun 9 17:36:16 2009 for CMSSW by  doxygen 1.5.4