Fill all branches of a tree separately (because every branch has to satisfy different conditions)

Dear experts,

I am trying to fill a tree ( which has with more than one branch) that I created. Now, the problem is that, the variables to be stores in these branches need to satisfy different conditions. Which means that I cannot simply create new branches and fill the tree once inside the entries loop as shown below:

t2->Branch(“d0_kpipi0”,&d0_kpipi0,“d0_kpipi0/D”);
t2->Branch(“d0_e”,&d0_e,“d0_e/D”);
t2->Branch(“d0_kpi”,&d0_kpi,“d0_kpi/D”);
t2->Branch(“d0_mu”,&d0_mu,“d0_mu/D”);

// Loop again over candidates
for (Long64_t i=0; i<nentries; i++){
tree->GetEntry(i);
if(…)

t2->Fill();
}


Please read tips for efficient and successful posting and posting code

_ROOT Version:_5.34
Platform: Linux (Ubuntu16.04)
Compiler: Not Provided


How should I modify the macro so as to fill the branches (each with a different criteria).

Regards,
Sanjeeda

TBranch *b_d0_kpipi0 = t2->Branch("d0_kpipi0", &d0_kpipi0, "d0_kpipi0/D");
TBranch *b_d0_e = t2->Branch("d0_e", &d0_e, "d0_e/D");
// ...
// Loop again over candidates
for (Long64_t i = 0; i < nentries; i++) {
  tree->GetEntry(i);
  if (... candidate is not good ...) continue; // skip this "tree" entry
  // warning: below, you MUST make sure that every branch is filled
  //          exactly once for every good "tree" entry (so that later,
  //          all branches will have exactly the same number of entries)
  if (...) {
    b_d0_kpipi0->Fill();
  }
  if (...) {
    b_d0_e->Fill();
  }
}

Dear @Wile_E_Coyote,

Thanks a lot for your response. Here’s the macro that I prepared d0pi_d0_bkg.c (3.7 KB) . I get all the branches but I am still not able to get the required distributions.

Is there still some thing wrong with the macro?

You do not follow the requirement that every (newly created) branch is filled (exactly once) for every good “tree” entry (so that all new branches will have exactly the same numbers of entries).

It seems to me that you want to create histograms of distributions, not a new tree.

TFile *f1 = new TFile(outfilename, "RECREATE");
// histograms ("owned" by the opened above file) with 1000 "automatic bins"
TH1D *h_d0_kpipi0 = new TH1D("h_d0_kpipi0", "D^{0}_{} #rightarrow K #pi #pi^{0}_{};m;entries", 1000, 0., 0.);
TH1D *h_d0_e = new TH1D("h_d0_e", "D^{0}_{} #rightarrow e;m;entries", 1000, 0., 0.);
// ...
// Loop again over candidates
for (Long64_t i = 0; i < nentries; i++) {
  tree->GetEntry(i);
  if (... candidate is not good ...) continue; // skip this "tree" entry
  if (...) {
    h_d0_kpipi0->Fill(DzPi_M);
  }
  if (...) {
    h_d0_e->Fill(Dz_M);
  }
  // ...
}
f1->Write(); // save all "owned" histograms
delete f1; // automatically deletes all "owned" histograms, too

Dear @Wile_E_Coyote,

I wanted to create branches and not histograms. I could not do it for all the branches together, but I could do it for wow at a time. Here’s the macro. kpipi0.c (1.6 KB)

The sequence:
tree_branch->Fill(); tree->Fill();
fills the branch twice with the same event so, you should remove one of these calls (it does not matter which one).

BTW. Instead of f1->Close(), use:

delete f1; // automatically deletes "t1" and "t2", too
delete f; // automatically deletes "tree", too

…and for branches representing values that do not exist in all cases, you can simply store a boolean to signal “that value is valid”, and then don’t call TBranch::Fill() but TTree::Fill()