Copying events to new TFile

Howdy all,

I have a file, p7photons.root, which contains an ntuple with a bunch of event numbers (EvtEventId). I’m trying to loop through a TChain of reprocessed events, chain8, in order to pull out all the events that have the same event numbers as in the p7photons file. Unfortunately I’m clearly doing something wrong, but I’m not sure how to fix it.

Anyway, here’s the code:

void copy() {
	gROOT->Time();
	
	cout << " Load 7 ... "<< endl;
	TChain *chain7 = new TChain("MeritTuple");
	chain7->Add("p7photons.root");
	chain7->GetEntry(0);
	cout << " Load 8 ... "<< endl;
	TChain *chain8 = new TChain("MeritTuple");
	chain8->Add("v19r3p9/*.root");
	chain8->GetEntry(0);
	
	cout << " Creating new root-file ..."<< endl;
	TFile *newFile = new TFile("pass8p7photons.root","recreate");
	cout << " Creating new tree ..."<< endl;
	TTree *tree = (TTree*)chain7->GetTree()->CloneTree(0);
	TChain *newchain = (TChain*)chain7->CloneTree(0);
	TTree *tree = newchain->GetTree();
	
	cout << " Count entries ... "<< endl;
	Long64_t nentries7 = chain7->GetEntries();
	Long64_t nentries8 = chain8->GetEntries();
	
	cout << " Setting branch addresses ... "<< endl;
	id8 = chain8->GetBranch("EvtEventId");
	chain8->SetBranchAddress("EvtEventId",&EvtEventId);
	id7 = chain7->GetBranch("EvtEventId");
	chain7->SetBranchAddress("EvtEventId",&EvtEventId);
	
	cout << " Looping ... "<< endl;
	for (Int_t i = 0; i < nentries7; i++) {
		chain7->GetEntry(i);
		for (Int_t j = 0; j < nentries8; j++) {
		chain8->GetEntry(j);
		if ( id7 == id8 ) {
			tree->Fill();
			cout << j/nentries8 "%" << endl;
			return;
			}
		}
	}
	newFile->cd();
	tree->Write();
}

which then spits out the error:

Error: Symbol EvtEventId is not defined in current scope copy.c:26: *** Interpreter error recovered ***

The part that I don’t understand is that, as far as I can tell, I’m doing nearly the same thing in the following script, which works perfectly fine.

[code]void filter() {
TChain chain7 = new TChain(“MeritTuple”);
chain7->Add("P120/r02
.root");
chain7->GetEntry(0);

cout << " Creating new root-file ..."<< endl;
TFile *newFile = new TFile("p7photons.root","recreate");
cout << " Creating new tree ..."<< endl;
TTree *tree = (TTree*)chain7->GetTree()->CloneTree(0);
TChain *newchain = (TChain*)chain7->CloneTree(0);
TTree *tree = newchain->GetTree();

Long64_t nentries = chain7->GetEntries();
CTBParticleType = chain7->GetBranch("CTBParticleType");
chain7->SetBranchAddress("CTBParticleType",&CTBParticleType);

for (Int_t i=0; i<nentries;i++) {
	chain7->GetEntry(i);
	if ( CTBParticleType==0 ) tree->Fill();
}
newFile->cd();
tree->Write();

}
[/code]

Any help would be most appreciated.

Thanks in advance.

[quote] CTBParticleType = chain7->GetBranch(“CTBParticleType”);
chain7->SetBranchAddress(“CTBParticleType”,&CTBParticleType);[/quote]is wrong! The address expected by SetBranchAddress is a location where to stored the data in the branch and not the address of the branch (any way it obviously already know that part :slight_smile:).

[quote] id8 = chain8->GetBranch(“EvtEventId”);
chain8->SetBranchAddress(“EvtEventId”,&EvtEventId);[/quote]is different from the above code in the name of the variable (i.e EvtEventId is really never defined).

What you need in both case is: SimpleTypeOfTheContentOfTheBranch content; chain->SetBranchAddress("branch_name_with_simple_type",&content); TypeOfTheContentOfTheBranch content_ptr = 0; chain->SetBranchAddress("branch_name_with_object_type",&content_ptr);where you replace TypeOfTheContentOfTheBranch by the correct C++ type (I am guessing for EvtEventId it is likely to be a simple type and either long or int).

Cheers,
Philippe.
Cheers,
Philippe.