Old Tree branch to New Tree Branch

Hi Rooters,

I am trying to understand while creating a simplest case as follows;
input file → Tree do have several branches, say one branch origin_x;
outout file → Creating a new tree with a new branch calling same data ‘origin_x’ store in a new variable ‘xnew’;
Comparison is strange.
Code is as follows;

Problem: comparing new and old branch data should be same but it is not’
Can you please review if I am missing something.

void th()

/*************FILE INPUT READ***************/	
	TFile *f = new TFile("test.root");
	auto *t = (TTree*)f->Get("tree");
	Float_t xpre;
        t->SetBranchAddress("origin_x", &xpre);

/**********FILE OUTPUT CREATE ************/       
       auto hfile = new TFile("newtest.root", "RECREATE");
       Double_t xnew;
       TTree* simul = new TTree("cool", "Tree1");
       simul->Branch("xnew", &xnew, "xnew/D" );
/***************BEGIN LOOP********************/       

        Long64_t numEntries = t->GetEntries();
        for (int i; i< numEntries;i++){
         xnew=origin_xpre;   // Testing if data call is same


    // Clean up
    delete f;
    delete hfile;	


output → from test.root

output from newtest.root

Your variable in the loop should be xpre, not origin_xpre, and you are not getting the event or entry of the original tree in the loop, so origin_xpre (xpre) is always zero.

Thanks @dastudillo . I am sorry for the typo mistake. But still the problem exists.
Nothing changed. I am stuck with this weird step. Unable to understand whats wrong with this. I need such method to write my post processing script. But I stopped on this very first step. Can you please help me more.


Did you do that?

Thanks so much for pointing. I missed it but I learn it.
Problem solved.

For future root users if similar problem happens than here a solution;

for (int i; i< numEntries; i++){
         t->GetEntry(i),     // This was missing "MOST IMPORTANT"
         xnew=xpre;   // Testing if data call is same

Thanks @dastudillo

one other information is Long64_t inplace of int.


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