Mutiple or conditions

Hi,

I have three conditions and want to go to the top of loop if any of these conditions is true. So, I have written the following code, but it only works if I write if (cond1 && cond2 && cond3) continue; but this is not what I want to do.

Thank you. The code is below

bool cond1 = (ev->lep_id[0]*ev->lep_id[1]*ev->lep_id[2]*ev->lep_id[3] != 11*11*11*11);
bool cond2 = (ev->lep_id[0]*ev->lep_id[1]*ev->lep_id[2]*ev->lep_id[3] != 11*11*13*13);
bool cond3 = (ev->lep_id[0]*ev->lep_id[1]*ev->lep_id[2]*ev->lep_id[3] != 13*13*13*13);


if (cond1 || cond2 || cond3) continue;
Please post more of the loop. We can only guess what the code says.

but it only works if I write if (cond1 && cond2 && cond3) continue; but this is not what I want to do.


I'm not sure what you mean by this statement. Do you mean that (cond1 && cond2 && cond3) gives you the the intended behavior of the program, but you don't understand why? Do you mean that (cond1 && cond2 && cond3) works the way you expect, but it's not the intended behavior of the program?

Please give us a description of what you expect the program to do.

Edit:

Oh. Duh. Each of the conditions is true if the product of the values is not equal to some value. Of course, if cond1 is false (i.e., == 11^4), cond2 and cond3 will both be true. So, cond1 || cond2 || cond3 will always be true.
Last edited on
bool cond1 = (ev->lep_id[0]*ev->lep_id[1]*ev->lep_id[2]*ev->lep_id[3] != 11*11*11*11);
bool cond2 = (ev->lep_id[0]*ev->lep_id[1]*ev->lep_id[2]*ev->lep_id[3] != 11*11*13*13);
bool cond3 = (ev->lep_id[0]*ev->lep_id[1]*ev->lep_id[2]*ev->lep_id[3] != 13*13*13*13);

Here, the cond1 says that if it is not 2 electrons and 2 positrons
cond2 says that if it is not electron positron, and muon and anti-muon
cond3 says if it is not 2 muon and 2 anti-muons

So, I want to write if it is not ( 2 electrons and 2 positrons) or ( 2 muon and 2 anti-muons) or ( electron positron, and muon and anti-muon) continue.

I want to continue if any of the condition is true. So, I wrote

if (cond1 || cond2 || cond3) continue; but it does not give any results ( no events selected). I hope i made it clear now.
Last edited on
What doug4 is saying is that you are doing the following (simplified):
1
2
3
4
cond1 = some_value != 1;
cond2 = some_value != 2;

if (cond1 || cond2) continue;

Do you see the problem? If we expand this, we get
if (some_value != 1 || some_value != 2) continue;
• If some_value == 1, then cond1 will be false, and cond2 will be true.
• If some_value == 2, then cond1 will be true, and cond2 will be false.
• If some_value == anything else, then both conditions will be true.
In all cases, the OR'd result will be if (true).

Your conditions might have some physics meaning that you explained, but as far as pure logic goes, the union of all 3 of your conditions will always be true, so you need to re-think your logic.

Or, as doug4 said, you might have meant &&, or maybe you meant to use == instead of !=.

Edit: Perhaps you should be comparing your lep_id's individually?
1
2
3
4
bool cond1 = ev->lep_id[0] == 11 &&
             ev->lep_id[1] == 11 &&
             ev->lep_id[2] == 11 &&
             ev->lep_id[3] == 11;


Do an electron and positron both have the same lep_id?

cond2 says that if it is not electron positron, and muon and anti-muon
In English, chaining together a bunch of "nots" and "ands" can be very ambiguous and confusing because it's not clear if the "not" in your sentence is being distributed. Perhaps it might help to write your condition as:
!(electron || positron) && !(muon || anti_muon)
(I'm just guessing what you mean, I don't know physics)

_________________________________________________

Okay, final suggestion for now:
Your first condition is "not 2 electron and positrons".
If that means "!(2 electrons) && !(2 positions)", then you need to have some functionality to determine the number of electrons/positions.
For example:
1
2
3
4
5
6
7
8
9
int num_electrons(int lep_id[])
{
    return ...;
}

int num_positrons(int lep_id[])
{
   return ...;
}


Then, you can write your condition as:
(num_electrons(ev->lep_id) != 2 && num_positrons(ev->lep_id) != 2);

Or perhaps you meant "the sum of positrons and electrons should not be 2", then you would do:
(num_electrons(ev->lep_id) + num_positrons(ev->lep_id) != 2);

Maybe that will help clarify your code. I don't know. ...leaping leptons!
Last edited on
rather than break it up by conditions, do the math once and check the 3 constants may be cleaner?

