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