Hi Tommaso, I think the problem comes from your filling, since: TClonesArray* *vUp is an array of pointers to TClonesArrays, so that you access a POINTER to a TClonesArray with vUp[i], but access (a pointer to) a member of the TClonesArray with vUp[i]->[contau[i]] When you tried vUp[i][j], this was treating the pointer to the TClonesArray as the index in an array of TClonesArray's, and so the further dereference was attached to some spot in memory. So, pulling the code you mentioned together, one gets something like: TClonesArray* *vUp = new TClonesArray*[ntotal_zone]; TClonesArray* *vDown = new TClonesArray*[ntotal_zone]; int *contau = new int[ntotal_zone]; int *contad = new int[ntotal_zone]; TMatrix *notru = new TMatrix(ntotal_zone,neventUp); TMatrix *notrd = new TMatrix(ntotal_zone,neventDown); for(int in =0;in<ntotal_zone;in++) // initialization of all the TClonesArrays { vUp[in]= new TClonesArray("Track",neventUp); vDown[in]= new TClonesArray("Track",neventDown); contau[in]=0; contad[in]=0; } .... // time for filling, using new with placement new(vUp[nozone]->[contau[nozone]]) // <== dereference with -> first Track(trUp->x(),trUp->y(),trUp->a(),trUp->b(),trUp->pol()); Good luck, Rob Feuerbach On Wed, 21 May 2003, Tommaso Chiarusi wrote: > Date: Wed, 21 May 2003 19:41:18 +0000 > From: Tommaso Chiarusi <ss11002@arpat.toscana.it> > To: RootTalk <roottalk@pcroot.cern.ch> > Subject: [ROOT] array of TClonesArray > > Hello. > I have a script which works under cint. > I need to convert it into a compiled executable. > I become aware that cint automatically cures all the C++ venial sins in > the code... > Unfortunately, my g++ compilator feels such sins become as unforgiven. > > I try to cure my mistakens, so I managed to compile, > But I get Segmentation fault. > > I am quite sure that my problem IS NOT on some include files or > libraries which may lack. > It is a problem of segmentation, so it should concern the usage of > pointers. > > The problem is when I try to fill and then read an > "array of TClonesArray" > > > Conceptual sketch: > Imagine a detector subdivided into several zones. > The number of interesting zone is variable, say "ntotal_zone" > > For each zone I have several tracks. > So I need to make "ntotal_zone" collection of tracks. > I decided to collect the tracks of each zone by means of a TClonesArray. > So, I find myself with an array of TClonesArray. > > In the cint-script I use the following (!uncorrect! but working syntax) > > TClonesArray *vUp = new TClonesArray[ntotal_zone]; > TClonesArray *vDown = new TClonesArray[ntotal_zone]; > > int *contau = new int[ntotal_zone];// vectors which counts the > number of tracks for each zone > int *contad = new int[ntotal_zone]; > > TMatrix *notru = new TMatrix(ntotal_zone,neventUp); > TMatrix *notrd = new TMatrix(ntotal_zone,neventDown); > > > for(int in =0;in<ntotal_zone;in++) // initialization of all the > TClonesArrays > { > vUp[in]= new TClonesArray("Track",neventUp); > vDown[in]= new TClonesArray("Track",neventDown); > contau[in]=0; > contad[in]=0; > } > > Then when I fill I use the following (whithin a loop on the total > number of the zones - > nozone is the number of the currently analysed zone, let's call this > procedure as (p.1) ): > > > new(vUp[nozone][contau[nozone]]) > Track(trUp->x(),trUp->y(),trUp->a(),trUp->b(),trUp->pol()); > > > Note the array-form of the TClonesArray: > vUp[nozone] ....... should give the pointer to a TClonesArray > vUp[nozone][contau[nozone]] ....... should give the component of such > pointer > > > > > When trying to convert everithing into a g++ stand-alone executables, I > noticed that I was using a unforgiven syntax and I changed it by > using the following: > > TClonesArray* *vUp = new TClonesArray*[ntotal_zone]; > TClonesArray* *vDown = new TClonesArray*[ntotal_zone]; > > ... i.e. as a list of pointers to TClonesArray, which are initialized as > the same as in the cint-script. > > > The filling procedure should remain the same as (p.1). If I do this the > compiler prompt out the following errors > > Analisi_match3.C:321: no matching function for call to `Track::operator > new (unsigned int, TClonesArray &)' > /home/applicazioni/root/include/TObject.h:153: candidates are: static > void *TObject::operator new > (unsigned int) > /home/applicazioni/root/include/TObject.h:154: static > void *TObject::operator new > (unsigned int, void *) > > Instead, if I write (note the "&" in the argument of "new()" ) > > new(&vUp[nozone][contau[nozone]]) > Track(trUp->x(),trUp->y(),trUp->a(),trUp->b(),trUp->pol()); > > I get the program to compile, but when I start to iterate through the > TClonesArray, the program crashes dumping a sementation fault. > > What's wrong? > > My system is > > Toshiba Satellite 1800 LapTop, > Linux Mandrake 8.1 > Root Version 3.03/09 18 September 2002 > gcc version 2.96 20000731 (Mandrake Linux 8.1 2.96-0.62mdk) > > Thank you very much > > Tommaso > > -- /*************************************************** * Robert Feuerbach feuerbac@jlab.org * * Jefferson Lab CEBAF Center A120 * * 12000 Jefferson Avenue Office: (757) 269-7254 * * Mail Stop 12H Page: 584-7254 * * Newport News, VA 23606 Fax: (757) 249-1965 * ***************************************************/
This archive was generated by hypermail 2b29 : Thu Jan 01 2004 - 17:50:11 MET