Substituting values in a leaf of a tree

Dear root experts,

I am trying to write a code which (i) preserves the structure of an existing tree, and (ii) substitutes the very content of a leaf depending on its content. Namely in (ii), when a variable has the value -999., I want to fill that same leave with the value -10, but also not write again the events with value -999; for those events the value -999., I simply want to replicate the leaf/tree. The closest piece of code I could write to this end is the following.

void branching_rootfile(string data[], string branches[], const char* dir) {
    //Selecting specified branches from 
    int i = 0;
    int n_data = array_size(data);
    int n_branches = array_size(branches);
    for (i = 0; i < n_data; i++){
      //Loading root data
      string filename = (string(dir) + "tmp_" + (data[i]).c_str());
      const char * c1 = filename.c_str();
      TFile oldfile(c1, "READ");
      TTree* tmp_tree = static_cast <TTree*>(oldfile.Get("bdttree"));

      //Muting unselected branches
      tmp_tree->SetBranchStatus("*",0);
      int j=0;
      for(j=0; j<n_branches; j++){
        const char * c2 = branches[j].c_str();
        tmp_tree->SetBranchStatus(c2, 1);
      }

      //Writting new tree in a new root file 
      string file_out = (string(dir) + data[i]);
      const char * c3 = file_out.c_str();
      TFile newfile(c3, "RECREATE");
      TTree* branched_tree = tmp_tree->CloneTree();

      // Making the substitution
      const auto nentries = tmp_tree->GetEntries();
      float Jet2Pt;
      tmp_tree->SetBranchAddress("Jet2Pt", &Jet2Pt);
      int iev = 0;
      for (iev; iev < nentries; iev++) {
        tmp_tree->GetEntry(iev);
        if (Jet2Pt == -999.){
          Jet2Pt = -10.;
          branched_tree->Fill();
        }
      }
      //branched_tree->Clear();
    
      branched_tree->Write();
      //Deleting temporary file
      remove(c1);
    }
    }

I myself realize that when cloning the tree, those events where the Jet2Pt leaf has the value -999. is preserved, thus leading to my problem: a fraction of events are written with Jet2Pt having the value -10., but the original events where it has the value -999. are still there.

Am I by definition limited when using CloneTree() ? If so, what do-you see as alternative ?

Thanks in advance, Pedram.

Is an explicit request to clone the structure and copy the entries.
You meant:

TTree* branched_tree = tmp_tree->CloneTree(0); // Clone just the structure

It simply did. Since this option is just cloning the structure, I had to be specific in the code as to fill events for all cases:

for (iev; iev < nentries; iev++) {
  tmp_tree->GetEntry(iev);
  if (Jet2Pt == -999.){
    Jet2Pt = -10.;
    branched_tree->Fill();
  }
  else {branched_tree->Fill();}
}

Merci beaucoup !