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