Troubles sending data from a Tree to a new Tree

Hi, Rooters
I need to manipulate specific data from a huge Tree file to be add in other new Tree, next are steps I do:

  1. Read the Tree file.
  2. Relate Tree Branch with variables I want.
  3. Make new Tree (TreeDataMOD) and add branch.
  4. Define loop for select specific data (where GetPMT and GetTank are functions previously declared)
  5. Fill new Tree and write it.

Trouble is: When I browse new Tree, data added to branch TimeA , ChannelA ,ChargeA is wrong, mainly in branch ChannelA where appear some values near zero (rare) and others where it should be(near value: 400, see in image). However in branch TimeD , ChannelD,ChargeD values are OK. I have checked data entering to A’ branches as well as D branches and are good, so I don’t understand why is this happening.
Please I’ll appreciate any help. I attach images for better understanding.

TFile *f = TFile::Open("/home/ariel/Escritorio/Data.root");
 TTree* hits;
 f->GetObject("hits", hits);

TFile *Dfile = new TFile("/home/ariel/Escritorio/TreeDataMOD.root", "RECREATE");
TTree *TreeDataMOD = new TTree("TreeDataMOD");

TreeDataMOD->Branch("ChargeA",&ChargeA, "ChargeA/D");
TreeDataMOD->Branch("ChannelA",&ChannelA, "ChannelA/i");

TreeDataMOD->Branch("ChargeD",&ChargeD, ChargeD/D);
TreeDataMOD->Branch("ChannelD",&ChannelD, "ChannelD/i");

	for ( int i=0 ; i<hits->GetEntries() ; i++){

	hits-> GetEntry(i);

	if ( GetTank(iChannel)!=100) continue;
							if (GetPMT(iChannel) == 0){ 
							TimeA = iTime;
							ChannelA = iChannel;
							ChargeA = iCharge;
						if (GetPMT(iChannel) == 3){ 
							TimeD= iTime;
							ChannelD = iChannel;
							ChargeD = iCharge;

If “GetPMT(iChannel) == 0” you initialize TimeA, ChannelA, ChargeA, but you do NOT initialize TimeD, ChannelD, ChargeD.
If “GetPMT(iChannel) == 3”, you initialize TimeD, ChannelD, ChargeD, but you do NOT initialize TimeA, ChannelA, ChargeA.
In all other cases (except when “GetTank(iChannel)!=100”), you do NOT initialize any of these variables (they will have values which they got in some previous events).
Each call to “TreeDataMOD->Fill();” will always store ALL variables (i.e. you usually store “trash” in your new tree).

Hello, yes I initialized all that variables at the beginning of macro, also reset its after fill, but nothing, still appearing rare values.
All variables has same type, according to values they store.

Double_t TimeA=0,TimeB=0,TimeC=0,TimeD=0; Double_t ChargeA=0,ChargeB=0,ChargeC=0,ChargeD=0; UInt_t ChannelA=0,ChannelB=0,ChannelC=0,ChannelD=0;

Right before the line:
hits-> GetEntry(i);
add a line:
then observe how many times you will see 999 in your new tree.

Hello, Thanks for your reply I found the problem
this line: TreeDataMOD->Fill(); before finish the macro, is filling all variables even when conditions are not true, so solution is to fill inside conditions with Branch->Fill(), but I found other problem my tree’s branches has unequal number of entries, so when I run program appear warnings and data in branches are incorrect. What can I do, to built independent trees and link all with TChain?


but I found other problem my tree’s branches has unequal number of entries,

This is very likely to make it (almost) impossible to read the data back correctly.

You can either create two separate TTree which would contains the data and (in both TTree) a branch or two containing an indentifier for the even (often a Run number and Event Number).

Alternatively, you can replace the double by vector which you would fill with either 0 or 1 value each.