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