CMS 3D CMS Logo

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