SetBranchStatus for a vector of string


Please read tips for efficient and successful posting and posting code

_ROOT Version:6.21/01
_Platform: Ubuntu 18.04.4 LTS
_Compiler: gcc


Hi,

I am using root to copy a ttree with some columns dropped. Below is my code

void copytree()
{
   TFile f = TFile("higgsino_10_eos.root");
   TTree* oldtree = (TTree*) f.Get("UnnormedTree");
 
   // Deactivate all branches
   oldtree->SetBranchStatus("*", 0);

   	//keep only good_cols, remove blacklist
   	static const std::vector<std::string> blacklist = {"lep_core57","lep_coreCone","lep_pu_corr20","lep_pu_corr30","lep_pu_corr40","lep_pt_corr20","lep_pt_corr30","lep_pt_corr40"};
   	TObjArray* name_array = oldtree->GetListOfBranches();
	std::vector<string> good_cols;
	for(int i = 0; i < name_array->GetEntries(); ++i) 
	{ 
		auto value = name_array->At(i)->GetName();
		if(std::find(blacklist.begin(), blacklist.end(), value) != blacklist.end()){
			good_cols.push_back(value);
		}
	}
   // Activate only four of them
   	for(auto i =0; i < good_cols.size(); i++){
		auto activeBranchName = (const char *) &good_cols[i];
		oldtree->SetBranchStatus(activeBranchName, 1);
	}
   // Create a new file + a clone of old tree in new file
   TFile newfile("trial_delete_later.root", "recreate");
   auto newtree = oldtree->CloneTree();

   newtree->Print();
   newfile.Write();
}

The error shows

Error in <TTree::SetBranchStatus>: unknown branch -> ???]VU

Error in <TTree::SetBranchStatus>: unknown branch -> ???]VU

Error in <TTree::SetBranchStatus>: unknown branch -> Д?]VU

I know this is because the first argument of SetBranchStatus is const char* .

In my case, I only have a vector of string of branch name. How can I activate those branch?

Thanks!

instead of

you may have meant:

auto activeBranchName = good_cols[i].c_str();

Thanks so much! Now it runs successfully!

Best,

Kai

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