55 const char* branchname ,
58 fHaveStaticClassOffsets(0),
59 fReadStatus(kReadNothingYet),
60 fBranchName(branchname),
71 fHaveLeaf(rhs.fHaveLeaf),
72 fHaveStaticClassOffsets(rhs.fHaveStaticClassOffsets),
73 fReadStatus(rhs.fReadStatus),
74 fSetupStatus(rhs.fSetupStatus),
75 fBranchName(rhs.fBranchName),
76 fLeafName(rhs.fLeafName),
77 fTreeReader(rhs.fTreeReader),
81 fStaticClassOffsets(rhs.fStaticClassOffsets)
98 fTreeReader->DeregisterValueReader(
this);
100 RegisterWithTreeReader();
117 if (fTreeReader) fTreeReader->DeregisterValueReader(
this);
118 R__ASSERT((fLeafName.Length() == 0 ) == !fHaveLeaf
119 &&
"leafness disagreement");
120 R__ASSERT(fStaticClassOffsets.empty() == !fHaveStaticClassOffsets
121 &&
"static class offset disagreement");
129 if (!fTreeReader->RegisterValueReader(
this)) {
130 fTreeReader =
nullptr;
141template <ROOT::Internal::TTreeReaderValueBase::BranchProxyRead_t Func>
144 if ((fProxy->*Func)()) {
145 fReadStatus = kReadSuccess;
147 fReadStatus = kReadError;
154 if (!fProxy)
return kReadNothingYet;
155 if (fProxy->IsInitialized() || fProxy->Setup()) {
160 EReadType readtype = EReadType::kNoDirector;
164 case EReadType::kNoDirector:
165 fProxyReadFunc = &TTreeReaderValueBase::ProxyReadTemplate<&TBranchPoxy::ReadNoDirector>;
167 case EReadType::kReadParentNoCollection:
168 fProxyReadFunc = &TTreeReaderValueBase::ProxyReadTemplate<&TBranchPoxy::ReadParentNoCollection>;
170 case EReadType::kReadParentCollectionNoPointer:
171 fProxyReadFunc = &TTreeReaderValueBase::ProxyReadTemplate<&TBranchPoxy::ReadParentCollectionNoPointer>;
173 case EReadType::kReadParentCollectionPointer:
174 fProxyReadFunc = &TTreeReaderValueBase::ProxyReadTemplate<&TBranchPoxy::ReadParentCollectionPointer>;
176 case EReadType::kReadNoParentNoBranchCountCollectionPointer:
177 fProxyReadFunc = &TTreeReaderValueBase::ProxyReadTemplate<&TBranchPoxy::ReadNoParentNoBranchCountCollectionPointer>;
179 case EReadType::kReadNoParentNoBranchCountCollectionNoPointer:
180 fProxyReadFunc = &TTreeReaderValueBase::ProxyReadTemplate<&TBranchPoxy::ReadNoParentNoBranchCountCollectionNoPointer>;
182 case EReadType::kReadNoParentNoBranchCountNoCollection:
183 fProxyReadFunc = &TTreeReaderValueBase::ProxyReadTemplate<&TBranchPoxy::ReadNoParentNoBranchCountNoCollection>;
185 case EReadType::kReadNoParentBranchCountCollectionPointer:
186 fProxyReadFunc = &TTreeReaderValueBase::ProxyReadTemplate<&TBranchPoxy::ReadNoParentBranchCountCollectionPointer>;
188 case EReadType::kReadNoParentBranchCountCollectionNoPointer:
189 fProxyReadFunc = &TTreeReaderValueBase::ProxyReadTemplate<&TBranchPoxy::ReadNoParentBranchCountCollectionNoPointer>;
191 case EReadType::kReadNoParentBranchCountNoCollection:
192 fProxyReadFunc = &TTreeReaderValueBase::ProxyReadTemplate<&TBranchPoxy::ReadNoParentBranchCountNoCollection>;
194 case EReadType::kDefault:
198 return (this->*fProxyReadFunc)();
204 if (fProxy->Read()) {
205 fReadStatus = kReadSuccess;
207 fReadStatus = kReadError;
217 std::string ret = buf;
230 if (!fHaveLeaf || !newTree) {
238 fReadStatus = kReadError;
239 Error(
"TTreeReaderValueBase::GetLeaf()",
"Unable to get the branch from the tree");
243 fLeaf = myBranch->
GetLeaf(fLeafName);
245 Error(
"TTreeReaderValueBase::GetLeaf()",
"Failed to get the leaf from the branch");
253 if (ProxyRead() != kReadSuccess)
return 0;
257 return fLeaf->GetValuePointer();
260 fReadStatus = kReadError;
261 Error(
"TTreeReaderValueBase::GetAddress()",
"Unable to get the leaf");
265 if (fHaveStaticClassOffsets){
268 for (
unsigned int i = 0; i < fStaticClassOffsets.size() - 1; ++i){
269 address = *(
Byte_t**)(address + fStaticClassOffsets[i]);
272 return address + fStaticClassOffsets.back();
274 return (
Byte_t*)fProxy->GetWhere();
294 TRegexp leafNameExpression (
"\\.[a-zA-Z0-9_]+$");
295 TString leafName (fBranchName(leafNameExpression));
296 TString branchName = fBranchName(0, fBranchName.Length() - leafName.
Length());
297 auto branch = fTreeReader->GetTree()->GetBranch(branchName);
299 std::vector<TString> nameStack;
300 nameStack.push_back(
TString());
302 leafName = branchName(leafNameExpression);
303 branchName = branchName(0, branchName.
Length() - leafName.
Length());
305 branch = fTreeReader->GetTree()->GetBranch(branchName);
306 if (!branch) branch = fTreeReader->GetTree()->GetBranch(branchName +
".");
309 while (!branch && branchName.
Contains(
".")){
310 leafName = branchName(leafNameExpression);
311 branchName = branchName(0, branchName.
Length() - leafName.
Length());
312 branch = fTreeReader->GetTree()->GetBranch(branchName);
313 if (!branch) branch = fTreeReader->GetTree()->GetBranch(branchName +
".");
317 if (branch && branch->IsA() == TBranchElement::Class()){
320 TString traversingBranch = nameStack.back();
321 nameStack.pop_back();
327 std::vector<Long64_t> offsets;
334 while (nameStack.size() && found){
337 for (
int i = 0; i < myObjArray->
GetEntries(); ++i){
341 if (!strcmp(tempStreamerElement->
GetName(), traversingBranch.
Data())){
344 traversingBranch = nameStack.back();
345 nameStack.pop_back();
347 elementClass = tempStreamerElement->
GetClass();
355 if (!finalDataType) {
357 if (seType && seType->IsA() == TDataType::Class()) {
364 offsets.push_back(offset);
374 offsets.push_back(offset);
377 fStaticClassOffsets = offsets;
378 fHaveStaticClassOffsets = 1;
380 if (fDict != finalDataType && fDict != elementClass){
381 errMsg =
"Wrong data type ";
382 errMsg += finalDataType ? finalDataType->
GetName() : elementClass ? elementClass->
GetName() :
"UNKNOWN";
383 fSetupStatus = kSetupMismatch;
391 if (!fHaveStaticClassOffsets) {
392 errMsg =
"The tree does not have a branch called ";
393 errMsg += fBranchName;
394 errMsg +=
". You could check with TTree::Print() for available branches.";
395 fSetupStatus = kSetupMissingBranch;
401 myLeaf = branch->GetLeaf(
TString(leafName(1, leafName.
Length())));
403 errMsg =
"The tree does not have a branch, nor a sub-branch called ";
404 errMsg += fBranchName;
405 errMsg +=
". You could check with TTree::Print() for available branches.";
406 fSetupStatus = kSetupMissingBranch;
412 if (tempDict && fDict->IsA() == TDataType::Class() && tempDict->
GetType() == ((
TDataType*)fDict)->GetType()) {
414 branchActualType = fDict;
416 fBranchName = branchName;
417 fLeafName = leafName(1, leafName.
Length());
418 fHaveLeaf = fLeafName.Length() > 0;
419 fSetupStatus = kSetupMatchLeaf;
422 errMsg =
"Leaf of type ";
424 errMsg +=
" cannot be read by TTreeReaderValue<";
427 fSetupStatus = kSetupMismatch;
441 constexpr const char* errPrefix =
"TTreeReaderValueBase::CreateProxy()";
447 fSetupStatus = kSetupInternalError;
449 Error(errPrefix,
"TTreeReader object not set / available for branch %s!",
451 fSetupStatus = kSetupTreeDestructed;
455 auto branchFromFullName = fTreeReader->GetTree()->GetBranch(fBranchName);
456 if (branchFromFullName ==
nullptr)
457 branchFromFullName = fTreeReader->GetTree()->FindBranch(fBranchName);
460 const char* brDataType =
"{UNDETERMINED}";
461 if (branchFromFullName) {
463 brDataType = GetBranchDataType(branchFromFullName, brDictUnused, fDict);
465 Error(errPrefix,
"The template argument type T of %s accessing branch %s (which contains data of type %s) is not known to ROOT. You will need to create a dictionary for it.",
466 GetDerivedTypeName(), fBranchName.Data(), brDataType);
467 fSetupStatus = kSetupMissingDictionary;
475 if (namedProxy && namedProxy->
GetDict() == fDict) {
480 const std::string originalBranchName = fBranchName.Data();
482 TLeaf *myLeaf =
nullptr;
493 if (fBranchName.Contains(
".")) {
494 branch = SearchBranchWithCompositeName(myLeaf, branchActualType, errMsg);
502 if (branch && branch->IsA() == TBranchElement::Class() && branchFromFullName){
503 branch = branchFromFullName;
504 fStaticClassOffsets = {};
505 fHaveStaticClassOffsets = 0;
512 branch = branchFromFullName;
517 if (errMsg.empty()) {
518 errMsg =
"The tree does not have a branch called ";
519 errMsg += fBranchName.Data();
520 errMsg +=
". You could check with TTree::Print() for available branches.";
522#if !defined(_MSC_VER)
523#pragma GCC diagnostic push
524#pragma GCC diagnostic ignored "-Wformat-security"
526 Error(errPrefix, errMsg.c_str());
527#if !defined(_MSC_VER)
528#pragma GCC diagnostic pop
534 fSetupStatus = kSetupInternalError;
535 fStaticClassOffsets = {};
536 fHaveStaticClassOffsets = 0;
540 if (!myLeaf && !fHaveStaticClassOffsets) {
543 const char* branchActualTypeName = GetBranchDataType(branch, branchActualType, fDict);
544 if (!branchActualType) {
545 Error(errPrefix,
"The branch %s contains data of type %s, which does not have a dictionary.",
546 fBranchName.Data(), branchActualTypeName ? branchActualTypeName :
"{UNDETERMINED TYPE}");
553 auto dictAsClass =
dynamic_cast<TClass*
>(fDict);
554 auto branchActualTypeAsClass =
dynamic_cast<TClass*
>(branchActualType);
555 auto inheritance = dictAsClass && branchActualTypeAsClass && branchActualTypeAsClass->
InheritsFrom(dictAsClass);
557 if (fDict != branchActualType && !inheritance) {
560 bool complainAboutMismatch =
true;
561 if (dictdt && actualdt) {
564 complainAboutMismatch =
false;
569 complainAboutMismatch =
false;
572 if (complainAboutMismatch) {
574 "The branch %s contains data of type %s. It cannot be accessed by a TTreeReaderValue<%s>",
575 fBranchName.Data(), branchActualType->
GetName(),
590 bool isTopLevel = branch->
GetMother() == branch;
592 membername = strrchr(branch->
GetName(),
'.');
593 if (membername.
IsNull()) {
594 membername = branch->
GetName();
597 auto director = fTreeReader->fDirector;
613 Error(errPrefix,
"The branch %s is contained in a Friend TTree that is not directly attached to the main.\n"
614 "This is not yet supported by TTreeReader.",
618 const char *localBranchName = originalBranchName.c_str();
622 if (strncmp(localBranchName, branch->
GetTree()->
GetName(), len) == 0
623 && localBranchName[len] ==
'.'
625 localBranchName = localBranchName + len + 1;
627 len = strlen(fe_found->
GetName());
628 if (strncmp(localBranchName, fe_found->
GetName(), len) == 0
629 && localBranchName[len] ==
'.'
631 localBranchName = localBranchName + len + 1;
636 if ((
size_t)index < fTreeReader->fFriendProxies.size()) {
637 feproxy = fTreeReader->fFriendProxies.at(index);
641 fTreeReader->fFriendProxies.resize(index+1);
642 fTreeReader->fFriendProxies.at(index) = feproxy;
646 namedProxy =
new TNamedBranchProxy(director, branch, originalBranchName.c_str(), membername);
648 fTreeReader->AddProxy(namedProxy);
657 fSetupStatus = kSetupMatch;
659 fSetupStatus = kSetupMismatch;
673 if (branch->IsA() == TBranchElement::Class()) {
676 auto ResolveTypedef = [&]() ->
void {
677 if (dict->IsA() != TDataType::Class())
681 if (dict->IsA() != TDataType::Class()) {
683 if (dict != curDict) {
686 if (dict != curDict) {
701 if (element->IsA() == TStreamerSTL::Class()){
723 dict = TClonesArray::Class();
724 return "TClonesArray";
725 }
else if (brElement->
GetType() == 31
726 || brElement->
GetType() == 41) {
728 Error(
"TTreeReaderValueBase::GetBranchDataType()",
"Must use TTreeReaderArray to access a member of an object that is stored in a collection.");
734 Error(
"TTreeReaderValueBase::GetBranchDataType()",
"Unknown type and class combination: %i, %s", brElement->
GetType(), brElement->
GetClassName());
737 }
else if (branch->IsA() == TBranch::Class()
738 || branch->IsA() == TBranchObject::Class()
739 || branch->IsA() == TBranchSTL::Class()) {
740 if (branch->
GetTree()->IsA() == TNtuple::Class()){
745 if ((!dataTypeName || !dataTypeName[0])
746 && branch->IsA() == TBranch::Class()) {
756 if (myDataType && myDataType->IsA() == TDataType::Class()){
758 Error(
"TTreeReaderValueBase::GetBranchDataType()",
"Must use TTreeReaderArray to read branch %s: it contains an array or a collection.", branch->
GetName());
767 Error(
"TTreeReaderValueBase::GetBranchDataType()",
"The branch %s was created using a leaf list and cannot be represented as a C++ type. Please access one of its siblings using a TTreeReaderArray:", branch->
GetName());
770 while ((leaf = (
TLeaf*) iLeaves())) {
771 Error(
"TTreeReaderValueBase::GetBranchDataType()",
" %s.%s", branch->
GetName(), leaf->
GetName());
777 }
else if (branch->IsA() == TBranchClones::Class()) {
778 dict = TClonesArray::Class();
779 return "TClonesArray";
780 }
else if (branch->IsA() == TBranchRef::Class()) {
782 Error(
"TTreeReaderValueBase::GetBranchDataType()",
"The branch %s is a TBranchRef and cannot be represented as a C++ type.", branch->
GetName());
785 Error(
"TTreeReaderValueBase::GetBranchDataType()",
"The branch %s is of type %s - something that is not handled yet.", branch->
GetName(), branch->IsA()->
GetName());
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Base class for all the proxy object.
TBranchProxyDirector * GetDirector()
void SetDict(TDictionary *dict)
const Detail::TBranchProxy * GetProxy() const
TDictionary * GetDict() const
Base class of TTreeReaderValue.
void RegisterWithTreeReader()
Register with tree reader.
void NotifyNewTree(TTree *newTree)
The TTreeReader has switched to a new TTree. Update the leaf.
void * GetAddress()
Returns the memory address of the object being read.
static std::string GetElementTypeName(const std::type_info &ti)
Stringify the template argument.
ESetupStatus fSetupStatus
TTreeReaderValueBase(TTreeReader *reader, const char *branchname, TDictionary *dict)
Construct a tree value reader and register it with the reader object.
ROOT::Internal::TTreeReaderValueBase::EReadStatus ProxyReadTemplate()
Try to read the value from the TBranchProxy, returns the status of the read.
TTreeReader * fTreeReader
TTreeReaderValueBase & operator=(const TTreeReaderValueBase &)
Copy-assign.
virtual ~TTreeReaderValueBase()
Unregister from tree reader, cleanup.
EReadStatus ProxyReadDefaultImpl()
virtual void CreateProxy()
Create the proxy object for our branch.
int fHaveStaticClassOffsets
Detail::TBranchProxy * fProxy
std::vector< Long64_t > fStaticClassOffsets
TBranch * SearchBranchWithCompositeName(TLeaf *&myleaf, TDictionary *&branchActualType, std::string &err)
Search a branch the name of which contains a ".".
static const char * GetBranchDataType(TBranch *branch, TDictionary *&dict, TDictionary const *curDict)
Retrieve the type of data stored by branch; put its dictionary into dict, return its type name.
A Branch for the case of an object.
TStreamerInfo * GetInfo() const
Get streamer info for the branch class.
virtual const char * GetClassName() const
Return the name of the user class whose content is stored in this branch, if any.
TClass * GetCurrentClass()
Return a pointer to the current type of the data member corresponding to branch element.
virtual const char * GetTypeName() const
Return type name of element in the branch.
A TTree is a list of TBranches.
virtual TLeaf * GetLeaf(const char *name) const
Return pointer to the 1st Leaf named name in thisBranch.
virtual const char * GetClassName() const
Return the name of the user class whose content is stored in this branch, if any.
virtual TLeaf * FindLeaf(const char *name)
Find the leaf corresponding to the name 'searchname'.
TObjArray * GetListOfLeaves()
TBranch * GetMother() const
Get our top-level parent branch in the tree.
TClass instances represent classes, structs and namespaces in the ROOT type system.
TVirtualStreamerInfo * GetStreamerInfo(Int_t version=0, Bool_t isTransient=kFALSE) const
returns a pointer to the TVirtualStreamerInfo object for version If the object does not exist,...
Bool_t InheritsFrom(const char *cl) const
Return kTRUE if this class inherits from a class with name "classname".
static TClass * GetClass(const char *name, Bool_t load=kTRUE, Bool_t silent=kFALSE)
Static method returning pointer to TClass of the specified class name.
Basic data type descriptor (datatype information is obtained from CINT).
static TDataType * GetDataType(EDataType type)
Given a EDataType type, get the TDataType* that represents it.
This class defines an abstract interface that must be implemented by all classes that contain diction...
static TDictionary * GetDictionary(const char *name)
Retrieve the type (class, fundamental type, typedef etc) named "name".
A TFriendElement TF describes a TTree object TF in a file.
A TLeaf describes individual elements of a TBranch See TBranch structure in TTree.
virtual const char * GetTypeName() const
virtual TLeaf * GetLeafCount() const
If this leaf stores a variable-sized array or a multi-dimensional array whose last dimension has vari...
virtual Int_t GetLenStatic() const
Return the fixed length of this leaf.
virtual const char * GetName() const
Returns name of object.
Int_t GetEntries() const
Return the number of objects in array (i.e.
TObject * UncheckedAt(Int_t i) const
TObject * At(Int_t idx) const
TRangeDynCast is an adaptater class that allows the typed iteration through a TCollection.
Regular expression class.
virtual Bool_t IsaPointer() const
const char * GetTypeName() const
TClass * GetClass() const
Describe Streamer information for one class version.
TObjArray * GetElements() const
TSubString Strip(EStripType s=kTrailing, char c=' ') const
Return a substring of self stripped at beginning and/or end.
const char * Data() const
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
A simple, robust and fast interface to read values from ROOT columnar datasets such as TTree,...
A TTree represents a columnar dataset.
virtual TBranch * GetBranch(const char *name)
Return pointer to the branch with the given name in this tree or its friends.
virtual TTree * GetTree() const
Abstract Interface class describing Streamer information for one class.
virtual TObjArray * GetElements() const =0
virtual Int_t GetElementOffset(Int_t id) const =0
char * DemangleTypeIdName(const std::type_info &ti, int &errorCode)
Demangle in a portable way the type id name.