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