Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tutorial for TSelector #13

Closed
wants to merge 26 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
bbe78bd
Initial skeleton for h1analysisTreeReader tutorial
strykejern Jul 15, 2013
6ccee5f
Added error message for uninitialized proxy
strykejern Jul 16, 2013
3c1e7f2
Added support for type-checking top level leaves
strykejern Jul 16, 2013
3462f89
Further work on h1analysis tutorial with TTreeReader
strykejern Jul 16, 2013
8a5545f
Working reading of top level leaves with tree readers
strykejern Jul 17, 2013
68ed4d4
Code cleanup and structure in tutorial
strykejern Jul 17, 2013
7d09599
Commented out unneeded TTreeReaders
strykejern Jul 17, 2013
827075b
Removed unnecessary initializers and destructions
strykejern Jul 17, 2013
3944ea9
Started rewriting h1analysisTreeReader to not use pointers
strykejern Jul 18, 2013
97b6c04
Checking dictionary type and getting data type with numerical value
strykejern Jul 18, 2013
1c61589
Removed deprecated comments
strykejern Jul 18, 2013
3fc01c8
Added missing default initializer for fDirector
strykejern Jul 18, 2013
d6976d2
Tutorial based on h1analysis
strykejern Jul 18, 2013
3c849fb
Added support for setting local entry in TTreeReader
strykejern Jul 19, 2013
f0390b4
Removed the need for fChainOffset in tutorial
strykejern Jul 19, 2013
e7c7bd2
Added extra check for pointer change in TBranchProxy Setup() as well
strykejern Jul 19, 2013
0b06477
Fix for error message
strykejern Jul 19, 2013
7e36988
Removed unnecessary printf statement, and changed others to Error mes…
strykejern Jul 19, 2013
56a7c96
Added extra type-checking in array reader
strykejern Jul 19, 2013
365c66d
Refactored readers to begin with 'f'
strykejern Jul 19, 2013
7e45d59
Removed and moved includes
strykejern Jul 19, 2013
fae32ce
Comment cleanup
strykejern Jul 19, 2013
8ab303f
Optimizing SetLocalEntry
strykejern Jul 24, 2013
7d70a4c
Fixes behaviour when changing trees
strykejern Jul 25, 2013
65b4abb
Tutorial now working with PROOF
strykejern Jul 25, 2013
565ba8f
Added missing method descriptions
strykejern Jul 25, 2013
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions tree/treeplayer/inc/TTreeReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ class TTreeReader: public TObject {

TTreeReader():
fDirectory(0),
fEntryStatus(kEntryNoTree)
fEntryStatus(kEntryNoTree),
fDirector(0)
{}

TTreeReader(TTree* tree);
Expand All @@ -73,7 +74,8 @@ class TTreeReader: public TObject {
Bool_t IsChain() const { return TestBit(kBitIsChain); }

Bool_t SetNextEntry() { return SetEntry(GetCurrentEntry() + 1) == kEntryValid; }
EEntryStatus SetEntry(Long64_t entry);
EEntryStatus SetEntry(Long64_t entry) { return SetEntryBase(entry, kFALSE); }
EEntryStatus SetLocalEntry(Long64_t entry) { return SetEntryBase(entry, kTRUE); }

EEntryStatus GetEntryStatus() const { return fEntryStatus; }

Expand All @@ -90,6 +92,8 @@ class TTreeReader: public TObject {
void RegisterValueReader(ROOT::TTreeReaderValueBase* reader);
void DeregisterValueReader(ROOT::TTreeReaderValueBase* reader);

EEntryStatus SetEntryBase(Long64_t entry, Bool_t local);

private:

enum EPropertyBits {
Expand Down
6 changes: 5 additions & 1 deletion tree/treeplayer/inc/TTreeReaderValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,11 @@ class TTreeReaderValue: public ROOT::TTreeReaderValueBase {
TTreeReaderValue(TTreeReader& tr, const char* branchname):
TTreeReaderValueBase(&tr, branchname, TDictionary::GetDictionary(typeid(T))) {}

T* Get() {
T* Get() {
if (!fProxy){
Error("Get()", "Value reader not properly initialized, did you remember to call TTreeReader.Set(Next)Entry()?");
return 0;
}
void *address = GetAddress(); // Needed to figure out if it's a pointer
return fProxy->IsaPointer() ? *(T**)address : (T*)address; }
T* operator->() { return Get(); }
Expand Down
2 changes: 1 addition & 1 deletion tree/treeplayer/src/TBranchProxy.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ Bool_t ROOT::TBranchProxy::Setup()

// This is not sufficient for following pointers

} else if (!fBranch || fCurrentTreeNumber != fDirector->GetTree()->GetTreeNumber()) {
} else if (!fBranch || fCurrentTreeNumber != fDirector->GetTree()->GetTreeNumber() || fLastTree != fDirector->GetTree()) {

// This does not allow (yet) to precede the branch name with
// its mother's name
Expand Down
34 changes: 24 additions & 10 deletions tree/treeplayer/src/TTreeReader.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ void TTreeReader::Initialize()

//______________________________________________________________________________
Long64_t TTreeReader::GetCurrentEntry() const {
//Returns the index of the current entry being read

if (!fDirector) return 0;
Long64_t currentTreeEntry = fDirector->GetReadEntry();
if (fTree->IsA() == TChain::Class() && currentTreeEntry >= 0) {
Expand All @@ -94,7 +96,7 @@ Long64_t TTreeReader::GetCurrentEntry() const {
}

//______________________________________________________________________________
TTreeReader::EEntryStatus TTreeReader::SetEntry(Long64_t entry)
TTreeReader::EEntryStatus TTreeReader::SetEntryBase(Long64_t entry, Bool_t local)
{
// Load an entry into the tree, return the status of the read.
// For chains, entry is the global (i.e. not tree-local) entry number.
Expand All @@ -103,19 +105,27 @@ TTreeReader::EEntryStatus TTreeReader::SetEntry(Long64_t entry)
fEntryStatus = kEntryNoTree;
return fEntryStatus;
}
Int_t treeNumInChain = fTree->GetTreeNumber();

TTree* prevTree = fDirector->GetTree();

int loadResult = fTree->LoadTree(entry);
if (loadResult == -2) {
fEntryStatus = kEntryNotFound;
return fEntryStatus;
}
int loadResult;
if (!local){
Int_t treeNumInChain = fTree->GetTreeNumber();

Int_t currentTreeNumInChain = fTree->GetTreeNumber();
if (treeNumInChain != currentTreeNumInChain) {
fDirector->SetTree(fTree->GetTree());
loadResult = fTree->LoadTree(entry);

if (loadResult == -2) {
fEntryStatus = kEntryNotFound;
return fEntryStatus;
}

Int_t currentTreeNumInChain = fTree->GetTreeNumber();
if (treeNumInChain != currentTreeNumInChain) {
fDirector->SetTree(fTree->GetTree());
}
}
else {
loadResult = entry;
}
if (!prevTree || fDirector->GetReadEntry() == -1) {
// Tell readers we now have a tree
Expand Down Expand Up @@ -150,6 +160,10 @@ void TTreeReader::SetTree(TTree* tree)
if (!fDirector) {
Initialize();
}
else {
fDirector->SetTree(fTree);
fDirector->SetReadEntry(-1);
}
}

//______________________________________________________________________________
Expand Down
53 changes: 43 additions & 10 deletions tree/treeplayer/src/TTreeReaderArray.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,10 @@ namespace {
// Reader interface for leaf list
// SEE TTreeProxyGenerator.cxx:1319: '//We have a top level raw type'
class TObjectArrayReader: public ROOT::TVirtualCollectionReader {
private:
Int_t basicTypeSize;
public:
TObjectArrayReader() : basicTypeSize(-1) { }
~TObjectArrayReader() {}
TVirtualCollectionProxy* GetCP(ROOT::TBranchProxy* proxy) {
if (!proxy->Read()){
Expand All @@ -185,15 +188,26 @@ namespace {
virtual void* At(ROOT::TBranchProxy* proxy, size_t idx) {
if (!proxy->Read()) return 0;

Int_t objectSize;
void *array = (void*)proxy->GetStart();
TClass *myClass = proxy->GetClass();
if (!myClass){
Error("At()", "Cannot get class info from branch proxy.");
return 0;

if (basicTypeSize == -1){
TClass *myClass = proxy->GetClass();
if (!myClass){
Error("At()", "Cannot get class info from branch proxy.");
return 0;
}
objectSize = myClass->GetClassSize();
}
else {
objectSize = basicTypeSize;
}
Int_t objectSize = myClass->GetClassSize();
return (void*)((Byte_t*)array + (objectSize * idx));
}

void SetBasicTypeSize(Int_t size){
basicTypeSize = size;
}
};

class TArrayParameterSizeReader : public TObjectArrayReader {
Expand Down Expand Up @@ -525,16 +539,25 @@ void ROOT::TTreeReaderArrayBase::CreateProxy()
}
}
} else if (branch->IsA() == TBranch::Class()) {
printf("TBranch\n"); // TODO: Remove (necessary because of gdb bug)
TLeaf *topLeaf = branch->GetLeaf(branch->GetName());
Int_t size = 0;
TLeaf *sizeLeaf = topLeaf->GetLeafCounter(size);
if (!sizeLeaf) {
fImpl = new TArrayFixedSizeReader(size);
}
else {
fImpl = new TArrayParameterSizeReader(fTreeReader, sizeLeaf->GetName());
}
((TObjectArrayReader*)fImpl)->SetBasicTypeSize(((TDataType*)fDict)->Size());
} else if (branch->IsA() == TBranchClones::Class()) {
printf("TBranchClones\n"); // TODO: Remove (necessary because of gdb bug)
Error("CreateProxy", "Support for branches of type TBranchClones not implemented");
} else if (branch->IsA() == TBranchObject::Class()) {
printf("TBranchObject\n"); // TODO: Remove (necessary because of gdb bug)
Error("CreateProxy", "Support for branches of type TBranchObject not implemented");
} else if (branch->IsA() == TBranchSTL::Class()) {
printf("TBranchSTL\n"); // TODO: Remove (necessary because of gdb bug)
Error("CreateProxy", "Support for branches of type TBranchSTL not implemented");
fImpl = new TSTLReader();
} else if (branch->IsA() == TBranchRef::Class()) {
printf("TBranchRef\n"); // TODO: Remove (necessary because of gdb bug)
Error("CreateProxy", "Support for branches of type TBranchRef not implemented");
}
}

Expand Down Expand Up @@ -702,6 +725,16 @@ const char* ROOT::TTreeReaderArrayBase::GetBranchContentDataType(TBranch* branch
const char* dataTypeName = branch->GetClassName();
if ((!dataTypeName || !dataTypeName[0])
&& branch->IsA() == TBranch::Class()) {
TLeaf *myLeaf = branch->GetLeaf(branch->GetName());
if (myLeaf){
TDictionary *myDataType = TDictionary::GetDictionary(myLeaf->GetTypeName());
if (myDataType->IsA() == TDataType::Class()){
dict = TDataType::GetDataType((EDataType)((TDataType*)myDataType)->GetType());
contentTypeName = myLeaf->GetTypeName();
return 0;
}
}

// leaflist. Can't represent.
Error("GetBranchContentDataType()", "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 TTreeReaderValueArray:", branch->GetName());
TIter iLeaves(branch->GetListOfLeaves());
Expand Down
17 changes: 17 additions & 0 deletions tree/treeplayer/src/TTreeReaderValue.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ ROOT::TTreeReaderValueBase::~TTreeReaderValueBase()
//______________________________________________________________________________
ROOT::TTreeReaderValueBase::EReadStatus
ROOT::TTreeReaderValueBase::ProxyRead() {
// Try to read the value from the TBranchProxy, returns
// the status of the read.

if (!fProxy) return kReadNothingYet;
if (fProxy->Read()) {
fReadStatus = kReadSuccess;
Expand All @@ -81,6 +84,8 @@ ROOT::TTreeReaderValueBase::ProxyRead() {

//______________________________________________________________________________
TLeaf* ROOT::TTreeReaderValueBase::GetLeaf() {
// If we are reading a leaf, return the corresponding TLeaf.

if (fLeafName.Length() > 0){

Long64_t newChainOffset = fTreeReader->GetTree()->GetChainOffset();
Expand Down Expand Up @@ -119,6 +124,8 @@ TLeaf* ROOT::TTreeReaderValueBase::GetLeaf() {

//______________________________________________________________________________
void* ROOT::TTreeReaderValueBase::GetAddress() {
// Returns the memory address of the object being read.

if (ProxyRead() != kReadSuccess) return 0;

if (fLeafName.Length() > 0){
Expand Down Expand Up @@ -317,6 +324,16 @@ const char* ROOT::TTreeReaderValueBase::GetBranchDataType(TBranch* branch,
const char* dataTypeName = branch->GetClassName();
if ((!dataTypeName || !dataTypeName[0])
&& branch->IsA() == TBranch::Class()) {
TLeaf *myLeaf = branch->GetLeaf(branch->GetName());
if (myLeaf){
TDictionary *myDataType = TDictionary::GetDictionary(myLeaf->GetTypeName());
if (myDataType->IsA() == TDataType::Class()){
dict = TDataType::GetDataType((EDataType)((TDataType*)myDataType)->GetType());
return myLeaf->GetTypeName();
}
}


// leaflist. Can't represent.
Error("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 TTreeReaderValueArray:", branch->GetName());
TIter iLeaves(branch->GetListOfLeaves());
Expand Down
Loading