About Loop Over Several ROOT Files

Dear Rooters,

I want to make loop over 3 root files which name are Kinematics_1.root, Kinematics_2.root and Kinematics_3.root. Each files contain 100 event folder and each folder contain TreeK and under this there is a Particles branch. I want to access the branch Particles. I started to loop over root files. I define a char “filename” for this purpose. And then I try to access branch. If I have only one root file, I can do my analysis properly. But when I add other root files I encounter an error;

“Error: class, struct, union or type Key not defined.”
and
"Error: Incorrect assiggnment to key, wrong type ‘TObject*’

But I defined Key in my code. This error only occures when I try to use more than one root file.

I don’t know where am I wrong. I attached the code for you. Could you please help me to solve this problem.

Thank you very much and kind regards.

ps: I am using root version 5.16.00 and windows xp
charged_primary_dndeta.C (1.14 KB)

Your code contains far too many errors to give a meaningful answer (see some comments below). I strongly suggest to contact the person(s) who created your file/Tree to get more help.

Rene

[code]
{
TH1F *hEta = new TH1F(“heta”,“Eta Histogram”,100,-6,6);
TKey *key;
char *str2 = “/TreeK”;

char filename[100];
for(Int_t j=1; j<4; j++) {
sprintf(filename,“Kinematics_%d.root”,j);

  TFile *f = new TFile(filename,"read");

  TIter next(f.GetListOfKeys()); 

  while (key = (TKey*)next()) { //<====was Key
     TString t1 = TString(key->GetName())+TString(str2);
 TTree   *tree  = (TTree*)f->Get(t1);//Tree
 TBranch *branch = tree->GetBranch("Particles");//Branch
 TParticle *particles;
 //Where do you declare chain ?
     //What is Particles?
     chain.SetBranchAddress("particles",&Particles);

 //Are you sure that the branch contains directly One TParticle
     //or a TClonesArray of TParticle?
     branch->SetAddress(&particles);
 Long64_t nentries = chain->GetEntries();
 cout<<"Entries = "<<nentries<<endl;
 Long64_t nbytes = 0;
 for (Long64_t i=0; i<nentries;i++) {
    branch->GetEntry(i);
						
        TParticlePDG* pdg=particles->GetPDG();
    if(particles->IsPrimary()) {
       Int_t charge = pdg->Charge();
       if(charge !=0) {
	  cout<<"charge= "<<charge<<endl;	
	  cout<<"Eta() = "<<particles->Eta()<<endl;
	  hEta->Fill(particles->Eta());	
           }
        } 
 }
  }

}

hEta->Draw();
}[/code]

Dear Rene,

Thank you for your help. I solved my problem.

Cheers.

Dear Ayben,

How did you solve your problem? I find myself having a problem quite similar to yours…

I could copy my code but in essence: I loop over rootfiles, open them up, check for a value and close. However first loop runs like a charm, second gives me the same error as you have!

Kind Regards Steven

Hi,
this topic is 10 years old. Please open a new one describing in detail what you have attempted (e.g. by providing a minimal snippet of code that reproduces the issue). I am closing this topic.