# Subscript errors when attempting to index branch objects

**URL:** <https://root-forum.cern.ch/t/subscript-errors-when-attempting-to-index-branch-objects/41611>\
**Category:** Newbie\
**Created:** [September 26, 2020, 8:51pm UTC](https://root-forum.cern.ch/t/subscript-errors-when-attempting-to-index-branch-objects/41611 "2020-09-26T20:51:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cepheides](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/bcef8e/32.png) [@Cepheides](https://root-forum.cern.ch/u/Cepheides)\
**Post date:** [September 26, 2020, 8:51pm UTC](https://root-forum.cern.ch/t/subscript-errors-when-attempting-to-index-branch-objects/41611/1 "2020-09-26T20:51:44Z")

</div>

[HEP\_practice.C](https://root-forum.cern.ch/uploads/short-url/isf9ZcgwEJ9DHFQqSX9WhML2epw.C) (6.7 KB)

I am trying to follow [this worksheet](https://mhance.scipp.ucsc.edu/analysisintro.php) from Hance and recreate the invariant mass plot below from his root file. I have attached my code and the root file can be downloaded from the website.

Anyway, when I try to run my code, I get the following error:

```auto
/home/mhance/HEP_practice.C:105:42: error: subscripted value is not an array, pointer, or vector
                cout<<"i: "<<i<<" "<<"nLep: "<<nLeps35[i]<<endl;
                                                ~~~~~~~ ^~
/home/mhance/HEP_practice.C:106:13: error: subscripted value is not an array, pointer, or vector
                if(nLeps35[i] == 2){
                   ~~~~~~~ ^~
/home/mhance/HEP_practice.C:107:30: error: subscripted value is not an array, pointer, or vector
                        for(int j = 0; j < nLeps35[i]; j++){
                                           ~~~~~~~ ^~
/home/mhance/HEP_practice.C:109:15: error: subscripted value is not an array, pointer, or vector
                                if(flavLep[i] == 2){
                                   ~~~~~~~ ^~
/home/mhance/HEP_practice.C:110:33: error: subscripted value is not an array, pointer, or vector
                                        pos_muon.SetPtEtaPhiM(pTLep[i],e...
                                                              ~~~~~ ^~
/home/mhance/HEP_practice.C:110:43: error: subscripted value is not an array, pointer, or vector
  ...pos_muon.SetPtEtaPhiM(pTLep[i],etaLep[i],phiLep[i],0.105);
                                    ~~~~~~ ^~
/home/mhance/HEP_practice.C:110:53: error: subscripted value is not an array, pointer, or vector
  ...pos_muon.SetPtEtaPhiM(pTLep[i],etaLep[i],phiLep[i],0.105);
                                              ~~~~~~ ^~
/home/mhance/HEP_practice.C:119:15: error: subscripted value is not an array, pointer, or vector
                                if(flavLep[i] == -2){
                                   ~~~~~~~ ^~
/home/mhance/HEP_practice.C:120:33: error: subscripted value is not an array, pointer, or vector
                                        neg_muon.SetPtEtaPhiM(pTLep[i],e...
                                                              ~~~~~ ^~
/home/mhance/HEP_practice.C:120:43: error: subscripted value is not an array, pointer, or vector
  ...neg_muon.SetPtEtaPhiM(pTLep[i],etaLep[i],phiLep[i],0.105);
                                    ~~~~~~ ^~
/home/mhance/HEP_practice.C:120:53: error: subscripted value is not an array, pointer, or vector
  ...neg_muon.SetPtEtaPhiM(pTLep[i],etaLep[i],phiLep[i],0.105);

```

From my understanding, these indices work once you set the branches. What am I missing here? Thanks.

_ROOT Version:_ 6.22/00  
_Platform:_ Ubuntu 18.04

* * *

* * *

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [September 27, 2020, 7:08am UTC](https://root-forum.cern.ch/t/subscript-errors-when-attempting-to-index-branch-objects/41611/2 "2020-09-27T07:08:45Z")

</div>

The error message is clear: nLeps35, flavLep, etc. are not arrays, pointers or vectors. Your code declares them as simple (integer/float) numbers, so you need to know what they should be (scalars or otherwise) and declare/use them accordingly.

---

<div class="post-metadata">

**Author:** ![Cepheides](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/bcef8e/32.png) [@Cepheides](https://root-forum.cern.ch/u/Cepheides)\
**Post date:** [September 27, 2020, 3:23pm UTC](https://root-forum.cern.ch/t/subscript-errors-when-attempting-to-index-branch-objects/41611/3 "2020-09-27T15:23:57Z")

</div>

[HEP\_practiceV2.C](https://root-forum.cern.ch/uploads/short-url/vPk8t3onCpeKJfBapCGuwepi0Vy.C) (6.7 KB)

Hi, so I went ahead and made changes of the variables so that they are now arrays in stead of simple int/float variables. I also removed indices for nLeps35 since it is not an array variable. However, I get these `error: no matching member function for call to 'Fill'` errors. Should I try to convert the arrays from float to double as the error suggests or approach this differently?

```auto
In file included from input_line_13:1:
/home/nick/PHY496/mhance/HEP_practiceV2.C:98:6: error: no matching member function for call to 'SetBranchAddress'
        t1->SetBranchAddress("nLeps35",nLeps35);
        ~~~~^~~~~~~~~~~~~~~~
/home/nick/root/include/TTree.h:578:28: note: candidate function not viable: no known conversion from 'int' to 'void *' for 2nd argument; take the address of the argument with &
   virtual Int_t SetBranchAddress(const char *bname,void *add, TBranch **ptr = 0);
                           ^
/home/nick/root/include/TTree.h:581:29: note: candidate template ignored: could not match 'T **' against 'int'
   template <class T> Int_t SetBranchAddress(const char *bname, T **add, TBranch** ptr = 0) {
                            ^
/home/nick/root/include/TTree.h:590:29: note: candidate template ignored: could not match 'T *' against 'int'
   template <class T> Int_t SetBranchAddress(const char *bname, T *add, TBranch **ptr = 0) {
                            ^
/home/nick/root/include/TTree.h:579:28: note: candidate function not viable: requires 5 arguments, but 2 were provided
   virtual Int_t SetBranchAddress(const char *bname,void *add, TClass *realClass, EDataType datatype, Bool_t isptr);
                           ^
/home/nick/root/include/TTree.h:580:28: note: candidate function not viable: requires 6 arguments, but 2 were provided
   virtual Int_t SetBranchAddress(const char *bname,void *add, TBranch **ptr, TClass *realClass, EDataType datatype, Bool_t isptr);
                           ^
In file included from input_line_13:1:
/home/nick/PHY496/mhance/HEP_practiceV2.C:112:17: error: no matching member function for call to 'Fill'
                                        histpTLep->Fill(pTLep);
                                        ~~~~~~~~~~~^~~~
/home/nick/root/include/TH1.h:215:21: note: candidate function not viable: no known conversion from 'float [3]' to 'Double_t' (aka 'double') for 1st argument
   virtual Int_t Fill(Double_t x);
                    ^
/home/nick/root/include/TH1.h:216:21: note: candidate function not viable: requires 2 arguments, but 1 was provided
   virtual Int_t Fill(Double_t x, Double_t w);
                    ^
/home/nick/root/include/TH1.h:217:21: note: candidate function not viable: requires 2 arguments, but 1 was provided
   virtual Int_t Fill(const char *name, Double_t w);
                    ^
In file included from input_line_13:1:
/home/nick/PHY496/mhance/HEP_practiceV2.C:113:18: error: no matching member function for call to 'Fill'
                                        histetaLep->Fill(etaLep);
                                        ~~~~~~~~~~~~^~~~
/home/nick/root/include/TH1.h:215:21: note: candidate function not viable: no known conversion from 'float [3]' to 'Double_t' (aka 'double') for 1st argument
   virtual Int_t Fill(Double_t x);
                    ^
/home/nick/root/include/TH1.h:216:21: note: candidate function not viable: requires 2 arguments, but 1 was provided
   virtual Int_t Fill(Double_t x, Double_t w);
                    ^
/home/nick/root/include/TH1.h:217:21: note: candidate function not viable: requires 2 arguments, but 1 was provided
   virtual Int_t Fill(const char *name, Double_t w);
                    ^
In file included from input_line_13:1:
/home/nick/PHY496/mhance/HEP_practiceV2.C:114:18: error: no matching member function for call to 'Fill'
                                        histphiLep->Fill(phiLep);
                                        ~~~~~~~~~~~~^~~~
/home/nick/root/include/TH1.h:215:21: note: candidate function not viable: no known conversion from 'float [3]' to 'Double_t' (aka 'double') for 1st argument
   virtual Int_t Fill(Double_t x);
                    ^
/home/nick/root/include/TH1.h:216:21: note: candidate function not viable: requires 2 arguments, but 1 was provided
   virtual Int_t Fill(Double_t x, Double_t w);
                    ^
/home/nick/root/include/TH1.h:217:21: note: candidate function not viable: requires 2 arguments, but 1 was provided
   virtual Int_t Fill(const char *name, Double_t w);
                    ^
In file included from input_line_13:1:
/home/nick/PHY496/mhance/HEP_practiceV2.C:116:20: error: no matching member function for call to 'Fill'
                                        histpTLepPos->Fill(pTLep);
                                        ~~~~~~~~~~~~~~^~~~
/home/nick/root/include/TH1.h:215:21: note: candidate function not viable: no known conversion from 'float [3]' to 'Double_t' (aka 'double') for 1st argument
   virtual Int_t Fill(Double_t x);
                    ^
/home/nick/root/include/TH1.h:216:21: note: candidate function not viable: requires 2 arguments, but 1 was provided
   virtual Int_t Fill(Double_t x, Double_t w);
                    ^
/home/nick/root/include/TH1.h:217:21: note: candidate function not viable: requires 2 arguments, but 1 was provided
   virtual Int_t Fill(const char *name, Double_t w);
                    ^
In file included from input_line_13:1:
/home/nick/PHY496/mhance/HEP_practiceV2.C:117:21: error: no matching member function for call to 'Fill'
                                        histetaLepPos->Fill(etaLep);
                                        ~~~~~~~~~~~~~~~^~~~
/home/nick/root/include/TH1.h:215:21: note: candidate function not viable: no known conversion from 'float [3]' to 'Double_t' (aka 'double') for 1st argument
   virtual Int_t Fill(Double_t x);
                    ^
/home/nick/root/include/TH1.h:216:21: note: candidate function not viable: requires 2 arguments, but 1 was provided
   virtual Int_t Fill(Double_t x, Double_t w);
                    ^
/home/nick/root/include/TH1.h:217:21: note: candidate function not viable: requires 2 arguments, but 1 was provided
   virtual Int_t Fill(const char *name, Double_t w);
                    ^
In file included from input_line_13:1:
/home/nick/PHY496/mhance/HEP_practiceV2.C:118:21: error: no matching member function for call to 'Fill'
                                        histphiLepPos->Fill(phiLep);
                                        ~~~~~~~~~~~~~~~^~~~
/home/nick/root/include/TH1.h:215:21: note: candidate function not viable: no known conversion from 'float [3]' to 'Double_t' (aka 'double') for 1st argument
   virtual Int_t Fill(Double_t x);
                    ^
/home/nick/root/include/TH1.h:216:21: note: candidate function not viable: requires 2 arguments, but 1 was provided
   virtual Int_t Fill(Double_t x, Double_t w);
                    ^
/home/nick/root/include/TH1.h:217:21: note: candidate function not viable: requires 2 arguments, but 1 was provided
   virtual Int_t Fill(const char *name, Double_t w);
                    ^
In file included from input_line_13:1:
/home/nick/PHY496/mhance/HEP_practiceV2.C:122:17: error: no matching member function for call to 'Fill'
                                        histpTLep->Fill(pTLep);
                                        ~~~~~~~~~~~^~~~
/home/nick/root/include/TH1.h:215:21: note: candidate function not viable: no known conversion from 'float [3]' to 'Double_t' (aka 'double') for 1st argument
   virtual Int_t Fill(Double_t x);
                    ^
/home/nick/root/include/TH1.h:216:21: note: candidate function not viable: requires 2 arguments, but 1 was provided
   virtual Int_t Fill(Double_t x, Double_t w);
                    ^
/home/nick/root/include/TH1.h:217:21: note: candidate function not viable: requires 2 arguments, but 1 was provided
   virtual Int_t Fill(const char *name, Double_t w);
                    ^
In file included from input_line_13:1:
/home/nick/PHY496/mhance/HEP_practiceV2.C:123:18: error: no matching member function for call to 'Fill'
                                        histetaLep->Fill(etaLep);
                                        ~~~~~~~~~~~~^~~~
/home/nick/root/include/TH1.h:215:21: note: candidate function not viable: no known conversion from 'float [3]' to 'Double_t' (aka 'double') for 1st argument
   virtual Int_t Fill(Double_t x);
                    ^
/home/nick/root/include/TH1.h:216:21: note: candidate function not viable: requires 2 arguments, but 1 was provided
   virtual Int_t Fill(Double_t x, Double_t w);
                    ^
/home/nick/root/include/TH1.h:217:21: note: candidate function not viable: requires 2 arguments, but 1 was provided
   virtual Int_t Fill(const char *name, Double_t w);
                    ^
In file included from input_line_13:1:
/home/nick/PHY496/mhance/HEP_practiceV2.C:124:18: error: no matching member function for call to 'Fill'
                                        histphiLep->Fill(phiLep);
                                        ~~~~~~~~~~~~^~~~
/home/nick/root/include/TH1.h:215:21: note: candidate function not viable: no known conversion from 'float [3]' to 'Double_t' (aka 'double') for 1st argument
   virtual Int_t Fill(Double_t x);
                    ^
/home/nick/root/include/TH1.h:216:21: note: candidate function not viable: requires 2 arguments, but 1 was provided
   virtual Int_t Fill(Double_t x, Double_t w);
                    ^
/home/nick/root/include/TH1.h:217:21: note: candidate function not viable: requires 2 arguments, but 1 was provided
   virtual Int_t Fill(const char *name, Double_t w);
                    ^
In file included from input_line_13:1:
/home/nick/PHY496/mhance/HEP_practiceV2.C:126:20: error: no matching member function for call to 'Fill'
                                        histpTLepNeg->Fill(pTLep);
                                        ~~~~~~~~~~~~~~^~~~
/home/nick/root/include/TH1.h:215:21: note: candidate function not viable: no known conversion from 'float [3]' to 'Double_t' (aka 'double') for 1st argument
   virtual Int_t Fill(Double_t x);
                    ^
/home/nick/root/include/TH1.h:216:21: note: candidate function not viable: requires 2 arguments, but 1 was provided
   virtual Int_t Fill(Double_t x, Double_t w);
                    ^
/home/nick/root/include/TH1.h:217:21: note: candidate function not viable: requires 2 arguments, but 1 was provided
   virtual Int_t Fill(const char *name, Double_t w);
                    ^
In file included from input_line_13:1:
/home/nick/PHY496/mhance/HEP_practiceV2.C:127:21: error: no matching member function for call to 'Fill'
                                        histetaLepNeg->Fill(etaLep);
                                        ~~~~~~~~~~~~~~~^~~~
/home/nick/root/include/TH1.h:215:21: note: candidate function not viable: no known conversion from 'float [3]' to 'Double_t' (aka 'double') for 1st argument
   virtual Int_t Fill(Double_t x);
                    ^
/home/nick/root/include/TH1.h:216:21: note: candidate function not viable: requires 2 arguments, but 1 was provided
   virtual Int_t Fill(Double_t x, Double_t w);
                    ^
/home/nick/root/include/TH1.h:217:21: note: candidate function not viable: requires 2 arguments, but 1 was provided
   virtual Int_t Fill(const char *name, Double_t w);
                    ^
In file included from input_line_13:1:
/home/nick/PHY496/mhance/HEP_practiceV2.C:128:21: error: no matching member function for call to 'Fill'
                                        histphiLepNeg->Fill(phiLep);
                                        ~~~~~~~~~~~~~~~^~~~
/home/nick/root/include/TH1.h:215:21: note: candidate function not viable: no known conversion from 'float [3]' to 'Double_t' (aka 'double') for 1st argument
   virtual Int_t Fill(Double_t x);
                    ^
/home/nick/root/include/TH1.h:216:21: note: candidate function not viable: requires 2 arguments, but 1 was provided
   virtual Int_t Fill(Double_t x, Double_t w);
                    ^
/home/nick/root/include/TH1.h:217:21: note: candidate function not viable: requires 2 arguments, but 1 was provided
   virtual Int_t Fill(const char *name, Double_t w);

```

---

<div class="post-metadata">

**Author:** ![FoxWise](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/foxwise/32/37588_2.png) [@FoxWise](https://root-forum.cern.ch/u/FoxWise)\
**Post date:** [September 27, 2020, 4:55pm UTC](https://root-forum.cern.ch/t/subscript-errors-when-attempting-to-index-branch-objects/41611/4 "2020-09-27T16:55:11Z")

</div>

Hi,

When calling `	t1->SetBranchAddress("nLeps35",nLeps35);`

Error tells you that it wants pointer to address as a second argument, but not the value.

```auto
no known conversion from 'int' to 'void *' for 2nd argument;
take the address of the argument with &
virtual Int_t SetBranchAddress(const char *bname, void *add);

```

```auto
int var = 5;
cout<<var<<endl; // It prints 5 -- value of var
cout<<&var<<endl; // It prints something like 0x7f3944e77010
// the address name where the 5 is stored. This you need to pass as the 2nd argument

```

---

<div class="post-metadata">

**Author:** ![Cepheides](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/bcef8e/32.png) [@Cepheides](https://root-forum.cern.ch/u/Cepheides)\
**Post date:** [September 28, 2020, 12:25am UTC](https://root-forum.cern.ch/t/subscript-errors-when-attempting-to-index-branch-objects/41611/5 "2020-09-28T00:25:40Z")

</div>

[HEP\_practiceV2.C](https://root-forum.cern.ch/uploads/short-url/9hEqpD3QTAOoTuI5lNqlsjArF7K.C) (6.8 KB)  
Thanks FoxWise.

I made several adjustments to my code to resolve the errors that were outputted. Here is what is in the terminal when my code runs now:

```auto
Making histrograms...
Warning in <TCanvas::Constructor>: Deleting canvas with same name: c1
Warning in <TCanvas::Constructor>: Deleting canvas with same name: c2
Warning in <TCanvas::Constructor>: Deleting canvas with same name: c3
(int) 0

```

However, when I check the histograms, there are no entries at all even though I am filling the histograms. What am I missing here?

---

<div class="post-metadata">

**Author:** ![FoxWise](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/foxwise/32/37588_2.png) [@FoxWise](https://root-forum.cern.ch/u/FoxWise)\
**Post date:** [September 28, 2020, 1:07pm UTC](https://root-forum.cern.ch/t/subscript-errors-when-attempting-to-index-branch-objects/41611/6 "2020-09-28T13:07:23Z")

</div>

The output tells you that everything is fine. And if histograms are empty is logical mistake but not programming language one.

1. I am not sure what this line `t1->GetEvent(j);` supposed to do. I would remove it. You have `t1->GetEntry(i);`few lines above which fills your variables with branch values for this event.

2. You definitely use wrong indices (i where you should be using j). One loops over events. Another over the leptons

3. Be sure to loop over all collection of events, but not only 100 first. As your leptons appear very rare with your cuts.

cheers
