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