Re: problems with deleting TObjArray

From: Rene Brun (Rene.Brun@cern.ch)
Date: Tue Mar 07 2000 - 14:51:23 MET


Hi Mattias,
It is hard to follow what you want to do. You are declaring many times
teh same objects in the loop.
I have simplified your logic in this small test and running example.
To execute it with standard Root, do
 Root > gSystem->Load("libPhysics");
 Root > .x mattias.C

Rene Brun

void mattias() {
   
  TObjArray* veclist = new TObjArray();
  TVector3 vec3;
        
  int events=20;
  for (long iev=0;iev<events;iev++) {
    veclist->Delete();
    int N= gRandom->Gaus(100,20);
    for(int i=1; i<=N; i++) {
          vec3.SetX(gRandom->Gaus(0,10));
          vec3.SetY(gRandom->Gaus(0,10));
          vec3.SetZ(gRandom->Gaus(0,100));
          Float_t  E_part = 120;
          vec4 = new TLorentzVector(vec3,E_part);
          veclist->Add(vec4);
      }
   }
}



Mattias Davidsson wrote:
> 
> Hi!
> 
> I have a pointer to a TObjArray for a list of particles (4 vectors)
> 
> The code can be found below. Now the problem is that I can in no way
> delete the TObjArray inside the main event loop so as to use new
> particles for every event (sounds logical he?)
> 
> The routine main.cc you can see in attachement. I'm quite new to this
> but can not understand why inserting the veclist->Delete; (or Clear)
> inside  the main event loop does not work.
> 
> cheers!
> 
> /mattias
> 
> --
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> Mattias Davidsson,   http://www.quark.lu.se/~mattias
>  +46 (0)46 222 76 83
> Department of elementary particle physics
> Box 118 SE-221 00
> Lund, Sweden
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> 
>   ------------------------------------------------------------------------
> #include <stdlib.h>
> #include <iostream>
> #include <cmath>
> #include <fstream>
> #include <list>
> #include <string>
> #include <vector>
> #include "Pythia.h"
> #include "LorentzVector.h"
> #include "JetFindDict.h"
> 
> //
> //  This program produces DIS events from pythia, adds the stable particles
> //  to a TObjArray (veclist). The aim is then to feed these 4-vectors
> //  to the jetfinder package. See bottom for troubles.
> //
> 
> TROOT demo("demo","my first Root program");
> 
> int main() {
> 
>     const char* frame="CMS";
>     int iframe=3;
>     const char* beam="gamma/e+";
>     int ibeam=8;
>     const char* target="p+";
>     int itarget=2;
>     double E=297.2E0;
> 
>     double y_cut=0.1E0;
> 
>     // Uncomment below for e+ e-
>    //const char* frame="CMS";
>    //int iframe=3;
>    //const char* beam="e+";
>    //int ibeam=2;
>    //const char* target="e-";
>    //int itarget=2;
>    //double E=91.2E0;
> 
>    //double y_cut=0.E0;
> 
>   //  Choose jet finding scheme. Jade with y_cut
>   pydat1_.mstu[46-1]=4;      // MSTU(46)=4
>   pydat1_.paru[45-1]=y_cut; // PARU(45)=y_cut
> 
>   //  Disable preclustering and don't ignore neutrinos
>   pydat1_.paru[43-1]=0.E0;  // PARU(43)=0
>   pydat1_.mstu[41-1]=1;     // MSTU(41)=1
> 
>   // Setup ROOT jet finder
> 
>   JadeJetFinder  *jetf = new JadeJetFinder(0.0005);
>   //DurhamJetFinder   *jetf = new DurhamJetFinder(0.004); // Durham algorithm
>   //  IktJetFinder   *jetf = new IktJetFinder(0.01,'I'); // Inclusive kt algorithm
> 
>   // Select DIS process in PYTHIA.
>   pysubs_.msel=1;       // MSEL=0
>   pysubs_.msub[1-1]=1;  // MSUB(1)=1
> 
>   //Initialize Pythia
>   pyinit_(frame,beam,target,&E,iframe,ibeam,itarget);
> 
> 
>   // Declare 4vector list a'la ROOT
>   TObjArray* veclist = new TObjArray();
>   if (veclist) veclist->Delete();
> 
> 
>   // Main event loop
>   int events=20;
>   for (long iev=0;iev<events;iev++){
> 
>     //  if (veclist) veclist->Clear();
>     //Declare the 4 vector a'la ROOT
>     TLorentzVector* vec4 = new TLorentzVector();
>     if(vec4) delete vec4;
> 
>     // Declare a 3 vector a'la ROOT
>     TVector3* vec3  = new TVector3();
>     if(vec3) delete vec3;
> 
> 
>     // why do I get a segmentation fault when declaring these here???
>     //      TObjArray* veclist = new TObjArray();
>     //      if(veclist) veclist->Delete();
> 
>     //      TVector3* vec3  = new TVector3();
>     //      TLorentzVector* vec4 = new TLorentzVector();
>     //      if(vec3) delete vec3;
>     //      if(vec4) delete vec4;
> 
>     Float_t Pvec3[3];
> 
>     pyevnt_();
>     long nlist=2;
>     if (iev < 3) pylist_(&nlist);
> 
>     int N=pyjets_.n;
>     for(int i=1; i<=N; i++) {
>       if (pyjets_.k[1-1][i-1]==1 && pyjets_.k[2-1][i-1]!=22) {  // Stable particle (not photon)
>         if (pyjets_.k[2-1][i-1]!=-11 && pyjets_.k[2-1][i-1]!=2212) { // not the electron or proton
> 
>           Pvec3[0] = pyjets_.p[1-1][i-1];
>           Pvec3[0] = pyjets_.p[2-1][i-1];
>           Pvec3[0] = pyjets_.p[3-1][i-1];
>           Float_t  E_part = pyjets_.p[4-1][i-1];
>           vec3 = new TVector3(Pvec3);
>           vec4 = new TLorentzVector(*vec3,E_part);
>           veclist->Add(vec4);
>           delete vec3;
> 
>         }
>       }
>     }
>     // Perform Jet finding
>     jetf->setEvent(veclist);
>     jetf->setYCut(0.0003);
>     jetf->doFindJets();
> 
>     std::cout << "No'f jets: " << jetf->njets() << std::endl;
> 
>     //int Njet=0;
>     //pyclus_(&Njet);
>     //std::cout << "Pythia: " << Njet << std::endl;
> 
>     // print jet quantities
> 
>     //delete veclist;
>     // if (veclist) {veclist->Delete();
>     //delete veclist;}
>     //    event->Clear();
>     //  event->Clear();
>     std::cout << "even #: " << iev << std::endl;
>   }
> }



This archive was generated by hypermail 2b29 : Tue Jan 02 2001 - 11:50:21 MET