Impossible to read a tree in a loop

Dear experts,
I try try to read the tree [1] without success in root6. When I scan I have reasonable values, but when I try to print the tree values in a for loop by using my code made with the root MakeClass [2], it always print the same values. After you compile (make) and run (./do.exe) main.cc code will create a txt file in which I want to print the evt pt, eta… Do you know what is wrong? Please let me know if you need more information.
Regards

[1]
calpas.web.cern.ch/calpas/www/imp.root

[2]
calpas.web.cern.ch/calpas/www/h2taus.h
calpas.web.cern.ch/calpas/www/h2taus.C
calpas.web.cern.ch/calpas/www/main.cc
calpas.web.cern.ch/calpas/www/Makefile
calpas.web.cern.ch/calpas/www/myfunc.cc
calpas.web.cern.ch/calpas/www/variableint.txt
calpas.web.cern.ch/calpas/www/variable.txt

The link are not working for me. Could you attach the file to this forum?

Dear pcanal,

working now.

Regards

[1]
calpas.web.cern.ch/calpas/imp.root

[2]
calpas.web.cern.ch/calpas/h2taus.h
calpas.web.cern.ch/calpas/h2taus.C
calpas.web.cern.ch/calpas/main.cc
calpas.web.cern.ch/calpas/Makefile
calpas.web.cern.ch/calpas/myfunc.cc
calpas.web.cern.ch/calpas/variableint.txt
calpas.web.cern.ch/calpas/variable.txt

Dear experts,
any idea?
Regards

Looking at it.

Please note already some mistakes:

Error in <TTree::SetBranchAddress>: unknown branch -> NUP
Error in <TTree::SetBranchAddress>: unknown branch -> abs_mt_tot
Error in <TTree::SetBranchAddress>: unknown branch -> byLooseCombinedIsolationDeltaBetaCorr3Hits_1
Error in <TTree::SetBranchAddress>: unknown branch -> byLooseCombinedIsolationDeltaBetaCorr3Hits_2
Error in <TTree::SetBranchAddress>: unknown branch -> byMediumCombinedIsolationDeltaBetaCorr3Hits_1
Error in <TTree::SetBranchAddress>: unknown branch -> byMediumCombinedIsolationDeltaBetaCorr3Hits_2
Error in <TTree::SetBranchAddress>: unknown branch -> byTightCombinedIsolationDeltaBetaCorr3Hits_1
Error in <TTree::SetBranchAddress>: unknown branch -> byTightCombinedIsolationDeltaBetaCorr3Hits_2
Error in <TTree::SetBranchAddress>: unknown branch -> id_m_highpt_1
Error in <TTree::SetBranchAddress>: unknown branch -> id_m_highpt_2
Error in <TTree::SetBranchAddress>: unknown branch -> id_m_loose_1
Error in <TTree::SetBranchAddress>: unknown branch -> id_m_loose_2
Error in <TTree::SetBranchAddress>: unknown branch -> id_m_medium_1
Error in <TTree::SetBranchAddress>: unknown branch -> id_m_medium_2
Error in <TTree::SetBranchAddress>: unknown branch -> id_m_tight_1
Error in <TTree::SetBranchAddress>: unknown branch -> id_m_tight_2
Error in <TTree::SetBranchAddress>: unknown branch -> id_m_tightnovtx_1
Error in <TTree::SetBranchAddress>: unknown branch -> id_m_tightnovtx_2
Error in <TTree::SetBranchAddress>: unknown branch -> jpfid_1
Error in <TTree::SetBranchAddress>: unknown branch -> jpuid_1

Warning in <AddReader>: Ignored branch againstElectronLooseMVA6_1 because a branch with the same name already exists. TTreeReader requires an unique name for the branches. You may need to put a dot at the end of the name of top-level branches.

And why don’t you use just a TSelector?

Dear ferhue,
thank you for investigating. These error message are just missing branch, it does not explain why it does not print the value, like pt_1 whose branch exist, am I wrong?
I want to use this code because I’m using it for some time, so I need to understand why it does not work on this tree, and so far I do not see any obvious reason.
Regards

Your code generated with the MakeClass command seems to be obsolete. Please regenerate it based on your imp.root file (opten that tree and call MakeClass).

I did that and it works for me. See my attachments.

./do.exe 
main(): nb variable double to plot: 8
main(): nb variable int to plot: 0

main(): processing imp.root
main(): output file: cern.root
main(): filling hist...
dir: mutau
main(): tree name: TauCheck
main(): sample: imp
main(): starting loop...
evt : pt_1 : eta_1 : phi_1 : pt_2 : eta2 : phi_2 
h2taus::Loop(): processing entry 0
76 : 65.1505 : -1.84253 : 1.20704 : 36.4669 : -0.400104 : -1.70799
h2taus::Loop(): processing entry 1
213 : 17.891 : -0.374609 : 2.60092 : 39.4656 : 2.02646 : 0.0865245
h2taus::Loop(): processing entry 2
255 : 18.6775 : -0.20042 : -2.02228 : 28.7926 : 1.04519 : 1.19276
h2taus::Loop(): processing entry 3
300 : 39.9389 : 1.86067 : -2.32755 : 35.9949 : 0.945962 : 1.90616
h2taus::Loop(): processing entry 4
309 : 33.9243 : -0.191569 : 2.4174 : 23.1623 : -1.21362 : -0.136111
h2taus::Loop(): processing entry 5
360 : 33.1739 : 2.18875 : 2.16775 : 13.8821 : 0.638515 : -1.0763
h2taus::Loop(): processing entry 6
401 : 32.2926 : 0.586046 : -2.79062 : 19.9837 : -0.0226565 : 0.513885
h2taus::Loop(): processing entry 7
529 : 157.623 : -0.890429 : 2.57245 : 34.4457 : -0.21516 : -2.17631
h2taus::Loop(): processing entry 8
540 : 57.5379 : -1.32813 : 1.55342 : 14.2308 : 0.0427513 : -0.741833
h2taus::Loop(): processing entry 9
449299 : 22.1281 : -1.68221 : -0.18459 : 36.2172 : -1.6251 : 2.83617
main(): end loop!!

h2taus.C (1.14 KB)
h2taus.h (25 KB)

The mistake was that when you generated your h2taus MakeClass code, your code was using pt_1 as Doubles. Then you changed your tree code and used pt_1 as floats, but probably forgot to regenerate your h2_taus class. Or, you used a class type which is platform-dependent. To be sure, use Float_t and Double_t

In other words, if you just replace all in your original code all your Double variables with floats, everything should run smoothly.

Remember to mark as solved (green tick), thanks

Dear ferhue,

thank you very much for your investigation and your time, but I still do not understand why casting a float in a double is problem for ROOT?

Regards

ROOT can cast floats to doubles, that is not the problem.

However, you stored a 4 byte float in your file. And then you try to read this file with an 8 byte double. As a consequence, you try to read 8 bytes from this file, but in reality you just stored 4 bytes, so you will (implicitly) read the 4 correct ones and some extra 4 bytes from another place in the file, and force to glue them into an 8-byte fake number (undefined behaviour).

Another different thing would be to read the 4 bytes as a float, and later cast it to a double, which is a well defined operation.

Float_t pt_1f;
//Read your file and branch, get entry
Double_t pt_1 = pt_1f;//This works without problem.

Dear ferhue,

ok, thank you for your clear answer. Everything work fine now.

Regards