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