Problem in "if" statements

Dear developers,

I am use three iff statements in a script. But when I run this script it will fill my histograms for a full loop. All histograms become equal. Kindly help me to resolve this issue.
I am attaching a part of my script.

        j=0;
      for(unsigned int mu1=0; mu1 <nMu; ++mu1){
        jj=0;
        for(unsigned int mu2=0; mu2 <nMu; ++mu2) {

          if( (mu2 > mu1) &&  (VetoMu_charge[mu1]*VetoMu_charge[mu2]) < 0. ) {
 tmp1.SetPxPyPzE(VetoMu_px[mu1], VetoMu_py[mu1], VetoMu_pz[mu1], VetoMu_en[mu1]);
 tmp2.SetPxPyPzE(VetoMu_px[mu2], VetoMu_py[mu2], VetoMu_pz[mu2], VetoMu_en[mu2]);
for(int mu3=0; mu3 < 3; mu3++)  {

if(mu3==0){
 h1->Fill((tmp1 + tmp2).M());
}
else if(mu3==1){
 h2->Fill((tmp1 + tmp2).M());
}
else(mu3==2){
 h3->Fill((tmp1 + tmp2).M());
}
}

cheers,
Nab

Try to intend … doing it i found several “}” and one “if” missing.

   j=0;
   for(unsigned int mu1=0; mu1 <nMu; ++mu1){
      jj=0;
      for(unsigned int mu2=0; mu2 <nMu; ++mu2) {

         if( (mu2 > mu1) &&  (VetoMu_charge[mu1]*VetoMu_charge[mu2]) < 0. ) {
            tmp1.SetPxPyPzE(VetoMu_px[mu1], VetoMu_py[mu1], VetoMu_pz[mu1], VetoMu_en[mu1]);
            tmp2.SetPxPyPzE(VetoMu_px[mu2], VetoMu_py[mu2], VetoMu_pz[mu2], VetoMu_en[mu2]);
            for(int mu3=0; mu3 < 3; mu3++)  {

               if (mu3==0) {
                  h1->Fill((tmp1 + tmp2).M());
               } else if(mu3==1) {
                  h2->Fill((tmp1 + tmp2).M());
               } else if (mu3==2) { // "if" was missing
                  h3->Fill((tmp1 + tmp2).M());
               /// missing }
            /// missing }
         /// missing }
      }
   }

given his loops is running [0, 3), this else if (mu3==2) was equivalent to original else { (oh, he had something else where - else () { …). and the missing ‘}’ I guess explained by “a part of my script”.

to the OP - your code is doing what you’ve asked for. and please, ask basic C/C++ questions elsewhere, this is a ROOT specific forum. your code is similar to this:

int a, b, c;
for (int i = 0; i < 3; ++i)
{
   if (i == 0) a = 1;
   else if (i == 1) b = 1;
   else c = 1;
}

Is it surprising, a, b, and c now contain 1?

Dear couet,

Is there is some other way to do it?

Dear tpochep,

How can I contact c++ forum

I have sent you a part of macro. The missing braces are there at the end. All I need is how to use the if statements

For instance:
https://cal-linux.com/tutorials/conditionals.html

etc …

Any book on C++ basics explains how if-statement works.

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