Dear experts,
I’ve been struggling to write a macro that compares root files specific to our experiment. The tool is expected to handle a variety of file types that include custom classes created and maintained privately by users (hence they can be nested). I’ve been able to handle most of the usual stuff we gather in a typical file (histograms, graphs). I am currently struggling with parsing TTrees. Since most of the time I will not have many details on the structure, I’ve been scouring the forum for solutions on reading it via leaves. Basically, after loading a TTree and retrieving the first entry, trying to do:
// TTree::GetEntry() already called at this step
TLeaf *some_leaf = some_branch->GetListOfLeaves()->At(some_id);
some_leaf->GetValuePointer(); // this return nullptr
Any help on this is greatly appreciated. I’ve attached a sample macro illustrating the problem and here is a sample file that can be used CERNBox .
Regards,
Stefan
on behalf of NA62
test.cc (4.9 KB)
ROOT Version: 6.26/08
Platform: Ubuntu 22.04
Compiler: gcc version 10.4.0 (conda-forge gcc 10.4.0-19)
pcanal
January 20, 2025, 4:16pm
2
What does:
some_leaf->Print("");
some_leaf->GetBranch()->GetMother()->Print();
prints to the screen for the leaf that has some_leaf->GetValuePointer() == nullptr
?
Thanks for the prompt answer! Here is the output:
======== Print() =============
OBJ: TLeafElement fKineParts.fPDGcode fPDGcode[fKineParts_]
======== branch->GetMother->Print() =============
*Branch :Generated *
*Entries : 1000 : BranchElement (see below) *
*............................................................................*
*Br 0 :fUniqueID : UInt_t *
*Entries : 1000 : Total Size= 4589 bytes File Size = 130 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 31.38 *
*............................................................................*
*Br 1 :fBits : UInt_t *
*Entries : 1000 : Total Size= 8577 bytes File Size = 1392 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 5.81 *
*............................................................................*
*Br 2 :fEventNumber : Int_t *
*Entries : 1000 : Total Size= 4604 bytes File Size = 1526 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 2.68 *
*............................................................................*
*Br 3 :fNPurgedKineParts : Int_t *
*Entries : 1000 : Total Size= 4629 bytes File Size = 138 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 29.62 *
*............................................................................*
*Br 4 :fEventWeight : Float_t *
*Entries : 1000 : Total Size= 4604 bytes File Size = 141 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 28.96 *
*............................................................................*
*Br 5 :fOverlayBeamIntensity : Float_t *
*Entries : 1000 : Total Size= 4649 bytes File Size = 142 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 28.82 *
*............................................................................*
*Br 6 :fGeneParts : Int_t fGeneParts_ *
*Entries : 1000 : Total Size= 12191 bytes File Size = 1428 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 5.66 *
*............................................................................*
*Br 7 :fGeneParts.fUniqueID : UInt_t fUniqueID[fGeneParts_] *
*Entries : 1000 : Total Size= 16654 bytes File Size = 1552 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 10.32 *
*............................................................................*
*Br 8 :fGeneParts.fBits : UInt_t fBits[fGeneParts_] *
*Entries : 1000 : Total Size= 16634 bytes File Size = 1561 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 10.26 *
*............................................................................*
*Br 9 :fGeneParts.fPDGcode : Int_t fPDGcode[fGeneParts_] *
*Entries : 1000 : Total Size= 16649 bytes File Size = 1573 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 10.18 *
*............................................................................*
*Br 10 :fGeneParts.fMass : Float_t fMass[fGeneParts_] *
*Entries : 1000 : Total Size= 16634 bytes File Size = 4980 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 3.22 *
*............................................................................*
*Br 11 :fGeneParts.fInitialMomentum : *
* | TVector3 fInitialMomentum[fGeneParts_] *
*Entries : 1000 : Total Size= 124251 bytes File Size = 60039 *
*Baskets : 4 : Basket Size= 32000 bytes Compression= 2.06 *
*............................................................................*
*Br 12 :fKineParts : Int_t fKineParts_ *
*Entries : 1000 : Total Size= 24972 bytes File Size = 2061 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 3.92 *
*............................................................................*
*Br 13 :fKineParts.fUniqueID : UInt_t fUniqueID[fKineParts_] *
*Entries : 1000 : Total Size= 18766 bytes File Size = 1909 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 9.50 *
*............................................................................*
*Br 14 :fKineParts.fBits : UInt_t fBits[fKineParts_] *
*Entries : 1000 : Total Size= 18746 bytes File Size = 1913 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 9.47 *
*............................................................................*
*Br 15 :fKineParts.fPDGcode : Int_t fPDGcode[fKineParts_] *
*Entries : 1000 : Total Size= 18761 bytes File Size = 3050 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 5.94 *
*............................................................................*
*Br 16 :fKineParts.fMass : Float_t fMass[fKineParts_] *
*Entries : 1000 : Total Size= 18746 bytes File Size = 5770 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 3.14 *
*............................................................................*
*Br 17 :fKineParts.fInitialMomentum : *
* | TVector3 fInitialMomentum[fKineParts_] *
*Entries : 1000 : Total Size= 145477 bytes File Size = 90943 *
*Baskets : 5 : Basket Size= 32000 bytes Compression= 1.59 *
*............................................................................*
*Br 18 :fKineParts.fMCTrackID : Int_t fMCTrackID[fKineParts_] *
*Entries : 1000 : Total Size= 18771 bytes File Size = 6435 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 2.82 *
*............................................................................*
*Br 19 :fKineParts.fParentIndex : Int_t fParentIndex[fKineParts_] *
*Entries : 1000 : Total Size= 18781 bytes File Size = 3081 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 5.88 *
*............................................................................*
*Br 20 :fKineParts.fProdPos.fUniqueID : UInt_t fUniqueID[fKineParts_] *
*Entries : 1000 : Total Size= 18793 bytes File Size = 1912 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 9.49 *
*............................................................................*
*Br 21 :fKineParts.fProdPos.fBits : UInt_t fBits[fKineParts_] *
*Entries : 1000 : Total Size= 18773 bytes File Size = 1912 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 9.48 *
*............................................................................*
*Br 22 :fKineParts.fProdPos.fX : Float_t fX[fKineParts_] *
*Entries : 1000 : Total Size= 18758 bytes File Size = 10669 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.70 *
*............................................................................*
*Br 23 :fKineParts.fProdPos.fY : Float_t fY[fKineParts_] *
*Entries : 1000 : Total Size= 18758 bytes File Size = 10677 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.70 *
*............................................................................*
*Br 24 :fKineParts.fProdPos.fZ : Float_t fZ[fKineParts_] *
*Entries : 1000 : Total Size= 18758 bytes File Size = 8067 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 2.25 *
*............................................................................*
*Br 25 :fKineParts.fProdPos.fT : Float_t fT[fKineParts_] *
*Entries : 1000 : Total Size= 18758 bytes File Size = 7922 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 2.29 *
*............................................................................*
*Br 26 :fKineParts.fEndPos.fUniqueID : UInt_t fUniqueID[fKineParts_] *
*Entries : 1000 : Total Size= 18790 bytes File Size = 1922 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 9.44 *
*............................................................................*
*Br 27 :fKineParts.fEndPos.fBits : UInt_t fBits[fKineParts_] *
*Entries : 1000 : Total Size= 18770 bytes File Size = 1921 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 9.44 *
*............................................................................*
*Br 28 :fKineParts.fEndPos.fX : Float_t fX[fKineParts_] *
*Entries : 1000 : Total Size= 18755 bytes File Size = 15375 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.18 *
*............................................................................*
*Br 29 :fKineParts.fEndPos.fY : Float_t fY[fKineParts_] *
*Entries : 1000 : Total Size= 18755 bytes File Size = 15516 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.17 *
*............................................................................*
*Br 30 :fKineParts.fEndPos.fZ : Float_t fZ[fKineParts_] *
*Entries : 1000 : Total Size= 18755 bytes File Size = 11948 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.52 *
*............................................................................*
*Br 31 :fKineParts.fEndPos.fT : Float_t fT[fKineParts_] *
*Entries : 1000 : Total Size= 18755 bytes File Size = 13338 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.36 *
*............................................................................*
*Br 32 :fKineParts.fFinalMomentum : TVector3 fFinalMomentum[fKineParts_] *
*Entries : 1000 : Total Size= 145459 bytes File Size = 63577 *
*Baskets : 5 : Basket Size= 32000 bytes Compression= 2.28 *
*............................................................................*
*Br 33 :fKineParts.fProdProcessName : TString fProdProcessName[fKineParts_]*
*Entries : 1000 : Total Size= 29881 bytes File Size = 3567 *
*Baskets : 2 : Basket Size= 32000 bytes Compression= 8.19 *
*............................................................................*
*Br 34 :fKineParts.fEndProcessName : TString fEndProcessName[fKineParts_] *
*Entries : 1000 : Total Size= 41784 bytes File Size = 4942 *
*Baskets : 2 : Basket Size= 32000 bytes Compression= 8.32 *
*............................................................................*
*Br 35 :fKineParts.fPosAtCheckPoints : *
* | vector<vector<Float3Vector> > fPosAtCheckPoints[fKineParts_] *
*Entries : 1000 : Total Size= 919009 bytes File Size = 197284 *
*Baskets : 30 : Basket Size= 32000 bytes Compression= 4.65 *
*............................................................................*
*Br 36 :fKineParts.fMomAtCheckPoints : *
* | vector<vector<Float4Vector> > fMomAtCheckPoints[fKineParts_] *
*Entries : 1000 : Total Size= 1092454 bytes File Size = 259055 *
*Baskets : 35 : Basket Size= 32000 bytes Compression= 4.21 *
*............................................................................*
*Br 37 :fEventBoundaries : Int_t fEventBoundaries_ *
*Entries : 1000 : Total Size= 14990 bytes File Size = 1399 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 5.79 *
*............................................................................*
*Br 38 :fEventBoundaries.fUniqueID : UInt_t fUniqueID[fEventBoundaries_] *
*Entries : 1000 : Total Size= 8780 bytes File Size = 1406 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 5.76 *
*............................................................................*
*Br 39 :fEventBoundaries.fBits : UInt_t fBits[fEventBoundaries_] *
*Entries : 1000 : Total Size= 8760 bytes File Size = 1407 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 5.76 *
*............................................................................*
*Br 40 :fEventBoundaries.fID : Int_t fID[fEventBoundaries_] *
*Entries : 1000 : Total Size= 8750 bytes File Size = 1403 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 5.77 *
*............................................................................*
*Br 41 :fEventBoundaries.fStreamID : Int_t fStreamID[fEventBoundaries_] *
*Entries : 1000 : Total Size= 8780 bytes File Size = 1443 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 5.62 *
*............................................................................*
*Br 42 :fEventBoundaries.fTime : Double_t fTime[fEventBoundaries_] *
*Entries : 1000 : Total Size= 12760 bytes File Size = 1331 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 9.09 *
*............................................................................*
*Br 43 :fEventBoundaries.fFirstGenePartIndex : *
* | Int_t fFirstGenePartIndex[fEventBoundaries_] *
*Entries : 1000 : Total Size= 8830 bytes File Size = 1420 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 5.71 *
*............................................................................*
*Br 44 :fEventBoundaries.fNGeneParts : Int_t fNGeneParts[fEventBoundaries_]*
*Entries : 1000 : Total Size= 8790 bytes File Size = 1446 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 5.61 *
*............................................................................*
*Br 45 :fEventBoundaries.fFirstKinePartIndex : *
* | Int_t fFirstKinePartIndex[fEventBoundaries_] *
*Entries : 1000 : Total Size= 8830 bytes File Size = 1420 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 5.71 *
*............................................................................*
*Br 46 :fEventBoundaries.fNKineParts : Int_t fNKineParts[fEventBoundaries_]*
*Entries : 1000 : Total Size= 8790 bytes File Size = 2080 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 3.90 *
*............................................................................*
pcanal
January 22, 2025, 4:49pm
4
======== Print() =============
OBJ: TLeafElement fKineParts.fPDGcode fPDGcode[fKineParts_]
This is a element that is inside a collection. There is no single address (for each entries there is many value of fKineParts.fPDGcode
) and this is why GetValuePointer
return nullptr.
In order to read those content you would need to use the corresponding TVirtualCollectionProxy
and the address of the collection (in this case auto be = TBranchElement*)tree->GetBranch("fKineParts"); be->GetObject())
) and then using the TVirtualCollectionProxy
to iterate through the elements.
I see. Thank you! I’ll try implementing your suggestion.
system
Closed
February 11, 2025, 12:47pm
7
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.