00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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/BranchIDList.h"
00021 #include "DataFormats/Provenance/interface/BranchIDListHelper.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"
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/ObjectWithDict.h"
00047 #include "FWCore/Utilities/interface/TypeWithDict.h"
00048 #include "FWCore/Utilities/interface/WrappedClassName.h"
00049
00050
00051 #include "TBranch.h"
00052 #include "TChain.h"
00053 #include "TFile.h"
00054 #include "TTree.h"
00055
00056 namespace edm {
00057 namespace root {
00058 class FWLiteDelayedReader : public DelayedReader {
00059 public:
00060 FWLiteDelayedReader() : entry_(-1), eventTree_(0), reg_() {}
00061 void setEntry(Long64_t iEntry) { entry_ = iEntry; }
00062 void setTree(TTree* iTree) {eventTree_ = iTree;}
00063 void set(boost::shared_ptr<ProductRegistry const> iReg) { reg_ = iReg;}
00064 private:
00065 virtual WrapperOwningHolder getProduct_(BranchKey const& k, WrapperInterfaceBase const* interface, EDProductGetter const* ep) const;
00066 virtual std::auto_ptr<EventEntryDescription> getProvenance_(BranchKey const&) const {
00067 return std::auto_ptr<EventEntryDescription>();
00068 }
00069 virtual void mergeReaders_(DelayedReader*) {}
00070 virtual void reset_() {}
00071 Long64_t entry_;
00072 TTree* eventTree_;
00073 boost::shared_ptr<ProductRegistry const>(reg_);
00074 };
00075
00076 WrapperOwningHolder
00077 FWLiteDelayedReader::getProduct_(BranchKey const& k, WrapperInterfaceBase const* , EDProductGetter const* ) const {
00078 ProductRegistry::ProductList::const_iterator itFind= reg_->productList().find(k);
00079 if(itFind == reg_->productList().end()) {
00080 throw Exception(errors::ProductNotFound) << "could not find entry for product " << k;
00081 }
00082 BranchDescription const& bDesc = itFind->second;
00083
00084 TBranch* branch= eventTree_->GetBranch(bDesc.branchName().c_str());
00085 if(0 == branch) {
00086 throw cms::Exception("MissingBranch")
00087 << "could not find branch named '" << bDesc.branchName() << "'"
00088 << "\n Perhaps the data being requested was not saved in this file?";
00089 }
00090
00091 std::string const fullName = wrappedClassName(bDesc.className());
00092 TypeWithDict classType = TypeWithDict::byName(fullName);
00093 if(!bool(classType)) {
00094 throw cms::Exception("MissingDictionary")
00095 << "could not find dictionary for type '" << fullName << "'"
00096 << "\n Please make sure all the necessary libraries are available.";
00097 }
00098
00099
00100 void const* address = classType.construct().address();
00101 if(0 == address) {
00102 throw cms::Exception("FailedToCreate") << "could not create an instance of '" << fullName << "'";
00103 }
00104 branch->SetAddress(&address);
00105
00106 branch->GetEntry(entry_);
00107 return WrapperOwningHolder(address, bDesc.getInterface());
00108 }
00109
00110 struct TFWLiteSelectorMembers {
00111 TFWLiteSelectorMembers() :
00112 tree_(0),
00113 reg_(new ProductRegistry()),
00114 branchIDListHelper_(new BranchIDListHelper()),
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 boost::shared_ptr<BranchIDListHelper> branchIDListHelper_;
00129 ProcessHistory processNames_;
00130 boost::shared_ptr<FWLiteDelayedReader> reader_;
00131 std::vector<EventEntryDescription> prov_;
00132 std::vector<EventEntryDescription*> pointerToBranchBuffer_;
00133 FileFormatVersion fileFormatVersion_;
00134
00135 boost::shared_ptr<edm::BranchMapper> mapper_;
00136 edm::ProcessConfiguration pc_;
00137 boost::shared_ptr<edm::EventPrincipal> ep_;
00138 edm::ModuleDescription md_;
00139 };
00140 }
00141 }
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155 TFWLiteSelectorBasic::TFWLiteSelectorBasic() : m_(new edm::root::TFWLiteSelectorMembers),
00156 everythingOK_(false) {
00157 }
00158
00159
00160
00161
00162
00163
00164 TFWLiteSelectorBasic::~TFWLiteSelectorBasic() {
00165 }
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182 void
00183 TFWLiteSelectorBasic::Begin(TTree * iTree) {
00184 Init(iTree);
00185 begin(fInput);
00186 }
00187
00188 void
00189 TFWLiteSelectorBasic::SlaveBegin(TTree *iTree) {
00190 Init(iTree);
00191 preProcessing(fInput, *fOutput);
00192 }
00193
00194 void
00195 TFWLiteSelectorBasic::Init(TTree *iTree) {
00196 if(iTree == 0) return;
00197 m_->setTree(iTree);
00198 }
00199
00200
00201 Bool_t
00202 TFWLiteSelectorBasic::Notify() {
00203
00204
00205
00206 if(0 == m_->tree_) {
00207 std::cout << "No tree" << std::endl;
00208 return kFALSE;
00209 }
00210 TFile* file = m_->tree_->GetCurrentFile();
00211 if(0 == file) {
00212
00213 TChain* chain = dynamic_cast<TChain*>(m_->tree_);
00214 if(0 == chain) {
00215 std::cout << "No file" << std::endl;
00216 return kFALSE;
00217 }
00218 file = chain->GetFile();
00219 if(0 == file) {
00220 std::cout << "No file" << std::endl;
00221 return kFALSE;
00222 }
00223 }
00224 setupNewFile(*file);
00225 return everythingOK_ ? kTRUE: kFALSE;
00226 }
00227
00228 namespace {
00229 struct Operate {
00230 Operate(edm::EDProductGetter const* iGetter): old_(setRefCoreStreamer(iGetter)) {
00231 }
00232
00233 ~Operate() {setRefCoreStreamer(old_);}
00234 private:
00235 edm::EDProductGetter const* old_;
00236 };
00237 }
00238
00239 Bool_t
00240 TFWLiteSelectorBasic::Process(Long64_t iEntry) {
00241
00242 if(everythingOK_) {
00243 std::auto_ptr<edm::EventAuxiliary> eaux(new edm::EventAuxiliary());
00244 edm::EventAuxiliary& aux = *eaux;
00245 edm::EventAuxiliary* pAux= eaux.get();
00246 TBranch* branch = m_->tree_->GetBranch(edm::BranchTypeToAuxiliaryBranchName(edm::InEvent).c_str());
00247
00248 branch->SetAddress(&pAux);
00249 branch->GetEntry(iEntry);
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261 boost::shared_ptr<edm::EventSelectionIDVector> eventSelectionIDs_(new edm::EventSelectionIDVector);
00262 edm::EventSelectionIDVector* pEventSelectionIDVector = eventSelectionIDs_.get();
00263 TBranch* eventSelectionsBranch = m_->tree_->GetBranch(edm::poolNames::eventSelectionsBranchName().c_str());
00264 if(!eventSelectionsBranch) {
00265 throw edm::Exception(edm::errors::FatalRootError)
00266 << "Failed to find event Selections branch in event tree";
00267 }
00268 eventSelectionsBranch->SetAddress(&pEventSelectionIDVector);
00269 eventSelectionsBranch->GetEntry(iEntry);
00270
00271 boost::shared_ptr<edm::BranchListIndexes> branchListIndexes_(new edm::BranchListIndexes);
00272 edm::BranchListIndexes* pBranchListIndexes = branchListIndexes_.get();
00273 TBranch* branchListIndexBranch = m_->tree_->GetBranch(edm::poolNames::branchListIndexesBranchName().c_str());
00274 if(!branchListIndexBranch) {
00275 throw edm::Exception(edm::errors::FatalRootError)
00276 << "Failed to find branch list index branch in event tree";
00277 }
00278 branchListIndexBranch->SetAddress(&pBranchListIndexes);
00279 branchListIndexBranch->GetEntry(iEntry);
00280 m_->branchIDListHelper_->fixBranchListIndexes(*branchListIndexes_);
00281
00282 try {
00283 m_->reader_->setEntry(iEntry);
00284 boost::shared_ptr<edm::RunAuxiliary> runAux(new edm::RunAuxiliary(aux.run(), aux.time(), aux.time()));
00285 boost::shared_ptr<edm::RunPrincipal> rp(new edm::RunPrincipal(runAux, m_->reg_, m_->pc_));
00286 boost::shared_ptr<edm::LuminosityBlockAuxiliary> lumiAux(
00287 new edm::LuminosityBlockAuxiliary(rp->run(), 1, aux.time(), aux.time()));
00288 boost::shared_ptr<edm::LuminosityBlockPrincipal>lbp(
00289 new edm::LuminosityBlockPrincipal(lumiAux, m_->reg_, m_->pc_));
00290 m_->ep_->fillEventPrincipal(*eaux, eventSelectionIDs_, branchListIndexes_, m_->mapper_, m_->reader_.get());
00291 lbp->setRunPrincipal(rp);
00292 m_->ep_->setLuminosityBlockPrincipal(lbp);
00293 m_->processNames_ = m_->ep_->processHistory();
00294
00295 edm::Event event(*m_->ep_, m_->md_);
00296
00297
00298 Operate sentry(m_->ep_->prodGetter());
00299 process(event);
00300 } catch(std::exception const& iEx) {
00301 std::cout << "While processing entry " << iEntry << " the following exception was caught \n"
00302 << iEx.what() << std::endl;
00303 } catch(...) {
00304 std::cout << "While processing entry " << iEntry << " an unknown exception was caught" << std::endl;
00305 }
00306 }
00307 return everythingOK_ ? kTRUE: kFALSE;
00308 }
00309
00310 void
00311 TFWLiteSelectorBasic::SlaveTerminate() {
00312 postProcessing(*fOutput);
00313 }
00314
00315 void
00316 TFWLiteSelectorBasic::Terminate() {
00317 terminate(*fOutput);
00318 }
00319
00320 void
00321 TFWLiteSelectorBasic::setupNewFile(TFile& iFile) {
00322
00323
00324
00325
00326 TTree* metaDataTree = dynamic_cast<TTree*>(iFile.Get(edm::poolNames::metaDataTreeName().c_str()));
00327 if(!metaDataTree) {
00328 std::cout << "could not find TTree " << edm::poolNames::metaDataTreeName() << std::endl;
00329 everythingOK_ = false;
00330 return;
00331 }
00332 edm::FileFormatVersion* fftPtr = &(m_->fileFormatVersion_);
00333 if(metaDataTree->FindBranch(edm::poolNames::fileFormatVersionBranchName().c_str()) != 0) {
00334 metaDataTree->SetBranchAddress(edm::poolNames::fileFormatVersionBranchName().c_str(), &fftPtr);
00335 }
00336
00337
00338 edm::ProductRegistry* pReg = &(*m_->reg_);
00339 metaDataTree->SetBranchAddress(edm::poolNames::productDescriptionBranchName().c_str(), &(pReg));
00340
00341 m_->reg_->setFrozen();
00342
00343 typedef std::map<edm::ParameterSetID, edm::ParameterSetBlob> PsetMap;
00344 PsetMap psetMap;
00345 PsetMap *psetMapPtr = &psetMap;
00346 if(metaDataTree->FindBranch(edm::poolNames::parameterSetMapBranchName().c_str()) != 0) {
00347 metaDataTree->SetBranchAddress(edm::poolNames::parameterSetMapBranchName().c_str(), &psetMapPtr);
00348 } else {
00349 TTree* psetTree = dynamic_cast<TTree *>(iFile.Get(edm::poolNames::parameterSetsTreeName().c_str()));
00350 if(0 == psetTree) {
00351 throw edm::Exception(edm::errors::FileReadError) << "Could not find tree " << edm::poolNames::parameterSetsTreeName()
00352 << " in the input file.\n";
00353 }
00354 typedef std::pair<edm::ParameterSetID, edm::ParameterSetBlob> IdToBlobs;
00355 IdToBlobs idToBlob;
00356 IdToBlobs* pIdToBlob = &idToBlob;
00357 psetTree->SetBranchAddress(edm::poolNames::idToParameterSetBlobsBranchName().c_str(), &pIdToBlob);
00358 for(long long i = 0; i != psetTree->GetEntries(); ++i) {
00359 psetTree->GetEntry(i);
00360 psetMap.insert(idToBlob);
00361 }
00362 }
00363
00364 edm::ProcessHistoryRegistry::vector_type pHistVector;
00365 edm::ProcessHistoryRegistry::vector_type *pHistVectorPtr = &pHistVector;
00366 if(metaDataTree->FindBranch(edm::poolNames::processHistoryBranchName().c_str()) != 0) {
00367 metaDataTree->SetBranchAddress(edm::poolNames::processHistoryBranchName().c_str(), &pHistVectorPtr);
00368 }
00369
00370
00371 edm::ProcessConfigurationVector procConfigVector;
00372 edm::ProcessConfigurationVector* procConfigVectorPtr = &procConfigVector;
00373 if(metaDataTree->FindBranch(edm::poolNames::processConfigurationBranchName().c_str()) != 0) {
00374 metaDataTree->SetBranchAddress(edm::poolNames::processConfigurationBranchName().c_str(), &procConfigVectorPtr);
00375 }
00376
00377 boost::shared_ptr<edm::BranchIDListHelper> branchIDListsHelper(new edm::BranchIDListHelper);
00378 edm::BranchIDLists const* branchIDListsPtr = &branchIDListsHelper->branchIDLists();
00379 if(metaDataTree->FindBranch(edm::poolNames::branchIDListBranchName().c_str()) != 0) {
00380 metaDataTree->SetBranchAddress(edm::poolNames::branchIDListBranchName().c_str(), &branchIDListsPtr);
00381 }
00382
00383 metaDataTree->GetEntry(0);
00384
00385
00386 edm::pset::Registry& psetRegistry = *edm::pset::Registry::instance();
00387 for(PsetMap::const_iterator i = psetMap.begin(), iEnd = psetMap.end();
00388 i != iEnd; ++i) {
00389 edm::ParameterSet pset(i->second.pset());
00390 pset.setID(i->first);
00391 psetRegistry.insertMapped(pset);
00392 }
00393
00394 edm::ProcessHistoryRegistry::instance()->insertCollection(pHistVector);
00395 edm::ProcessConfigurationRegistry::instance()->insertCollection(procConfigVector);
00396
00397 m_->pointerToBranchBuffer_.erase(m_->pointerToBranchBuffer_.begin(),
00398 m_->pointerToBranchBuffer_.end());
00399
00400 fillProductRegistryTransients(procConfigVector, *m_->reg_);
00401 std::auto_ptr<edm::ProductRegistry> newReg(new edm::ProductRegistry());
00402
00403 edm::ProductRegistry::ProductList const& prodList = m_->reg_->productList();
00404 {
00405 for(edm::ProductRegistry::ProductList::const_iterator it = prodList.begin(), itEnd = prodList.end();
00406 it != itEnd; ++it) {
00407 edm::BranchDescription const& prod = it->second;
00408
00409 std::string newFriendlyName = edm::friendlyname::friendlyName(prod.className());
00410 if(newFriendlyName == prod.friendlyClassName()) {
00411 newReg->copyProduct(prod);
00412 } else {
00413 if(m_->fileFormatVersion_.splitProductIDs()) {
00414 throw edm::Exception(edm::errors::UnimplementedFeature)
00415 << "Cannot change friendly class name algorithm without more development work\n"
00416 << "to update BranchIDLists. Contact the framework group.\n";
00417 }
00418 edm::BranchDescription newBD(prod);
00419 newBD.updateFriendlyClassName();
00420 newReg->copyProduct(newBD);
00421
00422 prod.init();
00423 }
00424 }
00425
00426 newReg->setFrozen();
00427 m_->reg_.reset(newReg.release());
00428 }
00429
00430 edm::ProductRegistry::ProductList const& prodList2 = m_->reg_->productList();
00431 std::vector<edm::EventEntryDescription> temp(prodList2.size(), edm::EventEntryDescription());
00432 m_->prov_.swap(temp);
00433 std::vector<edm::EventEntryDescription>::iterator itB = m_->prov_.begin();
00434 m_->pointerToBranchBuffer_.reserve(prodList2.size());
00435
00436 for(edm::ProductRegistry::ProductList::const_iterator it = prodList2.begin(), itEnd = prodList2.end();
00437 it != itEnd; ++it, ++itB) {
00438 edm::BranchDescription const& prod = it->second;
00439 if(prod.branchType() == edm::InEvent) {
00440 prod.init();
00441
00442 if(m_->tree_->GetBranch(prod.branchName().c_str()) == 0) {
00443 prod.setDropped();
00444 }
00445
00446
00447
00448
00449
00450
00451
00452 }
00453 }
00454 m_->branchIDListHelper_->updateFromInput(*branchIDListsPtr);
00455 m_->ep_.reset(new edm::EventPrincipal(m_->reg_, m_->branchIDListHelper_, m_->pc_));
00456 everythingOK_ = true;
00457 }
00458
00459
00460
00461
00462
00463
00464
00465