How I will apply it to check the result entry by entry. There are 1107 entries. Also I want to save the result of each entry just to compare the values.
Here is my script.
your code already does almost what you want, there’s just the tiny matter of the scope of variables. To paraphrase your code a bit:
if (someCondition) { // a new basic block starts here
int flag = 1; // create a new variable
SomeObject obj(flag); // create an object
doSomeWork(flag, obj); // work with your new variables
} // basic block ends: variables die here (destructors called), and become undefined again
So the rule in C++ is that variables on the stack go out of scope once the basic block is exited in which they’re defined. For your variables z1_mass, diff_12, and so on, the problem likely is that these variables go out of scope before you can use them. If you change your code like this, you may be more successful:
for (ULong64_t nEntry = 0; nEntry < 10000; ++nEntry) {
tree->GetEntry(nEntry);
TLorentzVector tmp1(VetoMu_px[0], VetoMu_py[0], VeloMu_pz[0], VetoMu_en[0]);
TLorentzVector tmp2(VetoMu_px[1], VetoMu_py[1], VeloMu_pz[1], VetoMu_en[1]);
TLorentzVector tmp3(VetoMu_px[2], VetoMu_py[2], VeloMu_pz[2], VetoMu_en[2]);
double z1_mass, z2_mass, z3_mass;
if (VetoMu_charge[m1]*VetoMu_charge[m2] < 0.) z1_mass = (tmp1 + tmp2).M();
if (VetoMu_charge[m2]*VetoMu_charge[m3] < 0.) z2_mass = (tmp2 + tmp3).M();
if (VetoMu_charge[m3]*VetoMu_charge[m1] < 0.) z3_mass = (tmp1 + tmp3).M();
// now you can work with z1_mass etc., e.g. print them
std::printf("z1_mass %g z2_mass %g z3_mass %g\n", z1_mass, z2_mass, z3_mass);
}
// the following line would not work, because we're outside the basic block in which
// z1_mass etc. are defined:
// std::printf("z1_mass %g z2_mass %g z3_mass %g\n", z1_mass, z2_mass, z3_mass); // illegal!
change the nEntry < 10000 to sometime useful (especially because nab wrote that the tree has 1107 entries), i.e. auto entries = tree->GetEntries(); for (Long64_t nEntry = 0; nEntry < entries; ++nEntry) {...} ROOT is using a signed value for GetEntry, so no need for U.
if any of your three ifs is false, you have no initialization for one of the z*_mass variables -> you will be printing an uninitialized value. Don’t do that, it is undefined behavior.
I started from the example above and reformulated it in terms of TDataFrame in order to show this new functionality of ROOT 6.10.
I attach the macro which I used to generate some fake input, also created with TDataFrame createInput.C.