Initializing Muon Vectors: How to

I am trying to reconstruct two Z bosons.
I am using this tutorial as an example. https://root.cern.ch/doc/master/df103__NanoAODHiggsAnalysis_8C.html
However, I am still not familiar in using ROOT data frame and I am writing it in terms of what I am familiar with.

What I do not understand is how can I define i1, i2, and idx that is shown in the ROOT tutorial?

In my tree, I have the BranchMuon and the variables in this branch are called Muon.PT, Muon.Eta, Muon.Phi and Muon.Charge, and Muon.Mass.

My attempt to follow the tutorial is constructing a muon vector and defining the variables (which generates errors but I am still learning how to work with it):

> #ifdef __CLING__
> 
> R__LOAD_LIBRARY(libDelphes)
> 
> #include "classes/DelphesClasses.h"
> 
> #include "external/ExRootAnalysis/ExRootTreeReader.h"
> 
> #include "external/ExRootAnalysis/ExRootResult.h"
> 
> #else
> 
> class ExRootTreeReader;
> 
> class ExRootResult;
> 
> #endif
> 
> #include <vector>
> 
> template<typename T>
> 
> void CollectionFilter(const TClonesArray& inColl ,vector<T*>& outColl, Double_t ptMin=30, Double_t etaMax=2.5)
> 
> {
> 
> const TObject *object;
> 
> for (Int_t i = 0; i < inColl.GetEntriesFast(); i++)
> 
> {
> 
> object = inColl.At(i);
> 
> const T *t = static_cast<const T*>(object);
> 
> if(t->P4().Pt() < ptMin) continue;
> 
> if(TMath::Abs(t->P4().Eta()) > etaMax) continue;
> 
> outColl.push_back(t);
> 
> }
> 
> }
> 
> //-------------------------------------Looking at Leptons -----------------------------------------
> 
> void selectMuon(const char *inputFile)
> 
> {
> 
>   gSystem->Load("libDelphes");
> 
>   // Create chain of root trees
> 
>   TChain chain("Delphes");
> 
>   chain.Add(inputFile);
> 
>   // Create object of class ExRootTreeReader
> 
>   ExRootTreeReader *treeReader = new ExRootTreeReader(&chain);
> 
>   Long64_t numberOfEntries = treeReader->GetEntries();
> 
>   // Get pointers to branches used in this analysis
> 
>   TClonesArray *branchMuon = treeReader->UseBranch("Muon");
> 
>   
> 
>   // Book histograms
> 
>  
> 
>   TH1F *histZMass = new TH1F("mass", "M_{inv}(Z[1]); M_inv (GeV/c^2); Events", 50, 0.0, 1500);
> 
>   TH1F *histDiMuonMass = new TH1F("mass", "M_{inv}(Z[3]Z[5]); M_inv (GeV/c^2); Events", 50, 0.0, 1500);
> 
>   
> 
>   // Define variables
> 
>   
> 
>   float_t PT, Eta, Phi, Mass, Charge;  
> 
>  
> 
>   // Initializing the vectors
> 
>   //vector<const Muon*>  *muons = new vector<const Muon*>();;
> 
>   MuonVector muon(PT, Eta, Phi, Mass, Charge);
> 
>      
> 
>   // Loop over all events
> 
>   for(Int_t entry = 0; entry < numberOfEntries; ++entry)
> 
>   {
> 
>     // Load selected branches with data from specified event
> 
>     treeReader->ReadEntry(entry);
> 
>              
> 
>     // Select leptons with pT > 10 GeV and |eta| < 2.5
> 
>     
> 
>     CollectionFilter(*branchMuon, *muons, 10.0, 2.5);
> 
>    
> 
>       muon1 = (*muon)[0];
> 
>       muon2 = (*muon)[1];
> 
>       if(muon1->Charge != muon2->Charge)
> 
>       {
> 
>         ROOT::Math::PtEtaPhiMVector m1(PT[0], Eta[0], Phi[0], Mass[0]);
> 
>         ROOT::Math:PtEtaPhiMVector m2(PT[1], Eta[1], Phi[1], Mass[1]);
> 
>         const mass = (m1 + m2).M();
> 
>       }
> 
>       
> 
>       TLorentzVector dimuon = (muon1 + muon2);
> 
>       histDiMuonMass->Fill(mass);   
> 
>     
> 
>   }
> 
>     
>       
> 
> //  - - - - - - - - - - - - - - Drawing the Histograms - - - - - - - - - - - - - - - - - - - - - -   
> 
>    
> 
>   } // end of event for loop
> 
>   histDiMuonMass->Draw();
> 
>   }

