Loop over TVector

Hi,

I’m new to ROOT and I came across some behaviour I don’t understand. Maybe someone can explain to me:

I want to read some variables (one is a simple float, the others are x- and yvalue of a ROOT.TVector) stored in a tree, so I did the following:

data=[]
for entry in file.tree:
    data.append([entry.VectorVariable[0], entry.FloatVariable])

this seems to work but if I want to have the second entry of the vector in addition, like so:

for entry in file.tree:
    data.append([entry.VectorVariable[0], entry.VectorVariable[1], entry.FloatVariable])

this results in an index out of range.

Obviously I’m doing something wrong here, since even if I try to retrieve the whole TVector data[0] holds TVectors with the same xvalues and only the yvalues varying.

How to read the correct values for xvalue, yvalue and float for every event stored in the tree?

Need check size vector first, make sure has all data. Does “len(entry.VectorVariable)” has wanted 2 size always? If no, need provide sample .root file and script to help debug.

-Dom

Hi Dom,
thanks for your quick reply. Indeed there are entries with len(vector)=1 #-o … I should have checked first.
Sorry if I keep asking stupid questions but I still have some difficulties

here’s the tree.Print():

******************************************************************************
*Tree    :Data_SRAll: 0-lepton small ntuple                                  *
*Entries :   151369 : Total =       139562684 bytes  File  Size =   37854222 *
*        :          : Tree compression factor =   3.59                       *
******************************************************************************
*Br    0 :NTVars    : RunNumber/i:EventNumber/i:LumiBlockNumber/i:veto/i:    *
*         | eventWeight/F:pileupWeight/F:pileupWeightUp/F:pileupWeightDown/F:*
*         | genWeight/F:ttbarWeightHT/F:ttbarWeightPt2/F:ttbarAvgPt/F:       *
*         | WZweight/F:nJet/i:met/F:metPhi/F:dPhi/F:dPhiR/F:meffInc/F:       *
*         | hardproc/I:nBJet/I:nCJet/I:bTagWeight/F:bTagWeightBUp/F:         *
*         | bTagWeightBDown/F:bTagWeightCUp/F:bTagWeightCDown/F:             *
*         | bTagWeightLUp/F:bTagWeightLDown/F:cTagWeight/F:cTagWeightBUp/F:  *
*         | cTagWeightBDown/F:cTagWeightCUp/F:cTagWeightCDown/F:             *
*         | cTagWeightLUp/F:cTagWeightLDown/F:normWeight/F:normWeightUp/F:   *
*         | normWeightDown/F:cleaning/i:timing/F:jet1Emf/F:jet2Emf/F:        *
*         | jet1Chf/F:jet2Chf/F:pdfId1/I:pdfId2/I:tauN/i:tauJetBDTLoose/i:   *
*         | tauLooseN/i:tauMt/F:SherpaBugMET/F:dPhiBadTile/F:isNCBEvent/O    *
*Entries :   151369 : Total  Size=   32828784 bytes  File Size  =    8336751 *
*Baskets :      313 : Basket Size=     555008 bytes  Compression=   3.87     *
*............................................................................*
*Br    1 :jetPt     : vector<float>                                          *
*Entries :   151369 : Total  Size=    3666860 bytes  File Size  =    1941581 *
*Baskets :       53 : Basket Size=     174080 bytes  Compression=   1.82     *
*............................................................................*
*Br    2 :jetEta    : vector<float>                                          *
*Entries :   151369 : Total  Size=    3666919 bytes  File Size  =    2006645 *
*Baskets :       53 : Basket Size=     174080 bytes  Compression=   1.76     *
*............................................................................*
*Br    3 :jetPhi    : vector<float>                                          *
*Entries :   151369 : Total  Size=    3666919 bytes  File Size  =    2003904 *
*Baskets :       53 : Basket Size=     174080 bytes  Compression=   1.76     *
*............................................................................*
*Br    4 :jetM      : vector<float>                                          *
*Entries :   151369 : Total  Size=    3666801 bytes  File Size  =    1950585 *
*Baskets :       53 : Basket Size=     174080 bytes  Compression=   1.81     *
*............................................................................*
*Br    5 :jetBTag   : vector<float>                                          *
*Entries :   151369 : Total  Size=    3666978 bytes  File Size  =    1777931 *
*Baskets :       53 : Basket Size=     174080 bytes  Compression=   1.98     *
*............................................................................*
*Br    6 :jetFlav   : vector<int>                                            *
*Entries :   151369 : Total  Size=    3666978 bytes  File Size  =     539917 *
*Baskets :       53 : Basket Size=     174080 bytes  Compression=   6.54     *
*............................................................................*
*Br    7 :jetTagU   : vector<float>                                          *
*Entries :   151369 : Total  Size=    3666978 bytes  File Size  =     539706 *
*Baskets :       53 : Basket Size=     174080 bytes  Compression=   6.54     *
*............................................................................*
*Br    8 :jetTagB   : vector<float>                                          *
*Entries :   151369 : Total  Size=    3666978 bytes  File Size  =     539706 *
*Baskets :       53 : Basket Size=     174080 bytes  Compression=   6.54     *
*............................................................................*
*Br    9 :jetTagC   : vector<float>                                          *
*Entries :   151369 : Total  Size=    3666978 bytes  File Size  =     539706 *
*Baskets :       53 : Basket Size=     174080 bytes  Compression=   6.54     *
*............................................................................*
*Br   10 :jetFracSamplingMax : vector<float>                                 *
*Entries :   151369 : Total  Size=    3667810 bytes  File Size  =    1917979 *
*Baskets :       54 : Basket Size=     174080 bytes  Compression=   1.84     *
*............................................................................*
*Br   11 :jetFracSamplingMaxIndex : vector<float>                            *
*Entries :   151369 : Total  Size=    3668144 bytes  File Size  =     845146 *
*Baskets :       54 : Basket Size=     174080 bytes  Compression=   4.18     *
*............................................................................*
*Br   12 :tauPt     : vector<float>                                          *
*Entries :   151369 : Total  Size=    2170042 bytes  File Size  =     309365 *
*Baskets :       43 : Basket Size=     105984 bytes  Compression=   6.92     *
*............................................................................*
*Br   13 :tauEta    : vector<float>                                          *
*Entries :   151369 : Total  Size=    2170091 bytes  File Size  =     312774 *
*Baskets :       43 : Basket Size=     105984 bytes  Compression=   6.84     *
*............................................................................*
*Br   14 :tauPhi    : vector<float>                                          *
*Entries :   151369 : Total  Size=    2170091 bytes  File Size  =     312874 *
*Baskets :       43 : Basket Size=     105984 bytes  Compression=   6.84     *
*............................................................................*
*Br   15 :tauLooseSF : vector<float>                                         *
*Entries :   151369 : Total  Size=    2170301 bytes  File Size  =     283204 *
*Baskets :       43 : Basket Size=     105984 bytes  Compression=   7.56     *
*............................................................................*
*Br   16 :tauLooseSFStatUp : vector<float>                                   *
*Entries :   151369 : Total  Size=    2168625 bytes  File Size  =     283568 *
*Baskets :       43 : Basket Size=     106496 bytes  Compression=   7.55     *
*............................................................................*
*Br   17 :tauLooseSFStatDown : vector<float>                                 *
*Entries :   151369 : Total  Size=    2168723 bytes  File Size  =     283691 *
*Baskets :       43 : Basket Size=     106496 bytes  Compression=   7.55     *
*............................................................................*
*Br   18 :tauLooseSFSystUp : vector<float>                                   *
*Entries :   151369 : Total  Size=    2168625 bytes  File Size  =     283568 *
*Baskets :       43 : Basket Size=     106496 bytes  Compression=   7.55     *
*............................................................................*
*Br   19 :tauLooseSFSystDown : vector<float>                                 *
*Entries :   151369 : Total  Size=    2168723 bytes  File Size  =     283691 *
*Baskets :       43 : Basket Size=     106496 bytes  Compression=   7.55     *
*............................................................................*
*Br   20 :systWeights : vector<float>                                        *
*Entries :   151369 : Total  Size=    2738258 bytes  File Size  =     241533 *
*Baskets :       48 : Basket Size=     135680 bytes  Compression=  11.30     *
*............................................................................*
*Br   21 :btagSystWeights : vector<float>                                    *
*Entries :   151369 : Total  Size=    2738474 bytes  File Size  =     241755 *
*Baskets :       48 : Basket Size=     135680 bytes  Compression=  11.29     *
*............................................................................*
*Br   22 :NTReclusteringVars : NWcandidates/i:test/i:nJetsRecl/I             *
*Entries :   151369 : Total  Size=    2194116 bytes  File Size  =      10270 *
*Baskets :       19 : Basket Size=     555008 bytes  Compression= 177.04     *
*............................................................................*
*Br   23 :RTjets10SubJetIndeces : vector<vector<int> >                       *
*Entries :   151369 : Total  Size=    5010186 bytes  File Size  =     926806 *
*Baskets :       64 : Basket Size=     238080 bytes  Compression=   5.23     *
*............................................................................*
*Br   24 :RTjetPt   : vector<float>                                          *
*Entries :   151369 : Total  Size=    3472000 bytes  File Size  =    1733962 *
*Baskets :       52 : Basket Size=     164352 bytes  Compression=   1.92     *
*............................................................................*
*Br   25 :RTjetEta  : vector<float>                                          *
*Entries :   151369 : Total  Size=    3472058 bytes  File Size  =    1803220 *
*Baskets :       52 : Basket Size=     164352 bytes  Compression=   1.85     *
*............................................................................*
*Br   26 :RTjetPhi  : vector<float>                                          *
*Entries :   151369 : Total  Size=    3472058 bytes  File Size  =    1799591 *
*Baskets :       52 : Basket Size=     164352 bytes  Compression=   1.85     *
*............................................................................*
*Br   27 :RTjetM    : vector<float>                                          *
*Entries :   151369 : Total  Size=    3471902 bytes  File Size  =    1759581 *
*Baskets :       52 : Basket Size=     164352 bytes  Compression=   1.90     *
*............................................................................*
*Br   28 :ReclJetMass : vector<float>                                        *
*Entries :   151369 : Total  Size=    2152086 bytes  File Size  =     229454 *
*Baskets :       43 : Basket Size=     105472 bytes  Compression=   9.25     *
*............................................................................*
*Br   29 :ReclJetPt : vector<float>                                          *
*Entries :   151369 : Total  Size=    2151988 bytes  File Size  =     228630 *
*Baskets :       43 : Basket Size=     105472 bytes  Compression=   9.29     *
*............................................................................*
*Br   30 :ReclJetEta : vector<float>                                         *
*Entries :   151369 : Total  Size=    2152037 bytes  File Size  =     228566 *
*Baskets :       43 : Basket Size=     105472 bytes  Compression=   9.29     *
*............................................................................*
*Br   31 :ReclJetPhi : vector<float>                                         *
*Entries :   151369 : Total  Size=    2152037 bytes  File Size  =     228566 *
*Baskets :       43 : Basket Size=     105472 bytes  Compression=   9.29     *
*............................................................................*
*Br   32 :D2        : vector<float>                                          *
*Entries :   151369 : Total  Size=    2151645 bytes  File Size  =     228379 *
*Baskets :       43 : Basket Size=     105472 bytes  Compression=   9.29     *
*............................................................................*
*Br   33 :isWmedium : vector<bool>                                           *
*Entries :   151369 : Total  Size=    2151988 bytes  File Size  =     228630 *
*Baskets :       43 : Basket Size=     105472 bytes  Compression=   9.29     *
*............................................................................*
*Br   34 :isWtight  : vector<bool>                                           *
*Entries :   151369 : Total  Size=    2151939 bytes  File Size  =     228561 *
*Baskets :       43 : Basket Size=     105472 bytes  Compression=   9.29     *
*............................................................................*
*Br   35 :isZmedium : vector<bool>                                           *
*Entries :   151369 : Total  Size=    2151988 bytes  File Size  =     228630 *
*Baskets :       43 : Basket Size=     105472 bytes  Compression=   9.29     *
*............................................................................*
*Br   36 :isZtight  : vector<bool>                                           *
*Entries :   151369 : Total  Size=    2151939 bytes  File Size  =     228561 *
*Baskets :       43 : Basket Size=     105472 bytes  Compression=   9.29     *
*............................................................................*
*Br   37 :NTExtraVars : mettrack/F:mettrack_phi/F:mT2/F:mT2_noISR/F:Ap/F     *
*Entries :   151369 : Total  Size=    3101431 bytes  File Size  =    1965459 *
*Baskets :       32 : Basket Size=     555008 bytes  Compression=   1.54     *
*............................................................................*

