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 
25 #include "DataFormats/Provenance/interface/EventEntryDescription.h" // kludge to allow compilation
49 
50 // system include files
51 #include "TBranch.h"
52 #include "TChain.h"
53 #include "TFile.h"
54 #include "TTree.h"
55 
56 #include <iostream>
57 #include <memory>
58 #include <string>
59 #include <vector>
60 #include <unordered_map>
61 
62 namespace edm {
63  namespace root {
65  public:
67  void setEntry(Long64_t iEntry) { entry_ = iEntry; }
68  void setTree(TTree* iTree) {eventTree_ = iTree;}
69  void set(std::shared_ptr<std::unordered_map<unsigned int, BranchDescription const*>> iMap) { bidToDesc_ = std::move(iMap);}
70  private:
71  std::unique_ptr<WrapperBase> getTheProduct(BranchID const& k) const;
72  std::unique_ptr<WrapperBase> getProduct_(BranchID const& k, EDProductGetter const* ep) override;
73  virtual std::unique_ptr<EventEntryDescription> getProvenance_(BranchKey const&) const {
74  return std::unique_ptr<EventEntryDescription>();
75  }
76  void mergeReaders_(DelayedReader*) override {}
77  void reset_() override {}
78 
80  return nullptr;
81  }
83  return nullptr;
84  };
85 
86  Long64_t entry_;
87  TTree* eventTree_;
88  std::shared_ptr<std::unordered_map<unsigned int, BranchDescription const*>> bidToDesc_;
89  };
90 
91  std::unique_ptr<WrapperBase>
93  return getTheProduct(k);
94  }
95 
96  std::unique_ptr<WrapperBase>
98  auto itFind= bidToDesc_->find(k.id());
99  if(itFind == bidToDesc_->end()) {
100  throw Exception(errors::ProductNotFound) << "could not find entry for product " << k;
101  }
102  BranchDescription const& bDesc = *(itFind->second);
103 
104  TBranch* branch= eventTree_->GetBranch(bDesc.branchName().c_str());
105  if(nullptr == branch) {
106  throw cms::Exception("MissingBranch")
107  << "could not find branch named '" << bDesc.branchName() << "'"
108  << "\n Perhaps the data being requested was not saved in this file?";
109  }
110  //find the class type
111  std::string const fullName = wrappedClassName(bDesc.className());
112  TypeWithDict classType = TypeWithDict::byName(fullName);
113  if(!bool(classType)) {
114  throw cms::Exception("MissingDictionary")
115  << "could not find dictionary for type '" << fullName << "'"
116  << "\n Please make sure all the necessary libraries are available.";
117  }
118 
119  //create an instance of it
120  ObjectWithDict wrapperObj = classType.construct();
121  if(nullptr == wrapperObj.address()) {
122  throw cms::Exception("FailedToCreate") << "could not create an instance of '" << fullName << "'";
123  }
124  void* address = wrapperObj.address();
125  branch->SetAddress(&address);
126  ObjectWithDict edProdObj = wrapperObj.castObject(TypeWithDict::byName("edm::WrapperBase"));
127 
128  WrapperBase* prod = reinterpret_cast<WrapperBase*>(edProdObj.address());
129 
130  if(nullptr == prod) {
131  throw cms::Exception("FailedConversion")
132  << "failed to convert a '" << fullName
133  << "' to a edm::WrapperBase."
134  << "Please contact developers since something is very wrong.";
135  }
136  branch->GetEntry(entry_);
137  return std::unique_ptr<WrapperBase>(prod);
138  }
139 
142  tree_(nullptr),
143  reg_(new ProductRegistry()),
144  bidToDesc_(std::make_shared<std::unordered_map<unsigned int, BranchDescription const*>>()),
145  phreg_(new ProcessHistoryRegistry()),
146  branchIDListHelper_(new BranchIDListHelper()),
147  // Note that thinned collections are not supported yet, the next
148  // line just makes it compile but when the Ref or Ptr tries to
149  // find the thinned collection it will report them not found.
150  // More work needed here if this is needed (we think no one
151  // is using TFWLiteSelector anymore and intend to implement
152  // this properly if it turns out we are wrong)
153  thinnedAssociationsHelper_(new ThinnedAssociationsHelper()),
154  processNames_(),
155  reader_(new FWLiteDelayedReader),
156  prov_(),
157  pointerToBranchBuffer_(),
158  provRetriever_(new edm::ProductProvenanceRetriever(0)) {
159  reader_->set(get_underlying_safe(bidToDesc_));
160  }
161  void setTree(TTree* iTree) {
162  tree_ = iTree;
163  reader_->setTree(iTree);
164  }
165 
166  TTree const* tree() const {return get_underlying_safe(tree_);}
167  TTree*& tree() {return get_underlying_safe(tree_);}
168  std::shared_ptr<ProductRegistry const> reg() const {return get_underlying_safe(reg_);}
169  std::shared_ptr<ProductRegistry>& reg() {return get_underlying_safe(reg_);}
170  std::shared_ptr<BranchIDListHelper const> branchIDListHelper() const {return get_underlying_safe(branchIDListHelper_);}
171  std::shared_ptr<BranchIDListHelper>& branchIDListHelper() {return get_underlying_safe(branchIDListHelper_);}
172  std::shared_ptr<ThinnedAssociationsHelper const> thinnedAssociationsHelper() const {return get_underlying_safe(thinnedAssociationsHelper_);}
173  std::shared_ptr<ThinnedAssociationsHelper>& thinnedAssociationsHelper() {return get_underlying_safe(thinnedAssociationsHelper_);}
174 
182  std::vector<EventEntryDescription> prov_;
183  std::vector<EventEntryDescription const*> pointerToBranchBuffer_;
185 
190  };
191  }
192 }
193 
194 
195 //
196 // constants, enums and typedefs
197 //
198 
199 //
200 // static data member definitions
201 //
202 
203 //
204 // constructors and destructor
205 //
206 TFWLiteSelectorBasic::TFWLiteSelectorBasic() : m_(new edm::root::TFWLiteSelectorMembers),
207  everythingOK_(false) {
208 }
209 
210 // TFWLiteSelectorBasic::TFWLiteSelectorBasic(TFWLiteSelectorBasic const& rhs)
211 // {
212 // // do actual copying here;
213 // }
214 
216 }
217 
218 //
219 // assignment operators
220 //
221 // TFWLiteSelectorBasic const& TFWLiteSelectorBasic::operator=(TFWLiteSelectorBasic const& rhs)
222 // {
223 // //An exception safe implementation is
224 // TFWLiteSelectorBasic temp(rhs);
225 // swap(rhs);
226 //
227 // return *this;
228 // }
229 
230 //
231 // member functions
232 //
233 void
235  Init(iTree);
236  begin(fInput);
237 }
238 
239 void
241  Init(iTree);
242  preProcessing(fInput, *fOutput);
243 }
244 
245 void
247  if(iTree == nullptr) return;
248  m_->setTree(iTree);
249 }
250 
251 
252 Bool_t
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 
284  ~Operate() {setRefCoreStreamer(old_);}
285  private:
286  edm::EDProductGetter const* old_;
287  };
288 }
289 
290 Bool_t
292  //std::cout << "Process start" << std::endl;
293  if(everythingOK_) {
294  std::unique_ptr<edm::EventAuxiliary> eaux = std::make_unique<edm::EventAuxiliary>();
295  edm::EventAuxiliary& aux = *eaux;
296  edm::EventAuxiliary* pAux= eaux.get();
297  TBranch* branch = m_->tree_->GetBranch(edm::BranchTypeToAuxiliaryBranchName(edm::InEvent).c_str());
298 
299  branch->SetAddress(&pAux);
300  branch->GetEntry(iEntry);
301 
302 //NEW m_->processNames_ = aux.processHistory();
303 
304 // std::cout << "ProcessNames\n";
305 // for(auto const& name : m_->processNames_) {
306 // std::cout << " " << name << std::endl;
307 // }
308 
309  edm::EventSelectionIDVector eventSelectionIDs;
310  edm::EventSelectionIDVector* pEventSelectionIDVector = &eventSelectionIDs;
311  TBranch* eventSelectionsBranch = m_->tree_->GetBranch(edm::poolNames::eventSelectionsBranchName().c_str());
312  if(!eventSelectionsBranch) {
314  << "Failed to find event Selections branch in event tree";
315  }
316  eventSelectionsBranch->SetAddress(&pEventSelectionIDVector);
317  eventSelectionsBranch->GetEntry(iEntry);
318 
319  edm::BranchListIndexes branchListIndexes;
320  edm::BranchListIndexes* pBranchListIndexes = &branchListIndexes;
321  TBranch* branchListIndexBranch = m_->tree_->GetBranch(edm::poolNames::branchListIndexesBranchName().c_str());
322  if(!branchListIndexBranch) {
324  << "Failed to find branch list index branch in event tree";
325  }
326  branchListIndexBranch->SetAddress(&pBranchListIndexes);
327  branchListIndexBranch->GetEntry(iEntry);
328  m_->branchIDListHelper_->fixBranchListIndexes(branchListIndexes);
329 
330  try {
331  m_->reader_->setEntry(iEntry);
332  auto runAux = std::make_shared<edm::RunAuxiliary>(aux.run(), aux.time(), aux.time());
333  auto rp = std::make_shared<edm::RunPrincipal>(runAux, m_->reg(), m_->pc_, nullptr, 0);
334  auto lbp = std::make_shared<edm::LuminosityBlockPrincipal>(m_->reg(), m_->pc_, nullptr, 0);
335  lbp->setAux(edm::LuminosityBlockAuxiliary(rp->run(), 1, aux.time(), aux.time()));
336  m_->ep_->fillEventPrincipal(*eaux,
337  *m_->phreg_,
338  std::move(eventSelectionIDs),
339  std::move(branchListIndexes),
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 
361 void
363  postProcessing(*fOutput);
364 }
365 
366 void
368  terminate(*fOutput);
369 }
370 
371 void
373  //look up meta-data
374  //get product registry
375 
376  //std::vector<edm::EventProcessHistoryID> eventProcessHistoryIDs_;
377  TTree* metaDataTree = dynamic_cast<TTree*>(iFile.Get(edm::poolNames::metaDataTreeName().c_str()));
378  if(!metaDataTree) {
379  std::cout << "could not find TTree " << edm::poolNames::metaDataTreeName() << std::endl;
380  everythingOK_ = false;
381  return;
382  }
383  edm::FileFormatVersion* fftPtr = &(m_->fileFormatVersion_);
384  if(metaDataTree->FindBranch(edm::poolNames::fileFormatVersionBranchName().c_str()) != nullptr) {
385  metaDataTree->SetBranchAddress(edm::poolNames::fileFormatVersionBranchName().c_str(), &fftPtr);
386  }
387 
388 
389  edm::ProductRegistry* pReg = &(*m_->reg_);
390  metaDataTree->SetBranchAddress(edm::poolNames::productDescriptionBranchName().c_str(), &(pReg));
391 
392  typedef std::map<edm::ParameterSetID, edm::ParameterSetBlob> PsetMap;
393  PsetMap psetMap;
394  PsetMap *psetMapPtr = &psetMap;
395  if(metaDataTree->FindBranch(edm::poolNames::parameterSetMapBranchName().c_str()) != nullptr) {
396  metaDataTree->SetBranchAddress(edm::poolNames::parameterSetMapBranchName().c_str(), &psetMapPtr);
397  } else {
398  TTree* psetTree = dynamic_cast<TTree *>(iFile.Get(edm::poolNames::parameterSetsTreeName().c_str()));
399  if(nullptr == psetTree) {
401  << " in the input file.\n";
402  }
403  typedef std::pair<edm::ParameterSetID, edm::ParameterSetBlob> IdToBlobs;
404  IdToBlobs idToBlob;
405  IdToBlobs* pIdToBlob = &idToBlob;
406  psetTree->SetBranchAddress(edm::poolNames::idToParameterSetBlobsBranchName().c_str(), &pIdToBlob);
407  for(long long i = 0; i != psetTree->GetEntries(); ++i) {
408  psetTree->GetEntry(i);
409  psetMap.insert(idToBlob);
410  }
411  }
412 
414  edm::ProcessHistoryRegistry::vector_type *pHistVectorPtr = &pHistVector;
415  if(metaDataTree->FindBranch(edm::poolNames::processHistoryBranchName().c_str()) != nullptr) {
416  metaDataTree->SetBranchAddress(edm::poolNames::processHistoryBranchName().c_str(), &pHistVectorPtr);
417  }
418 
419 
420  edm::ProcessConfigurationVector procConfigVector;
421  edm::ProcessConfigurationVector* procConfigVectorPtr = &procConfigVector;
422  if(metaDataTree->FindBranch(edm::poolNames::processConfigurationBranchName().c_str()) != nullptr) {
423  metaDataTree->SetBranchAddress(edm::poolNames::processConfigurationBranchName().c_str(), &procConfigVectorPtr);
424  }
425 
426  auto branchIDListsHelper = std::make_shared<edm::BranchIDListHelper>();
427  edm::BranchIDLists const* branchIDListsPtr = &branchIDListsHelper->branchIDLists();
428  if(metaDataTree->FindBranch(edm::poolNames::branchIDListBranchName().c_str()) != nullptr) {
429  metaDataTree->SetBranchAddress(edm::poolNames::branchIDListBranchName().c_str(), &branchIDListsPtr);
430  }
431 
432  metaDataTree->GetEntry(0);
433 
434  for(auto& prod : m_->reg_->productListUpdator()) {
435  prod.second.init();
436  }
437 
438  // Merge into the registries. For now, we do NOT merge the product registry.
440  for(auto const& entry : psetMap) {
441  edm::ParameterSet pset(entry.second.pset());
442  pset.setID(entry.first);
443  psetRegistry.insertMapped(pset);
444  }
445 
446  for(auto const& ph : pHistVector) {
447  m_->phreg_->registerProcessHistory(ph);
448  }
449 
450  m_->pointerToBranchBuffer_.erase(m_->pointerToBranchBuffer_.begin(),
451  m_->pointerToBranchBuffer_.end());
452 
453  std::unique_ptr<edm::ProductRegistry> newReg = std::make_unique<edm::ProductRegistry>();
454 
455  edm::ProductRegistry::ProductList& prodList = m_->reg_->productListUpdator();
456  {
457  for(auto& item : prodList) {
458  edm::BranchDescription& prod = item.second;
459  //std::cout << "productname = " << item.second << " end " << std::endl;
460  std::string newFriendlyName = edm::friendlyname::friendlyName(prod.className());
461  if(newFriendlyName == prod.friendlyClassName()) {
462  newReg->copyProduct(prod);
463  } else {
464  if(m_->fileFormatVersion_.splitProductIDs()) {
466  << "Cannot change friendly class name algorithm without more development work\n"
467  << "to update BranchIDLists. Contact the framework group.\n";
468  }
469  edm::BranchDescription newBD(prod);
470  newBD.updateFriendlyClassName();
471  newReg->copyProduct(newBD);
472  }
473  prod.init();
474  }
475 
476  m_->reg().reset(newReg.release());
477  }
478 
479  edm::ProductRegistry::ProductList& prodList2 = m_->reg_->productListUpdator();
480  std::vector<edm::EventEntryDescription> temp(prodList2.size(), edm::EventEntryDescription());
481  m_->prov_.swap(temp);
482  m_->pointerToBranchBuffer_.reserve(prodList2.size());
483 
484  for(auto& item : prodList2) {
485  edm::BranchDescription& prod = item.second;
486  if(prod.branchType() == edm::InEvent) {
487  prod.init();
488  //NEED to do this and check to see if branch exists
489  if(m_->tree_->GetBranch(prod.branchName().c_str()) == nullptr) {
490  prod.setDropped(true);
491  }
492 
493  //std::cout << "id " << it->first << " branch " << it->second << std::endl;
494  //m_->pointerToBranchBuffer_.push_back(&(*itB));
495  //void* tmp = &(m_->pointerToBranchBuffer_.back());
496  //edm::EventEntryDescription* tmp = &(*itB);
497  //CDJ need to fix provenance and be backwards compatible, for now just don't read the branch
498  //m_->metaTree_->SetBranchAddress(prod.branchName().c_str(), tmp);
499  }
500  }
501  m_->branchIDListHelper_->updateFromInput(*branchIDListsPtr);
502  m_->reg_->setFrozen();
503  m_->bidToDesc_->clear();
504  for(auto const&p: m_->reg_->productList()) {
505  m_->bidToDesc_->emplace(p.second.branchID().id(),&p.second);
506  }
507  m_->ep_ = std::make_shared<edm::EventPrincipal>(m_->reg(), m_->branchIDListHelper(), m_->thinnedAssociationsHelper(), m_->pc_, nullptr);
508  everythingOK_ = true;
509 }
510 
511 //
512 // const member functions
513 //
514 
515 //
516 // static member functions
517 //
signalslot::Signal< void(StreamContext const &, ModuleCallingContext const &)> const * preEventReadFromSourceSignal() const override
std::vector< EventEntryDescription const * > pointerToBranchBuffer_
std::string const & idToParameterSetBlobsBranchName()
Definition: BranchType.cc:255
virtual void terminate(TList &out)=0
std::vector< ProcessConfiguration > ProcessConfigurationVector
std::string const & branchName() const
std::shared_ptr< ThinnedAssociationsHelper const > thinnedAssociationsHelper() const
std::string const & BranchTypeToAuxiliaryBranchName(BranchType const &branchType)
Definition: BranchType.cc:115
std::shared_ptr< std::unordered_map< unsigned int, BranchDescription const * > > bidToDesc_
BranchType const & branchType() const
std::vector< BranchIDList > BranchIDLists
Definition: BranchIDList.h:19
std::shared_ptr< BranchIDListHelper const > branchIDListHelper() const
Definition: chain.py:1
virtual std::unique_ptr< EventEntryDescription > getProvenance_(BranchKey const &) const
void * address() const
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_
ObjectWithDict castObject(TypeWithDict const &) const
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 & fileFormatVersionBranchName()
Definition: BranchType.cc:218
virtual void postProcessing(TList &out)=0
std::string const & eventSelectionsBranchName()
Definition: BranchType.cc:243
#define nullptr
edm::propagate_const< std::shared_ptr< edm::ProductProvenanceRetriever > > provRetriever_
std::shared_ptr< BranchIDListHelper > & branchIDListHelper()
virtual void process(const edm::Event &event)=0
unsigned int id() const
Definition: BranchID.h:23
signalslot::Signal< void(StreamContext const &, ModuleCallingContext const &)> const * postEventReadFromSourceSignal() const override
std::vector< EventSelectionID > EventSelectionIDVector
std::string const & parameterSetsTreeName()
Definition: BranchType.cc:251
static TypeWithDict byName(std::string const &name)
Definition: TypeWithDict.cc:82
std::vector< BranchListIndex > BranchListIndexes
std::string const & className() const
std::string friendlyName(std::string const &iFullName)
edm::propagate_const< std::shared_ptr< ProductRegistry > > reg_
edm::propagate_const< std::shared_ptr< FWLiteDelayedReader > > reader_
edm::propagate_const< std::shared_ptr< BranchIDListHelper > > branchIDListHelper_
std::string const & friendlyClassName() const
void setDropped(bool isDropped)
bool insertMapped(value_type const &v, bool forceUpdate=false)
Definition: Registry.cc:36
std::string const & metaDataTreeName()
Definition: BranchType.cc:168
std::shared_ptr< T > & get_underlying_safe(propagate_const< std::shared_ptr< T >> &iP)
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger but the state exists so we define the behavior If all triggers are the negative crieriion will lead to accepting the event(this again matches the behavior of"!*"before the partial wildcard feature was incorporated).The per-event"cost"of each negative criterion with multiple relevant triggers is about the same as!*was in the past
std::shared_ptr< ProductRegistry > & reg()
std::string const & parameterSetMapBranchName()
Definition: BranchType.cc:183
element_type const * get() const
std::string const & processHistoryBranchName()
Definition: BranchType.cc:198
int k[5][pyjets_maxn]
edm::propagate_const< std::shared_ptr< edm::EventPrincipal > > ep_
void SlaveTerminate() override
void SlaveBegin(TTree *) override
std::string wrappedClassName(std::string const &iFullName)
void Begin(TTree *) override
std::unique_ptr< WrapperBase > getProduct_(BranchID const &k, EDProductGetter const *ep) override
std::string const & productDescriptionBranchName()
Definition: BranchType.cc:173
std::string const & processConfigurationBranchName()
Definition: BranchType.cc:203
std::shared_ptr< ThinnedAssociationsHelper > & thinnedAssociationsHelper()
Bool_t Process(Long64_t) override
std::unique_ptr< WrapperBase > getTheProduct(BranchID const &k) const
edm::propagate_const< std::shared_ptr< ProcessHistoryRegistry > > phreg_
std::shared_ptr< ProductRegistry const > reg() const
HLT enums.
std::string const & branchIDListBranchName()
Definition: BranchType.cc:208
std::string const & branchListIndexesBranchName()
Definition: BranchType.cc:247
def move(src, dest)
Definition: eostools.py:510
static Registry * instance()
Definition: Registry.cc:13
void Init(TTree *) override
virtual void begin(TList *&in)=0