Subscript errors when attempting to index branch objects

HEP_practice.C (6.7 KB)

I am trying to follow this worksheet 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:

/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



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.

HEP_practiceV2.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?

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);

Hi,

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

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

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);
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

HEP_practiceV2.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:

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?

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