CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_6/src/FWCore/TFWLiteSelector/src/TFWLiteSelectorBasic.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 //
00004 // Package:     TFWLiteSelector
00005 // Class  :     TFWLiteSelectorBasic
00006 //
00007 // Implementation:
00008 //     <Notes on implementation>
00009 //
00010 // Original Author:  Chris Jones
00011 //         Created:  Tue Jun 27 17:58:10 EDT 2006
00012 //
00013 
00014 // user include files
00015 #include "FWCore/TFWLiteSelector/interface/TFWLiteSelectorBasic.h"
00016 
00017 #include "DataFormats/Common/interface/RefCoreStreamer.h"
00018 #include "DataFormats/Common/interface/WrapperOwningHolder.h"
00019 #include "DataFormats/Provenance/interface/BranchDescription.h"
00020 #include "DataFormats/Provenance/interface/BranchIDListHelper.h"
00021 #include "DataFormats/Provenance/interface/BranchIDListRegistry.h"
00022 #include "DataFormats/Provenance/interface/BranchListIndex.h"
00023 #include "DataFormats/Provenance/interface/BranchMapper.h"
00024 #include "DataFormats/Provenance/interface/BranchType.h"
00025 #include "DataFormats/Provenance/interface/EventAuxiliary.h"
00026 #include "DataFormats/Provenance/interface/EventEntryDescription.h" // kludge to allow compilation
00027 #include "DataFormats/Provenance/interface/EventSelectionID.h"
00028 #include "DataFormats/Provenance/interface/FileFormatVersion.h"
00029 #include "DataFormats/Provenance/interface/LuminosityBlockAuxiliary.h"
00030 #include "DataFormats/Provenance/interface/ModuleDescription.h"
00031 #include "DataFormats/Provenance/interface/ParameterSetBlob.h"
00032 #include "DataFormats/Provenance/interface/ProcessConfiguration.h"
00033 #include "DataFormats/Provenance/interface/ProcessHistory.h"
00034 #include "DataFormats/Provenance/interface/ProcessHistoryRegistry.h"
00035 #include "DataFormats/Provenance/interface/ProductRegistry.h"
00036 #include "DataFormats/Provenance/interface/RunAuxiliary.h"
00037 #include "FWCore/Framework/interface/DelayedReader.h"
00038 #include "FWCore/Framework/interface/Event.h"
00039 #include "FWCore/Framework/interface/EventPrincipal.h"
00040 #include "FWCore/Framework/interface/LuminosityBlockPrincipal.h"
00041 #include "FWCore/Framework/interface/RunPrincipal.h"
00042 #include "FWCore/ParameterSet/interface/FillProductRegistryTransients.h"
00043 #include "FWCore/ParameterSet/interface/Registry.h"
00044 #include "FWCore/Utilities/interface/EDMException.h"
00045 #include "FWCore/Utilities/interface/FriendlyName.h"
00046 #include "FWCore/Utilities/interface/WrappedClassName.h"
00047 
00048 // system include files
00049 #include "TBranch.h"
00050 #include "TChain.h"
00051 #include "TFile.h"
00052 #include "TTree.h"
00053 #include "Reflex/Type.h"
00054 
00055 namespace edm {
00056   namespace root {
00057     class FWLiteDelayedReader : public DelayedReader {
00058      public:
00059       FWLiteDelayedReader() : entry_(-1), eventTree_(0), reg_() {}
00060       void setEntry(Long64_t iEntry) { entry_ = iEntry; }
00061       void setTree(TTree* iTree) {eventTree_ = iTree;}
00062       void set(boost::shared_ptr<ProductRegistry const> iReg) { reg_ = iReg;}
00063      private:
00064       virtual WrapperOwningHolder getProduct_(BranchKey const& k, WrapperInterfaceBase const* interface, EDProductGetter const* ep) const;
00065       virtual std::auto_ptr<EventEntryDescription> getProvenance_(BranchKey const&) const {
00066         return std::auto_ptr<EventEntryDescription>();
00067       }
00068       virtual void mergeReaders_(DelayedReader*) {}
00069       virtual void reset_() {}
00070       Long64_t entry_;
00071       TTree* eventTree_;
00072       boost::shared_ptr<ProductRegistry const>(reg_);
00073     };
00074 
00075     WrapperOwningHolder
00076     FWLiteDelayedReader::getProduct_(BranchKey const& k, WrapperInterfaceBase const* /*interface*/, EDProductGetter const* /*ep*/) const {
00077       ProductRegistry::ProductList::const_iterator itFind= reg_->productList().find(k);
00078       if(itFind == reg_->productList().end()) {
00079         throw Exception(errors::ProductNotFound) << "could not find entry for product " << k;
00080       }
00081       BranchDescription const& bDesc = itFind->second;
00082 
00083       TBranch* branch= eventTree_->GetBranch(bDesc.branchName().c_str());
00084       if(0 == branch) {
00085         throw cms::Exception("MissingBranch")
00086         << "could not find branch named '" << bDesc.branchName() << "'"
00087         << "\n Perhaps the data being requested was not saved in this file?";
00088       }
00089       //find the class type
00090       std::string const fullName = wrappedClassName(bDesc.className());
00091       Reflex::Type classType = Reflex::Type::ByName(fullName);
00092       if(classType == Reflex::Type()) {
00093         throw cms::Exception("MissingDictionary")
00094         << "could not find dictionary for type '" << fullName << "'"
00095         << "\n Please make sure all the necessary libraries are available.";
00096       }
00097 
00098       //use reflex to create an instance of it
00099       Reflex::Object wrapperObj = classType.Construct();
00100       if(0 == wrapperObj.Address()) {
00101         throw cms::Exception("FailedToCreate") << "could not create an instance of '" << fullName << "'";
00102       }
00103       void* address  = wrapperObj.Address();
00104       branch->SetAddress(&address);
00105 
00106 
00107       branch->GetEntry(entry_);
00108       return WrapperOwningHolder(address, bDesc.getInterface());
00109     }
00110 
00111     struct TFWLiteSelectorMembers {
00112       TFWLiteSelectorMembers() :
00113       tree_(0),
00114       reg_(new ProductRegistry()),
00115       processNames_(),
00116       reader_(new FWLiteDelayedReader),
00117       prov_(),
00118       pointerToBranchBuffer_(),
00119       mapper_(new edm::BranchMapper) {
00120         reader_->set(reg_);
00121       }
00122       void setTree(TTree* iTree) {
00123         tree_ = iTree;
00124         reader_->setTree(iTree);
00125       }
00126       TTree* tree_;
00127       boost::shared_ptr<ProductRegistry> reg_;
00128       ProcessHistory processNames_;
00129       boost::shared_ptr<FWLiteDelayedReader> reader_;
00130       std::vector<EventEntryDescription> prov_;
00131       std::vector<EventEntryDescription*> pointerToBranchBuffer_;
00132       FileFormatVersion fileFormatVersion_;
00133 
00134       boost::shared_ptr<edm::BranchMapper> mapper_;
00135       edm::ProcessConfiguration pc_;
00136       boost::shared_ptr<edm::EventPrincipal> ep_;
00137       edm::ModuleDescription md_;
00138     };
00139   }
00140 }
00141 
00142 
00143 //
00144 // constants, enums and typedefs
00145 //
00146 
00147 //
00148 // static data member definitions
00149 //
00150 
00151 //
00152 // constructors and destructor
00153 //
00154 TFWLiteSelectorBasic::TFWLiteSelectorBasic() : m_(new edm::root::TFWLiteSelectorMembers),
00155                                      everythingOK_(false) {
00156 }
00157 
00158 // TFWLiteSelectorBasic::TFWLiteSelectorBasic(TFWLiteSelectorBasic const& rhs)
00159 // {
00160 //    // do actual copying here;
00161 // }
00162 
00163 TFWLiteSelectorBasic::~TFWLiteSelectorBasic() {
00164 }
00165 
00166 //
00167 // assignment operators
00168 //
00169 // TFWLiteSelectorBasic const& TFWLiteSelectorBasic::operator=(TFWLiteSelectorBasic const& rhs)
00170 // {
00171 //   //An exception safe implementation is
00172 //   TFWLiteSelectorBasic temp(rhs);
00173 //   swap(rhs);
00174 //
00175 //   return *this;
00176 // }
00177 
00178 //
00179 // member functions
00180 //
00181 void
00182 TFWLiteSelectorBasic::Begin(TTree * iTree) {
00183   Init(iTree);
00184   begin(fInput);
00185 }
00186 
00187 void
00188 TFWLiteSelectorBasic::SlaveBegin(TTree *iTree) {
00189   Init(iTree);
00190   preProcessing(fInput, *fOutput);
00191 }
00192 
00193 void
00194 TFWLiteSelectorBasic::Init(TTree *iTree) {
00195   if(iTree == 0) return;
00196   m_->setTree(iTree);
00197 }
00198 
00199 
00200 Bool_t
00201 TFWLiteSelectorBasic::Notify() {
00202   //std::cout << "Notify start" << std::endl;
00203   //we have switched to a new file
00204   //get new file from Tree
00205   if(0 == m_->tree_) {
00206     std::cout << "No tree" << std::endl;
00207     return kFALSE;
00208   }
00209   TFile* file = m_->tree_->GetCurrentFile();
00210   if(0 == file) {
00211      //When in Rome, do as the Romans
00212      TChain* chain = dynamic_cast<TChain*>(m_->tree_);
00213      if(0 == chain) {
00214         std::cout << "No file" << std::endl;
00215         return kFALSE;
00216      }
00217      file = chain->GetFile();
00218      if(0 == file) {
00219         std::cout << "No file" << std::endl;
00220         return kFALSE;
00221      }
00222   }
00223   setupNewFile(*file);
00224   return everythingOK_ ? kTRUE: kFALSE;
00225 }
00226 
00227 namespace  {
00228    struct Operate {
00229       Operate(edm::EDProductGetter const* iGetter): old_(setRefCoreStreamer(iGetter)) {
00230       }
00231 
00232       ~Operate() {setRefCoreStreamer(old_);}
00233    private:
00234       edm::EDProductGetter const* old_;
00235    };
00236 }
00237 
00238 Bool_t
00239 TFWLiteSelectorBasic::Process(Long64_t iEntry) {
00240    //std::cout << "Process start" << std::endl;
00241    if(everythingOK_) {
00242       std::auto_ptr<edm::EventAuxiliary> eaux(new edm::EventAuxiliary());
00243       edm::EventAuxiliary& aux = *eaux;
00244       edm::EventAuxiliary* pAux= eaux.get();
00245       TBranch* branch = m_->tree_->GetBranch(edm::BranchTypeToAuxiliaryBranchName(edm::InEvent).c_str());
00246 
00247       branch->SetAddress(&pAux);
00248       branch->GetEntry(iEntry);
00249 
00250 //NEW      m_->processNames_ = aux.processHistory();
00251 
00252 //      std::cout << "ProcessNames\n";
00253 //      for(edm::ProcessNameList::const_iterator itName = m_->processNames_.begin(),
00254 //        itNameEnd = m_->processNames_.end();
00255 //          itName != itNameEnd;
00256 //          ++itName) {
00257 //         std::cout << "  " << *itName << std::endl;
00258 //     }
00259 
00260       boost::shared_ptr<edm::EventSelectionIDVector> eventSelectionIDs_(new edm::EventSelectionIDVector);
00261       edm::EventSelectionIDVector* pEventSelectionIDVector = eventSelectionIDs_.get();
00262       TBranch* eventSelectionsBranch = m_->tree_->GetBranch(edm::poolNames::eventSelectionsBranchName().c_str());
00263       if(!eventSelectionsBranch) {
00264         throw edm::Exception(edm::errors::FatalRootError)
00265             << "Failed to find event Selections branch in event tree";
00266       }
00267       eventSelectionsBranch->SetAddress(&pEventSelectionIDVector);
00268       eventSelectionsBranch->GetEntry(iEntry);
00269 
00270       boost::shared_ptr<edm::BranchListIndexes> branchListIndexes_(new edm::BranchListIndexes);
00271       edm::BranchListIndexes* pBranchListIndexes = branchListIndexes_.get();
00272       TBranch* branchListIndexBranch = m_->tree_->GetBranch(edm::poolNames::branchListIndexesBranchName().c_str());
00273       if(!branchListIndexBranch) {
00274         throw edm::Exception(edm::errors::FatalRootError)
00275             << "Failed to find branch list index branch in event tree";
00276       }
00277       branchListIndexBranch->SetAddress(&pBranchListIndexes);
00278       branchListIndexBranch->GetEntry(iEntry);
00279       edm::BranchIDListHelper::fixBranchListIndexes(*branchListIndexes_);
00280 
00281       try {
00282          m_->reader_->setEntry(iEntry);
00283          boost::shared_ptr<edm::RunAuxiliary> runAux(new edm::RunAuxiliary(aux.run(), aux.time(), aux.time()));
00284          boost::shared_ptr<edm::RunPrincipal> rp(new edm::RunPrincipal(runAux, m_->reg_, m_->pc_));
00285          boost::shared_ptr<edm::LuminosityBlockAuxiliary> lumiAux(
00286                 new edm::LuminosityBlockAuxiliary(rp->run(), 1, aux.time(), aux.time()));
00287          boost::shared_ptr<edm::LuminosityBlockPrincipal>lbp(
00288             new edm::LuminosityBlockPrincipal(lumiAux, m_->reg_, m_->pc_, rp));
00289          m_->ep_->fillEventPrincipal(*eaux, lbp, eventSelectionIDs_, branchListIndexes_, m_->mapper_, m_->reader_.get());
00290          m_->processNames_ = m_->ep_->processHistory();
00291 
00292          edm::Event event(*m_->ep_, m_->md_);
00293 
00294          //Make the event principal accessible to edm::Ref's
00295          Operate sentry(m_->ep_->prodGetter());
00296          process(event);
00297       } catch(std::exception const& iEx) {
00298          std::cout << "While processing entry " << iEntry << " the following exception was caught \n"
00299                    << iEx.what() << std::endl;
00300       } catch(...) {
00301          std::cout << "While processing entry " << iEntry << " an unknown exception was caught" << std::endl;
00302       }
00303   }
00304   return everythingOK_ ? kTRUE: kFALSE;
00305 }
00306 
00307 void
00308 TFWLiteSelectorBasic::SlaveTerminate() {
00309   postProcessing(*fOutput);
00310 }
00311 
00312 void
00313 TFWLiteSelectorBasic::Terminate() {
00314   terminate(*fOutput);
00315 }
00316 
00317 void
00318 TFWLiteSelectorBasic::setupNewFile(TFile& iFile) {
00319   //look up meta-data
00320   //get product registry
00321 
00322   //std::vector<edm::EventProcessHistoryID> eventProcessHistoryIDs_;
00323   TTree* metaDataTree = dynamic_cast<TTree*>(iFile.Get(edm::poolNames::metaDataTreeName().c_str()));
00324   if(!metaDataTree) {
00325     std::cout << "could not find TTree " << edm::poolNames::metaDataTreeName() << std::endl;
00326     everythingOK_ = false;
00327     return;
00328   }
00329   edm::FileFormatVersion* fftPtr = &(m_->fileFormatVersion_);
00330   if(metaDataTree->FindBranch(edm::poolNames::fileFormatVersionBranchName().c_str()) != 0) {
00331     metaDataTree->SetBranchAddress(edm::poolNames::fileFormatVersionBranchName().c_str(), &fftPtr);
00332     }
00333 
00334 
00335   edm::ProductRegistry* pReg = &(*m_->reg_);
00336   metaDataTree->SetBranchAddress(edm::poolNames::productDescriptionBranchName().c_str(), &(pReg));
00337 
00338   m_->reg_->setFrozen();
00339 
00340   typedef std::map<edm::ParameterSetID, edm::ParameterSetBlob> PsetMap;
00341   PsetMap psetMap;
00342   PsetMap *psetMapPtr = &psetMap;
00343   if(metaDataTree->FindBranch(edm::poolNames::parameterSetMapBranchName().c_str()) != 0) {
00344     metaDataTree->SetBranchAddress(edm::poolNames::parameterSetMapBranchName().c_str(), &psetMapPtr);
00345   } else {
00346     TTree* psetTree = dynamic_cast<TTree *>(iFile.Get(edm::poolNames::parameterSetsTreeName().c_str()));
00347     if(0 == psetTree) {
00348       throw edm::Exception(edm::errors::FileReadError) << "Could not find tree " << edm::poolNames::parameterSetsTreeName()
00349       << " in the input file.\n";
00350     }
00351     typedef std::pair<edm::ParameterSetID, edm::ParameterSetBlob> IdToBlobs;
00352     IdToBlobs idToBlob;
00353     IdToBlobs* pIdToBlob = &idToBlob;
00354     psetTree->SetBranchAddress(edm::poolNames::idToParameterSetBlobsBranchName().c_str(), &pIdToBlob);
00355     for(long long i = 0; i != psetTree->GetEntries(); ++i) {
00356       psetTree->GetEntry(i);
00357       psetMap.insert(idToBlob);
00358     }
00359   }
00360 
00361   edm::ProcessHistoryRegistry::vector_type pHistVector;
00362   edm::ProcessHistoryRegistry::vector_type *pHistVectorPtr = &pHistVector;
00363   if(metaDataTree->FindBranch(edm::poolNames::processHistoryBranchName().c_str()) != 0) {
00364      metaDataTree->SetBranchAddress(edm::poolNames::processHistoryBranchName().c_str(), &pHistVectorPtr);
00365   }
00366 
00367 
00368   edm::ProcessConfigurationVector procConfigVector;
00369   edm::ProcessConfigurationVector* procConfigVectorPtr = &procConfigVector;
00370   if(metaDataTree->FindBranch(edm::poolNames::processConfigurationBranchName().c_str()) != 0) {
00371      metaDataTree->SetBranchAddress(edm::poolNames::processConfigurationBranchName().c_str(), &procConfigVectorPtr);
00372   }
00373 
00374   std::auto_ptr<edm::BranchIDListRegistry::collection_type> branchIDListsAPtr(new edm::BranchIDListRegistry::collection_type);
00375   edm::BranchIDListRegistry::collection_type *branchIDListsPtr = branchIDListsAPtr.get();
00376   if(metaDataTree->FindBranch(edm::poolNames::branchIDListBranchName().c_str()) != 0) {
00377     metaDataTree->SetBranchAddress(edm::poolNames::branchIDListBranchName().c_str(), &branchIDListsPtr);
00378   }
00379 
00380   metaDataTree->GetEntry(0);
00381 
00382   // Merge into the registries. For now, we do NOT merge the product registry.
00383   edm::pset::Registry& psetRegistry = *edm::pset::Registry::instance();
00384   for(PsetMap::const_iterator i = psetMap.begin(), iEnd = psetMap.end();
00385       i != iEnd; ++i) {
00386     edm::ParameterSet pset(i->second.pset());
00387     pset.setID(i->first);
00388     psetRegistry.insertMapped(pset);
00389   }
00390 
00391   edm::ProcessHistoryRegistry::instance()->insertCollection(pHistVector);
00392   edm::ProcessConfigurationRegistry::instance()->insertCollection(procConfigVector);
00393 
00394   m_->pointerToBranchBuffer_.erase(m_->pointerToBranchBuffer_.begin(),
00395                                    m_->pointerToBranchBuffer_.end());
00396 
00397   fillProductRegistryTransients(procConfigVector, *m_->reg_);
00398   std::auto_ptr<edm::ProductRegistry> newReg(new edm::ProductRegistry());
00399 
00400   edm::ProductRegistry::ProductList const& prodList = m_->reg_->productList();
00401   {
00402      for(edm::ProductRegistry::ProductList::const_iterator it = prodList.begin(), itEnd = prodList.end();
00403             it != itEnd; ++it) {
00404          edm::BranchDescription const& prod = it->second;
00405          //std::cout << "productname = " << it->second << " end " << std::endl;
00406          std::string newFriendlyName = edm::friendlyname::friendlyName(prod.className());
00407          if(newFriendlyName == prod.friendlyClassName()) {
00408            newReg->copyProduct(prod);
00409          } else {
00410            if(m_->fileFormatVersion_.splitProductIDs()) {
00411              throw edm::Exception(edm::errors::UnimplementedFeature)
00412                << "Cannot change friendly class name algorithm without more development work\n"
00413                << "to update BranchIDLists.  Contact the framework group.\n";
00414            }
00415            edm::BranchDescription newBD(prod);
00416            newBD.updateFriendlyClassName();
00417            newReg->copyProduct(newBD);
00418            // Need to call init to get old branch name.
00419            prod.init();
00420          }
00421        }
00422 
00423     newReg->setFrozen();
00424     m_->reg_.reset(newReg.release());
00425   }
00426 
00427   edm::ProductRegistry::ProductList const& prodList2 = m_->reg_->productList();
00428   std::vector<edm::EventEntryDescription> temp(prodList2.size(), edm::EventEntryDescription());
00429   m_->prov_.swap(temp);
00430   std::vector<edm::EventEntryDescription>::iterator itB = m_->prov_.begin();
00431   m_->pointerToBranchBuffer_.reserve(prodList2.size());
00432 
00433   for(edm::ProductRegistry::ProductList::const_iterator it = prodList2.begin(), itEnd = prodList2.end();
00434        it != itEnd; ++it, ++itB) {
00435     edm::BranchDescription const& prod = it->second;
00436     if(prod.branchType() == edm::InEvent) {
00437       prod.init();
00438       //NEED to do this and check to see if branch exists
00439       if(m_->tree_->GetBranch(prod.branchName().c_str()) == 0) {
00440         prod.setDropped();
00441       }
00442 
00443       //std::cout << "id " << it->first << " branch " << it->second << std::endl;
00444       //m_->pointerToBranchBuffer_.push_back(&(*itB));
00445       //void* tmp = &(m_->pointerToBranchBuffer_.back());
00446       //edm::EventEntryDescription* tmp = &(*itB);
00447       //CDJ need to fix provenance and be backwards compatible, for now just don't read the branch
00448       //m_->metaTree_->SetBranchAddress(prod.branchName().c_str(), tmp);
00449     }
00450   }
00451   edm::BranchIDListHelper::updateFromInput(*branchIDListsAPtr, "");
00452   m_->ep_.reset(new edm::EventPrincipal(m_->reg_, m_->pc_));
00453   everythingOK_ = true;
00454 }
00455 
00456 //
00457 // const member functions
00458 //
00459 
00460 //
00461 // static member functions
00462 //