Problem: want to access two different statements

Hi all. Here it is what I have

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
bool (...)
for (unsigned int i=0;i<event.rec()->electrons().size();i++)
      {
      const RecLeptonFormat* elec = &event.rec()->electrons()[i];
      const RecLeptonFormat* elecc = &event.rec()->electrons()[0];

      cout << "pt=" << elec->pt() << endl;
      cout << "pt=" << elecc->pt() << endl;

    HistptELEC->Fill(elec->pt());
    }

    for (unsigned int i=0;i<event.rec()->muons().size();i++)
    {
      const RecLeptonFormat* mu = &event.rec()->muons()[i]; 
      const RecLeptonFormat* muu = &event.rec()->muons()[0]; 

      cout << "pt=" << mu->pt() << endl;
      cout << "pt=" << muu->pt() << endl;

    HistptMU->Fill(mu->pt());
    }
   }
return true;
}


The problem is that I want to include the following, and don't know how because the scope in the two different loops is different (obvious):

1
2
3
4
5
6
7
8
9
10
11
12
13
      if ( elecc->pt() > muu->pt())  
      {
      Histpt->Fill(elecc->pt());
      cout << "particle pdgid=" << elecc->pdgid() << endl;
      cout << "pt=" << elecc->pt() << endl;
      }

      else if ( muu->pt() > elecc->pt()) 
      {
      Histpt->Fill(muu->pt());
      cout << "particle pdgid=" << muu->pdgid() << endl;
      cout << "pt=" << muu->pt() << endl;
      }


Can anyone help?
Last edited on
I don't understand your question. Where do you want to include the second code block? What are the two different statements you want to access? And from where do you want to access them?

Your first code block seems to be a fragment only?
Yes, the first one is only a fraction of the code.

First I find the maximum value of both arrays, and to do that you just need to acess the first value of the array:

1
2
3
4
5
const RecLeptonFormat* elecc = &event.rec()->electrons()[0];

and

const RecLeptonFormat* muu = &event.rec()->muons()[0]; 


Then I want to fill the histogram with the highest value of the two maximums:

1
2
3
4
5
6
7
8
9
10
11
12
13
if ( elecc->pt() > muu->pt())  
      {
      Histpt->Fill(elecc->pt());
      cout << "particle pdgid=" << elecc->pdgid() << endl;
      cout << "pt=" << elecc->pt() << endl;
      }

      else if ( muu->pt() > elecc->pt()) 
      {
      Histpt->Fill(muu->pt());
      cout << "particle pdgid=" << muu->pdgid() << endl;
      cout << "pt=" << muu->pt() << endl;
      }


But for doing that I need access to the pointers muu->pdgid() and elecc->pdgid(), and I don't know how to access them because they are in two different for conditions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
for (unsigned int i=0;i<event.rec()->electrons().size();i++)
      {
      const RecLeptonFormat* elec = &event.rec()->electrons()[i];
      const RecLeptonFormat* elecc = &event.rec()->electrons()[0];

      cout << "pt=" << elec->pt() << endl;
      cout << "pt=" << elecc->pt() << endl;
    }

and

    for (unsigned int i=0;i<event.rec()->muons().size();i++)
    {
      const RecLeptonFormat* mu = &event.rec()->muons()[i]; 
      const RecLeptonFormat* muu = &event.rec()->muons()[0]; 

      cout << "pt=" << mu->pt() << endl;
      cout << "pt=" << muu->pt() << endl;

    }


The code structure is what I wrote in the first block:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
bool (...)
for (unsigned int i=0;i<event.rec()->electrons().size();i++)
      {
      const RecLeptonFormat* elec = &event.rec()->electrons()[i];
      const RecLeptonFormat* elecc = &event.rec()->electrons()[0];

      cout << "pt=" << elec->pt() << endl;
      cout << "pt=" << elecc->pt() << endl;

    }

    for (unsigned int i=0;i<event.rec()->muons().size();i++)
    {
      const RecLeptonFormat* mu = &event.rec()->muons()[i]; 
      const RecLeptonFormat* muu = &event.rec()->muons()[0]; 

      cout << "pt=" << mu->pt() << endl;
      cout << "pt=" << muu->pt() << endl;
    }
   }
return 


And I want to run it for the condition ( elecc->pt() > muu->pt() ) or the condition ( muu->pt() > elecc->pt() ) but I just don't know to access to both pointers (that point to the maximum value of the array) elecc->pt() and muu->pt() in the same statement.

Maybe it would help if you declare your variables outside the for-loops? Like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const RecLeptonFormat* elec = nullptr;
const RecLeptonFormat* elecc = nullptr;

for (unsigned int i = 0; i < event.rec()->electrons().size(); i++)
{
    elec = &event.rec()->electrons()[i];
    elecc = &event.rec()->electrons()[0];

    cout << "pt=" << elec->pt() << endl;
    cout << "pt=" << elecc->pt() << endl;
}

// ...

if ((elecc != nullptr) && (muu != nullptr))
{
    if (elecc->pt() > muu->pt())  
    {
        Histpt->Fill(elecc->pt());
        cout << "particle pdgid=" << elecc->pdgid() << endl;
        cout << "pt=" << elecc->pt() << endl;
    }
    else if (muu->pt() > elecc->pt()) 
    {
        Histpt->Fill(muu->pt());
        cout << "particle pdgid=" << muu->pdgid() << endl;
        cout << "pt=" << muu->pt() << endl;
    }
}
Yes, it helped!

Thank you!
Registered users can post here. Sign in or register to post.