How can I define the two muon vectors? How can I write idx, i1 and i2?

EDIT: Below is my current progress:

I am trying to reconstruct two Z bosons.
I am using this tutorial as an example. https://root.cern.ch/doc/master/df103__NanoAODHiggsAnalysis_8C.html
However, I am still not familiar in using ROOT data frame and I am writing it in terms of what I am familiar with.

What I do not understand is how is i1, i2, and idx that is shown in the ROOT tutorial defined?

In my tree, I have the BranchMuon and the variables in this branch are called Muon.PT, Muon.Eta, Muon.Phi and Muon.Charge, and Muon.Mass.

My attempt to follow the tutorial is constructing a muon vector and defining the variables (which generates errors but I am still learning how to work with it):

#ifdef __CLING__
R__LOAD_LIBRARY(libDelphes)
#include "classes/DelphesClasses.h"
#include "external/ExRootAnalysis/ExRootTreeReader.h"
#include "external/ExRootAnalysis/ExRootResult.h"
#else
class ExRootTreeReader;
class ExRootResult;
#endif
#include <vector>

#include "ROOT/RDataFrame.hxx"
#include "ROOT/RVec.hxx"
#include "ROOT/RDF/RInterface.hxx"
#include "TCanvas.h"
#include "TH1D.h"
#include "TLatex.h"
#include "TLegend.h"
#include "Math/Vector4Dfwd.h"
#include "TStyle.h"
using namespace ROOT::VecOps;
using RNode = ROOT::RDF::RNode;
using rvec_f = const RVec<float> &;
using rvec_i = const RVec<int> &;
const auto z_mass = 91.2;

template<typename T>
void CollectionFilter(const TClonesArray& inColl ,vector<T*>& outColl, Double_t ptMin=30, Double_t etaMax=2.5)
{

const TObject *object;

for (Int_t i = 0; i < inColl.GetEntriesFast(); i++)
{

object = inColl.At(i);
const T *t = static_cast<const T*>(object);

if(t->P4().Pt() < ptMin) continue;
if(TMath::Abs(t->P4().Eta()) > etaMax) continue;

outColl.push_back(t);

}
}



