Re: TClonesArray

From: Maxim Nikulin <nikulin_at_gorodok.net>
Date: Sun, 13 Mar 2005 23:07:50 +0600


Dear Axel,

On March, 8 you wrote:

> Hi Mario,
> there's no need to copy the particles. Just add them to your TCLonesArray:
> 
> (*fpart)[m]=prt;

Axel, can you explain the statement from comments to the TObject *&TClonesArray::operator[](Int_t idx) method:

http://root.cern.ch/root/htmldoc/src/TClonesArray.cxx.html ...

    // Return pointer to reserved area in which a new object of clones
    // class can be constructed. This operator should not be used for
    // lefthand side assignments, like a[2] = xxx. Only like,
    // new (a[2]) myClass, or xxx = a[2].
...

By the way, this operator is described as inline function on the page http://root.cern.ch/root/htmldoc/TClonesArray.html I think there is a problem with THtml...

Thank you

-- 
Maxim Nikulin

> 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 :)
>>
Received on Sun Mar 13 2005 - 18:07:40 MET

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