the float variable is the met in NTVars and the vector is RTjetM
Is there a way to get all RTjetM Vectors independently of their length?
simply doing:

In [76]: tree
Out[76]: <ROOT.TTree object ("Data_SRAll") at 0x29928d0>

In [77]: data=[]        

In [78]: for event in tree:
   ....:     data.append(event.RTjetM)
   ....:     

fills data with the very same vector

In [79]: data[0]
Out[79]: <ROOT.vector<float> object at 0x2f32020>

In [80]: data[10]
Out[80]: <ROOT.vector<float> object at 0x2f32020>

but on the other hand:

In [84]: data=[]

In [85]: for event in tree:
    data.append(event.RTjetM[0])
   ....:     

In [86]: data[0]
Out[86]: 10.932619094848633

In [87]: data[10]
Out[87]: 13.312379837036133

seems to get the first entry of all the vectors. Why is that?

[quote]seems to get the first entry of all the vectors. Why is that?[/quote]because this is literally what the code asks for … i.e. In [85]: for event in tree: data.append(event.RTjetM[0])says: for each entry in the TTree, add the first ([0]) element of the vector named ‘event.RTjetM’ into the python array named ‘data’ …

[quote]How to read the correct values for xvalue, yvalue and float for every event stored in the tree?[/quote]I am not sure what ’ xvalue, yvalue and float ’ refer to in your TTree. Could you me more specific?

Thanks,
Philippe.

So is not ROOT.TVector in file, but is ROOT.std.vector(‘float’). Is anyway not understand why need copy in list? Iter on std.vector itself fine (and use size correct!), is example:for val in event.RTjetM: print val
Now if really only want all value, no structure:for event in tree: data += event.RTjetM
-Dom

thanks again for your replies.
Sorry I was not specific enough, my question is why

for event in tree:
   ....:     data.append(event.RTjetM)

gives a list filled with equal vectors?

Is same object over over over: object insert is pointer insert (Python reference), not object copy. Is TTree use same address and is PyROOT recycle address (because faster and identity important). Can copy if want: use module copy or copy constructor.

-Dom

I see, thanks again :slight_smile: