Problem with selecting tracks in the particle stack

Dear Rooters,

I have a problem with looping over tracks in the stack. I just want to select tracks from the stack only one time and add this track to TObjArray selParticles object. If there are more than one track has the same label in the stack, I want to select the one track with the smallest chi^2 value and put it in selParticles and other (with same label tracks) tracks should go to rejParticles. I make following macro for this purpose,

AliStack *stack = mcEvent->Stack();

Int_t ntracks = fESD->GetNumberOfTracks();

TObjArray selParticles;
TObjArray rejParticles;

Int_t i1, i2,i3, lab1, lab2;
Bool_t *accept = new Bool_t[ntracks];

 for (i1 = 0; i1 < ntracks; i1++) {
    accept[i1] = kTRUE;
    AliESDtrack *trk1 = fESD->GetTrack(i1);
lab1 = TMath::Abs(trk1->GetLabel());

       for (i2 = i1+1; i2 < ntracks; i2++) {
          AliESDtrack *trk2 = fESD->GetTrack(i2);
          lab2 = TMath::Abs(trk2->GetLabel());
        
            // check if labels are equal
              if (lab1 == lab2) {
                   if (trk1->GetConstrainedChi2() < trk2->GetConstrainedChi2()) {
                     accept[i1] = kTRUE;
                     accept[i2] = kFALSE;     
                     } else {
                    accept[i1] = kFALSE;
                    accept[i2] = kTRUE;         
                     }
       
              } // for if

   } // for i2

} //for i1

for (i3 = 0; i3 < ntracks; i3++) {
AliESDtrack *trk3 = fESD->GetTrack(i3);

if(accept[i3]==kTRUE)
{
	selParticles.Add(trk3);	
}
if(accept[i3]==kFALSE)
{
	rejParticles.Add(trk3);
}

} //for i3

When I print the results some tracks counted more than one time in selParticles, following values are part of my result,

Stackpart label=1854 chi^2=0.000000
label=1854 chi^2=0.000000
label=1854 chi^2=0.000000
label=1854 chi^2=0.158253
label=1854 chi^2=42.000000
label=1854 chi^2=0.000000
label=1854 chi^2=3.531250

SelPart label=1854 chi^2=0.000000
label=1854 chi^2=0.000000
label=1854 chi^2=0.000000
label=1854 chi^2=0.158253

RejPart lab=1854 chi^2=42.000000
lab=1854 chi^2=3.531250

But the result SelPart should include only one time lab=1854 chi^2=0.000000 track.

I don’t know where I’m wrong. Could you please help me to solve this problem.

Thank you,

Kind regards to All,

Ayben

Hi,

The logic/algorithm in the loop is incorrect. I think you meant something more like:[code]for (i1 = 0; i1 < ntracks; i1++) {
accept[i1] = kTRUE;
}
for (i1 = 0; i1 < ntracks; i1++) {
if (!accept[i1]) {
// Track already rejected, let’s move on
continue;
}
AliESDtrack *trk1 = fESD->GetTrack(i1);

lab1 = TMath::Abs(trk1->GetLabel());
int min_i2 = i1;
AliESDtrack *mintrk = trk1;
for (i2 = i1+1; i2 < ntracks; i2++) {
if (!accept[i2]) {
// Track already rejected, let’s move on
continue;
}
AliESDtrack *trk2 = fESD->GetTrack(i2);
lab2 = TMath::Abs(trk2->GetLabel());

  // check if labels are equal 
  if (lab1 == lab2) { 
      if (mintrk->GetConstrainedChi2() > trk2->GetConstrainedChi2()) {
         accept[min_i2] = kFALSE;
         min_i1 =  i2;
         mintrk = trk2;
      } else {
         accept[i2] = kFALSE;
      }
  } // for if lab1==lab2

} // for i2
} //for i1 [/code]

Cheers,
Philippe.

PS. Your previous algorithm accept (at least) all the Tracks which have a lower Chi2 than the tracks with higher indices.