product = ev->lep_id[0]*ev->lep_id[1]*ev->lep_id[2]*ev->lep_id[3];
if(product != 14641 && ...
@OP The trouble with your contortionate logic is you, and therefore we, don't have a clear picture of what your arithmetic is supposed to do other than feed some sort of a -ve based filtering process. On your say-so it is supposed to determine how many particles of 4 different types spit out the end for further processing (maybe, but outside the scope of this current trauma)

@jonnins approach is eminently clear and this can be taken a step further.

It appears to me you'd be better off cascading the decisions and deciding on the basis of when a condition is satisfied rather than when it is not - let the cascade do that. (Perhaps even use a switch structure.)

int electrons{0};
    int positrons{0};
    int muons{0};
    int anti_muons{0};
    
    int product = ev->lep_id[0]*ev->lep_id[1]*ev->lep_id[2]*ev->lep_id[3];
    
    if (productfactor == 14641)
    {
        electrons = 2; // If it's not these 2 then say what it is!
        positrons = 2;
    }
    else if (product == 20449)
    {
        // ONLY UPDATE RELEVANT PARTICLES   
    }
    else if (product == 28561)
    {
        // ONLY UPDATE RELEVANT PARTICLES   
    }
    else
    {
       default values;
    }
    
    ... do_something()
Last edited on
@s6kagupt, your code worked with three && ... because that is what you are trying to do.

Agree that @Jonnin's approach is much simpler. Note that he uses &&, not ||

But surely there must be a better way of identifying what leptons you have?
Yes, it works with three &&. But this is not what i intended to do.

The problem is solved using == condition and I modified the code accordingly. Thanks for the discussion. It was very fruitful.

bool cond1 = (ev->lep_id[0]*ev->lep_id[1]*ev->lep_id[2]*ev->lep_id[3] == 11*11*11*11);
bool cond2 = (ev->lep_id[0]*ev->lep_id[1]*ev->lep_id[2]*ev->lep_id[3] == 11*11*13*13);
bool cond3 = (ev->lep_id[0]*ev->lep_id[1]*ev->lep_id[2]*ev->lep_id[3] == 13*13*13*13);

bool cond4 = ev->nlep == 4;
bool cond5 = ev->njet == 0;
bool cond6 = ev->etmis < 50;


if ((cond1 || cond2 || cond3) && (cond4 && cond5 && cond6))
Mmm, but you have computed exactly the same product three times
ev->lep_id[0]*ev->lep_id[1]*ev->lep_id[2]*ev->lep_id[3]
Bit wasteful.
Yes, the code is a mixture of CERN ROOT programming and C++ programming.
Yes, the code is a mixture of CERN ROOT programming and C++ programming.


I think you missed the point that @lastchance (and, earlier, @jonnin) made. You are multiplying the same 4 numbers together 3 times--once for each of cond1, cond2 and cond3. Unless CERN ROOT programming requires inefficient, redundant code, this is a calculation that can be made 1 time and reused when determining cond1...3.

1
2
3
4
int product = ev->lep_id[0]*ev->lep_id[1]*ev->lep_id[2]*ev->lep_id[3];
bool cond1 = (product == 11*11*11*11);
bool cond2 = (product == 11*11*13*13);
bool cond3 = (product == 13*13*13*13);


I might even go a little further and add some additional variables to make the code more clear:

1
2
3
4
5
6
7
int elec_pos = 11 * 11;
int mu_antimu = 13 * 13;
int product = ev->lep_id[0]*ev->lep_id[1]*ev->lep_id[2]*ev->lep_id[3];

bool cond1 = (product == elec_pos * elec_pos);
bool cond2 = (product == elec_pos * mu_antimu);
bool cond3 = (product == mu_antimu * mu_antimu);
The increase in clarity/readability of that last code excerpt vastly outweighs any theoretical minor performance increase, imo. So it's a win-win (easier to read, and might save a few cycles).
Last edited on
What happens if
ev->lep_id[0] == ev->lep_id[1] == 121
and
ev->lep_id[2] == ev->lep_id[3] == 1

?



a mixture of CERN ROOT programming and

Now we know why the European taxpayer is paying so much into CERN!
Now we know why the European taxpayer is paying so much into CERN!
You can download CERN ROOT for free. It's a shocker - just have a look at their schematic dependency diagrams.

But in fairness to ROOT, it can't be blamed for bad programming practice and an obvious lack of a plan.

CERN must pay on lines of random code however long it takes them to fuck around with such a simple exercise.
someone still pays by line of code? Sign me up, there are so many ways to milk that stuff. Ive got a neural network generator that makes like 5 pages of code to multiply 2 numbers.
Ive got a neural network generator that makes like 5 pages of code to multiply 2 numbers.
Your HP 15C no doubt?
? no, that is just a calculator.
neural nets have a couple of pages of weights in big arrays for all the sum of products etc. You can make them do idiotic simple stuff, like a boolean condition, and it will take several pages of weights, multiplications, lookup tables, and such to get the result. Its the old sledgehammer vs fly thing... they were not meant to do simple things, but they *can*.

the core of a lot of NN code is a table of constant* weights that are multiplied by the numerical form of the inputs, added up, checked against a threshold to figure out which branch to do next, repeat across several branches (sort of like leaf to root of a tree, start at leaves with inputs working toward root for the 'answer') until the input has been resolved to an output (which can be a vector of outputs). Even the thresholding is overcooked -- it uses a sigmoid or similar function rather than an if statement.

*constant when running inputs to get an answer. they are modified on the back end slowly as it 'learns' (user input reinforces when it gets the answer right or wrong, or its fed data with known answers so it can adjust to improve its accuracy).

in short, for simple problems, they do about 1000x the work, kind of like using an xml parser for hello world :P
Last edited on
@jonnin, I too had a HP15C among others ... I was only joking.

(As you might know there is a free IOS HP15C app which is as good as the real one)
Ah, I thought you were referring to my 2 page calculator code which would could be embedded in a pay by line program to turn a little math into a lot of bloat.
I don't know if I still have my 15.
Registered users can post here. Sign in or register to post.