When setting the branch address of a branch, a pointer to the correct variable type is needed.
There exist multiple methods to find the type needed.
There will be error output similar to the following if you choose the wrong one:
Error in <TTree::SetBranchAddress>: The pointer type given "Int_t" (3) does not correspond to the type needed "Double_t" (8) by the branch: branch_name
. This is hard to automate, though.
TBranch
has the method Int_t TBranch::GetExpectedType(TClass *&expectedClass, EDataType &expectedType)
which fills EDataType expectedType
with the type needed (for example with (EDataType) (EDataType::kDouble_t) : (int) 8
for a Double_t
).
TLeaf
has a method virtual const char* TLeaf::GetTypeName() const
that returns the type of the leaf as a string (so e.g. "Double_t"
).
Is there a way to dynamically create a variable of the type needed by a given TBranch
? Especially with the second method given above, I thought it must be possible, yet I cannot seem to find a way. There must be a way, though, since it is possible to just use the branches of a tree in pyROOT just by knowing their name…
Axel
May 10, 2017, 5:38pm
2
Hi,
After LoadEntry()
, GetAddress()
will point to an allocated object (or a member of an allocated object). Does that do?
Axel
Neither TBranch
, nor TLeaf
, nor TTree
have a method called LoadEntry()
. Did you mean GetEntry()
? If that is the case, then, no, this does not work:
root [22] auto t = (TTree*) _file0->Get("DYTuple/DecayTree")
(TTree *) 0x556242819b40
root [24] auto b = (TBranch*)t->GetListOfBranches()->FindObject("y")
(TBranch *) 0x556242c7fdd0
root [32] b->GetEntry()
(int) 8
root [33] b->GetAddress()
(char *) nullptr
The integer returned by GetEntry
is the same integer which one can get from GetExpectedType
(8 in this case, because the branch needs a Double_t
), though.
Axel
May 11, 2017, 6:51am
4
Apologies - that happens when replying from memory… Here is the correct sequence:
branch->GetEntry(0);
branch->GetAddress()
Cheers, Axel.
I also tried that:
root [39] b->GetEntry(0)
(int) 8
root [40] b->GetAddress()
(char *) nullptr
Axel
May 11, 2017, 9:04am
6
Can you post the output of tree->Print()?
Sure, here you go (I skipped the other branches, some are Int_t, some Long64_t, but most are Double_t):
root [2] t->Print()
******************************************************************************
*Tree :DecayTree : DecayTree *
*Entries : 1246 : Total = 518020 bytes File Size = 363058 *
* : : Tree compression factor = 1.36 *
******************************************************************************
[Skipping the other branches...]
*............................................................................*
*Br 57 :y : y/D *
*Entries : 1246 : Total Size= 10507 bytes File Size = 9453 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.06 *
*............................................................................*
If you do need the full output I can also post that, though the above is the branch I tried to access in my test in the previous posts.
Axel
May 11, 2017, 9:10am
8
I’d actually need the other branches… to understand whether “y” is part of something else.
You can also try to call tree->SetMakeClass(1)
which doesn’t read an object as an object, but as a set of double
, int
etc. Like that, every branch should get its address!
Axel.
It is not part of anything else, just a plain branch with one leaf. But here you go:
root [2] t->Print()
******************************************************************************
*Tree :DecayTree : DecayTree *
*Entries : 1246 : Total = 518020 bytes File Size = 363058 *
* : : Tree compression factor = 1.36 *
******************************************************************************
*Br 0 :Z0_ENDVERTEX_CHI2 : Z0_ENDVERTEX_CHI2/D *
*Entries : 1246 : Total Size= 10587 bytes File Size = 6113 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.65 *
*............................................................................*
*Br 1 :Z0_ENDVERTEX_NDOF : Z0_ENDVERTEX_NDOF/I *
*Entries : 1246 : Total Size= 5595 bytes File Size = 149 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 34.05 *
*............................................................................*
*Br 2 :Z0_P : Z0_P/D *
*Entries : 1246 : Total Size= 10522 bytes File Size = 9657 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.04 *
*............................................................................*
*Br 3 :Z0_PT : Z0_PT/D *
*Entries : 1246 : Total Size= 10527 bytes File Size = 9541 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.05 *
*............................................................................*
*Br 4 :Z0_PE : Z0_PE/D *
*Entries : 1246 : Total Size= 10527 bytes File Size = 9656 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.04 *
*............................................................................*
*Br 5 :Z0_PX : Z0_PX/D *
*Entries : 1246 : Total Size= 10527 bytes File Size = 5818 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.73 *
*............................................................................*
*Br 6 :Z0_PY : Z0_PY/D *
*Entries : 1246 : Total Size= 10527 bytes File Size = 5799 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.73 *
*............................................................................*
*Br 7 :Z0_PZ : Z0_PZ/D *
*Entries : 1246 : Total Size= 10527 bytes File Size = 6223 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.61 *
*............................................................................*
*Br 8 :Z0_MM : Z0_MM/D *
*Entries : 1246 : Total Size= 10527 bytes File Size = 6283 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.60 *
*............................................................................*
*Br 9 :Z0_L0DiMuonDecision_TOS : Z0_L0DiMuonDecision_TOS/O *
*Entries : 1246 : Total Size= 1881 bytes File Size = 126 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 10.64 *
*............................................................................*
*Br 10 :Z0_Hlt1DiMuonHighMassDecision_TOS : *
* | Z0_Hlt1DiMuonHighMassDecision_TOS/O *
*Entries : 1246 : Total Size= 1931 bytes File Size = 136 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 9.93 *
*............................................................................*
*Br 11 :Z0_Hlt1DiMuonLowMassDecision_TOS : *
* | Z0_Hlt1DiMuonLowMassDecision_TOS/O *
*Entries : 1246 : Total Size= 1926 bytes File Size = 351 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 3.85 *
*............................................................................*
*Br 12 :Z0_Hlt2DiMuonDY2Decision_TOS : Z0_Hlt2DiMuonDY2Decision_TOS/O *
*Entries : 1246 : Total Size= 1906 bytes File Size = 285 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 4.72 *
*............................................................................*
*Br 13 :Z0_Hlt2DiMuonDY3Decision_TOS : Z0_Hlt2DiMuonDY3Decision_TOS/O *
*Entries : 1246 : Total Size= 1906 bytes File Size = 210 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 6.41 *
*............................................................................*
*Br 14 :Z0_Hlt2DiMuonDY4Decision_TOS : Z0_Hlt2DiMuonDY4Decision_TOS/O *
*Entries : 1246 : Total Size= 1906 bytes File Size = 329 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 4.09 *
*............................................................................*
*Br 15 :Z0_Hlt2DiMuonUnbiasedZmmDecision_TOS : *
* | Z0_Hlt2DiMuonUnbiasedZmmDecision_TOS/O *
*Entries : 1246 : Total Size= 1946 bytes File Size = 139 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 9.74 *
*............................................................................*
*Br 16 :muplus_TrEta : muplus_TrEta/D *
*Entries : 1246 : Total Size= 10562 bytes File Size = 9430 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.07 *
*............................................................................*
*Br 17 :muplus_TrPhi : muplus_TrPhi/D *
*Entries : 1246 : Total Size= 10562 bytes File Size = 9738 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.03 *
*............................................................................*
*Br 18 :muplus_TrPChi2 : muplus_TrPChi2/D *
*Entries : 1246 : Total Size= 10572 bytes File Size = 9501 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.06 *
*............................................................................*
*Br 19 :muplus_MINIP : muplus_MINIP/D *
*Entries : 1246 : Total Size= 10562 bytes File Size = 9503 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.06 *
*............................................................................*
*Br 20 :muplus_OnlyJet_PT : muplus_OnlyJet_PT/D *
*Entries : 1246 : Total Size= 10587 bytes File Size = 9613 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.05 *
*............................................................................*
*Br 21 :muplus_OnlyJet_P : muplus_OnlyJet_P/D *
*Entries : 1246 : Total Size= 10582 bytes File Size = 9660 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.04 *
*............................................................................*
*Br 22 :muplus_P : muplus_P/D *
*Entries : 1246 : Total Size= 10542 bytes File Size = 9609 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.05 *
*............................................................................*
*Br 23 :muplus_PT : muplus_PT/D *
*Entries : 1246 : Total Size= 10547 bytes File Size = 9489 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.06 *
*............................................................................*
*Br 24 :muplus_PE : muplus_PE/D *
*Entries : 1246 : Total Size= 10547 bytes File Size = 9612 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.05 *
*............................................................................*
*Br 25 :muplus_PX : muplus_PX/D *
*Entries : 1246 : Total Size= 10547 bytes File Size = 5945 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.69 *
*............................................................................*
*Br 26 :muplus_PY : muplus_PY/D *
*Entries : 1246 : Total Size= 10547 bytes File Size = 5911 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.70 *
*............................................................................*
*Br 27 :muplus_PZ : muplus_PZ/D *
*Entries : 1246 : Total Size= 10547 bytes File Size = 6185 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.62 *
*............................................................................*
*Br 28 :muplus_ProbNNmu : muplus_ProbNNmu/D *
*Entries : 1246 : Total Size= 10577 bytes File Size = 9363 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.07 *
*............................................................................*
*Br 29 :muplus_cpt_0.10 : muplus_cpt_0.10/D *
*Entries : 1246 : Total Size= 10577 bytes File Size = 2279 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 4.41 *
*............................................................................*
*Br 30 :muplus_cpt_0.50 : muplus_cpt_0.50/D *
*Entries : 1246 : Total Size= 10577 bytes File Size = 8037 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.25 *
*............................................................................*
*Br 31 :muminus_TrEta : muminus_TrEta/D *
*Entries : 1246 : Total Size= 10567 bytes File Size = 9459 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.06 *
*............................................................................*
*Br 32 :muminus_TrPhi : muminus_TrPhi/D *
*Entries : 1246 : Total Size= 10567 bytes File Size = 9764 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.03 *
*............................................................................*
*Br 33 :muminus_TrPChi2 : muminus_TrPChi2/D *
*Entries : 1246 : Total Size= 10577 bytes File Size = 9542 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.05 *
*............................................................................*
*Br 34 :muminus_MINIP : muminus_MINIP/D *
*Entries : 1246 : Total Size= 10567 bytes File Size = 9554 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.05 *
*............................................................................*
*Br 35 :muminus_OnlyJet_PT : muminus_OnlyJet_PT/D *
*Entries : 1246 : Total Size= 10592 bytes File Size = 9661 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.04 *
*............................................................................*
*Br 36 :muminus_OnlyJet_P : muminus_OnlyJet_P/D *
*Entries : 1246 : Total Size= 10587 bytes File Size = 9708 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.04 *
*............................................................................*
*Br 37 :muminus_P : muminus_P/D *
*Entries : 1246 : Total Size= 10547 bytes File Size = 9645 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.04 *
*............................................................................*
*Br 38 :muminus_PT : muminus_PT/D *
*Entries : 1246 : Total Size= 10552 bytes File Size = 9506 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.06 *
*............................................................................*
*Br 39 :muminus_PE : muminus_PE/D *
*Entries : 1246 : Total Size= 10552 bytes File Size = 9642 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.04 *
*............................................................................*
*Br 40 :muminus_PX : muminus_PX/D *
*Entries : 1246 : Total Size= 10552 bytes File Size = 5976 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.68 *
*............................................................................*
*Br 41 :muminus_PY : muminus_PY/D *
*Entries : 1246 : Total Size= 10552 bytes File Size = 5919 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.70 *
*............................................................................*
*Br 42 :muminus_PZ : muminus_PZ/D *
*Entries : 1246 : Total Size= 10552 bytes File Size = 6211 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.62 *
*............................................................................*
*Br 43 :muminus_ProbNNmu : muminus_ProbNNmu/D *
*Entries : 1246 : Total Size= 10582 bytes File Size = 9396 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.07 *
*............................................................................*
*Br 44 :muminus_cpt_0.10 : muminus_cpt_0.10/D *
*Entries : 1246 : Total Size= 10582 bytes File Size = 2457 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 4.09 *
*............................................................................*
*Br 45 :muminus_cpt_0.50 : muminus_cpt_0.50/D *
*Entries : 1246 : Total Size= 10582 bytes File Size = 8064 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.25 *
*............................................................................*
*Br 46 :totCandidates : totCandidates/l *
*Entries : 1246 : Total Size= 10567 bytes File Size = 317 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 31.71 *
*............................................................................*
*Br 47 :runNumber : runNumber/i *
*Entries : 1246 : Total Size= 5555 bytes File Size = 197 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 25.71 *
*............................................................................*
*Br 48 :eventNumber : eventNumber/l *
*Entries : 1246 : Total Size= 10557 bytes File Size = 6107 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.65 *
*............................................................................*
*Br 49 :nPVs : nPVs/I *
*Entries : 1246 : Total Size= 5530 bytes File Size = 898 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 5.63 *
*............................................................................*
*Br 50 :nTracks : nTracks/I *
*Entries : 1246 : Total Size= 5545 bytes File Size = 2408 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 2.10 *
*............................................................................*
*Br 51 :nVeloTracks : nVeloTracks/I *
*Entries : 1246 : Total Size= 5565 bytes File Size = 2154 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 2.35 *
*............................................................................*
*Br 52 :nITClusters : nITClusters/I *
*Entries : 1246 : Total Size= 5565 bytes File Size = 2789 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.82 *
*............................................................................*
*Br 53 :nSPDHits : nSPDHits/I *
*Entries : 1246 : Total Size= 5550 bytes File Size = 2604 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.94 *
*............................................................................*
*Br 54 :nMuonTracks : nMuonTracks/I *
*Entries : 1246 : Total Size= 5565 bytes File Size = 1421 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 3.57 *
*............................................................................*
*Br 55 :opening_angle : opening_angle/D *
*Entries : 1246 : Total Size= 10567 bytes File Size = 9171 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.10 *
*............................................................................*
*Br 56 :isolation : isolation/D *
*Entries : 1246 : Total Size= 10547 bytes File Size = 8969 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.12 *
*............................................................................*
*Br 57 :y : y/D *
*Entries : 1246 : Total Size= 10507 bytes File Size = 9453 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.06 *
*............................................................................*
*Br 58 :test : test/I *
*Entries : 1246 : Total Size= 5530 bytes File Size = 3186 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.59 *
*............................................................................*
I tried the SetMakeClass
, but I still get a null pointer:
root [3] t->SetMakeClass(1)
root [4] auto b = (TBranch*)t->GetListOfBranches()->FindObject("y")
(TBranch *) 0x5598b5e9c2d0
root [5] b->GetEntry(0)
(int) 8
root [6] b->GetAddress()
(char *) nullptr
Axel
May 11, 2017, 9:14am
11
Ah sorry - it’s a leaf Don’t we all love the TTree complexity…
Try t->GetLeaf("y")->GetValuePointer()
Cheers, Axel.
This seems to work
Attaching file ../ntuple/dy_tuple_12_md_reduced_test.root as _file0...
(TFile *) 0x560f1c30c6a0
root [1] auto t = (TTree*) _file0->Get("DYTuple/DecayTree")
(TTree *) 0x560f1c703a80
root [2] t->GetLeaf("y")->GetValuePointer()
(void *) 0x560f1d0b4ab0
root [3] auto b = (TBranch*)t->GetListOfBranches()->FindObject("y")
(TBranch *) 0x560f1cd09430
root [4] b->SetAddress(t->GetLeaf("y")->GetValuePointer())
root [5] b->GetEntry(0)
(int) 8
root [8] auto f = new TTreeFormula("y", "y", t)
(TTreeFormula *) 0x560f1e08dbb0
root [9] f->EvalInstance()
(double) 3.485760
(Which is what I wanted to use it for in the end, setting the branch addresses correctly and automatically so a TTreeFormula
works.
Ok, I was stupid. As I mentioned, in the end what I want to do is evaluate a TTreeFormula
. And this already sets the addresses internally correctly, so I don’t even need to do this…
But thank you for your help, it made quite a few internal things of ROOT more clear to me.
Cheers,
Andreas
system
Closed
May 25, 2017, 9:41am
15
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.