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 // system include files
15 #include "TFile.h"
16 #include "TTree.h"
17 #include "TChain.h"
18 #include "TBranch.h"
19 #include "TClass.h"
20 #include "Reflex/Type.h"
21 // user include files
23 
28 #include "DataFormats/Provenance/interface/EventEntryDescription.h" // kludge to allow compilation
53 
54 namespace edm {
55  namespace root {
57  public:
59  void setEntry(Long64_t iEntry) { entry_ = iEntry; }
60  void setTree(TTree* iTree) {eventTree_ = iTree;}
61  void set(boost::shared_ptr<ProductRegistry const> iReg) { reg_ = iReg;}
62  private:
63  virtual std::auto_ptr<EDProduct> getProduct_(BranchKey const& k, EDProductGetter const* ep) const;
64  virtual std::auto_ptr<EventEntryDescription> getProvenance_(BranchKey const&) const {
65  return std::auto_ptr<EventEntryDescription>();
66  }
67  Long64_t entry_;
68  TTree* eventTree_;
69  boost::shared_ptr<ProductRegistry const>(reg_);
70  };
71 
72  std::auto_ptr<EDProduct>
74  ProductRegistry::ProductList::const_iterator itFind= reg_->productList().find(k);
75  if(itFind == reg_->productList().end()) {
76  throw Exception(errors::ProductNotFound)<<"could not find entry for product "<<k;
77  }
78  BranchDescription const& bDesc = itFind->second;
79 
80  TBranch* branch= eventTree_->GetBranch(bDesc.branchName().c_str());
81  if(0 == branch) {
82  throw cms::Exception("MissingBranch")
83  <<"could not find branch named '"<<bDesc.branchName()<<"'"
84  <<"\n Perhaps the data being requested was not saved in this file?";
85  }
86  //find the class type
87  std::string const fullName = wrappedClassName(bDesc.className());
88  Reflex::Type classType = Reflex::Type::ByName(fullName);
89  if(classType == Reflex::Type()) {
90  throw cms::Exception("MissingDictionary")
91  <<"could not find dictionary for type '"<<fullName<<"'"
92  <<"\n Please make sure all the necessary libraries are available.";
93  return std::auto_ptr<EDProduct>();
94  }
95 
96  //We can't use reflex to create the instance since Reflex uses 'malloc' instead of new
97  /*
98  //use reflex to create an instance of it
99  Reflex::Object wrapperObj = classType.Construct();
100  if(0 == wrapperObj.Address()) {
101  throw cms::Exception("FailedToCreate") <<"could not create an instance of '"<<fullName<<"'";
102  }
103  void* address = wrapperObj.Address();
104  */
105  TClass* rootClassType=TClass::GetClass(classType.TypeInfo());
106  if(0 == rootClassType) {
107  throw cms::Exception("MissingRootDictionary")
108  <<"could not find a ROOT dictionary for type '"<<fullName<<"'"
109  <<"\n Please make sure all the necessary libraries are available.";
110  return std::auto_ptr<EDProduct>();
111  }
112  void* address = rootClassType->New();
113  branch->SetAddress(&address);
114 
115  /*
116  Reflex::Object edProdObj = wrapperObj.CastObject(Reflex::Type::ByName("edm::EDProduct"));
117 
118  EDProduct* prod = reinterpret_cast<EDProduct*>(edProdObj.Address());
119  */
120  static TClass* edproductTClass = TClass::GetClass(typeid(EDProduct));
121  EDProduct* prod = reinterpret_cast<EDProduct*>(rootClassType->DynamicCast(edproductTClass,address,true));
122 
123  if(0 == prod) {
124  throw cms::Exception("FailedConversion")
125  << "failed to convert a '" << fullName
126  << "' to a edm::EDProduct."
127  << "Please contact developers since something is very wrong.";
128  }
129  branch->GetEntry(entry_);
130  return std::auto_ptr<EDProduct>(prod);
131  }
132 
135  tree_(0),
136  metaTree_(0),
137  reg_(new ProductRegistry()),
138  processNames_(),
140  productMap_(),
141  prov_(),
143  {
144  reader_->set(reg_);}
145  void setTree(TTree* iTree) {
146  tree_ = iTree;
147  reader_->setTree(iTree);
148  }
149  void setMetaTree(TTree* iTree) {
150  metaTree_ = iTree;
151  }
152  TTree* tree_;
153  TTree* metaTree_;
154  boost::shared_ptr<ProductRegistry> reg_;
156  boost::shared_ptr<FWLiteDelayedReader> reader_;
157  typedef std::map<ProductID, BranchDescription> ProductMap;
159  std::vector<EventEntryDescription> prov_;
160  std::vector<EventEntryDescription*> pointerToBranchBuffer_;
162  };
163  }
164 }
165 
166 
167 //
168 // constants, enums and typedefs
169 //
170 
171 //
172 // static data member definitions
173 //
174 
175 //
176 // constructors and destructor
177 //
178 TFWLiteSelectorBasic::TFWLiteSelectorBasic() : m_(new edm::root::TFWLiteSelectorMembers),
179  everythingOK_(false) {
180 }
181 
182 // TFWLiteSelectorBasic::TFWLiteSelectorBasic(TFWLiteSelectorBasic const& rhs)
183 // {
184 // // do actual copying here;
185 // }
186 
188 {
189 }
190 
191 //
192 // assignment operators
193 //
194 // TFWLiteSelectorBasic const& TFWLiteSelectorBasic::operator=(TFWLiteSelectorBasic const& rhs)
195 // {
196 // //An exception safe implementation is
197 // TFWLiteSelectorBasic temp(rhs);
198 // swap(rhs);
199 //
200 // return *this;
201 // }
202 
203 //
204 // member functions
205 //
206 void
208  Init(iTree);
209  begin(fInput);
210 }
211 
212 void
214  Init(iTree);
215  preProcessing(fInput,*fOutput);
216 }
217 
218 void
220  if(iTree==0) return;
221  m_->setTree(iTree);
222 }
223 
224 
225 Bool_t
227  //std::cout <<"Notify start"<<std::endl;
228  //we have switched to a new file
229  //get new file from Tree
230  if(0==m_->tree_) {
231  std::cout <<"No tree"<<std::endl;
232  return kFALSE;
233  }
234  TFile* file = m_->tree_->GetCurrentFile();
235  if(0 == file) {
236  //When in Rome, do as the Romans
237  TChain* chain = dynamic_cast<TChain*>(m_->tree_);
238  if(0 == chain) {
239  std::cout <<"No file"<<std::endl;
240  return kFALSE;
241  }
242  file = chain->GetFile();
243  if(0==file) {
244  std::cout <<"No file"<<std::endl;
245  return kFALSE;
246  }
247  }
248  setupNewFile(*file);
249  return everythingOK_ ? kTRUE: kFALSE;
250 }
251 
252 namespace {
253  struct Operate {
254  Operate(edm::EDProductGetter const* iGetter): old_(setRefCoreStreamer(iGetter)) {
255  }
256 
257  ~Operate() {setRefCoreStreamer(old_);}
258  private:
259  edm::EDProductGetter const* old_;
260  };
261 }
262 
263 Bool_t
265  //std::cout <<"Process start"<<std::endl;
266  if(everythingOK_) {
267  std::auto_ptr<edm::EventAuxiliary> eaux(new edm::EventAuxiliary());
268  edm::EventAuxiliary& aux = *eaux;
269  edm::EventAuxiliary* pAux= eaux.get();
270  TBranch* branch = m_->tree_->GetBranch(edm::BranchTypeToAuxiliaryBranchName(edm::InEvent).c_str());
271 
272  branch->SetAddress(&pAux);
273  branch->GetEntry(iEntry);
274 
275 //NEW m_->processNames_ = aux.processHistory();
276 
277 // std::cout <<"ProcessNames\n";
278 // for(edm::ProcessNameList::const_iterator itName = m_->processNames_.begin(),
279 // itNameEnd = m_->processNames_.end();
280 // itName != itNameEnd;
281 // ++itName) {
282 // std::cout <<" "<<*itName<< std::endl;
283  // }
284 
285  boost::shared_ptr<edm::EventSelectionIDVector> eventSelectionIDs_(new edm::EventSelectionIDVector);
286  edm::EventSelectionIDVector* pEventSelectionIDVector = eventSelectionIDs_.get();
287  TBranch* eventSelectionsBranch = m_->tree_->GetBranch(edm::poolNames::eventSelectionsBranchName().c_str());
288  if (!eventSelectionsBranch) {
290  << "Failed to find event Selections branch in event tree";
291  }
292  eventSelectionsBranch->SetAddress(&pEventSelectionIDVector);
293  eventSelectionsBranch->GetEntry(iEntry);
294 
295  boost::shared_ptr<edm::BranchListIndexes> branchListIndexes_(new edm::BranchListIndexes);
296  edm::BranchListIndexes* pBranchListIndexes = branchListIndexes_.get();
297  TBranch* branchListIndexBranch = m_->tree_->GetBranch(edm::poolNames::branchListIndexesBranchName().c_str());
298  if (!branchListIndexBranch) {
300  << "Failed to find branch list index branch in event tree";
301  }
302  branchListIndexBranch->SetAddress(&pBranchListIndexes);
303  branchListIndexBranch->GetEntry(iEntry);
304 
305  try {
306  m_->reader_->setEntry(iEntry);
308  boost::shared_ptr<edm::ProductRegistry const> reg(m_->reg_);
309  boost::shared_ptr<edm::RunAuxiliary> runAux(new edm::RunAuxiliary(aux.run(), aux.time(), aux.time()));
310  boost::shared_ptr<edm::RunPrincipal> rp(new edm::RunPrincipal(runAux, reg, pc));
311  boost::shared_ptr<edm::LuminosityBlockAuxiliary> lumiAux(
312  new edm::LuminosityBlockAuxiliary(rp->run(), 1, aux.time(), aux.time()));
313  boost::shared_ptr<edm::LuminosityBlockPrincipal>lbp(
314  new edm::LuminosityBlockPrincipal(lumiAux, reg, pc, rp));
315  boost::shared_ptr<edm::BranchMapper> mapper(new edm::BranchMapper);
316  edm::EventPrincipal ep(reg, pc);
317  ep.fillEventPrincipal(eaux, lbp, eventSelectionIDs_, branchListIndexes_, mapper, m_->reader_);
318  m_->processNames_ = ep.processHistory();
319 
321  edm::Event event(ep,md);
322 
323  //Make the event principal accessible to edm::Ref's
324  Operate sentry(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 kTRUE;
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()) != 0) {
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  m_->reg_->setFrozen();
368  typedef std::map<edm::ParameterSetID, edm::ParameterSetBlob> PsetMap;
369  PsetMap psetMap;
370  PsetMap *psetMapPtr = &psetMap;
371  if(metaDataTree->FindBranch(edm::poolNames::parameterSetMapBranchName().c_str()) != 0) {
372  metaDataTree->SetBranchAddress(edm::poolNames::parameterSetMapBranchName().c_str(), &psetMapPtr);
373  } else {
374  TTree* psetTree = dynamic_cast<TTree *>(iFile.Get(edm::poolNames::parameterSetsTreeName().c_str()));
375  if(0==psetTree) {
377  << " in the input file.\n";
378  }
379  typedef std::pair<edm::ParameterSetID, edm::ParameterSetBlob> IdToBlobs;
380  IdToBlobs idToBlob;
381  IdToBlobs* pIdToBlob = &idToBlob;
382  psetTree->SetBranchAddress(edm::poolNames::idToParameterSetBlobsBranchName().c_str(), &pIdToBlob);
383  for(long long i = 0; i != psetTree->GetEntries(); ++i) {
384  psetTree->GetEntry(i);
385  psetMap.insert(idToBlob);
386  }
387  }
388 
390  edm::ProcessHistoryRegistry::vector_type *pHistVectorPtr = &pHistVector;
391  if(metaDataTree->FindBranch(edm::poolNames::processHistoryBranchName().c_str()) != 0) {
392  metaDataTree->SetBranchAddress(edm::poolNames::processHistoryBranchName().c_str(), &pHistVectorPtr);
393  }
394 
395 
396  edm::ProcessConfigurationVector procConfigVector;
397  edm::ProcessConfigurationVector * procConfigVectorPtr=&procConfigVector;
398  if(metaDataTree->FindBranch(edm::poolNames::processConfigurationBranchName().c_str()) != 0) {
399  metaDataTree->SetBranchAddress(edm::poolNames::processConfigurationBranchName().c_str(), &procConfigVectorPtr);
400  }
401 
402  std::auto_ptr<edm::BranchIDListRegistry::collection_type> branchIDListsAPtr(new edm::BranchIDListRegistry::collection_type);
403  edm::BranchIDListRegistry::collection_type *branchIDListsPtr = branchIDListsAPtr.get();
404  if(metaDataTree->FindBranch(edm::poolNames::branchIDListBranchName().c_str()) != 0) {
405  metaDataTree->SetBranchAddress(edm::poolNames::branchIDListBranchName().c_str(), &branchIDListsPtr);
406  }
407 
408  metaDataTree->GetEntry(0);
409 
410  m_->metaTree_ = dynamic_cast<TTree*>(iFile.Get(edm::poolNames::eventMetaDataTreeName().c_str()));
411  //provBranch->GetEntry(iEntry);
412  if(0 == m_->metaTree_) {
413  std::cout <<"could not find TTree "<<edm::poolNames::eventMetaDataTreeName() <<std::endl;
414  everythingOK_ = false;
415  return;
416  }
417 
418  // Merge into the registries. For now, we do NOT merge the product registry.
420  for (PsetMap::const_iterator i = psetMap.begin(), iEnd = psetMap.end();
421  i != iEnd; ++i) {
422  edm::ParameterSet pset(i->second.pset());
423  pset.setID(i->first);
424  psetRegistry.insertMapped(pset);
425  }
426 
429 
430  m_->productMap_.erase(m_->productMap_.begin(),m_->productMap_.end());
431  m_->pointerToBranchBuffer_.erase(m_->pointerToBranchBuffer_.begin(),
432  m_->pointerToBranchBuffer_.end());
433 
434  fillProductRegistryTransients(procConfigVector,*m_->reg_);
435  std::auto_ptr<edm::ProductRegistry> newReg(new edm::ProductRegistry());
436 
437 
438  edm::ProductRegistry::ProductList const& prodList = m_->reg_->productList();
439 {
440  for(edm::ProductRegistry::ProductList::const_iterator it = prodList.begin(), itEnd = prodList.end();
441  it != itEnd; ++it) {
442  edm::BranchDescription const& prod = it->second;
443  //std::cout << "productname = "<<it->second<<" end "<<std::endl;
444  std::string newFriendlyName = edm::friendlyname::friendlyName(prod.className());
445  if(newFriendlyName == prod.friendlyClassName()) {
446  newReg->copyProduct(prod);
447  } else {
448 
449  if(m_->fileFormatVersion_.splitProductIDs()) {
451  << "Cannot change friendly class name algorithm without more development work\n"
452  << "to update BranchIDLists. Contact the framework group.\n";
453  }
454  edm::BranchDescription newBD(prod);
455  newBD.updateFriendlyClassName();
456  newReg->copyProduct(newBD);
457  // Need to call init to get old branch name.
458  prod.init();
459  }
460  }
461 
462  newReg->setFrozen();
463  m_->reg_.reset(newReg.release());
464  }
465 
466  edm::ProductRegistry::ProductList const& prodList2 = m_->reg_->productList();
467  std::vector<edm::EventEntryDescription> temp(prodList2.size(), edm::EventEntryDescription());
468  m_->prov_.swap(temp);
469  std::vector<edm::EventEntryDescription>::iterator itB = m_->prov_.begin();
470  m_->pointerToBranchBuffer_.reserve(prodList2.size());
471 
472  for (edm::ProductRegistry::ProductList::const_iterator it = prodList2.begin(), itEnd = prodList2.end();
473  it != itEnd; ++it, ++itB) {
474  edm::BranchDescription const& prod = it->second;
475  if(prod.branchType() == edm::InEvent) {
476  prod.init();
477  //NEED to do this and check to see if branch exists
478  if (m_->tree_->GetBranch(prod.branchName().c_str())==0) {
479  prod.setDropped();
480  }
481  m_->productMap_.insert(std::make_pair(it->second.oldProductID(), it->second));
482 
483  //std::cout <<"id "<<it->first<<" branch "<<it->second<<std::endl;
484  //m_->pointerToBranchBuffer_.push_back(& (*itB));
485  //void* tmp = &(m_->pointerToBranchBuffer_.back());
486  //edm::EventEntryDescription* tmp = & (*itB);
487  //CDJ need to fix provenance and be backwards compatible, for now just don't read the branch
488  //m_->metaTree_->SetBranchAddress(prod.branchName().c_str(), tmp);
489  }
490  }
491 
492  everythingOK_ = true;
493 }
494 
495 //
496 // const member functions
497 //
498 
499 //
500 // static member functions
501 //
std::string const & idToParameterSetBlobsBranchName()
Definition: BranchType.cc:239
virtual void terminate(TList &out)=0
int i
Definition: DBlmapReader.cc:9
std::string const & BranchTypeToAuxiliaryBranchName(BranchType const &branchType)
Definition: BranchType.cc:108
BranchType const & branchType() const
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_
list file
Definition: dbtoweb.py:253
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
std::map< ProductID, BranchDescription > ProductMap
void setRefCoreStreamer(bool resetAll=false)
virtual void SlaveBegin(TTree *)
std::string const & fileFormatVersionBranchName()
Definition: BranchType.cc:202
virtual void postProcessing(TList &out)=0
std::string const & eventSelectionsBranchName()
Definition: BranchType.cc:227
bool insertMapped(value_type const &v)
virtual void process(const edm::Event &event)=0
ProcessHistory const & processHistory() const
Definition: Principal.h:112
void fillProductRegistryTransients(std::vector< ProcessConfiguration > const &pcVec, ProductRegistry const &preg, bool okToRegister=false)
std::vector< EventSelectionID > EventSelectionIDVector
std::string const & parameterSetsTreeName()
Definition: BranchType.cc:235
ProcessConfigurationRegistry::vector_type ProcessConfigurationVector
std::vector< BranchListIndex > BranchListIndexes
std::string const & className() const
std::string friendlyName(std::string const &iFullName)
std::string const & friendlyClassName() const
tuple pset
Definition: CrabTask.py:85
std::string const & metaDataTreeName()
Definition: BranchType.cc:157
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:172
tuple prod
Definition: CrabTask.py:87
std::string const & processHistoryBranchName()
Definition: BranchType.cc:187
boost::shared_ptr< edm::root::TFWLiteSelectorMembers > m_
int k[5][pyjets_maxn]
void fillEventPrincipal(std::auto_ptr< EventAuxiliary > aux, boost::shared_ptr< LuminosityBlockPrincipal > lbp, boost::shared_ptr< EventSelectionIDVector > eventSelectionIDs=boost::shared_ptr< EventSelectionIDVector >(new EventSelectionIDVector), boost::shared_ptr< BranchListIndexes > branchListIndexes=boost::shared_ptr< BranchListIndexes >(new BranchListIndexes), boost::shared_ptr< BranchMapper > mapper=boost::shared_ptr< BranchMapper >(new BranchMapper), boost::shared_ptr< DelayedReader > rtrv=boost::shared_ptr< DelayedReader >(new NoDelayedReader))
string fullName
std::string const & eventMetaDataTreeName()
Definition: BranchType.cc:248
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:162
std::string const & processConfigurationBranchName()
Definition: BranchType.cc:192
boost::shared_ptr< FWLiteDelayedReader > reader_
author Stefano ARGIRO author Bill Tanenbaum
std::vector< value_type > vector_type
virtual std::auto_ptr< EDProduct > getProduct_(BranchKey const &k, EDProductGetter const *ep) const
tuple cout
Definition: gather_cfg.py:41
std::string const & branchIDListBranchName()
Definition: BranchType.cc:197
std::string const & branchListIndexesBranchName()
Definition: BranchType.cc:231
static ThreadSafeRegistry * instance()
virtual void Begin(TTree *)
EDProductGetter const * prodGetter() const
Definition: Principal.h:79
virtual void begin(TList *&in)=0
string root
initialization
Definition: dbtoconf.py:70