CMS 3D CMS Logo

TFWLiteSelectorBasic.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 
3 //
4 // Package: TFWLiteSelector
5 // Class : TFWLiteSelectorBasic
6 //
7 // Implementation:
8 // <Notes on implementation>
9 //
10 // Original Author: Chris Jones
11 // Created: Tue Jun 27 17:58:10 EDT 2006
12 //
13 
14 // user include files
16 
26 #include "DataFormats/Provenance/interface/EventEntryDescription.h" // kludge to allow compilation
51 
52 // system include files
53 #include "TBranch.h"
54 #include "TChain.h"
55 #include "TFile.h"
56 #include "TTree.h"
57 
58 #include <iostream>
59 #include <memory>
60 #include <string>
61 #include <vector>
62 #include <unordered_map>
63 
64 namespace edm {
65  namespace root {
67  public:
68  FWLiteDelayedReader() : entry_(-1), eventTree_(nullptr) {}
69  void setEntry(Long64_t iEntry) { entry_ = iEntry; }
70  void setTree(TTree* iTree) { eventTree_ = iTree; }
71  void set(std::shared_ptr<std::unordered_map<unsigned int, BranchDescription const*>> iMap) {
72  bidToDesc_ = std::move(iMap);
73  }
74 
75  private:
76  std::unique_ptr<WrapperBase> getTheProduct(BranchID const& k) const;
77  std::shared_ptr<WrapperBase> getProduct_(BranchID const& k, EDProductGetter const* ep) override;
78  virtual std::unique_ptr<EventEntryDescription> getProvenance_(BranchKey const&) const {
79  return std::unique_ptr<EventEntryDescription>();
80  }
81  void mergeReaders_(DelayedReader*) override {}
82  void reset_() override {}
83 
85  const override {
86  return nullptr;
87  }
89  const override {
90  return nullptr;
91  };
92 
93  Long64_t entry_;
94  TTree* eventTree_;
95  std::shared_ptr<std::unordered_map<unsigned int, BranchDescription const*>> bidToDesc_;
96  };
97 
98  std::shared_ptr<WrapperBase> FWLiteDelayedReader::getProduct_(BranchID const& k, EDProductGetter const* /*ep*/) {
99  return getTheProduct(k);
100  }
101 
102  std::unique_ptr<WrapperBase> FWLiteDelayedReader::getTheProduct(BranchID const& k) const {
103  auto itFind = bidToDesc_->find(k.id());
104  if (itFind == bidToDesc_->end()) {
105  throw Exception(errors::ProductNotFound) << "could not find entry for product " << k;
106  }
107  BranchDescription const& bDesc = *(itFind->second);
108 
109  TBranch* branch = eventTree_->GetBranch(bDesc.branchName().c_str());
110  if (nullptr == branch) {
111  throw cms::Exception("MissingBranch") << "could not find branch named '" << bDesc.branchName() << "'"
112  << "\n Perhaps the data being requested was not saved in this file?";
113  }
114  //find the class type
117  if (!bool(classType)) {
118  throw cms::Exception("MissingDictionary") << "could not find dictionary for type '" << fullName << "'"
119  << "\n Please make sure all the necessary libraries are available.";
120  }
121 
122  //create an instance of it
123  ObjectWithDict wrapperObj = classType.construct();
124  if (nullptr == wrapperObj.address()) {
125  throw cms::Exception("FailedToCreate") << "could not create an instance of '" << fullName << "'";
126  }
127  void* address = wrapperObj.address();
128  branch->SetAddress(&address);
129  ObjectWithDict edProdObj = wrapperObj.castObject(TypeWithDict::byName("edm::WrapperBase"));
130 
131  WrapperBase* prod = reinterpret_cast<WrapperBase*>(edProdObj.address());
132 
133  if (nullptr == prod) {
134  throw cms::Exception("FailedConversion") << "failed to convert a '" << fullName << "' to a edm::WrapperBase."
135  << "Please contact developers since something is very wrong.";
136  }
137  branch->GetEntry(entry_);
138  return std::unique_ptr<WrapperBase>(prod);
139  }
140 
143  : tree_(nullptr),
144  reg_(new ProductRegistry()),
145  bidToDesc_(std::make_shared<std::unordered_map<unsigned int, BranchDescription const*>>()),
148  // Note that thinned collections are not supported yet, the next
149  // line just makes it compile but when the Ref or Ptr tries to
150  // find the thinned collection it will report them not found.
151  // More work needed here if this is needed (we think no one
152  // is using TFWLiteSelector anymore and intend to implement
153  // this properly if it turns out we are wrong)
155  processNames_(),
157  prov_(),
161  }
162  void setTree(TTree* iTree) {
163  tree_ = iTree;
164  reader_->setTree(iTree);
165  }
166 
167  TTree const* tree() const { return get_underlying_safe(tree_); }
168  TTree*& tree() { return get_underlying_safe(tree_); }
169  std::shared_ptr<ProductRegistry const> reg() const { return get_underlying_safe(reg_); }
170  std::shared_ptr<ProductRegistry>& reg() { return get_underlying_safe(reg_); }
171  std::shared_ptr<BranchIDListHelper const> branchIDListHelper() const {
173  }
174  std::shared_ptr<BranchIDListHelper>& branchIDListHelper() { return get_underlying_safe(branchIDListHelper_); }
175  std::shared_ptr<ThinnedAssociationsHelper const> thinnedAssociationsHelper() const {
177  }
178  std::shared_ptr<ThinnedAssociationsHelper>& thinnedAssociationsHelper() {
180  }
181 
190  std::vector<EventEntryDescription> prov_;
191  std::vector<EventEntryDescription const*> pointerToBranchBuffer_;
193 
198  };
199  } // namespace root
200 } // namespace edm
201 
202 //
203 // constants, enums and typedefs
204 //
205 
206 //
207 // static data member definitions
208 //
209 
210 //
211 // constructors and destructor
212 //
213 TFWLiteSelectorBasic::TFWLiteSelectorBasic() : m_(new edm::root::TFWLiteSelectorMembers), everythingOK_(false) {}
214 
215 // TFWLiteSelectorBasic::TFWLiteSelectorBasic(TFWLiteSelectorBasic const& rhs)
216 // {
217 // // do actual copying here;
218 // }
219 
221 
222 //
223 // assignment operators
224 //
225 // TFWLiteSelectorBasic const& TFWLiteSelectorBasic::operator=(TFWLiteSelectorBasic const& rhs)
226 // {
227 // //An exception safe implementation is
228 // TFWLiteSelectorBasic temp(rhs);
229 // swap(rhs);
230 //
231 // return *this;
232 // }
233 
234 //
235 // member functions
236 //
237 void TFWLiteSelectorBasic::Begin(TTree* iTree) {
238  Init(iTree);
239  begin(fInput);
240 }
241 
243  Init(iTree);
244  preProcessing(fInput, *fOutput);
245 }
246 
247 void TFWLiteSelectorBasic::Init(TTree* iTree) {
248  if (iTree == nullptr)
249  return;
250  m_->setTree(iTree);
251 }
252 
254  //std::cout << "Notify start" << std::endl;
255  //we have switched to a new file
256  //get new file from Tree
257  if (nullptr == m_->tree_) {
258  std::cout << "No tree" << std::endl;
259  return kFALSE;
260  }
261  TFile* file = m_->tree_->GetCurrentFile();
262  if (nullptr == file) {
263  //When in Rome, do as the Romans
264  TChain* chain = dynamic_cast<TChain*>(m_->tree());
265  if (nullptr == chain) {
266  std::cout << "No file" << std::endl;
267  return kFALSE;
268  }
269  file = chain->GetFile();
270  if (nullptr == file) {
271  std::cout << "No file" << std::endl;
272  return kFALSE;
273  }
274  }
275  setupNewFile(*file);
276  return everythingOK_ ? kTRUE : kFALSE;
277 }
278 
279 namespace {
280  struct Operate {
281  Operate(edm::EDProductGetter const* iGetter) : old_(setRefCoreStreamer(iGetter)) {}
282 
283  ~Operate() { setRefCoreStreamer(old_); }
284 
285  private:
286  edm::EDProductGetter const* old_;
287  };
288 } // namespace
289 
290 Bool_t TFWLiteSelectorBasic::Process(Long64_t iEntry) {
291  //std::cout << "Process start" << std::endl;
292  if (everythingOK_) {
293  std::unique_ptr<edm::EventAuxiliary> eaux = std::make_unique<edm::EventAuxiliary>();
294  edm::EventAuxiliary& aux = *eaux;
295  edm::EventAuxiliary* pAux = eaux.get();
296  TBranch* branch = m_->tree_->GetBranch(edm::BranchTypeToAuxiliaryBranchName(edm::InEvent).c_str());
297 
298  branch->SetAddress(&pAux);
299  branch->GetEntry(iEntry);
300 
301  //NEW m_->processNames_ = aux.processHistory();
302 
303  // std::cout << "ProcessNames\n";
304  // for(auto const& name : m_->processNames_) {
305  // std::cout << " " << name << std::endl;
306  // }
307 
308  edm::EventSelectionIDVector eventSelectionIDs;
309  edm::EventSelectionIDVector* pEventSelectionIDVector = &eventSelectionIDs;
310  TBranch* eventSelectionsBranch = m_->tree_->GetBranch(edm::poolNames::eventSelectionsBranchName().c_str());
311  if (!eventSelectionsBranch) {
312  throw edm::Exception(edm::errors::FatalRootError) << "Failed to find event Selections branch in event tree";
313  }
314  eventSelectionsBranch->SetAddress(&pEventSelectionIDVector);
315  eventSelectionsBranch->GetEntry(iEntry);
316 
317  edm::BranchListIndexes branchListIndexes;
318  edm::BranchListIndexes* pBranchListIndexes = &branchListIndexes;
319  TBranch* branchListIndexBranch = m_->tree_->GetBranch(edm::poolNames::branchListIndexesBranchName().c_str());
320  if (!branchListIndexBranch) {
321  throw edm::Exception(edm::errors::FatalRootError) << "Failed to find branch list index branch in event tree";
322  }
323  branchListIndexBranch->SetAddress(&pBranchListIndexes);
324  branchListIndexBranch->GetEntry(iEntry);
325  m_->branchIDListHelper_->fixBranchListIndexes(branchListIndexes);
326  edm::EventToProcessBlockIndexes dummyEventToProcessBlockIndexes;
327 
328  try {
329  m_->reader_->setEntry(iEntry);
330  auto runAux = std::make_shared<edm::RunAuxiliary>(aux.run(), aux.time(), aux.time());
331  auto rp = std::make_shared<edm::RunPrincipal>(runAux, m_->reg(), m_->pc_, nullptr, 0);
332  auto lbp = std::make_shared<edm::LuminosityBlockPrincipal>(m_->reg(), m_->pc_, nullptr, 0);
333  lbp->setAux(edm::LuminosityBlockAuxiliary(rp->run(), 1, aux.time(), aux.time()));
334  auto history = m_->phreg_->getMapped(eaux->processHistoryID());
335  m_->ep_->fillEventPrincipal(*eaux,
336  history,
337  std::move(eventSelectionIDs),
338  std::move(branchListIndexes),
339  dummyEventToProcessBlockIndexes,
340  *(m_->provRetriever_),
341  m_->reader_.get());
342  lbp->setRunPrincipal(rp);
343  m_->ep_->setLuminosityBlockPrincipal(lbp.get());
344  m_->processNames_ = m_->ep_->processHistory();
345 
346  edm::Event event(*m_->ep_, m_->md_, nullptr);
347 
348  //Make the event principal accessible to edm::Ref's
349  Operate sentry(m_->ep_->prodGetter());
350  process(event);
351  } catch (std::exception const& iEx) {
352  std::cout << "While processing entry " << iEntry << " the following exception was caught \n"
353  << iEx.what() << std::endl;
354  } catch (...) {
355  std::cout << "While processing entry " << iEntry << " an unknown exception was caught" << std::endl;
356  }
357  }
358  return everythingOK_ ? kTRUE : kFALSE;
359 }
360 
362 
364 
366  //look up meta-data
367  //get product registry
368 
369  //std::vector<edm::EventProcessHistoryID> eventProcessHistoryIDs_;
370  TTree* metaDataTree = dynamic_cast<TTree*>(iFile.Get(edm::poolNames::metaDataTreeName().c_str()));
371  if (!metaDataTree) {
372  std::cout << "could not find TTree " << edm::poolNames::metaDataTreeName() << std::endl;
373  everythingOK_ = false;
374  return;
375  }
376  edm::FileFormatVersion* fftPtr = &(m_->fileFormatVersion_);
377  if (metaDataTree->FindBranch(edm::poolNames::fileFormatVersionBranchName().c_str()) != nullptr) {
378  metaDataTree->SetBranchAddress(edm::poolNames::fileFormatVersionBranchName().c_str(), &fftPtr);
379  }
380 
381  edm::ProductRegistry* pReg = &(*m_->reg_);
382  metaDataTree->SetBranchAddress(edm::poolNames::productDescriptionBranchName().c_str(), &(pReg));
383 
384  typedef std::map<edm::ParameterSetID, edm::ParameterSetBlob> PsetMap;
385  PsetMap psetMap;
386  PsetMap* psetMapPtr = &psetMap;
387  if (metaDataTree->FindBranch(edm::poolNames::parameterSetMapBranchName().c_str()) != nullptr) {
388  metaDataTree->SetBranchAddress(edm::poolNames::parameterSetMapBranchName().c_str(), &psetMapPtr);
389  } else {
390  TTree* psetTree = dynamic_cast<TTree*>(iFile.Get(edm::poolNames::parameterSetsTreeName().c_str()));
391  if (nullptr == psetTree) {
393  << "Could not find tree " << edm::poolNames::parameterSetsTreeName() << " in the input file.\n";
394  }
395  typedef std::pair<edm::ParameterSetID, edm::ParameterSetBlob> IdToBlobs;
396  IdToBlobs idToBlob;
397  IdToBlobs* pIdToBlob = &idToBlob;
398  psetTree->SetBranchAddress(edm::poolNames::idToParameterSetBlobsBranchName().c_str(), &pIdToBlob);
399  for (long long i = 0; i != psetTree->GetEntries(); ++i) {
400  psetTree->GetEntry(i);
401  psetMap.insert(idToBlob);
402  }
403  }
404 
406  edm::ProcessHistoryRegistry::vector_type* pHistVectorPtr = &pHistVector;
407  if (metaDataTree->FindBranch(edm::poolNames::processHistoryBranchName().c_str()) != nullptr) {
408  metaDataTree->SetBranchAddress(edm::poolNames::processHistoryBranchName().c_str(), &pHistVectorPtr);
409  }
410 
411  edm::ProcessConfigurationVector procConfigVector;
412  edm::ProcessConfigurationVector* procConfigVectorPtr = &procConfigVector;
413  if (metaDataTree->FindBranch(edm::poolNames::processConfigurationBranchName().c_str()) != nullptr) {
414  metaDataTree->SetBranchAddress(edm::poolNames::processConfigurationBranchName().c_str(), &procConfigVectorPtr);
415  }
416 
417  auto branchIDListsHelper = std::make_shared<edm::BranchIDListHelper>();
418  edm::BranchIDLists const* branchIDListsPtr = &branchIDListsHelper->branchIDLists();
419  if (metaDataTree->FindBranch(edm::poolNames::branchIDListBranchName().c_str()) != nullptr) {
420  metaDataTree->SetBranchAddress(edm::poolNames::branchIDListBranchName().c_str(), &branchIDListsPtr);
421  }
422 
423  metaDataTree->GetEntry(0);
424 
425  for (auto& prod : m_->reg_->productListUpdator()) {
426  prod.second.init();
427  setIsMergeable(prod.second);
428  }
429 
430  // Merge into the registries. For now, we do NOT merge the product registry.
432  for (auto const& entry : psetMap) {
433  edm::ParameterSet pset(entry.second.pset());
434  pset.setID(entry.first);
435  psetRegistry.insertMapped(pset);
436  }
437 
438  for (auto const& ph : pHistVector) {
439  m_->phreg_->registerProcessHistory(ph);
440  }
441 
442  m_->pointerToBranchBuffer_.erase(m_->pointerToBranchBuffer_.begin(), m_->pointerToBranchBuffer_.end());
443 
444  std::unique_ptr<edm::ProductRegistry> newReg = std::make_unique<edm::ProductRegistry>();
445 
446  edm::ProductRegistry::ProductList& prodList = m_->reg_->productListUpdator();
447  {
448  for (auto& item : prodList) {
449  edm::BranchDescription& prod = item.second;
450  //std::cout << "productname = " << item.second << " end " << std::endl;
451  std::string newFriendlyName = edm::friendlyname::friendlyName(prod.className());
452  if (newFriendlyName == prod.friendlyClassName()) {
453  newReg->copyProduct(prod);
454  } else {
455  if (m_->fileFormatVersion_.splitProductIDs()) {
457  << "Cannot change friendly class name algorithm without more development work\n"
458  << "to update BranchIDLists. Contact the framework group.\n";
459  }
461  newBD.updateFriendlyClassName();
462  newReg->copyProduct(newBD);
463  }
464  prod.init();
465  }
466 
467  m_->reg().reset(newReg.release());
468  }
469 
470  edm::ProductRegistry::ProductList& prodList2 = m_->reg_->productListUpdator();
471  std::vector<edm::EventEntryDescription> temp(prodList2.size(), edm::EventEntryDescription());
472  m_->prov_.swap(temp);
473  m_->pointerToBranchBuffer_.reserve(prodList2.size());
474 
475  for (auto& item : prodList2) {
476  edm::BranchDescription& prod = item.second;
477  if (prod.branchType() == edm::InEvent) {
478  prod.init();
479  //NEED to do this and check to see if branch exists
480  if (m_->tree_->GetBranch(prod.branchName().c_str()) == nullptr) {
481  prod.setDropped(true);
482  }
483  prod.setOnDemand(true);
484 
485  //std::cout << "id " << it->first << " branch " << it->second << std::endl;
486  //m_->pointerToBranchBuffer_.push_back(&(*itB));
487  //void* tmp = &(m_->pointerToBranchBuffer_.back());
488  //edm::EventEntryDescription* tmp = &(*itB);
489  //CDJ need to fix provenance and be backwards compatible, for now just don't read the branch
490  //m_->metaTree_->SetBranchAddress(prod.branchName().c_str(), tmp);
491  }
492  }
493  m_->branchIDListHelper_->updateFromInput(*branchIDListsPtr);
494  m_->reg_->setFrozen();
495  m_->bidToDesc_->clear();
496  for (auto const& p : m_->reg_->productList()) {
497  m_->bidToDesc_->emplace(p.second.branchID().id(), &p.second);
498  }
499  m_->ep_ = std::make_shared<edm::EventPrincipal>(
500  m_->reg(), m_->branchIDListHelper(), m_->thinnedAssociationsHelper(), m_->pc_, nullptr);
501  everythingOK_ = true;
502 }
503 
504 //
505 // const member functions
506 //
507 
508 //
509 // static member functions
510 //
std::vector< EventEntryDescription const * > pointerToBranchBuffer_
virtual void terminate(TList &out)=0
std::vector< ProcessConfiguration > ProcessConfigurationVector
std::string const & metaDataTreeName()
Definition: BranchType.cc:159
std::shared_ptr< std::unordered_map< unsigned int, BranchDescription const * > > bidToDesc_
std::vector< BranchIDList > BranchIDLists
Definition: BranchIDList.h:19
virtual void preProcessing(const TList *in, TList &out)=0
std::vector< EventEntryDescription > prov_
edm::propagate_const< TTree * > tree_
edm::propagate_const< std::shared_ptr< edm::root::TFWLiteSelectorMembers > > m_
std::map< BranchKey, BranchDescription > ProductList
edm::propagate_const< std::shared_ptr< ThinnedAssociationsHelper > > thinnedAssociationsHelper_
void mergeReaders_(DelayedReader *) override
void setRefCoreStreamer(bool resetAll=false)
std::string const & processHistoryBranchName()
Definition: BranchType.cc:177
virtual void postProcessing(TList &out)=0
std::shared_ptr< ThinnedAssociationsHelper const > thinnedAssociationsHelper() const
constexpr std::shared_ptr< T > & get_underlying_safe(propagate_const< std::shared_ptr< T >> &iP)
ObjectWithDict castObject(TypeWithDict const &) const
edm::propagate_const< std::shared_ptr< edm::ProductProvenanceRetriever > > provRetriever_
std::shared_ptr< BranchIDListHelper > & branchIDListHelper()
std::string const & parameterSetMapBranchName()
Definition: BranchType.cc:168
virtual void process(const edm::Event &event)=0
std::string const & fileFormatVersionBranchName()
Definition: BranchType.cc:189
std::vector< EventSelectionID > EventSelectionIDVector
std::string const & productDescriptionBranchName()
Definition: BranchType.cc:162
static TypeWithDict byName(std::string const &name)
Definition: TypeWithDict.cc:74
std::string const & branchIDListBranchName()
Definition: BranchType.cc:183
constexpr element_type const * get() const
std::vector< BranchListIndex > BranchListIndexes
virtual std::unique_ptr< EventEntryDescription > getProvenance_(BranchKey const &) const
std::shared_ptr< WrapperBase > getProduct_(BranchID const &k, EDProductGetter const *ep) override
std::string friendlyName(std::string const &iFullName)
std::string const & className() const
signalslot::Signal< void(StreamContext const &, ModuleCallingContext const &)> const * preEventReadFromSourceSignal() const override
std::string const & branchName() const
std::shared_ptr< ProductRegistry const > reg() const
edm::propagate_const< std::shared_ptr< ProductRegistry > > reg_
std::string const & parameterSetsTreeName()
Definition: BranchType.cc:216
edm::propagate_const< std::shared_ptr< FWLiteDelayedReader > > reader_
edm::propagate_const< std::shared_ptr< BranchIDListHelper > > branchIDListHelper_
bool insertMapped(value_type const &v, bool forceUpdate=false)
Definition: Registry.cc:32
std::shared_ptr< ProductRegistry > & reg()
std::string const & idToParameterSetBlobsBranchName()
Definition: BranchType.cc:218
edm::propagate_const< std::shared_ptr< edm::EventPrincipal > > ep_
std::string const & eventSelectionsBranchName()
Definition: BranchType.cc:210
void SlaveTerminate() override
std::unique_ptr< WrapperBase > getTheProduct(BranchID const &k) const
void SlaveBegin(TTree *) override
std::string const & processConfigurationBranchName()
Definition: BranchType.cc:180
std::shared_ptr< BranchIDListHelper const > branchIDListHelper() const
std::string wrappedClassName(std::string const &iFullName)
void Begin(TTree *) override
void * address() const
std::shared_ptr< ThinnedAssociationsHelper > & thinnedAssociationsHelper()
Bool_t Process(Long64_t) override
edm::propagate_const< std::shared_ptr< std::unordered_map< unsigned int, BranchDescription const * > > > bidToDesc_
edm::propagate_const< std::shared_ptr< ProcessHistoryRegistry > > phreg_
HLT enums.
std::string const & branchListIndexesBranchName()
Definition: BranchType.cc:212
std::string const & BranchTypeToAuxiliaryBranchName(BranchType const &branchType)
Definition: BranchType.cc:116
signalslot::Signal< void(StreamContext const &, ModuleCallingContext const &)> const * postEventReadFromSourceSignal() const override
void setIsMergeable(BranchDescription &)
def move(src, dest)
Definition: eostools.py:511
static Registry * instance()
Definition: Registry.cc:12
Definition: event.py:1
void Init(TTree *) override
virtual void begin(TList *&in)=0