CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
BareRootProductGetter.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: FWLite
4 // Class : BareRootProductGetter
5 //
6 // Implementation:
7 // <Notes on implementation>
8 //
9 // Original Author: Chris Jones
10 // Created: Tue May 23 11:03:31 EDT 2006
11 //
12 
13 // user include files
15 
19 
20 // system include files
21 
22 #include "Reflex/Type.h"
23 #include "TROOT.h"
24 #include "TBranch.h"
25 #include "TClass.h"
26 #include "TFile.h"
27 #include "TTree.h"
28 //
29 // constants, enums and typedefs
30 //
31 
32 //
33 // static data member definitions
34 //
35 
36 //
37 // constructors and destructor
38 //
40 }
41 
42 // BareRootProductGetter::BareRootProductGetter(BareRootProductGetter const& rhs) {
43 // // do actual copying here;
44 // }
45 
47 }
48 
49 //
50 // assignment operators
51 //
52 // BareRootProductGetter const& BareRootProductGetter::operator=(BareRootProductGetter const& rhs) {
53 // //An exception safe implementation is
54 // BareRootProductGetter temp(rhs);
55 // swap(rhs);
56 //
57 // return *this;
58 // }
59 
60 //
61 // member functions
62 //
63 
64 //
65 // const member functions
66 //
69  // std::cout << "getIt called" << std::endl;
70  TFile* currentFile = dynamic_cast<TFile*>(gROOT->GetListOfFiles()->Last());
71  if(0 == currentFile) {
72  throw cms::Exception("FileNotFound")
73  << "unable to find the TFile '" << gROOT->GetListOfFiles()->Last() << "'\n"
74  << "retrieved by calling 'gROOT->GetListOfFiles()->Last()'\n"
75  << "Please check the list of files.";
76  }
77  if(branchMap_.updateFile(currentFile)) {
78  idToBuffers_.clear();
79  }
80  TTree* eventTree = branchMap_.getEventTree();
81  // std::cout << "eventTree " << eventTree << std::endl;
82  if(0 == eventTree) {
83  throw cms::Exception("NoEventsTree")
84  << "unable to find the TTree '" << edm::poolNames::eventTreeName() << "' in the last open file, \n"
85  << "file: '" << branchMap_.getFile()->GetName()
86  << "'\n Please check that the file is a standard CMS ROOT format.\n"
87  << "If the above is not the file you expect then please open your data file after all other files.";
88  }
89  Long_t eventEntry = eventTree->GetReadEntry();
90  // std::cout << "eventEntry " << eventEntry << std::endl;
91  branchMap_.updateEvent(eventEntry);
92  if(eventEntry < 0) {
93  throw cms::Exception("GetEntryNotCalled")
94  << "please call GetEntry for the 'Events' TTree for each event in order to make edm::Ref's work."
95  << "\n Also be sure to call 'SetAddress' for all Branches after calling the GetEntry."
96  ;
97  }
98 
99  Buffer* buffer = 0;
100  IdToBuffers::iterator itBuffer = idToBuffers_.find(iID);
101  // std::cout << "Buffers" << std::endl;
102  if(itBuffer == idToBuffers_.end()) {
103  buffer = createNewBuffer(iID);
104  // std::cout << "buffer " << buffer << std::endl;
105  if(0 == buffer) {
106  return edm::WrapperHolder();
107  }
108  } else {
109  buffer = &(itBuffer->second);
110  }
111  if(0 == buffer) {
112  throw cms::Exception("NullBuffer")
113  << "Found a null buffer which is supposed to hold the data item."
114  << "\n Please contact developers since this message should not happen.";
115  }
116  if(0 == buffer->branch_) {
117  throw cms::Exception("NullBranch")
118  << "The TBranch which should hold the data item is null."
119  << "\n Please contact the developers since this message should not happen.";
120  }
121  if(buffer->eventEntry_ != eventEntry) {
122  //NOTE: Need to reset address because user could have set the address themselves
123  //std::cout << "new event" << std::endl;
124 
125  edm::WrapperInterfaceBase const* interface = branchMap_.productToBranch(iID).getInterface();
126  //ROOT WORKAROUND: Create new objects so any internal data cache will get cleared
127  void* address = buffer->class_->New();
128 
130  if(!prod.isValid()) {
131  cms::Exception("FailedConversion")
132  << "failed to convert a '" << buffer->class_->GetName()
133  << "' to a edm::WrapperHolder."
134  << "Please contact developers since something is very wrong.";
135  }
136  buffer->address_ = address;
137  buffer->product_ = prod;
138  //END WORKAROUND
139 
140  address = &(buffer->address_);
141  buffer->branch_->SetAddress(address);
142 
143  buffer->branch_->GetEntry(eventEntry);
144  buffer->eventEntry_ = eventEntry;
145  }
146  if(!buffer->product_.isValid()) {
147  throw cms::Exception("BranchGetEntryFailed")
148  << "Calling GetEntry with index " << eventEntry
149  << "for branch " << buffer->branch_->GetName() << " failed.";
150  }
151 
152  return edm::WrapperHolder(buffer->product_.wrapper(), buffer->product_.interface());
153 }
154 
157  //find the branch
159 
160  TBranch* branch= branchMap_.getEventTree()->GetBranch(bdesc.branchName().c_str());
161  if(0 == branch) {
162  //we do not thrown on missing branches since 'getIt' should not throw under that condition
163  return 0;
164  }
165  //find the class type
166  std::string const fullName = edm::wrappedClassName(bdesc.className());
167  Reflex::Type classType = Reflex::Type::ByName(fullName);
168  if(classType == Reflex::Type()) {
169  cms::Exception("MissingDictionary")
170  << "could not find dictionary for type '" << fullName << "'"
171  << "\n Please make sure all the necessary libraries are available.";
172  return 0;
173  }
174 
175  //We can't use reflex to create the instance since Reflex uses 'malloc' instead of new
176  /*
177  //use reflex to create an instance of it
178  Reflex::Object wrapperObj = classType.Construct();
179  if(0 == wrapperObj.Address()) {
180  cms::Exception("FailedToCreate") << "could not create an instance of '" << fullName << "'";
181  return 0;
182  }
183 
184  Reflex::Object edProdObj = wrapperObj.CastObject(Reflex::Type::ByName("edm::WrapperHolder"));
185 
186  edm::WrapperHolder* prod = reinterpret_cast<edm::WrapperHolder*>(edProdObj.Address());
187  */
188  TClass* rootClassType = TClass::GetClass(classType.TypeInfo());
189  if(0 == rootClassType) {
190  throw cms::Exception("MissingRootDictionary")
191  << "could not find a ROOT dictionary for type '" << fullName << "'"
192  << "\n Please make sure all the necessary libraries are available.";
193  return 0;
194  }
195  void* address = rootClassType->New();
196 
197  //static TClass* edproductTClass = TClass::GetClass(typeid(edm::WrapperHolder));
198  //edm::WrapperHolder* prod = reinterpret_cast<edm::WrapperHolder*>(rootClassType->DynamicCast(edproductTClass, address, true));
200  if(!prod.isValid()) {
201  cms::Exception("FailedConversion")
202  << "failed to convert a '" << fullName
203  << "' to a edm::WrapperOwningHolder."
204  << "Please contact developers since something is very wrong.";
205  }
206 
207  //connect the instance to the branch
208  //void* address = wrapperObj.Address();
209  Buffer b(prod, branch, address, rootClassType);
210  idToBuffers_[iID] = b;
211 
212  //As of 5.13 ROOT expects the memory address held by the pointer passed to
213  // SetAddress to be valid forever
214  address = &(idToBuffers_[iID].address_);
215  branch->SetAddress(address);
216 
217  return &(idToBuffers_[iID]);
218 }
219 
220 //
221 // static member functions
222 //
char * address
Definition: mlp_lapack.h:14
std::string & branchName() const
virtual edm::WrapperHolder getIt(edm::ProductID const &) const
bool updateEvent(Long_t eventEntry)
bool updateFile(TFile *file)
TFile * getFile() const
TTree * getEventTree() const
WrapperInterfaceBase const * interface() const
Definition: WrapperHolder.h:80
std::string const & className() const
const edm::BranchDescription & productToBranch(const edm::ProductID &pid)
WrapperInterfaceBase const * getInterface() const
string fullName
Buffer * createNewBuffer(edm::ProductID const &) const
std::string wrappedClassName(std::string const &iFullName)
double b
Definition: hdecay.h:120
edm::WrapperOwningHolder product_
std::string const & eventTreeName()
Definition: BranchType.cc:254
bool isValid() const
Definition: WrapperHolder.h:27
fwlite::BranchMapReader branchMap_
void const * wrapper() const
Definition: WrapperHolder.h:76