void selectMuon(const char *inputFile)
{
  gSystem->Load("libDelphes");

  // Create chain of root trees
  TChain chain("Delphes");
  chain.Add(inputFile);

  // Create object of class ExRootTreeReader
  ExRootTreeReader *treeReader = new ExRootTreeReader(&chain);
  Long64_t numberOfEntries = treeReader->GetEntries();

  // Get pointers to branches used in this analysis
  TClonesArray *branchMuon = treeReader->UseBranch("Muon");
  
  // Book histograms
 
  TH1F *histZMass = new TH1F("mass", "M_{inv}(Z[1]); M_inv (GeV/c^2); Events", 50, 0.0, 1500);
  TH1F *histDiMuonMass = new TH1F("mass", "M_{inv}(Z[3]Z[5]); M_inv (GeV/c^2); Events", 50, 0.0, 1500);
  

  // Define variables
  
  float_t PT, Eta, Phi, Mass, Charge;  
 
  // Initializing the vectors
  //vector<const Muon*>  *muons = new vector<const Muon*>();;
  //MuonVector muon(PT, Eta, Phi, Mass, Charge);
  RVec<RVec<size_t>> reco_zz_to_4l(rvec_f PT, rvec_f Eta, rvec_f Phi, rvec_f Mass, rvec_i Charge);
  
    RVec<RVec<size_t>> idx(2);
    idx[0].reserve(2); idx[1].reserve(2);
    auto idx_cmb = Combinations(PT, 2);
    for (size_t i = 0; i < idx_cmb[0].size(); i++)
    {
      const auto i1 = idx_cmb[0][i];
      const auto i2 = idx_cmb[1][i];

      
      if(Charge[i1] != Charge[i2]
      {

      

        ROOT::Math::PtEtaPhiMVector m1(PT[0], Eta[0], Phi[0], Mass[0]);
        ROOT::Math::PtEtaPhiMVector m2(PT[1], Eta[1], Phi[1], Mass[1]);
        const auto mass = (m1 + m2).M();
     }
    }

      histDiMuonMass->Fill(mass);   
    
  
    

   
   // end of event for loop
  histDiMuonMass->Draw();
  
  }

The errors that I am getting is below.

In file included from input_line_188:1:
/mnt/c/1/MG5_aMC_v2_6_6/Delphes/examples/selectMuon.C:89:16: error: subscripted value is not an array, pointer, or vector
      if(Charge[i1] != Charge[i2]
         ~~~~~~^~~
/mnt/c/1/MG5_aMC_v2_6_6/Delphes/examples/selectMuon.C:89:30: error: subscripted value is not an array, pointer, or vector
      if(Charge[i1] != Charge[i2]
                       ~~~~~~^~~
/mnt/c/1/MG5_aMC_v2_6_6/Delphes/examples/selectMuon.C:100:28: error: use of undeclared identifier 'mass'
      histDiMuonMass->Fill(mass);

How can I fix it?
Thank you.
___
Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


Hi,
Charge is declared as a float but you are using it as if it was an array, that’s what the compilation error is about.

Hope this helps,
Enrico

Thank you. That makes sense!
I just don’t understand now why I get this error:

In file included from input_line_205:1:
/mnt/c/1/MG5_aMC_v2_6_6/Delphes/examples/selectMuon.C:82:33: error: use of undeclared identifier ‘PT’
auto idx_cmb = Combinations(PT, 2);
^
/mnt/c/1/MG5_aMC_v2_6_6/Delphes/examples/selectMuon.C:89:10: error: use of undeclared identifier ‘Charge’
if(Charge[i1] != Charge[i2]
^
/mnt/c/1/MG5_aMC_v2_6_6/Delphes/examples/selectMuon.C:89:24: error: use of undeclared identifier ‘Charge’
if(Charge[i1] != Charge[i2]
^
/mnt/c/1/MG5_aMC_v2_6_6/Delphes/examples/selectMuon.C:100:28: error: use of undeclared identifier ‘mass’
histDiMuonMass->Fill(mass);

Haven’t I defined PT and Charge to be variables in my branches? It seems like the ROOT tutorial defines pt differently; but I don’t understand why or how.
Thanks!

TBrowser
Here is how my Delphes;1 Tree looks

Thank you for sending the documentation. I may have found something useful. In my above code, I am trying to define the branches PT, Eta, Phi, and Charge. (I don’ have a Mass Branch though).
I first attempt to extract the branches from my tree (called Delphes that is under the .root file created by MadGraph) by adding:

/*
#ifdef CLING
R__LOAD_LIBRARY(libDelphes)
#include “classes/DelphesClasses.h”
#include “external/ExRootAnalysis/ExRootTreeReader.h”
#include “external/ExRootAnalysis/ExRootResult.h”
#else
class ExRootTreeReader;
class ExRootResult;
#endif
*/
#include

#include “ROOT/RDataFrame.hxx”
#include “ROOT/RVec.hxx”
#include “ROOT/RDF/RInterface.hxx”
#include “TCanvas.h”
#include “TH1D.h”
#include “TLatex.h”
#include “TLegend.h”
#include “Math/Vector4Dfwd.h”
#include “TStyle.h”
using namespace ROOT::VecOps;
using RNode = ROOT::RDF::RNode;
using rvec_f = const RVec &;
using rvec_i = const RVec &;
const auto z_mass = 91.2;

template
void CollectionFilter(const TClonesArray& inColl ,vector<T*>& outColl, Double_t ptMin=30, Double_t etaMax=2.5)
{

const TObject *object;

for (Int_t i = 0; i < inColl.GetEntriesFast(); i++)
{

object = inColl.At(i);
const T t = static_cast<const T>(object);

if(t->P4().Pt() < ptMin) continue;
if(TMath::Abs(t->P4().Eta()) > etaMax) continue;

outColl.push_back(t);

}
}

//-------------------------------------Looking at Leptons -----------------------------------------

void selectMuon()
{
//gSystem->Load(“libDelphes”);

// Create chain of root trees
//TChain chain(“Delphes”);
//chain.Add(inputFile);

// Create object of class ExRootTreeReader
//ExRootTreeReader *treeReader = new ExRootTreeReader(&chain);
//Long64_t numberOfEntries = treeReader->GetEntries();

// Get pointers to branches used in this analysis
//TClonesArray *branchMuon = treeReader->UseBranch(“Muon”);

// Book histograms

TH1F *histZMass = new TH1F(“mass”, “M_{inv}(Z[1]); M_inv (GeV/c^2); Events”, 50, 0.0, 1500);
TH1F *histDiMuonMass = new TH1F(“mass”, “M_{inv}(Z[3]Z[5]); M_inv (GeV/c^2); Events”, 50, 0.0, 1500);

// Define variables

// TPaveText PT = new TPaveText(0.00518135,0.810811,0.507772,0.989189,“br”);
ROOT::RDataFrame d(“Delphes”, “tag_1_delphes_events.root”, {“Muon”});
auto rdf = d.Define(“muon_pt”, “Muon.PT”);
// Initializing the vectors
//vector<const Muon
> muons = new vector<const Muon>();;
//MuonVector muon(PT, Eta, Phi, Mass, Charge);
RVec<RVec<size_t>> reco_zz_to_4l(rvec_f (Muon.PT), rvec_f (Muon.Eta), rvec_f (Muon.Phi), rvec_f (Muon.Mass), rvec_i (Muon.Charge));

RVec<RVec<size_t>> idx(2);
idx[0].reserve(2); idx[1].reserve(2);
auto idx_cmb = Combinations((Muon.PT), 2);
for (size_t i = 0; i < idx_cmb[0].size(); i++)
{
  const auto i1 = idx_cmb[0][i];
  const auto i2 = idx_cmb[1][i];
  
  if((Muon.Charge)[i1] != (Muon.Charge)[i2])
  {

    ROOT::Math::PtEtaPhiMVector m1((Muon.PT)[i1], (Muon.Eta)[i1], (Muon.Phi)[i1], (Muon.Mass)[i1]);
    ROOT::Math::PtEtaPhiMVector m2((Muon.PT)[i2], (Muon.Eta)[i2], (Muon.Phi)[i2], (Muon.Mass)[i2]);
    const auto mass = (m1 + m2).M();
 }
}

  //histDiMuonMass->Fill(mass);   
    auto h10 = d.Histo1D("Muon.PT");

// - - - - - - - - - - - - - - Drawing the Histograms - - - - - - - - - - - - - - - - - - - - - -

// end of event for loop
//histDiMuonMass->Draw();
h10->Draw();
}

However, I am still getting the following error:

root [11] .x selectMuon.C
In file included from input_line_59:1:
/mnt/c/1/MG5_aMC_v2_6_6/trisignal/Events/run_01/selectMuon.C:81:44: error: use of undeclared identifier ‘Muon’
RVec<RVec<size_t>> reco_zz_to_4l(rvec_f (Muon.PT), rvec_f (Muon.Eta), rvec_f (Muon.Phi), rvec_…
^
/mnt/c/1/MG5_aMC_v2_6_6/trisignal/Events/run_01/selectMuon.C:81:62: error: use of undeclared identifier ‘Muon’
RVec<RVec<size_t>> reco_zz_to_4l(rvec_f (Muon.PT), rvec_f (Muon.Eta), rvec_f (Muon.Phi), rvec_…
^
/mnt/c/1/MG5_aMC_v2_6_6/trisignal/Events/run_01/selectMuon.C:81:81: error: use of undeclared identifier ‘Muon’
RVec<RVec<size_t>> reco_zz_to_4l(rvec_f (Muon.PT), rvec_f (Muon.Eta), rvec_f (Muon.Phi), rvec_…
^
/mnt/c/1/MG5_aMC_v2_6_6/trisignal/Events/run_01/selectMuon.C:81:100: error: use of undeclared identifier ‘Muon’
…reco_zz_to_4l(rvec_f (Muon.PT), rvec_f (Muon.Eta), rvec_f (Muon.Phi), rvec_f (Muon.Mass), rvec_…
^
/mnt/c/1/MG5_aMC_v2_6_6/trisignal/Events/run_01/selectMuon.C:81:120: error: use of undeclared identifier ‘Muon’
…(Muon.PT), rvec_f (Muon.Eta), rvec_f (Muon.Phi), rvec_f (Muon.Mass), rvec_i (Muon.Charge));
^
/mnt/c/1/MG5_aMC_v2_6_6/trisignal/Events/run_01/selectMuon.C:85:34: error: use of undeclared identifier ‘Muon’
auto idx_cmb = Combinations((Muon.PT), 2);
^
/mnt/c/1/MG5_aMC_v2_6_6/trisignal/Events/run_01/selectMuon.C:91:11: error: use of undeclared identifier ‘Muon’
if((Muon.Charge)[i1] != (Muon.Charge)[i2])
^
/mnt/c/1/MG5_aMC_v2_6_6/trisignal/Events/run_01/selectMuon.C:91:32: error: use of undeclared identifier ‘Muon’
if((Muon.Charge)[i1] != (Muon.Charge)[i2])
^
/mnt/c/1/MG5_aMC_v2_6_6/trisignal/Events/run_01/selectMuon.C:94:41: error: use of undeclared identifier ‘Muon’
ROOT::Math::PtEtaPhiMVector m1((Muon.PT)[i1], (Muon.Eta)[i1], (Muon.Phi)[i1], (Muon.Mass)[i1]);
^
/mnt/c/1/MG5_aMC_v2_6_6/trisignal/Events/run_01/selectMuon.C:94:56: error: use of undeclared identifier ‘Muon’
ROOT::Math::PtEtaPhiMVector m1((Muon.PT)[i1], (Muon.Eta)[i1], (Muon.Phi)[i1], (Muon.Mass)[i1]);
^
/mnt/c/1/MG5_aMC_v2_6_6/trisignal/Events/run_01/selectMuon.C:94:72: error: use of undeclared identifier ‘Muon’
ROOT::Math::PtEtaPhiMVector m1((Muon.PT)[i1], (Muon.Eta)[i1], (Muon.Phi)[i1], (Muon.Mass)[i1]);
^
/mnt/c/1/MG5_aMC_v2_6_6/trisignal/Events/run_01/selectMuon.C:94:88: error: use of undeclared identifier ‘Muon’
ROOT::Math::PtEtaPhiMVector m1((Muon.PT)[i1], (Muon.Eta)[i1], (Muon.Phi)[i1], (Muon.Mass)[i1]);
^
/mnt/c/1/MG5_aMC_v2_6_6/trisignal/Events/run_01/selectMuon.C:95:41: error: use of undeclared identifier ‘Muon’
ROOT::Math::PtEtaPhiMVector m2((Muon.PT)[i2], (Muon.Eta)[i2], (Muon.Phi)[i2], (Muon.Mass)[i2]);
^
/mnt/c/1/MG5_aMC_v2_6_6/trisignal/Events/run_01/selectMuon.C:95:56: error: use of undeclared identifier ‘Muon’
ROOT::Math::PtEtaPhiMVector m2((Muon.PT)[i2], (Muon.Eta)[i2], (Muon.Phi)[i2], (Muon.Mass)[i2]);
^
/mnt/c/1/MG5_aMC_v2_6_6/trisignal/Events/run_01/selectMuon.C:95:72: error: use of undeclared identifier ‘Muon’
ROOT::Math::PtEtaPhiMVector m2((Muon.PT)[i2], (Muon.Eta)[i2], (Muon.Phi)[i2], (Muon.Mass)[i2]);
^
/mnt/c/1/MG5_aMC_v2_6_6/trisignal/Events/run_01/selectMuon.C:95:88: error: use of undeclared identifier ‘Muon’
ROOT::Math::PtEtaPhiMVector m2((Muon.PT)[i2], (Muon.Eta)[i2], (Muon.Phi)[i2], (Muon.Mass)[i2]);

Why aren’t the branches getting recognized? How can I fix it?

probably need to use strings as parameter rather than undefined variable names.

@pcanal Do you have an example of how I can use branches as strings? Thank you

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.