# How to find and save a smallest number among three numbers

Dear developers,

I have developed a macro which is used to create three double numbers and then I want to find out the smallest among three in each entry. But this script is not working. Kindly correct my mistake.

``````"  for (int i=0;i<1000;i++)   {

TLorentzVector tmp1, tmp2, tmp3;

t->GetEntry(i);

// for(int m=0; m<VetoMu;m++)   {

if(VetoMu<2)continue;

int m1=0, m2=1, m3=2;

if(m1==0) tmp1.SetPxPyPzE(VetoMu_px[m1], VetoMu_py[m1], VetoMu_pz[m1], VetoMu_en[m1]);
if(m2==1) tmp2.SetPxPyPzE(VetoMu_px[m2], VetoMu_py[m2], VetoMu_pz[m2], VetoMu_en[m2]);
if(m3==2) tmp3.SetPxPyPzE(VetoMu_px[m3], VetoMu_py[m3], VetoMu_pz[m3], VetoMu_en[m3]);

if (VetoMu_charge[m1]*VetoMu_charge[m2] < 0.) {

double a = (tmp1 + tmp2).M();
}
if (VetoMu_charge[m2]*VetoMu_charge[m3] < 0.) {

double b = (tmp2 + tmp3).M();
}
if (VetoMu_charge[m3]*VetoMu_charge[m1] < 0.) {

double c = (tmp1 + tmp3).M();
}

/* Taking input */

cin >> a >> b >> c;
/* If a is smaller than b and c */

if (a < b && a < c) {
cout << "smallest number is " << a;

/* If b is smaller than a and c */

}
else if (b < a && b < c) {
cout << "smallest number is " << b;

}
else if {
cout << "smallest number is " << c;
}

return 0;
}  "
``````

Cheers,
Nab

Hi,

this link might be useful https://stackoverflow.com/questions/9424173/c-find-the-smallest-amongst-3-numbers

Cheers,
D

Hi,

I tried all these tutorial examples but I donot explain which is the smallest in a, b and c.
I used these lines
" d = a<b? (a<c?a:c) : (b<c?b:c);
cout << "the smallest of the numbers is: " <<d<<endl; "
Could you please explain what does it mean? I want to say according to these lines which is the smallest number either a,b or c

Thyanks

Hi,

alternatively, you can modify the working example I delivered to you here How to check conditions entry by entry in a nested loop with this one, which prints the minimum z per entry in your tree:

``````void analyse() {

ROOT::Experimental::TDataFrame d("mytree","myfile.root");
using floats = std::vector<float>;
using ints = std::vector<int>;

auto retMinZ = [](floats& x, floats& y, floats& z, floats& t, ints& c){
std::vector<TLorentzVector> tmp = {{x, y, z, t},
{x, y, z, t},
{x, y, z, t}};
return std::min({(tmp + tmp).M(), (tmp + tmp).M(), (tmp + tmp).M()});

};

d.Define("minZmass",retMinZ,{"VetoMu_px","VetoMu_py","VetoMu_pz","VetoMu_en","VetoMu_charge"})
.Foreach([](double m){std::cout << "Min z mass is " << m << std::endl;}, {"minZmass"});

}
``````

I want to add some lines in my code which just save a single number which is smallest of all. That may be “a” sometime may be “b” sometime or may be “c” depending on the situation or the number of entries.

Hi,

do you want to save in a new TTree in a new TFile a dataset containing the smallest z mass event by event?
If yes,

``````void analyse() {

ROOT::Experimental::TDataFrame d("mytree","myfile.root");
using floats = std::vector<float>;
using ints = std::vector<int>;

auto retMinZ = [](floats& x, floats& y, floats& z, floats& t, ints& c){
std::vector<TLorentzVector> tmp = {{x, y, z, t},
{x, y, z, t},
{x, y, z, t}};
return std::min({(tmp + tmp).M(), (tmp + tmp).M(), (tmp + tmp).M()});

};

d.Define("minZmass",retMinZ,{"VetoMu_px","VetoMu_py","VetoMu_pz","VetoMu_en","VetoMu_charge"})
.Snapshot("mytree","myNewFile.root", "minZmass");
}
``````

if you mean the smallest number of all, just replace the last two lines of code with

``````auto minOfAll = Define("minZmass",retMinZ {"VetoMu_px","VetoMu_py","VetoMu_pz","VetoMu_en","VetoMu_charge"})
.Min("minZmass");
std::cout << "Min value of all: " << *minOfAll << std::endl;
``````

Hi,

I have added these lines in my script
" d = a<b? (a<c?a:c) : (b<c?b:c);
cout << "the smallest of the numbers is: " <<d<<endl;
cout<<"a : "<< a<<endl;
cout<<"b : "<< b<<endl;
cout<<"c : "<< c<<endl;
"
and the output is like

" the smallest of the numbers is: 40.6711
a : 85.3864
b : 40.6711
c : 42.4108
the smallest of the numbers is: 42.4108
a : 90.4736
b : 67.2951
c : 42.4108
the smallest of the numbers is: 32.0982
a : 32.0982
b : 67.2951
c : 87.6047
the smallest of the numbers is: 37.6647
a : 89.5612
b : 37.6647
c : 87.6047
the smallest of the numbers is: 41.5752
a : 92.0955
b : 41.5752
c : 87.6047
the smallest of the numbers is: 41.5752
a : 182.359
b : 41.5752
c : 93.1043
"
The problem is the smallest value differs for each entry. How would I know which is the smallest occurring number (a,b,c) in average. One way is to check each entry manually and then note it down which is off course not possible because entries may exceed thousand or above. I want to develop an algorithm which just runs on it and tell me which value is smallest on average. And then I will save it in a histogram or in a tree.

I think this has been answered above.

I think you need sth like this:

``````int counta = 0, countb = 0, countc=0;

for(...)//Here a for loop over the events...
{

if(a==b && b==c)
{
cout << "The three numbers are equal";
counta++;
countb++;
countc++;
}
else if(a==b)
{
if(c<a)
{
cout << "c is the smallest";
countc++;
}
else
{
cout << "a and b are the smallest";
counta++;
countb++;
}
}
else if(a==c)
{
if(b<a)
{
cout << "b is the smallest";
countb++;
}
else
{
cout << "a and c are the smallest";
counta++;
countc++;
}
}
else if(b==c)
{
if(a<b)
{
cout << "a is the smallest";
counta++;
}
else
{
cout << "b and c are the smallest";
countb++;
countc++;
}
}
else if(a<b)
{
if(a<c)
{
cout << "a is the smallest";
counta++;
}
else
{
cout << "c is the smallest";
countc++;
}
}
else
{
if(b<c)
{
cout << "b is the smallest";
countb++;
}
else
{
cout << "c is the smallest";
countc++;
}
}

}//Finish for loop

cout << "Ranking of smallest: " << counta << " " << countb << " " << countc << endl;
``````

``````d = a<b? (a<c?a:c) : (b<c?b:c);
``````

See http://www.cplusplus.com/forum/articles/14631/
This is the same as doing:

``````
if(a<b)
{
if(a<c) d=a;
else d=c;
}
else
{
if(b<c) d=b;
else d=c;
}
``````

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