56 const char* branchname ,
59 fHaveStaticClassOffsets(false),
60 fReadStatus(kReadNothingYet),
61 fBranchName(branchname),
72 fHaveLeaf(rhs.fHaveLeaf),
73 fHaveStaticClassOffsets(rhs.fHaveStaticClassOffsets),
74 fReadStatus(rhs.fReadStatus),
75 fSetupStatus(rhs.fSetupStatus),
76 fBranchName(rhs.fBranchName),
77 fLeafName(rhs.fLeafName),
78 fTreeReader(rhs.fTreeReader),
82 fStaticClassOffsets(rhs.fStaticClassOffsets)
99 fTreeReader->DeregisterValueReader(
this);
101 RegisterWithTreeReader();
118 if (fTreeReader) fTreeReader->DeregisterValueReader(
this);
119 R__ASSERT((fLeafName.Length() == 0 ) == !fHaveLeaf
120 &&
"leafness disagreement");
121 R__ASSERT(fStaticClassOffsets.empty() == !fHaveStaticClassOffsets
122 &&
"static class offset disagreement");
130 if (!fTreeReader->RegisterValueReader(
this)) {
131 fTreeReader =
nullptr;
142template <ROOT::Internal::TTreeReaderValueBase::BranchProxyRead_t Func>
145 if ((fProxy->*Func)()) {
146 fReadStatus = kReadSuccess;
148 fReadStatus = kReadError;
155 if (!fProxy)
return kReadNothingYet;
156 if (fProxy->IsInitialized() || fProxy->Setup()) {
161 EReadType readtype = EReadType::kNoDirector;
165 case EReadType::kNoDirector:
166 fProxyReadFunc = &TTreeReaderValueBase::ProxyReadTemplate<&TBranchPoxy::ReadNoDirector>;
168 case EReadType::kReadParentNoCollection:
169 fProxyReadFunc = &TTreeReaderValueBase::ProxyReadTemplate<&TBranchPoxy::ReadParentNoCollection>;
171 case EReadType::kReadParentCollectionNoPointer:
172 fProxyReadFunc = &TTreeReaderValueBase::ProxyReadTemplate<&TBranchPoxy::ReadParentCollectionNoPointer>;
174 case EReadType::kReadParentCollectionPointer:
175 fProxyReadFunc = &TTreeReaderValueBase::ProxyReadTemplate<&TBranchPoxy::ReadParentCollectionPointer>;
177 case EReadType::kReadNoParentNoBranchCountCollectionPointer:
178 fProxyReadFunc = &TTreeReaderValueBase::ProxyReadTemplate<&TBranchPoxy::ReadNoParentNoBranchCountCollectionPointer>;
180 case EReadType::kReadNoParentNoBranchCountCollectionNoPointer:
181 fProxyReadFunc = &TTreeReaderValueBase::ProxyReadTemplate<&TBranchPoxy::ReadNoParentNoBranchCountCollectionNoPointer>;
183 case EReadType::kReadNoParentNoBranchCountNoCollection:
184 fProxyReadFunc = &TTreeReaderValueBase::ProxyReadTemplate<&TBranchPoxy::ReadNoParentNoBranchCountNoCollection>;
186 case EReadType::kReadNoParentBranchCountCollectionPointer:
187 fProxyReadFunc = &TTreeReaderValueBase::ProxyReadTemplate<&TBranchPoxy::ReadNoParentBranchCountCollectionPointer>;
189 case EReadType::kReadNoParentBranchCountCollectionNoPointer:
190 fProxyReadFunc = &TTreeReaderValueBase::ProxyReadTemplate<&TBranchPoxy::ReadNoParentBranchCountCollectionNoPointer>;
192 case EReadType::kReadNoParentBranchCountNoCollection:
193 fProxyReadFunc = &TTreeReaderValueBase::ProxyReadTemplate<&TBranchPoxy::ReadNoParentBranchCountNoCollection>;
195 case EReadType::kDefault:
199 return (this->*fProxyReadFunc)();
205 if (fProxy->Read()) {
206 fReadStatus = kReadSuccess;
208 fReadStatus = kReadError;
218 std::string ret = buf;
231 if (!fHaveLeaf || !newTree) {
239 fReadStatus = kReadError;
240 Error(
"TTreeReaderValueBase::GetLeaf()",
"Unable to get the branch from the tree");
244 fLeaf = myBranch->
GetLeaf(fLeafName);
246 Error(
"TTreeReaderValueBase::GetLeaf()",
"Failed to get the leaf from the branch");
254 if (ProxyRead() != kReadSuccess)
return nullptr;
258 return fLeaf->GetValuePointer();
261 fReadStatus = kReadError;
262 Error(
"TTreeReaderValueBase::GetAddress()",
"Unable to get the leaf");
266 if (fHaveStaticClassOffsets){
269 for (
unsigned int i = 0; i < fStaticClassOffsets.size() - 1; ++i){
270 address = *(
Byte_t**)(address + fStaticClassOffsets[i]);
273 return address + fStaticClassOffsets.back();
275 return (
Byte_t*)fProxy->GetWhere();
295 TRegexp leafNameExpression (
"\\.[a-zA-Z0-9_]+$");
296 TString leafName (fBranchName(leafNameExpression));
297 TString branchName = fBranchName(0, fBranchName.Length() - leafName.
Length());
298 auto branch = fTreeReader->GetTree()->GetBranch(branchName);
300 std::vector<TString> nameStack;
301 nameStack.push_back(
TString());
303 leafName = branchName(leafNameExpression);
304 branchName = branchName(0, branchName.
Length() - leafName.
Length());
306 branch = fTreeReader->GetTree()->GetBranch(branchName);
307 if (!branch) branch = fTreeReader->GetTree()->GetBranch(branchName +
".");
310 while (!branch && branchName.
Contains(
".")){
311 leafName = branchName(leafNameExpression);
312 branchName = branchName(0, branchName.
Length() - leafName.
Length());
313 branch = fTreeReader->GetTree()->GetBranch(branchName);
314 if (!branch) branch = fTreeReader->GetTree()->GetBranch(branchName +
".");
321 TString traversingBranch = nameStack.back();
322 nameStack.pop_back();
328 std::vector<Long64_t> offsets;
330 TClass *elementClass =
nullptr;
335 while (!nameStack.empty() && found){
338 for (
int i = 0; i < myObjArray->
GetEntries(); ++i){
342 if (!strcmp(tempStreamerElement->
GetName(), traversingBranch.
Data())){
345 traversingBranch = nameStack.back();
346 nameStack.pop_back();
348 elementClass = tempStreamerElement->
GetClass();
356 if (!finalDataType) {
365 offsets.push_back(
offset);
375 offsets.push_back(
offset);
378 fStaticClassOffsets = offsets;
379 fHaveStaticClassOffsets =
true;
381 if (fDict != finalDataType && fDict != elementClass){
382 errMsg =
"Wrong data type ";
383 errMsg += finalDataType ? finalDataType->
GetName() : elementClass ? elementClass->
GetName() :
"UNKNOWN";
384 fSetupStatus = kSetupMismatch;
392 if (!fHaveStaticClassOffsets) {
393 errMsg =
"The tree does not have a branch called ";
394 errMsg += fBranchName;
395 errMsg +=
". You could check with TTree::Print() for available branches.";
396 fSetupStatus = kSetupMissingBranch;
402 myLeaf = branch->GetLeaf(
TString(leafName(1, leafName.
Length())));
404 errMsg =
"The tree does not have a branch, nor a sub-branch called ";
405 errMsg += fBranchName;
406 errMsg +=
". You could check with TTree::Print() for available branches.";
407 fSetupStatus = kSetupMissingBranch;
415 branchActualType = fDict;
417 fBranchName = branchName;
418 fLeafName = leafName(1, leafName.
Length());
419 fHaveLeaf = fLeafName.Length() > 0;
420 fSetupStatus = kSetupMatchLeaf;
423 errMsg =
"Leaf of type ";
425 errMsg +=
" cannot be read by TTreeReaderValue<";
428 fSetupStatus = kSetupMismatch;
442 constexpr const char* errPrefix =
"TTreeReaderValueBase::CreateProxy()";
448 fSetupStatus = kSetupInternalError;
450 Error(errPrefix,
"TTreeReader object not set / available for branch %s!",
452 fSetupStatus = kSetupTreeDestructed;
456 auto branchFromFullName = fTreeReader->GetTree()->GetBranch(fBranchName);
457 if (branchFromFullName ==
nullptr)
458 branchFromFullName = fTreeReader->GetTree()->FindBranch(fBranchName);
461 const char* brDataType =
"{UNDETERMINED}";
462 if (branchFromFullName) {
464 brDataType = GetBranchDataType(branchFromFullName, brDictUnused, fDict);
466 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.",
467 GetDerivedTypeName(), fBranchName.Data(), brDataType);
468 fSetupStatus = kSetupMissingDictionary;
476 if (namedProxy && namedProxy->
GetDict() == fDict) {
481 const std::string originalBranchName = fBranchName.Data();
483 TLeaf *myLeaf =
nullptr;
494 if (fBranchName.Contains(
".")) {
495 branch = SearchBranchWithCompositeName(myLeaf, branchActualType, errMsg);
504 branch = branchFromFullName;
505 fStaticClassOffsets = {};
506 fHaveStaticClassOffsets =
false;
513 branch = branchFromFullName;
518 if (errMsg.empty()) {
519 errMsg =
"The tree does not have a branch called ";
520 errMsg += fBranchName.Data();
521 errMsg +=
". You could check with TTree::Print() for available branches.";
523#if !defined(_MSC_VER)
524#pragma GCC diagnostic push
525#pragma GCC diagnostic ignored "-Wformat-security"
527 Error(errPrefix, errMsg.c_str());
528#if !defined(_MSC_VER)
529#pragma GCC diagnostic pop
535 fSetupStatus = kSetupInternalError;
536 fStaticClassOffsets = {};
537 fHaveStaticClassOffsets =
false;
541 if (!myLeaf && !fHaveStaticClassOffsets) {
544 const char* branchActualTypeName = GetBranchDataType(branch, branchActualType, fDict);
545 if (!branchActualType) {
546 Error(errPrefix,
"The branch %s contains data of type %s, which does not have a dictionary.",
547 fBranchName.Data(), branchActualTypeName ? branchActualTypeName :
"{UNDETERMINED TYPE}");
554 auto dictAsClass =
dynamic_cast<TClass*
>(fDict);
555 auto branchActualTypeAsClass =
dynamic_cast<TClass*
>(branchActualType);
556 auto inheritance = dictAsClass && branchActualTypeAsClass && branchActualTypeAsClass->
InheritsFrom(dictAsClass);
558 if (fDict != branchActualType && !inheritance) {
561 TEnum *dictenum =
dynamic_cast<TEnum*
>(fDict);
562 TEnum *actualenum =
dynamic_cast<TEnum*
>(branchActualType);
563 bool complainAboutMismatch =
true;
564 if (dictdt && actualdt) {
567 complainAboutMismatch =
false;
572 complainAboutMismatch =
false;
574 }
else if ( (dictdt || dictenum) && (actualdt || actualenum) ) {
577 complainAboutMismatch =
false;
579 if (complainAboutMismatch) {
581 "The branch %s contains data of type %s. It cannot be accessed by a TTreeReaderValue<%s>",
582 fBranchName.Data(), branchActualType->
GetName(),
597 bool isTopLevel = branch->
GetMother() == branch;
599 membername = strrchr(branch->
GetName(),
'.');
600 if (membername.
IsNull()) {
601 membername = branch->
GetName();
604 auto director = fTreeReader->fDirector;
620 Error(errPrefix,
"The branch %s is contained in a Friend TTree that is not directly attached to the main.\n"
621 "This is not yet supported by TTreeReader.",
625 const char *localBranchName = originalBranchName.c_str();
630 && localBranchName[
len] ==
'.'
632 localBranchName = localBranchName +
len + 1;
635 if (strncmp(localBranchName, fe_found->
GetName(),
len) == 0
636 && localBranchName[
len] ==
'.'
638 localBranchName = localBranchName +
len + 1;
643 if ((
size_t)index < fTreeReader->fFriendProxies.size()) {
644 feproxy = fTreeReader->fFriendProxies.at(
index);
648 fTreeReader->fFriendProxies.resize(
index+1);
649 fTreeReader->fFriendProxies.at(
index) = feproxy;
653 namedProxy =
new TNamedBranchProxy(director, branch, originalBranchName.c_str(), membername);
655 fTreeReader->AddProxy(namedProxy);
664 fSetupStatus = kSetupMatch;
666 fSetupStatus = kSetupMismatch;
683 auto ResolveTypedef = [&]() ->
void {
690 if (dict != curDict) {
693 if (dict != curDict) {
731 return "TClonesArray";
732 }
else if (brElement->
GetType() == 31
733 || brElement->
GetType() == 41) {
735 Error(
"TTreeReaderValueBase::GetBranchDataType()",
"Must use TTreeReaderArray to access a member of an object that is stored in a collection.");
741 Error(
"TTreeReaderValueBase::GetBranchDataType()",
"Unknown type and class combination: %i, %s", brElement->
GetType(), brElement->
GetClassName());
752 if ((!dataTypeName || !dataTypeName[0])
765 Error(
"TTreeReaderValueBase::GetBranchDataType()",
"Must use TTreeReaderArray to read branch %s: it contains an array or a collection.", branch->
GetName());
774 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());
776 TLeaf* leaf =
nullptr;
777 while ((leaf = (
TLeaf*) iLeaves())) {
778 Error(
"TTreeReaderValueBase::GetBranchDataType()",
" %s.%s", branch->
GetName(), leaf->
GetName());
786 return "TClonesArray";
789 Error(
"TTreeReaderValueBase::GetBranchDataType()",
"The branch %s is a TBranchRef and cannot be represented as a C++ type.", branch->
GetName());
792 Error(
"TTreeReaderValueBase::GetBranchDataType()",
"The branch %s is of type %s - something that is not handled yet.", branch->
GetName(), branch->
IsA()->
GetName());
808 std::string str = cl->GetName();
809 str +=
" instance associated to column ";
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h offset
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t UChar_t len
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.
bool fHaveLeaf
Whether the data is in a leaf.
bool fHaveStaticClassOffsets
Whether !fStaticClassOffsets.empty()
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
Setup status of this data access.
TTreeReaderValueBase(TTreeReader *reader, const char *branchname, TDictionary *dict)
Construct a tree value reader and register it with the reader object.
TString fBranchName
Name of the branch to read data from.
ROOT::Internal::TTreeReaderValueBase::EReadStatus ProxyReadTemplate()
Try to read the value from the TBranchProxy, returns the status of the read.
TTreeReader * fTreeReader
Tree reader we belong to.
TDictionary * fDict
Type that the branch should contain.
TTreeReaderValueBase & operator=(const TTreeReaderValueBase &)
Copy-assign.
EReadStatus fReadStatus
Read status of this data access.
const char * GetBranchName() const
virtual ~TTreeReaderValueBase()
Unregister from tree reader, cleanup.
EReadStatus ProxyReadDefaultImpl()
virtual void CreateProxy()
Create the proxy object for our branch.
Detail::TBranchProxy * fProxy
Proxy for this branch, owned by TTreeReader.
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.
const char * GetClassName() const override
Return the name of the user class whose content is stored in this branch, if any.
TStreamerInfo * GetInfo() const
Get streamer info for the branch class.
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.
TClass * IsA() const override
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 override
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.
TClass * IsA() const override
This class defines an abstract interface that must be implemented by all classes that contain diction...
TClass * IsA() const override
static TDictionary * GetDictionary(const char *name)
Retrieve the type (class, fundamental type, typedef etc) named "name".
The TEnum class implements the enum type.
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.
const char * GetName() const override
Returns name of object.
Int_t GetEntries() const override
Return the number of objects in array (i.e.
TObject * At(Int_t idx) const override
TObject * UncheckedAt(Int_t i) const
Regular expression class.
virtual Bool_t IsaPointer() const
const char * GetTypeName() const
TClass * GetClass() const
TClass * IsA() const override
Describes a persistent version of a class.
TObjArray * GetElements() const override
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
TClass * IsA() const override
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.