sorting objects in a TObjArray

From: chiara zampolli <Chiara.Zampolli_at_bo.infn.it>
Date: Sun, 30 Oct 2005 14:36:11 +0000


Dear all,

    I would like to sort the objects of a TObjArray, the element of which are instances of the AliTOFUncalib class. What I do is:

...

    TObjArray *UCdata=new TObjArray(10);     for (Int_t itrk=0; itrk<10; itrk++) {

        AliESDtrack *t=event->GetTrack(itrk);                //read from 
an event
        AliTOFUncalib *ciccino = new AliTOFUncalib;         
        ciccino->CopyFromAliESD(t);
        UCdata->Add(ciccino);
     }

    Int_t entries = UCdata->GetEntries();     qsort(UCdata,entries,sizeof(AliTOFUncalib*),cmp);
...

where the function cmp is defined as follows:

static int cmp(const void *p1, const void *p2) {

    AliTOFUncalib *u1=*((AliTOFUncalib**)p1); //I'm not sure of this....

    cout << " qui ok, u1 = " << u1 << endl;     Float_t t1 = u1->GetTOFsignal();
    cout << " t1 = " << t1 << endl;
    AliTOFUncalib *u2=*((AliTOFUncalib**)p2);     cout << " qui ok, u2 = " << u2 << endl;     Float_t t2 = u2->GetTOFsignal();
    cout << " t1 = " << t1 << ", t2 = " << t2 << endl;     if (t1<t2) return -1;
    if (t1==t2) return 0;
    return 1;
}

In this way, the sorting does not work at all (the session crashes), and, moreover, it seems as if nothing actually is passed to the function cmp (as the nonsense printing of t1 -a very high number- make me believe). I do not know whether this is a problem of the TObjArray (are its element consecutive in memory?) or of the way I pass the elements of the array, and use them in cmp.

      Can you help me?
      Thank you in advance.
      Cheers,
            chiara
Received on Sun Oct 30 2005 - 14:36:13 MET

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