Re: TClonesArray

From: Axel Naumann <axel_at_fnal.gov>
Date: Mon, 07 Mar 2005 12:12:52 -0600


Hi Mario,
there's no need to copy the particles. Just add them to your TCLonesArray:

(*fpart)[m]=prt;

You can find a working example attached; running it with .L test.C+ works for me.

Axel.

Mario Kadastik wrote:
> Hello,
>
> I'm trying to filter pythia events before saving them to tree. Currently
> I get them like this:
> TClonesArray* particles = (TClonesArray*)pythia->GetListOfParticles();
> which I then save to tree like this:
> TTree* part = new TTree("parttree","A tree of particles");
> part->Branch("particles", &particles);
> for (Int_t i=0; i<nEvents; i++) {
> pythia->GenerateEvent()
> part->Fill();
> }
>
> What I'd like to do is to only take some of the particles that mach a
> specific KF code. What I tried to do:
> TClonesArray* particles = (TClonesArray*)pythia->GetListOfParticles();
> TClonesArray *fpart = new TClonesArray("TMCParticle");
> TClonesArray &fp = *fpart;
> TMCParticle *prt = new TMCParticle();
> Int_t fKF = 0, m=0;
> TFile* f = new TFile(file,"RECREATE"); // open file to write output tree
> TTree* part = new TTree("parttree","A tree of particles");
> part->Branch("particles", &fpart);
> for (Int_t i = 0; i < nEvents; i++) {
> fpart->Clear();
> pythia->GenerateEvent();
> m=0;
> for (Int_t j=0; j<particles->GetEntries(); j++) {
> prt=(TMCParticle*)particles->At(j);
> fKF=abs(prt->GetKF());
> if (prt->GetKS() < 20 && (fKF==5 || fKF==6 || fKF==9900041 ||
> fKF==9900042 || (fKF>10 && fKF<17))) {
> new ((*fpart)[m]) *prt;
> m++;
> }
> }
> part->Fill();
> }
> For some reason this doesn't work. I get segfaults at part->Fill(). If I
> added a cout << "added particle" << endl; just after new ((*fpart)[m])
> *prt; then
> it ran through a number of Fill()-s and then crashed.
>
> Mario
>
> PS! I did remove some cout << endl; and similiar lines so if it works
> now then I'd be surprised :)
>

#include "TPythia6.h"
#include "TClonesArray.h"
#include "TFile.h"
#include "TTree.h"
#include "TMCParticle6.h"

void test() {
TPythia6* pythia=TPythia6::Instance();
 pythia->SetupTest();
 pythia->Initialize("CMS","p","pbar", 1000.); const char* file="test.root";
const int nEvents=10;

TClonesArray *fpart = new TClonesArray("TMCParticle"); TMCParticle *prt = new TMCParticle();
Int_t fKF = 0, m=0;
TFile* f = new TFile(file,"RECREATE"); // open file to write output tree TTree* part = new TTree("parttree","A tree of particles"); part->Branch("particles", &fpart);
for (Int_t i = 0; i < nEvents; i++) {
  printf("event %d\n", i);
   fpart->Clear();
   pythia->GenerateEvent();
   m=0;
   TClonesArray* particles = (TClonesArray*)pythia->GetListOfParticles();    for (Int_t j=0; j<particles->GetEntries() && j<100; j++) {

     printf("  prt %d\n", j);
     prt=(TMCParticle*)particles->At(j);
     fKF=abs(prt->GetKF());
     if (j%2) {
          (*fpart)[m]=prt;
           m++;
     }

   }
   part->Fill();
 }
 part->Write();
 delete f;
} Received on Mon Mar 07 2005 - 19:39:47 MET

This archive was generated by hypermail 2.2.0 : Tue Jan 02 2007 - 14:45:05 MET