Difficulty with Conditionals
Jun 1, 2015 at 5:17pm UTC
Hey!
I'm having a little difficulty with my code. Here it is:
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
bool user::Execute(SampleFormat& sample, const EventFormat& event)
{
// ***************************************************************************
// Example of analysis with generated particles
// Concerned samples : LHE/STDHEP/HEPMC
// ***************************************************************************
if (event.mc()!=0)
{
cout << "---------------NEW EVENT-------------------" << endl;
// Initial state
unsigned int n = event.mc()->particles().size();
for (unsigned int i=0;i<n;i++)
{
const MCParticleFormat* prt = &event.mc()->particles()[i];
if ( ((prt->pdgid()==13) && (prt->pdgid()==-13)) || ((prt->pdgid()==11) && (prt->pdgid()==-11) )
{
cout << "----------------------------------" << endl;
cout << "MC particle" << endl;
cout << "----------------------------------" << endl;
// display index particle
cout << "index=" << i+1;
// display kinematics information
cout << "px=" << prt->px()
<< " py=" << prt->py()
<< " pz=" << prt->pz()
<< " e=" << prt->e()
<< " m=" << prt->m()<< endl;
cout << "pt=" << prt->pt()
<< " eta=" << prt->eta()
<< " phi=" << prt->phi() << endl;
// Transverse missing energy (MET)
cout << "MET pt=" << event.mc()->MET().pt()
<< " phi=" << event.mc()->MET().phi() << endl;
cout << endl;
// Transverse missing hadronic energy (MHT)
cout << "MHT pt=" << event.mc()->MHT().pt()
<< " phi=" << event.mc()->MHT().phi() << endl;
cout << endl;
// Total transverse energy (TET)
cout << "TET=" << event.mc()->TET() << endl;
cout << endl;
// Total transverse hadronic energy (THT)
cout << "THT=" << event.mc()->THT() << endl;
cout << endl;
Histm->Fill(prt->m());
Histpt->Fill(prt->pt());
Histpx->Fill(prt->px());
Histpy->Fill(prt->py());
Histpz->Fill(prt->pz());
Histe->Fill(prt->e());
Histeta->Fill(prt->eta());
Histphi->Fill(prt->phi());
}
}
}
return true ;
}
But it gives no information about the particles with the pdgid mentioned above. I have 50k events and there are two types of event: one with two particles -13 and 13 and the other with the particles 11 and -11.
Instead if I do:
if ( (prt->pdgid()==13) || (prt->pdgid()==-13) || (prt->pdgid()==11) || (prt->pdgid()==-11) )
It does read the information (related only to the particle 13 I guess?).
Can anyone help?
Last edited on Jun 1, 2015 at 5:19pm UTC
Jun 1, 2015 at 6:49pm UTC
You seem to have a fundamental misunderstanding of Boolean logic. The expression
(prt->pdgid()==13) && (prt->pdgid()==-13))
is always false. There is no way for an immutable expression to evaluate to two different values. And if
prt->pdgid()
is a mutating function, we have some C++ language fundamentals to go over.
Jun 4, 2015 at 3:01pm UTC
Thank you for your reply, I got that cleared out. I have one more question:
If you do more than one if in the bool type of function, like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
for (....)
{
if (condition1)
{
}
if (condition2)
{
}
(...)
if (conditionN)
{
}
}
Does it read if(condition2) even if if(condition1) is true?
Last edited on Jun 4, 2015 at 3:01pm UTC
Jun 4, 2015 at 3:21pm UTC
yes it does unless you use the else keyword.
Jun 4, 2015 at 3:46pm UTC
Thank you.
Jun 5, 2015 at 1:45pm UTC
Now I don't have an error in the code but it just doesn't fill the histogram I want to build.
Here's 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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
bool user::Execute(SampleFormat& sample, const EventFormat& event)
{
// ***************************************************************************
// Example of analysis with generated particles
// Concerned samples : LHE/STDHEP/HEPMC
// ***************************************************************************
if (event.mc()!=0)
{
cout << "---------------NEW EVENT-------------------" << endl;
// Initial state
unsigned int n = event.mc()->particles().size();
for (unsigned int i=0;i<n;i++)
{
const MCParticleFormat* prt = &event.mc()->particles()[i];
if ( (prt->pdgid()== 13) )
{
cout << "----------------------------------" << endl;
cout << "MC particle" << endl;
cout << "----------------------------------" << endl;
// display index particle
cout << "index=" << i+1;
// display kinematics information
cout << "px=" << prt->px()
<< " py=" << prt->py()
<< " pz=" << prt->pz()
<< " e=" << prt->e()
<< " m=" << prt->m()<< endl;
cout << "pt=" << prt->pt()
<< " eta=" << prt->eta()
<< " phi=" << prt->phi() << endl;
/* // display particle mother id
if (part.mother1()==0)
{
cout << "particle with no mother." << endl;
}
else if (part.mother2()==0 || part.mother1()==part.mother2())
{
const MCParticleFormat* mother = part.mother1();
cout << "particle coming from the decay of "
<< mother->pdgid() << "." << endl;
}
else
{
const MCParticleFormat* mother1 = part.mother1();
const MCParticleFormat* mother2 = part.mother2();
cout << "particle coming from interaction between "
<< mother1->pdgid() << " and " << mother2->pdgid()
<< "." << endl;
}
*/
// Transverse missing energy (MET)
cout << "MET pt=" << event.mc()->MET().pt()
<< " phi=" << event.mc()->MET().phi() << endl;
cout << endl;
// Transverse missing hadronic energy (MHT)
cout << "MHT pt=" << event.mc()->MHT().pt()
<< " phi=" << event.mc()->MHT().phi() << endl;
cout << endl;
// Total transverse energy (TET)
cout << "TET=" << event.mc()->TET() << endl;
cout << endl;
// Total transverse hadronic energy (THT)
cout << "THT=" << event.mc()->THT() << endl;
cout << endl;
if ( prt->mother2()==0 || prt->mother1()==prt->mother2() )
{
const MCParticleFormat* mother = prt->mother1();
if ( mother->pdgid() == 24 ) { Histpt->Fill(prt->pt()); }
}
}
}
}
return true ;
}
When I open my file.root I don't have any entries and it must be related to the following code I wrote:
1 2 3 4 5 6
if ( prt->mother2()==0 || prt->mother1()==prt->mother2() )
{
const MCParticleFormat* mother = prt->mother1();
if ( mother->pdgid() == 24 ) { Histpt->Fill(prt->pt()); }
}
Can anyone help?
Last edited on Jun 5, 2015 at 1:46pm UTC
Jun 5, 2015 at 5:35pm UTC
Problem solved!
Topic archived. No new replies allowed.